]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
chase: reuse "done" to open fd of starting point
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 20 Jul 2023 05:56:43 +0000 (14:56 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 29 Jul 2023 12:58:29 +0000 (21:58 +0900)
For readability that 'done' and 'fd' are always consistent with each other.

- dir_fd == AT_FDCWD:
  - path is absolute:
    - previous: fd = open("/")
    - current:  fd = openat(AT_FDCWD, "/")
  - path is relative:
    - previous: fd = openat(AT_FDCWD, ".")
    - current:  fd = openat(AT_FDCWD, ".")
- dir_fd >= 0:
  - dir_fd points to "/":
    - previous: fd = openat(dir_fd, ".")
    - current:  fd = openat(dir_fd, "/")
  - dir_fd does not point to "/":
    - previous: fd = openat(dir_fd, ".")
    - current:  fd = openat(dir_fd, ".")

Hence, this should not change any behavior. Just refactoring.

src/basic/chase.c

index a4d9edb4c9705ec2c7f4d9aa29ac8e3ffa395f2e..2db0b412618bff85846d2a1a2cddee78d270a847 100644 (file)
@@ -212,30 +212,27 @@ int chaseat(int dir_fd, const char *path, ChaseFlags flags, char **ret_path, int
                         return -ENOMEM;
         }
 
-        /* If we get AT_FDCWD, we always resolve symlinks relative to the host's root. Only if a positive
-         * directory file descriptor is provided we will look at CHASE_AT_RESOLVE_IN_ROOT to determine
-         * whether to resolve symlinks in it or not. */
-        if (dir_fd >= 0 && FLAGS_SET(flags, CHASE_AT_RESOLVE_IN_ROOT))
-                root_fd = openat(dir_fd, ".", O_CLOEXEC|O_DIRECTORY|O_PATH);
-        else
-                root_fd = open("/", O_CLOEXEC|O_DIRECTORY|O_PATH);
-        if (root_fd < 0)
-                return -errno;
-
         /* If a positive directory file descriptor is provided, always resolve the given path relative to it,
          * regardless of whether it is absolute or not. If we get AT_FDCWD, follow regular openat()
          * semantics, if the path is relative, resolve against the current working directory. Otherwise,
          * resolve against root. */
-        if (dir_fd >= 0 || !path_is_absolute(path))
-                fd = openat(dir_fd, ".", O_CLOEXEC|O_DIRECTORY|O_PATH);
-        else
-                fd = open("/", O_CLOEXEC|O_DIRECTORY|O_PATH);
+        fd = openat(dir_fd, done ?: ".", O_CLOEXEC|O_DIRECTORY|O_PATH);
         if (fd < 0)
                 return -errno;
 
         if (fstat(fd, &st) < 0)
                 return -errno;
 
+        /* If we get AT_FDCWD, we always resolve symlinks relative to the host's root. Only if a positive
+         * directory file descriptor is provided we will look at CHASE_AT_RESOLVE_IN_ROOT to determine
+         * whether to resolve symlinks in it or not. */
+        if (dir_fd >= 0 && FLAGS_SET(flags, CHASE_AT_RESOLVE_IN_ROOT))
+                root_fd = openat(dir_fd, ".", O_CLOEXEC|O_DIRECTORY|O_PATH);
+        else
+                root_fd = open("/", O_CLOEXEC|O_DIRECTORY|O_PATH);
+        if (root_fd < 0)
+                return -errno;
+
         if (FLAGS_SET(flags, CHASE_TRAIL_SLASH))
                 append_trail_slash = ENDSWITH_SET(buffer, "/", "/.");