diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libalpm/alpm.h | 6 | ||||
-rw-r--r-- | lib/libalpm/db.c | 117 | ||||
-rw-r--r-- | lib/libalpm/db.h | 3 |
3 files changed, 88 insertions, 38 deletions
diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h index 844d9bf2..a3772a05 100644 --- a/lib/libalpm/alpm.h +++ b/lib/libalpm/alpm.h @@ -31,6 +31,8 @@ extern "C" { #include <time.h> /* for time_t */ #include <stdarg.h> /* for va_list */ +#define DEPRECATED __attribute__((deprecated)) + /* * Arch Linux Package Management library */ @@ -143,7 +145,9 @@ alpm_list_t *alpm_option_get_syncdbs(); * Databases */ -pmdb_t *alpm_db_register(const char *treename); +/* Preferred interfaces db_register_local and db_register_sync */ +pmdb_t *alpm_db_register_local(void); +pmdb_t *alpm_db_register_sync(const char *treename); int alpm_db_unregister(pmdb_t *db); int alpm_db_unregister_all(void); diff --git a/lib/libalpm/db.c b/lib/libalpm/db.c index e79aad89..86165066 100644 --- a/lib/libalpm/db.c +++ b/lib/libalpm/db.c @@ -53,11 +53,11 @@ * @{ */ -/** Register a package database - * @param treename the name of the repository +/** Register a sync database of packages. + * @param treename the name of the sync repository * @return a pmdb_t* on success (the value), NULL on error */ -pmdb_t SYMEXPORT *alpm_db_register(const char *treename) +pmdb_t SYMEXPORT *alpm_db_register_sync(const char *treename) { ALPM_LOG_FUNC; @@ -67,7 +67,22 @@ pmdb_t SYMEXPORT *alpm_db_register(const char *treename) /* Do not register a database if a transaction is on-going */ ASSERT(handle->trans == NULL, RET_ERR(PM_ERR_TRANS_NOT_NULL, NULL)); - return(_alpm_db_register(treename)); + return(_alpm_db_register_sync(treename)); +} + +/** Register the local package database. + * @return a pmdb_t* representing the local database, or NULL on error + */ +pmdb_t SYMEXPORT *alpm_db_register_local(void) +{ + ALPM_LOG_FUNC; + + /* Sanity checks */ + ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, NULL)); + /* Do not register a database if a transaction is on-going */ + ASSERT(handle->trans == NULL, RET_ERR(PM_ERR_TRANS_NOT_NULL, NULL)); + + return(_alpm_db_register_local()); } /* Helper function for alpm_db_unregister{_all} */ @@ -161,6 +176,7 @@ int SYMEXPORT alpm_db_unregister(pmdb_t *db) */ int SYMEXPORT alpm_db_setserver(pmdb_t *db, const char *url) { + alpm_list_t *i; int found = 0; ALPM_LOG_FUNC; @@ -168,18 +184,11 @@ int SYMEXPORT alpm_db_setserver(pmdb_t *db, const char *url) /* Sanity checks */ ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1)); - if(strcmp(db->treename, "local") == 0) { - if(handle->db_local != NULL) { + for(i = handle->dbs_sync; i && !found; i = i->next) { + pmdb_t *sdb = i->data; + if(strcmp(db->treename, sdb->treename) == 0) { found = 1; } - } else { - alpm_list_t *i; - 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); @@ -690,7 +699,7 @@ alpm_list_t *_alpm_db_search(pmdb_t *db, const alpm_list_t *needles) return(ret); } -pmdb_t *_alpm_db_register(const char *treename) +pmdb_t *_alpm_db_register_local(void) { struct stat buf; pmdb_t *db; @@ -699,23 +708,63 @@ pmdb_t *_alpm_db_register(const char *treename) ALPM_LOG_FUNC; - if(strcmp(treename, "local") == 0) { - if(handle->db_local != NULL) { - _alpm_log(PM_LOG_WARNING, _("attempt to re-register the 'local' DB\n")); - RET_ERR(PM_ERR_DB_NOT_NULL, NULL); + if(handle->db_local != NULL) { + _alpm_log(PM_LOG_WARNING, _("attempt to re-register the 'local' DB\n")); + RET_ERR(PM_ERR_DB_NOT_NULL, NULL); + } + + _alpm_log(PM_LOG_DEBUG, "registering local database\n"); + + /* make sure the database directory exists */ + dbpath = alpm_option_get_dbpath(); + if(!dbpath) { + _alpm_log(PM_LOG_WARNING, _("database path is undefined\n")); + RET_ERR(PM_ERR_DB_OPEN, NULL); + } + snprintf(path, PATH_MAX, "%slocal", dbpath); + /* TODO this is rediculous, we try to do this even if we can't */ + if(stat(path, &buf) != 0 || !S_ISDIR(buf.st_mode)) { + _alpm_log(PM_LOG_DEBUG, "database dir '%s' does not exist, creating it\n", + path); + if(_alpm_makepath(path) != 0) { + RET_ERR(PM_ERR_SYSTEM, NULL); } - } else { - alpm_list_t *i; - for(i = handle->dbs_sync; i; i = i->next) { - pmdb_t *sdb = i->data; - if(strcmp(treename, sdb->treename) == 0) { - _alpm_log(PM_LOG_DEBUG, "attempt to re-register the '%s' database, using existing\n", sdb->treename); - return sdb; - } + } + + db = _alpm_db_new(dbpath, "local"); + if(db == NULL) { + RET_ERR(PM_ERR_DB_CREATE, NULL); + } + + _alpm_log(PM_LOG_DEBUG, "opening database '%s'\n", db->treename); + if(_alpm_db_open(db) == -1) { + _alpm_db_free(db); + RET_ERR(PM_ERR_DB_OPEN, NULL); + } + + handle->db_local = db; + return(db); +} + +pmdb_t *_alpm_db_register_sync(const char *treename) +{ + struct stat buf; + pmdb_t *db; + const char *dbpath; + char path[PATH_MAX]; + alpm_list_t *i; + + ALPM_LOG_FUNC; + + for(i = handle->dbs_sync; i; i = i->next) { + pmdb_t *sdb = i->data; + if(strcmp(treename, sdb->treename) == 0) { + _alpm_log(PM_LOG_DEBUG, "attempt to re-register the '%s' database, using existing\n", sdb->treename); + return sdb; } } - - _alpm_log(PM_LOG_DEBUG, "registering database '%s'\n", treename); + + _alpm_log(PM_LOG_DEBUG, "registering sync database '%s'\n", treename); /* make sure the database directory exists */ dbpath = alpm_option_get_dbpath(); @@ -723,7 +772,8 @@ pmdb_t *_alpm_db_register(const char *treename) _alpm_log(PM_LOG_WARNING, _("database path is undefined\n")); RET_ERR(PM_ERR_DB_OPEN, NULL); } - snprintf(path, PATH_MAX, "%s%s", dbpath, treename); + /* all sync DBs now reside in the sync/ subdir of the dbpath */ + snprintf(path, PATH_MAX, "%ssync/%s", dbpath, treename); /* TODO this is rediculous, we try to do this even if we can't */ if(stat(path, &buf) != 0 || !S_ISDIR(buf.st_mode)) { _alpm_log(PM_LOG_DEBUG, "database dir '%s' does not exist, creating it\n", @@ -744,12 +794,7 @@ pmdb_t *_alpm_db_register(const char *treename) RET_ERR(PM_ERR_DB_OPEN, NULL); } - if(strcmp(treename, "local") == 0) { - handle->db_local = db; - } else { - handle->dbs_sync = alpm_list_add(handle->dbs_sync, db); - } - + handle->dbs_sync = alpm_list_add(handle->dbs_sync, db); return(db); } diff --git a/lib/libalpm/db.h b/lib/libalpm/db.h index 2597c8c4..15cc4c4c 100644 --- a/lib/libalpm/db.h +++ b/lib/libalpm/db.h @@ -52,7 +52,8 @@ pmdb_t *_alpm_db_new(const char *dbpath, const char *treename); void _alpm_db_free(pmdb_t *db); int _alpm_db_cmp(const void *db1, const void *db2); alpm_list_t *_alpm_db_search(pmdb_t *db, const alpm_list_t *needles); -pmdb_t *_alpm_db_register(const char *treename); +pmdb_t *_alpm_db_register_local(void); +pmdb_t *_alpm_db_register_sync(const char *treename); /* be.c, backend specific calls */ int _alpm_db_install(pmdb_t *db, const char *dbfile); |