logo

utils-std

Collection of commonly available Unix tools
commit: 40752967b170218b7e93b5d32a046c2c90ac4304
parent dba7c89b63a14ec31c54b4a8a0a387cc9db14525
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Mon,  3 Jun 2024 04:38:13 +0200

cmd/mv: Skip same files

Throwing an error when they are the same files (but different entries)
breaks existing scripts (for example gettext-tiny install.sh).

Opt to simply skip them (also allowed by POSIX) as checking if they
are the same directory entries seems non-trivial beyond throwing
an error when the same path is given twice.

Diffstat:

Mcmd/mv.c12++----------
Mtest-cmd/mv.t6------
2 files changed, 2 insertions(+), 16 deletions(-)

diff --git a/cmd/mv.c b/cmd/mv.c @@ -12,8 +12,8 @@ #include <assert.h> #include <errno.h> #include <fcntl.h> // open -#include <limits.h> // PATH_MAX #include <libgen.h> // basename +#include <limits.h> // PATH_MAX #include <locale.h> // setlocale #include <stdbool.h> #include <stdint.h> // SIZE_MAX @@ -98,15 +98,7 @@ do_renameat(const char *restrict src, struct named_fd destdir, const char *restr return -1; } - if(dest_status.st_ino == src_status.st_ino) - { - fprintf(stderr, - "mv: Error: Source '%s' and destination '%s/%s' are the same file\n", - src, - destdir.name, - dest); - return -1; - } + if(dest_status.st_ino == src_status.st_ino && dest_status.st_dev == src_status.st_dev) return 0; if(no_clob) { diff --git a/test-cmd/mv.t b/test-cmd/mv.t @@ -62,20 +62,14 @@ POSIX mv(1) step 2, same file $ test -e same $ ln same Same $ mv same Same - mv: Error: Source 'same' and destination './Same' are the same file - [1] $ test -e same $ test -e Same $ ln -s same same-s $ mv same same-s - mv: Error: Source 'same' and destination './same-s' are the same file - [1] $ test -e same $ test -e Same $ test -e same-s $ mv Same same-s - mv: Error: Source 'Same' and destination './same-s' are the same file - [1] $ test -e same $ test -e Same $ test -e same-s