]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
file_utils: add exists_dir_at()
authorChristian Brauner <christian.brauner@ubuntu.com>
Sun, 9 Aug 2020 17:33:23 +0000 (19:33 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Sun, 9 Aug 2020 17:52:32 +0000 (19:52 +0200)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/file_utils.c
src/lxc/file_utils.h
src/lxc/utils.c

index 70791f29faf2c4925a312177f0cb003096159db8..46912642f71da0ccea82c3f6e8a63155011a2d4c 100644 (file)
@@ -539,3 +539,15 @@ int timens_offset_write(clockid_t clk_id, int64_t s_offset, int64_t ns_offset)
 
        return 0;
 }
+
+bool exists_dir_at(int dir_fd, const char *path)
+{
+       struct stat sb;
+       int ret;
+
+       ret = fstatat(dir_fd, path, &sb, 0);
+       if (ret < 0)
+               return false;
+
+       return S_ISDIR(sb.st_mode);
+}
index a759a6f3611efabf2189f869acd73219a1f2c39d..48571185d28260d145bec9decc70bc109909a5ce 100644 (file)
@@ -73,5 +73,6 @@ __hidden extern int lxc_open_dirfd(const char *dir);
 __hidden extern FILE *fdopen_cached(int fd, const char *mode, void **caller_freed_buffer);
 __hidden extern FILE *fopen_cached(const char *path, const char *mode, void **caller_freed_buffer);
 __hidden extern int timens_offset_write(clockid_t clk_id, int64_t s_offset, int64_t ns_offset);
+__hidden extern bool exists_dir_at(int dir_fd, const char *path);
 
 #endif /* __LXC_FILE_UTILS_H */
index 0649c02957f6965034d1a7f58358ec4f662e067e..9dd34050d26e2c153fc7f2a34be4de7fd9deb6fa 100644 (file)
@@ -569,15 +569,7 @@ gid_t get_ns_gid(gid_t orig)
 
 bool dir_exists(const char *path)
 {
-       struct stat sb;
-       int ret;
-
-       ret = stat(path, &sb);
-       if (ret < 0)
-               /* Could be something other than eexist, just say "no". */
-               return false;
-
-       return S_ISDIR(sb.st_mode);
+       return exists_dir_at(-1, path);
 }
 
 /* Note we don't use SHA-1 here as we don't want to depend on HAVE_GNUTLS.