]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/copy.c
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>
26 #include "btrfs-util.h"
30 #define COPY_BUFFER_SIZE (16*1024)
32 int copy_bytes(int fdf
, int fdt
, off_t max_bytes
, bool try_reflink
) {
33 bool try_sendfile
= true;
39 /* Try btrfs reflinks first. */
40 if (try_reflink
&& max_bytes
== (off_t
) -1) {
41 r
= btrfs_reflink(fdf
, fdt
);
47 size_t m
= COPY_BUFFER_SIZE
;
50 if (max_bytes
!= (off_t
) -1) {
55 if ((off_t
) m
> max_bytes
)
56 m
= (size_t) max_bytes
;
59 /* First try sendfile(), unless we already tried */
62 n
= sendfile(fdt
, fdf
, NULL
, m
);
64 if (errno
!= EINVAL
&& errno
!= ENOSYS
)
68 /* use fallback below */
69 } else if (n
== 0) /* EOF */
76 /* As a fallback just copy bits by hand */
80 n
= read(fdf
, buf
, m
);
86 r
= loop_write(fdt
, buf
, (size_t) n
, false);
92 if (max_bytes
!= (off_t
) -1) {
93 assert(max_bytes
>= n
);
101 static int fd_copy_symlink(int df
, const char *from
, const struct stat
*st
, int dt
, const char *to
) {
102 _cleanup_free_
char *target
= NULL
;
109 r
= readlinkat_malloc(df
, from
, &target
);
113 if (symlinkat(target
, dt
, to
) < 0)
116 if (fchownat(dt
, to
, st
->st_uid
, st
->st_gid
, AT_SYMLINK_NOFOLLOW
) < 0)
122 static int fd_copy_regular(int df
, const char *from
, const struct stat
*st
, int dt
, const char *to
) {
123 _cleanup_close_
int fdf
= -1, fdt
= -1;
124 struct timespec ts
[2];
131 fdf
= openat(df
, from
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
135 fdt
= openat(dt
, to
, O_WRONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
, st
->st_mode
& 07777);
139 r
= copy_bytes(fdf
, fdt
, (off_t
) -1, true);
145 if (fchown(fdt
, st
->st_uid
, st
->st_gid
) < 0)
148 if (fchmod(fdt
, st
->st_mode
& 07777) < 0)
153 (void) futimens(fdt
, ts
);
155 (void) copy_xattr(fdf
, fdt
);
168 static int fd_copy_fifo(int df
, const char *from
, const struct stat
*st
, int dt
, const char *to
) {
175 r
= mkfifoat(dt
, to
, st
->st_mode
& 07777);
179 if (fchownat(dt
, to
, st
->st_uid
, st
->st_gid
, AT_SYMLINK_NOFOLLOW
) < 0)
182 if (fchmodat(dt
, to
, st
->st_mode
& 07777, 0) < 0)
188 static int fd_copy_node(int df
, const char *from
, const struct stat
*st
, int dt
, const char *to
) {
195 r
= mknodat(dt
, to
, st
->st_mode
, st
->st_rdev
);
199 if (fchownat(dt
, to
, st
->st_uid
, st
->st_gid
, AT_SYMLINK_NOFOLLOW
) < 0)
202 if (fchmodat(dt
, to
, st
->st_mode
& 07777, 0) < 0)
208 static int fd_copy_directory(
211 const struct stat
*st
,
214 dev_t original_device
,
217 _cleanup_close_
int fdf
= -1, fdt
= -1;
218 _cleanup_closedir_
DIR *d
= NULL
;
227 fdf
= openat(df
, from
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
229 fdf
= fcntl(df
, F_DUPFD_CLOEXEC
, 3);
236 r
= mkdirat(dt
, to
, st
->st_mode
& 07777);
239 else if (errno
== EEXIST
&& merge
)
244 fdt
= openat(dt
, to
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
251 struct timespec ut
[2] = {
256 if (fchown(fdt
, st
->st_uid
, st
->st_gid
) < 0)
259 if (fchmod(fdt
, st
->st_mode
& 07777) < 0)
262 (void) futimens(fdt
, ut
);
263 (void) copy_xattr(dirfd(d
), fdt
);
266 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
270 if (STR_IN_SET(de
->d_name
, ".", ".."))
273 if (fstatat(dirfd(d
), de
->d_name
, &buf
, AT_SYMLINK_NOFOLLOW
) < 0) {
278 if (buf
.st_dev
!= original_device
)
281 if (S_ISREG(buf
.st_mode
))
282 q
= fd_copy_regular(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
);
283 else if (S_ISDIR(buf
.st_mode
))
284 q
= fd_copy_directory(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
, original_device
, merge
);
285 else if (S_ISLNK(buf
.st_mode
))
286 q
= fd_copy_symlink(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
);
287 else if (S_ISFIFO(buf
.st_mode
))
288 q
= fd_copy_fifo(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
);
289 else if (S_ISBLK(buf
.st_mode
) || S_ISCHR(buf
.st_mode
))
290 q
= fd_copy_node(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
);
294 if (q
== -EEXIST
&& merge
)
304 int copy_tree_at(int fdf
, const char *from
, int fdt
, const char *to
, bool merge
) {
310 if (fstatat(fdf
, from
, &st
, AT_SYMLINK_NOFOLLOW
) < 0)
313 if (S_ISREG(st
.st_mode
))
314 return fd_copy_regular(fdf
, from
, &st
, fdt
, to
);
315 else if (S_ISDIR(st
.st_mode
))
316 return fd_copy_directory(fdf
, from
, &st
, fdt
, to
, st
.st_dev
, merge
);
317 else if (S_ISLNK(st
.st_mode
))
318 return fd_copy_symlink(fdf
, from
, &st
, fdt
, to
);
319 else if (S_ISFIFO(st
.st_mode
))
320 return fd_copy_fifo(fdf
, from
, &st
, fdt
, to
);
321 else if (S_ISBLK(st
.st_mode
) || S_ISCHR(st
.st_mode
))
322 return fd_copy_node(fdf
, from
, &st
, fdt
, to
);
327 int copy_tree(const char *from
, const char *to
, bool merge
) {
328 return copy_tree_at(AT_FDCWD
, from
, AT_FDCWD
, to
, merge
);
331 int copy_directory_fd(int dirfd
, const char *to
, bool merge
) {
338 if (fstat(dirfd
, &st
) < 0)
341 if (!S_ISDIR(st
.st_mode
))
344 return fd_copy_directory(dirfd
, NULL
, &st
, AT_FDCWD
, to
, st
.st_dev
, merge
);
347 int copy_file_fd(const char *from
, int fdt
, bool try_reflink
) {
348 _cleanup_close_
int fdf
= -1;
354 fdf
= open(from
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
358 r
= copy_bytes(fdf
, fdt
, (off_t
) -1, try_reflink
);
360 (void) copy_times(fdf
, fdt
);
361 (void) copy_xattr(fdf
, fdt
);
366 int copy_file(const char *from
, const char *to
, int flags
, mode_t mode
, unsigned chattr_flags
) {
372 RUN_WITH_UMASK(0000) {
373 fdt
= open(to
, flags
|O_WRONLY
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
, mode
);
378 if (chattr_flags
!= 0)
379 (void) chattr_fd(fdt
, chattr_flags
, (unsigned) -1);
381 r
= copy_file_fd(from
, fdt
, true);
388 if (close(fdt
) < 0) {
396 int copy_file_atomic(const char *from
, const char *to
, mode_t mode
, bool replace
, unsigned chattr_flags
) {
397 _cleanup_free_
char *t
= NULL
;
403 r
= tempfn_random(to
, NULL
, &t
);
407 r
= copy_file(from
, t
, O_NOFOLLOW
|O_EXCL
, mode
, chattr_flags
);
412 r
= renameat(AT_FDCWD
, t
, AT_FDCWD
, to
);
416 r
= rename_noreplace(AT_FDCWD
, t
, AT_FDCWD
, to
);
418 (void) unlink_noerrno(t
);
425 int copy_times(int fdf
, int fdt
) {
426 struct timespec ut
[2];
433 if (fstat(fdf
, &st
) < 0)
439 if (futimens(fdt
, ut
) < 0)
442 if (fd_getcrtime(fdf
, &crtime
) >= 0)
443 (void) fd_setcrtime(fdt
, crtime
);
448 int copy_xattr(int fdf
, int fdt
) {
449 _cleanup_free_
char *bufa
= NULL
, *bufb
= NULL
;
450 size_t sza
= 100, szb
= 100;
460 n
= flistxattr(fdf
, bufa
, sza
);
478 assert(l
< (size_t) n
);
480 if (startswith(p
, "user.")) {
489 m
= fgetxattr(fdf
, p
, bufb
, szb
);
491 if (errno
== ERANGE
) {
500 if (fsetxattr(fdt
, p
, bufb
, m
, 0) < 0)