1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/sendfile.h>
23 #include <sys/xattr.h>
25 //#include "alloc-util.h"
26 //#include "btrfs-util.h"
27 //#include "chattr-util.h"
29 //#include "dirent-util.h"
30 //#include "fd-util.h"
32 //#include "fs-util.h"
34 //#include "string-util.h"
36 //#include "umask-util.h"
38 //#include "xattr-util.h"
40 #define COPY_BUFFER_SIZE (16*1024)
42 int copy_bytes(int fdf, int fdt, uint64_t max_bytes, bool try_reflink) {
43 bool try_sendfile = true, try_splice = true;
48 #if 0 /// UNNEEDED by elogind
49 /* Try btrfs reflinks first. */
51 max_bytes == (uint64_t) -1 &&
52 lseek(fdf, 0, SEEK_CUR) == 0 &&
53 lseek(fdt, 0, SEEK_CUR) == 0) {
55 r = btrfs_reflink(fdf, fdt);
57 return 0; /* we copied the whole thing, hence hit EOF, return 0 */
61 size_t m = COPY_BUFFER_SIZE;
64 if (max_bytes != (uint64_t) -1) {
69 if ((uint64_t) m > max_bytes)
70 m = (size_t) max_bytes;
73 /* First try sendfile(), unless we already tried */
76 n = sendfile(fdt, fdf, NULL, m);
78 if (errno != EINVAL && errno != ENOSYS)
82 /* use fallback below */
83 } else if (n == 0) /* EOF */
90 /* The try splice, unless we already tried */
92 n = splice(fdf, NULL, fdt, NULL, m, 0);
94 if (errno != EINVAL && errno != ENOSYS)
98 /* use fallback below */
99 } else if (n == 0) /* EOF */
106 /* As a fallback just copy bits by hand */
110 n = read(fdf, buf, m);
113 if (n == 0) /* EOF */
116 r = loop_write(fdt, buf, (size_t) n, false);
122 if (max_bytes != (uint64_t) -1) {
123 assert(max_bytes >= (uint64_t) n);
128 return 0; /* return 0 if we hit EOF earlier than the size limit */
131 #if 0 /// UNNEEDED by elogind
132 static int fd_copy_symlink(int df, const char *from, const struct stat *st, int dt, const char *to) {
133 _cleanup_free_ char *target = NULL;
140 r = readlinkat_malloc(df, from, &target);
144 if (symlinkat(target, dt, to) < 0)
147 if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
153 static int fd_copy_regular(int df, const char *from, const struct stat *st, int dt, const char *to) {
154 _cleanup_close_ int fdf = -1, fdt = -1;
155 struct timespec ts[2];
162 fdf = openat(df, from, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
166 fdt = openat(dt, to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, st->st_mode & 07777);
170 r = copy_bytes(fdf, fdt, (uint64_t) -1, true);
176 if (fchown(fdt, st->st_uid, st->st_gid) < 0)
179 if (fchmod(fdt, st->st_mode & 07777) < 0)
184 (void) futimens(fdt, ts);
186 (void) copy_xattr(fdf, fdt);
199 static int fd_copy_fifo(int df, const char *from, const struct stat *st, int dt, const char *to) {
206 r = mkfifoat(dt, to, st->st_mode & 07777);
210 if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
213 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
219 static int fd_copy_node(int df, const char *from, const struct stat *st, int dt, const char *to) {
226 r = mknodat(dt, to, st->st_mode, st->st_rdev);
230 if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
233 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
239 static int fd_copy_directory(
242 const struct stat *st,
245 dev_t original_device,
248 _cleanup_close_ int fdf = -1, fdt = -1;
249 _cleanup_closedir_ DIR *d = NULL;
258 fdf = openat(df, from, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
260 fdf = fcntl(df, F_DUPFD_CLOEXEC, 3);
267 r = mkdirat(dt, to, st->st_mode & 07777);
270 else if (errno == EEXIST && merge)
275 fdt = openat(dt, to, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
282 struct timespec ut[2] = {
287 if (fchown(fdt, st->st_uid, st->st_gid) < 0)
290 if (fchmod(fdt, st->st_mode & 07777) < 0)
293 (void) futimens(fdt, ut);
294 (void) copy_xattr(dirfd(d), fdt);
297 FOREACH_DIRENT_ALL(de, d, return -errno) {
301 if (STR_IN_SET(de->d_name, ".", ".."))
304 if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
309 if (buf.st_dev != original_device)
312 if (S_ISREG(buf.st_mode))
313 q = fd_copy_regular(dirfd(d), de->d_name, &buf, fdt, de->d_name);
314 else if (S_ISDIR(buf.st_mode))
315 q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, merge);
316 else if (S_ISLNK(buf.st_mode))
317 q = fd_copy_symlink(dirfd(d), de->d_name, &buf, fdt, de->d_name);
318 else if (S_ISFIFO(buf.st_mode))
319 q = fd_copy_fifo(dirfd(d), de->d_name, &buf, fdt, de->d_name);
320 else if (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode))
321 q = fd_copy_node(dirfd(d), de->d_name, &buf, fdt, de->d_name);
325 if (q == -EEXIST && merge)
335 int copy_tree_at(int fdf, const char *from, int fdt, const char *to, bool merge) {
341 if (fstatat(fdf, from, &st, AT_SYMLINK_NOFOLLOW) < 0)
344 if (S_ISREG(st.st_mode))
345 return fd_copy_regular(fdf, from, &st, fdt, to);
346 else if (S_ISDIR(st.st_mode))
347 return fd_copy_directory(fdf, from, &st, fdt, to, st.st_dev, merge);
348 else if (S_ISLNK(st.st_mode))
349 return fd_copy_symlink(fdf, from, &st, fdt, to);
350 else if (S_ISFIFO(st.st_mode))
351 return fd_copy_fifo(fdf, from, &st, fdt, to);
352 else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode))
353 return fd_copy_node(fdf, from, &st, fdt, to);
358 int copy_tree(const char *from, const char *to, bool merge) {
359 return copy_tree_at(AT_FDCWD, from, AT_FDCWD, to, merge);
362 int copy_directory_fd(int dirfd, const char *to, bool merge) {
369 if (fstat(dirfd, &st) < 0)
372 if (!S_ISDIR(st.st_mode))
375 return fd_copy_directory(dirfd, NULL, &st, AT_FDCWD, to, st.st_dev, merge);
378 int copy_file_fd(const char *from, int fdt, bool try_reflink) {
379 _cleanup_close_ int fdf = -1;
385 fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
389 r = copy_bytes(fdf, fdt, (uint64_t) -1, try_reflink);
391 (void) copy_times(fdf, fdt);
392 (void) copy_xattr(fdf, fdt);
397 int copy_file(const char *from, const char *to, int flags, mode_t mode, unsigned chattr_flags) {
403 RUN_WITH_UMASK(0000) {
404 fdt = open(to, flags|O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode);
409 if (chattr_flags != 0)
410 (void) chattr_fd(fdt, chattr_flags, (unsigned) -1);
412 r = copy_file_fd(from, fdt, true);
419 if (close(fdt) < 0) {
427 int copy_file_atomic(const char *from, const char *to, mode_t mode, bool replace, unsigned chattr_flags) {
428 _cleanup_free_ char *t = NULL;
434 r = tempfn_random(to, NULL, &t);
438 r = copy_file(from, t, O_NOFOLLOW|O_EXCL, mode, chattr_flags);
443 r = renameat(AT_FDCWD, t, AT_FDCWD, to);
447 r = rename_noreplace(AT_FDCWD, t, AT_FDCWD, to);
449 (void) unlink_noerrno(t);
456 int copy_times(int fdf, int fdt) {
457 struct timespec ut[2];
464 if (fstat(fdf, &st) < 0)
470 if (futimens(fdt, ut) < 0)
473 if (fd_getcrtime(fdf, &crtime) >= 0)
474 (void) fd_setcrtime(fdt, crtime);
479 int copy_xattr(int fdf, int fdt) {
480 _cleanup_free_ char *bufa = NULL, *bufb = NULL;
481 size_t sza = 100, szb = 100;
491 n = flistxattr(fdf, bufa, sza);
509 assert(l < (size_t) n);
511 if (startswith(p, "user.")) {
520 m = fgetxattr(fdf, p, bufb, szb);
522 if (errno == ERANGE) {
531 if (fsetxattr(fdt, p, bufb, m, 0) < 0)