From 1a54de02b585e58511a3db4171ccabb3a3a0cce0 Mon Sep 17 00:00:00 2001 From: Timo Teras Date: Tue, 7 Jul 2009 09:27:56 +0300 Subject: db: open flags revisited more fine grained control what to load, and rename some of the flags to be shorter. --- src/apk_database.h | 10 +++++++-- src/cache.c | 2 +- src/database.c | 62 +++++++++++++++++++++++++++++------------------------- src/fetch.c | 2 +- src/info.c | 2 +- src/search.c | 2 +- 6 files changed, 45 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/apk_database.h b/src/apk_database.h index 4fb6ac8..1b23d51 100644 --- a/src/apk_database.h +++ b/src/apk_database.h @@ -116,8 +116,14 @@ struct apk_db_file *apk_db_file_query(struct apk_database *db, #define APK_OPENF_READ 0x0000 #define APK_OPENF_WRITE 0x0001 #define APK_OPENF_CREATE 0x0002 -#define APK_OPENF_EMPTY_STATE 0x0004 -#define APK_OPENF_EMPTY_REPOS 0x0008 +#define APK_OPENF_NO_INSTALLED 0x0010 +#define APK_OPENF_NO_SCRIPTS 0x0020 +#define APK_OPENF_NO_WORLD 0x0040 +#define APK_OPENF_NO_REPOS 0x0080 + +#define APK_OPENF_NO_STATE (APK_OPENF_NO_INSTALLED | \ + APK_OPENF_NO_SCRIPTS | \ + APK_OPENF_NO_WORLD) int apk_db_open(struct apk_database *db, const char *root, unsigned int flags); int apk_db_write_config(struct apk_database *db); diff --git a/src/cache.c b/src/cache.c index 152008b..e368cb3 100644 --- a/src/cache.c +++ b/src/cache.c @@ -117,7 +117,7 @@ static int cache_main(void *ctx, int argc, char **argv) return -100; r = apk_db_open(&db, apk_root, - (actions & CACHE_DOWNLOAD) ? 0 : APK_OPENF_EMPTY_STATE); + (actions & CACHE_DOWNLOAD) ? 0 : APK_OPENF_NO_STATE); if (r != 0) return r; diff --git a/src/database.c b/src/database.c index fda50db..55e97fa 100644 --- a/src/database.c +++ b/src/database.c @@ -576,7 +576,7 @@ static int apk_db_scriptdb_read(struct apk_database *db, struct apk_istream *is) return 0; } -static int apk_db_read_state(struct apk_database *db) +static int apk_db_read_state(struct apk_database *db, int flags) { struct apk_istream *is; apk_blob_t blob; @@ -592,25 +592,31 @@ static int apk_db_read_state(struct apk_database *db) */ fchdir(db->root_fd); - blob = apk_blob_from_file("var/lib/apk/world"); - if (APK_BLOB_IS_NULL(blob)) - return -ENOENT; - apk_deps_parse(db, &db->world, blob); - free(blob.ptr); + if (!(flags & APK_OPENF_NO_WORLD)) { + blob = apk_blob_from_file("var/lib/apk/world"); + if (APK_BLOB_IS_NULL(blob)) + return -ENOENT; + apk_deps_parse(db, &db->world, blob); + free(blob.ptr); - for (i = 0; i < db->world->num; i++) - db->world->item[i].name->flags |= APK_NAME_TOPLEVEL; + for (i = 0; i < db->world->num; i++) + db->world->item[i].name->flags |= APK_NAME_TOPLEVEL; + } - is = apk_istream_from_file("var/lib/apk/installed"); - if (is != NULL) { - apk_db_index_read(db, is, -1); - is->close(is); + if (!(flags & APK_OPENF_NO_INSTALLED)) { + is = apk_istream_from_file("var/lib/apk/installed"); + if (is != NULL) { + apk_db_index_read(db, is, -1); + is->close(is); + } } - is = apk_istream_from_file("var/lib/apk/scripts"); - if (is != NULL) { - apk_db_scriptdb_read(db, is); - is->close(is); + if (!(flags & APK_OPENF_NO_SCRIPTS)) { + is = apk_istream_from_file("var/lib/apk/scripts"); + if (is != NULL) { + apk_db_scriptdb_read(db, is); + is->close(is); + } } return 0; @@ -702,23 +708,21 @@ int apk_db_open(struct apk_database *db, const char *root, unsigned int flags) apk_blob_for_each_segment(blob, ":", add_protected_path, db); if (root != NULL) { - if (!(flags & APK_OPENF_EMPTY_STATE)) { - r = apk_db_read_state(db); - if (r == -ENOENT && (flags & APK_OPENF_CREATE)) { - r = apk_db_create(db); - if (r != 0) { - msg = "Unable to create database"; - goto ret_r; - } - r = apk_db_read_state(db); - } + r = apk_db_read_state(db, flags); + if (r == -ENOENT && (flags & APK_OPENF_CREATE)) { + r = apk_db_create(db); if (r != 0) { - msg = "Unable to read database state"; + msg = "Unable to create database"; goto ret_r; } + r = apk_db_read_state(db, flags); + } + if (r != 0) { + msg = "Unable to read database state"; + goto ret_r; } - if (!(flags & APK_OPENF_EMPTY_REPOS)) { + if (!(flags & APK_OPENF_NO_REPOS)) { if (apk_repos == NULL) apk_repos = "/etc/apk/repositories"; blob = apk_blob_from_file(apk_repos); @@ -733,7 +737,7 @@ int apk_db_open(struct apk_database *db, const char *root, unsigned int flags) db->cache_dir = apk_linked_cache_dir; } - if (!(flags & APK_OPENF_EMPTY_REPOS)) { + if (!(flags & APK_OPENF_NO_REPOS)) { list_for_each_entry(repo, &apk_repository_list.list, list) apk_db_add_repository(db, APK_BLOB_STR(repo->url)); } diff --git a/src/fetch.c b/src/fetch.c index 501dff1..8e53b2c 100644 --- a/src/fetch.c +++ b/src/fetch.c @@ -132,7 +132,7 @@ static int fetch_main(void *ctx, int argc, char **argv) struct apk_database db; int i, j, r; - r = apk_db_open(&db, apk_root, APK_OPENF_EMPTY_STATE); + r = apk_db_open(&db, apk_root, APK_OPENF_NO_STATE); if (r != 0) return r; diff --git a/src/info.c b/src/info.c index 60dd910..4be29dc 100644 --- a/src/info.c +++ b/src/info.c @@ -313,7 +313,7 @@ static int info_main(void *ctx, int argc, char **argv) struct apk_database db; int r; - if (apk_db_open(&db, apk_root, APK_OPENF_READ + APK_OPENF_EMPTY_REPOS) < 0) + if (apk_db_open(&db, apk_root, APK_OPENF_READ | APK_OPENF_NO_REPOS) < 0) return -1; if (ictx->action != NULL) diff --git a/src/search.c b/src/search.c index 9acf4f3..52429eb 100644 --- a/src/search.c +++ b/src/search.c @@ -118,7 +118,7 @@ static int search_main(void *ctx, int argc, char **argv) struct apk_database db; int r; - if (apk_db_open(&db, apk_root, APK_OPENF_READ + APK_OPENF_EMPTY_STATE) < 0) + if (apk_db_open(&db, apk_root, APK_OPENF_READ | APK_OPENF_NO_STATE) < 0) return -1; if (ictx->action != NULL) -- cgit v1.2.3-70-g09d2