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/>.
25 int copy_bytes(int fdf, int fdt, off_t max_bytes) {
36 if (max_bytes != (off_t) -1) {
41 if ((off_t) m > max_bytes)
42 m = (size_t) max_bytes;
45 n = read(fdf, buf, m);
52 k = loop_write(fdt, buf, n, false);
56 return errno ? -errno : -EIO;
58 if (max_bytes != (off_t) -1) {
59 assert(max_bytes >= n);
67 static int fd_copy_symlink(int df, const char *from, const struct stat *st, int dt, const char *to) {
68 _cleanup_free_ char *target = NULL;
75 r = readlinkat_malloc(df, from, &target);
79 if (symlinkat(target, dt, to) < 0)
82 if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
88 static int fd_copy_regular(int df, const char *from, const struct stat *st, int dt, const char *to) {
89 _cleanup_close_ int fdf = -1, fdt = -1;
96 fdf = openat(df, from, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
100 fdt = openat(dt, to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, st->st_mode & 07777);
104 r = copy_bytes(fdf, fdt, (off_t) -1);
110 if (fchown(fdt, st->st_uid, st->st_gid) < 0)
113 if (fchmod(fdt, st->st_mode & 07777) < 0)
127 static int fd_copy_fifo(int df, const char *from, const struct stat *st, int dt, const char *to) {
134 r = mkfifoat(dt, to, st->st_mode & 07777);
138 if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
141 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
147 static int fd_copy_node(int df, const char *from, const struct stat *st, int dt, const char *to) {
154 r = mknodat(dt, to, st->st_mode, st->st_rdev);
158 if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
161 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
167 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) {
168 _cleanup_close_ int fdf = -1, fdt = -1;
169 _cleanup_closedir_ DIR *d = NULL;
178 fdf = openat(df, from, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
187 r = mkdirat(dt, to, st->st_mode & 07777);
190 else if (errno == EEXIST && merge)
195 fdt = openat(dt, to, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
202 if (fchown(fdt, st->st_uid, st->st_gid) < 0)
205 if (fchmod(fdt, st->st_mode & 07777) < 0)
209 FOREACH_DIRENT(de, d, return -errno) {
213 if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
218 if (buf.st_dev != original_device)
221 if (S_ISREG(buf.st_mode))
222 q = fd_copy_regular(dirfd(d), de->d_name, &buf, fdt, de->d_name);
223 else if (S_ISDIR(buf.st_mode))
224 q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, merge);
225 else if (S_ISLNK(buf.st_mode))
226 q = fd_copy_symlink(dirfd(d), de->d_name, &buf, fdt, de->d_name);
227 else if (S_ISFIFO(buf.st_mode))
228 q = fd_copy_fifo(dirfd(d), de->d_name, &buf, fdt, de->d_name);
229 else if (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode))
230 q = fd_copy_node(dirfd(d), de->d_name, &buf, fdt, de->d_name);
234 if (q == -EEXIST && merge)
244 int copy_tree(const char *from, const char *to, bool merge) {
250 if (lstat(from, &st) < 0)
253 if (S_ISREG(st.st_mode))
254 return fd_copy_regular(AT_FDCWD, from, &st, AT_FDCWD, to);
255 else if (S_ISDIR(st.st_mode))
256 return fd_copy_directory(AT_FDCWD, from, &st, AT_FDCWD, to, st.st_dev, merge);
257 else if (S_ISLNK(st.st_mode))
258 return fd_copy_symlink(AT_FDCWD, from, &st, AT_FDCWD, to);
259 else if (S_ISFIFO(st.st_mode))
260 return fd_copy_fifo(AT_FDCWD, from, &st, AT_FDCWD, to);
261 else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode))
262 return fd_copy_node(AT_FDCWD, from, &st, AT_FDCWD, to);
267 int copy_file(const char *from, const char *to, int flags, mode_t mode) {
268 _cleanup_close_ int fdf = -1, fdt = -1;
274 fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
278 fdt = open(to, flags|O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode);
282 r = copy_bytes(fdf, fdt, (off_t) -1);