chiark / gitweb /
copy: rework copy_file_atomic() to copy the specified file via O_TMPFILE if possible
[elogind.git] / src / basic / copy.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of systemd.
4
5   Copyright 2014 Lennart Poettering
6 ***/
7
8 //#include <dirent.h>
9 //#include <errno.h>
10 //#include <fcntl.h>
11 //#include <stddef.h>
12 //#include <stdio.h>
13 //#include <stdlib.h>
14 //#include <string.h>
15 #include <sys/sendfile.h>
16 //#include <sys/stat.h>
17 #include <sys/xattr.h>
18 //#include <time.h>
19 //#include <unistd.h>
20
21 //#include "alloc-util.h"
22 //#include "btrfs-util.h"
23 //#include "chattr-util.h"
24 #include "copy.h"
25 //#include "dirent-util.h"
26 //#include "fd-util.h"
27 //#include "fileio.h"
28 //#include "fs-util.h"
29 #include "io-util.h"
30 //#include "macro.h"
31 #include "missing.h"
32 //#include "string-util.h"
33 #include "strv.h"
34 #include "time-util.h"
35 //#include "umask-util.h"
36 #include "user-util.h"
37 //#include "xattr-util.h"
38
39 #define COPY_BUFFER_SIZE (16*1024u)
40
41 static ssize_t try_copy_file_range(
42                 int fd_in, loff_t *off_in,
43                 int fd_out, loff_t *off_out,
44                 size_t len,
45                 unsigned int flags) {
46
47         static int have = -1;
48         ssize_t r;
49
50         if (have == 0)
51                 return -ENOSYS;
52
53         r = copy_file_range(fd_in, off_in, fd_out, off_out, len, flags);
54         if (have < 0)
55                 have = r >= 0 || errno != ENOSYS;
56         if (r < 0)
57                 return -errno;
58
59         return r;
60 }
61
62 enum {
63         FD_IS_NO_PIPE,
64         FD_IS_BLOCKING_PIPE,
65         FD_IS_NONBLOCKING_PIPE,
66 };
67
68 static int fd_is_nonblock_pipe(int fd) {
69         struct stat st;
70         int flags;
71
72         /* Checks whether the specified file descriptor refers to a pipe, and if so if O_NONBLOCK is set. */
73
74         if (fstat(fd, &st) < 0)
75                 return -errno;
76
77         if (!S_ISFIFO(st.st_mode))
78                 return FD_IS_NO_PIPE;
79
80         flags = fcntl(fd, F_GETFL);
81         if (flags < 0)
82                 return -errno;
83
84         return FLAGS_SET(flags, O_NONBLOCK) ? FD_IS_NONBLOCKING_PIPE : FD_IS_BLOCKING_PIPE;
85 }
86
87 int copy_bytes_full(
88                 int fdf, int fdt,
89                 uint64_t max_bytes,
90                 CopyFlags copy_flags,
91                 void **ret_remains,
92                 size_t *ret_remains_size) {
93
94         bool try_cfr = true, try_sendfile = true, try_splice = true;
95         int r, nonblock_pipe = -1;
96         size_t m = SSIZE_MAX; /* that is the maximum that sendfile and c_f_r accept */
97
98         assert(fdf >= 0);
99         assert(fdt >= 0);
100
101         /* Tries to copy bytes from the file descriptor 'fdf' to 'fdt' in the smartest possible way. Copies a maximum
102          * of 'max_bytes', which may be specified as UINT64_MAX, in which no maximum is applied. Returns negative on
103          * error, zero if EOF is hit before the bytes limit is hit and positive otherwise. If the copy fails for some
104          * reason but we read but didn't yet write some data an ret_remains/ret_remains_size is not NULL, then it will
105          * be initialized with an allocated buffer containing this "remaining" data. Note that these two parameters are
106          * initialized with a valid buffer only on failure and only if there's actually data already read. Otherwise
107          * these parameters if non-NULL are set to NULL. */
108
109         if (ret_remains)
110                 *ret_remains = NULL;
111         if (ret_remains_size)
112                 *ret_remains_size = 0;
113
114 #if 0 /// UNNEEDED by elogind
115         /* Try btrfs reflinks first. This only works on regular, seekable files, hence let's check the file offsets of
116          * source and destination first. */
117         if ((copy_flags & COPY_REFLINK)) {
118                 off_t foffset;
119
120                 foffset = lseek(fdf, 0, SEEK_CUR);
121                 if (foffset >= 0) {
122                         off_t toffset;
123
124                         toffset = lseek(fdt, 0, SEEK_CUR);
125                         if (toffset >= 0) {
126
127                                 if (foffset == 0 && toffset == 0 && max_bytes == UINT64_MAX)
128                                         r = btrfs_reflink(fdf, fdt); /* full file reflink */
129                                 else
130                                         r = btrfs_clone_range(fdf, foffset, fdt, toffset, max_bytes == UINT64_MAX ? 0 : max_bytes); /* partial reflink */
131                                 if (r >= 0) {
132                                         off_t t;
133
134                                         /* This worked, yay! Now — to be fully correct — let's adjust the file pointers */
135                                         if (max_bytes == UINT64_MAX) {
136
137                                                 /* We cloned to the end of the source file, let's position the read
138                                                  * pointer there, and query it at the same time. */
139                                                 t = lseek(fdf, 0, SEEK_END);
140                                                 if (t < 0)
141                                                         return -errno;
142                                                 if (t < foffset)
143                                                         return -ESPIPE;
144
145                                                 /* Let's adjust the destination file write pointer by the same number
146                                                  * of bytes. */
147                                                 t = lseek(fdt, toffset + (t - foffset), SEEK_SET);
148                                                 if (t < 0)
149                                                         return -errno;
150
151                                                 return 0; /* we copied the whole thing, hence hit EOF, return 0 */
152                                         } else {
153                                                 t = lseek(fdf, foffset + max_bytes, SEEK_SET);
154                                                 if (t < 0)
155                                                         return -errno;
156
157                                                 t = lseek(fdt, toffset + max_bytes, SEEK_SET);
158                                                 if (t < 0)
159                                                         return -errno;
160
161                                                 return 1; /* we copied only some number of bytes, which worked, but this means we didn't hit EOF, return 1 */
162                                         }
163                                 }
164
165                                 log_debug_errno(r, "Reflinking didn't work, falling back to non-reflink copying: %m");
166                         }
167                 }
168         }
169 #endif // 0
170
171         for (;;) {
172                 ssize_t n;
173
174                 if (max_bytes <= 0)
175                         return 1; /* return > 0 if we hit the max_bytes limit */
176
177                 if (max_bytes != UINT64_MAX && m > max_bytes)
178                         m = max_bytes;
179
180                 /* First try copy_file_range(), unless we already tried */
181                 if (try_cfr) {
182                         n = try_copy_file_range(fdf, NULL, fdt, NULL, m, 0u);
183                         if (n < 0) {
184                                 if (!IN_SET(n, -EINVAL, -ENOSYS, -EXDEV, -EBADF))
185                                         return n;
186
187                                 try_cfr = false;
188                                 /* use fallback below */
189                         } else if (n == 0) /* EOF */
190                                 break;
191                         else
192                                 /* Success! */
193                                 goto next;
194                 }
195
196                 /* First try sendfile(), unless we already tried */
197                 if (try_sendfile) {
198                         n = sendfile(fdt, fdf, NULL, m);
199                         if (n < 0) {
200                                 if (!IN_SET(errno, EINVAL, ENOSYS))
201                                         return -errno;
202
203                                 try_sendfile = false;
204                                 /* use fallback below */
205                         } else if (n == 0) /* EOF */
206                                 break;
207                         else
208                                 /* Success! */
209                                 goto next;
210                 }
211
212                 /* Then try splice, unless we already tried. */
213                 if (try_splice) {
214
215                         /* splice()'s asynchronous I/O support is a bit weird. When it encounters a pipe file
216                          * descriptor, then it will ignore its O_NONBLOCK flag and instead only honour the
217                          * SPLICE_F_NONBLOCK flag specified in its flag parameter. Let's hide this behaviour here, and
218                          * check if either of the specified fds are a pipe, and if so, let's pass the flag
219                          * automatically, depending on O_NONBLOCK being set.
220                          *
221                          * Here's a twist though: when we use it to move data between two pipes of which one has
222                          * O_NONBLOCK set and the other has not, then we have no individual control over O_NONBLOCK
223                          * behaviour. Hence in that case we can't use splice() and still guarantee systematic
224                          * O_NONBLOCK behaviour, hence don't. */
225
226                         if (nonblock_pipe < 0) {
227                                 int a, b;
228
229                                 /* Check if either of these fds is a pipe, and if so non-blocking or not */
230                                 a = fd_is_nonblock_pipe(fdf);
231                                 if (a < 0)
232                                         return a;
233
234                                 b = fd_is_nonblock_pipe(fdt);
235                                 if (b < 0)
236                                         return b;
237
238                                 if ((a == FD_IS_NO_PIPE && b == FD_IS_NO_PIPE) ||
239                                     (a == FD_IS_BLOCKING_PIPE && b == FD_IS_NONBLOCKING_PIPE) ||
240                                     (a == FD_IS_NONBLOCKING_PIPE && b == FD_IS_BLOCKING_PIPE))
241
242                                         /* splice() only works if one of the fds is a pipe. If neither is, let's skip
243                                          * this step right-away. As mentioned above, if one of the two fds refers to a
244                                          * blocking pipe and the other to a non-blocking pipe, we can't use splice()
245                                          * either, hence don't try either. This hence means we can only use splice() if
246                                          * either only one of the two fds is a pipe, or if both are pipes with the same
247                                          * nonblocking flag setting. */
248
249                                         try_splice = false;
250                                 else
251                                         nonblock_pipe = a == FD_IS_NONBLOCKING_PIPE || b == FD_IS_NONBLOCKING_PIPE;
252                         }
253                 }
254
255                 if (try_splice) {
256                         n = splice(fdf, NULL, fdt, NULL, m, nonblock_pipe ? SPLICE_F_NONBLOCK : 0);
257                         if (n < 0) {
258                                 if (!IN_SET(errno, EINVAL, ENOSYS))
259                                         return -errno;
260
261                                 try_splice = false;
262                                 /* use fallback below */
263                         } else if (n == 0) /* EOF */
264                                 break;
265                         else
266                                 /* Success! */
267                                 goto next;
268                 }
269
270                 /* As a fallback just copy bits by hand */
271                 {
272                         uint8_t buf[MIN(m, COPY_BUFFER_SIZE)], *p = buf;
273                         ssize_t z;
274
275                         n = read(fdf, buf, sizeof buf);
276                         if (n < 0)
277                                 return -errno;
278                         if (n == 0) /* EOF */
279                                 break;
280
281                         z = (size_t) n;
282                         do {
283                                 ssize_t k;
284
285                                 k = write(fdt, p, z);
286                                 if (k < 0) {
287                                         r = -errno;
288
289                                         if (ret_remains) {
290                                                 void *copy;
291
292                                                 copy = memdup(p, z);
293                                                 if (!copy)
294                                                         return -ENOMEM;
295
296                                                 *ret_remains = copy;
297                                         }
298
299                                         if (ret_remains_size)
300                                                 *ret_remains_size = z;
301
302                                         return r;
303                                 }
304
305                                 assert(k <= z);
306                                 z -= k;
307                                 p += k;
308                         } while (z > 0);
309                 }
310
311         next:
312                 if (max_bytes != (uint64_t) -1) {
313                         assert(max_bytes >= (uint64_t) n);
314                         max_bytes -= n;
315                 }
316                 /* sendfile accepts at most SSIZE_MAX-offset bytes to copy,
317                  * so reduce our maximum by the amount we already copied,
318                  * but don't go below our copy buffer size, unless we are
319                  * close the limit of bytes we are allowed to copy. */
320                 m = MAX(MIN(COPY_BUFFER_SIZE, max_bytes), m - n);
321         }
322
323         return 0; /* return 0 if we hit EOF earlier than the size limit */
324 }
325
326 #if 0 /// UNNEEDED by elogind
327 static int fd_copy_symlink(
328                 int df,
329                 const char *from,
330                 const struct stat *st,
331                 int dt,
332                 const char *to,
333                 uid_t override_uid,
334                 gid_t override_gid,
335                 CopyFlags copy_flags) {
336
337         _cleanup_free_ char *target = NULL;
338         int r;
339
340         assert(from);
341         assert(st);
342         assert(to);
343
344         r = readlinkat_malloc(df, from, &target);
345         if (r < 0)
346                 return r;
347
348         if (symlinkat(target, dt, to) < 0)
349                 return -errno;
350
351         if (fchownat(dt, to,
352                      uid_is_valid(override_uid) ? override_uid : st->st_uid,
353                      gid_is_valid(override_gid) ? override_gid : st->st_gid,
354                      AT_SYMLINK_NOFOLLOW) < 0)
355                 return -errno;
356
357         return 0;
358 }
359
360 static int fd_copy_regular(
361                 int df,
362                 const char *from,
363                 const struct stat *st,
364                 int dt,
365                 const char *to,
366                 uid_t override_uid,
367                 gid_t override_gid,
368                 CopyFlags copy_flags) {
369
370         _cleanup_close_ int fdf = -1, fdt = -1;
371         struct timespec ts[2];
372         int r, q;
373
374         assert(from);
375         assert(st);
376         assert(to);
377
378         fdf = openat(df, from, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
379         if (fdf < 0)
380                 return -errno;
381
382         fdt = openat(dt, to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, st->st_mode & 07777);
383         if (fdt < 0)
384                 return -errno;
385
386         r = copy_bytes(fdf, fdt, (uint64_t) -1, copy_flags);
387         if (r < 0) {
388                 (void) unlinkat(dt, to, 0);
389                 return r;
390         }
391
392         if (fchown(fdt,
393                    uid_is_valid(override_uid) ? override_uid : st->st_uid,
394                    gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0)
395                 r = -errno;
396
397         if (fchmod(fdt, st->st_mode & 07777) < 0)
398                 r = -errno;
399
400         ts[0] = st->st_atim;
401         ts[1] = st->st_mtim;
402         (void) futimens(fdt, ts);
403         (void) copy_xattr(fdf, fdt);
404
405         q = close(fdt);
406         fdt = -1;
407
408         if (q < 0) {
409                 r = -errno;
410                 (void) unlinkat(dt, to, 0);
411         }
412
413         return r;
414 }
415
416 static int fd_copy_fifo(
417                 int df,
418                 const char *from,
419                 const struct stat *st,
420                 int dt,
421                 const char *to,
422                 uid_t override_uid,
423                 gid_t override_gid,
424                 CopyFlags copy_flags) {
425         int r;
426
427         assert(from);
428         assert(st);
429         assert(to);
430
431         r = mkfifoat(dt, to, st->st_mode & 07777);
432         if (r < 0)
433                 return -errno;
434
435         if (fchownat(dt, to,
436                      uid_is_valid(override_uid) ? override_uid : st->st_uid,
437                      gid_is_valid(override_gid) ? override_gid : st->st_gid,
438                      AT_SYMLINK_NOFOLLOW) < 0)
439                 r = -errno;
440
441         if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
442                 r = -errno;
443
444         return r;
445 }
446
447 static int fd_copy_node(
448                 int df,
449                 const char *from,
450                 const struct stat *st,
451                 int dt,
452                 const char *to,
453                 uid_t override_uid,
454                 gid_t override_gid,
455                 CopyFlags copy_flags) {
456         int r;
457
458         assert(from);
459         assert(st);
460         assert(to);
461
462         r = mknodat(dt, to, st->st_mode, st->st_rdev);
463         if (r < 0)
464                 return -errno;
465
466         if (fchownat(dt, to,
467                      uid_is_valid(override_uid) ? override_uid : st->st_uid,
468                      gid_is_valid(override_gid) ? override_gid : st->st_gid,
469                      AT_SYMLINK_NOFOLLOW) < 0)
470                 r = -errno;
471
472         if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
473                 r = -errno;
474
475         return r;
476 }
477
478 static int fd_copy_directory(
479                 int df,
480                 const char *from,
481                 const struct stat *st,
482                 int dt,
483                 const char *to,
484                 dev_t original_device,
485                 uid_t override_uid,
486                 gid_t override_gid,
487                 CopyFlags copy_flags) {
488
489         _cleanup_close_ int fdf = -1, fdt = -1;
490         _cleanup_closedir_ DIR *d = NULL;
491         struct dirent *de;
492         bool created;
493         int r;
494
495         assert(st);
496         assert(to);
497
498         if (from)
499                 fdf = openat(df, from, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
500         else
501                 fdf = fcntl(df, F_DUPFD_CLOEXEC, 3);
502         if (fdf < 0)
503                 return -errno;
504
505         d = fdopendir(fdf);
506         if (!d)
507                 return -errno;
508         fdf = -1;
509
510         r = mkdirat(dt, to, st->st_mode & 07777);
511         if (r >= 0)
512                 created = true;
513         else if (errno == EEXIST && (copy_flags & COPY_MERGE))
514                 created = false;
515         else
516                 return -errno;
517
518         fdt = openat(dt, to, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
519         if (fdt < 0)
520                 return -errno;
521
522         r = 0;
523
524         FOREACH_DIRENT_ALL(de, d, return -errno) {
525                 struct stat buf;
526                 int q;
527
528                 if (dot_or_dot_dot(de->d_name))
529                         continue;
530
531                 if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
532                         r = -errno;
533                         continue;
534                 }
535
536                 if (buf.st_dev != original_device)
537                         continue;
538
539                 if (S_ISREG(buf.st_mode))
540                         q = fd_copy_regular(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
541                 else if (S_ISDIR(buf.st_mode))
542                         q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, override_uid, override_gid, copy_flags);
543                 else if (S_ISLNK(buf.st_mode))
544                         q = fd_copy_symlink(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
545                 else if (S_ISFIFO(buf.st_mode))
546                         q = fd_copy_fifo(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
547                 else if (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode) || S_ISSOCK(buf.st_mode))
548                         q = fd_copy_node(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
549                 else
550                         q = -EOPNOTSUPP;
551
552                 if (q == -EEXIST && (copy_flags & COPY_MERGE))
553                         q = 0;
554
555                 if (q < 0)
556                         r = q;
557         }
558
559         if (created) {
560                 struct timespec ut[2] = {
561                         st->st_atim,
562                         st->st_mtim
563                 };
564
565                 if (fchown(fdt,
566                            uid_is_valid(override_uid) ? override_uid : st->st_uid,
567                            gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0)
568                         r = -errno;
569
570                 if (fchmod(fdt, st->st_mode & 07777) < 0)
571                         r = -errno;
572
573                 (void) copy_xattr(dirfd(d), fdt);
574                 (void) futimens(fdt, ut);
575         }
576
577         return r;
578 }
579
580 int copy_tree_at(int fdf, const char *from, int fdt, const char *to, uid_t override_uid, gid_t override_gid, CopyFlags copy_flags) {
581         struct stat st;
582
583         assert(from);
584         assert(to);
585
586         if (fstatat(fdf, from, &st, AT_SYMLINK_NOFOLLOW) < 0)
587                 return -errno;
588
589         if (S_ISREG(st.st_mode))
590                 return fd_copy_regular(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
591         else if (S_ISDIR(st.st_mode))
592                 return fd_copy_directory(fdf, from, &st, fdt, to, st.st_dev, override_uid, override_gid, copy_flags);
593         else if (S_ISLNK(st.st_mode))
594                 return fd_copy_symlink(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
595         else if (S_ISFIFO(st.st_mode))
596                 return fd_copy_fifo(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
597         else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode) || S_ISSOCK(st.st_mode))
598                 return fd_copy_node(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
599         else
600                 return -EOPNOTSUPP;
601 }
602
603 int copy_tree(const char *from, const char *to, uid_t override_uid, gid_t override_gid, CopyFlags copy_flags) {
604         return copy_tree_at(AT_FDCWD, from, AT_FDCWD, to, override_uid, override_gid, copy_flags);
605 }
606
607 int copy_directory_fd(int dirfd, const char *to, CopyFlags copy_flags) {
608         struct stat st;
609
610         assert(dirfd >= 0);
611         assert(to);
612
613         if (fstat(dirfd, &st) < 0)
614                 return -errno;
615
616         if (!S_ISDIR(st.st_mode))
617                 return -ENOTDIR;
618
619         return fd_copy_directory(dirfd, NULL, &st, AT_FDCWD, to, st.st_dev, UID_INVALID, GID_INVALID, copy_flags);
620 }
621
622 int copy_directory(const char *from, const char *to, CopyFlags copy_flags) {
623         struct stat st;
624
625         assert(from);
626         assert(to);
627
628         if (lstat(from, &st) < 0)
629                 return -errno;
630
631         if (!S_ISDIR(st.st_mode))
632                 return -ENOTDIR;
633
634         return fd_copy_directory(AT_FDCWD, from, &st, AT_FDCWD, to, st.st_dev, UID_INVALID, GID_INVALID, copy_flags);
635 }
636
637 int copy_file_fd(const char *from, int fdt, CopyFlags copy_flags) {
638         _cleanup_close_ int fdf = -1;
639         int r;
640
641         assert(from);
642         assert(fdt >= 0);
643
644         fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
645         if (fdf < 0)
646                 return -errno;
647
648         r = copy_bytes(fdf, fdt, (uint64_t) -1, copy_flags);
649
650         (void) copy_times(fdf, fdt);
651         (void) copy_xattr(fdf, fdt);
652
653         return r;
654 }
655
656 int copy_file(const char *from, const char *to, int flags, mode_t mode, unsigned chattr_flags, CopyFlags copy_flags) {
657         int fdt = -1, r;
658
659         assert(from);
660         assert(to);
661
662         RUN_WITH_UMASK(0000) {
663                 fdt = open(to, flags|O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode);
664                 if (fdt < 0)
665                         return -errno;
666         }
667
668         if (chattr_flags != 0)
669                 (void) chattr_fd(fdt, chattr_flags, (unsigned) -1);
670
671         r = copy_file_fd(from, fdt, copy_flags);
672         if (r < 0) {
673                 close(fdt);
674                 (void) unlink(to);
675                 return r;
676         }
677
678         if (close(fdt) < 0) {
679                 unlink_noerrno(to);
680                 return -errno;
681         }
682
683         return 0;
684 }
685
686 int copy_file_atomic(const char *from, const char *to, mode_t mode, unsigned chattr_flags, CopyFlags copy_flags) {
687         _cleanup_(unlink_and_freep) char *t = NULL;
688         _cleanup_close_ int fdt = -1;
689         int r;
690
691         assert(from);
692         assert(to);
693
694         /* We try to use O_TMPFILE here to create the file if we can. Note that that only works if COPY_REPLACE is not
695          * set though as we need to use linkat() for linking the O_TMPFILE file into the file system but that system
696          * call can't replace existing files. Hence, if COPY_REPLACE is set we create a temporary name in the file
697          * system right-away and unconditionally which we then can renameat() to the right name after we completed
698          * writing it. */
699
700         if (copy_flags & COPY_REPLACE) {
701                 r = tempfn_random(to, NULL, &t);
702                 if (r < 0)
703                         return r;
704
705                 fdt = open(t, O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY|O_WRONLY|O_CLOEXEC, 0600);
706                 if (fdt < 0) {
707                         t = mfree(t);
708                         return -errno;
709                 }
710         } else {
711                 fdt = open_tmpfile_linkable(to, O_WRONLY|O_CLOEXEC, &t);
712                 if (fdt < 0)
713                         return fdt;
714         }
715
716         if (chattr_flags != 0)
717                 (void) chattr_fd(fdt, chattr_flags, (unsigned) -1);
718
719         r = copy_file_fd(from, fdt, copy_flags);
720         if (r < 0)
721                 return r;
722
723         if (fchmod(fdt, mode) < 0)
724                 return -errno;
725
726         if (copy_flags & COPY_REPLACE) {
727                 if (renameat(AT_FDCWD, t, AT_FDCWD, to) < 0)
728                         return -errno;
729         } else {
730                 r = link_tmpfile(fdt, t, to);
731                 if (r < 0)
732                         return r;
733         }
734
735         t = mfree(t);
736         return 0;
737 }
738
739 int copy_times(int fdf, int fdt) {
740         struct timespec ut[2];
741         struct stat st;
742         usec_t crtime = 0;
743
744         assert(fdf >= 0);
745         assert(fdt >= 0);
746
747         if (fstat(fdf, &st) < 0)
748                 return -errno;
749
750         ut[0] = st.st_atim;
751         ut[1] = st.st_mtim;
752
753         if (futimens(fdt, ut) < 0)
754                 return -errno;
755
756         if (fd_getcrtime(fdf, &crtime) >= 0)
757                 (void) fd_setcrtime(fdt, crtime);
758
759         return 0;
760 }
761
762 int copy_xattr(int fdf, int fdt) {
763         _cleanup_free_ char *bufa = NULL, *bufb = NULL;
764         size_t sza = 100, szb = 100;
765         ssize_t n;
766         int ret = 0;
767         const char *p;
768
769         for (;;) {
770                 bufa = malloc(sza);
771                 if (!bufa)
772                         return -ENOMEM;
773
774                 n = flistxattr(fdf, bufa, sza);
775                 if (n == 0)
776                         return 0;
777                 if (n > 0)
778                         break;
779                 if (errno != ERANGE)
780                         return -errno;
781
782                 sza *= 2;
783
784                 bufa = mfree(bufa);
785         }
786
787         p = bufa;
788         while (n > 0) {
789                 size_t l;
790
791                 l = strlen(p);
792                 assert(l < (size_t) n);
793
794                 if (startswith(p, "user.")) {
795                         ssize_t m;
796
797                         if (!bufb) {
798                                 bufb = malloc(szb);
799                                 if (!bufb)
800                                         return -ENOMEM;
801                         }
802
803                         m = fgetxattr(fdf, p, bufb, szb);
804                         if (m < 0) {
805                                 if (errno == ERANGE) {
806                                         szb *= 2;
807                                         bufb = mfree(bufb);
808                                         continue;
809                                 }
810
811                                 return -errno;
812                         }
813
814                         if (fsetxattr(fdt, p, bufb, m, 0) < 0)
815                                 ret = -errno;
816                 }
817
818                 p += l + 1;
819                 n -= l + 1;
820         }
821
822         return ret;
823 }
824 #endif // 0