summaryrefslogtreecommitdiff
path: root/src/basic/copy.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2018-06-05 16:52:22 +0200
committerLennart Poettering <lennart@poettering.net>2018-06-07 17:40:14 +0200
commitec6bdf72597937c0fb981f5b1e04bbbe15000cc5 (patch)
tree5d34b9b7bfabc4311e982267d1bc60484fdfb8ca /src/basic/copy.c
parent3f4030399350a853803aad244812b83c1dda3a35 (diff)
downloadsystemd-ec6bdf72597937c0fb981f5b1e04bbbe15000cc5.tar.gz
systemd-ec6bdf72597937c0fb981f5b1e04bbbe15000cc5.tar.bz2
systemd-ec6bdf72597937c0fb981f5b1e04bbbe15000cc5.zip
copy: rework copy_file_atomic() to copy the specified file via O_TMPFILE if possible
Diffstat (limited to 'src/basic/copy.c')
-rw-r--r--src/basic/copy.c48
1 files changed, 36 insertions, 12 deletions
diff --git a/src/basic/copy.c b/src/basic/copy.c
index 650de612b8..80d3a9dcdf 100644
--- a/src/basic/copy.c
+++ b/src/basic/copy.c
@@ -681,31 +681,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;
+
+ 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;
+ }
- r = copy_file(from, t, O_NOFOLLOW|O_EXCL, mode, chattr_flags, copy_flags);
+ 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;
}