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