]> git.ipfire.org Git - pakfire.git/commitdiff
path: Correctly set and return absolute paths
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 6 Oct 2023 16:21:54 +0000 (16:21 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 6 Oct 2023 16:21:54 +0000 (16:21 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/path.c

index 8e13cac112636866942f2fa36244093223393aa6..dfe8c791677e14f8dc8ddade7eb6ab0b39c0cc87 100644 (file)
@@ -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)