]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/copy.c
update TODO
[thirdparty/systemd.git] / src / shared / copy.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <stddef.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <sys/sendfile.h>
9 #include <sys/xattr.h>
10 #include <unistd.h>
11
12 #include "alloc-util.h"
13 #include "btrfs-util.h"
14 #include "chattr-util.h"
15 #include "copy.h"
16 #include "dirent-util.h"
17 #include "fd-util.h"
18 #include "fileio.h"
19 #include "fs-util.h"
20 #include "io-util.h"
21 #include "macro.h"
22 #include "missing_syscall.h"
23 #include "mkdir-label.h"
24 #include "mountpoint-util.h"
25 #include "nulstr-util.h"
26 #include "rm-rf.h"
27 #include "selinux-util.h"
28 #include "signal-util.h"
29 #include "stat-util.h"
30 #include "stdio-util.h"
31 #include "string-util.h"
32 #include "strv.h"
33 #include "sync-util.h"
34 #include "time-util.h"
35 #include "tmpfile-util.h"
36 #include "umask-util.h"
37 #include "user-util.h"
38 #include "xattr-util.h"
39
40 #define COPY_BUFFER_SIZE (16U*1024U)
41
42 /* A safety net for descending recursively into file system trees to copy. On Linux PATH_MAX is 4096, which means the
43 * deepest valid path one can build is around 2048, which we hence use as a safety net here, to not spin endlessly in
44 * case of bind mount cycles and suchlike. */
45 #define COPY_DEPTH_MAX 2048U
46
47 static ssize_t try_copy_file_range(
48 int fd_in, loff_t *off_in,
49 int fd_out, loff_t *off_out,
50 size_t len,
51 unsigned flags) {
52
53 static int have = -1;
54 ssize_t r;
55
56 if (have == 0)
57 return -ENOSYS;
58
59 r = copy_file_range(fd_in, off_in, fd_out, off_out, len, flags);
60 if (have < 0)
61 have = r >= 0 || errno != ENOSYS;
62 if (r < 0)
63 return -errno;
64
65 return r;
66 }
67
68 enum {
69 FD_IS_NO_PIPE,
70 FD_IS_BLOCKING_PIPE,
71 FD_IS_NONBLOCKING_PIPE,
72 };
73
74 static int fd_is_nonblock_pipe(int fd) {
75 struct stat st;
76 int flags;
77
78 /* Checks whether the specified file descriptor refers to a pipe, and if so if O_NONBLOCK is set. */
79
80 if (fstat(fd, &st) < 0)
81 return -errno;
82
83 if (!S_ISFIFO(st.st_mode))
84 return FD_IS_NO_PIPE;
85
86 flags = fcntl(fd, F_GETFL);
87 if (flags < 0)
88 return -errno;
89
90 return FLAGS_SET(flags, O_NONBLOCK) ? FD_IS_NONBLOCKING_PIPE : FD_IS_BLOCKING_PIPE;
91 }
92
93 static int look_for_signals(CopyFlags copy_flags) {
94 int r;
95
96 if ((copy_flags & (COPY_SIGINT|COPY_SIGTERM)) == 0)
97 return 0;
98
99 r = pop_pending_signal(copy_flags & COPY_SIGINT ? SIGINT : 0,
100 copy_flags & COPY_SIGTERM ? SIGTERM : 0);
101 if (r < 0)
102 return r;
103 if (r != 0)
104 return log_debug_errno(SYNTHETIC_ERRNO(EINTR),
105 "Got %s, cancelling copy operation.", signal_to_string(r));
106
107 return 0;
108 }
109
110 static int create_hole(int fd, off_t size) {
111 off_t offset;
112 off_t end;
113
114 offset = lseek(fd, 0, SEEK_CUR);
115 if (offset < 0)
116 return -errno;
117
118 end = lseek(fd, 0, SEEK_END);
119 if (end < 0)
120 return -errno;
121
122 /* If we're not at the end of the target file, try to punch a hole in the existing space using fallocate(). */
123
124 if (offset < end &&
125 fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, offset, MIN(size, end - offset)) < 0 &&
126 !ERRNO_IS_NOT_SUPPORTED(errno))
127 return -errno;
128
129 if (end - offset >= size) {
130 /* If we've created the full hole, set the file pointer to the end of the hole we created and exit. */
131 if (lseek(fd, offset + size, SEEK_SET) < 0)
132 return -errno;
133
134 return 0;
135 }
136
137 /* If we haven't created the full hole, use ftruncate() to grow the file (and the hole) to the
138 * required size and move the file pointer to the end of the file. */
139
140 size -= end - offset;
141
142 if (ftruncate(fd, end + size) < 0)
143 return -errno;
144
145 if (lseek(fd, 0, SEEK_END) < 0)
146 return -errno;
147
148 return 0;
149 }
150
151 int copy_bytes_full(
152 int fdf, int fdt,
153 uint64_t max_bytes,
154 CopyFlags copy_flags,
155 void **ret_remains,
156 size_t *ret_remains_size,
157 copy_progress_bytes_t progress,
158 void *userdata) {
159
160 bool try_cfr = true, try_sendfile = true, try_splice = true, copied_something = false;
161 int r, nonblock_pipe = -1;
162 size_t m = SSIZE_MAX; /* that is the maximum that sendfile and c_f_r accept */
163
164 assert(fdf >= 0);
165 assert(fdt >= 0);
166
167 /* Tries to copy bytes from the file descriptor 'fdf' to 'fdt' in the smartest possible way. Copies a maximum
168 * of 'max_bytes', which may be specified as UINT64_MAX, in which no maximum is applied. Returns negative on
169 * error, zero if EOF is hit before the bytes limit is hit and positive otherwise. If the copy fails for some
170 * reason but we read but didn't yet write some data an ret_remains/ret_remains_size is not NULL, then it will
171 * be initialized with an allocated buffer containing this "remaining" data. Note that these two parameters are
172 * initialized with a valid buffer only on failure and only if there's actually data already read. Otherwise
173 * these parameters if non-NULL are set to NULL. */
174
175 if (ret_remains)
176 *ret_remains = NULL;
177 if (ret_remains_size)
178 *ret_remains_size = 0;
179
180 /* Try btrfs reflinks first. This only works on regular, seekable files, hence let's check the file offsets of
181 * source and destination first. */
182 if ((copy_flags & COPY_REFLINK)) {
183 off_t foffset;
184
185 foffset = lseek(fdf, 0, SEEK_CUR);
186 if (foffset >= 0) {
187 off_t toffset;
188
189 toffset = lseek(fdt, 0, SEEK_CUR);
190 if (toffset >= 0) {
191
192 if (foffset == 0 && toffset == 0 && max_bytes == UINT64_MAX)
193 r = btrfs_reflink(fdf, fdt); /* full file reflink */
194 else
195 r = btrfs_clone_range(fdf, foffset, fdt, toffset, max_bytes == UINT64_MAX ? 0 : max_bytes); /* partial reflink */
196 if (r >= 0) {
197 off_t t;
198
199 /* This worked, yay! Now — to be fully correct — let's adjust the file pointers */
200 if (max_bytes == UINT64_MAX) {
201
202 /* We cloned to the end of the source file, let's position the read
203 * pointer there, and query it at the same time. */
204 t = lseek(fdf, 0, SEEK_END);
205 if (t < 0)
206 return -errno;
207 if (t < foffset)
208 return -ESPIPE;
209
210 /* Let's adjust the destination file write pointer by the same number
211 * of bytes. */
212 t = lseek(fdt, toffset + (t - foffset), SEEK_SET);
213 if (t < 0)
214 return -errno;
215
216 return 0; /* we copied the whole thing, hence hit EOF, return 0 */
217 } else {
218 t = lseek(fdf, foffset + max_bytes, SEEK_SET);
219 if (t < 0)
220 return -errno;
221
222 t = lseek(fdt, toffset + max_bytes, SEEK_SET);
223 if (t < 0)
224 return -errno;
225
226 return 1; /* we copied only some number of bytes, which worked, but this means we didn't hit EOF, return 1 */
227 }
228 }
229 }
230 }
231 }
232
233 for (;;) {
234 ssize_t n;
235
236 if (max_bytes <= 0)
237 return 1; /* return > 0 if we hit the max_bytes limit */
238
239 r = look_for_signals(copy_flags);
240 if (r < 0)
241 return r;
242
243 if (max_bytes != UINT64_MAX && m > max_bytes)
244 m = max_bytes;
245
246 if (copy_flags & COPY_HOLES) {
247 off_t c, e;
248
249 c = lseek(fdf, 0, SEEK_CUR);
250 if (c < 0)
251 return -errno;
252
253 /* To see if we're in a hole, we search for the next data offset. */
254 e = lseek(fdf, c, SEEK_DATA);
255 if (e < 0 && errno == ENXIO)
256 /* If errno == ENXIO, that means we've reached the final hole of the file and
257 * that hole isn't followed by more data. */
258 e = lseek(fdf, 0, SEEK_END);
259 if (e < 0)
260 return -errno;
261
262 /* If we're in a hole (current offset is not a data offset), create a hole of the
263 * same size in the target file. */
264 if (e > c) {
265 r = create_hole(fdt, e - c);
266 if (r < 0)
267 return r;
268 }
269
270 c = e; /* Set c to the start of the data segment. */
271
272 /* After copying a potential hole, find the end of the data segment by looking for
273 * the next hole. If we get ENXIO, we're at EOF. */
274 e = lseek(fdf, c, SEEK_HOLE);
275 if (e < 0) {
276 if (errno == ENXIO)
277 break;
278 return -errno;
279 }
280
281 /* SEEK_HOLE modifies the file offset so we need to move back to the initial offset. */
282 if (lseek(fdf, c, SEEK_SET) < 0)
283 return -errno;
284
285 /* Make sure we're not copying more than the current data segment. */
286 m = MIN(m, (size_t) e - c);
287 }
288
289 /* First try copy_file_range(), unless we already tried */
290 if (try_cfr) {
291 n = try_copy_file_range(fdf, NULL, fdt, NULL, m, 0u);
292 if (n < 0) {
293 if (!IN_SET(n, -EINVAL, -ENOSYS, -EXDEV, -EBADF))
294 return n;
295
296 try_cfr = false;
297 /* use fallback below */
298 } else if (n == 0) { /* likely EOF */
299
300 if (copied_something)
301 break;
302
303 /* So, we hit EOF immediately, without having copied a single byte. This
304 * could indicate two things: the file is actually empty, or we are on some
305 * virtual file system such as procfs/sysfs where the syscall actually
306 * doesn't work but doesn't return an error. Try to handle that, by falling
307 * back to simple read()s in case we encounter empty files.
308 *
309 * See: https://lwn.net/Articles/846403/ */
310 try_cfr = try_sendfile = try_splice = false;
311 } else
312 /* Success! */
313 goto next;
314 }
315
316 /* First try sendfile(), unless we already tried */
317 if (try_sendfile) {
318 n = sendfile(fdt, fdf, NULL, m);
319 if (n < 0) {
320 if (!IN_SET(errno, EINVAL, ENOSYS))
321 return -errno;
322
323 try_sendfile = false;
324 /* use fallback below */
325 } else if (n == 0) { /* likely EOF */
326
327 if (copied_something)
328 break;
329
330 try_sendfile = try_splice = false; /* same logic as above for copy_file_range() */
331 } else
332 /* Success! */
333 goto next;
334 }
335
336 /* Then try splice, unless we already tried. */
337 if (try_splice) {
338
339 /* splice()'s asynchronous I/O support is a bit weird. When it encounters a pipe file
340 * descriptor, then it will ignore its O_NONBLOCK flag and instead only honour the
341 * SPLICE_F_NONBLOCK flag specified in its flag parameter. Let's hide this behaviour
342 * here, and check if either of the specified fds are a pipe, and if so, let's pass
343 * the flag automatically, depending on O_NONBLOCK being set.
344 *
345 * Here's a twist though: when we use it to move data between two pipes of which one
346 * has O_NONBLOCK set and the other has not, then we have no individual control over
347 * O_NONBLOCK behaviour. Hence in that case we can't use splice() and still guarantee
348 * systematic O_NONBLOCK behaviour, hence don't. */
349
350 if (nonblock_pipe < 0) {
351 int a, b;
352
353 /* Check if either of these fds is a pipe, and if so non-blocking or not */
354 a = fd_is_nonblock_pipe(fdf);
355 if (a < 0)
356 return a;
357
358 b = fd_is_nonblock_pipe(fdt);
359 if (b < 0)
360 return b;
361
362 if ((a == FD_IS_NO_PIPE && b == FD_IS_NO_PIPE) ||
363 (a == FD_IS_BLOCKING_PIPE && b == FD_IS_NONBLOCKING_PIPE) ||
364 (a == FD_IS_NONBLOCKING_PIPE && b == FD_IS_BLOCKING_PIPE))
365
366 /* splice() only works if one of the fds is a pipe. If neither is,
367 * let's skip this step right-away. As mentioned above, if one of the
368 * two fds refers to a blocking pipe and the other to a non-blocking
369 * pipe, we can't use splice() either, hence don't try either. This
370 * hence means we can only use splice() if either only one of the two
371 * fds is a pipe, or if both are pipes with the same nonblocking flag
372 * setting. */
373
374 try_splice = false;
375 else
376 nonblock_pipe = a == FD_IS_NONBLOCKING_PIPE || b == FD_IS_NONBLOCKING_PIPE;
377 }
378 }
379
380 if (try_splice) {
381 n = splice(fdf, NULL, fdt, NULL, m, nonblock_pipe ? SPLICE_F_NONBLOCK : 0);
382 if (n < 0) {
383 if (!IN_SET(errno, EINVAL, ENOSYS))
384 return -errno;
385
386 try_splice = false;
387 /* use fallback below */
388 } else if (n == 0) { /* likely EOF */
389
390 if (copied_something)
391 break;
392
393 try_splice = false; /* same logic as above for copy_file_range() + sendfile() */
394 } else
395 /* Success! */
396 goto next;
397 }
398
399 /* As a fallback just copy bits by hand */
400 {
401 uint8_t buf[MIN(m, COPY_BUFFER_SIZE)], *p = buf;
402 ssize_t z;
403
404 n = read(fdf, buf, sizeof buf);
405 if (n < 0)
406 return -errno;
407 if (n == 0) /* EOF */
408 break;
409
410 z = (size_t) n;
411 do {
412 ssize_t k;
413
414 k = write(fdt, p, z);
415 if (k < 0) {
416 r = -errno;
417
418 if (ret_remains) {
419 void *copy;
420
421 copy = memdup(p, z);
422 if (!copy)
423 return -ENOMEM;
424
425 *ret_remains = copy;
426 }
427
428 if (ret_remains_size)
429 *ret_remains_size = z;
430
431 return r;
432 }
433
434 assert(k <= z);
435 z -= k;
436 p += k;
437 } while (z > 0);
438 }
439
440 next:
441 if (progress) {
442 r = progress(n, userdata);
443 if (r < 0)
444 return r;
445 }
446
447 if (max_bytes != UINT64_MAX) {
448 assert(max_bytes >= (uint64_t) n);
449 max_bytes -= n;
450 }
451
452 /* sendfile accepts at most SSIZE_MAX-offset bytes to copy, so reduce our maximum by the
453 * amount we already copied, but don't go below our copy buffer size, unless we are close the
454 * limit of bytes we are allowed to copy. */
455 m = MAX(MIN(COPY_BUFFER_SIZE, max_bytes), m - n);
456
457 copied_something = true;
458 }
459
460 return 0; /* return 0 if we hit EOF earlier than the size limit */
461 }
462
463 static int fd_copy_symlink(
464 int df,
465 const char *from,
466 const struct stat *st,
467 int dt,
468 const char *to,
469 uid_t override_uid,
470 gid_t override_gid,
471 CopyFlags copy_flags) {
472
473 _cleanup_free_ char *target = NULL;
474 int r;
475
476 assert(from);
477 assert(st);
478 assert(to);
479
480 r = readlinkat_malloc(df, from, &target);
481 if (r < 0)
482 return r;
483
484 if (copy_flags & COPY_MAC_CREATE) {
485 r = mac_selinux_create_file_prepare_at(dt, to, S_IFLNK);
486 if (r < 0)
487 return r;
488 }
489 r = RET_NERRNO(symlinkat(target, dt, to));
490 if (copy_flags & COPY_MAC_CREATE)
491 mac_selinux_create_file_clear();
492 if (r < 0) {
493 if (FLAGS_SET(copy_flags, COPY_GRACEFUL_WARN) && (ERRNO_IS_PRIVILEGE(r) || ERRNO_IS_NOT_SUPPORTED(r))) {
494 log_notice_errno(r, "Failed to copy symlink '%s', ignoring: %m", from);
495 return 0;
496 }
497
498 return r;
499 }
500
501 if (fchownat(dt, to,
502 uid_is_valid(override_uid) ? override_uid : st->st_uid,
503 gid_is_valid(override_gid) ? override_gid : st->st_gid,
504 AT_SYMLINK_NOFOLLOW) < 0)
505 r = -errno;
506
507 (void) copy_xattr(df, from, dt, to, copy_flags);
508 (void) utimensat(dt, to, (struct timespec[]) { st->st_atim, st->st_mtim }, AT_SYMLINK_NOFOLLOW);
509 return r;
510 }
511
512 /* Encapsulates the database we store potential hardlink targets in */
513 typedef struct HardlinkContext {
514 int dir_fd; /* An fd to the directory we use as lookup table. Never AT_FDCWD. Lazily created, when
515 * we add the first entry. */
516
517 /* These two fields are used to create the hardlink repository directory above — via
518 * mkdirat(parent_fd, subdir) — and are kept so that we can automatically remove the directory again
519 * when we are done. */
520 int parent_fd; /* Possibly AT_FDCWD */
521 char *subdir;
522 } HardlinkContext;
523
524 static int hardlink_context_setup(
525 HardlinkContext *c,
526 int dt,
527 const char *to,
528 CopyFlags copy_flags) {
529
530 _cleanup_close_ int dt_copy = -EBADF;
531 int r;
532
533 assert(c);
534 assert(c->dir_fd < 0 && c->dir_fd != AT_FDCWD);
535 assert(c->parent_fd < 0);
536 assert(!c->subdir);
537
538 /* If hardlink recreation is requested we have to maintain a database of inodes that are potential
539 * hardlink sources. Given that generally disk sizes have to be assumed to be larger than what fits
540 * into physical RAM we cannot maintain that database in dynamic memory alone. Here we opt to
541 * maintain it on disk, to simplify things: inside the destination directory we'll maintain a
542 * temporary directory consisting of hardlinks of every inode we copied that might be subject of
543 * hardlinks. We can then use that as hardlink source later on. Yes, this means additional disk IO
544 * but thankfully Linux is optimized for this kind of thing. If this ever becomes a performance
545 * bottleneck we can certainly place an in-memory hash table in front of this, but for the beginning,
546 * let's keep things simple, and just use the disk as lookup table for inodes.
547 *
548 * Note that this should have zero performance impact as long as .n_link of all files copied remains
549 * <= 0, because in that case we will not actually allocate the hardlink inode lookup table directory
550 * on disk (we do so lazily, when the first candidate with .n_link > 1 is seen). This means, in the
551 * common case where hardlinks are not used at all or only for few files the fact that we store the
552 * table on disk shouldn't matter perfomance-wise. */
553
554 if (!FLAGS_SET(copy_flags, COPY_HARDLINKS))
555 return 0;
556
557 if (dt == AT_FDCWD)
558 dt_copy = AT_FDCWD;
559 else if (dt < 0)
560 return -EBADF;
561 else {
562 dt_copy = fcntl(dt, F_DUPFD_CLOEXEC, 3);
563 if (dt_copy < 0)
564 return -errno;
565 }
566
567 r = tempfn_random_child(to, "hardlink", &c->subdir);
568 if (r < 0)
569 return r;
570
571 c->parent_fd = TAKE_FD(dt_copy);
572
573 /* We don't actually create the directory we keep the table in here, that's done on-demand when the
574 * first entry is added, using hardlink_context_realize() below. */
575 return 1;
576 }
577
578 static int hardlink_context_realize(HardlinkContext *c) {
579 if (!c)
580 return 0;
581
582 if (c->dir_fd >= 0) /* Already realized */
583 return 1;
584
585 if (c->parent_fd < 0 && c->parent_fd != AT_FDCWD) /* Not configured */
586 return 0;
587
588 assert(c->subdir);
589
590 c->dir_fd = open_mkdir_at(c->parent_fd, c->subdir, O_EXCL|O_CLOEXEC, 0700);
591 if (c->dir_fd < 0)
592 return c->dir_fd;
593
594 return 1;
595 }
596
597 static void hardlink_context_destroy(HardlinkContext *c) {
598 int r;
599
600 assert(c);
601
602 /* Automatically remove the hardlink lookup table directory again after we are done. This is used via
603 * _cleanup_() so that we really delete this, even on failure. */
604
605 if (c->dir_fd >= 0) {
606 r = rm_rf_children(TAKE_FD(c->dir_fd), REMOVE_PHYSICAL, NULL); /* consumes dir_fd in all cases, even on failure */
607 if (r < 0)
608 log_debug_errno(r, "Failed to remove hardlink store (%s) contents, ignoring: %m", c->subdir);
609
610 assert(c->parent_fd >= 0 || c->parent_fd == AT_FDCWD);
611 assert(c->subdir);
612
613 if (unlinkat(c->parent_fd, c->subdir, AT_REMOVEDIR) < 0)
614 log_debug_errno(errno, "Failed to remove hardlink store (%s) directory, ignoring: %m", c->subdir);
615 }
616
617 assert_cc(AT_FDCWD < 0);
618 c->parent_fd = safe_close(c->parent_fd);
619
620 c->subdir = mfree(c->subdir);
621 }
622
623 static int try_hardlink(
624 HardlinkContext *c,
625 const struct stat *st,
626 int dt,
627 const char *to) {
628
629 char dev_ino[DECIMAL_STR_MAX(dev_t)*2 + DECIMAL_STR_MAX(uint64_t) + 4];
630
631 assert(st);
632 assert(dt >= 0 || dt == AT_FDCWD);
633 assert(to);
634
635 if (!c) /* No temporary hardlink directory, don't bother */
636 return 0;
637
638 if (st->st_nlink <= 1) /* Source not hardlinked, don't bother */
639 return 0;
640
641 if (c->dir_fd < 0) /* not yet realized, hence empty */
642 return 0;
643
644 xsprintf(dev_ino, "%u:%u:%" PRIu64, major(st->st_dev), minor(st->st_dev), (uint64_t) st->st_ino);
645 if (linkat(c->dir_fd, dev_ino, dt, to, 0) < 0) {
646 if (errno != ENOENT) /* doesn't exist in store yet */
647 log_debug_errno(errno, "Failed to hardlink %s to %s, ignoring: %m", dev_ino, to);
648 return 0;
649 }
650
651 return 1;
652 }
653
654 static int memorize_hardlink(
655 HardlinkContext *c,
656 const struct stat *st,
657 int dt,
658 const char *to) {
659
660 char dev_ino[DECIMAL_STR_MAX(dev_t)*2 + DECIMAL_STR_MAX(uint64_t) + 4];
661 int r;
662
663 assert(st);
664 assert(dt >= 0 || dt == AT_FDCWD);
665 assert(to);
666
667 if (!c) /* No temporary hardlink directory, don't bother */
668 return 0;
669
670 if (st->st_nlink <= 1) /* Source not hardlinked, don't bother */
671 return 0;
672
673 r = hardlink_context_realize(c); /* Create the hardlink store lazily */
674 if (r < 0)
675 return r;
676
677 xsprintf(dev_ino, "%u:%u:%" PRIu64, major(st->st_dev), minor(st->st_dev), (uint64_t) st->st_ino);
678 if (linkat(dt, to, c->dir_fd, dev_ino, 0) < 0) {
679 log_debug_errno(errno, "Failed to hardlink %s to %s, ignoring: %m", to, dev_ino);
680 return 0;
681 }
682
683 return 1;
684 }
685
686 static int fd_copy_tree_generic(
687 int df,
688 const char *from,
689 const struct stat *st,
690 int dt,
691 const char *to,
692 dev_t original_device,
693 unsigned depth_left,
694 uid_t override_uid,
695 gid_t override_gid,
696 CopyFlags copy_flags,
697 Hashmap *denylist,
698 HardlinkContext *hardlink_context,
699 const char *display_path,
700 copy_progress_path_t progress_path,
701 copy_progress_bytes_t progress_bytes,
702 void *userdata);
703
704 static int fd_copy_regular(
705 int df,
706 const char *from,
707 const struct stat *st,
708 int dt,
709 const char *to,
710 uid_t override_uid,
711 gid_t override_gid,
712 CopyFlags copy_flags,
713 HardlinkContext *hardlink_context,
714 copy_progress_bytes_t progress,
715 void *userdata) {
716
717 _cleanup_close_ int fdf = -EBADF, fdt = -EBADF;
718 int r, q;
719
720 assert(from);
721 assert(st);
722 assert(to);
723
724 r = try_hardlink(hardlink_context, st, dt, to);
725 if (r < 0)
726 return r;
727 if (r > 0) /* worked! */
728 return 0;
729
730 fdf = openat(df, from, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
731 if (fdf < 0)
732 return -errno;
733
734 if (copy_flags & COPY_MAC_CREATE) {
735 r = mac_selinux_create_file_prepare_at(dt, to, S_IFREG);
736 if (r < 0)
737 return r;
738 }
739 fdt = openat(dt, to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, st->st_mode & 07777);
740 if (copy_flags & COPY_MAC_CREATE)
741 mac_selinux_create_file_clear();
742 if (fdt < 0)
743 return -errno;
744
745 r = copy_bytes_full(fdf, fdt, UINT64_MAX, copy_flags, NULL, NULL, progress, userdata);
746 if (r < 0)
747 goto fail;
748
749 if (fchown(fdt,
750 uid_is_valid(override_uid) ? override_uid : st->st_uid,
751 gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0)
752 r = -errno;
753
754 if (fchmod(fdt, st->st_mode & 07777) < 0)
755 r = -errno;
756
757 (void) futimens(fdt, (struct timespec[]) { st->st_atim, st->st_mtim });
758 (void) copy_xattr(fdf, NULL, fdt, NULL, copy_flags);
759
760 if (copy_flags & COPY_FSYNC) {
761 if (fsync(fdt) < 0) {
762 r = -errno;
763 goto fail;
764 }
765 }
766
767 q = close_nointr(TAKE_FD(fdt)); /* even if this fails, the fd is now invalidated */
768 if (q < 0) {
769 r = q;
770 goto fail;
771 }
772
773 (void) memorize_hardlink(hardlink_context, st, dt, to);
774 return r;
775
776 fail:
777 (void) unlinkat(dt, to, 0);
778 return r;
779 }
780
781 static int fd_copy_fifo(
782 int df,
783 const char *from,
784 const struct stat *st,
785 int dt,
786 const char *to,
787 uid_t override_uid,
788 gid_t override_gid,
789 CopyFlags copy_flags,
790 HardlinkContext *hardlink_context) {
791 int r;
792
793 assert(from);
794 assert(st);
795 assert(to);
796
797 r = try_hardlink(hardlink_context, st, dt, to);
798 if (r < 0)
799 return r;
800 if (r > 0) /* worked! */
801 return 0;
802
803 if (copy_flags & COPY_MAC_CREATE) {
804 r = mac_selinux_create_file_prepare_at(dt, to, S_IFIFO);
805 if (r < 0)
806 return r;
807 }
808 r = RET_NERRNO(mkfifoat(dt, to, st->st_mode & 07777));
809 if (copy_flags & COPY_MAC_CREATE)
810 mac_selinux_create_file_clear();
811 if (r < 0) {
812 if (FLAGS_SET(copy_flags, COPY_GRACEFUL_WARN) && (ERRNO_IS_PRIVILEGE(r) || ERRNO_IS_NOT_SUPPORTED(r))) {
813 log_notice_errno(r, "Failed to copy fifo '%s', ignoring: %m", from);
814 return 0;
815 }
816
817 return r;
818 }
819
820 if (fchownat(dt, to,
821 uid_is_valid(override_uid) ? override_uid : st->st_uid,
822 gid_is_valid(override_gid) ? override_gid : st->st_gid,
823 AT_SYMLINK_NOFOLLOW) < 0)
824 r = -errno;
825
826 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
827 r = -errno;
828
829 (void) utimensat(dt, to, (struct timespec[]) { st->st_atim, st->st_mtim }, AT_SYMLINK_NOFOLLOW);
830
831 (void) memorize_hardlink(hardlink_context, st, dt, to);
832 return r;
833 }
834
835 static int fd_copy_node(
836 int df,
837 const char *from,
838 const struct stat *st,
839 int dt,
840 const char *to,
841 uid_t override_uid,
842 gid_t override_gid,
843 CopyFlags copy_flags,
844 HardlinkContext *hardlink_context) {
845 int r;
846
847 assert(from);
848 assert(st);
849 assert(to);
850
851 r = try_hardlink(hardlink_context, st, dt, to);
852 if (r < 0)
853 return r;
854 if (r > 0) /* worked! */
855 return 0;
856
857 if (copy_flags & COPY_MAC_CREATE) {
858 r = mac_selinux_create_file_prepare_at(dt, to, st->st_mode & S_IFMT);
859 if (r < 0)
860 return r;
861 }
862 r = RET_NERRNO(mknodat(dt, to, st->st_mode, st->st_rdev));
863 if (copy_flags & COPY_MAC_CREATE)
864 mac_selinux_create_file_clear();
865 if (r < 0) {
866 if (FLAGS_SET(copy_flags, COPY_GRACEFUL_WARN) && (ERRNO_IS_PRIVILEGE(r) || ERRNO_IS_NOT_SUPPORTED(r))) {
867 log_notice_errno(r, "Failed to copy node '%s', ignoring: %m", from);
868 return 0;
869 }
870
871 return r;
872 }
873
874 if (fchownat(dt, to,
875 uid_is_valid(override_uid) ? override_uid : st->st_uid,
876 gid_is_valid(override_gid) ? override_gid : st->st_gid,
877 AT_SYMLINK_NOFOLLOW) < 0)
878 r = -errno;
879
880 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
881 r = -errno;
882
883 (void) utimensat(dt, to, (struct timespec[]) { st->st_atim, st->st_mtim }, AT_SYMLINK_NOFOLLOW);
884
885 (void) memorize_hardlink(hardlink_context, st, dt, to);
886 return r;
887 }
888
889 static int fd_copy_directory(
890 int df,
891 const char *from,
892 const struct stat *st,
893 int dt,
894 const char *to,
895 dev_t original_device,
896 unsigned depth_left,
897 uid_t override_uid,
898 gid_t override_gid,
899 CopyFlags copy_flags,
900 Hashmap *denylist,
901 HardlinkContext *hardlink_context,
902 const char *display_path,
903 copy_progress_path_t progress_path,
904 copy_progress_bytes_t progress_bytes,
905 void *userdata) {
906
907 _cleanup_(hardlink_context_destroy) HardlinkContext our_hardlink_context = {
908 .dir_fd = -EBADF,
909 .parent_fd = -EBADF,
910 };
911
912 _cleanup_close_ int fdf = -EBADF, fdt = -EBADF;
913 _cleanup_closedir_ DIR *d = NULL;
914 bool exists, created;
915 int r;
916
917 assert(st);
918 assert(to);
919
920 if (depth_left == 0)
921 return -ENAMETOOLONG;
922
923 if (from)
924 fdf = openat(df, from, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
925 else
926 fdf = fcntl(df, F_DUPFD_CLOEXEC, 3);
927 if (fdf < 0)
928 return -errno;
929
930 if (!hardlink_context) {
931 /* If recreating hardlinks is requested let's set up a context for that now. */
932 r = hardlink_context_setup(&our_hardlink_context, dt, to, copy_flags);
933 if (r < 0)
934 return r;
935 if (r > 0) /* It's enabled and allocated, let's now use the same context for all recursive
936 * invocations from here down */
937 hardlink_context = &our_hardlink_context;
938 }
939
940 d = take_fdopendir(&fdf);
941 if (!d)
942 return -errno;
943
944 exists = false;
945 if (copy_flags & COPY_MERGE_EMPTY) {
946 r = dir_is_empty_at(dt, to, /* ignore_hidden_or_backup= */ false);
947 if (r < 0 && r != -ENOENT)
948 return r;
949 else if (r == 1)
950 exists = true;
951 }
952
953 if (exists)
954 created = false;
955 else {
956 if (copy_flags & COPY_MAC_CREATE)
957 r = mkdirat_label(dt, to, st->st_mode & 07777);
958 else
959 r = mkdirat(dt, to, st->st_mode & 07777);
960 if (r >= 0)
961 created = true;
962 else if (errno == EEXIST && (copy_flags & COPY_MERGE))
963 created = false;
964 else
965 return -errno;
966 }
967
968 fdt = openat(dt, to, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
969 if (fdt < 0)
970 return -errno;
971
972 r = 0;
973
974 if (PTR_TO_INT(hashmap_get(denylist, st)) == DENY_CONTENTS) {
975 log_debug("%s is in the denylist, not recursing", from);
976 goto finish;
977 }
978
979 FOREACH_DIRENT_ALL(de, d, return -errno) {
980 const char *child_display_path = NULL;
981 _cleanup_free_ char *dp = NULL;
982 struct stat buf;
983 int q;
984
985 if (dot_or_dot_dot(de->d_name))
986 continue;
987
988 r = look_for_signals(copy_flags);
989 if (r < 0)
990 return r;
991
992 if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
993 r = -errno;
994 continue;
995 }
996
997 if (progress_path) {
998 if (display_path)
999 child_display_path = dp = path_join(display_path, de->d_name);
1000 else
1001 child_display_path = de->d_name;
1002
1003 r = progress_path(child_display_path, &buf, userdata);
1004 if (r < 0)
1005 return r;
1006 }
1007
1008 if (PTR_TO_INT(hashmap_get(denylist, &buf)) == DENY_INODE) {
1009 log_debug("%s/%s is in the denylist, ignoring", from, de->d_name);
1010 continue;
1011 }
1012
1013 if (S_ISDIR(buf.st_mode)) {
1014 /*
1015 * Don't descend into directories on other file systems, if this is requested. We do a simple
1016 * .st_dev check here, which basically comes for free. Note that we do this check only on
1017 * directories, not other kind of file system objects, for two reason:
1018 *
1019 * • The kernel's overlayfs pseudo file system that overlays multiple real file systems
1020 * propagates the .st_dev field of the file system a file originates from all the way up
1021 * through the stack to stat(). It doesn't do that for directories however. This means that
1022 * comparing .st_dev on non-directories suggests that they all are mount points. To avoid
1023 * confusion we hence avoid relying on this check for regular files.
1024 *
1025 * • The main reason we do this check at all is to protect ourselves from bind mount cycles,
1026 * where we really want to avoid descending down in all eternity. However the .st_dev check
1027 * is usually not sufficient for this protection anyway, as bind mount cycles from the same
1028 * file system onto itself can't be detected that way. (Note we also do a recursion depth
1029 * check, which is probably the better protection in this regard, which is why
1030 * COPY_SAME_MOUNT is optional).
1031 */
1032
1033 if (FLAGS_SET(copy_flags, COPY_SAME_MOUNT)) {
1034 if (buf.st_dev != original_device)
1035 continue;
1036
1037 r = fd_is_mount_point(dirfd(d), de->d_name, 0);
1038 if (r < 0)
1039 return r;
1040 if (r > 0)
1041 continue;
1042 }
1043 }
1044
1045 q = fd_copy_tree_generic(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device,
1046 depth_left-1, override_uid, override_gid, copy_flags, denylist,
1047 hardlink_context, child_display_path, progress_path, progress_bytes,
1048 userdata);
1049
1050 if (q == -EINTR) /* Propagate SIGINT/SIGTERM up instantly */
1051 return q;
1052 if (q == -EEXIST && (copy_flags & COPY_MERGE))
1053 q = 0;
1054 if (q < 0)
1055 r = q;
1056 }
1057
1058 finish:
1059 if (created) {
1060 if (fchown(fdt,
1061 uid_is_valid(override_uid) ? override_uid : st->st_uid,
1062 gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0)
1063 r = -errno;
1064
1065 if (fchmod(fdt, st->st_mode & 07777) < 0)
1066 r = -errno;
1067
1068 (void) copy_xattr(dirfd(d), NULL, fdt, NULL, copy_flags);
1069 (void) futimens(fdt, (struct timespec[]) { st->st_atim, st->st_mtim });
1070 }
1071
1072 if (copy_flags & COPY_FSYNC_FULL) {
1073 if (fsync(fdt) < 0)
1074 return -errno;
1075 }
1076
1077 return r;
1078 }
1079
1080 static int fd_copy_leaf(
1081 int df,
1082 const char *from,
1083 const struct stat *st,
1084 int dt,
1085 const char *to,
1086 uid_t override_uid,
1087 gid_t override_gid,
1088 CopyFlags copy_flags,
1089 HardlinkContext *hardlink_context,
1090 const char *display_path,
1091 copy_progress_bytes_t progress_bytes,
1092 void *userdata) {
1093 int r;
1094
1095 if (S_ISREG(st->st_mode))
1096 r = fd_copy_regular(df, from, st, dt, to, override_uid, override_gid, copy_flags, hardlink_context, progress_bytes, userdata);
1097 else if (S_ISLNK(st->st_mode))
1098 r = fd_copy_symlink(df, from, st, dt, to, override_uid, override_gid, copy_flags);
1099 else if (S_ISFIFO(st->st_mode))
1100 r = fd_copy_fifo(df, from, st, dt, to, override_uid, override_gid, copy_flags, hardlink_context);
1101 else if (S_ISBLK(st->st_mode) || S_ISCHR(st->st_mode) || S_ISSOCK(st->st_mode))
1102 r = fd_copy_node(df, from, st, dt, to, override_uid, override_gid, copy_flags, hardlink_context);
1103 else
1104 r = -EOPNOTSUPP;
1105
1106 return r;
1107 }
1108
1109 static int fd_copy_tree_generic(
1110 int df,
1111 const char *from,
1112 const struct stat *st,
1113 int dt,
1114 const char *to,
1115 dev_t original_device,
1116 unsigned depth_left,
1117 uid_t override_uid,
1118 gid_t override_gid,
1119 CopyFlags copy_flags,
1120 Hashmap *denylist,
1121 HardlinkContext *hardlink_context,
1122 const char *display_path,
1123 copy_progress_path_t progress_path,
1124 copy_progress_bytes_t progress_bytes,
1125 void *userdata) {
1126 int r;
1127
1128 if (S_ISDIR(st->st_mode))
1129 return fd_copy_directory(df, from, st, dt, to, original_device, depth_left-1, override_uid,
1130 override_gid, copy_flags, denylist, hardlink_context, display_path,
1131 progress_path, progress_bytes, userdata);
1132
1133 DenyType t = PTR_TO_INT(hashmap_get(denylist, st));
1134 if (t == DENY_INODE) {
1135 log_debug("%s is in the denylist, ignoring", from);
1136 return 0;
1137 } else if (t == DENY_CONTENTS)
1138 log_debug("%s is configured to have its contents excluded, but is not a directory", from);
1139
1140 r = fd_copy_leaf(df, from, st, dt, to, override_uid, override_gid, copy_flags, hardlink_context, display_path, progress_bytes, userdata);
1141 /* We just tried to copy a leaf node of the tree. If it failed because the node already exists *and* the COPY_REPLACE flag has been provided, we should unlink the node and re-copy. */
1142 if (r == -EEXIST && (copy_flags & COPY_REPLACE)) {
1143 /* This codepath is us trying to address an error to copy, if the unlink fails, lets just return the original error. */
1144 if (unlinkat(dt, to, 0) < 0)
1145 return r;
1146
1147 r = fd_copy_leaf(df, from, st, dt, to, override_uid, override_gid, copy_flags, hardlink_context, display_path, progress_bytes, userdata);
1148 }
1149
1150 return r;
1151 }
1152
1153 int copy_tree_at_full(
1154 int fdf,
1155 const char *from,
1156 int fdt,
1157 const char *to,
1158 uid_t override_uid,
1159 gid_t override_gid,
1160 CopyFlags copy_flags,
1161 Hashmap *denylist,
1162 copy_progress_path_t progress_path,
1163 copy_progress_bytes_t progress_bytes,
1164 void *userdata) {
1165
1166 struct stat st;
1167 int r;
1168
1169 assert(from);
1170 assert(to);
1171
1172 if (fstatat(fdf, from, &st, AT_SYMLINK_NOFOLLOW) < 0)
1173 return -errno;
1174
1175 r = fd_copy_tree_generic(fdf, from, &st, fdt, to, st.st_dev, COPY_DEPTH_MAX, override_uid,
1176 override_gid, copy_flags, denylist, NULL, NULL, progress_path,
1177 progress_bytes, userdata);
1178 if (r < 0)
1179 return r;
1180
1181 if (S_ISDIR(st.st_mode) && (copy_flags & COPY_SYNCFS)) {
1182 /* If the top-level inode is a directory run syncfs() now. */
1183 r = syncfs_path(fdt, to);
1184 if (r < 0)
1185 return r;
1186 } else if ((copy_flags & (COPY_FSYNC_FULL|COPY_SYNCFS)) != 0) {
1187 /* fsync() the parent dir of what we just copied if COPY_FSYNC_FULL is set. Also do this in
1188 * case COPY_SYNCFS is set but the top-level inode wasn't actually a directory. We do this so that
1189 * COPY_SYNCFS provides reasonable synchronization semantics on any kind of inode: when the
1190 * copy operation is done the whole inode — regardless of its type — and all its children
1191 * will be synchronized to disk. */
1192 r = fsync_parent_at(fdt, to);
1193 if (r < 0)
1194 return r;
1195 }
1196
1197 return 0;
1198 }
1199
1200 static int sync_dir_by_flags(const char *path, CopyFlags copy_flags) {
1201
1202 if (copy_flags & COPY_SYNCFS)
1203 return syncfs_path(AT_FDCWD, path);
1204 if (copy_flags & COPY_FSYNC_FULL)
1205 return fsync_parent_at(AT_FDCWD, path);
1206
1207 return 0;
1208 }
1209
1210 int copy_directory_fd_full(
1211 int dirfd,
1212 const char *to,
1213 CopyFlags copy_flags,
1214 copy_progress_path_t progress_path,
1215 copy_progress_bytes_t progress_bytes,
1216 void *userdata) {
1217
1218 struct stat st;
1219 int r;
1220
1221 assert(dirfd >= 0);
1222 assert(to);
1223
1224 if (fstat(dirfd, &st) < 0)
1225 return -errno;
1226
1227 r = stat_verify_directory(&st);
1228 if (r < 0)
1229 return r;
1230
1231 r = fd_copy_directory(
1232 dirfd, NULL,
1233 &st,
1234 AT_FDCWD, to,
1235 st.st_dev,
1236 COPY_DEPTH_MAX,
1237 UID_INVALID, GID_INVALID,
1238 copy_flags,
1239 NULL, NULL, NULL,
1240 progress_path,
1241 progress_bytes,
1242 userdata);
1243 if (r < 0)
1244 return r;
1245
1246 r = sync_dir_by_flags(to, copy_flags);
1247 if (r < 0)
1248 return r;
1249
1250 return 0;
1251 }
1252
1253 int copy_directory_full(
1254 const char *from,
1255 const char *to,
1256 CopyFlags copy_flags,
1257 copy_progress_path_t progress_path,
1258 copy_progress_bytes_t progress_bytes,
1259 void *userdata) {
1260
1261 struct stat st;
1262 int r;
1263
1264 assert(from);
1265 assert(to);
1266
1267 if (lstat(from, &st) < 0)
1268 return -errno;
1269
1270 r = stat_verify_directory(&st);
1271 if (r < 0)
1272 return r;
1273
1274 r = fd_copy_directory(
1275 AT_FDCWD, from,
1276 &st,
1277 AT_FDCWD, to,
1278 st.st_dev,
1279 COPY_DEPTH_MAX,
1280 UID_INVALID, GID_INVALID,
1281 copy_flags,
1282 NULL, NULL, NULL,
1283 progress_path,
1284 progress_bytes,
1285 userdata);
1286 if (r < 0)
1287 return r;
1288
1289 r = sync_dir_by_flags(to, copy_flags);
1290 if (r < 0)
1291 return r;
1292
1293 return 0;
1294 }
1295
1296 int copy_file_fd_at_full(
1297 int dir_fdf,
1298 const char *from,
1299 int fdt,
1300 CopyFlags copy_flags,
1301 copy_progress_bytes_t progress_bytes,
1302 void *userdata) {
1303
1304 _cleanup_close_ int fdf = -EBADF;
1305 struct stat st;
1306 int r;
1307
1308 assert(dir_fdf >= 0 || dir_fdf == AT_FDCWD);
1309 assert(from);
1310 assert(fdt >= 0);
1311
1312 fdf = openat(dir_fdf, from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
1313 if (fdf < 0)
1314 return -errno;
1315
1316 r = fd_verify_regular(fdf);
1317 if (r < 0)
1318 return r;
1319
1320 if (fstat(fdt, &st) < 0)
1321 return -errno;
1322
1323 r = copy_bytes_full(fdf, fdt, UINT64_MAX, copy_flags, NULL, NULL, progress_bytes, userdata);
1324 if (r < 0)
1325 return r;
1326
1327 /* Make sure to copy file attributes only over if target is a regular
1328 * file (so that copying a file to /dev/null won't alter the access
1329 * mode/ownership of that device node...) */
1330 if (S_ISREG(st.st_mode)) {
1331 (void) copy_times(fdf, fdt, copy_flags);
1332 (void) copy_xattr(fdf, NULL, fdt, NULL, copy_flags);
1333 }
1334
1335 if (copy_flags & COPY_FSYNC_FULL) {
1336 r = fsync_full(fdt);
1337 if (r < 0)
1338 return r;
1339 } else if (copy_flags & COPY_FSYNC) {
1340 if (fsync(fdt) < 0)
1341 return -errno;
1342 }
1343
1344 return 0;
1345 }
1346
1347 int copy_file_at_full(
1348 int dir_fdf,
1349 const char *from,
1350 int dir_fdt,
1351 const char *to,
1352 int flags,
1353 mode_t mode,
1354 unsigned chattr_flags,
1355 unsigned chattr_mask,
1356 CopyFlags copy_flags,
1357 copy_progress_bytes_t progress_bytes,
1358 void *userdata) {
1359
1360 _cleanup_close_ int fdf = -EBADF, fdt = -EBADF;
1361 struct stat st;
1362 int r;
1363
1364 assert(dir_fdf >= 0 || dir_fdf == AT_FDCWD);
1365 assert(dir_fdt >= 0 || dir_fdt == AT_FDCWD);
1366 assert(from);
1367 assert(to);
1368
1369 fdf = openat(dir_fdf, from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
1370 if (fdf < 0)
1371 return -errno;
1372
1373 if (fstat(fdf, &st) < 0)
1374 return -errno;
1375
1376 r = stat_verify_regular(&st);
1377 if (r < 0)
1378 return r;
1379
1380 WITH_UMASK(0000) {
1381 if (copy_flags & COPY_MAC_CREATE) {
1382 r = mac_selinux_create_file_prepare_at(dir_fdt, to, S_IFREG);
1383 if (r < 0)
1384 return r;
1385 }
1386 fdt = openat(dir_fdt, to, flags|O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY,
1387 mode != MODE_INVALID ? mode : st.st_mode);
1388 if (copy_flags & COPY_MAC_CREATE)
1389 mac_selinux_create_file_clear();
1390 if (fdt < 0)
1391 return -errno;
1392 }
1393
1394 if (!FLAGS_SET(flags, O_EXCL)) { /* if O_EXCL was used we created the thing as regular file, no need to check again */
1395 r = fd_verify_regular(fdt);
1396 if (r < 0)
1397 goto fail;
1398 }
1399
1400 if (chattr_mask != 0)
1401 (void) chattr_fd(fdt, chattr_flags, chattr_mask & CHATTR_EARLY_FL, NULL);
1402
1403 r = copy_bytes_full(fdf, fdt, UINT64_MAX, copy_flags, NULL, NULL, progress_bytes, userdata);
1404 if (r < 0)
1405 goto fail;
1406
1407 (void) copy_times(fdf, fdt, copy_flags);
1408 (void) copy_xattr(fdf, NULL, fdt, NULL, copy_flags);
1409
1410 if (chattr_mask != 0)
1411 (void) chattr_fd(fdt, chattr_flags, chattr_mask & ~CHATTR_EARLY_FL, NULL);
1412
1413 if (copy_flags & (COPY_FSYNC|COPY_FSYNC_FULL)) {
1414 if (fsync(fdt) < 0) {
1415 r = -errno;
1416 goto fail;
1417 }
1418 }
1419
1420 r = close_nointr(TAKE_FD(fdt)); /* even if this fails, the fd is now invalidated */
1421 if (r < 0)
1422 goto fail;
1423
1424 if (copy_flags & COPY_FSYNC_FULL) {
1425 r = fsync_parent_at(dir_fdt, to);
1426 if (r < 0)
1427 goto fail;
1428 }
1429
1430 return 0;
1431
1432 fail:
1433 /* Only unlink if we definitely are the ones who created the file */
1434 if (FLAGS_SET(flags, O_EXCL))
1435 (void) unlinkat(dir_fdt, to, 0);
1436
1437 return r;
1438 }
1439
1440 int copy_file_atomic_at_full(
1441 int dir_fdf,
1442 const char *from,
1443 int dir_fdt,
1444 const char *to,
1445 mode_t mode,
1446 unsigned chattr_flags,
1447 unsigned chattr_mask,
1448 CopyFlags copy_flags,
1449 copy_progress_bytes_t progress_bytes,
1450 void *userdata) {
1451
1452 _cleanup_(unlink_and_freep) char *t = NULL;
1453 _cleanup_close_ int fdt = -EBADF;
1454 int r;
1455
1456 assert(from);
1457 assert(to);
1458
1459 if (copy_flags & COPY_MAC_CREATE) {
1460 r = mac_selinux_create_file_prepare_at(dir_fdt, to, S_IFREG);
1461 if (r < 0)
1462 return r;
1463 }
1464 fdt = open_tmpfile_linkable_at(dir_fdt, to, O_WRONLY|O_CLOEXEC, &t);
1465 if (copy_flags & COPY_MAC_CREATE)
1466 mac_selinux_create_file_clear();
1467 if (fdt < 0)
1468 return fdt;
1469
1470 if (chattr_mask != 0)
1471 (void) chattr_fd(fdt, chattr_flags, chattr_mask & CHATTR_EARLY_FL, NULL);
1472
1473 r = copy_file_fd_at_full(dir_fdf, from, fdt, copy_flags, progress_bytes, userdata);
1474 if (r < 0)
1475 return r;
1476
1477 if (fchmod(fdt, mode) < 0)
1478 return -errno;
1479
1480 if ((copy_flags & (COPY_FSYNC|COPY_FSYNC_FULL))) {
1481 /* Sync the file */
1482 if (fsync(fdt) < 0)
1483 return -errno;
1484 }
1485
1486 r = link_tmpfile_at(fdt, dir_fdt, t, to, copy_flags & COPY_REPLACE);
1487 if (r < 0)
1488 return r;
1489
1490 t = mfree(t);
1491
1492 if (chattr_mask != 0)
1493 (void) chattr_fd(fdt, chattr_flags, chattr_mask & ~CHATTR_EARLY_FL, NULL);
1494
1495 r = close_nointr(TAKE_FD(fdt)); /* even if this fails, the fd is now invalidated */
1496 if (r < 0)
1497 goto fail;
1498
1499 if (copy_flags & COPY_FSYNC_FULL) {
1500 /* Sync the parent directory */
1501 r = fsync_parent_at(dir_fdt, to);
1502 if (r < 0)
1503 goto fail;
1504 }
1505
1506 return 0;
1507
1508 fail:
1509 (void) unlinkat(dir_fdt, to, 0);
1510 return r;
1511 }
1512
1513 int copy_times(int fdf, int fdt, CopyFlags flags) {
1514 struct stat st;
1515
1516 assert(fdf >= 0);
1517 assert(fdt >= 0);
1518
1519 if (fstat(fdf, &st) < 0)
1520 return -errno;
1521
1522 if (futimens(fdt, (struct timespec[2]) { st.st_atim, st.st_mtim }) < 0)
1523 return -errno;
1524
1525 if (FLAGS_SET(flags, COPY_CRTIME)) {
1526 usec_t crtime;
1527
1528 if (fd_getcrtime(fdf, &crtime) >= 0)
1529 (void) fd_setcrtime(fdt, crtime);
1530 }
1531
1532 return 0;
1533 }
1534
1535 int copy_access(int fdf, int fdt) {
1536 struct stat st;
1537
1538 assert(fdf >= 0);
1539 assert(fdt >= 0);
1540
1541 /* Copies just the access mode (and not the ownership) from fdf to fdt */
1542
1543 if (fstat(fdf, &st) < 0)
1544 return -errno;
1545
1546 return RET_NERRNO(fchmod(fdt, st.st_mode & 07777));
1547 }
1548
1549 int copy_rights_with_fallback(int fdf, int fdt, const char *patht) {
1550 struct stat st;
1551
1552 assert(fdf >= 0);
1553 assert(fdt >= 0);
1554
1555 /* Copies both access mode and ownership from fdf to fdt */
1556
1557 if (fstat(fdf, &st) < 0)
1558 return -errno;
1559
1560 return fchmod_and_chown_with_fallback(fdt, patht, st.st_mode & 07777, st.st_uid, st.st_gid);
1561 }
1562
1563 int copy_xattr(int df, const char *from, int dt, const char *to, CopyFlags copy_flags) {
1564 _cleanup_free_ char *names = NULL;
1565 int ret = 0, r;
1566
1567 r = listxattr_at_malloc(df, from, 0, &names);
1568 if (r < 0)
1569 return r;
1570
1571 NULSTR_FOREACH(p, names) {
1572 _cleanup_free_ char *value = NULL;
1573
1574 if (!FLAGS_SET(copy_flags, COPY_ALL_XATTRS) && !startswith(p, "user."))
1575 continue;
1576
1577 r = getxattr_at_malloc(df, from, p, 0, &value);
1578 if (r == -ENODATA)
1579 continue; /* gone by now */
1580 if (r < 0)
1581 return r;
1582
1583 if (xsetxattr(dt, to, p, value, r, 0) < 0)
1584 ret = -errno;
1585 }
1586
1587 return ret;
1588 }