]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
stat-util: replace is_dir() + is_dir_fd() by single is_dir_full() call
authorLennart Poettering <lennart@poettering.net>
Wed, 13 Jul 2022 21:43:36 +0000 (23:43 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 14 Jul 2022 08:11:50 +0000 (10:11 +0200)
This new call can execute both of the old operations, but also do
generic fstatat() like behaviour.

src/basic/stat-util.c
src/basic/stat-util.h

index 64c2f80f3c3ff6b1a6b69e2f46ac6406bbe4d362..c31b4d89d020e35c6d00be51bfa8f5377a4498d1 100644 (file)
@@ -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;
 
index 7f0b3dc0af5567b075009e307a4f511e4e4d0900..56f15534aa2565d51f0f6b0f1ecd8bc44785f93b 100644 (file)
 #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);