#include "config.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <sys/stat.h>
#include <dirent.h>
#include <regex.h>
#include <time.h>
#include "db.h"
#include "alpm_list.h"
#include "log.h"
#include "util.h"
#include "error.h"
#include "server.h"
#include "handle.h"
#include "cache.h"
#include "alpm.h"
pmdb_t SYMEXPORT *alpm_db_register_sync(const char *treename)
{
ALPM_LOG_FUNC;
ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, NULL));
ASSERT(treename != NULL && strlen(treename) != 0, RET_ERR(PM_ERR_WRONG_ARGS, NULL));
ASSERT(handle->trans == NULL, RET_ERR(PM_ERR_TRANS_NOT_NULL, NULL));
return(_alpm_db_register_sync(treename));
}
pmdb_t SYMEXPORT *alpm_db_register_local(void)
{
ALPM_LOG_FUNC;
ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, NULL));
ASSERT(handle->trans == NULL, RET_ERR(PM_ERR_TRANS_NOT_NULL, NULL));
return(_alpm_db_register_local());
}
static void _alpm_db_unregister(pmdb_t *db)
{
if(db == NULL) {
return;
}
_alpm_log(PM_LOG_DEBUG, "closing database '%s'\n", db->treename);
_alpm_db_close(db);
_alpm_log(PM_LOG_DEBUG, "unregistering database '%s'\n", db->treename);
_alpm_db_free(db);
}
int SYMEXPORT alpm_db_unregister_all(void)
{
alpm_list_t *i;
ALPM_LOG_FUNC;
ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
ASSERT(handle->trans == NULL, RET_ERR(PM_ERR_TRANS_NOT_NULL, -1));
_alpm_db_unregister(handle->db_local);
handle->db_local = NULL;
for(i = handle->dbs_sync; i; i = i->next) {
pmdb_t *db = i->data;
_alpm_db_unregister(db);
i->data = NULL;
}
FREELIST(handle->dbs_sync);
return(0);
}
int SYMEXPORT alpm_db_unregister(pmdb_t *db)
{
int found = 0;
ALPM_LOG_FUNC;
ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
ASSERT(db != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1));
ASSERT(handle->trans == NULL, RET_ERR(PM_ERR_TRANS_NOT_NULL, -1));
if(db == handle->db_local) {
handle->db_local = NULL;
found = 1;
} else {
void *data;
handle->dbs_sync = alpm_list_remove(handle->dbs_sync,
db, _alpm_db_cmp, &data);
if(data) {
found = 1;
}
}
if(!found) {
RET_ERR(PM_ERR_DB_NOT_FOUND, -1);
}
_alpm_db_unregister(db);
return(0);
}
int SYMEXPORT alpm_db_setserver(pmdb_t *db, const char *url)
{
alpm_list_t *i;
int found = 0;
ALPM_LOG_FUNC;
ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
for(i = handle->dbs_sync; i && !found; i = i->next) {
pmdb_t *sdb = i->data;
if(strcmp(db->treename, sdb->treename) == 0) {
found = 1;
}
}
if(!found) {
RET_ERR(PM_ERR_DB_NOT_FOUND, -1);
}
if(url && strlen(url)) {
pmserver_t *server;
if((server = _alpm_server_new(url))
|