]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/copy.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Lennart Poettering
15 #include <sys/sendfile.h>
17 #include <sys/xattr.h>
21 #include "alloc-util.h"
22 #include "btrfs-util.h"
23 #include "chattr-util.h"
25 #include "dirent-util.h"
32 #include "string-util.h"
34 #include "time-util.h"
35 #include "umask-util.h"
36 #include "user-util.h"
37 #include "xattr-util.h"
39 #define COPY_BUFFER_SIZE (16*1024u)
41 static ssize_t
try_copy_file_range(
42 int fd_in
, loff_t
*off_in
,
43 int fd_out
, loff_t
*off_out
,
53 r
= copy_file_range(fd_in
, off_in
, fd_out
, off_out
, len
, flags
);
55 have
= r
>= 0 || errno
!= ENOSYS
;
65 FD_IS_NONBLOCKING_PIPE
,
68 static int fd_is_nonblock_pipe(int fd
) {
72 /* Checks whether the specified file descriptor refers to a pipe, and if so if O_NONBLOCK is set. */
74 if (fstat(fd
, &st
) < 0)
77 if (!S_ISFIFO(st
.st_mode
))
80 flags
= fcntl(fd
, F_GETFL
);
84 return (flags
& O_NONBLOCK
) == O_NONBLOCK
? FD_IS_NONBLOCKING_PIPE
: FD_IS_BLOCKING_PIPE
;
92 size_t *ret_remains_size
) {
94 bool try_cfr
= true, try_sendfile
= true, try_splice
= true;
95 int r
, nonblock_pipe
= -1;
96 size_t m
= SSIZE_MAX
; /* that is the maximum that sendfile and c_f_r accept */
101 /* Tries to copy bytes from the file descriptor 'fdf' to 'fdt' in the smartest possible way. Copies a maximum
102 * of 'max_bytes', which may be specified as UINT64_MAX, in which no maximum is applied. Returns negative on
103 * error, zero if EOF is hit before the bytes limit is hit and positive otherwise. If the copy fails for some
104 * reason but we read but didn't yet write some data an ret_remains/ret_remains_size is not NULL, then it will
105 * be initialized with an allocated buffer containing this "remaining" data. Note that these two parameters are
106 * initialized with a valid buffer only on failure and only if there's actually data already read. Otherwise
107 * these parameters if non-NULL are set to NULL. */
111 if (ret_remains_size
)
112 *ret_remains_size
= 0;
114 /* Try btrfs reflinks first. This only works on regular, seekable files, hence let's check the file offsets of
115 * source and destination first. */
116 if ((copy_flags
& COPY_REFLINK
)) {
119 foffset
= lseek(fdf
, 0, SEEK_CUR
);
123 toffset
= lseek(fdt
, 0, SEEK_CUR
);
126 if (foffset
== 0 && toffset
== 0 && max_bytes
== UINT64_MAX
)
127 r
= btrfs_reflink(fdf
, fdt
); /* full file reflink */
129 r
= btrfs_clone_range(fdf
, foffset
, fdt
, toffset
, max_bytes
== UINT64_MAX
? 0 : max_bytes
); /* partial reflink */
133 /* This worked, yay! Now — to be fully correct — let's adjust the file pointers */
134 if (max_bytes
== UINT64_MAX
) {
136 /* We cloned to the end of the source file, let's position the read
137 * pointer there, and query it at the same time. */
138 t
= lseek(fdf
, 0, SEEK_END
);
144 /* Let's adjust the destination file write pointer by the same number
146 t
= lseek(fdt
, toffset
+ (t
- foffset
), SEEK_SET
);
150 return 0; /* we copied the whole thing, hence hit EOF, return 0 */
152 t
= lseek(fdf
, foffset
+ max_bytes
, SEEK_SET
);
156 t
= lseek(fdt
, toffset
+ max_bytes
, SEEK_SET
);
160 return 1; /* we copied only some number of bytes, which worked, but this means we didn't hit EOF, return 1 */
164 log_debug_errno(r
, "Reflinking didn't work, falling back to non-reflink copying: %m");
173 return 1; /* return > 0 if we hit the max_bytes limit */
175 if (max_bytes
!= UINT64_MAX
&& m
> max_bytes
)
178 /* First try copy_file_range(), unless we already tried */
180 n
= try_copy_file_range(fdf
, NULL
, fdt
, NULL
, m
, 0u);
182 if (!IN_SET(n
, -EINVAL
, -ENOSYS
, -EXDEV
, -EBADF
))
186 /* use fallback below */
187 } else if (n
== 0) /* EOF */
194 /* First try sendfile(), unless we already tried */
196 n
= sendfile(fdt
, fdf
, NULL
, m
);
198 if (!IN_SET(errno
, EINVAL
, ENOSYS
))
201 try_sendfile
= false;
202 /* use fallback below */
203 } else if (n
== 0) /* EOF */
210 /* Then try splice, unless we already tried. */
213 /* splice()'s asynchronous I/O support is a bit weird. When it encounters a pipe file
214 * descriptor, then it will ignore its O_NONBLOCK flag and instead only honour the
215 * SPLICE_F_NONBLOCK flag specified in its flag parameter. Let's hide this behaviour here, and
216 * check if either of the specified fds are a pipe, and if so, let's pass the flag
217 * automatically, depending on O_NONBLOCK being set.
219 * Here's a twist though: when we use it to move data between two pipes of which one has
220 * O_NONBLOCK set and the other has not, then we have no individual control over O_NONBLOCK
221 * behaviour. Hence in that case we can't use splice() and still guarantee systematic
222 * O_NONBLOCK behaviour, hence don't. */
224 if (nonblock_pipe
< 0) {
227 /* Check if either of these fds is a pipe, and if so non-blocking or not */
228 a
= fd_is_nonblock_pipe(fdf
);
232 b
= fd_is_nonblock_pipe(fdt
);
236 if ((a
== FD_IS_NO_PIPE
&& b
== FD_IS_NO_PIPE
) ||
237 (a
== FD_IS_BLOCKING_PIPE
&& b
== FD_IS_NONBLOCKING_PIPE
) ||
238 (a
== FD_IS_NONBLOCKING_PIPE
&& b
== FD_IS_BLOCKING_PIPE
))
240 /* splice() only works if one of the fds is a pipe. If neither is, let's skip
241 * this step right-away. As mentioned above, if one of the two fds refers to a
242 * blocking pipe and the other to a non-blocking pipe, we can't use splice()
243 * either, hence don't try either. This hence means we can only use splice() if
244 * either only one of the two fds is a pipe, or if both are pipes with the same
245 * nonblocking flag setting. */
249 nonblock_pipe
= a
== FD_IS_NONBLOCKING_PIPE
|| b
== FD_IS_NONBLOCKING_PIPE
;
254 n
= splice(fdf
, NULL
, fdt
, NULL
, m
, nonblock_pipe
? SPLICE_F_NONBLOCK
: 0);
256 if (!IN_SET(errno
, EINVAL
, ENOSYS
))
260 /* use fallback below */
261 } else if (n
== 0) /* EOF */
268 /* As a fallback just copy bits by hand */
270 uint8_t buf
[MIN(m
, COPY_BUFFER_SIZE
)], *p
= buf
;
273 n
= read(fdf
, buf
, sizeof buf
);
276 if (n
== 0) /* EOF */
283 k
= write(fdt
, p
, z
);
297 if (ret_remains_size
)
298 *ret_remains_size
= z
;
310 if (max_bytes
!= (uint64_t) -1) {
311 assert(max_bytes
>= (uint64_t) n
);
314 /* sendfile accepts at most SSIZE_MAX-offset bytes to copy,
315 * so reduce our maximum by the amount we already copied,
316 * but don't go below our copy buffer size, unless we are
317 * close the limit of bytes we are allowed to copy. */
318 m
= MAX(MIN(COPY_BUFFER_SIZE
, max_bytes
), m
- n
);
321 return 0; /* return 0 if we hit EOF earlier than the size limit */
324 static int fd_copy_symlink(
327 const struct stat
*st
,
332 CopyFlags copy_flags
) {
334 _cleanup_free_
char *target
= NULL
;
341 r
= readlinkat_malloc(df
, from
, &target
);
345 if (symlinkat(target
, dt
, to
) < 0)
349 uid_is_valid(override_uid
) ? override_uid
: st
->st_uid
,
350 gid_is_valid(override_gid
) ? override_gid
: st
->st_gid
,
351 AT_SYMLINK_NOFOLLOW
) < 0)
357 static int fd_copy_regular(
360 const struct stat
*st
,
365 CopyFlags copy_flags
) {
367 _cleanup_close_
int fdf
= -1, fdt
= -1;
368 struct timespec ts
[2];
375 fdf
= openat(df
, from
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
379 fdt
= openat(dt
, to
, O_WRONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
, st
->st_mode
& 07777);
383 r
= copy_bytes(fdf
, fdt
, (uint64_t) -1, copy_flags
);
385 (void) unlinkat(dt
, to
, 0);
390 uid_is_valid(override_uid
) ? override_uid
: st
->st_uid
,
391 gid_is_valid(override_gid
) ? override_gid
: st
->st_gid
) < 0)
394 if (fchmod(fdt
, st
->st_mode
& 07777) < 0)
399 (void) futimens(fdt
, ts
);
400 (void) copy_xattr(fdf
, fdt
);
407 (void) unlinkat(dt
, to
, 0);
413 static int fd_copy_fifo(
416 const struct stat
*st
,
421 CopyFlags copy_flags
) {
428 r
= mkfifoat(dt
, to
, st
->st_mode
& 07777);
433 uid_is_valid(override_uid
) ? override_uid
: st
->st_uid
,
434 gid_is_valid(override_gid
) ? override_gid
: st
->st_gid
,
435 AT_SYMLINK_NOFOLLOW
) < 0)
438 if (fchmodat(dt
, to
, st
->st_mode
& 07777, 0) < 0)
444 static int fd_copy_node(
447 const struct stat
*st
,
452 CopyFlags copy_flags
) {
459 r
= mknodat(dt
, to
, st
->st_mode
, st
->st_rdev
);
464 uid_is_valid(override_uid
) ? override_uid
: st
->st_uid
,
465 gid_is_valid(override_gid
) ? override_gid
: st
->st_gid
,
466 AT_SYMLINK_NOFOLLOW
) < 0)
469 if (fchmodat(dt
, to
, st
->st_mode
& 07777, 0) < 0)
475 static int fd_copy_directory(
478 const struct stat
*st
,
481 dev_t original_device
,
484 CopyFlags copy_flags
) {
486 _cleanup_close_
int fdf
= -1, fdt
= -1;
487 _cleanup_closedir_
DIR *d
= NULL
;
496 fdf
= openat(df
, from
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
498 fdf
= fcntl(df
, F_DUPFD_CLOEXEC
, 3);
507 r
= mkdirat(dt
, to
, st
->st_mode
& 07777);
510 else if (errno
== EEXIST
&& (copy_flags
& COPY_MERGE
))
515 fdt
= openat(dt
, to
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
521 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
525 if (dot_or_dot_dot(de
->d_name
))
528 if (fstatat(dirfd(d
), de
->d_name
, &buf
, AT_SYMLINK_NOFOLLOW
) < 0) {
533 if (buf
.st_dev
!= original_device
)
536 if (S_ISREG(buf
.st_mode
))
537 q
= fd_copy_regular(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
, override_uid
, override_gid
, copy_flags
);
538 else if (S_ISDIR(buf
.st_mode
))
539 q
= fd_copy_directory(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
, original_device
, override_uid
, override_gid
, copy_flags
);
540 else if (S_ISLNK(buf
.st_mode
))
541 q
= fd_copy_symlink(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
, override_uid
, override_gid
, copy_flags
);
542 else if (S_ISFIFO(buf
.st_mode
))
543 q
= fd_copy_fifo(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
, override_uid
, override_gid
, copy_flags
);
544 else if (S_ISBLK(buf
.st_mode
) || S_ISCHR(buf
.st_mode
) || S_ISSOCK(buf
.st_mode
))
545 q
= fd_copy_node(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
, override_uid
, override_gid
, copy_flags
);
549 if (q
== -EEXIST
&& (copy_flags
& COPY_MERGE
))
557 struct timespec ut
[2] = {
563 uid_is_valid(override_uid
) ? override_uid
: st
->st_uid
,
564 gid_is_valid(override_gid
) ? override_gid
: st
->st_gid
) < 0)
567 if (fchmod(fdt
, st
->st_mode
& 07777) < 0)
570 (void) copy_xattr(dirfd(d
), fdt
);
571 (void) futimens(fdt
, ut
);
577 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
) {
583 if (fstatat(fdf
, from
, &st
, AT_SYMLINK_NOFOLLOW
) < 0)
586 if (S_ISREG(st
.st_mode
))
587 return fd_copy_regular(fdf
, from
, &st
, fdt
, to
, override_uid
, override_gid
, copy_flags
);
588 else if (S_ISDIR(st
.st_mode
))
589 return fd_copy_directory(fdf
, from
, &st
, fdt
, to
, st
.st_dev
, override_uid
, override_gid
, copy_flags
);
590 else if (S_ISLNK(st
.st_mode
))
591 return fd_copy_symlink(fdf
, from
, &st
, fdt
, to
, override_uid
, override_gid
, copy_flags
);
592 else if (S_ISFIFO(st
.st_mode
))
593 return fd_copy_fifo(fdf
, from
, &st
, fdt
, to
, override_uid
, override_gid
, copy_flags
);
594 else if (S_ISBLK(st
.st_mode
) || S_ISCHR(st
.st_mode
) || S_ISSOCK(st
.st_mode
))
595 return fd_copy_node(fdf
, from
, &st
, fdt
, to
, override_uid
, override_gid
, copy_flags
);
600 int copy_tree(const char *from
, const char *to
, uid_t override_uid
, gid_t override_gid
, CopyFlags copy_flags
) {
601 return copy_tree_at(AT_FDCWD
, from
, AT_FDCWD
, to
, override_uid
, override_gid
, copy_flags
);
604 int copy_directory_fd(int dirfd
, const char *to
, CopyFlags copy_flags
) {
610 if (fstat(dirfd
, &st
) < 0)
613 if (!S_ISDIR(st
.st_mode
))
616 return fd_copy_directory(dirfd
, NULL
, &st
, AT_FDCWD
, to
, st
.st_dev
, UID_INVALID
, GID_INVALID
, copy_flags
);
619 int copy_directory(const char *from
, const char *to
, CopyFlags copy_flags
) {
625 if (lstat(from
, &st
) < 0)
628 if (!S_ISDIR(st
.st_mode
))
631 return fd_copy_directory(AT_FDCWD
, from
, &st
, AT_FDCWD
, to
, st
.st_dev
, UID_INVALID
, GID_INVALID
, copy_flags
);
634 int copy_file_fd(const char *from
, int fdt
, CopyFlags copy_flags
) {
635 _cleanup_close_
int fdf
= -1;
641 fdf
= open(from
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
645 r
= copy_bytes(fdf
, fdt
, (uint64_t) -1, copy_flags
);
647 (void) copy_times(fdf
, fdt
);
648 (void) copy_xattr(fdf
, fdt
);
653 int copy_file(const char *from
, const char *to
, int flags
, mode_t mode
, unsigned chattr_flags
, CopyFlags copy_flags
) {
659 RUN_WITH_UMASK(0000) {
660 fdt
= open(to
, flags
|O_WRONLY
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
, mode
);
665 if (chattr_flags
!= 0)
666 (void) chattr_fd(fdt
, chattr_flags
, (unsigned) -1);
668 r
= copy_file_fd(from
, fdt
, copy_flags
);
675 if (close(fdt
) < 0) {
683 int copy_file_atomic(const char *from
, const char *to
, mode_t mode
, unsigned chattr_flags
, CopyFlags copy_flags
) {
684 _cleanup_free_
char *t
= NULL
;
690 r
= tempfn_random(to
, NULL
, &t
);
694 r
= copy_file(from
, t
, O_NOFOLLOW
|O_EXCL
, mode
, chattr_flags
, copy_flags
);
698 if (copy_flags
& COPY_REPLACE
) {
699 r
= renameat(AT_FDCWD
, t
, AT_FDCWD
, to
);
703 r
= rename_noreplace(AT_FDCWD
, t
, AT_FDCWD
, to
);
712 int copy_times(int fdf
, int fdt
) {
713 struct timespec ut
[2];
720 if (fstat(fdf
, &st
) < 0)
726 if (futimens(fdt
, ut
) < 0)
729 if (fd_getcrtime(fdf
, &crtime
) >= 0)
730 (void) fd_setcrtime(fdt
, crtime
);
735 int copy_xattr(int fdf
, int fdt
) {
736 _cleanup_free_
char *bufa
= NULL
, *bufb
= NULL
;
737 size_t sza
= 100, szb
= 100;
747 n
= flistxattr(fdf
, bufa
, sza
);
765 assert(l
< (size_t) n
);
767 if (startswith(p
, "user.")) {
776 m
= fgetxattr(fdf
, p
, bufb
, szb
);
778 if (errno
== ERANGE
) {
787 if (fsetxattr(fdt
, p
, bufb
, m
, 0) < 0)