libalpm/db.c : add alpm_db_unregister_all.

This basically moves the code from alpm_release, which was mostly about
unregistering all databases, to a safer alpm_db_unregister_all.
This allows to avoid modifying the dbs_sync list while iterating over it,
and and also prevent alpm_release from looping infinitely when a database
can't be unregistered.

Signed-off-by: Chantry Xavier <shiningxc@gmail.com>
This commit is contained in:
Chantry Xavier 2007-07-09 17:02:29 +02:00 committed by Dan McGee
parent 47cada81a0
commit ee977019e3
4 changed files with 56 additions and 25 deletions

View file

@ -62,24 +62,12 @@ int SYMEXPORT alpm_initialize(void)
*/ */
int SYMEXPORT alpm_release(void) int SYMEXPORT alpm_release(void)
{ {
int dbs_left = 0;
ALPM_LOG_FUNC; ALPM_LOG_FUNC;
ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1)); ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
/* close local database */ if(alpm_db_unregister_all() == -1) {
if(handle->db_local) { return(-1);
alpm_db_unregister(handle->db_local);
handle->db_local = NULL;
}
/* and also sync ones */
while((dbs_left = alpm_list_count(handle->dbs_sync)) > 0) {
pmdb_t *db = (pmdb_t *)handle->dbs_sync->data;
_alpm_log(PM_LOG_DEBUG, "removing DB %s, %d remaining...",
db->treename, dbs_left);
alpm_db_unregister(db);
db = NULL;
} }
_alpm_handle_free(handle); _alpm_handle_free(handle);

View file

@ -145,6 +145,7 @@ alpm_list_t *alpm_option_get_syncdbs();
pmdb_t *alpm_db_register(const char *treename); pmdb_t *alpm_db_register(const char *treename);
int alpm_db_unregister(pmdb_t *db); int alpm_db_unregister(pmdb_t *db);
int alpm_db_unregister_all();
const char *alpm_db_get_name(const pmdb_t *db); const char *alpm_db_get_name(const pmdb_t *db);
const char *alpm_db_get_url(const pmdb_t *db); const char *alpm_db_get_url(const pmdb_t *db);

View file

@ -70,6 +70,35 @@ pmdb_t SYMEXPORT *alpm_db_register(const char *treename)
return(_alpm_db_register(treename)); return(_alpm_db_register(treename));
} }
/** Unregister all package databases
* @return 0 on success, -1 on error (pm_errno is set accordingly)
*/
int SYMEXPORT alpm_db_unregister_all()
{
alpm_list_t *i;
ALPM_LOG_FUNC;
/* Sanity checks */
ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
/* Do not unregister a database if a transaction is on-going */
ASSERT(handle->trans == NULL || handle->trans->state == STATE_INTERRUPTED,
RET_ERR(PM_ERR_TRANS_NOT_NULL, -1));
/* close local database */
_alpm_db_unregister(handle->db_local);
handle->db_local = NULL;
/* and also sync ones */
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);
}
/** Unregister a package database /** Unregister a package database
* @param db pointer to the package database to unregister * @param db pointer to the package database to unregister
* @return 0 on success, -1 on error (pm_errno is set accordingly) * @return 0 on success, -1 on error (pm_errno is set accordingly)
@ -84,13 +113,17 @@ int SYMEXPORT alpm_db_unregister(pmdb_t *db)
ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1)); ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
ASSERT(db != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1)); ASSERT(db != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1));
/* Do not unregister a database if a transaction is on-going */ /* Do not unregister a database if a transaction is on-going */
ASSERT(handle->trans == NULL || handle->trans->state == STATE_INTERRUPTED, ASSERT(handle->trans == NULL || handle->trans->state == STATE_INTERRUPTED,
RET_ERR(PM_ERR_TRANS_NOT_NULL, -1)); RET_ERR(PM_ERR_TRANS_NOT_NULL, -1));
if(db == handle->db_local) { if(db == handle->db_local) {
handle->db_local = NULL; handle->db_local = NULL;
found = 1; found = 1;
} else { } else {
/* Warning : this function shouldn't be used to unregister all sync databases
* by walking through the list returned by alpm_option_get_syncdbs,
* because the db is removed from that list here.
*/
void *data; void *data;
handle->dbs_sync = alpm_list_remove(handle->dbs_sync, db, _alpm_db_cmp, &data); handle->dbs_sync = alpm_list_remove(handle->dbs_sync, db, _alpm_db_cmp, &data);
if(data) { if(data) {
@ -102,16 +135,7 @@ int SYMEXPORT alpm_db_unregister(pmdb_t *db)
RET_ERR(PM_ERR_DB_NOT_FOUND, -1); RET_ERR(PM_ERR_DB_NOT_FOUND, -1);
} }
_alpm_log(PM_LOG_DEBUG, "unregistering database '%s'", db->treename); _alpm_db_unregister(db);
/* Cleanup */
_alpm_db_free_pkgcache(db);
_alpm_log(PM_LOG_DEBUG, "closing database '%s'", db->treename);
_alpm_db_close(db);
_alpm_db_free(db);
return(0); return(0);
} }
@ -542,6 +566,23 @@ error:
/** @} */ /** @} */
/* Helper function for alpm_db_unregister{_all} */
void _alpm_db_unregister(pmdb_t *db)
{
if(db == NULL) {
return;
}
_alpm_log(PM_LOG_DEBUG, "unregistering database '%s'", db->treename);
/* Cleanup */
_alpm_db_free_pkgcache(db);
_alpm_log(PM_LOG_DEBUG, "closing database '%s'", db->treename);
_alpm_db_close(db);
_alpm_db_free(db);
}
pmdb_t *_alpm_db_new(const char *dbpath, const char *treename) pmdb_t *_alpm_db_new(const char *dbpath, const char *treename)
{ {
pmdb_t *db; pmdb_t *db;

View file

@ -53,6 +53,7 @@ void _alpm_db_free(pmdb_t *db);
int _alpm_db_cmp(const void *db1, const void *db2); int _alpm_db_cmp(const void *db1, const void *db2);
alpm_list_t *_alpm_db_search(pmdb_t *db, const alpm_list_t *needles); 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(const char *treename);
void _alpm_db_unregister(pmdb_t *db);
/* be.c, backend specific calls */ /* be.c, backend specific calls */
int _alpm_db_install(pmdb_t *db, const char *dbfile); int _alpm_db_install(pmdb_t *db, const char *dbfile);