HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/shared/copy.c
Git blob: 51dbf080ad63edd1efcb9689115bcb62f2e24468
Lines 1001–1834 of 1,834
static int fd_copy_node(
int df,
const char *from,
const struct stat *st,
int dt,
const char *to,
uid_t override_uid,
gid_t override_gid,
CopyFlags copy_flags,
usec_t ts_clamp,
HardlinkContext *hardlink_context) {
int r;
assert(st);
assert(to);
r = try_hardlink(hardlink_context, st, dt, to);
if (r < 0)
return r;
if (r > 0) /* worked! */
return 0;
if (copy_flags & COPY_MAC_CREATE) {
r = mac_selinux_create_file_prepare_at(dt, to, st->st_mode & S_IFMT, /* label_context= */ NULL);
if (r < 0)
return r;
}
r = RET_NERRNO(mknodat(dt, to, st->st_mode, st->st_rdev));
if (copy_flags & COPY_MAC_CREATE)
mac_selinux_create_file_clear();
if (FLAGS_SET(copy_flags, COPY_GRACEFUL_WARN) && (ERRNO_IS_NEG_PRIVILEGE(r) || ERRNO_IS_NEG_NOT_SUPPORTED(r))) {
log_notice_errno(r, "Failed to copy node%s%s%s, ignoring: %m",
isempty(from) ? "" : " '",
strempty(from),
isempty(from) ? "" : "'");
return 0;
} else if (r < 0)
return r;
if (fchownat(dt, to,
uid_is_valid(override_uid) ? override_uid : st->st_uid,
gid_is_valid(override_gid) ? override_gid : st->st_gid,
AT_SYMLINK_NOFOLLOW) < 0)
r = -errno;
if (fchmodat(dt, to, st->st_mode & 07777, AT_SYMLINK_NOFOLLOW) < 0)
r = -errno;
(void) clamp_utimensat(dt, to, st, ts_clamp);
(void) memorize_hardlink(hardlink_context, st, dt, to);
return r;
}
static int fd_copy_directory(
int df,
const char *from,
const struct stat *st,
int dt,
const char *to,
dev_t original_device,
unsigned depth_left,
uid_t override_uid,
gid_t override_gid,
CopyFlags copy_flags,
usec_t ts_clamp,
Hashmap *denylist,
Hashmap *subvolumes,
HardlinkContext *hardlink_context,
const char *display_path,
copy_progress_path_t progress_path,
copy_progress_bytes_t progress_bytes,
void *userdata) {
_cleanup_(hardlink_context_destroy) HardlinkContext our_hardlink_context = {
.dir_fd = -EBADF,
.parent_fd = -EBADF,
};
_cleanup_free_ DirectoryEntries *des = NULL;
_cleanup_close_ int fdf = -EBADF, fdt = -EBADF;
_cleanup_closedir_ DIR *d = NULL;
struct stat dt_st;
bool exists;
int r;
assert(st);
if (depth_left == 0)
return -ENAMETOOLONG;
fdf = xopenat(df, from, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
if (fdf < 0)
return fdf;
if (!hardlink_context) {
/* If recreating hardlinks is requested let's set up a context for that now. */
r = hardlink_context_setup(&our_hardlink_context, dt, to, copy_flags);
if (r < 0)
return r;
if (r > 0) /* It's enabled and allocated, let's now use the same context for all recursive
* invocations from here down */
hardlink_context = &our_hardlink_context;
}
d = take_fdopendir(&fdf);
if (!d)
return -errno;
r = dir_is_empty_at(dt, to, /* ignore_hidden_or_backup= */ false);
if (r < 0 && r != -ENOENT)
return r;
if ((r > 0 && !(copy_flags & (COPY_MERGE|COPY_MERGE_EMPTY))) || (r == 0 && !FLAGS_SET(copy_flags, COPY_MERGE)))
return -EEXIST;
exists = r >= 0;
XOpenFlags flags = copy_flags & COPY_MAC_CREATE ? XO_LABEL : 0;
if (hashmap_contains(subvolumes, st)) {
flags |= XO_SUBVOLUME;
if ((PTR_TO_INT(hashmap_get(subvolumes, st)) & BTRFS_SUBVOL_NODATACOW))
flags |= XO_NOCOW;
}
fdt = xopenat_lock_full(dt, to,
O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW|(exists ? 0 : O_CREAT|O_EXCL),
flags,
st->st_mode & 07777,
copy_flags & COPY_LOCK_BSD ? LOCK_BSD : LOCK_NONE,
LOCK_EX);
if (fdt < 0)
return fdt;
if (exists && FLAGS_SET(copy_flags, COPY_RESTORE_DIRECTORY_TIMESTAMPS) && fstat(fdt, &dt_st) < 0)
return -errno;
int ret = 0;
if (PTR_TO_INT(hashmap_get(denylist, st)) == DENY_CONTENTS) {
log_debug("%s is in the denylist, not recursing", from ?: "file to copy");
goto finish;
}
/* Walk children in deterministic (alphabetical) order. The natural readdir() order depends on the
* source filesystem's directory storage (e.g. ext4 dir hash) and varies across hosts, which leaks
* into the destination when it records entries in insertion order (e.g. vfat). Sorting here keeps
* copy_tree() reproducible regardless of the source filesystem layout. */
r = readdir_all(dirfd(d), RECURSE_DIR_SORT, &des);
if (r < 0)
return r;
FOREACH_ARRAY(i, des->entries, des->n_entries) {
struct dirent *de = *i;
const char *child_display_path = NULL;
_cleanup_free_ char *dp = NULL;
struct stat buf;
r = look_for_signals(copy_flags);
if (r < 0)
return r;
if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
RET_GATHER(ret, -errno);
continue;
}
if (progress_path) {
if (display_path)
child_display_path = dp = path_join(display_path, de->d_name);
else
child_display_path = de->d_name;
r = progress_path(child_display_path, &buf, userdata);
if (r < 0)
return r;
}
if (PTR_TO_INT(hashmap_get(denylist, &buf)) == DENY_INODE) {
log_debug("%s%s%s is in the denylist, ignoring",
strempty(from), isempty(from) ? "" : "/", de->d_name);
continue;
}
if (S_ISDIR(buf.st_mode)) {
/*
* Don't descend into directories on other file systems, if this is requested. We do a simple
* .st_dev check here, which basically comes for free. Note that we do this check only on
* directories, not other kind of file system objects, for two reason:
*
* • The kernel's overlayfs pseudo file system that overlays multiple real file systems
* propagates the .st_dev field of the file system a file originates from all the way up
* through the stack to stat(). It doesn't do that for directories however. This means that
* comparing .st_dev on non-directories suggests that they all are mount points. To avoid
* confusion we hence avoid relying on this check for regular files.
*
* • The main reason we do this check at all is to protect ourselves from bind mount cycles,
* where we really want to avoid descending down in all eternity. However the .st_dev check
* is usually not sufficient for this protection anyway, as bind mount cycles from the same
* file system onto itself can't be detected that way. (Note we also do a recursion depth
* check, which is probably the better protection in this regard, which is why
* COPY_SAME_MOUNT is optional).
*/
if (FLAGS_SET(copy_flags, COPY_SAME_MOUNT)) {
if (buf.st_dev != original_device)
continue;
r = is_mount_point_at(dirfd(d), de->d_name, 0);
if (r < 0)
return r;
if (r > 0)
continue;
}
}
r = fd_copy_tree_generic(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device,
depth_left-1, override_uid, override_gid, copy_flags & ~COPY_LOCK_BSD,
ts_clamp, denylist, subvolumes, hardlink_context, child_display_path, progress_path,
progress_bytes, userdata);
/* Propagate SIGINT/SIGTERM, ENOSPC, and fs-verity fails up instantly */
if (IN_SET(r, -EINTR, -ENOSPC, -ESOCKTNOSUPPORT))
return r;
if (r == -EEXIST && (copy_flags & COPY_MERGE))
r = 0;
RET_GATHER(ret, r);
}
finish:
if (FLAGS_SET(copy_flags, COPY_MERGE_APPLY_STAT) || !exists) {
if (fchown(fdt,
uid_is_valid(override_uid) ? override_uid : st->st_uid,
gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0)
RET_GATHER(ret, -errno);
if (fchmod(fdt, st->st_mode & 07777) < 0)
RET_GATHER(ret, -errno);
/* Run hardlink context cleanup now because it potentially changes timestamps */
hardlink_context_destroy(&our_hardlink_context);
(void) copy_xattr(dirfd(d), NULL, fdt, NULL, copy_flags);
(void) clamp_futimens(fdt, st, ts_clamp);
} else if (FLAGS_SET(copy_flags, COPY_RESTORE_DIRECTORY_TIMESTAMPS)) {
/* Run hardlink context cleanup now because it potentially changes timestamps */
hardlink_context_destroy(&our_hardlink_context);
/* If the directory already exists, make sure the timestamps stay the same as before. */
(void) futimens(fdt, (struct timespec[]) { dt_st.st_atim, dt_st.st_mtim });
}
if (copy_flags & COPY_FSYNC_FULL) {
if (fsync(fdt) < 0)
return -errno;
}
if (ret < 0)
return ret;
return copy_flags & COPY_LOCK_BSD ? TAKE_FD(fdt) : 0;
}
static int fd_copy_leaf(
int df,
const char *from,
const struct stat *st,
int dt,
const char *to,
uid_t override_uid,
gid_t override_gid,
CopyFlags copy_flags,
usec_t ts_clamp,
HardlinkContext *hardlink_context,
const char *display_path,
copy_progress_bytes_t progress_bytes,
void *userdata) {
int r;
if (S_ISREG(st->st_mode))
r = fd_copy_regular(df, from, st, dt, to, override_uid, override_gid, copy_flags, ts_clamp, hardlink_context, progress_bytes, userdata);
else if (S_ISLNK(st->st_mode))
r = fd_copy_symlink(df, from, st, dt, to, override_uid, override_gid, copy_flags, ts_clamp);
else if (S_ISFIFO(st->st_mode))
r = fd_copy_fifo(df, from, st, dt, to, override_uid, override_gid, copy_flags, ts_clamp, hardlink_context);
else if (S_ISBLK(st->st_mode) || S_ISCHR(st->st_mode) || S_ISSOCK(st->st_mode))
r = fd_copy_node(df, from, st, dt, to, override_uid, override_gid, copy_flags, ts_clamp, hardlink_context);
else
r = -EOPNOTSUPP;
return r;
}
static int fd_copy_tree_generic(
int df,
const char *from,
const struct stat *st,
int dt,
const char *to,
dev_t original_device,
unsigned depth_left,
uid_t override_uid,
gid_t override_gid,
CopyFlags copy_flags,
usec_t ts_clamp,
Hashmap *denylist,
Hashmap *subvolumes,
HardlinkContext *hardlink_context,
const char *display_path,
copy_progress_path_t progress_path,
copy_progress_bytes_t progress_bytes,
void *userdata) {
int r;
assert(!FLAGS_SET(copy_flags, COPY_LOCK_BSD));
if (S_ISDIR(st->st_mode))
return fd_copy_directory(df, from, st, dt, to, original_device, depth_left-1, override_uid,
override_gid, copy_flags, ts_clamp, denylist, subvolumes, hardlink_context,
display_path, progress_path, progress_bytes, userdata);
/* Only if we are copying a directory we are fine if the target dir is referenced by fd only */
if (!to)
return -ENOTDIR;
DenyType t = PTR_TO_INT(hashmap_get(denylist, st));
if (t == DENY_INODE) {
log_debug("%s is in the denylist, ignoring", from ?: "file to copy");
return 0;
} else if (t == DENY_CONTENTS)
log_debug("%s is configured to have its contents excluded, but is not a directory", from ?: "file to copy");
r = fd_copy_leaf(df, from, st, dt, to, override_uid, override_gid, copy_flags, ts_clamp, hardlink_context, display_path, progress_bytes, userdata);
/* We just tried to copy a leaf node of the tree. If it failed because the node already exists *and* the COPY_REPLACE flag has been provided, we should unlink the node and re-copy. */
if (r == -EEXIST && (copy_flags & COPY_REPLACE)) {
/* This codepath is us trying to address an error to copy, if the unlink fails, lets just return the original error. */
if (unlinkat(dt, to, 0) < 0)
return r;
r = fd_copy_leaf(df, from, st, dt, to, override_uid, override_gid, copy_flags, ts_clamp, hardlink_context, display_path, progress_bytes, userdata);
}
return r;
}
int copy_tree_at_full(
int fdf,
const char *from,
int fdt,
const char *to,
uid_t override_uid,
gid_t override_gid,
CopyFlags copy_flags,
usec_t ts_clamp,
Hashmap *denylist,
Hashmap *subvolumes,
copy_progress_path_t progress_path,
copy_progress_bytes_t progress_bytes,
void *userdata) {
struct stat st;
int r;
assert(!FLAGS_SET(copy_flags, COPY_LOCK_BSD));
if (fstatat(fdf, strempty(from), &st, AT_SYMLINK_NOFOLLOW | (isempty(from) ? AT_EMPTY_PATH : 0)) < 0)
return -errno;
r = fd_copy_tree_generic(fdf, from, &st, fdt, to, st.st_dev, COPY_DEPTH_MAX, override_uid,
override_gid, copy_flags, ts_clamp, denylist, subvolumes, NULL, NULL, progress_path,
progress_bytes, userdata);
if (r < 0)
return r;
if (S_ISDIR(st.st_mode) && (copy_flags & COPY_SYNCFS)) {
/* If the top-level inode is a directory run syncfs() now. */
r = syncfs_path(fdt, to);
if (r < 0)
return r;
} else if ((copy_flags & (COPY_FSYNC_FULL|COPY_SYNCFS)) != 0) {
/* fsync() the parent dir of what we just copied if COPY_FSYNC_FULL is set. Also do this in
* case COPY_SYNCFS is set but the top-level inode wasn't actually a directory. We do this so that
* COPY_SYNCFS provides reasonable synchronization semantics on any kind of inode: when the
* copy operation is done the whole inode — regardless of its type — and all its children
* will be synchronized to disk. */
r = fsync_parent_at(fdt, to);
if (r < 0)
return r;
}
return 0;
}
static int sync_dir_by_flags(int dir_fd, const char *path, CopyFlags copy_flags) {
assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
assert(path);
if (copy_flags & COPY_SYNCFS)
return syncfs_path(dir_fd, path);
if (copy_flags & COPY_FSYNC_FULL)
return fsync_parent_at(dir_fd, path);
return 0;
}
int copy_directory_at_full(
int dir_fdf,
const char *from,
int dir_fdt,
const char *to,
uid_t override_uid,
gid_t override_gid,
CopyFlags copy_flags,
copy_progress_path_t progress_path,
copy_progress_bytes_t progress_bytes,
void *userdata) {
_cleanup_close_ int fdt = -EBADF;
struct stat st;
int r;
assert(dir_fdf >= 0 || dir_fdf == AT_FDCWD);
assert(dir_fdt >= 0 || dir_fdt == AT_FDCWD);
assert(to);
if (fstatat(dir_fdf, strempty(from), &st, AT_SYMLINK_NOFOLLOW|(isempty(from) ? AT_EMPTY_PATH : 0)) < 0)
return -errno;
r = stat_verify_directory(&st);
if (r < 0)
return r;
r = fd_copy_directory(
dir_fdf, from,
&st,
dir_fdt, to,
st.st_dev,
COPY_DEPTH_MAX,
override_uid,
override_gid,
copy_flags,
/* ts_clamp= */ USEC_INFINITY,
/* denylist= */ NULL,
/* subvolumes= */ NULL,
/* hardlink_context= */ NULL,
/* display_path= */ NULL,
progress_path,
progress_bytes,
userdata);
if (r < 0)
return r;
if (FLAGS_SET(copy_flags, COPY_LOCK_BSD))
fdt = r;
r = sync_dir_by_flags(dir_fdt, to, copy_flags);
if (r < 0)
return r;
return FLAGS_SET(copy_flags, COPY_LOCK_BSD) ? TAKE_FD(fdt) : 0;
}
int copy_file_fd_at_full(
int dir_fdf,
const char *from,
int fdt,
CopyFlags copy_flags,
copy_progress_bytes_t progress_bytes,
void *userdata) {
_cleanup_close_ int fdf = -EBADF;
struct stat st;
int r;
assert(dir_fdf >= 0 || dir_fdf == AT_FDCWD);
assert(fdt >= 0);
assert(!FLAGS_SET(copy_flags, COPY_LOCK_BSD));
fdf = xopenat_full(dir_fdf, from, O_RDONLY|O_CLOEXEC|O_NOCTTY, XO_REGULAR, 0);
if (fdf < 0)
return fdf;
if (fstat(fdt, &st) < 0)
return -errno;
r = copy_bytes_full(fdf, fdt, UINT64_MAX, copy_flags, NULL, NULL, progress_bytes, userdata);
if (r < 0)
return r;
/* Make sure to copy file attributes only over if target is a regular
* file (so that copying a file to /dev/null won't alter the access
* mode/ownership of that device node...) */
if (S_ISREG(st.st_mode)) {
(void) copy_times(fdf, fdt, copy_flags);
(void) copy_xattr(fdf, NULL, fdt, NULL, copy_flags);
}
if (FLAGS_SET(copy_flags, COPY_VERIFY_LINKED)) {
r = fd_verify_linked(fdf);
if (r < 0)
return r;
}
if (copy_flags & COPY_FSYNC_FULL) {
r = fsync_full(fdt);
if (r < 0)
return r;
} else if (copy_flags & COPY_FSYNC) {
if (fsync(fdt) < 0)
return -errno;
}
return 0;
}
int copy_file_at_full(
int dir_fdf,
const char *from,
int dir_fdt,
const char *to,
int flags,
mode_t mode,
unsigned chattr_flags,
unsigned chattr_mask,
CopyFlags copy_flags,
copy_progress_bytes_t progress_bytes,
void *userdata) {
_cleanup_close_ int fdf = -EBADF, fdt = -EBADF;
int r;
assert(dir_fdf >= 0 || dir_fdf == AT_FDCWD);
assert(dir_fdt >= 0 || dir_fdt == AT_FDCWD);
assert(to);
fdf = xopenat_full(dir_fdf, from, O_RDONLY|O_CLOEXEC|O_NOCTTY, XO_REGULAR, 0);
if (fdf < 0)
return fdf;
if (mode == MODE_INVALID) {
struct stat st;
if (fstat(fdf, &st) < 0)
return -errno;
mode = st.st_mode;
}
WITH_UMASK(0000) {
fdt = xopenat_lock_full(dir_fdt, to,
flags|O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY,
XO_REGULAR | (copy_flags & COPY_MAC_CREATE ? XO_LABEL : 0),
mode,
copy_flags & COPY_LOCK_BSD ? LOCK_BSD : LOCK_NONE, LOCK_EX);
if (fdt < 0)
return fdt;
}
r = prepare_nocow(fdf, /* from= */ NULL, fdt, &chattr_mask, &chattr_flags);
if (r < 0)
return r;
if ((chattr_mask & CHATTR_EARLY_FL) != 0)
(void) chattr_fd(fdt, chattr_flags, chattr_mask & CHATTR_EARLY_FL);
r = copy_bytes_full(fdf, fdt, UINT64_MAX, copy_flags & ~COPY_LOCK_BSD, NULL, NULL, progress_bytes, userdata);
if (r < 0)
goto fail;
(void) copy_times(fdf, fdt, copy_flags);
(void) copy_xattr(fdf, NULL, fdt, NULL, copy_flags);
if (FLAGS_SET(copy_flags, COPY_VERIFY_LINKED)) {
r = fd_verify_linked(fdf);
if (r < 0)
goto fail;
}
unsigned nocow = FLAGS_SET(copy_flags, COPY_NOCOW_AFTER) ? FS_NOCOW_FL : 0;
if (((chattr_mask & ~CHATTR_EARLY_FL) | nocow) != 0)
(void) chattr_fd(fdt, chattr_flags | nocow, (chattr_mask & ~CHATTR_EARLY_FL) | nocow);
if (copy_flags & (COPY_FSYNC|COPY_FSYNC_FULL)) {
if (fsync(fdt) < 0) {
r = -errno;
goto fail;
}
}
if (!FLAGS_SET(copy_flags, COPY_LOCK_BSD)) {
r = close_nointr(TAKE_FD(fdt)); /* even if this fails, the fd is now invalidated */
if (r < 0)
goto fail;
}
if (copy_flags & COPY_FSYNC_FULL) {
r = fsync_parent_at(dir_fdt, to);
if (r < 0)
goto fail;
}
return copy_flags & COPY_LOCK_BSD ? TAKE_FD(fdt) : 0;
fail:
/* Only unlink if we definitely are the ones who created the file */
if (FLAGS_SET(flags, O_EXCL))
(void) unlinkat(dir_fdt, to, 0);
return r;
}
int copy_file_atomic_at_full(
int dir_fdf,
const char *from,
int dir_fdt,
const char *to,
mode_t mode,
unsigned chattr_flags,
unsigned chattr_mask,
CopyFlags copy_flags,
copy_progress_bytes_t progress_bytes,
void *userdata) {
_cleanup_(unlink_and_freep) char *t = NULL;
_cleanup_close_ int fdt = -EBADF;
int r;
assert(to);
assert(!FLAGS_SET(copy_flags, COPY_LOCK_BSD));
if (copy_flags & COPY_MAC_CREATE) {
r = mac_selinux_create_file_prepare_at(dir_fdt, to, S_IFREG, /* label_context= */ NULL);
if (r < 0)
return r;
}
fdt = open_tmpfile_linkable_at(dir_fdt, to, O_WRONLY|O_CLOEXEC, &t);
if (copy_flags & COPY_MAC_CREATE)
mac_selinux_create_file_clear();
if (fdt < 0)
return fdt;
r = prepare_nocow(dir_fdf, from, fdt, &chattr_mask, &chattr_flags);
if (r < 0)
return r;
if ((chattr_mask & CHATTR_EARLY_FL) != 0)
(void) chattr_fd(fdt, chattr_flags, chattr_mask & CHATTR_EARLY_FL);
r = copy_file_fd_at_full(dir_fdf, from, fdt, copy_flags, progress_bytes, userdata);
if (r < 0)
return r;
if (fchmod(fdt, mode) < 0)
return -errno;
if ((copy_flags & (COPY_FSYNC|COPY_FSYNC_FULL))) {
/* Sync the file */
if (fsync(fdt) < 0)
return -errno;
}
r = link_tmpfile_at(fdt, dir_fdt, t, to, (copy_flags & COPY_REPLACE) ? LINK_TMPFILE_REPLACE : 0);
if (r < 0)
return r;
t = mfree(t);
unsigned nocow = FLAGS_SET(copy_flags, COPY_NOCOW_AFTER) ? FS_NOCOW_FL : 0;
if (((chattr_mask & ~CHATTR_EARLY_FL) | nocow) != 0)
(void) chattr_fd(fdt, chattr_flags | nocow, (chattr_mask & ~CHATTR_EARLY_FL) | nocow);
r = close_nointr(TAKE_FD(fdt)); /* even if this fails, the fd is now invalidated */
if (r < 0)
goto fail;
if (copy_flags & COPY_FSYNC_FULL) {
/* Sync the parent directory */
r = fsync_parent_at(dir_fdt, to);
if (r < 0)
goto fail;
}
return 0;
fail:
/* link_tmpfile_at() succeeded, so 'to' is now published. In replacement mode, do not
* remove it again, as that may delete a pre-existing target replaced by this copy. */
if (!FLAGS_SET(copy_flags, COPY_REPLACE))
(void) unlinkat(dir_fdt, to, 0);
return r;
}
int copy_times_full(int fdf, int fdt, CopyFlags flags, usec_t ts_clamp) {
struct stat st;
int r;
assert(fdf >= 0);
assert(fdt >= 0);
if (fstat(fdf, &st) < 0)
return -errno;
r = clamp_futimens(fdt, &st, ts_clamp);
if (r < 0)
return r;
if (FLAGS_SET(flags, COPY_CRTIME)) {
usec_t crtime;
if (fd_getcrtime(fdf, &crtime) >= 0)
(void) fd_setcrtime(fdt, MIN(crtime, ts_clamp));
}
return 0;
}
int copy_access(int fdf, int fdt) {
struct stat st;
assert(fdf >= 0);
assert(fdt >= 0);
/* Copies just the access mode (and not the ownership) from fdf to fdt */
if (fstat(fdf, &st) < 0)
return -errno;
return RET_NERRNO(fchmod(fdt, st.st_mode & 07777));
}
int copy_owner(int fdf, int fdt) {
struct stat st;
assert(fdf >= 0);
assert(fdt >= 0);
if (fstat(fdf, &st) < 0)
return -errno;
return RET_NERRNO(fchown(fdt, st.st_uid, st.st_gid));
}
int copy_rights_with_fallback(int fdf, int fdt, const char *patht) {
struct stat st;
assert(fdf >= 0);
assert(fdt >= 0);
/* Copies both access mode and ownership from fdf to fdt */
if (fstat(fdf, &st) < 0)
return -errno;
return fchmod_and_chown_with_fallback(fdt, patht, st.st_mode & 07777, st.st_uid, st.st_gid);
}
int copy_xattr(int df, const char *from, int dt, const char *to, CopyFlags copy_flags) {
_cleanup_free_ char *names = NULL;
int ret = 0, r;
r = listxattr_at_malloc(df, from, 0, &names);
if (r < 0)
return r;
NULSTR_FOREACH(p, names) {
if (!FLAGS_SET(copy_flags, COPY_ALL_XATTRS) && !startswith(p, "user."))
continue;
_cleanup_free_ char *value = NULL;
size_t value_size;
r = getxattr_at_malloc(df, from, p, 0, &value, &value_size);
if (r == -ENODATA)
continue; /* gone by now */
if (r < 0)
return r;
RET_GATHER(ret, xsetxattr_full(dt, to, /* at_flags= */ 0, p, value, value_size, /* xattr_flags= */ 0));
}
return ret;
}
int reflink(int infd, int outfd) {
int r;
assert(infd >= 0);
assert(outfd >= 0);
/* Make sure we invoke the ioctl on a regular file, so that no device driver accidentally gets it. */
r = fd_verify_regular(outfd);
if (r < 0)
return r;
/* FICLONE was introduced in Linux 4.5 but it uses the same number as BTRFS_IOC_CLONE introduced earlier */
assert_cc(FICLONE == BTRFS_IOC_CLONE);
return RET_NERRNO(ioctl(outfd, FICLONE, infd));
}
assert_cc(sizeof(struct file_clone_range) == sizeof(struct btrfs_ioctl_clone_range_args));
int reflink_range(int infd, uint64_t in_offset, int outfd, uint64_t out_offset, uint64_t size) {
int r;
assert(infd >= 0);
assert(outfd >= 0);
/* size==UINT64_MAX mean "clone everything". Translate to 0 for the kernel. */
if (size == UINT64_MAX)
size = 0;
/* Inside the kernel, FICLONE is identical to FICLONERANGE with offsets and size set to zero, let's
* simplify things and use the simple ioctl in that case. */
if (in_offset == 0 && out_offset == 0 && size == 0)
return reflink(infd, outfd);
r = fd_verify_regular(outfd);
if (r < 0)
return r;
assert_cc(FICLONERANGE == BTRFS_IOC_CLONE_RANGE);
struct file_clone_range args = {
.src_fd = infd,
.src_offset = in_offset,
.src_length = size,
.dest_offset = out_offset,
};
return RET_NERRNO(ioctl(outfd, FICLONERANGE, &args));
}