From d2d09898037ce78f282d16d13d30f2f673a805d1 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 6 Oct 2023 16:21:54 +0000 Subject: [PATCH] path: Correctly set and return absolute paths Signed-off-by: Michael Tremer --- src/libpakfire/path.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/libpakfire/path.c b/src/libpakfire/path.c index 8e13cac11..dfe8c7916 100644 --- a/src/libpakfire/path.c +++ b/src/libpakfire/path.c @@ -44,6 +44,7 @@ static void pakfire_path_free_segments(struct pakfire_path* path) { // Reset the data structure path->segments = NULL; path->num_segments = 0; + path->is_absolute = 0; } static void pakfire_path_free(struct pakfire_path* path) { @@ -100,11 +101,11 @@ static int pakfire_path_import_segments(struct pakfire_path* path, const char* s // Is the path absolute? if (*s == '/') { - path->is_absolute = 1; - // If we are joining strings and the new string is absolute, // we throw away all segments. pakfire_path_free_segments(path); + + path->is_absolute = 1; } // Copy path into buffer @@ -175,14 +176,19 @@ static int pakfire_path_to_string(struct pakfire_path* path, char* buffer, const return -ENOBUFS; // Return / for empty paths - if (!path->num_segments) - return __pakfire_string_set(buffer, length, "/"); + if (!path->num_segments) { + if (path->is_absolute) + return __pakfire_string_set(buffer, length, "/"); + else + return __pakfire_string_set(buffer, length, ""); + } for (unsigned int i = 0; i < path->num_segments; i++) { s = path->segments[i]; // Add separator - *p++ = '/'; + if (i > 0 || path->is_absolute) + *p++ = '/'; // Add the segment while (*s) -- 2.47.3