chiark / gitweb /
copy: rework copy_file_atomic() to copy the specified file via O_TMPFILE if possible
[elogind.git] / src / basic / copy.c
index c09292b944a946f1f4b5bd5c1e94d0c3b43d4d85..5b53f2e0fc3fefe57025cbeb80c29550b3f1402d 100644 (file)
@@ -29,7 +29,6 @@
 #include "io-util.h"
 //#include "macro.h"
 #include "missing.h"
-//#include "mount-util.h"
 //#include "string-util.h"
 #include "strv.h"
 #include "time-util.h"
@@ -534,38 +533,13 @@ static int fd_copy_directory(
                         continue;
                 }
 
-                if (S_ISDIR(buf.st_mode)) {
-                        /*
-                         * Don't descend into directories on other file systems, if this is requested. We do a simple
-                         * .st_dev check here, which basically comes for free. Note that we do this check only on
-                         * directories, not other kind of file system objects, for two reason:
-                         *
-                         * • The kernel's overlayfs pseudo file system that overlays multiple real file systems
-                         *   propagates the .st_dev field of the file system a file originates from all the way up
-                         *   through the stack to stat(). It doesn't do that for directories however. This means that
-                         *   comparing .st_dev on non-directories suggests that they all are mount points. To avoid
-                         *   confusion we hence avoid relying on this check for regular files.
-                         *
-                         * • The main reason we do this check at all is to protect ourselves from bind mount cycles,
-                         *   where we really want to avoid descending down in all eternity. However the .st_dev check
-                         *   is usually not sufficient for this protection anyway, as bind mount cycles from the same
-                         *   file system onto itself can't be detected that way.
-                         */
-
-                        if (FLAGS_SET(copy_flags, COPY_SAME_MOUNT)) {
-                                if (buf.st_dev != original_device)
-                                        continue;
-
-                                r = fd_is_mount_point(dirfd(d), de->d_name, 0);
-                                if (r < 0)
-                                        return r;
-                                if (r > 0)
-                                        continue;
-                        }
+                if (buf.st_dev != original_device)
+                        continue;
 
-                        q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, override_uid, override_gid, copy_flags);
-                } else if (S_ISREG(buf.st_mode))
+                if (S_ISREG(buf.st_mode))
                         q = fd_copy_regular(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
+                else if (S_ISDIR(buf.st_mode))
+                        q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, override_uid, override_gid, copy_flags);
                 else if (S_ISLNK(buf.st_mode))
                         q = fd_copy_symlink(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
                 else if (S_ISFIFO(buf.st_mode))
@@ -710,31 +684,55 @@ int copy_file(const char *from, const char *to, int flags, mode_t mode, unsigned
 }
 
 int copy_file_atomic(const char *from, const char *to, mode_t mode, unsigned chattr_flags, CopyFlags copy_flags) {
-        _cleanup_free_ char *t = NULL;
+        _cleanup_(unlink_and_freep) char *t = NULL;
+        _cleanup_close_ int fdt = -1;
         int r;
 
         assert(from);
         assert(to);
 
-        r = tempfn_random(to, NULL, &t);
-        if (r < 0)
-                return r;
+        /* We try to use O_TMPFILE here to create the file if we can. Note that that only works if COPY_REPLACE is not
+         * set though as we need to use linkat() for linking the O_TMPFILE file into the file system but that system
+         * call can't replace existing files. Hence, if COPY_REPLACE is set we create a temporary name in the file
+         * system right-away and unconditionally which we then can renameat() to the right name after we completed
+         * writing it. */
+
+        if (copy_flags & COPY_REPLACE) {
+                r = tempfn_random(to, NULL, &t);
+                if (r < 0)
+                        return r;
 
-        r = copy_file(from, t, O_NOFOLLOW|O_EXCL, mode, chattr_flags, copy_flags);
+                fdt = open(t, O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY|O_WRONLY|O_CLOEXEC, 0600);
+                if (fdt < 0) {
+                        t = mfree(t);
+                        return -errno;
+                }
+        } else {
+                fdt = open_tmpfile_linkable(to, O_WRONLY|O_CLOEXEC, &t);
+                if (fdt < 0)
+                        return fdt;
+        }
+
+        if (chattr_flags != 0)
+                (void) chattr_fd(fdt, chattr_flags, (unsigned) -1);
+
+        r = copy_file_fd(from, fdt, copy_flags);
         if (r < 0)
                 return r;
 
+        if (fchmod(fdt, mode) < 0)
+                return -errno;
+
         if (copy_flags & COPY_REPLACE) {
-                r = renameat(AT_FDCWD, t, AT_FDCWD, to);
+                if (renameat(AT_FDCWD, t, AT_FDCWD, to) < 0)
+                        return -errno;
+        } else {
+                r = link_tmpfile(fdt, t, to);
                 if (r < 0)
-                        r = -errno;
-        } else
-                r = rename_noreplace(AT_FDCWD, t, AT_FDCWD, to);
-        if (r < 0) {
-                (void) unlink(t);
-                return r;
+                        return r;
         }
 
+        t = mfree(t);
         return 0;
 }