From 5454df9c3110f7742500e5d2af1ea300924c0120 Mon Sep 17 00:00:00 2001 From: "G.raud Meyer" Date: Thu, 29 Mar 2018 13:28:10 +0200 Subject: [PATCH] rename: check source file access early This change makes rename detect inexisting files given on the command line and consider them faliures. This is particularly useful with --no-act (to detect extraneous arguments). It also prevents skipping non existing files (when the modified name happens to exist). This makes --verbose not print skipping messages of false positives (the access error is printed instead). --- misc-utils/rename.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/misc-utils/rename.c b/misc-utils/rename.c index 147e54fe9..9983ad2a9 100644 --- a/misc-utils/rename.c +++ b/misc-utils/rename.c @@ -18,6 +18,7 @@ for i in $@; do N=`echo "$i" | sed "s/$FROM/$TO/g"`; mv "$i" "$N"; done #include #include #include +#include #include #include #include @@ -59,6 +60,11 @@ static int do_symlink(char *from, char *to, char *s, int verbose, int noact, int int ret = 1; struct stat sb; + if (faccessat(AT_FDCWD, s, F_OK, AT_SYMLINK_NOFOLLOW) != 0) { + warn(_("%s: not accessible"), s); + return 2; + } + if (lstat(s, &sb) == -1) { warn(_("stat of %s failed"), s); return 2; @@ -106,12 +112,18 @@ static int do_file(char *from, char *to, char *s, int verbose, int noact, int no char *newname = NULL, *file=NULL; int ret = 1; + if (access(s, F_OK) != 0) { + warn(_("%s: not accessible"), s); + return 2; + } + if (strchr(from, '/') == NULL && strchr(to, '/') == NULL) file = strrchr(s, '/'); if (file == NULL) file = s; if (string_replace(from, to, file, s, &newname)) return 0; + if (nooverwrite && access(newname, F_OK) == 0) { if (verbose) printf(_("Skipping existing file: `%s'\n"), newname);