]> git.ipfire.org Git - pakfire.git/commitdiff
path: Add function to expand paths
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 30 Jan 2025 15:19:15 +0000 (15:19 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 30 Jan 2025 15:19:15 +0000 (15:19 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/ctx.c
src/pakfire/path.c
src/pakfire/path.h

index 8159766d4f86c9e8cf96a877437704ccc16b64b4..be95c61b1960de2d1fb12940c3ff6571ce4fed67 100644 (file)
@@ -25,7 +25,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <syslog.h>
-#include <wordexp.h>
 
 #include <curl/curl.h>
 #include <magic.h>
@@ -34,6 +33,7 @@
 #include <pakfire/ctx.h>
 #include <pakfire/logging.h>
 #include <pakfire/os.h>
+#include <pakfire/path.h>
 #include <pakfire/string.h>
 
 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
index 5c31b44abb129442a4828115d52d399c47b273e8..0f5a60e790f15718dae5534cb68391002ac55f4e 100644 (file)
@@ -27,6 +27,7 @@
 #include <sys/stat.h>
 #include <time.h>
 #include <unistd.h>
+#include <wordexp.h>
 
 #include <pakfire/path.h>
 #include <pakfire/string.h>
@@ -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);
 }
index 77ce9f23e1f06b06f345e60b9987c6cb361f61f3..37980a0e80b7fdf1843e69de75d3484b6f8faa42 100644 (file)
@@ -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);