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>
25 #include "btrfs-util.h"
28 int copy_bytes(int fdf, int fdt, off_t max_bytes) {
29 bool try_sendfile = true;
38 if (max_bytes != (off_t) -1) {
43 if ((off_t) m > max_bytes)
44 m = (size_t) max_bytes;
47 /* First try sendfile(), unless we already tried */
50 n = sendfile(fdt, fdf, NULL, m);
52 if (errno != EINVAL && errno != ENOSYS)
56 /* use fallback below */
57 } else if (n == 0) /* EOF */
64 /* As a fallback just copy bits by hand */
69 n = read(fdf, buf, m);
75 r = loop_write(fdt, buf, n, false);
82 if (max_bytes != (off_t) -1) {
83 assert(max_bytes >= n);
91 static int fd_copy_symlink(int df, const char *from, const struct stat *st, int dt, const char *to) {
92 _cleanup_free_ char *target = NULL;
99 r = readlinkat_malloc(df, from, &target);
103 if (symlinkat(target, dt, to) < 0)
106 if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
112 static int fd_copy_regular(int df, const char *from, const struct stat *st, int dt, const char *to) {
113 _cleanup_close_ int fdf = -1, fdt = -1;
120 fdf = openat(df, from, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
124 fdt = openat(dt, to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, st->st_mode & 07777);
128 r = copy_bytes(fdf, fdt, (off_t) -1);
134 if (fchown(fdt, st->st_uid, st->st_gid) < 0)
137 if (fchmod(fdt, st->st_mode & 07777) < 0)
151 static int fd_copy_fifo(int df, const char *from, const struct stat *st, int dt, const char *to) {
158 r = mkfifoat(dt, to, st->st_mode & 07777);
162 if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
165 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
171 static int fd_copy_node(int df, const char *from, const struct stat *st, int dt, const char *to) {
178 r = mknodat(dt, to, st->st_mode, st->st_rdev);
182 if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
185 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
191 static int fd_copy_directory(
194 const struct stat *st,
197 dev_t original_device,
200 _cleanup_close_ int fdf = -1, fdt = -1;
201 _cleanup_closedir_ DIR *d = NULL;
210 fdf = openat(df, from, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
212 fdf = fcntl(df, F_DUPFD_CLOEXEC, 3);
219 r = mkdirat(dt, to, st->st_mode & 07777);
222 else if (errno == EEXIST && merge)
227 fdt = openat(dt, to, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
234 if (fchown(fdt, st->st_uid, st->st_gid) < 0)
237 if (fchmod(fdt, st->st_mode & 07777) < 0)
241 FOREACH_DIRENT(de, d, return -errno) {
245 if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
250 if (buf.st_dev != original_device)
253 if (S_ISREG(buf.st_mode))
254 q = fd_copy_regular(dirfd(d), de->d_name, &buf, fdt, de->d_name);
255 else if (S_ISDIR(buf.st_mode))
256 q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, merge);
257 else if (S_ISLNK(buf.st_mode))
258 q = fd_copy_symlink(dirfd(d), de->d_name, &buf, fdt, de->d_name);
259 else if (S_ISFIFO(buf.st_mode))
260 q = fd_copy_fifo(dirfd(d), de->d_name, &buf, fdt, de->d_name);
261 else if (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode))
262 q = fd_copy_node(dirfd(d), de->d_name, &buf, fdt, de->d_name);
266 if (q == -EEXIST && merge)
276 int copy_tree(const char *from, const char *to, bool merge) {
282 if (lstat(from, &st) < 0)
285 if (S_ISREG(st.st_mode))
286 return fd_copy_regular(AT_FDCWD, from, &st, AT_FDCWD, to);
287 else if (S_ISDIR(st.st_mode))
288 return fd_copy_directory(AT_FDCWD, from, &st, AT_FDCWD, to, st.st_dev, merge);
289 else if (S_ISLNK(st.st_mode))
290 return fd_copy_symlink(AT_FDCWD, from, &st, AT_FDCWD, to);
291 else if (S_ISFIFO(st.st_mode))
292 return fd_copy_fifo(AT_FDCWD, from, &st, AT_FDCWD, to);
293 else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode))
294 return fd_copy_node(AT_FDCWD, from, &st, AT_FDCWD, to);
299 int copy_tree_fd(int dirfd, const char *to, bool merge) {
306 if (fstat(dirfd, &st) < 0)
309 if (!S_ISDIR(st.st_mode))
312 return fd_copy_directory(dirfd, NULL, &st, AT_FDCWD, to, st.st_dev, merge);
315 int copy_file_fd(const char *from, int fdt) {
316 _cleanup_close_ int fdf = -1;
321 fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
325 return copy_bytes(fdf, fdt, (off_t) -1);
328 int copy_file(const char *from, const char *to, int flags, mode_t mode) {
334 fdt = open(to, flags|O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode);
338 r = copy_file_fd(from, fdt);
345 if (close(fdt) < 0) {