Commit 1a54de02 authored by Timo Teräs's avatar Timo Teräs

db: open flags revisited

more fine grained control what to load, and rename some of the
flags to be shorter.
parent 4d940c79
...@@ -116,8 +116,14 @@ struct apk_db_file *apk_db_file_query(struct apk_database *db, ...@@ -116,8 +116,14 @@ struct apk_db_file *apk_db_file_query(struct apk_database *db,
#define APK_OPENF_READ 0x0000 #define APK_OPENF_READ 0x0000
#define APK_OPENF_WRITE 0x0001 #define APK_OPENF_WRITE 0x0001
#define APK_OPENF_CREATE 0x0002 #define APK_OPENF_CREATE 0x0002
#define APK_OPENF_EMPTY_STATE 0x0004 #define APK_OPENF_NO_INSTALLED 0x0010
#define APK_OPENF_EMPTY_REPOS 0x0008 #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_open(struct apk_database *db, const char *root, unsigned int flags);
int apk_db_write_config(struct apk_database *db); int apk_db_write_config(struct apk_database *db);
......
...@@ -117,7 +117,7 @@ static int cache_main(void *ctx, int argc, char **argv) ...@@ -117,7 +117,7 @@ static int cache_main(void *ctx, int argc, char **argv)
return -100; return -100;
r = apk_db_open(&db, apk_root, 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) if (r != 0)
return r; return r;
......
...@@ -576,7 +576,7 @@ static int apk_db_scriptdb_read(struct apk_database *db, struct apk_istream *is) ...@@ -576,7 +576,7 @@ static int apk_db_scriptdb_read(struct apk_database *db, struct apk_istream *is)
return 0; 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; struct apk_istream *is;
apk_blob_t blob; apk_blob_t blob;
...@@ -592,25 +592,31 @@ static int apk_db_read_state(struct apk_database *db) ...@@ -592,25 +592,31 @@ static int apk_db_read_state(struct apk_database *db)
*/ */
fchdir(db->root_fd); fchdir(db->root_fd);
blob = apk_blob_from_file("var/lib/apk/world"); if (!(flags & APK_OPENF_NO_WORLD)) {
if (APK_BLOB_IS_NULL(blob)) blob = apk_blob_from_file("var/lib/apk/world");
return -ENOENT; if (APK_BLOB_IS_NULL(blob))
apk_deps_parse(db, &db->world, blob); return -ENOENT;
free(blob.ptr); apk_deps_parse(db, &db->world, blob);
free(blob.ptr);
for (i = 0; i < db->world->num; i++) for (i = 0; i < db->world->num; i++)
db->world->item[i].name->flags |= APK_NAME_TOPLEVEL; db->world->item[i].name->flags |= APK_NAME_TOPLEVEL;
}
is = apk_istream_from_file("var/lib/apk/installed"); if (!(flags & APK_OPENF_NO_INSTALLED)) {
if (is != NULL) { is = apk_istream_from_file("var/lib/apk/installed");
apk_db_index_read(db, is, -1); if (is != NULL) {
is->close(is); apk_db_index_read(db, is, -1);
is->close(is);
}
} }
is = apk_istream_from_file("var/lib/apk/scripts"); if (!(flags & APK_OPENF_NO_SCRIPTS)) {
if (is != NULL) { is = apk_istream_from_file("var/lib/apk/scripts");
apk_db_scriptdb_read(db, is); if (is != NULL) {
is->close(is); apk_db_scriptdb_read(db, is);
is->close(is);
}
} }
return 0; return 0;
...@@ -702,23 +708,21 @@ int apk_db_open(struct apk_database *db, const char *root, unsigned int flags) ...@@ -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); apk_blob_for_each_segment(blob, ":", add_protected_path, db);
if (root != NULL) { if (root != NULL) {
if (!(flags & APK_OPENF_EMPTY_STATE)) { r = apk_db_read_state(db, flags);
r = apk_db_read_state(db); if (r == -ENOENT && (flags & APK_OPENF_CREATE)) {
if (r == -ENOENT && (flags & APK_OPENF_CREATE)) { r = apk_db_create(db);
r = apk_db_create(db);
if (r != 0) {
msg = "Unable to create database";
goto ret_r;
}
r = apk_db_read_state(db);
}
if (r != 0) { if (r != 0) {
msg = "Unable to read database state"; msg = "Unable to create database";
goto ret_r; 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) if (apk_repos == NULL)
apk_repos = "/etc/apk/repositories"; apk_repos = "/etc/apk/repositories";
blob = apk_blob_from_file(apk_repos); 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) ...@@ -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; 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) list_for_each_entry(repo, &apk_repository_list.list, list)
apk_db_add_repository(db, APK_BLOB_STR(repo->url)); apk_db_add_repository(db, APK_BLOB_STR(repo->url));
} }
......
...@@ -132,7 +132,7 @@ static int fetch_main(void *ctx, int argc, char **argv) ...@@ -132,7 +132,7 @@ static int fetch_main(void *ctx, int argc, char **argv)
struct apk_database db; struct apk_database db;
int i, j, r; 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) if (r != 0)
return r; return r;
......
...@@ -313,7 +313,7 @@ static int info_main(void *ctx, int argc, char **argv) ...@@ -313,7 +313,7 @@ static int info_main(void *ctx, int argc, char **argv)
struct apk_database db; struct apk_database db;
int r; 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; return -1;
if (ictx->action != NULL) if (ictx->action != NULL)
......
...@@ -118,7 +118,7 @@ static int search_main(void *ctx, int argc, char **argv) ...@@ -118,7 +118,7 @@ static int search_main(void *ctx, int argc, char **argv)
struct apk_database db; struct apk_database db;
int r; 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; return -1;
if (ictx->action != NULL) if (ictx->action != NULL)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment