logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git
commit: 02bea55023e6f26d5c6eb7cf167a28e49528e92e
parent b170e6a9eb7564dbead8a94660c9808131d827a2
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Wed, 28 Aug 2024 20:08:50 +0200

cmd/mv: copy mode, times, owner&group

Diffstat:

Mcmd/mv.c31++++++++++++++++++++++++++++---
1 file changed, 28 insertions(+), 3 deletions(-)

diff --git a/cmd/mv.c b/cmd/mv.c @@ -62,19 +62,44 @@ copy_file_unlink(struct named_fd srcdir, int in = openat(srcdir.fd, src, O_RDONLY | O_NOCTTY); if(in < 0) { - fprintf(stderr, "mv: Failed opening '%s/%s': %s\n", srcdir.name, src, strerror(errno)); + fprintf(stderr, "mv: Failed opening source '%s/%s': %s\n", srcdir.name, src, strerror(errno)); errno = 0; return -1; } - int out = openat(destdir.fd, dest, O_WRONLY | O_CREAT | O_NOCTTY); + int out = openat(destdir.fd, dest, O_WRONLY | O_CREAT | O_NOCTTY, src_status.st_mode); if(out < 0) { - fprintf(stderr, "mv: Failed opening '%s/%s': %s\n", destdir.name, dest, strerror(errno)); + fprintf(stderr, + "mv: Failed opening destination '%s/%s': %s\n", + destdir.name, + dest, + strerror(errno)); errno = 0; return -1; } + const struct timespec times[2] = {src_status.st_atim, src_status.st_mtim}; + if(futimens(out, times) != 0) + { + fprintf(stderr, + "mv: Warning: Failed copying access & modification times to '%s/%s': %s\n", + destdir.name, + dest, + strerror(errno)); + errno = 0; + } + + if(fchown(out, src_status.st_uid, src_status.st_gid) != 0) + { + fprintf(stderr, + "mv: Warning: Failed copying owner & group to '%s/%s': %s\n", + destdir.name, + dest, + strerror(errno)); + errno = 0; + } + if(auto_file_copy(in, out, src_status.st_size, 0) < 0) return -1; return unlinkat(srcdir.fd, src, 0);