]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tmpfiles: rework empty_directory() to also use chase_symlinks()
authorLennart Poettering <lennart@poettering.net>
Thu, 15 Sep 2022 18:35:04 +0000 (19:35 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 23 Sep 2022 07:28:59 +0000 (09:28 +0200)
src/tmpfiles/tmpfiles.c

index 8442cce5bc4091d5a47692f74801d754b038647c..0031b6ee49d13f516c837acc2f236f227f6c6c16 100644 (file)
@@ -1793,26 +1793,33 @@ static int create_subvolume(Item *i, const char *path) {
 }
 
 static int empty_directory(Item *i, const char *path, CreationMode creation) {
+        _cleanup_close_ int fd = -1;
+        struct stat st;
         int r;
 
         assert(i);
         assert(i->type == EMPTY_DIRECTORY);
 
-        r = is_dir(path, false);
+        r = chase_symlinks(path, arg_root, CHASE_SAFE|CHASE_WARN, NULL, &fd);
+        if (r == -ENOLINK) /* Unsafe symlink: already covered by CHASE_WARN */
+                return fd;
         if (r == -ENOENT) {
-                /* Option "e" operates only on existing objects. Do not
-                 * print errors about non-existent files or directories */
-                log_debug("Skipping missing directory: %s", path);
+                /* Option "e" operates only on existing objects. Do not print errors about non-existent files
+                 * or directories */
+                log_debug_errno(r, "Skipping missing directory: %s", path);
                 return 0;
         }
         if (r < 0)
-                return log_error_errno(r, "is_dir() failed on path %s: %m", path);
-        if (r == 0) {
-                log_warning("\"%s\" already exists and is not a directory.", path);
+                return log_error_errno(r, "Failed to open directory '%s': %m", path);
+
+        if (fstat(fd, &st) < 0)
+                return log_error_errno(errno, "Failed to fstat(%s): %m", path);
+        if (!S_ISDIR(st.st_mode)) {
+                log_warning("'%s' already exists and is not a directory.", path);
                 return 0;
         }
 
-        return path_set_perms(i, path, creation);
+        return fd_set_perms(i, fd, path, &st, creation);
 }
 
 static int create_device(Item *i, mode_t file_type) {