]> git.ipfire.org Git - pakfire.git/commitdiff
pakfire: Allow setting a custom cache path through the configuration
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 9 Mar 2023 18:39:56 +0000 (18:39 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 9 Mar 2023 18:39:56 +0000 (18:39 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/pakfire.c

index b223565e7e0382b4cb1d7c82645d7f50c3f6ca3d..584416ced616e8859015d7d5a64a36987e148de9 100644 (file)
@@ -717,6 +717,31 @@ ERROR:
        return r;
 }
 
+static int pakfire_set_cache_path(struct pakfire* pakfire) {
+       char basepath[PATH_MAX];
+       int r;
+
+       // Fetch the path from the configuration file
+       const char* path = pakfire_config_get(pakfire->config, "general", "cache_path", NULL);
+       if (!path) {
+               const char* home = pakfire_get_home(pakfire, pakfire->uid);
+               if (!home)
+                       return 1;
+
+               // Append a suffix to the home directory
+               r = pakfire_string_format(basepath, "%s/.pakfire/cache", 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, pakfire->arch);
+}
+
 PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, const char* path,
                const char* arch, const char* conf, int flags,
                int loglevel, pakfire_log_callback log_callback, void* log_data) {
@@ -858,19 +883,10 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, const char* path,
        }
 
        // Set cache path
-       if (p->uid) {
-               const char* home = pakfire_get_home(p, p->uid);
-               if (!home) {
-                       ERROR(p, "Could not find home directory\n");
-                       errno = ENOENT;
-                       goto ERROR;
-               }
-
-               pakfire_string_format(p->cache_path, "%s/.pakfire/cache/%s/%s/%s",
-                       home, p->distro.id, p->distro.version_id, p->arch);
-       } else {
-               pakfire_string_format(p->cache_path, "%s/%s/%s/%s",
-                       PAKFIRE_CACHE_DIR, p->distro.id, p->distro.version_id, p->arch);
+       r = pakfire_set_cache_path(p);
+       if (r) {
+               ERROR(p, "Could not set cache path: %m\n");
+               goto ERROR;
        }
 
        // Set keystore path