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:
parent
0849ff3660
commit
5454df9c31
|
@ -18,6 +18,7 @@ for i in $@; do N=`echo "$i" | sed "s/$FROM/$TO/g"`; mv "$i" "$N"; done
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.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;
|
int ret = 1;
|
||||||
struct stat sb;
|
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) {
|
if (lstat(s, &sb) == -1) {
|
||||||
warn(_("stat of %s failed"), s);
|
warn(_("stat of %s failed"), s);
|
||||||
return 2;
|
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;
|
char *newname = NULL, *file=NULL;
|
||||||
int ret = 1;
|
int ret = 1;
|
||||||
|
|
||||||
|
if (access(s, F_OK) != 0) {
|
||||||
|
warn(_("%s: not accessible"), s);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
if (strchr(from, '/') == NULL && strchr(to, '/') == NULL)
|
if (strchr(from, '/') == NULL && strchr(to, '/') == NULL)
|
||||||
file = strrchr(s, '/');
|
file = strrchr(s, '/');
|
||||||
if (file == NULL)
|
if (file == NULL)
|
||||||
file = s;
|
file = s;
|
||||||
if (string_replace(from, to, file, s, &newname))
|
if (string_replace(from, to, file, s, &newname))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (nooverwrite && access(newname, F_OK) == 0) {
|
if (nooverwrite && access(newname, F_OK) == 0) {
|
||||||
if (verbose)
|
if (verbose)
|
||||||
printf(_("Skipping existing file: `%s'\n"), newname);
|
printf(_("Skipping existing file: `%s'\n"), newname);
|
||||||
|
|
Loading…
Reference in New Issue