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 #if 0 /// UNNEEDED by elogind
69 /* Try btrfs reflinks first. */
70 if ((copy_flags & COPY_REFLINK) &&
71 max_bytes == (uint64_t) -1 &&
72 lseek(fdf, 0, SEEK_CUR) == 0 &&
73 lseek(fdt, 0, SEEK_CUR) == 0) {
75 r = btrfs_reflink(fdf, fdt);
77 return 0; /* we copied the whole thing, hence hit EOF, return 0 */
84 if (max_bytes != (uint64_t) -1) {
86 return 1; /* return > 0 if we hit the max_bytes limit */
92 /* First try copy_file_range(), unless we already tried */
94 n = try_copy_file_range(fdf, NULL, fdt, NULL, m, 0u);
96 if (!IN_SET(n, -EINVAL, -ENOSYS, -EXDEV, -EBADF))
100 /* use fallback below */
101 } else if (n == 0) /* EOF */
108 /* First try sendfile(), unless we already tried */
110 n = sendfile(fdt, fdf, NULL, m);
112 if (!IN_SET(errno, EINVAL, ENOSYS))
115 try_sendfile = false;
116 /* use fallback below */
117 } else if (n == 0) /* EOF */
124 /* Then try splice, unless we already tried */
126 n = splice(fdf, NULL, fdt, NULL, m, 0);
128 if (!IN_SET(errno, EINVAL, ENOSYS))
132 /* use fallback below */
133 } else if (n == 0) /* EOF */
140 /* As a fallback just copy bits by hand */
142 uint8_t buf[MIN(m, COPY_BUFFER_SIZE)];
144 n = read(fdf, buf, sizeof buf);
147 if (n == 0) /* EOF */
150 r = loop_write(fdt, buf, (size_t) n, false);
156 if (max_bytes != (uint64_t) -1) {
157 assert(max_bytes >= (uint64_t) n);
160 /* sendfile accepts at most SSIZE_MAX-offset bytes to copy,
161 * so reduce our maximum by the amount we already copied,
162 * but don't go below our copy buffer size, unless we are
163 * close the limit of bytes we are allowed to copy. */
164 m = MAX(MIN(COPY_BUFFER_SIZE, max_bytes), m - n);
167 return 0; /* return 0 if we hit EOF earlier than the size limit */
170 #if 0 /// UNNEEDED by elogind
171 static int fd_copy_symlink(
174 const struct stat *st,
179 CopyFlags copy_flags) {
181 _cleanup_free_ char *target = NULL;
188 r = readlinkat_malloc(df, from, &target);
192 if (symlinkat(target, dt, to) < 0)
196 uid_is_valid(override_uid) ? override_uid : st->st_uid,
197 gid_is_valid(override_gid) ? override_gid : st->st_gid,
198 AT_SYMLINK_NOFOLLOW) < 0)
204 static int fd_copy_regular(
207 const struct stat *st,
212 CopyFlags copy_flags) {
214 _cleanup_close_ int fdf = -1, fdt = -1;
215 struct timespec ts[2];
222 fdf = openat(df, from, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
226 fdt = openat(dt, to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, st->st_mode & 07777);
230 r = copy_bytes(fdf, fdt, (uint64_t) -1, copy_flags);
232 (void) unlinkat(dt, to, 0);
237 uid_is_valid(override_uid) ? override_uid : st->st_uid,
238 gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0)
241 if (fchmod(fdt, st->st_mode & 07777) < 0)
246 (void) futimens(fdt, ts);
247 (void) copy_xattr(fdf, fdt);
254 (void) unlinkat(dt, to, 0);
260 static int fd_copy_fifo(
263 const struct stat *st,
268 CopyFlags copy_flags) {
275 r = mkfifoat(dt, to, st->st_mode & 07777);
280 uid_is_valid(override_uid) ? override_uid : st->st_uid,
281 gid_is_valid(override_gid) ? override_gid : st->st_gid,
282 AT_SYMLINK_NOFOLLOW) < 0)
285 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
291 static int fd_copy_node(
294 const struct stat *st,
299 CopyFlags copy_flags) {
306 r = mknodat(dt, to, st->st_mode, st->st_rdev);
311 uid_is_valid(override_uid) ? override_uid : st->st_uid,
312 gid_is_valid(override_gid) ? override_gid : st->st_gid,
313 AT_SYMLINK_NOFOLLOW) < 0)
316 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
322 static int fd_copy_directory(
325 const struct stat *st,
328 dev_t original_device,
331 CopyFlags copy_flags) {
333 _cleanup_close_ int fdf = -1, fdt = -1;
334 _cleanup_closedir_ DIR *d = NULL;
343 fdf = openat(df, from, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
345 fdf = fcntl(df, F_DUPFD_CLOEXEC, 3);
354 r = mkdirat(dt, to, st->st_mode & 07777);
357 else if (errno == EEXIST && (copy_flags & COPY_MERGE))
362 fdt = openat(dt, to, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
368 FOREACH_DIRENT_ALL(de, d, return -errno) {
372 if (dot_or_dot_dot(de->d_name))
375 if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
380 if (buf.st_dev != original_device)
383 if (S_ISREG(buf.st_mode))
384 q = fd_copy_regular(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
385 else if (S_ISDIR(buf.st_mode))
386 q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, override_uid, override_gid, copy_flags);
387 else if (S_ISLNK(buf.st_mode))
388 q = fd_copy_symlink(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
389 else if (S_ISFIFO(buf.st_mode))
390 q = fd_copy_fifo(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
391 else if (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode) || S_ISSOCK(buf.st_mode))
392 q = fd_copy_node(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
396 if (q == -EEXIST && (copy_flags & COPY_MERGE))
404 struct timespec ut[2] = {
410 uid_is_valid(override_uid) ? override_uid : st->st_uid,
411 gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0)
414 if (fchmod(fdt, st->st_mode & 07777) < 0)
417 (void) copy_xattr(dirfd(d), fdt);
418 (void) futimens(fdt, ut);
424 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) {
430 if (fstatat(fdf, from, &st, AT_SYMLINK_NOFOLLOW) < 0)
433 if (S_ISREG(st.st_mode))
434 return fd_copy_regular(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
435 else if (S_ISDIR(st.st_mode))
436 return fd_copy_directory(fdf, from, &st, fdt, to, st.st_dev, override_uid, override_gid, copy_flags);
437 else if (S_ISLNK(st.st_mode))
438 return fd_copy_symlink(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
439 else if (S_ISFIFO(st.st_mode))
440 return fd_copy_fifo(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
441 else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode) || S_ISSOCK(st.st_mode))
442 return fd_copy_node(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
447 int copy_tree(const char *from, const char *to, uid_t override_uid, gid_t override_gid, CopyFlags copy_flags) {
448 return copy_tree_at(AT_FDCWD, from, AT_FDCWD, to, override_uid, override_gid, copy_flags);
451 int copy_directory_fd(int dirfd, const char *to, CopyFlags copy_flags) {
457 if (fstat(dirfd, &st) < 0)
460 if (!S_ISDIR(st.st_mode))
463 return fd_copy_directory(dirfd, NULL, &st, AT_FDCWD, to, st.st_dev, UID_INVALID, GID_INVALID, copy_flags);
466 int copy_directory(const char *from, const char *to, CopyFlags copy_flags) {
472 if (lstat(from, &st) < 0)
475 if (!S_ISDIR(st.st_mode))
478 return fd_copy_directory(AT_FDCWD, from, &st, AT_FDCWD, to, st.st_dev, UID_INVALID, GID_INVALID, copy_flags);
481 int copy_file_fd(const char *from, int fdt, CopyFlags copy_flags) {
482 _cleanup_close_ int fdf = -1;
488 fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
492 r = copy_bytes(fdf, fdt, (uint64_t) -1, copy_flags);
494 (void) copy_times(fdf, fdt);
495 (void) copy_xattr(fdf, fdt);
500 int copy_file(const char *from, const char *to, int flags, mode_t mode, unsigned chattr_flags, CopyFlags copy_flags) {
506 RUN_WITH_UMASK(0000) {
507 fdt = open(to, flags|O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode);
512 if (chattr_flags != 0)
513 (void) chattr_fd(fdt, chattr_flags, (unsigned) -1);
515 r = copy_file_fd(from, fdt, copy_flags);
522 if (close(fdt) < 0) {
530 int copy_file_atomic(const char *from, const char *to, mode_t mode, unsigned chattr_flags, CopyFlags copy_flags) {
531 _cleanup_free_ char *t = NULL;
537 r = tempfn_random(to, NULL, &t);
541 r = copy_file(from, t, O_NOFOLLOW|O_EXCL, mode, chattr_flags, copy_flags);
545 if (copy_flags & COPY_REPLACE) {
546 r = renameat(AT_FDCWD, t, AT_FDCWD, to);
550 r = rename_noreplace(AT_FDCWD, t, AT_FDCWD, to);
559 int copy_times(int fdf, int fdt) {
560 struct timespec ut[2];
567 if (fstat(fdf, &st) < 0)
573 if (futimens(fdt, ut) < 0)
576 if (fd_getcrtime(fdf, &crtime) >= 0)
577 (void) fd_setcrtime(fdt, crtime);
582 int copy_xattr(int fdf, int fdt) {
583 _cleanup_free_ char *bufa = NULL, *bufb = NULL;
584 size_t sza = 100, szb = 100;
594 n = flistxattr(fdf, bufa, sza);
612 assert(l < (size_t) n);
614 if (startswith(p, "user.")) {
623 m = fgetxattr(fdf, p, bufb, szb);
625 if (errno == ERANGE) {
634 if (fsetxattr(fdt, p, bufb, m, 0) < 0)