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).
This commit is contained in:
G.raud Meyer 2018-03-29 13:28:10 +02:00
parent 0849ff3660
commit 5454df9c31
1 changed files with 12 additions and 0 deletions

View 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);