]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
rename: check source file access early
authorG.raud Meyer <graud@gmx.com>
Thu, 29 Mar 2018 11:28:10 +0000 (13:28 +0200)
committerG.raud Meyer <graud@gmx.com>
Mon, 9 Apr 2018 12:37:56 +0000 (14:37 +0200)
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

index 147e54fe92f7af21b95f2d45c8c786cc7be331a3..9983ad2a9d8f2e8079f319fa9dd7c1b28fec5131 100644 (file)
@@ -18,6 +18,7 @@ for i in $@; do N=`echo "$i" | sed "s/$FROM/$TO/g"`; mv "$i" "$N"; done
 #include <stdlib.h>
 #include <errno.h>
 #include <getopt.h>
+#include <fcntl.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -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);