1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Lennart Poettering
15 #include <sys/sendfile.h>
16 //#include <sys/stat.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"
26 //#include "fd-util.h"
28 //#include "fs-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 #if 0 /// UNNEEDED by elogind
88 /* Try btrfs reflinks first. This only works on regular, seekable files, hence let's check the file offsets of
89 * source and destination first. */
90 if ((copy_flags & COPY_REFLINK)) {
93 foffset = lseek(fdf, 0, SEEK_CUR);
97 toffset = lseek(fdt, 0, SEEK_CUR);
100 if (foffset == 0 && toffset == 0 && max_bytes == UINT64_MAX)
101 r = btrfs_reflink(fdf, fdt); /* full file reflink */
103 r = btrfs_clone_range(fdf, foffset, fdt, toffset, max_bytes == UINT64_MAX ? 0 : max_bytes); /* partial reflink */
107 /* This worked, yay! Now — to be fully correct — let's adjust the file pointers */
108 if (max_bytes == UINT64_MAX) {
110 /* We cloned to the end of the source file, let's position the read
111 * pointer there, and query it at the same time. */
112 t = lseek(fdf, 0, SEEK_END);
118 /* Let's adjust the destination file write pointer by the same number
120 t = lseek(fdt, toffset + (t - foffset), SEEK_SET);
124 return 0; /* we copied the whole thing, hence hit EOF, return 0 */
126 t = lseek(fdf, foffset + max_bytes, SEEK_SET);
130 t = lseek(fdt, toffset + max_bytes, SEEK_SET);
134 return 1; /* we copied only some number of bytes, which worked, but this means we didn't hit EOF, return 1 */
138 log_debug_errno(r, "Reflinking didn't work, falling back to non-reflink copying: %m");
147 if (max_bytes != (uint64_t) -1) {
149 return 1; /* return > 0 if we hit the max_bytes limit */
155 /* First try copy_file_range(), unless we already tried */
157 n = try_copy_file_range(fdf, NULL, fdt, NULL, m, 0u);
159 if (!IN_SET(n, -EINVAL, -ENOSYS, -EXDEV, -EBADF))
163 /* use fallback below */
164 } else if (n == 0) /* EOF */
171 /* First try sendfile(), unless we already tried */
173 n = sendfile(fdt, fdf, NULL, m);
175 if (!IN_SET(errno, EINVAL, ENOSYS))
178 try_sendfile = false;
179 /* use fallback below */
180 } else if (n == 0) /* EOF */
187 /* Then try splice, unless we already tried */
189 n = splice(fdf, NULL, fdt, NULL, m, 0);
191 if (!IN_SET(errno, EINVAL, ENOSYS))
195 /* use fallback below */
196 } else if (n == 0) /* EOF */
203 /* As a fallback just copy bits by hand */
205 uint8_t buf[MIN(m, COPY_BUFFER_SIZE)], *p = buf;
208 n = read(fdf, buf, sizeof buf);
211 if (n == 0) /* EOF */
218 k = write(fdt, p, z);
232 if (ret_remains_size)
233 *ret_remains_size = z;
245 if (max_bytes != (uint64_t) -1) {
246 assert(max_bytes >= (uint64_t) n);
249 /* sendfile accepts at most SSIZE_MAX-offset bytes to copy,
250 * so reduce our maximum by the amount we already copied,
251 * but don't go below our copy buffer size, unless we are
252 * close the limit of bytes we are allowed to copy. */
253 m = MAX(MIN(COPY_BUFFER_SIZE, max_bytes), m - n);
256 return 0; /* return 0 if we hit EOF earlier than the size limit */
259 #if 0 /// UNNEEDED by elogind
260 static int fd_copy_symlink(
263 const struct stat *st,
268 CopyFlags copy_flags) {
270 _cleanup_free_ char *target = NULL;
277 r = readlinkat_malloc(df, from, &target);
281 if (symlinkat(target, dt, to) < 0)
285 uid_is_valid(override_uid) ? override_uid : st->st_uid,
286 gid_is_valid(override_gid) ? override_gid : st->st_gid,
287 AT_SYMLINK_NOFOLLOW) < 0)
293 static int fd_copy_regular(
296 const struct stat *st,
301 CopyFlags copy_flags) {
303 _cleanup_close_ int fdf = -1, fdt = -1;
304 struct timespec ts[2];
311 fdf = openat(df, from, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
315 fdt = openat(dt, to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, st->st_mode & 07777);
319 r = copy_bytes(fdf, fdt, (uint64_t) -1, copy_flags);
321 (void) unlinkat(dt, to, 0);
326 uid_is_valid(override_uid) ? override_uid : st->st_uid,
327 gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0)
330 if (fchmod(fdt, st->st_mode & 07777) < 0)
335 (void) futimens(fdt, ts);
336 (void) copy_xattr(fdf, fdt);
343 (void) unlinkat(dt, to, 0);
349 static int fd_copy_fifo(
352 const struct stat *st,
357 CopyFlags copy_flags) {
364 r = mkfifoat(dt, to, st->st_mode & 07777);
369 uid_is_valid(override_uid) ? override_uid : st->st_uid,
370 gid_is_valid(override_gid) ? override_gid : st->st_gid,
371 AT_SYMLINK_NOFOLLOW) < 0)
374 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
380 static int fd_copy_node(
383 const struct stat *st,
388 CopyFlags copy_flags) {
395 r = mknodat(dt, to, st->st_mode, st->st_rdev);
400 uid_is_valid(override_uid) ? override_uid : st->st_uid,
401 gid_is_valid(override_gid) ? override_gid : st->st_gid,
402 AT_SYMLINK_NOFOLLOW) < 0)
405 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
411 static int fd_copy_directory(
414 const struct stat *st,
417 dev_t original_device,
420 CopyFlags copy_flags) {
422 _cleanup_close_ int fdf = -1, fdt = -1;
423 _cleanup_closedir_ DIR *d = NULL;
432 fdf = openat(df, from, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
434 fdf = fcntl(df, F_DUPFD_CLOEXEC, 3);
443 r = mkdirat(dt, to, st->st_mode & 07777);
446 else if (errno == EEXIST && (copy_flags & COPY_MERGE))
451 fdt = openat(dt, to, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
457 FOREACH_DIRENT_ALL(de, d, return -errno) {
461 if (dot_or_dot_dot(de->d_name))
464 if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
469 if (buf.st_dev != original_device)
472 if (S_ISREG(buf.st_mode))
473 q = fd_copy_regular(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
474 else if (S_ISDIR(buf.st_mode))
475 q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, override_uid, override_gid, copy_flags);
476 else if (S_ISLNK(buf.st_mode))
477 q = fd_copy_symlink(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
478 else if (S_ISFIFO(buf.st_mode))
479 q = fd_copy_fifo(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
480 else if (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode) || S_ISSOCK(buf.st_mode))
481 q = fd_copy_node(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
485 if (q == -EEXIST && (copy_flags & COPY_MERGE))
493 struct timespec ut[2] = {
499 uid_is_valid(override_uid) ? override_uid : st->st_uid,
500 gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0)
503 if (fchmod(fdt, st->st_mode & 07777) < 0)
506 (void) copy_xattr(dirfd(d), fdt);
507 (void) futimens(fdt, ut);
513 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) {
519 if (fstatat(fdf, from, &st, AT_SYMLINK_NOFOLLOW) < 0)
522 if (S_ISREG(st.st_mode))
523 return fd_copy_regular(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
524 else if (S_ISDIR(st.st_mode))
525 return fd_copy_directory(fdf, from, &st, fdt, to, st.st_dev, override_uid, override_gid, copy_flags);
526 else if (S_ISLNK(st.st_mode))
527 return fd_copy_symlink(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
528 else if (S_ISFIFO(st.st_mode))
529 return fd_copy_fifo(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
530 else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode) || S_ISSOCK(st.st_mode))
531 return fd_copy_node(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
536 int copy_tree(const char *from, const char *to, uid_t override_uid, gid_t override_gid, CopyFlags copy_flags) {
537 return copy_tree_at(AT_FDCWD, from, AT_FDCWD, to, override_uid, override_gid, copy_flags);
540 int copy_directory_fd(int dirfd, const char *to, CopyFlags copy_flags) {
546 if (fstat(dirfd, &st) < 0)
549 if (!S_ISDIR(st.st_mode))
552 return fd_copy_directory(dirfd, NULL, &st, AT_FDCWD, to, st.st_dev, UID_INVALID, GID_INVALID, copy_flags);
555 int copy_directory(const char *from, const char *to, CopyFlags copy_flags) {
561 if (lstat(from, &st) < 0)
564 if (!S_ISDIR(st.st_mode))
567 return fd_copy_directory(AT_FDCWD, from, &st, AT_FDCWD, to, st.st_dev, UID_INVALID, GID_INVALID, copy_flags);
570 int copy_file_fd(const char *from, int fdt, CopyFlags copy_flags) {
571 _cleanup_close_ int fdf = -1;
577 fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
581 r = copy_bytes(fdf, fdt, (uint64_t) -1, copy_flags);
583 (void) copy_times(fdf, fdt);
584 (void) copy_xattr(fdf, fdt);
589 int copy_file(const char *from, const char *to, int flags, mode_t mode, unsigned chattr_flags, CopyFlags copy_flags) {
595 RUN_WITH_UMASK(0000) {
596 fdt = open(to, flags|O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode);
601 if (chattr_flags != 0)
602 (void) chattr_fd(fdt, chattr_flags, (unsigned) -1);
604 r = copy_file_fd(from, fdt, copy_flags);
611 if (close(fdt) < 0) {
619 int copy_file_atomic(const char *from, const char *to, mode_t mode, unsigned chattr_flags, CopyFlags copy_flags) {
620 _cleanup_free_ char *t = NULL;
626 r = tempfn_random(to, NULL, &t);
630 r = copy_file(from, t, O_NOFOLLOW|O_EXCL, mode, chattr_flags, copy_flags);
634 if (copy_flags & COPY_REPLACE) {
635 r = renameat(AT_FDCWD, t, AT_FDCWD, to);
639 r = rename_noreplace(AT_FDCWD, t, AT_FDCWD, to);
648 int copy_times(int fdf, int fdt) {
649 struct timespec ut[2];
656 if (fstat(fdf, &st) < 0)
662 if (futimens(fdt, ut) < 0)
665 if (fd_getcrtime(fdf, &crtime) >= 0)
666 (void) fd_setcrtime(fdt, crtime);
671 int copy_xattr(int fdf, int fdt) {
672 _cleanup_free_ char *bufa = NULL, *bufb = NULL;
673 size_t sza = 100, szb = 100;
683 n = flistxattr(fdf, bufa, sza);
701 assert(l < (size_t) n);
703 if (startswith(p, "user.")) {
712 m = fgetxattr(fdf, p, bufb, szb);
714 if (errno == ERANGE) {
723 if (fsetxattr(fdt, p, bufb, m, 0) < 0)