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