diff options
author | Timo Teräs <timo.teras@iki.fi> | 2020-10-09 12:44:32 +0300 |
---|---|---|
committer | Timo Teräs <timo.teras@iki.fi> | 2020-10-09 16:09:19 +0300 |
commit | a627ab8eb8387ebf5735cf525ae72bfa159aa39a (patch) | |
tree | 6c11a133c2743d03c4b4bc8be0f9cb42819aaee7 /src/context.c | |
parent | 354713d2f746c197eed6a1feb4c6af3420af6c15 (diff) | |
download | apk-tools-a627ab8eb8387ebf5735cf525ae72bfa159aa39a.tar.gz apk-tools-a627ab8eb8387ebf5735cf525ae72bfa159aa39a.tar.bz2 apk-tools-a627ab8eb8387ebf5735cf525ae72bfa159aa39a.tar.xz apk-tools-a627ab8eb8387ebf5735cf525ae72bfa159aa39a.zip |
make apk_database optional for applets that don't need it
The new v3 applets don't use it, and eventually all applets will
use the new formats only.
This prepares the code for this, and moves trust, id_cache, and
root_fd to apk_ctx which is enough for v3 applets at this time.
The generic code is made to not initialize apk_database if it's
not needed.
Diffstat (limited to 'src/context.c')
-rw-r--r-- | src/context.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/context.c b/src/context.c index f7b745c..6f953b0 100644 --- a/src/context.c +++ b/src/context.c @@ -7,6 +7,9 @@ * SPDX-License-Identifier: GPL-2.0-only */ +#include <errno.h> +#include <unistd.h> +#include <sys/stat.h> #include "apk_context.h" void apk_ctx_init(struct apk_ctx *ac) @@ -22,6 +25,54 @@ void apk_ctx_init(struct apk_ctx *ac) void apk_ctx_free(struct apk_ctx *ac) { + apk_id_cache_free(&ac->id_cache); + adb_trust_free(&ac->trust); apk_string_array_free(&ac->repository_list); apk_string_array_free(&ac->private_keys); } + +int apk_ctx_prepare(struct apk_ctx *ac) +{ + if (ac->flags & APK_SIMULATE && + ac->open_flags & (APK_OPENF_CREATE | APK_OPENF_WRITE)) { + ac->open_flags &= ~(APK_OPENF_CREATE | APK_OPENF_WRITE); + ac->open_flags |= APK_OPENF_READ; + } + if (!ac->cache_dir) ac->cache_dir = "etc/apk/cache"; + if (!ac->keys_dir) ac->keys_dir = "etc/apk/keys"; + if (!ac->root) ac->root = "/"; + if (!ac->cache_max_age) ac->cache_max_age = 4*60*60; /* 4 hours default */ + + ac->root_fd = openat(AT_FDCWD, ac->root, O_RDONLY | O_CLOEXEC); + if (ac->root_fd < 0 && (ac->open_flags & APK_OPENF_CREATE)) { + mkdirat(AT_FDCWD, ac->root, 0755); + ac->root_fd = openat(AT_FDCWD, ac->root, O_RDONLY | O_CLOEXEC); + } + if (ac->root_fd < 0) { + apk_err(&ac->out, "Unable to open root: %s", apk_error_str(errno)); + return -errno; + } + return 0; +} + +int apk_ctx_fd_keys(struct apk_ctx *ac) +{ + if (ac->keys_fd <= 0) ac->keys_fd = openat(ac->root_fd, ac->keys_dir, O_RDONLY | O_CLOEXEC); + return ac->keys_fd; +} + +struct adb_trust *apk_ctx_get_trust(struct apk_ctx *ac) +{ + if (!ac->trust.mdctx) { + int r = adb_trust_init(&ac->trust, dup(apk_ctx_fd_keys(ac)), ac->private_keys); + if (r) return ERR_PTR(r); + } + return &ac->trust; +} + +struct apk_id_cache *apk_ctx_get_id_cache(struct apk_ctx *ac) +{ + if (!ac->id_cache.root_fd) + apk_id_cache_init(&ac->id_cache, apk_ctx_fd_root(ac)); + return &ac->id_cache; +} |