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"
43 //#include "string-util.h"
45 #include "time-util.h"
46 //#include "umask-util.h"
47 //#include "xattr-util.h"
49 #define COPY_BUFFER_SIZE (16*1024)
51 int copy_bytes(int fdf, int fdt, uint64_t max_bytes, bool try_reflink) {
52 bool try_sendfile = true, try_splice = true;
57 #if 0 /// UNNEEDED by elogind
58 /* Try btrfs reflinks first. */
60 max_bytes == (uint64_t) -1 &&
61 lseek(fdf, 0, SEEK_CUR) == 0 &&
62 lseek(fdt, 0, SEEK_CUR) == 0) {
64 r = btrfs_reflink(fdf, fdt);
66 return 0; /* we copied the whole thing, hence hit EOF, return 0 */
70 size_t m = COPY_BUFFER_SIZE;
73 if (max_bytes != (uint64_t) -1) {
76 return 1; /* return > 0 if we hit the max_bytes limit */
78 if ((uint64_t) m > max_bytes)
79 m = (size_t) max_bytes;
82 /* First try sendfile(), unless we already tried */
85 n = sendfile(fdt, fdf, NULL, m);
87 if (errno != EINVAL && errno != ENOSYS)
91 /* use fallback below */
92 } else if (n == 0) /* EOF */
99 /* The try splice, unless we already tried */
101 n = splice(fdf, NULL, fdt, NULL, m, 0);
103 if (errno != EINVAL && errno != ENOSYS)
107 /* use fallback below */
108 } else if (n == 0) /* EOF */
115 /* As a fallback just copy bits by hand */
119 n = read(fdf, buf, m);
122 if (n == 0) /* EOF */
125 r = loop_write(fdt, buf, (size_t) n, false);
131 if (max_bytes != (uint64_t) -1) {
132 assert(max_bytes >= (uint64_t) n);
137 return 0; /* return 0 if we hit EOF earlier than the size limit */
140 #if 0 /// UNNEEDED by elogind
141 static int fd_copy_symlink(int df, const char *from, const struct stat *st, int dt, const char *to) {
142 _cleanup_free_ char *target = NULL;
149 r = readlinkat_malloc(df, from, &target);
153 if (symlinkat(target, dt, to) < 0)
156 if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
162 static int fd_copy_regular(int df, const char *from, const struct stat *st, int dt, const char *to) {
163 _cleanup_close_ int fdf = -1, fdt = -1;
164 struct timespec ts[2];
171 fdf = openat(df, from, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
175 fdt = openat(dt, to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, st->st_mode & 07777);
179 r = copy_bytes(fdf, fdt, (uint64_t) -1, true);
185 if (fchown(fdt, st->st_uid, st->st_gid) < 0)
188 if (fchmod(fdt, st->st_mode & 07777) < 0)
193 (void) futimens(fdt, ts);
195 (void) copy_xattr(fdf, fdt);
208 static int fd_copy_fifo(int df, const char *from, const struct stat *st, int dt, const char *to) {
215 r = mkfifoat(dt, to, st->st_mode & 07777);
219 if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
222 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
228 static int fd_copy_node(int df, const char *from, const struct stat *st, int dt, const char *to) {
235 r = mknodat(dt, to, st->st_mode, st->st_rdev);
239 if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
242 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
248 static int fd_copy_directory(
251 const struct stat *st,
254 dev_t original_device,
257 _cleanup_close_ int fdf = -1, fdt = -1;
258 _cleanup_closedir_ DIR *d = NULL;
267 fdf = openat(df, from, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
269 fdf = fcntl(df, F_DUPFD_CLOEXEC, 3);
276 r = mkdirat(dt, to, st->st_mode & 07777);
279 else if (errno == EEXIST && merge)
284 fdt = openat(dt, to, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
291 struct timespec ut[2] = {
296 if (fchown(fdt, st->st_uid, st->st_gid) < 0)
299 if (fchmod(fdt, st->st_mode & 07777) < 0)
302 (void) futimens(fdt, ut);
303 (void) copy_xattr(dirfd(d), fdt);
306 FOREACH_DIRENT_ALL(de, d, return -errno) {
310 if (STR_IN_SET(de->d_name, ".", ".."))
313 if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
318 if (buf.st_dev != original_device)
321 if (S_ISREG(buf.st_mode))
322 q = fd_copy_regular(dirfd(d), de->d_name, &buf, fdt, de->d_name);
323 else if (S_ISDIR(buf.st_mode))
324 q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, merge);
325 else if (S_ISLNK(buf.st_mode))
326 q = fd_copy_symlink(dirfd(d), de->d_name, &buf, fdt, de->d_name);
327 else if (S_ISFIFO(buf.st_mode))
328 q = fd_copy_fifo(dirfd(d), de->d_name, &buf, fdt, de->d_name);
329 else if (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode))
330 q = fd_copy_node(dirfd(d), de->d_name, &buf, fdt, de->d_name);
334 if (q == -EEXIST && merge)
344 int copy_tree_at(int fdf, const char *from, int fdt, const char *to, bool merge) {
350 if (fstatat(fdf, from, &st, AT_SYMLINK_NOFOLLOW) < 0)
353 if (S_ISREG(st.st_mode))
354 return fd_copy_regular(fdf, from, &st, fdt, to);
355 else if (S_ISDIR(st.st_mode))
356 return fd_copy_directory(fdf, from, &st, fdt, to, st.st_dev, merge);
357 else if (S_ISLNK(st.st_mode))
358 return fd_copy_symlink(fdf, from, &st, fdt, to);
359 else if (S_ISFIFO(st.st_mode))
360 return fd_copy_fifo(fdf, from, &st, fdt, to);
361 else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode))
362 return fd_copy_node(fdf, from, &st, fdt, to);
367 int copy_tree(const char *from, const char *to, bool merge) {
368 return copy_tree_at(AT_FDCWD, from, AT_FDCWD, to, merge);
371 int copy_directory_fd(int dirfd, const char *to, bool merge) {
378 if (fstat(dirfd, &st) < 0)
381 if (!S_ISDIR(st.st_mode))
384 return fd_copy_directory(dirfd, NULL, &st, AT_FDCWD, to, st.st_dev, merge);
387 int copy_file_fd(const char *from, int fdt, bool try_reflink) {
388 _cleanup_close_ int fdf = -1;
394 fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
398 r = copy_bytes(fdf, fdt, (uint64_t) -1, try_reflink);
400 (void) copy_times(fdf, fdt);
401 (void) copy_xattr(fdf, fdt);
406 int copy_file(const char *from, const char *to, int flags, mode_t mode, unsigned chattr_flags) {
412 RUN_WITH_UMASK(0000) {
413 fdt = open(to, flags|O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode);
418 if (chattr_flags != 0)
419 (void) chattr_fd(fdt, chattr_flags, (unsigned) -1);
421 r = copy_file_fd(from, fdt, true);
428 if (close(fdt) < 0) {
436 int copy_file_atomic(const char *from, const char *to, mode_t mode, bool replace, unsigned chattr_flags) {
437 _cleanup_free_ char *t = NULL;
443 r = tempfn_random(to, NULL, &t);
447 r = copy_file(from, t, O_NOFOLLOW|O_EXCL, mode, chattr_flags);
452 r = renameat(AT_FDCWD, t, AT_FDCWD, to);
456 r = rename_noreplace(AT_FDCWD, t, AT_FDCWD, to);
458 (void) unlink_noerrno(t);
465 int copy_times(int fdf, int fdt) {
466 struct timespec ut[2];
473 if (fstat(fdf, &st) < 0)
479 if (futimens(fdt, ut) < 0)
482 if (fd_getcrtime(fdf, &crtime) >= 0)
483 (void) fd_setcrtime(fdt, crtime);
488 int copy_xattr(int fdf, int fdt) {
489 _cleanup_free_ char *bufa = NULL, *bufb = NULL;
490 size_t sza = 100, szb = 100;
500 n = flistxattr(fdf, bufa, sza);
518 assert(l < (size_t) n);
520 if (startswith(p, "user.")) {
529 m = fgetxattr(fdf, p, bufb, szb);
531 if (errno == ERANGE) {
540 if (fsetxattr(fdt, p, bufb, m, 0) < 0)