From: Lennart Poettering Date: Wed, 13 Jul 2022 21:43:36 +0000 (+0200) Subject: stat-util: replace is_dir() + is_dir_fd() by single is_dir_full() call X-Git-Tag: v252-rc1~651^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a586dc791ca465f4087473d2ad6794b7776aee2d;p=thirdparty%2Fsystemd.git stat-util: replace is_dir() + is_dir_fd() by single is_dir_full() call This new call can execute both of the old operations, but also do generic fstatat() like behaviour. --- diff --git a/src/basic/stat-util.c b/src/basic/stat-util.c index 64c2f80f3c3..c31b4d89d02 100644 --- a/src/basic/stat-util.c +++ b/src/basic/stat-util.c @@ -35,31 +35,23 @@ int is_symlink(const char *path) { return !!S_ISLNK(info.st_mode); } -int is_dir(const char* path, bool follow) { +int is_dir_full(int atfd, const char* path, bool follow) { struct stat st; int r; - assert(path); + assert(atfd >= 0 || atfd == AT_FDCWD); + assert(atfd >= 0 || path); - if (follow) - r = stat(path, &st); + if (path) + r = fstatat(atfd, path, &st, follow ? 0 : AT_SYMLINK_NOFOLLOW); else - r = lstat(path, &st); + r = fstat(atfd, &st); if (r < 0) return -errno; return !!S_ISDIR(st.st_mode); } -int is_dir_fd(int fd) { - struct stat st; - - if (fstat(fd, &st) < 0) - return -errno; - - return !!S_ISDIR(st.st_mode); -} - int is_device_node(const char *path) { struct stat info; diff --git a/src/basic/stat-util.h b/src/basic/stat-util.h index 7f0b3dc0af5..56f15534aa2 100644 --- a/src/basic/stat-util.h +++ b/src/basic/stat-util.h @@ -13,8 +13,13 @@ #include "missing_stat.h" int is_symlink(const char *path); -int is_dir(const char *path, bool follow); -int is_dir_fd(int fd); +int is_dir_full(int atfd, const char *fname, bool follow); +static inline int is_dir(const char *path, bool follow) { + return is_dir_full(AT_FDCWD, path, follow); +} +static inline int is_dir_fd(int fd) { + return is_dir_full(fd, NULL, false); +} int is_device_node(const char *path); int dir_is_empty_at(int dir_fd, const char *path, bool ignore_hidden_or_backup);