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