]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/copy.c
2 This file is part of systemd.
4 Copyright 2014 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include <sys/sendfile.h>
29 #include <sys/xattr.h>
33 #include "alloc-util.h"
34 #include "btrfs-util.h"
35 #include "chattr-util.h"
37 #include "dirent-util.h"
44 #include "string-util.h"
46 #include "time-util.h"
47 #include "umask-util.h"
48 #include "xattr-util.h"
50 #define COPY_BUFFER_SIZE (16*1024u)
52 static ssize_t
try_copy_file_range(int fd_in
, loff_t
*off_in
,
53 int fd_out
, loff_t
*off_out
,
62 r
= copy_file_range(fd_in
, off_in
, fd_out
, off_out
, len
, flags
);
63 if (_unlikely_(have
< 0))
64 have
= r
>= 0 || errno
!= ENOSYS
;
71 int copy_bytes(int fdf
, int fdt
, uint64_t max_bytes
, bool try_reflink
) {
72 bool try_cfr
= true, try_sendfile
= true, try_splice
= true;
74 size_t m
= SSIZE_MAX
; /* that is the maximum that sendfile and c_f_r accept */
79 /* Try btrfs reflinks first. */
81 max_bytes
== (uint64_t) -1 &&
82 lseek(fdf
, 0, SEEK_CUR
) == 0 &&
83 lseek(fdt
, 0, SEEK_CUR
) == 0) {
85 r
= btrfs_reflink(fdf
, fdt
);
87 return 0; /* we copied the whole thing, hence hit EOF, return 0 */
93 if (max_bytes
!= (uint64_t) -1) {
95 return 1; /* return > 0 if we hit the max_bytes limit */
101 /* First try copy_file_range(), unless we already tried */
103 n
= try_copy_file_range(fdf
, NULL
, fdt
, NULL
, m
, 0u);
105 if (!IN_SET(n
, -EINVAL
, -ENOSYS
, -EXDEV
, -EBADF
))
109 /* use fallback below */
110 } else if (n
== 0) /* EOF */
117 /* First try sendfile(), unless we already tried */
119 n
= sendfile(fdt
, fdf
, NULL
, m
);
121 if (!IN_SET(errno
, EINVAL
, ENOSYS
))
124 try_sendfile
= false;
125 /* use fallback below */
126 } else if (n
== 0) /* EOF */
133 /* Then try splice, unless we already tried */
135 n
= splice(fdf
, NULL
, fdt
, NULL
, m
, 0);
137 if (!IN_SET(errno
, EINVAL
, ENOSYS
))
141 /* use fallback below */
142 } else if (n
== 0) /* EOF */
149 /* As a fallback just copy bits by hand */
151 uint8_t buf
[MIN(m
, COPY_BUFFER_SIZE
)];
153 n
= read(fdf
, buf
, sizeof buf
);
156 if (n
== 0) /* EOF */
159 r
= loop_write(fdt
, buf
, (size_t) n
, false);
165 if (max_bytes
!= (uint64_t) -1) {
166 assert(max_bytes
>= (uint64_t) n
);
169 /* sendfile accepts at most SSIZE_MAX-offset bytes to copy,
170 * so reduce our maximum by the amount we already copied,
171 * but don't go below our copy buffer size, unless we are
172 * close the limit of bytes we are allowed to copy. */
173 m
= MAX(MIN(COPY_BUFFER_SIZE
, max_bytes
), m
- n
);
176 return 0; /* return 0 if we hit EOF earlier than the size limit */
179 static int fd_copy_symlink(int df
, const char *from
, const struct stat
*st
, int dt
, const char *to
) {
180 _cleanup_free_
char *target
= NULL
;
187 r
= readlinkat_malloc(df
, from
, &target
);
191 if (symlinkat(target
, dt
, to
) < 0)
194 if (fchownat(dt
, to
, st
->st_uid
, st
->st_gid
, AT_SYMLINK_NOFOLLOW
) < 0)
200 static int fd_copy_regular(int df
, const char *from
, const struct stat
*st
, int dt
, const char *to
) {
201 _cleanup_close_
int fdf
= -1, fdt
= -1;
202 struct timespec ts
[2];
209 fdf
= openat(df
, from
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
213 fdt
= openat(dt
, to
, O_WRONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
, st
->st_mode
& 07777);
217 r
= copy_bytes(fdf
, fdt
, (uint64_t) -1, true);
223 if (fchown(fdt
, st
->st_uid
, st
->st_gid
) < 0)
226 if (fchmod(fdt
, st
->st_mode
& 07777) < 0)
231 (void) futimens(fdt
, ts
);
233 (void) copy_xattr(fdf
, fdt
);
246 static int fd_copy_fifo(int df
, const char *from
, const struct stat
*st
, int dt
, const char *to
) {
253 r
= mkfifoat(dt
, to
, st
->st_mode
& 07777);
257 if (fchownat(dt
, to
, st
->st_uid
, st
->st_gid
, AT_SYMLINK_NOFOLLOW
) < 0)
260 if (fchmodat(dt
, to
, st
->st_mode
& 07777, 0) < 0)
266 static int fd_copy_node(int df
, const char *from
, const struct stat
*st
, int dt
, const char *to
) {
273 r
= mknodat(dt
, to
, st
->st_mode
, st
->st_rdev
);
277 if (fchownat(dt
, to
, st
->st_uid
, st
->st_gid
, AT_SYMLINK_NOFOLLOW
) < 0)
280 if (fchmodat(dt
, to
, st
->st_mode
& 07777, 0) < 0)
286 static int fd_copy_directory(
289 const struct stat
*st
,
292 dev_t original_device
,
295 _cleanup_close_
int fdf
= -1, fdt
= -1;
296 _cleanup_closedir_
DIR *d
= NULL
;
305 fdf
= openat(df
, from
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
307 fdf
= fcntl(df
, F_DUPFD_CLOEXEC
, 3);
316 r
= mkdirat(dt
, to
, st
->st_mode
& 07777);
319 else if (errno
== EEXIST
&& merge
)
324 fdt
= openat(dt
, to
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
330 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
334 if (dot_or_dot_dot(de
->d_name
))
337 if (fstatat(dirfd(d
), de
->d_name
, &buf
, AT_SYMLINK_NOFOLLOW
) < 0) {
342 if (buf
.st_dev
!= original_device
)
345 if (S_ISREG(buf
.st_mode
))
346 q
= fd_copy_regular(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
);
347 else if (S_ISDIR(buf
.st_mode
))
348 q
= fd_copy_directory(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
, original_device
, merge
);
349 else if (S_ISLNK(buf
.st_mode
))
350 q
= fd_copy_symlink(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
);
351 else if (S_ISFIFO(buf
.st_mode
))
352 q
= fd_copy_fifo(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
);
353 else if (S_ISBLK(buf
.st_mode
) || S_ISCHR(buf
.st_mode
) || S_ISSOCK(buf
.st_mode
))
354 q
= fd_copy_node(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
);
358 if (q
== -EEXIST
&& merge
)
366 struct timespec ut
[2] = {
371 if (fchown(fdt
, st
->st_uid
, st
->st_gid
) < 0)
374 if (fchmod(fdt
, st
->st_mode
& 07777) < 0)
377 (void) copy_xattr(dirfd(d
), fdt
);
378 (void) futimens(fdt
, ut
);
384 int copy_tree_at(int fdf
, const char *from
, int fdt
, const char *to
, bool merge
) {
390 if (fstatat(fdf
, from
, &st
, AT_SYMLINK_NOFOLLOW
) < 0)
393 if (S_ISREG(st
.st_mode
))
394 return fd_copy_regular(fdf
, from
, &st
, fdt
, to
);
395 else if (S_ISDIR(st
.st_mode
))
396 return fd_copy_directory(fdf
, from
, &st
, fdt
, to
, st
.st_dev
, merge
);
397 else if (S_ISLNK(st
.st_mode
))
398 return fd_copy_symlink(fdf
, from
, &st
, fdt
, to
);
399 else if (S_ISFIFO(st
.st_mode
))
400 return fd_copy_fifo(fdf
, from
, &st
, fdt
, to
);
401 else if (S_ISBLK(st
.st_mode
) || S_ISCHR(st
.st_mode
) || S_ISSOCK(st
.st_mode
))
402 return fd_copy_node(fdf
, from
, &st
, fdt
, to
);
407 int copy_tree(const char *from
, const char *to
, bool merge
) {
408 return copy_tree_at(AT_FDCWD
, from
, AT_FDCWD
, to
, merge
);
411 int copy_directory_fd(int dirfd
, const char *to
, bool merge
) {
417 if (fstat(dirfd
, &st
) < 0)
420 if (!S_ISDIR(st
.st_mode
))
423 return fd_copy_directory(dirfd
, NULL
, &st
, AT_FDCWD
, to
, st
.st_dev
, merge
);
426 int copy_directory(const char *from
, const char *to
, bool merge
) {
432 if (lstat(from
, &st
) < 0)
435 if (!S_ISDIR(st
.st_mode
))
438 return fd_copy_directory(AT_FDCWD
, from
, &st
, AT_FDCWD
, to
, st
.st_dev
, merge
);
441 int copy_file_fd(const char *from
, int fdt
, bool try_reflink
) {
442 _cleanup_close_
int fdf
= -1;
448 fdf
= open(from
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
452 r
= copy_bytes(fdf
, fdt
, (uint64_t) -1, try_reflink
);
454 (void) copy_times(fdf
, fdt
);
455 (void) copy_xattr(fdf
, fdt
);
460 int copy_file(const char *from
, const char *to
, int flags
, mode_t mode
, unsigned chattr_flags
) {
466 RUN_WITH_UMASK(0000) {
467 fdt
= open(to
, flags
|O_WRONLY
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
, mode
);
472 if (chattr_flags
!= 0)
473 (void) chattr_fd(fdt
, chattr_flags
, (unsigned) -1);
475 r
= copy_file_fd(from
, fdt
, true);
482 if (close(fdt
) < 0) {
490 int copy_file_atomic(const char *from
, const char *to
, mode_t mode
, bool replace
, unsigned chattr_flags
) {
491 _cleanup_free_
char *t
= NULL
;
497 r
= tempfn_random(to
, NULL
, &t
);
501 r
= copy_file(from
, t
, O_NOFOLLOW
|O_EXCL
, mode
, chattr_flags
);
506 r
= renameat(AT_FDCWD
, t
, AT_FDCWD
, to
);
510 r
= rename_noreplace(AT_FDCWD
, t
, AT_FDCWD
, to
);
512 (void) unlink_noerrno(t
);
519 int copy_times(int fdf
, int fdt
) {
520 struct timespec ut
[2];
527 if (fstat(fdf
, &st
) < 0)
533 if (futimens(fdt
, ut
) < 0)
536 if (fd_getcrtime(fdf
, &crtime
) >= 0)
537 (void) fd_setcrtime(fdt
, crtime
);
542 int copy_xattr(int fdf
, int fdt
) {
543 _cleanup_free_
char *bufa
= NULL
, *bufb
= NULL
;
544 size_t sza
= 100, szb
= 100;
554 n
= flistxattr(fdf
, bufa
, sza
);
572 assert(l
< (size_t) n
);
574 if (startswith(p
, "user.")) {
583 m
= fgetxattr(fdf
, p
, bufb
, szb
);
585 if (errno
== ERANGE
) {
594 if (fsetxattr(fdt
, p
, bufb
, m
, 0) < 0)