chiark / gitweb /
copy: drop _unlikely_() that isn't obviously the case
[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 is has O_NONBLOCK 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 & O_NONBLOCK) == 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 != (uint64_t) -1) {
175                         if (max_bytes <= 0)
176                                 return 1; /* return > 0 if we hit the max_bytes limit */
177
178                         if (m > max_bytes)
179                                 m = max_bytes;
180                 }
181
182                 /* First try copy_file_range(), unless we already tried */
183                 if (try_cfr) {
184                         n = try_copy_file_range(fdf, NULL, fdt, NULL, m, 0u);
185                         if (n < 0) {
186                                 if (!IN_SET(n, -EINVAL, -ENOSYS, -EXDEV, -EBADF))
187                                         return n;
188
189                                 try_cfr = false;
190                                 /* use fallback below */
191                         } else if (n == 0) /* EOF */
192                                 break;
193                         else
194                                 /* Success! */
195                                 goto next;
196                 }
197
198                 /* First try sendfile(), unless we already tried */
199                 if (try_sendfile) {
200                         n = sendfile(fdt, fdf, NULL, m);
201                         if (n < 0) {
202                                 if (!IN_SET(errno, EINVAL, ENOSYS))
203                                         return -errno;
204
205                                 try_sendfile = false;
206                                 /* use fallback below */
207                         } else if (n == 0) /* EOF */
208                                 break;
209                         else
210                                 /* Success! */
211                                 goto next;
212                 }
213
214                 /* Then try splice, unless we already tried. */
215                 if (try_splice) {
216
217                         /* splice()'s asynchronous I/O support is a bit weird. When it encounters a pipe file
218                          * descriptor, then it will ignore its O_NONBLOCK flag and instead only honour the
219                          * SPLICE_F_NONBLOCK flag specified in its flag parameter. Let's hide this behaviour here, and
220                          * check if either of the specified fds are a pipe, and if so, let's pass the flag
221                          * automatically, depending on O_NONBLOCK being set.
222                          *
223                          * Here's a twist though: when we use it to move data between two pipes of which one has
224                          * O_NONBLOCK set and the other has not, then we have no individual control over O_NONBLOCK
225                          * behaviour. Hence in that case we can't use splice() and still guarantee systematic
226                          * O_NONBLOCK behaviour, hence don't. */
227
228                         if (nonblock_pipe < 0) {
229                                 int a, b;
230
231                                 /* Check if either of these fds is a pipe, and if so non-blocking or not */
232                                 a = fd_is_nonblock_pipe(fdf);
233                                 if (a < 0)
234                                         return a;
235
236                                 b = fd_is_nonblock_pipe(fdt);
237                                 if (b < 0)
238                                         return b;
239
240                                 if ((a == FD_IS_NO_PIPE && b == FD_IS_NO_PIPE) ||
241                                     (a == FD_IS_BLOCKING_PIPE && b == FD_IS_NONBLOCKING_PIPE) ||
242                                     (a == FD_IS_NONBLOCKING_PIPE && b == FD_IS_BLOCKING_PIPE))
243
244                                         /* splice() only works if one of the fds is a pipe. If neither is, let's skip
245                                          * this step right-away. As mentioned above, if one of the two fds refers to a
246                                          * blocking pipe and the other to a non-blocking pipe, we can't use splice()
247                                          * either, hence don't try either. This hence means we can only use splice() if
248                                          * either only one of the two fds is a pipe, or if both are pipes with the same
249                                          * nonblocking flag setting. */
250
251                                         try_splice = false;
252                                 else
253                                         nonblock_pipe = a == FD_IS_NONBLOCKING_PIPE || b == FD_IS_NONBLOCKING_PIPE;
254                         }
255                 }
256
257                 if (try_splice) {
258                         n = splice(fdf, NULL, fdt, NULL, m, nonblock_pipe ? SPLICE_F_NONBLOCK : 0);
259                         if (n < 0) {
260                                 if (!IN_SET(errno, EINVAL, ENOSYS))
261                                         return -errno;
262
263                                 try_splice = false;
264                                 /* use fallback below */
265                         } else if (n == 0) /* EOF */
266                                 break;
267                         else
268                                 /* Success! */
269                                 goto next;
270                 }
271
272                 /* As a fallback just copy bits by hand */
273                 {
274                         uint8_t buf[MIN(m, COPY_BUFFER_SIZE)], *p = buf;
275                         ssize_t z;
276
277                         n = read(fdf, buf, sizeof buf);
278                         if (n < 0)
279                                 return -errno;
280                         if (n == 0) /* EOF */
281                                 break;
282
283                         z = (size_t) n;
284                         do {
285                                 ssize_t k;
286
287                                 k = write(fdt, p, z);
288                                 if (k < 0) {
289                                         r = -errno;
290
291                                         if (ret_remains) {
292                                                 void *copy;
293
294                                                 copy = memdup(p, z);
295                                                 if (!copy)
296                                                         return -ENOMEM;
297
298                                                 *ret_remains = copy;
299                                         }
300
301                                         if (ret_remains_size)
302                                                 *ret_remains_size = z;
303
304                                         return r;
305                                 }
306
307                                 assert(k <= z);
308                                 z -= k;
309                                 p += k;
310                         } while (z > 0);
311                 }
312
313         next:
314                 if (max_bytes != (uint64_t) -1) {
315                         assert(max_bytes >= (uint64_t) n);
316                         max_bytes -= n;
317                 }
318                 /* sendfile accepts at most SSIZE_MAX-offset bytes to copy,
319                  * so reduce our maximum by the amount we already copied,
320                  * but don't go below our copy buffer size, unless we are
321                  * close the limit of bytes we are allowed to copy. */
322                 m = MAX(MIN(COPY_BUFFER_SIZE, max_bytes), m - n);
323         }
324
325         return 0; /* return 0 if we hit EOF earlier than the size limit */
326 }
327
328 #if 0 /// UNNEEDED by elogind
329 static int fd_copy_symlink(
330                 int df,
331                 const char *from,
332                 const struct stat *st,
333                 int dt,
334                 const char *to,
335                 uid_t override_uid,
336                 gid_t override_gid,
337                 CopyFlags copy_flags) {
338
339         _cleanup_free_ char *target = NULL;
340         int r;
341
342         assert(from);
343         assert(st);
344         assert(to);
345
346         r = readlinkat_malloc(df, from, &target);
347         if (r < 0)
348                 return r;
349
350         if (symlinkat(target, dt, to) < 0)
351                 return -errno;
352
353         if (fchownat(dt, to,
354                      uid_is_valid(override_uid) ? override_uid : st->st_uid,
355                      gid_is_valid(override_gid) ? override_gid : st->st_gid,
356                      AT_SYMLINK_NOFOLLOW) < 0)
357                 return -errno;
358
359         return 0;
360 }
361
362 static int fd_copy_regular(
363                 int df,
364                 const char *from,
365                 const struct stat *st,
366                 int dt,
367                 const char *to,
368                 uid_t override_uid,
369                 gid_t override_gid,
370                 CopyFlags copy_flags) {
371
372         _cleanup_close_ int fdf = -1, fdt = -1;
373         struct timespec ts[2];
374         int r, q;
375
376         assert(from);
377         assert(st);
378         assert(to);
379
380         fdf = openat(df, from, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
381         if (fdf < 0)
382                 return -errno;
383
384         fdt = openat(dt, to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, st->st_mode & 07777);
385         if (fdt < 0)
386                 return -errno;
387
388         r = copy_bytes(fdf, fdt, (uint64_t) -1, copy_flags);
389         if (r < 0) {
390                 (void) unlinkat(dt, to, 0);
391                 return r;
392         }
393
394         if (fchown(fdt,
395                    uid_is_valid(override_uid) ? override_uid : st->st_uid,
396                    gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0)
397                 r = -errno;
398
399         if (fchmod(fdt, st->st_mode & 07777) < 0)
400                 r = -errno;
401
402         ts[0] = st->st_atim;
403         ts[1] = st->st_mtim;
404         (void) futimens(fdt, ts);
405         (void) copy_xattr(fdf, fdt);
406
407         q = close(fdt);
408         fdt = -1;
409
410         if (q < 0) {
411                 r = -errno;
412                 (void) unlinkat(dt, to, 0);
413         }
414
415         return r;
416 }
417
418 static int fd_copy_fifo(
419                 int df,
420                 const char *from,
421                 const struct stat *st,
422                 int dt,
423                 const char *to,
424                 uid_t override_uid,
425                 gid_t override_gid,
426                 CopyFlags copy_flags) {
427         int r;
428
429         assert(from);
430         assert(st);
431         assert(to);
432
433         r = mkfifoat(dt, to, st->st_mode & 07777);
434         if (r < 0)
435                 return -errno;
436
437         if (fchownat(dt, to,
438                      uid_is_valid(override_uid) ? override_uid : st->st_uid,
439                      gid_is_valid(override_gid) ? override_gid : st->st_gid,
440                      AT_SYMLINK_NOFOLLOW) < 0)
441                 r = -errno;
442
443         if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
444                 r = -errno;
445
446         return r;
447 }
448
449 static int fd_copy_node(
450                 int df,
451                 const char *from,
452                 const struct stat *st,
453                 int dt,
454                 const char *to,
455                 uid_t override_uid,
456                 gid_t override_gid,
457                 CopyFlags copy_flags) {
458         int r;
459
460         assert(from);
461         assert(st);
462         assert(to);
463
464         r = mknodat(dt, to, st->st_mode, st->st_rdev);
465         if (r < 0)
466                 return -errno;
467
468         if (fchownat(dt, to,
469                      uid_is_valid(override_uid) ? override_uid : st->st_uid,
470                      gid_is_valid(override_gid) ? override_gid : st->st_gid,
471                      AT_SYMLINK_NOFOLLOW) < 0)
472                 r = -errno;
473
474         if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
475                 r = -errno;
476
477         return r;
478 }
479
480 static int fd_copy_directory(
481                 int df,
482                 const char *from,
483                 const struct stat *st,
484                 int dt,
485                 const char *to,
486                 dev_t original_device,
487                 uid_t override_uid,
488                 gid_t override_gid,
489                 CopyFlags copy_flags) {
490
491         _cleanup_close_ int fdf = -1, fdt = -1;
492         _cleanup_closedir_ DIR *d = NULL;
493         struct dirent *de;
494         bool created;
495         int r;
496
497         assert(st);
498         assert(to);
499
500         if (from)
501                 fdf = openat(df, from, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
502         else
503                 fdf = fcntl(df, F_DUPFD_CLOEXEC, 3);
504         if (fdf < 0)
505                 return -errno;
506
507         d = fdopendir(fdf);
508         if (!d)
509                 return -errno;
510         fdf = -1;
511
512         r = mkdirat(dt, to, st->st_mode & 07777);
513         if (r >= 0)
514                 created = true;
515         else if (errno == EEXIST && (copy_flags & COPY_MERGE))
516                 created = false;
517         else
518                 return -errno;
519
520         fdt = openat(dt, to, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
521         if (fdt < 0)
522                 return -errno;
523
524         r = 0;
525
526         FOREACH_DIRENT_ALL(de, d, return -errno) {
527                 struct stat buf;
528                 int q;
529
530                 if (dot_or_dot_dot(de->d_name))
531                         continue;
532
533                 if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
534                         r = -errno;
535                         continue;
536                 }
537
538                 if (buf.st_dev != original_device)
539                         continue;
540
541                 if (S_ISREG(buf.st_mode))
542                         q = fd_copy_regular(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
543                 else if (S_ISDIR(buf.st_mode))
544                         q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, override_uid, override_gid, copy_flags);
545                 else if (S_ISLNK(buf.st_mode))
546                         q = fd_copy_symlink(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
547                 else if (S_ISFIFO(buf.st_mode))
548                         q = fd_copy_fifo(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
549                 else if (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode) || S_ISSOCK(buf.st_mode))
550                         q = fd_copy_node(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
551                 else
552                         q = -EOPNOTSUPP;
553
554                 if (q == -EEXIST && (copy_flags & COPY_MERGE))
555                         q = 0;
556
557                 if (q < 0)
558                         r = q;
559         }
560
561         if (created) {
562                 struct timespec ut[2] = {
563                         st->st_atim,
564                         st->st_mtim
565                 };
566
567                 if (fchown(fdt,
568                            uid_is_valid(override_uid) ? override_uid : st->st_uid,
569                            gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0)
570                         r = -errno;
571
572                 if (fchmod(fdt, st->st_mode & 07777) < 0)
573                         r = -errno;
574
575                 (void) copy_xattr(dirfd(d), fdt);
576                 (void) futimens(fdt, ut);
577         }
578
579         return r;
580 }
581
582 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) {
583         struct stat st;
584
585         assert(from);
586         assert(to);
587
588         if (fstatat(fdf, from, &st, AT_SYMLINK_NOFOLLOW) < 0)
589                 return -errno;
590
591         if (S_ISREG(st.st_mode))
592                 return fd_copy_regular(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
593         else if (S_ISDIR(st.st_mode))
594                 return fd_copy_directory(fdf, from, &st, fdt, to, st.st_dev, override_uid, override_gid, copy_flags);
595         else if (S_ISLNK(st.st_mode))
596                 return fd_copy_symlink(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
597         else if (S_ISFIFO(st.st_mode))
598                 return fd_copy_fifo(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
599         else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode) || S_ISSOCK(st.st_mode))
600                 return fd_copy_node(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
601         else
602                 return -EOPNOTSUPP;
603 }
604
605 int copy_tree(const char *from, const char *to, uid_t override_uid, gid_t override_gid, CopyFlags copy_flags) {
606         return copy_tree_at(AT_FDCWD, from, AT_FDCWD, to, override_uid, override_gid, copy_flags);
607 }
608
609 int copy_directory_fd(int dirfd, const char *to, CopyFlags copy_flags) {
610         struct stat st;
611
612         assert(dirfd >= 0);
613         assert(to);
614
615         if (fstat(dirfd, &st) < 0)
616                 return -errno;
617
618         if (!S_ISDIR(st.st_mode))
619                 return -ENOTDIR;
620
621         return fd_copy_directory(dirfd, NULL, &st, AT_FDCWD, to, st.st_dev, UID_INVALID, GID_INVALID, copy_flags);
622 }
623
624 int copy_directory(const char *from, const char *to, CopyFlags copy_flags) {
625         struct stat st;
626
627         assert(from);
628         assert(to);
629
630         if (lstat(from, &st) < 0)
631                 return -errno;
632
633         if (!S_ISDIR(st.st_mode))
634                 return -ENOTDIR;
635
636         return fd_copy_directory(AT_FDCWD, from, &st, AT_FDCWD, to, st.st_dev, UID_INVALID, GID_INVALID, copy_flags);
637 }
638
639 int copy_file_fd(const char *from, int fdt, CopyFlags copy_flags) {
640         _cleanup_close_ int fdf = -1;
641         int r;
642
643         assert(from);
644         assert(fdt >= 0);
645
646         fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
647         if (fdf < 0)
648                 return -errno;
649
650         r = copy_bytes(fdf, fdt, (uint64_t) -1, copy_flags);
651
652         (void) copy_times(fdf, fdt);
653         (void) copy_xattr(fdf, fdt);
654
655         return r;
656 }
657
658 int copy_file(const char *from, const char *to, int flags, mode_t mode, unsigned chattr_flags, CopyFlags copy_flags) {
659         int fdt = -1, r;
660
661         assert(from);
662         assert(to);
663
664         RUN_WITH_UMASK(0000) {
665                 fdt = open(to, flags|O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode);
666                 if (fdt < 0)
667                         return -errno;
668         }
669
670         if (chattr_flags != 0)
671                 (void) chattr_fd(fdt, chattr_flags, (unsigned) -1);
672
673         r = copy_file_fd(from, fdt, copy_flags);
674         if (r < 0) {
675                 close(fdt);
676                 (void) unlink(to);
677                 return r;
678         }
679
680         if (close(fdt) < 0) {
681                 unlink_noerrno(to);
682                 return -errno;
683         }
684
685         return 0;
686 }
687
688 int copy_file_atomic(const char *from, const char *to, mode_t mode, unsigned chattr_flags, CopyFlags copy_flags) {
689         _cleanup_free_ char *t = NULL;
690         int r;
691
692         assert(from);
693         assert(to);
694
695         r = tempfn_random(to, NULL, &t);
696         if (r < 0)
697                 return r;
698
699         r = copy_file(from, t, O_NOFOLLOW|O_EXCL, mode, chattr_flags, copy_flags);
700         if (r < 0)
701                 return r;
702
703         if (copy_flags & COPY_REPLACE) {
704                 r = renameat(AT_FDCWD, t, AT_FDCWD, to);
705                 if (r < 0)
706                         r = -errno;
707         } else
708                 r = rename_noreplace(AT_FDCWD, t, AT_FDCWD, to);
709         if (r < 0) {
710                 (void) unlink(t);
711                 return r;
712         }
713
714         return 0;
715 }
716
717 int copy_times(int fdf, int fdt) {
718         struct timespec ut[2];
719         struct stat st;
720         usec_t crtime = 0;
721
722         assert(fdf >= 0);
723         assert(fdt >= 0);
724
725         if (fstat(fdf, &st) < 0)
726                 return -errno;
727
728         ut[0] = st.st_atim;
729         ut[1] = st.st_mtim;
730
731         if (futimens(fdt, ut) < 0)
732                 return -errno;
733
734         if (fd_getcrtime(fdf, &crtime) >= 0)
735                 (void) fd_setcrtime(fdt, crtime);
736
737         return 0;
738 }
739
740 int copy_xattr(int fdf, int fdt) {
741         _cleanup_free_ char *bufa = NULL, *bufb = NULL;
742         size_t sza = 100, szb = 100;
743         ssize_t n;
744         int ret = 0;
745         const char *p;
746
747         for (;;) {
748                 bufa = malloc(sza);
749                 if (!bufa)
750                         return -ENOMEM;
751
752                 n = flistxattr(fdf, bufa, sza);
753                 if (n == 0)
754                         return 0;
755                 if (n > 0)
756                         break;
757                 if (errno != ERANGE)
758                         return -errno;
759
760                 sza *= 2;
761
762                 bufa = mfree(bufa);
763         }
764
765         p = bufa;
766         while (n > 0) {
767                 size_t l;
768
769                 l = strlen(p);
770                 assert(l < (size_t) n);
771
772                 if (startswith(p, "user.")) {
773                         ssize_t m;
774
775                         if (!bufb) {
776                                 bufb = malloc(szb);
777                                 if (!bufb)
778                                         return -ENOMEM;
779                         }
780
781                         m = fgetxattr(fdf, p, bufb, szb);
782                         if (m < 0) {
783                                 if (errno == ERANGE) {
784                                         szb *= 2;
785                                         bufb = mfree(bufb);
786                                         continue;
787                                 }
788
789                                 return -errno;
790                         }
791
792                         if (fsetxattr(fdt, p, bufb, m, 0) < 0)
793                                 ret = -errno;
794                 }
795
796                 p += l + 1;
797                 n -= l + 1;
798         }
799
800         return ret;
801 }
802 #endif // 0