]> git.ipfire.org Git - pakfire.git/commitdiff
ctx: Store cache path
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 31 Oct 2023 12:46:13 +0000 (12:46 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 31 Oct 2023 12:49:35 +0000 (12:49 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/cli/pakfire-builder.c
src/libpakfire/ctx.c
src/libpakfire/include/pakfire/ctx.h
src/libpakfire/libpakfire.sym
src/libpakfire/pakfire.c

index a43183efead375cb91eebce8506e0d0d854b427f..583b4a1f81632ac7facb51a9347790d296cdd1dd 100644 (file)
@@ -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),
index aff3a02a30771ed954b6343bf603590a099acfc8..15d20238a5defd296123fa45fcfb07ff2c94a8be 100644 (file)
 
 #include <ctype.h>
 #include <errno.h>
+#include <limits.h>
 #include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
 #include <syslog.h>
+#include <wordexp.h>
 
 #include <pakfire/config.h>
 #include <pakfire/ctx.h>
 #include <pakfire/logging.h>
 #include <pakfire/os.h>
 #include <pakfire/private.h>
+#include <pakfire/string.h>
 
 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,
index b72c8cd624154551aab773fde7f313bdd2c682da..ea09146e3dd81bd72cd59b3dd23f0bf03464bf61 100644 (file)
@@ -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,
index 80493ffad2c1c5c2dc44148da0aa47d5fcd1a014..57ffb8295da514bdf36a34c8522475a1c41086cf 100644 (file)
@@ -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;
index 247e1beac5130d9260947fd066232e351ffab2d0..77bece47733a16ce772f3aae2cf3e162fa669e56 100644 (file)
@@ -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) {