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,
#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);
......
......@@ -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;
......
......@@ -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,6 +592,7 @@ static int apk_db_read_state(struct apk_database *db)
*/
fchdir(db->root_fd);
if (!(flags & APK_OPENF_NO_WORLD)) {
blob = apk_blob_from_file("var/lib/apk/world");
if (APK_BLOB_IS_NULL(blob))
return -ENOENT;
......@@ -600,18 +601,23 @@ static int apk_db_read_state(struct apk_database *db)
for (i = 0; i < db->world->num; i++)
db->world->item[i].name->flags |= APK_NAME_TOPLEVEL;
}
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);
}
}
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);
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 create database";
goto ret_r;
}
r = apk_db_read_state(db);
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));
}
......
......@@ -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;
......
......@@ -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)
......
......@@ -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)
......
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