logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git
commit: fa7b0128536ce7a6fbe1f5899bee44ff5fe10ec4
parent a18473d0080d142ac9514a7f22e8d3ce45759da0
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Fri, 20 Sep 2024 03:18:39 +0200

cmd/mv: unify error message formatting

Diffstat:

Mcmd/mv.c84++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
Mtest-cmd/mv.t18+++++++++---------
2 files changed, 60 insertions(+), 42 deletions(-)

diff --git a/cmd/mv.c b/cmd/mv.c @@ -62,7 +62,11 @@ 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 source '%s/%s': %s\n", srcdir.name, src, strerror(errno)); + fprintf(stderr, + "mv: error: Failed opening source '%s/%s': %s\n", + srcdir.name, + src, + strerror(errno)); errno = 0; return -1; } @@ -71,7 +75,7 @@ copy_file_unlink(struct named_fd srcdir, if(out < 0) { fprintf(stderr, - "mv: Failed opening destination '%s/%s': %s\n", + "mv: error: Failed opening destination '%s/%s': %s\n", destdir.name, dest, strerror(errno)); @@ -83,7 +87,7 @@ copy_file_unlink(struct named_fd srcdir, if(futimens(out, times) != 0) { fprintf(stderr, - "mv: Warning: Failed copying access & modification times to '%s/%s': %s\n", + "mv: warning: Failed copying access & modification times to '%s/%s': %s\n", destdir.name, dest, strerror(errno)); @@ -93,7 +97,7 @@ copy_file_unlink(struct named_fd srcdir, 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", + "mv: warning: Failed copying owner & group to '%s/%s': %s\n", destdir.name, dest, strerror(errno)); @@ -111,8 +115,10 @@ rename_dir_entries(struct named_fd srcdir, struct named_fd destdir) DIR *dirsrc = fdopendir(srcdir.fd); if(dirsrc == NULL) { - fprintf( - stderr, "mv: Failed fd-opening source directory '%s': %s\n", srcdir.name, strerror(errno)); + fprintf(stderr, + "mv: error: Failed fd-opening source directory '%s': %s\n", + srcdir.name, + strerror(errno)); return -1; } @@ -125,8 +131,10 @@ rename_dir_entries(struct named_fd srcdir, struct named_fd destdir) { if(errno == 0) break; - fprintf( - stderr, "mv: Failed reading source directory '%s': %s\n", srcdir.name, strerror(errno)); + fprintf(stderr, + "mv: error: Failed reading source directory '%s': %s\n", + srcdir.name, + strerror(errno)); closedir(dirsrc); errno = 0; return -1; @@ -153,7 +161,7 @@ do_renameat(struct named_fd srcdir, { if(destdir.fd == srcdir.fd && strcmp(src, dest) == 0) { - fprintf(stderr, "mv: Error, passed to both source and destination: '%s'\n", src); + fprintf(stderr, "mv: error: Passed to both source and destination: '%s'\n", src); return -1; } @@ -161,7 +169,7 @@ do_renameat(struct named_fd srcdir, if(fstatat(srcdir.fd, src, &src_status, AT_SYMLINK_NOFOLLOW) < 0) { fprintf(stderr, - "mv: Failed getting status for source file '%s/%s': %s\n", + "mv: error: Failed getting status for source file '%s/%s': %s\n", srcdir.name, src, strerror(errno)); @@ -183,7 +191,7 @@ do_renameat(struct named_fd srcdir, if(ret < 0 && errno != ENOENT) { fprintf(stderr, - "mv: Failed getting status for destination file '%s/%s': %s\n", + "mv: error: Failed getting status for destination file '%s/%s': %s\n", destdir.name, dest, strerror(errno)); @@ -198,7 +206,7 @@ do_renameat(struct named_fd srcdir, if(no_clob) { - fprintf(stderr, "mv: Destination file '%s/%s' already exists\n", destdir.name, dest); + fprintf(stderr, "mv: error: Destination file '%s/%s' already exists\n", destdir.name, dest); return -1; } @@ -214,9 +222,10 @@ do_renameat(struct named_fd srcdir, { if(faccessat(destdir.fd, dest, W_OK, 0) == 0) { - if(!consentf("mv: No write permissions for destination file '%s/%s', overwrite? [yN] ", - destdir.name, - dest)) + if(!consentf( + "mv: error: No write permissions for destination file '%s/%s', overwrite? [yN] ", + destdir.name, + dest)) return 0; } else @@ -248,7 +257,7 @@ do_renameat(struct named_fd srcdir, if(child_srcdir.fd < 0) { fprintf(stderr, - "mv: Failed opening source directory '%s/%s': %s\n", + "mv: error: Failed opening source directory '%s/%s': %s\n", srcdir.name, src, strerror(errno)); @@ -258,7 +267,7 @@ do_renameat(struct named_fd srcdir, if(mkdirat(destdir.fd, dest, src_status.st_mode) < 0) { fprintf(stderr, - "mv: Failed creating destination directory '%s/%s': %s\n", + "mv: error: Failed creating destination directory '%s/%s': %s\n", destdir.name, dest, strerror(errno)); @@ -280,7 +289,7 @@ do_renameat(struct named_fd srcdir, if(unlinkat(srcdir.fd, src, AT_REMOVEDIR) < 0) { fprintf(stderr, - "mv: Failed removing source directory '%s/%s': %s\n", + "mv: error: Failed removing source directory '%s/%s': %s\n", srcdir.name, src, strerror(errno)); @@ -296,34 +305,40 @@ do_renameat(struct named_fd srcdir, case ENOTDIR: if(destdir.fd != AT_FDCWD) { - fprintf(stderr, "mv: Failed moving '%s' into '%s': %s\n", src, dest, strerror(errno)); + fprintf( + stderr, "mv: error: Failed moving '%s' into '%s': %s\n", src, dest, strerror(errno)); return -1; } int tmp_destdir = openat(destdir.fd, dest, O_RDONLY | O_DIRECTORY | O_CLOEXEC); if(tmp_destdir < 0) { - fprintf( - stderr, "mv: Failed opening destination directory '%s': %s\n", dest, strerror(errno)); + fprintf(stderr, + "mv: error: Failed opening destination directory '%s': %s\n", + dest, + strerror(errno)); return -1; } if(renameat(srcdir.fd, src, tmp_destdir, src) < 0) { - fprintf( - stderr, "mv: Failed moving '%s' into directory '%s': %s\n", src, dest, strerror(errno)); + fprintf(stderr, + "mv: error: Failed moving '%s' into directory '%s': %s\n", + src, + dest, + strerror(errno)); return -1; } if(close(tmp_destdir) < 0) { - fprintf(stderr, "mv: Failed closing directory '%s': %s\n", dest, strerror(errno)); + fprintf(stderr, "mv: error: Failed closing directory '%s': %s\n", dest, strerror(errno)); return -1; } break; default: fprintf(stderr, - "mv: Failed moving '%s' to '%s/%s': %s\n", + "mv: error: Failed moving '%s' to '%s/%s': %s\n", src, destdir.name, dest, @@ -383,8 +398,10 @@ main(int argc, char *argv[]) if(destdir.fd < 0) { - fprintf( - stderr, "mv: Failed opening destination directory '%s': %s\n", optarg, strerror(errno)); + fprintf(stderr, + "mv: error: Failed opening destination directory '%s': %s\n", + optarg, + strerror(errno)); return 1; } break; @@ -392,11 +409,11 @@ main(int argc, char *argv[]) verbose = true; break; case ':': - fprintf(stderr, "mv: Error: Missing operand for option: '-%c'\n", optopt); + fprintf(stderr, "mv: error: Missing operand for option: '-%c'\n", optopt); usage(); return 1; case '?': - fprintf(stderr, "mv: Error: Unrecognised option: '-%c'\n", optopt); + fprintf(stderr, "mv: error: Unrecognised option: '-%c'\n", optopt); usage(); return 1; } @@ -409,7 +426,7 @@ main(int argc, char *argv[]) setlocale(LC_ALL, ""); if(errno != 0) { - fprintf(stderr, "%s: Warning: Failed to initialize locales: %s\n", argv0, strerror(errno)); + fprintf(stderr, "%s: warning: Failed to initialize locales: %s\n", argv0, strerror(errno)); errno = 0; } @@ -422,7 +439,7 @@ main(int argc, char *argv[]) { if(argc <= 1) { - fprintf(stderr, "mv: Not enough operands, %d given, expect >= 2\n", argc); + fprintf(stderr, "mv: error: Not enough operands, %d given, expect >= 2\n", argc); return 1; } @@ -443,7 +460,7 @@ main(int argc, char *argv[]) if(destdir.fd < 0) { fprintf(stderr, - "mv: Failed opening destination directory '%s': %s\n", + "mv: error: Failed opening destination directory '%s': %s\n", destdir.name, strerror(errno)); @@ -470,7 +487,8 @@ main(int argc, char *argv[]) if(close(destdir.fd) < 0) { - fprintf(stderr, "mv: Failed closing directory '%s': %s\n", destdir.name, strerror(errno)); + fprintf( + stderr, "mv: error: Failed closing directory '%s': %s\n", destdir.name, strerror(errno)); return 1; } diff --git a/test-cmd/mv.t b/test-cmd/mv.t @@ -9,12 +9,12 @@ POSIX, non-directory target with a trailing slash is an error $ touch nondir file $ mv file nondir/ - mv: Failed opening destination directory 'nondir/': Not a directory + mv: error: Failed opening destination directory 'nondir/': Not a directory [1] $ test -e file $ test -e nondir $ mv enoent nondir/ - mv: Failed opening destination directory 'nondir/': Not a directory + mv: error: Failed opening destination directory 'nondir/': Not a directory [1] $ rm nondir file @@ -22,15 +22,15 @@ POSIX mv(1) step 1a, no -f option, no -i option $ mkdir -m -w 1a_no-f_no-write $ touch src $ printf '\n' | mv src 1a_no-f_no-write/dest - mv: Failed moving 'src' to './1a_no-f_no-write/dest': Permission denied + mv: error: Failed moving 'src' to './1a_no-f_no-write/dest': Permission denied [1] $ test -e src $ printf 'n\n' | mv src 1a_no-f_no-write/dest - mv: Failed moving 'src' to './1a_no-f_no-write/dest': Permission denied + mv: error: Failed moving 'src' to './1a_no-f_no-write/dest': Permission denied [1] $ test -e src $ printf 'y\n' | mv src 1a_no-f_no-write/dest - mv: Failed moving 'src' to './1a_no-f_no-write/dest': Permission denied + mv: error: Failed moving 'src' to './1a_no-f_no-write/dest': Permission denied [1] $ test ! -e src [1] @@ -57,7 +57,7 @@ POSIX mv(1) step 1b, no -f option, -i passed POSIX mv(1) step 2, same file $ touch same-name $ mv same-name same-name - mv: Error, passed to both source and destination: 'same-name' + mv: error: Passed to both source and destination: 'same-name' [1] $ test -e same-name $ rm same-name @@ -66,7 +66,7 @@ POSIX mv(1) step 2, same file, hardlink $ touch same-src-h $ ln same-src-h same-dest-h $ mv same-src-h same-src-h - mv: Error, passed to both source and destination: 'same-src-h' + mv: error: Passed to both source and destination: 'same-src-h' [1] $ test -e same-src-h $ test -e same-dest-h @@ -76,7 +76,7 @@ POSIX mv(1) step 2, same file, symlink $ touch same-src-s $ ln -s same-src-s same-dest-s $ mv same-src-s same-src-s - mv: Error, passed to both source and destination: 'same-src-s' + mv: error: Passed to both source and destination: 'same-src-s' [1] $ test -e same-src-s $ test -e same-dest-s @@ -115,7 +115,7 @@ Where destination is an existing file $ touch foo_file_trail destfile_trail $ mv foo_file_trail destfile_trail/ - mv: Failed opening destination directory 'destfile_trail/': Not a directory + mv: error: Failed opening destination directory 'destfile_trail/': Not a directory [1] $ test -f foo_file_trail $ test -f destfile_trail