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 #define COPY_BUFFER_SIZE (16*1024)
30 int copy_bytes(int fdf, int fdt, off_t max_bytes, bool try_reflink) {
31 bool try_sendfile = true;
37 /* Try btrfs reflinks first. */
38 if (try_reflink && max_bytes == (off_t) -1) {
39 r = btrfs_reflink(fdf, fdt);
45 size_t m = COPY_BUFFER_SIZE;
48 if (max_bytes != (off_t) -1) {
53 if ((off_t) m > max_bytes)
54 m = (size_t) max_bytes;
57 /* First try sendfile(), unless we already tried */
60 n = sendfile(fdt, fdf, NULL, m);
62 if (errno != EINVAL && errno != ENOSYS)
66 /* use fallback below */
67 } else if (n == 0) /* EOF */
74 /* As a fallback just copy bits by hand */
78 n = read(fdf, buf, m);
84 r = loop_write(fdt, buf, (size_t) n, false);
90 if (max_bytes != (off_t) -1) {
91 assert(max_bytes >= n);
99 static int fd_copy_symlink(int df, const char *from, const struct stat *st, int dt, const char *to) {
100 _cleanup_free_ char *target = NULL;
107 r = readlinkat_malloc(df, from, &target);
111 if (symlinkat(target, dt, to) < 0)
114 if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
120 static int fd_copy_regular(int df, const char *from, const struct stat *st, int dt, const char *to) {
121 _cleanup_close_ int fdf = -1, fdt = -1;
128 fdf = openat(df, from, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
132 fdt = openat(dt, to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, st->st_mode & 07777);
136 r = copy_bytes(fdf, fdt, (off_t) -1, true);
142 if (fchown(fdt, st->st_uid, st->st_gid) < 0)
145 if (fchmod(fdt, st->st_mode & 07777) < 0)
159 static int fd_copy_fifo(int df, const char *from, const struct stat *st, int dt, const char *to) {
166 r = mkfifoat(dt, to, st->st_mode & 07777);
170 if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
173 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
179 static int fd_copy_node(int df, const char *from, const struct stat *st, int dt, const char *to) {
186 r = mknodat(dt, to, st->st_mode, st->st_rdev);
190 if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
193 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
199 static int fd_copy_directory(
202 const struct stat *st,
205 dev_t original_device,
208 _cleanup_close_ int fdf = -1, fdt = -1;
209 _cleanup_closedir_ DIR *d = NULL;
218 fdf = openat(df, from, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
220 fdf = fcntl(df, F_DUPFD_CLOEXEC, 3);
227 r = mkdirat(dt, to, st->st_mode & 07777);
230 else if (errno == EEXIST && merge)
235 fdt = openat(dt, to, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
242 if (fchown(fdt, st->st_uid, st->st_gid) < 0)
245 if (fchmod(fdt, st->st_mode & 07777) < 0)
249 FOREACH_DIRENT(de, d, return -errno) {
253 if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
258 if (buf.st_dev != original_device)
261 if (S_ISREG(buf.st_mode))
262 q = fd_copy_regular(dirfd(d), de->d_name, &buf, fdt, de->d_name);
263 else if (S_ISDIR(buf.st_mode))
264 q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, merge);
265 else if (S_ISLNK(buf.st_mode))
266 q = fd_copy_symlink(dirfd(d), de->d_name, &buf, fdt, de->d_name);
267 else if (S_ISFIFO(buf.st_mode))
268 q = fd_copy_fifo(dirfd(d), de->d_name, &buf, fdt, de->d_name);
269 else if (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode))
270 q = fd_copy_node(dirfd(d), de->d_name, &buf, fdt, de->d_name);
274 if (q == -EEXIST && merge)
284 int copy_tree_at(int fdf, const char *from, int fdt, const char *to, bool merge) {
290 if (fstatat(fdf, from, &st, AT_SYMLINK_NOFOLLOW) < 0)
293 if (S_ISREG(st.st_mode))
294 return fd_copy_regular(fdf, from, &st, fdt, to);
295 else if (S_ISDIR(st.st_mode))
296 return fd_copy_directory(fdf, from, &st, fdt, to, st.st_dev, merge);
297 else if (S_ISLNK(st.st_mode))
298 return fd_copy_symlink(fdf, from, &st, fdt, to);
299 else if (S_ISFIFO(st.st_mode))
300 return fd_copy_fifo(fdf, from, &st, fdt, to);
301 else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode))
302 return fd_copy_node(fdf, from, &st, fdt, to);
307 int copy_tree(const char *from, const char *to, bool merge) {
308 return copy_tree_at(AT_FDCWD, from, AT_FDCWD, to, merge);
311 int copy_directory_fd(int dirfd, const char *to, bool merge) {
318 if (fstat(dirfd, &st) < 0)
321 if (!S_ISDIR(st.st_mode))
324 return fd_copy_directory(dirfd, NULL, &st, AT_FDCWD, to, st.st_dev, merge);
327 int copy_file_fd(const char *from, int fdt, bool try_reflink) {
328 _cleanup_close_ int fdf = -1;
333 fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
337 return copy_bytes(fdf, fdt, (off_t) -1, try_reflink);
340 int copy_file(const char *from, const char *to, int flags, mode_t mode) {
346 fdt = open(to, flags|O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode);
350 r = copy_file_fd(from, fdt, true);
357 if (close(fdt) < 0) {