2 This file is part of systemd.
4 Copyright 2014 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include <sys/sendfile.h>
28 //#include <sys/stat.h>
29 #include <sys/xattr.h>
33 //#include "alloc-util.h"
34 //#include "btrfs-util.h"
35 //#include "chattr-util.h"
37 //#include "dirent-util.h"
38 //#include "fd-util.h"
40 //#include "fs-util.h"
44 //#include "string-util.h"
46 #include "time-util.h"
47 //#include "umask-util.h"
48 #include "user-util.h"
49 //#include "xattr-util.h"
51 #define COPY_BUFFER_SIZE (16*1024u)
53 static ssize_t try_copy_file_range(int fd_in, loff_t *off_in,
54 int fd_out, loff_t *off_out,
63 r = copy_file_range(fd_in, off_in, fd_out, off_out, len, flags);
64 if (_unlikely_(have < 0))
65 have = r >= 0 || errno != ENOSYS;
72 int copy_bytes(int fdf, int fdt, uint64_t max_bytes, CopyFlags copy_flags) {
73 bool try_cfr = true, try_sendfile = true, try_splice = true;
75 size_t m = SSIZE_MAX; /* that is the maximum that sendfile and c_f_r accept */
80 #if 0 /// UNNEEDED by elogind
81 /* Try btrfs reflinks first. */
82 if ((copy_flags & COPY_REFLINK) &&
83 max_bytes == (uint64_t) -1 &&
84 lseek(fdf, 0, SEEK_CUR) == 0 &&
85 lseek(fdt, 0, SEEK_CUR) == 0) {
87 r = btrfs_reflink(fdf, fdt);
89 return 0; /* we copied the whole thing, hence hit EOF, return 0 */
96 if (max_bytes != (uint64_t) -1) {
98 return 1; /* return > 0 if we hit the max_bytes limit */
104 /* First try copy_file_range(), unless we already tried */
106 n = try_copy_file_range(fdf, NULL, fdt, NULL, m, 0u);
108 if (!IN_SET(n, -EINVAL, -ENOSYS, -EXDEV, -EBADF))
112 /* use fallback below */
113 } else if (n == 0) /* EOF */
120 /* First try sendfile(), unless we already tried */
122 n = sendfile(fdt, fdf, NULL, m);
124 if (!IN_SET(errno, EINVAL, ENOSYS))
127 try_sendfile = false;
128 /* use fallback below */
129 } else if (n == 0) /* EOF */
136 /* Then try splice, unless we already tried */
138 n = splice(fdf, NULL, fdt, NULL, m, 0);
140 if (!IN_SET(errno, EINVAL, ENOSYS))
144 /* use fallback below */
145 } else if (n == 0) /* EOF */
152 /* As a fallback just copy bits by hand */
154 uint8_t buf[MIN(m, COPY_BUFFER_SIZE)];
156 n = read(fdf, buf, sizeof buf);
159 if (n == 0) /* EOF */
162 r = loop_write(fdt, buf, (size_t) n, false);
168 if (max_bytes != (uint64_t) -1) {
169 assert(max_bytes >= (uint64_t) n);
172 /* sendfile accepts at most SSIZE_MAX-offset bytes to copy,
173 * so reduce our maximum by the amount we already copied,
174 * but don't go below our copy buffer size, unless we are
175 * close the limit of bytes we are allowed to copy. */
176 m = MAX(MIN(COPY_BUFFER_SIZE, max_bytes), m - n);
179 return 0; /* return 0 if we hit EOF earlier than the size limit */
182 #if 0 /// UNNEEDED by elogind
183 static int fd_copy_symlink(
186 const struct stat *st,
191 CopyFlags copy_flags) {
193 _cleanup_free_ char *target = NULL;
200 r = readlinkat_malloc(df, from, &target);
204 if (symlinkat(target, dt, to) < 0)
208 uid_is_valid(override_uid) ? override_uid : st->st_uid,
209 gid_is_valid(override_gid) ? override_gid : st->st_gid,
210 AT_SYMLINK_NOFOLLOW) < 0)
216 static int fd_copy_regular(
219 const struct stat *st,
224 CopyFlags copy_flags) {
226 _cleanup_close_ int fdf = -1, fdt = -1;
227 struct timespec ts[2];
234 fdf = openat(df, from, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
238 fdt = openat(dt, to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, st->st_mode & 07777);
242 r = copy_bytes(fdf, fdt, (uint64_t) -1, copy_flags);
249 uid_is_valid(override_uid) ? override_uid : st->st_uid,
250 gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0)
253 if (fchmod(fdt, st->st_mode & 07777) < 0)
258 (void) futimens(fdt, ts);
259 (void) copy_xattr(fdf, fdt);
272 static int fd_copy_fifo(
275 const struct stat *st,
280 CopyFlags copy_flags) {
287 r = mkfifoat(dt, to, st->st_mode & 07777);
292 uid_is_valid(override_uid) ? override_uid : st->st_uid,
293 gid_is_valid(override_gid) ? override_gid : st->st_gid,
294 AT_SYMLINK_NOFOLLOW) < 0)
297 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
303 static int fd_copy_node(
306 const struct stat *st,
311 CopyFlags copy_flags) {
318 r = mknodat(dt, to, st->st_mode, st->st_rdev);
323 uid_is_valid(override_uid) ? override_uid : st->st_uid,
324 gid_is_valid(override_gid) ? override_gid : st->st_gid,
325 AT_SYMLINK_NOFOLLOW) < 0)
328 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
334 static int fd_copy_directory(
337 const struct stat *st,
340 dev_t original_device,
343 CopyFlags copy_flags) {
345 _cleanup_close_ int fdf = -1, fdt = -1;
346 _cleanup_closedir_ DIR *d = NULL;
355 fdf = openat(df, from, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
357 fdf = fcntl(df, F_DUPFD_CLOEXEC, 3);
366 r = mkdirat(dt, to, st->st_mode & 07777);
369 else if (errno == EEXIST && (copy_flags & COPY_MERGE))
374 fdt = openat(dt, to, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
380 FOREACH_DIRENT_ALL(de, d, return -errno) {
384 if (dot_or_dot_dot(de->d_name))
387 if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
392 if (buf.st_dev != original_device)
395 if (S_ISREG(buf.st_mode))
396 q = fd_copy_regular(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
397 else if (S_ISDIR(buf.st_mode))
398 q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, override_uid, override_gid, copy_flags);
399 else if (S_ISLNK(buf.st_mode))
400 q = fd_copy_symlink(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
401 else if (S_ISFIFO(buf.st_mode))
402 q = fd_copy_fifo(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
403 else if (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode) || S_ISSOCK(buf.st_mode))
404 q = fd_copy_node(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
408 if (q == -EEXIST && (copy_flags & COPY_MERGE))
416 struct timespec ut[2] = {
422 uid_is_valid(override_uid) ? override_uid : st->st_uid,
423 gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0)
426 if (fchmod(fdt, st->st_mode & 07777) < 0)
429 (void) copy_xattr(dirfd(d), fdt);
430 (void) futimens(fdt, ut);
436 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) {
442 if (fstatat(fdf, from, &st, AT_SYMLINK_NOFOLLOW) < 0)
445 if (S_ISREG(st.st_mode))
446 return fd_copy_regular(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
447 else if (S_ISDIR(st.st_mode))
448 return fd_copy_directory(fdf, from, &st, fdt, to, st.st_dev, override_uid, override_gid, copy_flags);
449 else if (S_ISLNK(st.st_mode))
450 return fd_copy_symlink(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
451 else if (S_ISFIFO(st.st_mode))
452 return fd_copy_fifo(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
453 else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode) || S_ISSOCK(st.st_mode))
454 return fd_copy_node(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
459 int copy_tree(const char *from, const char *to, uid_t override_uid, gid_t override_gid, CopyFlags copy_flags) {
460 return copy_tree_at(AT_FDCWD, from, AT_FDCWD, to, override_uid, override_gid, copy_flags);
463 int copy_directory_fd(int dirfd, const char *to, CopyFlags copy_flags) {
469 if (fstat(dirfd, &st) < 0)
472 if (!S_ISDIR(st.st_mode))
475 return fd_copy_directory(dirfd, NULL, &st, AT_FDCWD, to, st.st_dev, UID_INVALID, GID_INVALID, copy_flags);
478 int copy_directory(const char *from, const char *to, CopyFlags copy_flags) {
484 if (lstat(from, &st) < 0)
487 if (!S_ISDIR(st.st_mode))
490 return fd_copy_directory(AT_FDCWD, from, &st, AT_FDCWD, to, st.st_dev, UID_INVALID, GID_INVALID, copy_flags);
493 int copy_file_fd(const char *from, int fdt, CopyFlags copy_flags) {
494 _cleanup_close_ int fdf = -1;
500 fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
504 r = copy_bytes(fdf, fdt, (uint64_t) -1, copy_flags);
506 (void) copy_times(fdf, fdt);
507 (void) copy_xattr(fdf, fdt);
512 int copy_file(const char *from, const char *to, int flags, mode_t mode, unsigned chattr_flags, CopyFlags copy_flags) {
518 RUN_WITH_UMASK(0000) {
519 fdt = open(to, flags|O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode);
524 if (chattr_flags != 0)
525 (void) chattr_fd(fdt, chattr_flags, (unsigned) -1);
527 r = copy_file_fd(from, fdt, copy_flags);
534 if (close(fdt) < 0) {
542 int copy_file_atomic(const char *from, const char *to, mode_t mode, unsigned chattr_flags, CopyFlags copy_flags) {
543 _cleanup_free_ char *t = NULL;
549 r = tempfn_random(to, NULL, &t);
553 r = copy_file(from, t, O_NOFOLLOW|O_EXCL, mode, chattr_flags, copy_flags);
557 if (copy_flags & COPY_REPLACE) {
558 r = renameat(AT_FDCWD, t, AT_FDCWD, to);
562 r = rename_noreplace(AT_FDCWD, t, AT_FDCWD, to);
571 int copy_times(int fdf, int fdt) {
572 struct timespec ut[2];
579 if (fstat(fdf, &st) < 0)
585 if (futimens(fdt, ut) < 0)
588 if (fd_getcrtime(fdf, &crtime) >= 0)
589 (void) fd_setcrtime(fdt, crtime);
594 int copy_xattr(int fdf, int fdt) {
595 _cleanup_free_ char *bufa = NULL, *bufb = NULL;
596 size_t sza = 100, szb = 100;
606 n = flistxattr(fdf, bufa, sza);
624 assert(l < (size_t) n);
626 if (startswith(p, "user.")) {
635 m = fgetxattr(fdf, p, bufb, szb);
637 if (errno == ERANGE) {
646 if (fsetxattr(fdt, p, bufb, m, 0) < 0)