]>
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 "user-util.h"
49 #include "xattr-util.h"
51 #define COPY_BUFFER_SIZE (16*1024u)
53 static ssize_t
try_copy_file_range(int fd_in
, loff_t
*off_in
,
54 int fd_out
, loff_t
*off_out
,
63 r
= copy_file_range(fd_in
, off_in
, fd_out
, off_out
, len
, flags
);
64 if (_unlikely_(have
< 0))
65 have
= r
>= 0 || errno
!= ENOSYS
;
72 int copy_bytes(int fdf
, int fdt
, uint64_t max_bytes
, CopyFlags copy_flags
) {
73 bool try_cfr
= true, try_sendfile
= true, try_splice
= true;
75 size_t m
= SSIZE_MAX
; /* that is the maximum that sendfile and c_f_r accept */
80 /* Try btrfs reflinks first. */
81 if ((copy_flags
& COPY_REFLINK
) &&
82 max_bytes
== (uint64_t) -1 &&
83 lseek(fdf
, 0, SEEK_CUR
) == 0 &&
84 lseek(fdt
, 0, SEEK_CUR
) == 0) {
86 r
= btrfs_reflink(fdf
, fdt
);
88 return 0; /* we copied the whole thing, hence hit EOF, return 0 */
94 if (max_bytes
!= (uint64_t) -1) {
96 return 1; /* return > 0 if we hit the max_bytes limit */
102 /* First try copy_file_range(), unless we already tried */
104 n
= try_copy_file_range(fdf
, NULL
, fdt
, NULL
, m
, 0u);
106 if (!IN_SET(n
, -EINVAL
, -ENOSYS
, -EXDEV
, -EBADF
))
110 /* use fallback below */
111 } else if (n
== 0) /* EOF */
118 /* First try sendfile(), unless we already tried */
120 n
= sendfile(fdt
, fdf
, NULL
, m
);
122 if (!IN_SET(errno
, EINVAL
, ENOSYS
))
125 try_sendfile
= false;
126 /* use fallback below */
127 } else if (n
== 0) /* EOF */
134 /* Then try splice, unless we already tried */
136 n
= splice(fdf
, NULL
, fdt
, NULL
, m
, 0);
138 if (!IN_SET(errno
, EINVAL
, ENOSYS
))
142 /* use fallback below */
143 } else if (n
== 0) /* EOF */
150 /* As a fallback just copy bits by hand */
152 uint8_t buf
[MIN(m
, COPY_BUFFER_SIZE
)];
154 n
= read(fdf
, buf
, sizeof buf
);
157 if (n
== 0) /* EOF */
160 r
= loop_write(fdt
, buf
, (size_t) n
, false);
166 if (max_bytes
!= (uint64_t) -1) {
167 assert(max_bytes
>= (uint64_t) n
);
170 /* sendfile accepts at most SSIZE_MAX-offset bytes to copy,
171 * so reduce our maximum by the amount we already copied,
172 * but don't go below our copy buffer size, unless we are
173 * close the limit of bytes we are allowed to copy. */
174 m
= MAX(MIN(COPY_BUFFER_SIZE
, max_bytes
), m
- n
);
177 return 0; /* return 0 if we hit EOF earlier than the size limit */
180 static int fd_copy_symlink(
183 const struct stat
*st
,
188 CopyFlags copy_flags
) {
190 _cleanup_free_
char *target
= NULL
;
197 r
= readlinkat_malloc(df
, from
, &target
);
201 if (symlinkat(target
, dt
, to
) < 0)
205 uid_is_valid(override_uid
) ? override_uid
: st
->st_uid
,
206 gid_is_valid(override_gid
) ? override_gid
: st
->st_gid
,
207 AT_SYMLINK_NOFOLLOW
) < 0)
213 static int fd_copy_regular(
216 const struct stat
*st
,
221 CopyFlags copy_flags
) {
223 _cleanup_close_
int fdf
= -1, fdt
= -1;
224 struct timespec ts
[2];
231 fdf
= openat(df
, from
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
235 fdt
= openat(dt
, to
, O_WRONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
, st
->st_mode
& 07777);
239 r
= copy_bytes(fdf
, fdt
, (uint64_t) -1, copy_flags
);
246 uid_is_valid(override_uid
) ? override_uid
: st
->st_uid
,
247 gid_is_valid(override_gid
) ? override_gid
: st
->st_gid
) < 0)
250 if (fchmod(fdt
, st
->st_mode
& 07777) < 0)
255 (void) futimens(fdt
, ts
);
256 (void) copy_xattr(fdf
, fdt
);
269 static int fd_copy_fifo(
272 const struct stat
*st
,
277 CopyFlags copy_flags
) {
284 r
= mkfifoat(dt
, to
, st
->st_mode
& 07777);
289 uid_is_valid(override_uid
) ? override_uid
: st
->st_uid
,
290 gid_is_valid(override_gid
) ? override_gid
: st
->st_gid
,
291 AT_SYMLINK_NOFOLLOW
) < 0)
294 if (fchmodat(dt
, to
, st
->st_mode
& 07777, 0) < 0)
300 static int fd_copy_node(
303 const struct stat
*st
,
308 CopyFlags copy_flags
) {
315 r
= mknodat(dt
, to
, st
->st_mode
, st
->st_rdev
);
320 uid_is_valid(override_uid
) ? override_uid
: st
->st_uid
,
321 gid_is_valid(override_gid
) ? override_gid
: st
->st_gid
,
322 AT_SYMLINK_NOFOLLOW
) < 0)
325 if (fchmodat(dt
, to
, st
->st_mode
& 07777, 0) < 0)
331 static int fd_copy_directory(
334 const struct stat
*st
,
337 dev_t original_device
,
340 CopyFlags copy_flags
) {
342 _cleanup_close_
int fdf
= -1, fdt
= -1;
343 _cleanup_closedir_
DIR *d
= NULL
;
352 fdf
= openat(df
, from
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
354 fdf
= fcntl(df
, F_DUPFD_CLOEXEC
, 3);
363 r
= mkdirat(dt
, to
, st
->st_mode
& 07777);
366 else if (errno
== EEXIST
&& (copy_flags
& COPY_MERGE
))
371 fdt
= openat(dt
, to
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
377 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
381 if (dot_or_dot_dot(de
->d_name
))
384 if (fstatat(dirfd(d
), de
->d_name
, &buf
, AT_SYMLINK_NOFOLLOW
) < 0) {
389 if (buf
.st_dev
!= original_device
)
392 if (S_ISREG(buf
.st_mode
))
393 q
= fd_copy_regular(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
, override_uid
, override_gid
, copy_flags
);
394 else if (S_ISDIR(buf
.st_mode
))
395 q
= fd_copy_directory(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
, original_device
, override_uid
, override_gid
, copy_flags
);
396 else if (S_ISLNK(buf
.st_mode
))
397 q
= fd_copy_symlink(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
, override_uid
, override_gid
, copy_flags
);
398 else if (S_ISFIFO(buf
.st_mode
))
399 q
= fd_copy_fifo(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
, override_uid
, override_gid
, copy_flags
);
400 else if (S_ISBLK(buf
.st_mode
) || S_ISCHR(buf
.st_mode
) || S_ISSOCK(buf
.st_mode
))
401 q
= fd_copy_node(dirfd(d
), de
->d_name
, &buf
, fdt
, de
->d_name
, override_uid
, override_gid
, copy_flags
);
405 if (q
== -EEXIST
&& (copy_flags
& COPY_MERGE
))
413 struct timespec ut
[2] = {
419 uid_is_valid(override_uid
) ? override_uid
: st
->st_uid
,
420 gid_is_valid(override_gid
) ? override_gid
: st
->st_gid
) < 0)
423 if (fchmod(fdt
, st
->st_mode
& 07777) < 0)
426 (void) copy_xattr(dirfd(d
), fdt
);
427 (void) futimens(fdt
, ut
);
433 int copy_tree_at(int fdf
, const char *from
, int fdt
, const char *to
, uid_t override_uid
, gid_t override_gid
, CopyFlags copy_flags
) {
439 if (fstatat(fdf
, from
, &st
, AT_SYMLINK_NOFOLLOW
) < 0)
442 if (S_ISREG(st
.st_mode
))
443 return fd_copy_regular(fdf
, from
, &st
, fdt
, to
, override_uid
, override_gid
, copy_flags
);
444 else if (S_ISDIR(st
.st_mode
))
445 return fd_copy_directory(fdf
, from
, &st
, fdt
, to
, st
.st_dev
, override_uid
, override_gid
, copy_flags
);
446 else if (S_ISLNK(st
.st_mode
))
447 return fd_copy_symlink(fdf
, from
, &st
, fdt
, to
, override_uid
, override_gid
, copy_flags
);
448 else if (S_ISFIFO(st
.st_mode
))
449 return fd_copy_fifo(fdf
, from
, &st
, fdt
, to
, override_uid
, override_gid
, copy_flags
);
450 else if (S_ISBLK(st
.st_mode
) || S_ISCHR(st
.st_mode
) || S_ISSOCK(st
.st_mode
))
451 return fd_copy_node(fdf
, from
, &st
, fdt
, to
, override_uid
, override_gid
, copy_flags
);
456 int copy_tree(const char *from
, const char *to
, uid_t override_uid
, gid_t override_gid
, CopyFlags copy_flags
) {
457 return copy_tree_at(AT_FDCWD
, from
, AT_FDCWD
, to
, override_uid
, override_gid
, copy_flags
);
460 int copy_directory_fd(int dirfd
, const char *to
, CopyFlags copy_flags
) {
466 if (fstat(dirfd
, &st
) < 0)
469 if (!S_ISDIR(st
.st_mode
))
472 return fd_copy_directory(dirfd
, NULL
, &st
, AT_FDCWD
, to
, st
.st_dev
, UID_INVALID
, GID_INVALID
, copy_flags
);
475 int copy_directory(const char *from
, const char *to
, CopyFlags copy_flags
) {
481 if (lstat(from
, &st
) < 0)
484 if (!S_ISDIR(st
.st_mode
))
487 return fd_copy_directory(AT_FDCWD
, from
, &st
, AT_FDCWD
, to
, st
.st_dev
, UID_INVALID
, GID_INVALID
, copy_flags
);
490 int copy_file_fd(const char *from
, int fdt
, CopyFlags copy_flags
) {
491 _cleanup_close_
int fdf
= -1;
497 fdf
= open(from
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
501 r
= copy_bytes(fdf
, fdt
, (uint64_t) -1, copy_flags
);
503 (void) copy_times(fdf
, fdt
);
504 (void) copy_xattr(fdf
, fdt
);
509 int copy_file(const char *from
, const char *to
, int flags
, mode_t mode
, unsigned chattr_flags
, CopyFlags copy_flags
) {
515 RUN_WITH_UMASK(0000) {
516 fdt
= open(to
, flags
|O_WRONLY
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
, mode
);
521 if (chattr_flags
!= 0)
522 (void) chattr_fd(fdt
, chattr_flags
, (unsigned) -1);
524 r
= copy_file_fd(from
, fdt
, copy_flags
);
531 if (close(fdt
) < 0) {
539 int copy_file_atomic(const char *from
, const char *to
, mode_t mode
, unsigned chattr_flags
, CopyFlags copy_flags
) {
540 _cleanup_free_
char *t
= NULL
;
546 r
= tempfn_random(to
, NULL
, &t
);
550 r
= copy_file(from
, t
, O_NOFOLLOW
|O_EXCL
, mode
, chattr_flags
, copy_flags
);
554 if (copy_flags
& COPY_REPLACE
) {
555 r
= renameat(AT_FDCWD
, t
, AT_FDCWD
, to
);
559 r
= rename_noreplace(AT_FDCWD
, t
, AT_FDCWD
, to
);
568 int copy_times(int fdf
, int fdt
) {
569 struct timespec ut
[2];
576 if (fstat(fdf
, &st
) < 0)
582 if (futimens(fdt
, ut
) < 0)
585 if (fd_getcrtime(fdf
, &crtime
) >= 0)
586 (void) fd_setcrtime(fdt
, crtime
);
591 int copy_xattr(int fdf
, int fdt
) {
592 _cleanup_free_
char *bufa
= NULL
, *bufb
= NULL
;
593 size_t sza
= 100, szb
= 100;
603 n
= flistxattr(fdf
, bufa
, sza
);
621 assert(l
< (size_t) n
);
623 if (startswith(p
, "user.")) {
632 m
= fgetxattr(fdf
, p
, bufb
, szb
);
634 if (errno
== ERANGE
) {
643 if (fsetxattr(fdt
, p
, bufb
, m
, 0) < 0)