_alpm_handle_free: free all in-memory resources

Freeing handle resources was previously split awkwardly between
_alpm_handle_free and alpm_release.  This consolidates the freeing of
all in-memory resources to _alpm_handle_free, leaving alpm_release as a
thin wrapper that provides safety checks and frees any external
resources, e.g. removing lock files.

Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
This commit is contained in:
Andrew Gregory 2023-11-18 16:27:58 -08:00 committed by Allan McRae
parent 00d2b1f902
commit 929bad61c0
2 changed files with 25 additions and 21 deletions

View file

@ -93,34 +93,16 @@ cleanup:
return NULL; return NULL;
} }
/* check current state and free all resources including storage locks */
int SYMEXPORT alpm_release(alpm_handle_t *myhandle) int SYMEXPORT alpm_release(alpm_handle_t *myhandle)
{ {
int ret = 0;
alpm_db_t *db;
CHECK_HANDLE(myhandle, return -1); CHECK_HANDLE(myhandle, return -1);
ASSERT(myhandle->trans == NULL, RET_ERR(myhandle, ALPM_ERR_TRANS_NOT_NULL, -1));
/* close local database */
db = myhandle->db_local;
if(db) {
db->ops->unregister(db);
myhandle->db_local = NULL;
}
if(alpm_unregister_all_syncdbs(myhandle) == -1) {
ret = -1;
}
#ifdef HAVE_LIBCURL
curl_multi_cleanup(myhandle->curlm);
curl_global_cleanup();
FREELIST(myhandle->server_errors);
#endif
_alpm_handle_unlock(myhandle); _alpm_handle_unlock(myhandle);
_alpm_handle_free(myhandle); _alpm_handle_free(myhandle);
return ret; return 0;
} }
const char SYMEXPORT *alpm_version(void) const char SYMEXPORT *alpm_version(void)

View file

@ -48,12 +48,28 @@ alpm_handle_t *_alpm_handle_new(void)
return handle; return handle;
} }
/* free all in-memory resources */
void _alpm_handle_free(alpm_handle_t *handle) void _alpm_handle_free(alpm_handle_t *handle)
{ {
alpm_list_t *i;
alpm_db_t *db;
if(handle == NULL) { if(handle == NULL) {
return; return;
} }
/* close local database */
if((db = handle->db_local)) {
db->ops->unregister(db);
}
/* unregister all sync dbs */
for(i = handle->dbs_sync; i; i = i->next) {
db = i->data;
db->ops->unregister(db);
}
alpm_list_free(handle->dbs_sync);
/* close logfile */ /* close logfile */
if(handle->logstream) { if(handle->logstream) {
fclose(handle->logstream); fclose(handle->logstream);
@ -68,6 +84,12 @@ void _alpm_handle_free(alpm_handle_t *handle)
FREELIST(handle->known_keys); FREELIST(handle->known_keys);
#endif #endif
#ifdef HAVE_LIBCURL
curl_multi_cleanup(handle->curlm);
curl_global_cleanup();
FREELIST(handle->server_errors);
#endif
/* free memory */ /* free memory */
_alpm_trans_free(handle->trans); _alpm_trans_free(handle->trans);
FREE(handle->root); FREE(handle->root);