From 929bad61c09a323e18874b8981c491b7e7ddd8d9 Mon Sep 17 00:00:00 2001 From: Andrew Gregory Date: Sat, 18 Nov 2023 16:27:58 -0800 Subject: [PATCH] _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 --- lib/libalpm/alpm.c | 24 +++--------------------- lib/libalpm/handle.c | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/lib/libalpm/alpm.c b/lib/libalpm/alpm.c index d0c04a03..fe151d0e 100644 --- a/lib/libalpm/alpm.c +++ b/lib/libalpm/alpm.c @@ -93,34 +93,16 @@ cleanup: return NULL; } +/* check current state and free all resources including storage locks */ int SYMEXPORT alpm_release(alpm_handle_t *myhandle) { - int ret = 0; - alpm_db_t *db; - CHECK_HANDLE(myhandle, return -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 + ASSERT(myhandle->trans == NULL, RET_ERR(myhandle, ALPM_ERR_TRANS_NOT_NULL, -1)); _alpm_handle_unlock(myhandle); _alpm_handle_free(myhandle); - return ret; + return 0; } const char SYMEXPORT *alpm_version(void) diff --git a/lib/libalpm/handle.c b/lib/libalpm/handle.c index d1eafeda..f7650be4 100644 --- a/lib/libalpm/handle.c +++ b/lib/libalpm/handle.c @@ -48,12 +48,28 @@ alpm_handle_t *_alpm_handle_new(void) return handle; } +/* free all in-memory resources */ void _alpm_handle_free(alpm_handle_t *handle) { + alpm_list_t *i; + alpm_db_t *db; + if(handle == NULL) { 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 */ if(handle->logstream) { fclose(handle->logstream); @@ -68,6 +84,12 @@ void _alpm_handle_free(alpm_handle_t *handle) FREELIST(handle->known_keys); #endif +#ifdef HAVE_LIBCURL + curl_multi_cleanup(handle->curlm); + curl_global_cleanup(); + FREELIST(handle->server_errors); +#endif + /* free memory */ _alpm_trans_free(handle->trans); FREE(handle->root);