From 41250642007125f3a74643acd45470139cee760a Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 31 Oct 2023 12:46:13 +0000 Subject: [PATCH] ctx: Store cache path Signed-off-by: Michael Tremer --- src/cli/pakfire-builder.c | 5 +++ src/libpakfire/ctx.c | 49 ++++++++++++++++++++++++++++ src/libpakfire/include/pakfire/ctx.h | 5 +++ src/libpakfire/libpakfire.sym | 2 ++ src/libpakfire/pakfire.c | 18 ++-------- 5 files changed, 63 insertions(+), 16 deletions(-) diff --git a/src/cli/pakfire-builder.c b/src/cli/pakfire-builder.c index a43183ef..583b4a1f 100644 --- a/src/cli/pakfire-builder.c +++ b/src/cli/pakfire-builder.c @@ -146,6 +146,11 @@ int main(int argc, char* argv[]) { // Setup progress callback pakfire_ctx_set_progress_callback(ctx, cli_setup_progressbar, NULL); + // Set the default cache path + r = pakfire_ctx_set_cache_path(ctx, "~/.cache/pakfire"); + if (r) + goto ERROR; + struct cli_config config = { .ctx = ctx, .distro = cli_get_default_distro(ctx), diff --git a/src/libpakfire/ctx.c b/src/libpakfire/ctx.c index aff3a02a..15d20238 100644 --- a/src/libpakfire/ctx.c +++ b/src/libpakfire/ctx.c @@ -20,16 +20,19 @@ #include #include +#include #include #include #include #include +#include #include #include #include #include #include +#include struct pakfire_ctx { // Reference counter @@ -53,6 +56,11 @@ struct pakfire_ctx { void* data; } log; + // Paths + struct pakfire_ctx_paths { + char cache[PATH_MAX]; + } paths; + // Confirm struct pakfire_ctx_confirm { pakfire_confirm_callback callback; @@ -185,6 +193,11 @@ PAKFIRE_EXPORT int pakfire_ctx_create(struct pakfire_ctx** ctx, const char* path if (r) goto ERROR; + // Setup the default cache path + r = pakfire_ctx_set_cache_path(c, PAKFIRE_CACHE_DIR); + if (r) + goto ERROR; + // Load the distribution information r = pakfire_distro(&c->distro, NULL); if (r) @@ -275,6 +288,42 @@ void pakfire_ctx_log(struct pakfire_ctx* ctx, int level, const char* file, int l va_end(args); } +// Paths + +PAKFIRE_EXPORT const char* pakfire_ctx_get_cache_path(struct pakfire_ctx* ctx) { + return ctx->paths.cache; +} + +PAKFIRE_EXPORT int pakfire_ctx_set_cache_path(struct pakfire_ctx* ctx, const char* path) { + wordexp_t result = {}; + int r; + + // Expand the path + r = wordexp(path, &result, 0); + if (r) { + r = -EINVAL; + goto ERROR; + } + + // There should only be one result + if (result.we_wordc != 1) { + r = -EINVAL; + goto ERROR; + } + + // Store the result + r = pakfire_string_set(ctx->paths.cache, result.we_wordv[0]); + if (r) + goto ERROR; + + CTX_DEBUG(ctx, "Set cache path to %s\n", ctx->paths.cache); + +ERROR: + wordfree(&result); + + return r; +} + // Confirm PAKFIRE_EXPORT void pakfire_ctx_set_confirm_callback(struct pakfire_ctx* ctx, diff --git a/src/libpakfire/include/pakfire/ctx.h b/src/libpakfire/include/pakfire/ctx.h index b72c8cd6..ea09146e 100644 --- a/src/libpakfire/include/pakfire/ctx.h +++ b/src/libpakfire/include/pakfire/ctx.h @@ -55,6 +55,11 @@ void pakfire_ctx_set_log_level(struct pakfire_ctx* ctx, int level); void pakfire_ctx_set_log_callback(struct pakfire_ctx* ctx, pakfire_log_callback callback, void* data); +// Paths + +const char* pakfire_ctx_get_cache_path(struct pakfire_ctx* ctx); +int pakfire_ctx_set_cache_path(struct pakfire_ctx* ctx, const char* path); + // Confirm typedef int (*pakfire_confirm_callback)(struct pakfire_ctx* ctx, struct pakfire* pakfire, diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index 80493ffa..57ffb829 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -32,6 +32,8 @@ global: pakfire_ctx_set_pick_solution_callback; pakfire_ctx_has_flag; pakfire_ctx_set_flag; + pakfire_ctx_get_cache_path; + pakfire_ctx_set_cache_path; # pakfire pakfire_check; diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index 247e1bea..77bece47 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -677,26 +677,12 @@ static int pakfire_read_os_release(struct pakfire* pakfire) { } static int pakfire_set_cache_path(struct pakfire* pakfire) { - char basepath[PATH_MAX]; - int r; - + const char* cache_path = pakfire_ctx_get_cache_path(pakfire->ctx); const char* arch = pakfire_get_effective_arch(pakfire); - // Fetch the path from the configuration file - const char* path = pakfire_config_get(pakfire->config, "general", "cache_path", NULL); - if (!path) { - // Append a suffix to the home directory - r = pakfire_string_format(basepath, "%s/.pakfire/cache", pakfire->user.home); - if (r) - return 1; - - // Use the basepath as path - path = basepath; - } - // Format the final path return pakfire_string_format(pakfire->cache_path, "%s/%s/%s/%s", - path, pakfire->distro.id, pakfire->distro.version_id, arch); + cache_path, pakfire->distro.id, pakfire->distro.version_id, arch); } static int pakfire_setup_user(struct pakfire* pakfire) { -- 2.39.2