]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
chase-symlinks: Return "." as path from chase_symlinks_at() instead of NULL
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 23 Feb 2023 13:24:56 +0000 (14:24 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 6 Mar 2023 12:42:40 +0000 (13:42 +0100)
src/basic/chase-symlinks.c

index 46e4e7ed7a84e64b3b5226313bfa8499c56f42b8..bab5a5b2864f0577320f611ebe59719a2b48b674 100644 (file)
@@ -215,7 +215,7 @@ int chase_symlinks_at(
                 return -errno;
 
         if (flags & CHASE_TRAIL_SLASH)
-                append_trail_slash = endswith(buffer, "/") || endswith(buffer, "/.");
+                append_trail_slash = ENDSWITH_SET(buffer, "/", "/.");
 
         for (todo = buffer;;) {
                 _cleanup_free_ char *first = NULL;
@@ -380,8 +380,15 @@ int chase_symlinks_at(
                 close_and_replace(fd, child);
         }
 
-        if (ret_path)
+        if (ret_path) {
+                if (!done) {
+                        done = strdup(append_trail_slash ? "./" : ".");
+                        if (!done)
+                                return -ENOMEM;
+                }
+
                 *ret_path = TAKE_PTR(done);
+        }
 
         if (ret_fd) {
                 /* Return the O_PATH fd we currently are looking to the caller. It can translate it to a
@@ -400,6 +407,12 @@ chased_one:
         if (ret_path) {
                 const char *e;
 
+                if (!done) {
+                        done = strdup(append_trail_slash ? "./" : ".");
+                        if (!done)
+                                return -ENOMEM;
+                }
+
                 /* todo may contain slashes at the beginning. */
                 r = path_find_first_component(&todo, /* accept_dot_dot= */ true, &e);
                 if (r < 0)
@@ -489,6 +502,12 @@ int chase_symlinks(
                 if (!q)
                         return -ENOMEM;
 
+                path_simplify(q);
+
+                if (FLAGS_SET(flags, CHASE_TRAIL_SLASH) && ENDSWITH_SET(path, "/", "/."))
+                        if (!strextend(&q, "/"))
+                                return -ENOMEM;
+
                 *ret_path = TAKE_PTR(q);
         }