From e212f422796da9e626030289faf083407c8955df Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 2 Apr 2023 01:25:46 +0900 Subject: [PATCH] fd-util: introduce dir_fd_is_root_or_cwd() --- src/basic/chase.c | 5 +---- src/basic/fd-util.h | 4 ++++ src/shared/find-esp.c | 5 +---- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/basic/chase.c b/src/basic/chase.c index 7d87f998f1..318454ca88 100644 --- a/src/basic/chase.c +++ b/src/basic/chase.c @@ -162,10 +162,7 @@ int chaseat(int dir_fd, const char *path, ChaseFlags flags, char **ret_path, int /* If we get AT_FDCWD or dir_fd points to "/", then we always resolve symlinks relative to * the host's root. Hence, CHASE_AT_RESOLVE_IN_ROOT is meaningless. */ - if (dir_fd >= 0) - r = dir_fd_is_root(dir_fd); - else - r = true; + r = dir_fd_is_root_or_cwd(dir_fd); if (r < 0) return r; if (r > 0) diff --git a/src/basic/fd-util.h b/src/basic/fd-util.h index 91f3d7fe9d..655ad29284 100644 --- a/src/basic/fd-util.h +++ b/src/basic/fd-util.h @@ -2,6 +2,7 @@ #pragma once #include +#include #include #include #include @@ -101,6 +102,9 @@ int read_nr_open(void); int fd_get_diskseq(int fd, uint64_t *ret); int dir_fd_is_root(int dir_fd); +static inline int dir_fd_is_root_or_cwd(int dir_fd) { + return dir_fd == AT_FDCWD ? true : dir_fd_is_root(dir_fd); +} /* The maximum length a buffer for a /proc/self/fd/ path needs */ #define PROC_FD_PATH_MAX \ diff --git a/src/shared/find-esp.c b/src/shared/find-esp.c index 60f80387f8..0d45249d63 100644 --- a/src/shared/find-esp.c +++ b/src/shared/find-esp.c @@ -444,10 +444,7 @@ int find_esp_and_warn_at( assert(rfd >= 0 || rfd == AT_FDCWD); - if (rfd >= 0) - r = dir_fd_is_root(rfd); - else - r = true; + r = dir_fd_is_root_or_cwd(rfd); if (r < 0) return log_error_errno(r, "Failed to check if directory file descriptor is root: %m"); if (r == 0) -- 2.25.1