]>
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(int fd_in
, loff_t
*off_in
,
42 int fd_out
, loff_t
*off_out
,
51 r
= copy_file_range(fd_in
, off_in
, fd_out
, off_out
, len
, flags
);
52 if (_unlikely_(have
< 0))
53 have
= r
>= 0 || errno
!= ENOSYS
;
65 size_t *ret_remains_size
) {
67 bool try_cfr
= true, try_sendfile
= true, try_splice
= true;
69 size_t m
= SSIZE_MAX
; /* that is the maximum that sendfile and c_f_r accept */
74 /* Tries to copy bytes from the file descriptor 'fdf' to 'fdt' in the smartest possible way. Copies a maximum
75 * of 'max_bytes', which may be specified as UINT64_MAX, in which no maximum is applied. Returns negative on
76 * error, zero if EOF is hit before the bytes limit is hit and positive otherwise. If the copy fails for some
77 * reason but we read but didn't yet write some data an ret_remains/ret_remains_size is not NULL, then it will
78 * be initialized with an allocated buffer containing this "remaining" data. Note that these two parameters are
79 * initialized with a valid buffer only on failure and only if there's actually data already read. Otherwise
80 * these parameters if non-NULL are set to NULL. */
85 *ret_remains_size
= 0;
87 /* Try btrfs reflinks first. This only works on regular, seekable files, hence let's check the file offsets of
88 * source and destination first. */
89 if ((copy_flags
& COPY_REFLINK
)) {
92 foffset
= lseek(fdf
, 0, SEEK_CUR
);
96 toffset
= lseek(fdt
, 0, SEEK_CUR
);
99 if (foffset
== 0 && toffset
== 0 && max_bytes
== UINT64_MAX
)
100 r
= btrfs_reflink(fdf
, fdt
); /* full file reflink */
102 r
= btrfs_clone_range(fdf
, foffset
, fdt
, toffset
, max_bytes
== UINT64_MAX
? 0 : max_bytes
); /* partial reflink */
106 /* This worked, yay! Now — to be fully correct — let's adjust the file pointers */
107 if (max_bytes
== UINT64_MAX
) {
109 /* We cloned to the end of the source file, let's position the read
110 * pointer there, and query it at the same time. */
111 t
= lseek(fdf
, 0, SEEK_END
);
117 /* Let's adjust the destination file write pointer by the same number
119 t
= lseek(fdt
, toffset
+ (t
- foffset
), SEEK_SET
);
123 return 0; /* we copied the whole thing, hence hit EOF, return 0 */
125 t
= lseek(fdf
, foffset
+ max_bytes
, SEEK_SET
);
129 t
= lseek(fdt
, toffset
+ max_bytes
, SEEK_SET
);
133 return 1; /* we copied only some number of bytes, which worked, but this means we didn't hit EOF, return 1 */
137 log_debug_errno(r
, "Reflinking didn't work, falling back to non-reflink copying: %m");
145 if (max_bytes
!= (uint64_t) -1) {
147 return 1; /* return > 0 if we hit the max_bytes limit */
153 /* First try copy_file_range(), unless we already tried */
155 n
= try_copy_file_range(fdf
, NULL
, fdt
, NULL
, m
, 0u);
157 if (!IN_SET(n
, -EINVAL
, -ENOSYS
, -EXDEV
, -EBADF
))
161 /* use fallback below */
162 } else if (n
== 0) /* EOF */
169 /* First try sendfile(), unless we already tried */
171 n
= sendfile(fdt
, fdf
, NULL
, m
);
173 if (!IN_SET(errno
, EINVAL
, ENOSYS
))
176 try_sendfile
= false;
177 /* use fallback below */
178 } else if (n
== 0) /* EOF */
185 /* Then try splice, unless we already tried */
187 n
= splice(fdf
, NULL
, fdt
, NULL
, m
, 0);
189 if (!IN_SET(errno
, EINVAL
, ENOSYS
))
193 /* use fallback below */
194 } else if (n
== 0) /* EOF */
201 /* As a fallback just copy bits by hand */
203 uint8_t buf
[MIN(m
, COPY_BUFFER_SIZE
)], *p
= buf
;
206 n
= read(fdf
, buf
, sizeof buf
);
209 if (n
== 0) /* EOF */
216 k
= write(fdt
, p
, z
);
230 if (ret_remains_size
)
231 *ret_remains_size
= z
;
243 if (max_bytes
!= (uint64_t) -1) {
244 assert(max_bytes
>= (uint64_t) n
);
247 /* sendfile accepts at most SSIZE_MAX-offset bytes to copy,
248 * so reduce our maximum by the amount we already copied,
249 * but don't go below our copy buffer size, unless we are
250 * close the limit of bytes we are allowed to copy. */
251 m
= MAX(MIN(COPY_BUFFER_SIZE
, max_bytes
), m
- n
);
254 return 0; /* return 0 if we hit EOF earlier than the size limit */
257 static int fd_copy_symlink(
260 const struct stat
*st
,
265 CopyFlags copy_flags
) {
267 _cleanup_free_
char *target
= NULL
;
274 r
= readlinkat_malloc(df
, from
, &target
);
278 if (symlinkat(target
, dt
, to
) < 0)
282 uid_is_valid(override_uid
) ? override_uid
: st
->st_uid
,
283 gid_is_valid(override_gid
) ? override_gid
: st
->st_gid
,
284 AT_SYMLINK_NOFOLLOW
) < 0)
290 static int fd_copy_regular(
293 const struct stat
*st
,
298 CopyFlags copy_flags
) {
300 _cleanup_close_
int fdf
= -1, fdt
= -1;
301 struct timespec ts
[2];
308 fdf
= openat(df
, from
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
312 fdt
= openat(dt
, to
, O_WRONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
, st
->st_mode
& 07777);
316 r
= copy_bytes(fdf
, fdt
, (uint64_t) -1, copy_flags
);
318 (void) unlinkat(dt
, to
, 0);
323 uid_is_valid(override_uid
) ? override_uid
: st
->st_uid
,
324 gid_is_valid(override_gid
) ? override_gid
: st
->st_gid
) < 0)
327 if (fchmod(fdt
, st
->st_mode
& 07777) < 0)
332 (void) futimens(fdt
, ts
);
333 (void) copy_xattr(fdf
, fdt
);
340 (void) unlinkat(dt
, to
, 0);
346 static int fd_copy_fifo(
349 const struct stat
*st
,
354 CopyFlags copy_flags
) {
361 r
= mkfifoat(dt
, to
, st
->st_mode
& 07777);
366 uid_is_valid(override_uid
) ? override_uid
: st
->st_uid
,
367 gid_is_valid(override_gid
) ? override_gid
: st
->st_gid
,
368 AT_SYMLINK_NOFOLLOW
) < 0)
371 if (fchmodat(dt
, to
, st
->st_mode
& 07777, 0) < 0)
377 static int fd_copy_node(
380 const struct stat
*st
,
385 CopyFlags copy_flags
) {
392 r
= mknodat(dt
, to
, st
->st_mode
, st
->st_rdev
);
397 uid_is_valid(override_uid
) ? override_uid
: st
->st_uid
,
398 gid_is_valid(override_gid
) ? override_gid
: st
->st_gid
,
399 AT_SYMLINK_NOFOLLOW
) < 0)
402 if (fchmodat(dt
, to
, st
->st_mode
& 07777, 0) < 0)
408 static int fd_copy_directory(
411 const struct stat
*st
,
414 dev_t original_device
,
417 CopyFlags copy_flags
) {
419 _cleanup_close_
int fdf
= -1, fdt
= -1;
420 _cleanup_closedir_
DIR *d
= NULL
;
429 fdf
= openat(df
, from
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
431 fdf
= fcntl(df
, F_DUPFD_CLOEXEC
, 3);
440 r
= mkdirat(dt
, to
, st
->st_mode
& 07777);
443 else if (errno
== EEXIST
&& (copy_flags
& COPY_MERGE
))
448 fdt
= openat(dt
, to
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
454 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
458 if (dot_or_dot_dot(de
->d_name
))
461 if (fstatat(dirfd(d
), de
->d_name
, &buf
, AT_SYMLINK_NOFOLLOW
) < 0) {
466 if (buf
.st_dev
!= original_device
)
469 if (S_ISREG(buf
.st_mode
))
470 q
= fd_copy_regular(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
, override_uid
, override_gid
, copy_flags
);
471 else if (S_ISDIR(buf
.st_mode
))
472 q
= fd_copy_directory(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
, original_device
, override_uid
, override_gid
, copy_flags
);
473 else if (S_ISLNK(buf
.st_mode
))
474 q
= fd_copy_symlink(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
, override_uid
, override_gid
, copy_flags
);
475 else if (S_ISFIFO(buf
.st_mode
))
476 q
= fd_copy_fifo(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
, override_uid
, override_gid
, copy_flags
);
477 else if (S_ISBLK(buf
.st_mode
) || S_ISCHR(buf
.st_mode
) || S_ISSOCK(buf
.st_mode
))
478 q
= fd_copy_node(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
, override_uid
, override_gid
, copy_flags
);
482 if (q
== -EEXIST
&& (copy_flags
& COPY_MERGE
))
490 struct timespec ut
[2] = {
496 uid_is_valid(override_uid
) ? override_uid
: st
->st_uid
,
497 gid_is_valid(override_gid
) ? override_gid
: st
->st_gid
) < 0)
500 if (fchmod(fdt
, st
->st_mode
& 07777) < 0)
503 (void) copy_xattr(dirfd(d
), fdt
);
504 (void) futimens(fdt
, ut
);
510 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
) {
516 if (fstatat(fdf
, from
, &st
, AT_SYMLINK_NOFOLLOW
) < 0)
519 if (S_ISREG(st
.st_mode
))
520 return fd_copy_regular(fdf
, from
, &st
, fdt
, to
, override_uid
, override_gid
, copy_flags
);
521 else if (S_ISDIR(st
.st_mode
))
522 return fd_copy_directory(fdf
, from
, &st
, fdt
, to
, st
.st_dev
, override_uid
, override_gid
, copy_flags
);
523 else if (S_ISLNK(st
.st_mode
))
524 return fd_copy_symlink(fdf
, from
, &st
, fdt
, to
, override_uid
, override_gid
, copy_flags
);
525 else if (S_ISFIFO(st
.st_mode
))
526 return fd_copy_fifo(fdf
, from
, &st
, fdt
, to
, override_uid
, override_gid
, copy_flags
);
527 else if (S_ISBLK(st
.st_mode
) || S_ISCHR(st
.st_mode
) || S_ISSOCK(st
.st_mode
))
528 return fd_copy_node(fdf
, from
, &st
, fdt
, to
, override_uid
, override_gid
, copy_flags
);
533 int copy_tree(const char *from
, const char *to
, uid_t override_uid
, gid_t override_gid
, CopyFlags copy_flags
) {
534 return copy_tree_at(AT_FDCWD
, from
, AT_FDCWD
, to
, override_uid
, override_gid
, copy_flags
);
537 int copy_directory_fd(int dirfd
, const char *to
, CopyFlags copy_flags
) {
543 if (fstat(dirfd
, &st
) < 0)
546 if (!S_ISDIR(st
.st_mode
))
549 return fd_copy_directory(dirfd
, NULL
, &st
, AT_FDCWD
, to
, st
.st_dev
, UID_INVALID
, GID_INVALID
, copy_flags
);
552 int copy_directory(const char *from
, const char *to
, CopyFlags copy_flags
) {
558 if (lstat(from
, &st
) < 0)
561 if (!S_ISDIR(st
.st_mode
))
564 return fd_copy_directory(AT_FDCWD
, from
, &st
, AT_FDCWD
, to
, st
.st_dev
, UID_INVALID
, GID_INVALID
, copy_flags
);
567 int copy_file_fd(const char *from
, int fdt
, CopyFlags copy_flags
) {
568 _cleanup_close_
int fdf
= -1;
574 fdf
= open(from
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
578 r
= copy_bytes(fdf
, fdt
, (uint64_t) -1, copy_flags
);
580 (void) copy_times(fdf
, fdt
);
581 (void) copy_xattr(fdf
, fdt
);
586 int copy_file(const char *from
, const char *to
, int flags
, mode_t mode
, unsigned chattr_flags
, CopyFlags copy_flags
) {
592 RUN_WITH_UMASK(0000) {
593 fdt
= open(to
, flags
|O_WRONLY
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
, mode
);
598 if (chattr_flags
!= 0)
599 (void) chattr_fd(fdt
, chattr_flags
, (unsigned) -1);
601 r
= copy_file_fd(from
, fdt
, copy_flags
);
608 if (close(fdt
) < 0) {
616 int copy_file_atomic(const char *from
, const char *to
, mode_t mode
, unsigned chattr_flags
, CopyFlags copy_flags
) {
617 _cleanup_free_
char *t
= NULL
;
623 r
= tempfn_random(to
, NULL
, &t
);
627 r
= copy_file(from
, t
, O_NOFOLLOW
|O_EXCL
, mode
, chattr_flags
, copy_flags
);
631 if (copy_flags
& COPY_REPLACE
) {
632 r
= renameat(AT_FDCWD
, t
, AT_FDCWD
, to
);
636 r
= rename_noreplace(AT_FDCWD
, t
, AT_FDCWD
, to
);
645 int copy_times(int fdf
, int fdt
) {
646 struct timespec ut
[2];
653 if (fstat(fdf
, &st
) < 0)
659 if (futimens(fdt
, ut
) < 0)
662 if (fd_getcrtime(fdf
, &crtime
) >= 0)
663 (void) fd_setcrtime(fdt
, crtime
);
668 int copy_xattr(int fdf
, int fdt
) {
669 _cleanup_free_
char *bufa
= NULL
, *bufb
= NULL
;
670 size_t sza
= 100, szb
= 100;
680 n
= flistxattr(fdf
, bufa
, sza
);
698 assert(l
< (size_t) n
);
700 if (startswith(p
, "user.")) {
709 m
= fgetxattr(fdf
, p
, bufb
, szb
);
711 if (errno
== ERANGE
) {
720 if (fsetxattr(fdt
, p
, bufb
, m
, 0) < 0)