From: Daan De Meyer Date: Wed, 24 Jul 2024 12:56:11 +0000 (+0200) Subject: copy: Copy file attributes as well X-Git-Tag: v257-rc1~817^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2356104efcc599439c887b087da83fbeca1fa9a6;p=thirdparty%2Fsystemd.git copy: Copy file attributes as well Let's make sure we copy all file attributes that can be copied as well. --- diff --git a/src/shared/copy.c b/src/shared/copy.c index ac1a9b5933a..c5c5cf083d0 100644 --- a/src/shared/copy.c +++ b/src/shared/copy.c @@ -798,6 +798,7 @@ static int fd_copy_regular( void *userdata) { _cleanup_close_ int fdf = -EBADF, fdt = -EBADF; + unsigned attrs = 0; int r, q; assert(st); @@ -813,6 +814,10 @@ static int fd_copy_regular( if (fdf < 0) return fdf; + r = read_attr_fd(fdf, &attrs); + if (r < 0 && !ERRNO_IS_NOT_SUPPORTED(r) && r != -ELOOP) + return r; + if (copy_flags & COPY_MAC_CREATE) { r = mac_selinux_create_file_prepare_at(dt, to, S_IFREG); if (r < 0) @@ -824,6 +829,9 @@ static int fd_copy_regular( if (fdt < 0) return -errno; + if (attrs != 0) + (void) chattr_full(fdt, NULL, attrs, CHATTR_EARLY_FL, NULL, NULL, CHATTR_FALLBACK_BITWISE); + r = copy_bytes_full(fdf, fdt, UINT64_MAX, copy_flags, NULL, NULL, progress, userdata); if (r < 0) goto fail; @@ -839,6 +847,9 @@ static int fd_copy_regular( (void) futimens(fdt, (struct timespec[]) { st->st_atim, st->st_mtim }); (void) copy_xattr(fdf, NULL, fdt, NULL, copy_flags); + if (attrs != 0) + (void) chattr_full(fdt, NULL, attrs, ~CHATTR_EARLY_FL, NULL, NULL, CHATTR_FALLBACK_BITWISE); + if (FLAGS_SET(copy_flags, COPY_VERIFY_LINKED)) { r = fd_verify_linked(fdf); if (r < 0) @@ -1413,6 +1424,7 @@ int copy_file_at_full( _cleanup_close_ int fdf = -EBADF, fdt = -EBADF; struct stat st; + unsigned attrs = 0; int r; assert(dir_fdf >= 0 || dir_fdf == AT_FDCWD); @@ -1430,6 +1442,10 @@ int copy_file_at_full( if (r < 0) return r; + r = read_attr_at(dir_fdf, from, &attrs); + if (r < 0 && !ERRNO_IS_NOT_SUPPORTED(r) && r != -ELOOP) + return r; + WITH_UMASK(0000) { fdt = xopenat_lock_full(dir_fdt, to, flags|O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, @@ -1446,8 +1462,10 @@ int copy_file_at_full( goto fail; } - if (chattr_mask != 0) - (void) chattr_fd(fdt, chattr_flags, chattr_mask & CHATTR_EARLY_FL, NULL); + attrs = (attrs & ~chattr_mask) | (chattr_flags & chattr_mask); + + if (attrs != 0) + (void) chattr_full(fdt, NULL, attrs, CHATTR_EARLY_FL, NULL, NULL, CHATTR_FALLBACK_BITWISE); r = copy_bytes_full(fdf, fdt, UINT64_MAX, copy_flags & ~COPY_LOCK_BSD, NULL, NULL, progress_bytes, userdata); if (r < 0) @@ -1462,8 +1480,8 @@ int copy_file_at_full( goto fail; } - if (chattr_mask != 0) - (void) chattr_fd(fdt, chattr_flags, chattr_mask & ~CHATTR_EARLY_FL, NULL); + if (attrs != 0) + (void) chattr_full(fdt, NULL, attrs, ~CHATTR_EARLY_FL, NULL, NULL, CHATTR_FALLBACK_BITWISE); if (copy_flags & (COPY_FSYNC|COPY_FSYNC_FULL)) { if (fsync(fdt) < 0) { @@ -1508,6 +1526,7 @@ int copy_file_atomic_at_full( _cleanup_(unlink_and_freep) char *t = NULL; _cleanup_close_ int fdt = -EBADF; + unsigned attrs = 0; int r; assert(to); @@ -1524,8 +1543,14 @@ int copy_file_atomic_at_full( if (fdt < 0) return fdt; - if (chattr_mask != 0) - (void) chattr_fd(fdt, chattr_flags, chattr_mask & CHATTR_EARLY_FL, NULL); + r = read_attr_at(dir_fdf, from, &attrs); + if (r < 0 && !ERRNO_IS_NOT_SUPPORTED(r) && r != -ELOOP) + return r; + + attrs = (attrs & ~chattr_mask) | (chattr_flags & chattr_mask); + + if (attrs != 0) + (void) chattr_full(fdt, NULL, attrs, CHATTR_EARLY_FL, NULL, NULL, CHATTR_FALLBACK_BITWISE); r = copy_file_fd_at_full(dir_fdf, from, fdt, copy_flags, progress_bytes, userdata); if (r < 0) @@ -1546,8 +1571,8 @@ int copy_file_atomic_at_full( t = mfree(t); - if (chattr_mask != 0) - (void) chattr_fd(fdt, chattr_flags, chattr_mask & ~CHATTR_EARLY_FL, NULL); + if (attrs != 0) + (void) chattr_full(fdt, NULL, attrs, ~CHATTR_EARLY_FL, NULL, NULL, CHATTR_FALLBACK_BITWISE); r = close_nointr(TAKE_FD(fdt)); /* even if this fails, the fd is now invalidated */ if (r < 0)