]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
stat-util: Add dir_is_empty_at
authorRyan Gonzalez <rymg19@gmail.com>
Mon, 7 Jan 2019 18:27:38 +0000 (12:27 -0600)
committerRyan Gonzalez <rymg19@gmail.com>
Tue, 8 Jan 2019 16:23:17 +0000 (10:23 -0600)
src/basic/stat-util.c
src/basic/stat-util.h

index 57700e2388a1b8e0d671c2ba8ed093f88181b30c..fdc82c61bcd4c751b98fad645751e85b8eb7aa7a 100644 (file)
@@ -67,13 +67,22 @@ int is_device_node(const char *path) {
         return !!(S_ISBLK(info.st_mode) || S_ISCHR(info.st_mode));
 }
 
-int dir_is_empty(const char *path) {
-        _cleanup_closedir_ DIR *d;
+int dir_is_empty_at(int dir_fd, const char *path) {
+        _cleanup_close_ int fd = -1;
+        _cleanup_closedir_ DIR *d = NULL;
         struct dirent *de;
 
-        d = opendir(path);
+        if (path)
+                fd = openat(dir_fd, path, O_RDONLY|O_DIRECTORY|O_CLOEXEC);
+        else
+                fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
+        if (fd < 0)
+                return -errno;
+
+        d = fdopendir(fd);
         if (!d)
                 return -errno;
+        fd = -1;
 
         FOREACH_DIRENT(de, d, return -errno)
                 return 0;
index 0a08e642b54beab1c2282d69ce16a555c9053dcf..74fb7251b3872c4f08f38383bab7ab4c0f8979cd 100644 (file)
@@ -15,7 +15,10 @@ int is_dir(const char *path, bool follow);
 int is_dir_fd(int fd);
 int is_device_node(const char *path);
 
-int dir_is_empty(const char *path);
+int dir_is_empty_at(int dir_fd, const char *path);
+static inline int dir_is_empty(const char *path) {
+        return dir_is_empty_at(AT_FDCWD, path);
+}
 
 static inline int dir_is_populated(const char *path) {
         int r;