]>
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" |
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 |
42 | static 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, | |
46 | unsigned int flags) { | |
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 |
63 | enum { |
64 | FD_IS_NO_PIPE, | |
65 | FD_IS_BLOCKING_PIPE, | |
66 | FD_IS_NONBLOCKING_PIPE, | |
67 | }; | |
68 | ||
69 | static 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 |
88 | int copy_bytes_full( |
89 | int fdf, int fdt, | |
90 | uint64_t max_bytes, | |
91 | CopyFlags copy_flags, | |
92 | void **ret_remains, | |
93 | size_t *ret_remains_size) { | |
94 | ||
a44202e9 | 95 | bool try_cfr = true, try_sendfile = true, try_splice = true; |
e0c5c7d8 | 96 | int r, nonblock_pipe = -1; |
7c2da2ca | 97 | size_t m = SSIZE_MAX; /* that is the maximum that sendfile and c_f_r accept */ |
cda134ab | 98 | |
849958d1 LP |
99 | assert(fdf >= 0); |
100 | assert(fdt >= 0); | |
101 | ||
78ba8cf7 LP |
102 | /* Tries to copy bytes from the file descriptor 'fdf' to 'fdt' in the smartest possible way. Copies a maximum |
103 | * of 'max_bytes', which may be specified as UINT64_MAX, in which no maximum is applied. Returns negative on | |
7a23c7fd LP |
104 | * error, zero if EOF is hit before the bytes limit is hit and positive otherwise. If the copy fails for some |
105 | * reason but we read but didn't yet write some data an ret_remains/ret_remains_size is not NULL, then it will | |
106 | * be initialized with an allocated buffer containing this "remaining" data. Note that these two parameters are | |
107 | * initialized with a valid buffer only on failure and only if there's actually data already read. Otherwise | |
108 | * these parameters if non-NULL are set to NULL. */ | |
109 | ||
110 | if (ret_remains) | |
111 | *ret_remains = NULL; | |
112 | if (ret_remains_size) | |
113 | *ret_remains_size = 0; | |
78ba8cf7 | 114 | |
5de6e116 LP |
115 | /* Try btrfs reflinks first. This only works on regular, seekable files, hence let's check the file offsets of |
116 | * source and destination first. */ | |
117 | if ((copy_flags & COPY_REFLINK)) { | |
118 | off_t foffset; | |
119 | ||
120 | foffset = lseek(fdf, 0, SEEK_CUR); | |
121 | if (foffset >= 0) { | |
122 | off_t toffset; | |
123 | ||
124 | toffset = lseek(fdt, 0, SEEK_CUR); | |
125 | if (toffset >= 0) { | |
126 | ||
127 | if (foffset == 0 && toffset == 0 && max_bytes == UINT64_MAX) | |
128 | r = btrfs_reflink(fdf, fdt); /* full file reflink */ | |
129 | else | |
130 | r = btrfs_clone_range(fdf, foffset, fdt, toffset, max_bytes == UINT64_MAX ? 0 : max_bytes); /* partial reflink */ | |
131 | if (r >= 0) { | |
132 | off_t t; | |
133 | ||
134 | /* This worked, yay! Now — to be fully correct — let's adjust the file pointers */ | |
135 | if (max_bytes == UINT64_MAX) { | |
136 | ||
137 | /* We cloned to the end of the source file, let's position the read | |
138 | * pointer there, and query it at the same time. */ | |
139 | t = lseek(fdf, 0, SEEK_END); | |
140 | if (t < 0) | |
141 | return -errno; | |
142 | if (t < foffset) | |
143 | return -ESPIPE; | |
144 | ||
145 | /* Let's adjust the destination file write pointer by the same number | |
146 | * of bytes. */ | |
147 | t = lseek(fdt, toffset + (t - foffset), SEEK_SET); | |
148 | if (t < 0) | |
149 | return -errno; | |
150 | ||
151 | return 0; /* we copied the whole thing, hence hit EOF, return 0 */ | |
152 | } else { | |
153 | t = lseek(fdf, foffset + max_bytes, SEEK_SET); | |
154 | if (t < 0) | |
155 | return -errno; | |
156 | ||
157 | t = lseek(fdt, toffset + max_bytes, SEEK_SET); | |
158 | if (t < 0) | |
159 | return -errno; | |
160 | ||
161 | return 1; /* we copied only some number of bytes, which worked, but this means we didn't hit EOF, return 1 */ | |
162 | } | |
163 | } | |
164 | ||
165 | log_debug_errno(r, "Reflinking didn't work, falling back to non-reflink copying: %m"); | |
166 | } | |
167 | } | |
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: |
59f448cf LP |
311 | if (max_bytes != (uint64_t) -1) { |
312 | assert(max_bytes >= (uint64_t) n); | |
93240d3a LP |
313 | max_bytes -= n; |
314 | } | |
00a8cf77 ZJS |
315 | /* sendfile accepts at most SSIZE_MAX-offset bytes to copy, |
316 | * so reduce our maximum by the amount we already copied, | |
317 | * but don't go below our copy buffer size, unless we are | |
61233823 | 318 | * close the limit of bytes we are allowed to copy. */ |
00a8cf77 | 319 | m = MAX(MIN(COPY_BUFFER_SIZE, max_bytes), m - n); |
849958d1 LP |
320 | } |
321 | ||
f6d9c616 | 322 | return 0; /* return 0 if we hit EOF earlier than the size limit */ |
849958d1 LP |
323 | } |
324 | ||
d01cd401 LP |
325 | static int fd_copy_symlink( |
326 | int df, | |
327 | const char *from, | |
328 | const struct stat *st, | |
329 | int dt, | |
330 | const char *to, | |
331 | uid_t override_uid, | |
332 | gid_t override_gid, | |
333 | CopyFlags copy_flags) { | |
334 | ||
849958d1 LP |
335 | _cleanup_free_ char *target = NULL; |
336 | int r; | |
337 | ||
338 | assert(from); | |
339 | assert(st); | |
340 | assert(to); | |
341 | ||
342 | r = readlinkat_malloc(df, from, &target); | |
343 | if (r < 0) | |
344 | return r; | |
345 | ||
e156347e | 346 | if (symlinkat(target, dt, to) < 0) |
849958d1 | 347 | return -errno; |
849958d1 | 348 | |
d01cd401 LP |
349 | if (fchownat(dt, to, |
350 | uid_is_valid(override_uid) ? override_uid : st->st_uid, | |
351 | gid_is_valid(override_gid) ? override_gid : st->st_gid, | |
352 | AT_SYMLINK_NOFOLLOW) < 0) | |
849958d1 LP |
353 | return -errno; |
354 | ||
355 | return 0; | |
356 | } | |
357 | ||
d01cd401 LP |
358 | static int fd_copy_regular( |
359 | int df, | |
360 | const char *from, | |
361 | const struct stat *st, | |
362 | int dt, | |
363 | const char *to, | |
364 | uid_t override_uid, | |
365 | gid_t override_gid, | |
366 | CopyFlags copy_flags) { | |
367 | ||
849958d1 | 368 | _cleanup_close_ int fdf = -1, fdt = -1; |
ebd93cb6 | 369 | struct timespec ts[2]; |
849958d1 LP |
370 | int r, q; |
371 | ||
372 | assert(from); | |
373 | assert(st); | |
374 | assert(to); | |
375 | ||
376 | fdf = openat(df, from, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW); | |
377 | if (fdf < 0) | |
378 | return -errno; | |
379 | ||
380 | fdt = openat(dt, to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, st->st_mode & 07777); | |
e156347e | 381 | if (fdt < 0) |
849958d1 | 382 | return -errno; |
849958d1 | 383 | |
1c876927 | 384 | r = copy_bytes(fdf, fdt, (uint64_t) -1, copy_flags); |
849958d1 | 385 | if (r < 0) { |
7b938dfb | 386 | (void) unlinkat(dt, to, 0); |
849958d1 LP |
387 | return r; |
388 | } | |
389 | ||
d01cd401 LP |
390 | if (fchown(fdt, |
391 | uid_is_valid(override_uid) ? override_uid : st->st_uid, | |
392 | gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0) | |
849958d1 LP |
393 | r = -errno; |
394 | ||
395 | if (fchmod(fdt, st->st_mode & 07777) < 0) | |
396 | r = -errno; | |
397 | ||
ebd93cb6 LP |
398 | ts[0] = st->st_atim; |
399 | ts[1] = st->st_mtim; | |
400 | (void) futimens(fdt, ts); | |
e6bd041c LP |
401 | (void) copy_xattr(fdf, fdt); |
402 | ||
849958d1 LP |
403 | q = close(fdt); |
404 | fdt = -1; | |
405 | ||
406 | if (q < 0) { | |
407 | r = -errno; | |
7b938dfb | 408 | (void) unlinkat(dt, to, 0); |
849958d1 LP |
409 | } |
410 | ||
411 | return r; | |
412 | } | |
413 | ||
d01cd401 LP |
414 | static int fd_copy_fifo( |
415 | int df, | |
416 | const char *from, | |
417 | const struct stat *st, | |
418 | int dt, | |
419 | const char *to, | |
420 | uid_t override_uid, | |
421 | gid_t override_gid, | |
422 | CopyFlags copy_flags) { | |
849958d1 LP |
423 | int r; |
424 | ||
425 | assert(from); | |
426 | assert(st); | |
427 | assert(to); | |
428 | ||
429 | r = mkfifoat(dt, to, st->st_mode & 07777); | |
e156347e | 430 | if (r < 0) |
849958d1 | 431 | return -errno; |
849958d1 | 432 | |
d01cd401 LP |
433 | if (fchownat(dt, to, |
434 | uid_is_valid(override_uid) ? override_uid : st->st_uid, | |
435 | gid_is_valid(override_gid) ? override_gid : st->st_gid, | |
436 | AT_SYMLINK_NOFOLLOW) < 0) | |
849958d1 LP |
437 | r = -errno; |
438 | ||
439 | if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0) | |
440 | r = -errno; | |
441 | ||
442 | return r; | |
443 | } | |
444 | ||
d01cd401 LP |
445 | static int fd_copy_node( |
446 | int df, | |
447 | const char *from, | |
448 | const struct stat *st, | |
449 | int dt, | |
450 | const char *to, | |
451 | uid_t override_uid, | |
452 | gid_t override_gid, | |
453 | CopyFlags copy_flags) { | |
849958d1 LP |
454 | int r; |
455 | ||
456 | assert(from); | |
457 | assert(st); | |
458 | assert(to); | |
459 | ||
460 | r = mknodat(dt, to, st->st_mode, st->st_rdev); | |
e156347e | 461 | if (r < 0) |
849958d1 | 462 | return -errno; |
849958d1 | 463 | |
d01cd401 LP |
464 | if (fchownat(dt, to, |
465 | uid_is_valid(override_uid) ? override_uid : st->st_uid, | |
466 | gid_is_valid(override_gid) ? override_gid : st->st_gid, | |
467 | AT_SYMLINK_NOFOLLOW) < 0) | |
849958d1 LP |
468 | r = -errno; |
469 | ||
470 | if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0) | |
471 | r = -errno; | |
472 | ||
473 | return r; | |
474 | } | |
475 | ||
d7c7c334 LP |
476 | static int fd_copy_directory( |
477 | int df, | |
478 | const char *from, | |
479 | const struct stat *st, | |
480 | int dt, | |
481 | const char *to, | |
482 | dev_t original_device, | |
575a07d2 | 483 | unsigned depth_left, |
d01cd401 LP |
484 | uid_t override_uid, |
485 | gid_t override_gid, | |
1c876927 | 486 | CopyFlags copy_flags) { |
d7c7c334 | 487 | |
849958d1 LP |
488 | _cleanup_close_ int fdf = -1, fdt = -1; |
489 | _cleanup_closedir_ DIR *d = NULL; | |
490 | struct dirent *de; | |
491 | bool created; | |
492 | int r; | |
493 | ||
849958d1 LP |
494 | assert(st); |
495 | assert(to); | |
496 | ||
575a07d2 LP |
497 | if (depth_left == 0) |
498 | return -ENAMETOOLONG; | |
499 | ||
d7c7c334 LP |
500 | if (from) |
501 | fdf = openat(df, from, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW); | |
502 | else | |
503 | fdf = fcntl(df, F_DUPFD_CLOEXEC, 3); | |
b498c53d LP |
504 | if (fdf < 0) |
505 | return -errno; | |
849958d1 LP |
506 | |
507 | d = fdopendir(fdf); | |
508 | if (!d) | |
509 | return -errno; | |
510 | fdf = -1; | |
511 | ||
512 | r = mkdirat(dt, to, st->st_mode & 07777); | |
513 | if (r >= 0) | |
514 | created = true; | |
1c876927 | 515 | else if (errno == EEXIST && (copy_flags & COPY_MERGE)) |
849958d1 LP |
516 | created = false; |
517 | else | |
518 | return -errno; | |
519 | ||
520 | fdt = openat(dt, to, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW); | |
521 | if (fdt < 0) | |
522 | return -errno; | |
523 | ||
2c455af4 LP |
524 | r = 0; |
525 | ||
8420fa3a | 526 | FOREACH_DIRENT_ALL(de, d, return -errno) { |
849958d1 LP |
527 | struct stat buf; |
528 | int q; | |
529 | ||
49bfc877 | 530 | if (dot_or_dot_dot(de->d_name)) |
8420fa3a LP |
531 | continue; |
532 | ||
849958d1 LP |
533 | if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) { |
534 | r = -errno; | |
535 | continue; | |
536 | } | |
537 | ||
ef202b84 | 538 | if (S_ISDIR(buf.st_mode)) { |
f6a77804 LP |
539 | /* |
540 | * Don't descend into directories on other file systems, if this is requested. We do a simple | |
541 | * .st_dev check here, which basically comes for free. Note that we do this check only on | |
542 | * directories, not other kind of file system objects, for two reason: | |
543 | * | |
544 | * • The kernel's overlayfs pseudo file system that overlays multiple real file systems | |
545 | * propagates the .st_dev field of the file system a file originates from all the way up | |
546 | * through the stack to stat(). It doesn't do that for directories however. This means that | |
547 | * comparing .st_dev on non-directories suggests that they all are mount points. To avoid | |
548 | * confusion we hence avoid relying on this check for regular files. | |
549 | * | |
550 | * • The main reason we do this check at all is to protect ourselves from bind mount cycles, | |
551 | * where we really want to avoid descending down in all eternity. However the .st_dev check | |
552 | * is usually not sufficient for this protection anyway, as bind mount cycles from the same | |
575a07d2 LP |
553 | * file system onto itself can't be detected that way. (Note we also do a recursion depth |
554 | * check, which is probably the better protection in this regard, which is why | |
555 | * COPY_SAME_MOUNT is optional). | |
f6a77804 LP |
556 | */ |
557 | ||
558 | if (FLAGS_SET(copy_flags, COPY_SAME_MOUNT)) { | |
559 | if (buf.st_dev != original_device) | |
560 | continue; | |
561 | ||
562 | r = fd_is_mount_point(dirfd(d), de->d_name, 0); | |
563 | if (r < 0) | |
564 | return r; | |
565 | if (r > 0) | |
566 | continue; | |
567 | } | |
568 | ||
575a07d2 | 569 | 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); |
ef202b84 FB |
570 | } else if (S_ISREG(buf.st_mode)) |
571 | q = fd_copy_regular(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags); | |
849958d1 | 572 | else if (S_ISLNK(buf.st_mode)) |
d01cd401 | 573 | q = fd_copy_symlink(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags); |
849958d1 | 574 | else if (S_ISFIFO(buf.st_mode)) |
d01cd401 | 575 | q = fd_copy_fifo(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags); |
0e2b2cac | 576 | else if (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode) || S_ISSOCK(buf.st_mode)) |
d01cd401 | 577 | q = fd_copy_node(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags); |
849958d1 | 578 | else |
15411c0c | 579 | q = -EOPNOTSUPP; |
849958d1 | 580 | |
1c876927 | 581 | if (q == -EEXIST && (copy_flags & COPY_MERGE)) |
e156347e LP |
582 | q = 0; |
583 | ||
849958d1 LP |
584 | if (q < 0) |
585 | r = q; | |
586 | } | |
587 | ||
3b8483c0 LP |
588 | if (created) { |
589 | struct timespec ut[2] = { | |
590 | st->st_atim, | |
591 | st->st_mtim | |
592 | }; | |
593 | ||
d01cd401 LP |
594 | if (fchown(fdt, |
595 | uid_is_valid(override_uid) ? override_uid : st->st_uid, | |
596 | gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0) | |
3b8483c0 LP |
597 | r = -errno; |
598 | ||
599 | if (fchmod(fdt, st->st_mode & 07777) < 0) | |
600 | r = -errno; | |
601 | ||
602 | (void) copy_xattr(dirfd(d), fdt); | |
603 | (void) futimens(fdt, ut); | |
604 | } | |
605 | ||
849958d1 LP |
606 | return r; |
607 | } | |
608 | ||
d01cd401 | 609 | int copy_tree_at(int fdf, const char *from, int fdt, const char *to, uid_t override_uid, gid_t override_gid, CopyFlags copy_flags) { |
849958d1 LP |
610 | struct stat st; |
611 | ||
612 | assert(from); | |
613 | assert(to); | |
614 | ||
f2cbe59e | 615 | if (fstatat(fdf, from, &st, AT_SYMLINK_NOFOLLOW) < 0) |
849958d1 LP |
616 | return -errno; |
617 | ||
618 | if (S_ISREG(st.st_mode)) | |
d01cd401 | 619 | return fd_copy_regular(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags); |
849958d1 | 620 | else if (S_ISDIR(st.st_mode)) |
575a07d2 | 621 | return fd_copy_directory(fdf, from, &st, fdt, to, st.st_dev, COPY_DEPTH_MAX, override_uid, override_gid, copy_flags); |
849958d1 | 622 | else if (S_ISLNK(st.st_mode)) |
d01cd401 | 623 | return fd_copy_symlink(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags); |
849958d1 | 624 | else if (S_ISFIFO(st.st_mode)) |
d01cd401 | 625 | return fd_copy_fifo(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags); |
0e2b2cac | 626 | else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode) || S_ISSOCK(st.st_mode)) |
d01cd401 | 627 | return fd_copy_node(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags); |
849958d1 | 628 | else |
15411c0c | 629 | return -EOPNOTSUPP; |
849958d1 LP |
630 | } |
631 | ||
d01cd401 LP |
632 | int copy_tree(const char *from, const char *to, uid_t override_uid, gid_t override_gid, CopyFlags copy_flags) { |
633 | return copy_tree_at(AT_FDCWD, from, AT_FDCWD, to, override_uid, override_gid, copy_flags); | |
f2cbe59e LP |
634 | } |
635 | ||
1c876927 | 636 | int copy_directory_fd(int dirfd, const char *to, CopyFlags copy_flags) { |
d7c7c334 LP |
637 | struct stat st; |
638 | ||
639 | assert(dirfd >= 0); | |
640 | assert(to); | |
641 | ||
642 | if (fstat(dirfd, &st) < 0) | |
643 | return -errno; | |
644 | ||
645 | if (!S_ISDIR(st.st_mode)) | |
646 | return -ENOTDIR; | |
647 | ||
575a07d2 | 648 | return fd_copy_directory(dirfd, NULL, &st, AT_FDCWD, to, st.st_dev, COPY_DEPTH_MAX, UID_INVALID, GID_INVALID, copy_flags); |
d7c7c334 LP |
649 | } |
650 | ||
1c876927 | 651 | int copy_directory(const char *from, const char *to, CopyFlags copy_flags) { |
9a50e3ca LP |
652 | struct stat st; |
653 | ||
654 | assert(from); | |
655 | assert(to); | |
656 | ||
657 | if (lstat(from, &st) < 0) | |
658 | return -errno; | |
659 | ||
660 | if (!S_ISDIR(st.st_mode)) | |
661 | return -ENOTDIR; | |
662 | ||
575a07d2 | 663 | return fd_copy_directory(AT_FDCWD, from, &st, AT_FDCWD, to, st.st_dev, COPY_DEPTH_MAX, UID_INVALID, GID_INVALID, copy_flags); |
9a50e3ca LP |
664 | } |
665 | ||
1c876927 | 666 | int copy_file_fd(const char *from, int fdt, CopyFlags copy_flags) { |
cda134ab | 667 | _cleanup_close_ int fdf = -1; |
e6bd041c | 668 | int r; |
849958d1 LP |
669 | |
670 | assert(from); | |
cda134ab | 671 | assert(fdt >= 0); |
849958d1 LP |
672 | |
673 | fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY); | |
674 | if (fdf < 0) | |
675 | return -errno; | |
676 | ||
1c876927 | 677 | r = copy_bytes(fdf, fdt, (uint64_t) -1, copy_flags); |
e6bd041c LP |
678 | |
679 | (void) copy_times(fdf, fdt); | |
680 | (void) copy_xattr(fdf, fdt); | |
681 | ||
682 | return r; | |
cda134ab LP |
683 | } |
684 | ||
1c876927 | 685 | int copy_file(const char *from, const char *to, int flags, mode_t mode, unsigned chattr_flags, CopyFlags copy_flags) { |
a7f7d1bd | 686 | int fdt = -1, r; |
cda134ab LP |
687 | |
688 | assert(from); | |
689 | assert(to); | |
690 | ||
ebd93cb6 LP |
691 | RUN_WITH_UMASK(0000) { |
692 | fdt = open(to, flags|O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode); | |
693 | if (fdt < 0) | |
694 | return -errno; | |
695 | } | |
849958d1 | 696 | |
f2068bcc | 697 | if (chattr_flags != 0) |
db9a4254 | 698 | (void) chattr_fd(fdt, chattr_flags, (unsigned) -1, NULL); |
f2068bcc | 699 | |
1c876927 | 700 | r = copy_file_fd(from, fdt, copy_flags); |
849958d1 | 701 | if (r < 0) { |
cda134ab | 702 | close(fdt); |
7b938dfb | 703 | (void) unlink(to); |
849958d1 LP |
704 | return r; |
705 | } | |
706 | ||
cda134ab LP |
707 | if (close(fdt) < 0) { |
708 | unlink_noerrno(to); | |
709 | return -errno; | |
849958d1 LP |
710 | } |
711 | ||
712 | return 0; | |
713 | } | |
e6bd041c | 714 | |
1c876927 | 715 | int copy_file_atomic(const char *from, const char *to, mode_t mode, unsigned chattr_flags, CopyFlags copy_flags) { |
ec6bdf72 LP |
716 | _cleanup_(unlink_and_freep) char *t = NULL; |
717 | _cleanup_close_ int fdt = -1; | |
ebd93cb6 LP |
718 | int r; |
719 | ||
720 | assert(from); | |
721 | assert(to); | |
722 | ||
ec6bdf72 LP |
723 | /* We try to use O_TMPFILE here to create the file if we can. Note that that only works if COPY_REPLACE is not |
724 | * set though as we need to use linkat() for linking the O_TMPFILE file into the file system but that system | |
725 | * call can't replace existing files. Hence, if COPY_REPLACE is set we create a temporary name in the file | |
726 | * system right-away and unconditionally which we then can renameat() to the right name after we completed | |
727 | * writing it. */ | |
728 | ||
729 | if (copy_flags & COPY_REPLACE) { | |
730 | r = tempfn_random(to, NULL, &t); | |
731 | if (r < 0) | |
732 | return r; | |
733 | ||
734 | fdt = open(t, O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY|O_WRONLY|O_CLOEXEC, 0600); | |
735 | if (fdt < 0) { | |
736 | t = mfree(t); | |
737 | return -errno; | |
738 | } | |
739 | } else { | |
740 | fdt = open_tmpfile_linkable(to, O_WRONLY|O_CLOEXEC, &t); | |
741 | if (fdt < 0) | |
742 | return fdt; | |
743 | } | |
ebd93cb6 | 744 | |
ec6bdf72 | 745 | if (chattr_flags != 0) |
db9a4254 | 746 | (void) chattr_fd(fdt, chattr_flags, (unsigned) -1, NULL); |
ec6bdf72 LP |
747 | |
748 | r = copy_file_fd(from, fdt, copy_flags); | |
ebd93cb6 LP |
749 | if (r < 0) |
750 | return r; | |
751 | ||
ec6bdf72 LP |
752 | if (fchmod(fdt, mode) < 0) |
753 | return -errno; | |
754 | ||
1c876927 | 755 | if (copy_flags & COPY_REPLACE) { |
ec6bdf72 LP |
756 | if (renameat(AT_FDCWD, t, AT_FDCWD, to) < 0) |
757 | return -errno; | |
758 | } else { | |
759 | r = link_tmpfile(fdt, t, to); | |
f85ef957 | 760 | if (r < 0) |
ec6bdf72 | 761 | return r; |
ebd93cb6 LP |
762 | } |
763 | ||
ec6bdf72 | 764 | t = mfree(t); |
ebd93cb6 LP |
765 | return 0; |
766 | } | |
767 | ||
e6bd041c LP |
768 | int copy_times(int fdf, int fdt) { |
769 | struct timespec ut[2]; | |
770 | struct stat st; | |
a7f7d1bd | 771 | usec_t crtime = 0; |
e6bd041c LP |
772 | |
773 | assert(fdf >= 0); | |
774 | assert(fdt >= 0); | |
775 | ||
776 | if (fstat(fdf, &st) < 0) | |
777 | return -errno; | |
778 | ||
779 | ut[0] = st.st_atim; | |
780 | ut[1] = st.st_mtim; | |
781 | ||
782 | if (futimens(fdt, ut) < 0) | |
783 | return -errno; | |
784 | ||
785 | if (fd_getcrtime(fdf, &crtime) >= 0) | |
786 | (void) fd_setcrtime(fdt, crtime); | |
787 | ||
788 | return 0; | |
789 | } | |
790 | ||
791 | int copy_xattr(int fdf, int fdt) { | |
792 | _cleanup_free_ char *bufa = NULL, *bufb = NULL; | |
793 | size_t sza = 100, szb = 100; | |
794 | ssize_t n; | |
795 | int ret = 0; | |
796 | const char *p; | |
797 | ||
798 | for (;;) { | |
799 | bufa = malloc(sza); | |
800 | if (!bufa) | |
801 | return -ENOMEM; | |
802 | ||
803 | n = flistxattr(fdf, bufa, sza); | |
804 | if (n == 0) | |
805 | return 0; | |
806 | if (n > 0) | |
807 | break; | |
808 | if (errno != ERANGE) | |
809 | return -errno; | |
810 | ||
811 | sza *= 2; | |
812 | ||
97b11eed | 813 | bufa = mfree(bufa); |
e6bd041c LP |
814 | } |
815 | ||
816 | p = bufa; | |
817 | while (n > 0) { | |
818 | size_t l; | |
819 | ||
820 | l = strlen(p); | |
821 | assert(l < (size_t) n); | |
822 | ||
823 | if (startswith(p, "user.")) { | |
824 | ssize_t m; | |
825 | ||
826 | if (!bufb) { | |
827 | bufb = malloc(szb); | |
828 | if (!bufb) | |
829 | return -ENOMEM; | |
830 | } | |
831 | ||
832 | m = fgetxattr(fdf, p, bufb, szb); | |
833 | if (m < 0) { | |
834 | if (errno == ERANGE) { | |
835 | szb *= 2; | |
97b11eed | 836 | bufb = mfree(bufb); |
e6bd041c LP |
837 | continue; |
838 | } | |
839 | ||
840 | return -errno; | |
841 | } | |
842 | ||
843 | if (fsetxattr(fdt, p, bufb, m, 0) < 0) | |
844 | ret = -errno; | |
845 | } | |
846 | ||
847 | p += l + 1; | |
848 | n -= l + 1; | |
849 | } | |
850 | ||
851 | return ret; | |
852 | } |