From ecdacd15032578148489f868fd928fb502032c60 Mon Sep 17 00:00:00 2001 From: Timo Teras Date: Thu, 6 Aug 2009 14:25:03 +0300 Subject: all: implement database open options so user can override trusted keys directory and repositories file. --- src/add.c | 43 +++++++++++++++++++------------------------ src/apk.c | 49 ++++++++++++++++++++++++++++++++++--------------- src/apk_applet.h | 16 +++++----------- src/apk_database.h | 21 +++++++++++++++++---- src/audit.c | 20 +++++--------------- src/cache.c | 19 ++++++------------- src/database.c | 42 +++++++++++++++++++++++++----------------- src/del.c | 26 +++++++++++--------------- src/fetch.c | 24 ++++++++++-------------- src/fix.c | 24 ++++++++++-------------- src/index.c | 24 +++++++++++------------- src/info.c | 20 +++++++------------- src/search.c | 18 ++++++------------ src/update.c | 11 +++-------- src/upgrade.c | 24 ++++++++++-------------- src/ver.c | 15 ++++++--------- src/verify.c | 13 ++++--------- 17 files changed, 189 insertions(+), 220 deletions(-) diff --git a/src/add.c b/src/add.c index 5cff9e1..710f606 100644 --- a/src/add.c +++ b/src/add.c @@ -16,17 +16,17 @@ #include "apk_state.h" struct add_ctx { - unsigned int open_flags; const char *virtpkg; }; -static int add_parse(void *ctx, int optch, int optindex, const char *optarg) +static int add_parse(void *ctx, struct apk_db_options *dbopts, + int optch, int optindex, const char *optarg) { struct add_ctx *actx = (struct add_ctx *) ctx; switch (optch) { case 0x10000: - actx->open_flags |= APK_OPENF_CREATE; + dbopts->open_flags |= APK_OPENF_CREATE; break; case 'u': apk_flags |= APK_UPGRADE; @@ -57,22 +57,17 @@ static int non_repository_check(struct apk_database *db) } -static int add_main(void *ctx, int argc, char **argv) +static int add_main(void *ctx, struct apk_database *db, int argc, char **argv) { struct add_ctx *actx = (struct add_ctx *) ctx; - struct apk_database db; struct apk_state *state = NULL; struct apk_dependency_array *pkgs = NULL; struct apk_package *virtpkg = NULL; struct apk_dependency virtdep; - int i, r; - - r = apk_db_open(&db, apk_root, actx->open_flags | APK_OPENF_WRITE); - if (r != 0) - return r; + int i, r = 0; if (actx->virtpkg) { - if (non_repository_check(&db)) + if (non_repository_check(db)) goto err; virtpkg = apk_pkg_new(); @@ -80,14 +75,14 @@ static int add_main(void *ctx, int argc, char **argv) apk_error("Failed to allocate virtual meta package"); goto err; } - virtpkg->name = apk_db_get_name(&db, APK_BLOB_STR(actx->virtpkg)); + virtpkg->name = apk_db_get_name(db, APK_BLOB_STR(actx->virtpkg)); apk_blob_checksum(APK_BLOB_STR(virtpkg->name->name), apk_default_checksum(), &virtpkg->csum); virtpkg->version = strdup("0"); virtpkg->description = strdup("virtual meta package"); - apk_dep_from_pkg(&virtdep, &db, virtpkg); + apk_dep_from_pkg(&virtdep, db, virtpkg); virtdep.name->flags |= APK_NAME_TOPLEVEL; - virtpkg = apk_db_pkg_add(&db, virtpkg); + virtpkg = apk_db_pkg_add(db, virtpkg); } for (i = 0; i < argc; i++) { @@ -97,21 +92,21 @@ static int add_main(void *ctx, int argc, char **argv) struct apk_package *pkg = NULL; struct apk_sign_ctx sctx; - if (non_repository_check(&db)) + if (non_repository_check(db)) goto err; apk_sign_ctx_init(&sctx, APK_SIGN_VERIFY_AND_GENERATE, - NULL, db.keys_fd); - r = apk_pkg_read(&db, argv[i], &sctx, &pkg); + NULL, db->keys_fd); + r = apk_pkg_read(db, argv[i], &sctx, &pkg); apk_sign_ctx_free(&sctx); if (r != 0) { apk_error("%s: %s", argv[i], apk_error_str(r)); goto err; } - apk_dep_from_pkg(&dep, &db, pkg); + apk_dep_from_pkg(&dep, db, pkg); } else { - r = apk_dep_from_blob(&dep, &db, APK_BLOB_STR(argv[i])); + r = apk_dep_from_blob(&dep, db, APK_BLOB_STR(argv[i])); if (r != 0) goto err; } @@ -126,10 +121,10 @@ static int add_main(void *ctx, int argc, char **argv) if (virtpkg) { apk_deps_add(&pkgs, &virtdep); - apk_deps_add(&db.world, &virtdep); + apk_deps_add(&db->world, &virtdep); } - state = apk_state_new(&db); + state = apk_state_new(db); if (state == NULL) goto err; @@ -141,13 +136,12 @@ static int add_main(void *ctx, int argc, char **argv) goto err; } if (!virtpkg) - apk_deps_add(&db.world, &pkgs->item[i]); + apk_deps_add(&db->world, &pkgs->item[i]); } - r = apk_state_commit(state, &db); + r = apk_state_commit(state, db); err: if (state != NULL) apk_state_unref(state); - apk_db_close(&db); return r; } @@ -166,6 +160,7 @@ static struct apk_applet apk_add = { .help = "Add (or update) PACKAGEs to main dependencies and install " "them, while ensuring that all dependencies are met.", .arguments = "PACKAGE...", + .open_flags = APK_OPENF_WRITE, .context_size = sizeof(struct add_ctx), .num_options = ARRAY_SIZE(add_options), .options = add_options, diff --git a/src/apk.c b/src/apk.c index 122f9a7..42b1dbd 100644 --- a/src/apk.c +++ b/src/apk.c @@ -21,11 +21,10 @@ #include #include "apk_defines.h" +#include "apk_database.h" #include "apk_applet.h" #include "apk_blob.h" -const char *apk_root; -struct apk_repository_url apk_repository_list; int apk_verbosity = 1, apk_wait; unsigned int apk_flags = 0; @@ -53,6 +52,10 @@ static struct apk_option generic_options[] = { { 0x105, "wait", "Wait for TIME seconds to get an exclusive " "repository lock before failing", required_argument, "TIME" }, + { 0x107, "keys-dir", "Override directory of trusted keys", + required_argument, "KEYSDIR" }, + { 0x108, "repositories-file", "Override repositories file", + required_argument, "REPOFILE" } }; const char *apk_error_str(int error) @@ -239,10 +242,10 @@ static struct apk_applet *deduce_applet(int argc, char **argv) return NULL; } -static struct apk_repository_url *apk_repository_new(const char *url) +static struct apk_repository_list *apk_repository_new(const char *url) { - struct apk_repository_url *r = calloc(1, - sizeof(struct apk_repository_url)); + struct apk_repository_list *r = calloc(1, + sizeof(struct apk_repository_list)); if (r) { r->url = url; list_init(&r->list); @@ -289,11 +292,13 @@ int main(int argc, char **argv) struct option *opt, *all_options; int r, optindex, num_options; void *ctx = NULL; - struct apk_repository_url *repo = NULL; + struct apk_repository_list *repo = NULL; + struct apk_database db; + struct apk_db_options dbopts; + memset(&dbopts, 0, sizeof(dbopts)); + list_init(&dbopts.repository_list); umask(0); - apk_root = getenv("ROOT"); - list_init(&apk_repository_list.list); applet = deduce_applet(argc, argv); num_options = ARRAY_SIZE(generic_options) + 1; @@ -307,6 +312,8 @@ int main(int argc, char **argv) applet->options, applet->num_options); if (applet->context_size != 0) ctx = calloc(1, applet->context_size); + dbopts.open_flags = applet->open_flags; + apk_flags |= applet->forced_flags; } for (opt = all_options, sopt = short_options; opt->name != NULL; opt++) { @@ -330,12 +337,18 @@ int main(int argc, char **argv) return usage(applet); break; case 'p': - apk_root = optarg; + dbopts.root = optarg; + break; + case 0x107: + dbopts.keys_dir = optarg; + break; + case 0x108: + dbopts.repositories_file = optarg; break; case 'X': repo = apk_repository_new(optarg); if (repo) - list_add(&repo->list, &apk_repository_list.list); + list_add(&repo->list, &dbopts.repository_list); break; case 'q': apk_verbosity--; @@ -374,7 +387,7 @@ int main(int argc, char **argv) break; default: if (applet == NULL || applet->parse == NULL || - applet->parse(ctx, r, + applet->parse(ctx, &dbopts, r, optindex - ARRAY_SIZE(generic_options), optarg) != 0) return usage(applet); @@ -385,9 +398,6 @@ int main(int argc, char **argv) if (applet == NULL) return usage(NULL); - if (apk_root == NULL) - apk_root = "/"; - argc -= optind; argv += optind; if (argc >= 1 && strcmp(argv[0], applet->name) == 0) { @@ -395,7 +405,16 @@ int main(int argc, char **argv) argv++; } - r = applet->main(ctx, argc, argv); + r = apk_db_open(&db, &dbopts); + if (r != 0) { + apk_error("Failed to open apk database: %s", + apk_error_str(r)); + return r; + } + + r = applet->main(ctx, &db, argc, argv); + apk_db_close(&db); + if (r == -EINVAL) return usage(applet); return r; diff --git a/src/apk_applet.h b/src/apk_applet.h index 2782a92..6f59f04 100644 --- a/src/apk_applet.h +++ b/src/apk_applet.h @@ -14,15 +14,7 @@ #include #include "apk_defines.h" - -extern const char *apk_root; - -struct apk_repository_url { - struct list_head list; - const char *url; -}; - -extern struct apk_repository_url apk_repository_list; +#include "apk_database.h" struct apk_option { int val; @@ -37,12 +29,14 @@ struct apk_applet { const char *arguments; const char *help; + unsigned int open_flags, forced_flags; int context_size; int num_options; struct apk_option *options; - int (*parse)(void *ctx, int optch, int optindex, const char *optarg); - int (*main)(void *ctx, int argc, char **argv); + int (*parse)(void *ctx, struct apk_db_options *dbopts, + int optch, int optindex, const char *optarg); + int (*main)(void *ctx, struct apk_database *db, int argc, char **argv); }; extern struct apk_applet *__start_apkapplets, *__stop_apkapplets; diff --git a/src/apk_database.h b/src/apk_database.h index 1ded91e..09f9ef7 100644 --- a/src/apk_database.h +++ b/src/apk_database.h @@ -79,6 +79,19 @@ struct apk_repository { struct apk_checksum csum; }; +struct apk_repository_list { + struct list_head list; + const char *url; +}; + +struct apk_db_options { + unsigned long open_flags; + char *root; + char *keys_dir; + char *repositories_file; + struct list_head repository_list; +}; + struct apk_database { char *root; int root_fd, lock_fd, cache_fd, cachetmp_fd, keys_fd; @@ -120,9 +133,9 @@ struct apk_db_file *apk_db_file_query(struct apk_database *db, apk_blob_t dir, apk_blob_t name); -#define APK_OPENF_READ 0x0000 -#define APK_OPENF_WRITE 0x0001 -#define APK_OPENF_CREATE 0x0002 +#define APK_OPENF_READ 0x0001 +#define APK_OPENF_WRITE 0x0002 +#define APK_OPENF_CREATE 0x0004 #define APK_OPENF_NO_INSTALLED 0x0010 #define APK_OPENF_NO_SCRIPTS 0x0020 #define APK_OPENF_NO_WORLD 0x0040 @@ -132,7 +145,7 @@ struct apk_db_file *apk_db_file_query(struct apk_database *db, 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, struct apk_db_options *dbopts); int apk_db_write_config(struct apk_database *db); void apk_db_close(struct apk_database *db); int apk_db_permanent(struct apk_database *db); diff --git a/src/audit.c b/src/audit.c index 67cefac..df6dbdc 100644 --- a/src/audit.c +++ b/src/audit.c @@ -144,7 +144,8 @@ static int audit_system(struct apk_database *db) return 0; } -static int audit_parse(void *ctx, int optch, int optindex, const char *optarg) +static int audit_parse(void *ctx, struct apk_db_options *dbopts, + int optch, int optindex, const char *optarg) { struct audit_ctx *actx = (struct audit_ctx *) ctx; @@ -161,26 +162,14 @@ static int audit_parse(void *ctx, int optch, int optindex, const char *optarg) return 0; } -static int audit_main(void *ctx, int argc, char **argv) +static int audit_main(void *ctx, struct apk_database *db, int argc, char **argv) { struct audit_ctx *actx = (struct audit_ctx *) ctx; - struct apk_database db; - int r; if (actx->audit == NULL) return -EINVAL; - r = apk_db_open(&db, apk_root, - APK_OPENF_READ | APK_OPENF_NO_SCRIPTS | - APK_OPENF_NO_REPOS); - if (r != 0) { - apk_error("Unable to open db: %s", apk_error_str(r)); - return r; - } - r = actx->audit(&db); - apk_db_close(&db); - - return r; + return actx->audit(db); } static struct apk_option audit_options[] = { @@ -194,6 +183,7 @@ static struct apk_applet apk_audit = { .name = "audit", .help = "Audit the filesystem for changes compared to installed " "database.", + .open_flags = APK_OPENF_READ|APK_OPENF_NO_SCRIPTS|APK_OPENF_NO_REPOS, .context_size = sizeof(struct audit_ctx), .num_options = ARRAY_SIZE(audit_options), .options = audit_options, diff --git a/src/cache.c b/src/cache.c index 641cecd..d4509e1 100644 --- a/src/cache.c +++ b/src/cache.c @@ -141,11 +141,9 @@ static int cache_clean(struct apk_database *db) return 0; } -static int cache_main(void *ctx, int argc, char **argv) +static int cache_main(void *ctx, struct apk_database *db, int argc, char **argv) { - struct apk_database db; - int actions = 0; - int r; + int r = 0, actions = 0; if (argc != 1) return -EINVAL; @@ -159,24 +157,18 @@ static int cache_main(void *ctx, int argc, char **argv) else return -EINVAL; - r = apk_db_open(&db, apk_root, APK_OPENF_READ | - APK_OPENF_NO_SCRIPTS | APK_OPENF_NO_INSTALLED); - if (r != 0) - return r; - - if (!apk_db_cache_active(&db)) { + if (!apk_db_cache_active(db)) { apk_error("Package cache is not enabled.\n"); r = 2; goto err; } if (r == 0 && (actions & CACHE_CLEAN)) - r = cache_clean(&db); + r = cache_clean(db); if (r == 0 && (actions & CACHE_DOWNLOAD)) - r = cache_download(&db); + r = cache_download(db); err: - apk_db_close(&db); return r; } @@ -187,6 +179,7 @@ static struct apk_applet apk_cache = { "making /etc/apk/cache a symlink to the directory (on boot " "media) that will be used as package cache.", .arguments = "sync | clean | download", + .open_flags = APK_OPENF_READ|APK_OPENF_NO_SCRIPTS|APK_OPENF_NO_INSTALLED, .main = cache_main, }; diff --git a/src/database.c b/src/database.c index f7c3ac0..ab43ea2 100644 --- a/src/database.c +++ b/src/database.c @@ -878,15 +878,21 @@ static void handle_alarm(int sig) { } -int apk_db_open(struct apk_database *db, const char *root, unsigned int flags) +int apk_db_open(struct apk_database *db, struct apk_db_options *dbopts) { - const char *apk_repos = getenv("APK_REPOS"), *msg = NULL; - struct apk_repository_url *repo = NULL; + const char *msg = NULL; + struct apk_repository_list *repo = NULL; struct stat64 st; apk_blob_t blob; int r, rr = 0; memset(db, 0, sizeof(*db)); + if (dbopts->open_flags == 0) { + msg = "Invalid open flags (internal error)"; + r = -1; + goto ret_r; + } + apk_hash_init(&db->available.names, &pkg_name_hash_ops, 1000); apk_hash_init(&db->available.packages, &pkg_info_hash_ops, 4000); apk_hash_init(&db->installed.dirs, &dir_hash_ops, 2000); @@ -895,11 +901,11 @@ int apk_db_open(struct apk_database *db, const char *root, unsigned int flags) db->cache_dir = apk_static_cache_dir; db->permanent = 1; - db->root = strdup(root); + db->root = strdup(dbopts->root ?: "/"); db->root_fd = openat(AT_FDCWD, db->root, O_RDONLY); - if (db->root_fd < 0 && (flags & APK_OPENF_CREATE)) { + if (db->root_fd < 0 && (dbopts->open_flags & APK_OPENF_CREATE)) { mkdirat(AT_FDCWD, db->root, 0755); - db->root_fd = openat(AT_FDCWD, root, O_RDONLY); + db->root_fd = openat(AT_FDCWD, db->root, O_RDONLY); } if (db->root_fd < 0) { msg = "Unable to open root"; @@ -912,11 +918,11 @@ int apk_db_open(struct apk_database *db, const char *root, unsigned int flags) S_ISDIR(st.st_mode)) db->cache_dir = apk_linked_cache_dir; - if (flags & APK_OPENF_WRITE) { + if (dbopts->open_flags & APK_OPENF_WRITE) { db->lock_fd = openat(db->root_fd, "var/lib/apk/lock", O_CREAT | O_RDWR, 0400); if (db->lock_fd < 0 && errno == ENOENT && - (flags & APK_OPENF_CREATE)) { + (dbopts->open_flags & APK_OPENF_CREATE)) { r = apk_db_create(db); if (r != 0) { msg = "Unable to create database"; @@ -954,30 +960,32 @@ int apk_db_open(struct apk_database *db, const char *root, unsigned int flags) db->cache_fd = openat(db->root_fd, db->cache_dir, O_RDONLY); mkdirat(db->cache_fd, "tmp", 0644); db->cachetmp_fd = openat(db->cache_fd, "tmp", O_RDONLY); - db->keys_fd = openat(db->root_fd, "etc/apk/keys", O_RDONLY); + db->keys_fd = openat(db->root_fd, + dbopts->keys_dir ?: "etc/apk/keys", + O_RDONLY); - r = apk_db_read_state(db, flags); - if (r == -ENOENT && (flags & APK_OPENF_CREATE)) { + r = apk_db_read_state(db, dbopts->open_flags); + if (r == -ENOENT && (dbopts->open_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, flags); + r = apk_db_read_state(db, dbopts->open_flags); } if (r != 0) { msg = "Unable to read database state"; goto ret_r; } - if (!(flags & APK_OPENF_NO_REPOS)) { - list_for_each_entry(repo, &apk_repository_list.list, list) { + if (!(dbopts->open_flags & APK_OPENF_NO_REPOS)) { + list_for_each_entry(repo, &dbopts->repository_list, list) { r = apk_db_add_repository(db, APK_BLOB_STR(repo->url)); rr = r ?: rr; } - if (apk_repos == NULL) - apk_repos = "etc/apk/repositories"; - blob = apk_blob_from_file(db->root_fd, apk_repos); + blob = apk_blob_from_file( + db->root_fd, + dbopts->repositories_file ?: "etc/apk/repositories"); if (!APK_BLOB_IS_NULL(blob)) { r = apk_blob_for_each_segment( blob, "\n", diff --git a/src/del.c b/src/del.c index df452b1..26dce6a 100644 --- a/src/del.c +++ b/src/del.c @@ -14,7 +14,8 @@ #include "apk_state.h" #include "apk_database.h" -static int del_parse(void *ctx, int optch, int optindex, const char *optarg) +static int del_parse(void *ctx, struct apk_db_options *db, + int optch, int optindex, const char *optarg) { switch (optch) { case 'r': @@ -26,33 +27,29 @@ static int del_parse(void *ctx, int optch, int optindex, const char *optarg) return 0; } -static int del_main(void *ctx, int argc, char **argv) +static int del_main(void *ctx, struct apk_database *db, int argc, char **argv) { - struct apk_database db; struct apk_state *state; struct apk_name *name; int i, r = 0; - if (apk_db_open(&db, apk_root, APK_OPENF_WRITE) < 0) - return -1; - - if (db.world == NULL) - goto out; + if (db->world == NULL) + return 0; for (i = 0; i < argc; i++) { - name = apk_db_get_name(&db, APK_BLOB_STR(argv[i])); + name = apk_db_get_name(db, APK_BLOB_STR(argv[i])); name->flags &= ~APK_NAME_TOPLEVEL; - apk_deps_del(&db.world, name); + apk_deps_del(&db->world, name); } - state = apk_state_new(&db); + state = apk_state_new(db); if (state == NULL) goto err; for (i = 0; i < argc; i++) { struct apk_dependency dep; - name = apk_db_get_name(&db, APK_BLOB_STR(argv[i])); + name = apk_db_get_name(db, APK_BLOB_STR(argv[i])); dep = (struct apk_dependency) { .name = name, .result_mask = APK_DEPMASK_CONFLICT, @@ -64,12 +61,10 @@ static int del_main(void *ctx, int argc, char **argv) goto err; } } - r = apk_state_commit(state, &db); + r = apk_state_commit(state, db); err: if (state != NULL) apk_state_unref(state); -out: - apk_db_close(&db); return r; } @@ -83,6 +78,7 @@ static struct apk_applet apk_del = { .name = "del", .help = "Remove PACKAGEs from the main dependencies and uninstall them.", .arguments = "PACKAGE...", + .open_flags = APK_OPENF_WRITE, .num_options = ARRAY_SIZE(del_options), .options = del_options, .parse = del_parse, diff --git a/src/fetch.c b/src/fetch.c index 7ddc913..9d10f7c 100644 --- a/src/fetch.c +++ b/src/fetch.c @@ -66,7 +66,8 @@ static int cup(void) return 0; } -static int fetch_parse(void *ctx, int optch, int optindex, const char *optarg) +static int fetch_parse(void *ctx, struct apk_db_options *dbopts, + int optch, int optindex, const char *optarg) { struct fetch_ctx *fctx = (struct fetch_ctx *) ctx; @@ -95,7 +96,7 @@ static int fetch_package(struct fetch_ctx *fctx, { struct apk_istream *is; char pkgfile[PATH_MAX], url[PATH_MAX]; - int i, r, fd; + int r, i, fd; apk_pkg_format_plain(pkg, APK_BLOB_BUF(pkgfile)); @@ -162,11 +163,10 @@ static int fetch_package(struct fetch_ctx *fctx, return 0; } -static int fetch_main(void *ctx, int argc, char **argv) +static int fetch_main(void *ctx, struct apk_database *db, int argc, char **argv) { struct fetch_ctx *fctx = (struct fetch_ctx *) ctx; - struct apk_database db; - int i, j, r; + int r = 0, i, j; if (fctx->outdir_fd == 0) fctx->outdir_fd = AT_FDCWD; @@ -178,13 +178,9 @@ static int fetch_main(void *ctx, int argc, char **argv) return 0; } - r = apk_db_open(&db, apk_root, APK_OPENF_NO_STATE); - if (r != 0) - return r; - for (i = 0; i < argc; i++) { struct apk_dependency dep = (struct apk_dependency) { - .name = apk_db_get_name(&db, APK_BLOB_STR(argv[i])), + .name = apk_db_get_name(db, APK_BLOB_STR(argv[i])), .result_mask = APK_DEPMASK_REQUIRE, }; @@ -192,7 +188,7 @@ static int fetch_main(void *ctx, int argc, char **argv) struct apk_state *state; struct apk_change *change; - state = apk_state_new(&db); + state = apk_state_new(db); if (state == NULL) goto err; @@ -205,7 +201,7 @@ static int fetch_main(void *ctx, int argc, char **argv) } list_for_each_entry(change, &state->change_list_head, change_list) { - r = fetch_package(fctx, &db, change->newpkg); + r = fetch_package(fctx, db, change->newpkg); if (r != 0) goto err; } @@ -221,7 +217,7 @@ static int fetch_main(void *ctx, int argc, char **argv) == APK_VERSION_GREATER) pkg = dep.name->pkgs->item[j]; - r = fetch_package(fctx, &db, pkg); + r = fetch_package(fctx, db, pkg); if (r != 0) goto err; } else { @@ -232,7 +228,6 @@ static int fetch_main(void *ctx, int argc, char **argv) } err: - apk_db_close(&db); return r; } @@ -250,6 +245,7 @@ static struct apk_applet apk_fetch = { .help = "Download PACKAGEs from repositories to a local directory from " "which a local mirror repository can be created.", .arguments = "PACKAGE...", + .open_flags = APK_OPENF_READ|APK_OPENF_NO_STATE, .context_size = sizeof(struct fetch_ctx), .num_options = ARRAY_SIZE(fetch_options), .options = fetch_options, diff --git a/src/fix.c b/src/fix.c index 3086428..e54631d 100644 --- a/src/fix.c +++ b/src/fix.c @@ -19,7 +19,8 @@ struct fix_ctx { unsigned int reinstall : 1; }; -static int fix_parse(void *pctx, int optch, int optindex, const char *optarg) +static int fix_parse(void *pctx, struct apk_db_options *dbopts, + int optch, int optindex, const char *optarg) { struct fix_ctx *ctx = (struct fix_ctx *) pctx; switch (optch) { @@ -35,24 +36,19 @@ static int fix_parse(void *pctx, int optch, int optindex, const char *optarg) return 0; } -static int fix_main(void *pctx, int argc, char **argv) +static int fix_main(void *pctx, struct apk_database *db, int argc, char **argv) { struct fix_ctx *ctx = (struct fix_ctx *) pctx; - struct apk_database db; struct apk_state *state = NULL; struct apk_name *name; - int r, i, j; + int r = 0, i, j; - r = apk_db_open(&db, apk_root, APK_OPENF_WRITE); - if (r != 0) - return r; - - state = apk_state_new(&db); + state = apk_state_new(db); if (state == NULL) - goto err; + return -1; for (i = 0; i < argc; i++) { - name = apk_db_get_name(&db, APK_BLOB_STR(argv[i])); + name = apk_db_get_name(db, APK_BLOB_STR(argv[i])); if (name == NULL) goto err; @@ -72,7 +68,7 @@ static int fix_main(void *pctx, int argc, char **argv) for (i = 0; i < argc; i++) { struct apk_dependency dep; - r = apk_dep_from_blob(&dep, &db, APK_BLOB_STR(argv[i])); + r = apk_dep_from_blob(&dep, db, APK_BLOB_STR(argv[i])); if (r != 0) goto err; @@ -82,13 +78,12 @@ static int fix_main(void *pctx, int argc, char **argv) goto err; } } - r = apk_state_commit(state, &db); + r = apk_state_commit(state, db); err: if (r != 0 && i < argc) apk_error("Error while processing '%s'", argv[i]); if (state != NULL) apk_state_unref(state); - apk_db_close(&db); return r; } @@ -102,6 +97,7 @@ static struct apk_applet apk_fix = { .help = "Repair package or upgrade it without modifying main " "dependencies.", .arguments = "PACKAGE...", + .open_flags = APK_OPENF_WRITE, .context_size = sizeof(struct fix_ctx), .num_options = ARRAY_SIZE(fix_options), .options = fix_options, diff --git a/src/index.c b/src/index.c index d9cecfe..fbf1bcb 100644 --- a/src/index.c +++ b/src/index.c @@ -29,7 +29,8 @@ struct index_ctx { int method; }; -static int index_parse(void *ctx, int optch, int optindex, const char *optarg) +static int index_parse(void *ctx, struct apk_db_options *dbopts, + int optch, int optindex, const char *optarg) { struct index_ctx *ictx = (struct index_ctx *) ctx; @@ -80,9 +81,8 @@ static int warn_if_no_providers(apk_hash_item item, void *ctx) return 0; } -static int index_main(void *ctx, int argc, char **argv) +static int index_main(void *ctx, struct apk_database *db, int argc, char **argv) { - struct apk_database db; struct counts counts = {0}; struct apk_ostream *os; struct apk_file_info fi; @@ -99,9 +99,7 @@ static int index_main(void *ctx, int argc, char **argv) if (ictx->method == 0) ictx->method = APK_SIGN_GENERATE; - apk_db_open(&db, apk_root, APK_OPENF_READ | APK_OPENF_NO_STATE | APK_OPENF_NO_REPOS); - if ((r = index_read_file(&db, ictx)) < 0) { - apk_db_close(&db); + if ((r = index_read_file(db, ictx)) < 0) { apk_error("%s: %s", ictx->index, apk_error_str(r)); return r; } @@ -136,7 +134,7 @@ static int index_main(void *ctx, int argc, char **argv) break; /* If we have it in the old index already? */ - name = apk_db_query_name(&db, bname); + name = apk_db_query_name(db, bname); if (name == NULL || name->pkgs == NULL) break; @@ -154,8 +152,8 @@ static int index_main(void *ctx, int argc, char **argv) if (!found) { struct apk_sign_ctx sctx; - apk_sign_ctx_init(&sctx, ictx->method, NULL, db.keys_fd); - if (apk_pkg_read(&db, argv[i], &sctx, NULL) == 0) + apk_sign_ctx_init(&sctx, ictx->method, NULL, db->keys_fd); + if (apk_pkg_read(db, argv[i], &sctx, NULL) == 0) newpkgs++; apk_sign_ctx_free(&sctx); } @@ -166,7 +164,7 @@ static int index_main(void *ctx, int argc, char **argv) fi.name = "APKINDEX"; fi.mode = 0644 | S_IFREG; os = apk_ostream_counter(&fi.size); - apk_db_index_write(&db, os); + apk_db_index_write(db, os); os->close(os); } @@ -178,15 +176,14 @@ static int index_main(void *ctx, int argc, char **argv) os = apk_ostream_gzip(os); apk_tar_write_entry(os, &fi, NULL); } - total = apk_db_index_write(&db, os); + total = apk_db_index_write(db, os); if (ictx->method == APK_SIGN_GENERATE) { apk_tar_write_padding(os, &fi); apk_tar_write_entry(os, NULL, NULL); } os->close(os); - apk_hash_foreach(&db.available.names, warn_if_no_providers, &counts); - apk_db_close(&db); + apk_hash_foreach(&db->available.names, warn_if_no_providers, &counts); if (counts.unsatisfied != 0) apk_warning("Total of %d unsatisfiable package " @@ -212,6 +209,7 @@ static struct apk_applet apk_index = { .name = "index", .help = "Create repository index file from FILEs.", .arguments = "FILE...", + .open_flags = APK_OPENF_READ | APK_OPENF_NO_STATE | APK_OPENF_NO_REPOS, .context_size = sizeof(struct index_ctx), .num_options = ARRAY_SIZE(index_options), .options = index_options, diff --git a/src/info.c b/src/info.c index 073dc80..c3fa275 100644 --- a/src/info.c +++ b/src/info.c @@ -96,7 +96,7 @@ static int info_exists(struct info_ctx *ctx, struct apk_database *db, name = dep.name; if (name->pkgs == NULL) continue; - + for (j = 0; j < name->pkgs->num; j++) { pkg = name->pkgs->item[j]; if (apk_pkg_get_state(pkg) == APK_PKG_INSTALLED) @@ -281,7 +281,8 @@ static void info_print_description(struct apk_package *pkg) printf("%s-%s description:\n%s\n", pkg->name->name, pkg->version, pkg->description); } -static int info_parse(void *ctx, int optch, int optindex, const char *optarg) +static int info_parse(void *ctx, struct apk_db_options *dbopts, + int optch, int optindex, const char *optarg) { struct info_ctx *ictx = (struct info_ctx *) ctx; @@ -317,22 +318,14 @@ static int info_parse(void *ctx, int optch, int optindex, const char *optarg) return 0; } -static int info_main(void *ctx, int argc, char **argv) +static int info_main(void *ctx, struct apk_database *db, int argc, char **argv) { struct info_ctx *ictx = (struct info_ctx *) ctx; - struct apk_database db; - int r; - - if (apk_db_open(&db, apk_root, APK_OPENF_READ | APK_OPENF_NO_REPOS) < 0) - return -1; if (ictx->action != NULL) - r = ictx->action(ictx, &db, argc, argv); - else - r = info_list(ictx, &db, argc, argv); + return ictx->action(ictx, db, argc, argv); - apk_db_close(&db); - return r; + return info_list(ictx, db, argc, argv); } static struct apk_option info_options[] = { @@ -350,6 +343,7 @@ static struct apk_applet apk_info = { .name = "info", .help = "Give detailed information about PACKAGEs.", .arguments = "PACKAGE...", + .open_flags = APK_OPENF_READ | APK_OPENF_NO_REPOS, .context_size = sizeof(struct info_ctx), .num_options = ARRAY_SIZE(info_options), .options = info_options, diff --git a/src/search.c b/src/search.c index 52429eb..91c2913 100644 --- a/src/search.c +++ b/src/search.c @@ -98,7 +98,8 @@ static int search_desc(struct apk_database *db, int argc, char **argv) return 0; } -static int search_parse(void *ctx, int optch, int optindex, const char *optarg) +static int search_parse(void *ctx, struct apk_db_options *dbopts, + int optch, int optindex, const char *optarg) { struct search_ctx *ictx = (struct search_ctx *) ctx; @@ -112,22 +113,14 @@ static int search_parse(void *ctx, int optch, int optindex, const char *optarg) return 0; } -static int search_main(void *ctx, int argc, char **argv) +static int search_main(void *ctx, struct apk_database *db, int argc, char **argv) { struct search_ctx *ictx = (struct search_ctx *) ctx; - struct apk_database db; - int r; - - if (apk_db_open(&db, apk_root, APK_OPENF_READ | APK_OPENF_NO_STATE) < 0) - return -1; if (ictx->action != NULL) - r = ictx->action(&db, argc, argv); - else - r = search_list(&db, argc, argv); + return ictx->action(db, argc, argv); - apk_db_close(&db); - return r; + return search_list(db, argc, argv); } static struct apk_option search_options[] = { @@ -138,6 +131,7 @@ static struct apk_applet apk_search = { .name = "search", .help = "Search package names (and descriptions) by wildcard PATTERN.", .arguments = "PATTERN", + .open_flags = APK_OPENF_READ | APK_OPENF_NO_STATE, .context_size = sizeof(struct search_ctx), .num_options = ARRAY_SIZE(search_options), .options = search_options, diff --git a/src/update.c b/src/update.c index abf4021..5bf7295 100644 --- a/src/update.c +++ b/src/update.c @@ -15,21 +15,16 @@ #include "apk_database.h" #include "apk_version.h" -static int update_main(void *ctx, int argc, char **argv) +static int update_main(void *ctx, struct apk_database *db, int argc, char **argv) { - struct apk_database db; - - apk_flags |= APK_UPDATE_CACHE; - if (apk_db_open(&db, apk_root, APK_OPENF_READ) < 0) - return -1; - apk_db_close(&db); - return 0; } static struct apk_applet apk_update = { .name = "update", .help = "Update repository indexes from all remote repositories.", + .open_flags = APK_OPENF_READ | APK_OPENF_NO_STATE, + .forced_flags = APK_UPDATE_CACHE, .main = update_main, }; diff --git a/src/upgrade.c b/src/upgrade.c index 333035f..f91e8cc 100644 --- a/src/upgrade.c +++ b/src/upgrade.c @@ -16,7 +16,8 @@ #include "apk_database.h" #include "apk_state.h" -static int upgrade_parse(void *ctx, int optch, int optindex, const char *optarg) +static int upgrade_parse(void *ctx, struct apk_db_options *dbopts, + int optch, int optindex, const char *optarg) { switch (optch) { case 'a': @@ -28,35 +29,29 @@ static int upgrade_parse(void *ctx, int optch, int optindex, const char *optarg) return 0; } -static int upgrade_main(void *ctx, int argc, char **argv) +static int upgrade_main(void *ctx, struct apk_database *db, int argc, char **argv) { - struct apk_database db; struct apk_state *state = NULL; - int i, r; + int i, r = 0; apk_flags |= APK_UPGRADE; - r = apk_db_open(&db, apk_root, APK_OPENF_WRITE); - if (r != 0) - return r; - - state = apk_state_new(&db); + state = apk_state_new(db); if (state == NULL) goto err; - for (i = 0; i < db.world->num; i++) { - r = apk_state_lock_dependency(state, &db.world->item[i]); + for (i = 0; i < db->world->num; i++) { + r = apk_state_lock_dependency(state, &db->world->item[i]); if (r != 0) { apk_error("Unable to upgrade '%s'", - db.world->item[i].name->name); + db->world->item[i].name->name); goto err; } } - r = apk_state_commit(state, &db); + r = apk_state_commit(state, db); err: if (state != NULL) apk_state_unref(state); - apk_db_close(&db); return r; } @@ -70,6 +65,7 @@ static struct apk_applet apk_upgrade = { .name = "upgrade", .help = "Upgrade (or downgrade with -a) the currently installed " "packages to versions available in repositories.", + .open_flags = APK_OPENF_WRITE, .num_options = ARRAY_SIZE(upgrade_options), .options = upgrade_options, .parse = upgrade_parse, diff --git a/src/ver.c b/src/ver.c index d9b35f3..24754c1 100644 --- a/src/ver.c +++ b/src/ver.c @@ -45,7 +45,8 @@ static int ver_validate(int argc, char **argv) return r; } -static int ver_parse(void *ctx, int opt, int optindex, const char *optarg) +static int ver_parse(void *ctx, struct apk_db_options *dbopts, + int opt, int optindex, const char *optarg) { struct ver_ctx *ictx = (struct ver_ctx *) ctx; switch (opt) { @@ -89,10 +90,9 @@ static void ver_print_package_status(struct apk_package *pkg, const char *limit) printf("%-40s%s %s\n", pkgname, opstr, latest); } -static int ver_main(void *ctx, int argc, char **argv) +static int ver_main(void *ctx, struct apk_database *db, int argc, char **argv) { struct ver_ctx *ictx = (struct ver_ctx *) ctx; - struct apk_database db; struct apk_package *pkg; struct apk_name *name; int i, j, ret = 0; @@ -101,14 +101,11 @@ static int ver_main(void *ctx, int argc, char **argv) if (ictx->action != NULL) return ictx->action(argc, argv); - if (apk_db_open(&db, apk_root, APK_OPENF_READ) < 0) - return -1; - if (apk_verbosity > 0) printf("%-42sAvailable:\n", "Installed:"); if (argc == 0) { - list_for_each_entry(pkg, &db.installed.packages, + list_for_each_entry(pkg, &db->installed.packages, installed_pkgs_list) { ver_print_package_status(pkg, ictx->limchars); } @@ -116,7 +113,7 @@ static int ver_main(void *ctx, int argc, char **argv) } for (i = 0; i < argc; i++) { - name = apk_db_query_name(&db, APK_BLOB_STR(argv[i])); + name = apk_db_query_name(db, APK_BLOB_STR(argv[i])); if (name == NULL) { apk_error("Not found: %s", name); ret = 1; @@ -130,7 +127,6 @@ static int ver_main(void *ctx, int argc, char **argv) } ver_exit: - apk_db_close(&db); return ret; } @@ -145,6 +141,7 @@ static struct apk_applet apk_ver = { .name = "version", .help = "Compare package versions (in installed database vs. available)" " or do tests on version strings given on command line.", + .open_flags = APK_OPENF_READ, .context_size = sizeof(struct ver_ctx), .num_options = ARRAY_SIZE(ver_options), .options = ver_options, diff --git a/src/verify.c b/src/verify.c index dc9ddb6..e9505ca 100644 --- a/src/verify.c +++ b/src/verify.c @@ -16,20 +16,14 @@ #include "apk_applet.h" #include "apk_database.h" -static int verify_main(void *ctx, int argc, char **argv) +static int verify_main(void *ctx, struct apk_database *db, int argc, char **argv) { struct apk_sign_ctx sctx; struct apk_istream *is; - struct apk_database db; int i, r, ok, rc = 0; - apk_flags |= APK_ALLOW_UNTRUSTED; - r = apk_db_open(&db, apk_root, APK_OPENF_READ | APK_OPENF_NO_STATE); - if (r != 0) - return r; - for (i = 0; i < argc; i++) { - apk_sign_ctx_init(&sctx, APK_SIGN_VERIFY, NULL, db.keys_fd); + apk_sign_ctx_init(&sctx, APK_SIGN_VERIFY, NULL, db->keys_fd); is = apk_bstream_gunzip_mpart(apk_bstream_from_file(AT_FDCWD, argv[i]), apk_sign_ctx_mpart_cb, &sctx); if (is == NULL) { @@ -49,7 +43,6 @@ static int verify_main(void *ctx, int argc, char **argv) rc++; apk_sign_ctx_free(&sctx); } - apk_db_close(&db); return rc; } @@ -58,6 +51,8 @@ static struct apk_applet apk_verify = { .name = "verify", .help = "Verify package integrity and signature", .arguments = "FILE...", + .open_flags = APK_OPENF_READ | APK_OPENF_NO_STATE, + .forced_flags = APK_ALLOW_UNTRUSTED, .main = verify_main, }; -- cgit v1.2.3