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>
27 int copy_bytes(int fdf, int fdt, off_t max_bytes) {
28 bool try_sendfile = true;
37 if (max_bytes != (off_t) -1) {
42 if ((off_t) m > max_bytes)
43 m = (size_t) max_bytes;
46 /* First try sendfile(), unless we already tried */
49 n = sendfile(fdt, fdf, NULL, m);
51 if (errno != EINVAL && errno != ENOSYS)
55 /* use fallback below */
56 } else if (n == 0) /* EOF */
63 /* As a fallback just copy bits by hand */
68 n = read(fdf, buf, m);
75 k = loop_write(fdt, buf, n, false);
79 return errno ? -errno : -EIO;
84 if (max_bytes != (off_t) -1) {
85 assert(max_bytes >= n);
93 static int fd_copy_symlink(int df, const char *from, const struct stat *st, int dt, const char *to) {
94 _cleanup_free_ char *target = NULL;
101 r = readlinkat_malloc(df, from, &target);
105 if (symlinkat(target, dt, to) < 0)
108 if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
114 static int fd_copy_regular(int df, const char *from, const struct stat *st, int dt, const char *to) {
115 _cleanup_close_ int fdf = -1, fdt = -1;
122 fdf = openat(df, from, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
126 fdt = openat(dt, to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, st->st_mode & 07777);
130 r = copy_bytes(fdf, fdt, (off_t) -1);
136 if (fchown(fdt, st->st_uid, st->st_gid) < 0)
139 if (fchmod(fdt, st->st_mode & 07777) < 0)
153 static int fd_copy_fifo(int df, const char *from, const struct stat *st, int dt, const char *to) {
160 r = mkfifoat(dt, to, st->st_mode & 07777);
164 if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
167 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
173 static int fd_copy_node(int df, const char *from, const struct stat *st, int dt, const char *to) {
180 r = mknodat(dt, to, st->st_mode, st->st_rdev);
184 if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
187 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
193 static int fd_copy_directory(int df, const char *from, const struct stat *st, int dt, const char *to, dev_t original_device, bool merge) {
194 _cleanup_close_ int fdf = -1, fdt = -1;
195 _cleanup_closedir_ DIR *d = NULL;
204 fdf = openat(df, from, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
213 r = mkdirat(dt, to, st->st_mode & 07777);
216 else if (errno == EEXIST && merge)
221 fdt = openat(dt, to, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
228 if (fchown(fdt, st->st_uid, st->st_gid) < 0)
231 if (fchmod(fdt, st->st_mode & 07777) < 0)
235 FOREACH_DIRENT(de, d, return -errno) {
239 if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
244 if (buf.st_dev != original_device)
247 if (S_ISREG(buf.st_mode))
248 q = fd_copy_regular(dirfd(d), de->d_name, &buf, fdt, de->d_name);
249 else if (S_ISDIR(buf.st_mode))
250 q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, merge);
251 else if (S_ISLNK(buf.st_mode))
252 q = fd_copy_symlink(dirfd(d), de->d_name, &buf, fdt, de->d_name);
253 else if (S_ISFIFO(buf.st_mode))
254 q = fd_copy_fifo(dirfd(d), de->d_name, &buf, fdt, de->d_name);
255 else if (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode))
256 q = fd_copy_node(dirfd(d), de->d_name, &buf, fdt, de->d_name);
260 if (q == -EEXIST && merge)
270 int copy_tree(const char *from, const char *to, bool merge) {
276 if (lstat(from, &st) < 0)
279 if (S_ISREG(st.st_mode))
280 return fd_copy_regular(AT_FDCWD, from, &st, AT_FDCWD, to);
281 else if (S_ISDIR(st.st_mode))
282 return fd_copy_directory(AT_FDCWD, from, &st, AT_FDCWD, to, st.st_dev, merge);
283 else if (S_ISLNK(st.st_mode))
284 return fd_copy_symlink(AT_FDCWD, from, &st, AT_FDCWD, to);
285 else if (S_ISFIFO(st.st_mode))
286 return fd_copy_fifo(AT_FDCWD, from, &st, AT_FDCWD, to);
287 else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode))
288 return fd_copy_node(AT_FDCWD, from, &st, AT_FDCWD, to);
293 int copy_file_fd(const char *from, int fdt) {
294 _cleanup_close_ int fdf = -1;
299 fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
303 return copy_bytes(fdf, fdt, (off_t) -1);
306 int copy_file(const char *from, const char *to, int flags, mode_t mode) {
312 fdt = open(to, flags|O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode);
316 r = copy_file_fd(from, fdt);
323 if (close(fdt) < 0) {