From c8845af461ff240d8b0ec2ba00965f6bfae49e12 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Thu, 9 Mar 2023 18:39:56 +0000 Subject: [PATCH] pakfire: Allow setting a custom cache path through the configuration Signed-off-by: Michael Tremer --- src/libpakfire/pakfire.c | 42 +++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index b223565e..584416ce 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -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 -- 2.47.3