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;
60 int copy_bytes(int fdf, int fdt, uint64_t max_bytes, CopyFlags copy_flags) {
61 bool try_cfr = true, try_sendfile = true, try_splice = true;
63 size_t m = SSIZE_MAX; /* that is the maximum that sendfile and c_f_r accept */
68 /* Tries to copy bytes from the file descriptor 'fdf' to 'fdt' in the smartest possible way. Copies a maximum
69 * of 'max_bytes', which may be specified as UINT64_MAX, in which no maximum is applied. Returns negative on
70 * error, zero if EOF is hit before the bytes limit is hit and positive otherwise. */
72 #if 0 /// UNNEEDED by elogind
73 /* Try btrfs reflinks first. */
74 if ((copy_flags & COPY_REFLINK) &&
75 max_bytes == (uint64_t) -1 &&
76 lseek(fdf, 0, SEEK_CUR) == 0 &&
77 lseek(fdt, 0, SEEK_CUR) == 0) {
79 r = btrfs_reflink(fdf, fdt);
81 return 0; /* we copied the whole thing, hence hit EOF, return 0 */
88 if (max_bytes != (uint64_t) -1) {
90 return 1; /* return > 0 if we hit the max_bytes limit */
96 /* First try copy_file_range(), unless we already tried */
98 n = try_copy_file_range(fdf, NULL, fdt, NULL, m, 0u);
100 if (!IN_SET(n, -EINVAL, -ENOSYS, -EXDEV, -EBADF))
104 /* use fallback below */
105 } else if (n == 0) /* EOF */
112 /* First try sendfile(), unless we already tried */
114 n = sendfile(fdt, fdf, NULL, m);
116 if (!IN_SET(errno, EINVAL, ENOSYS))
119 try_sendfile = false;
120 /* use fallback below */
121 } else if (n == 0) /* EOF */
128 /* Then try splice, unless we already tried */
130 n = splice(fdf, NULL, fdt, NULL, m, 0);
132 if (!IN_SET(errno, EINVAL, ENOSYS))
136 /* use fallback below */
137 } else if (n == 0) /* EOF */
144 /* As a fallback just copy bits by hand */
146 uint8_t buf[MIN(m, COPY_BUFFER_SIZE)];
148 n = read(fdf, buf, sizeof buf);
151 if (n == 0) /* EOF */
154 r = loop_write(fdt, buf, (size_t) n, false);
160 if (max_bytes != (uint64_t) -1) {
161 assert(max_bytes >= (uint64_t) n);
164 /* sendfile accepts at most SSIZE_MAX-offset bytes to copy,
165 * so reduce our maximum by the amount we already copied,
166 * but don't go below our copy buffer size, unless we are
167 * close the limit of bytes we are allowed to copy. */
168 m = MAX(MIN(COPY_BUFFER_SIZE, max_bytes), m - n);
171 return 0; /* return 0 if we hit EOF earlier than the size limit */
174 #if 0 /// UNNEEDED by elogind
175 static int fd_copy_symlink(
178 const struct stat *st,
183 CopyFlags copy_flags) {
185 _cleanup_free_ char *target = NULL;
192 r = readlinkat_malloc(df, from, &target);
196 if (symlinkat(target, dt, to) < 0)
200 uid_is_valid(override_uid) ? override_uid : st->st_uid,
201 gid_is_valid(override_gid) ? override_gid : st->st_gid,
202 AT_SYMLINK_NOFOLLOW) < 0)
208 static int fd_copy_regular(
211 const struct stat *st,
216 CopyFlags copy_flags) {
218 _cleanup_close_ int fdf = -1, fdt = -1;
219 struct timespec ts[2];
226 fdf = openat(df, from, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
230 fdt = openat(dt, to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, st->st_mode & 07777);
234 r = copy_bytes(fdf, fdt, (uint64_t) -1, copy_flags);
236 (void) unlinkat(dt, to, 0);
241 uid_is_valid(override_uid) ? override_uid : st->st_uid,
242 gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0)
245 if (fchmod(fdt, st->st_mode & 07777) < 0)
250 (void) futimens(fdt, ts);
251 (void) copy_xattr(fdf, fdt);
258 (void) unlinkat(dt, to, 0);
264 static int fd_copy_fifo(
267 const struct stat *st,
272 CopyFlags copy_flags) {
279 r = mkfifoat(dt, to, st->st_mode & 07777);
284 uid_is_valid(override_uid) ? override_uid : st->st_uid,
285 gid_is_valid(override_gid) ? override_gid : st->st_gid,
286 AT_SYMLINK_NOFOLLOW) < 0)
289 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
295 static int fd_copy_node(
298 const struct stat *st,
303 CopyFlags copy_flags) {
310 r = mknodat(dt, to, st->st_mode, st->st_rdev);
315 uid_is_valid(override_uid) ? override_uid : st->st_uid,
316 gid_is_valid(override_gid) ? override_gid : st->st_gid,
317 AT_SYMLINK_NOFOLLOW) < 0)
320 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
326 static int fd_copy_directory(
329 const struct stat *st,
332 dev_t original_device,
335 CopyFlags copy_flags) {
337 _cleanup_close_ int fdf = -1, fdt = -1;
338 _cleanup_closedir_ DIR *d = NULL;
347 fdf = openat(df, from, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
349 fdf = fcntl(df, F_DUPFD_CLOEXEC, 3);
358 r = mkdirat(dt, to, st->st_mode & 07777);
361 else if (errno == EEXIST && (copy_flags & COPY_MERGE))
366 fdt = openat(dt, to, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
372 FOREACH_DIRENT_ALL(de, d, return -errno) {
376 if (dot_or_dot_dot(de->d_name))
379 if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
384 if (buf.st_dev != original_device)
387 if (S_ISREG(buf.st_mode))
388 q = fd_copy_regular(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
389 else if (S_ISDIR(buf.st_mode))
390 q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, override_uid, override_gid, copy_flags);
391 else if (S_ISLNK(buf.st_mode))
392 q = fd_copy_symlink(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
393 else if (S_ISFIFO(buf.st_mode))
394 q = fd_copy_fifo(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
395 else if (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode) || S_ISSOCK(buf.st_mode))
396 q = fd_copy_node(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
400 if (q == -EEXIST && (copy_flags & COPY_MERGE))
408 struct timespec ut[2] = {
414 uid_is_valid(override_uid) ? override_uid : st->st_uid,
415 gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0)
418 if (fchmod(fdt, st->st_mode & 07777) < 0)
421 (void) copy_xattr(dirfd(d), fdt);
422 (void) futimens(fdt, ut);
428 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) {
434 if (fstatat(fdf, from, &st, AT_SYMLINK_NOFOLLOW) < 0)
437 if (S_ISREG(st.st_mode))
438 return fd_copy_regular(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
439 else if (S_ISDIR(st.st_mode))
440 return fd_copy_directory(fdf, from, &st, fdt, to, st.st_dev, override_uid, override_gid, copy_flags);
441 else if (S_ISLNK(st.st_mode))
442 return fd_copy_symlink(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
443 else if (S_ISFIFO(st.st_mode))
444 return fd_copy_fifo(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
445 else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode) || S_ISSOCK(st.st_mode))
446 return fd_copy_node(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
451 int copy_tree(const char *from, const char *to, uid_t override_uid, gid_t override_gid, CopyFlags copy_flags) {
452 return copy_tree_at(AT_FDCWD, from, AT_FDCWD, to, override_uid, override_gid, copy_flags);
455 int copy_directory_fd(int dirfd, const char *to, CopyFlags copy_flags) {
461 if (fstat(dirfd, &st) < 0)
464 if (!S_ISDIR(st.st_mode))
467 return fd_copy_directory(dirfd, NULL, &st, AT_FDCWD, to, st.st_dev, UID_INVALID, GID_INVALID, copy_flags);
470 int copy_directory(const char *from, const char *to, CopyFlags copy_flags) {
476 if (lstat(from, &st) < 0)
479 if (!S_ISDIR(st.st_mode))
482 return fd_copy_directory(AT_FDCWD, from, &st, AT_FDCWD, to, st.st_dev, UID_INVALID, GID_INVALID, copy_flags);
485 int copy_file_fd(const char *from, int fdt, CopyFlags copy_flags) {
486 _cleanup_close_ int fdf = -1;
492 fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
496 r = copy_bytes(fdf, fdt, (uint64_t) -1, copy_flags);
498 (void) copy_times(fdf, fdt);
499 (void) copy_xattr(fdf, fdt);
504 int copy_file(const char *from, const char *to, int flags, mode_t mode, unsigned chattr_flags, CopyFlags copy_flags) {
510 RUN_WITH_UMASK(0000) {
511 fdt = open(to, flags|O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode);
516 if (chattr_flags != 0)
517 (void) chattr_fd(fdt, chattr_flags, (unsigned) -1);
519 r = copy_file_fd(from, fdt, copy_flags);
526 if (close(fdt) < 0) {
534 int copy_file_atomic(const char *from, const char *to, mode_t mode, unsigned chattr_flags, CopyFlags copy_flags) {
535 _cleanup_free_ char *t = NULL;
541 r = tempfn_random(to, NULL, &t);
545 r = copy_file(from, t, O_NOFOLLOW|O_EXCL, mode, chattr_flags, copy_flags);
549 if (copy_flags & COPY_REPLACE) {
550 r = renameat(AT_FDCWD, t, AT_FDCWD, to);
554 r = rename_noreplace(AT_FDCWD, t, AT_FDCWD, to);
563 int copy_times(int fdf, int fdt) {
564 struct timespec ut[2];
571 if (fstat(fdf, &st) < 0)
577 if (futimens(fdt, ut) < 0)
580 if (fd_getcrtime(fdf, &crtime) >= 0)
581 (void) fd_setcrtime(fdt, crtime);
586 int copy_xattr(int fdf, int fdt) {
587 _cleanup_free_ char *bufa = NULL, *bufb = NULL;
588 size_t sza = 100, szb = 100;
598 n = flistxattr(fdf, bufa, sza);
616 assert(l < (size_t) n);
618 if (startswith(p, "user.")) {
627 m = fgetxattr(fdf, p, bufb, szb);
629 if (errno == ERANGE) {
638 if (fsetxattr(fdt, p, bufb, m, 0) < 0)