From e00884ab554562fa5e6ac601aa99477e0f70cd75 Mon Sep 17 00:00:00 2001 From: Nagy Gabor <ngaba@bibl.u-szeged.hu> Date: Sat, 10 Jan 2009 16:25:03 +0100 Subject: [PATCH] db->pkgcache_loaded and db->grpcache_loaded
Clearly the old code was more elegant (NULL cache indicated "not loaded"), but it had some drawbacks, so from now on we indicate the state of caches explicitly. Old drawbacks: When we had an empty database (unstable), libalpm called db_populate after every pkgcache access, because NULL pkgcache indicated "not loaded" state. This is not a common case, but the same situation can happen with grpcache, which is more problematic: If the user had a custom repo with no groups, grpcache was always NULL. (grpcache is also loaded per database.) Thus every get_grpcache call induced a load_grpcache operation, so the benefits of grpcache was completely lost. Signed-off-by: Nagy Gabor <ngaba@bibl.u-szeged.hu> --- lib/libalpm/cache.c | 31 ++++++++++++++++--------------- lib/libalpm/db.h | 2 ++ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/lib/libalpm/cache.c b/lib/libalpm/cache.c index 1085779..8d18f3e 100644 --- a/lib/libalpm/cache.c +++ b/lib/libalpm/cache.c @@ -54,6 +54,7 @@ int _alpm_db_load_pkgcache(pmdb_t *db) return(-1); } + db->pkgcache_loaded = 1; return(0); } @@ -61,7 +62,7 @@ void _alpm_db_free_pkgcache(pmdb_t *db) { ALPM_LOG_FUNC; - if(db == NULL || db->pkgcache == NULL) { + if(db == NULL || !db->pkgcache_loaded) { return; } @@ -71,10 +72,9 @@ void _alpm_db_free_pkgcache(pmdb_t *db) alpm_list_free_inner(db->pkgcache, (alpm_list_fn_free)_alpm_pkg_free); alpm_list_free(db->pkgcache); db->pkgcache = NULL; + db->pkgcache_loaded = 0; - if(db->grpcache) { - _alpm_db_free_grpcache(db); - } + _alpm_db_free_grpcache(db); } alpm_list_t *_alpm_db_get_pkgcache(pmdb_t *db) @@ -85,13 +85,13 @@ alpm_list_t *_alpm_db_get_pkgcache(pmdb_t *db) return(NULL); } - if(!db->pkgcache) { + if(!db->pkgcache_loaded) { _alpm_db_load_pkgcache(db); } /* hmmm, still NULL ?*/ if(!db->pkgcache) { - _alpm_log(PM_LOG_DEBUG, "error: pkgcache is NULL for db '%s'\n", db->treename); + _alpm_log(PM_LOG_DEBUG, "warning: pkgcache is NULL for db '%s'\n", db->treename); } return(db->pkgcache); @@ -104,7 +104,7 @@ int _alpm_db_add_pkgincache(pmdb_t *db, pmpkg_t *pkg) ALPM_LOG_FUNC; - if(db == NULL || pkg == NULL) { + if(db == NULL || !db->pkgcache_loaded || pkg == NULL) { return(-1); } @@ -139,7 +139,7 @@ int _alpm_db_remove_pkgfromcache(pmdb_t *db, pmpkg_t *pkg) ALPM_LOG_FUNC; - if(db == NULL || pkg == NULL) { + if(db == NULL || !db->pkgcache_loaded || pkg == NULL) { return(-1); } @@ -172,7 +172,7 @@ pmpkg_t *_alpm_db_get_pkgfromcache(pmdb_t *db, const char *target) alpm_list_t *pkgcache = _alpm_db_get_pkgcache(db); if(!pkgcache) { - _alpm_log(PM_LOG_DEBUG, "error: failed to get '%s' from NULL pkgcache\n", + _alpm_log(PM_LOG_DEBUG, "warning: failed to get '%s' from NULL pkgcache\n", target); return(NULL); } @@ -192,10 +192,6 @@ int _alpm_db_load_grpcache(pmdb_t *db) return(-1); } - if(db->pkgcache == NULL) { - _alpm_db_load_pkgcache(db); - } - _alpm_log(PM_LOG_DEBUG, "loading group cache for repository '%s'\n", db->treename); @@ -230,6 +226,7 @@ int _alpm_db_load_grpcache(pmdb_t *db) } } + db->grpcache_loaded = 1; return(0); } @@ -239,15 +236,19 @@ void _alpm_db_free_grpcache(pmdb_t *db) ALPM_LOG_FUNC; - if(db == NULL || db->grpcache == NULL) { + if(db == NULL || !db->grpcache_loaded) { return; } + _alpm_log(PM_LOG_DEBUG, "freeing group cache for repository '%s'\n", + db->treename); + for(lg = db->grpcache; lg; lg = lg->next) { _alpm_grp_free(lg->data); lg->data = NULL; } FREELIST(db->grpcache); + db->grpcache_loaded = 0; } alpm_list_t *_alpm_db_get_grpcache(pmdb_t *db) @@ -258,7 +259,7 @@ alpm_list_t *_alpm_db_get_grpcache(pmdb_t *db) return(NULL); } - if(db->grpcache == NULL) { + if(!db->grpcache_loaded) { _alpm_db_load_grpcache(db); } diff --git a/lib/libalpm/db.h b/lib/libalpm/db.h index 25b90b5..55f5cd5 100644 --- a/lib/libalpm/db.h +++ b/lib/libalpm/db.h @@ -42,7 +42,9 @@ struct __pmdb_t { char *path; char *treename; void *handle; + int pkgcache_loaded; alpm_list_t *pkgcache; + int grpcache_loaded; alpm_list_t *grpcache; alpm_list_t *servers; }; -- 1.6.0.3