From: Yu Watanabe Date: Thu, 20 Jul 2023 05:56:43 +0000 (+0900) Subject: chase: reuse "done" to open fd of starting point X-Git-Tag: v255-rc1~875^2~6 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=00a050b395b6c38f6dea86fd660741bba00fadf0;p=thirdparty%2Fsystemd.git chase: reuse "done" to open fd of starting point 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. --- diff --git a/src/basic/chase.c b/src/basic/chase.c index a4d9edb4c97..2db0b412618 100644 --- a/src/basic/chase.c +++ b/src/basic/chase.c @@ -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, "/", "/.");