]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
chase: check the result is a directory or regular file only when the resolved path...
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 28 Jul 2025 18:25:17 +0000 (03:25 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 28 Jul 2025 18:37:48 +0000 (03:37 +0900)
Otherwise, if it is called with CHASE_NONEXISTENT, when we call
stat_verify_directory()/_regular() the struct stat is for one of the
parent directory, rather than for the result path.

With this change, we can safely specify CHASE_MUST_BE_DIRECTORY/REGULAR
with CHASE_NONEXISTENT.

More importantly, chaseat() internally sets CHASE_MUST_BE_DIRECTORY when
the input path ends with "/", "/,", "/..". Hence, without this change,
we cannot specify CHASE_NONEXISTENT safely.

Follow-up for 90b9f7a07e6f57825f416f6ce2db0a9f2086754b.

src/basic/chase.c

index 3a929498bf2cc1a1a832497bad244d162efd3f32..68bb9816ce40f251e1c4b5e13ba25d58ff71eae0 100644 (file)
@@ -509,16 +509,18 @@ int chaseat(int dir_fd, const char *path, ChaseFlags flags, char **ret_path, int
                 close_and_replace(fd, child);
         }
 
-        if (FLAGS_SET(flags, CHASE_MUST_BE_DIRECTORY)) {
-                r = stat_verify_directory(&st);
-                if (r < 0)
-                        return r;
-        }
+        if (exists) {
+                if (FLAGS_SET(flags, CHASE_MUST_BE_DIRECTORY)) {
+                        r = stat_verify_directory(&st);
+                        if (r < 0)
+                                return r;
+                }
 
-        if (FLAGS_SET(flags, CHASE_MUST_BE_REGULAR)) {
-                r = stat_verify_regular(&st);
-                if (r < 0)
-                        return r;
+                if (FLAGS_SET(flags, CHASE_MUST_BE_REGULAR)) {
+                        r = stat_verify_regular(&st);
+                        if (r < 0)
+                                return r;
+                }
         }
 
         if (ret_path) {