From: Michael Tremer Date: Thu, 30 Jan 2025 15:19:15 +0000 (+0000) Subject: path: Add function to expand paths X-Git-Tag: 0.9.30~259 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f2e5da2ea3f02698f78368ca7bcd901841267a7e;p=pakfire.git path: Add function to expand paths Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/ctx.c b/src/pakfire/ctx.c index 8159766d..be95c61b 100644 --- a/src/pakfire/ctx.c +++ b/src/pakfire/ctx.c @@ -25,7 +25,6 @@ #include #include #include -#include #include #include @@ -34,6 +33,7 @@ #include #include #include +#include #include struct pakfire_ctx { @@ -314,31 +314,7 @@ const char* pakfire_ctx_get_cache_path(struct pakfire_ctx* ctx) { } 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; - -ERROR: - wordfree(&result); - - return r; + return pakfire_path_expand(ctx->paths.cache, path); } // Confirm diff --git a/src/pakfire/path.c b/src/pakfire/path.c index 5c31b44a..0f5a60e7 100644 --- a/src/pakfire/path.c +++ b/src/pakfire/path.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -720,6 +721,34 @@ int __pakfire_path_realpath(char* dest, const size_t length, const char* path) { return __pakfire_string_set(dest, length, buffer); } +int __pakfire_path_expand(char* dest, const size_t length, 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(dest, length, result.we_wordv[0]); + if (r < 0) + goto ERROR; + +ERROR: + wordfree(&result); + + return r; +} + int pakfire_path_exists(const char* path) { return !access(path, F_OK); } diff --git a/src/pakfire/path.h b/src/pakfire/path.h index 77ce9f23..37980a0e 100644 --- a/src/pakfire/path.h +++ b/src/pakfire/path.h @@ -70,6 +70,10 @@ int __pakfire_path_replace_extension(char* path, const size_t length, const char __pakfire_path_realpath(dest, sizeof(dest), path) int __pakfire_path_realpath(char* dest, const size_t length, const char* path); +#define pakfire_path_expand(dest, path) \ + __pakfire_path_expand(dest, sizeof(dest), path) +int __pakfire_path_expand(char* dest, const size_t length, const char* path); + int pakfire_path_exists(const char* path); time_t pakfire_path_age(const char* path);