summaryrefslogtreecommitdiffstats
path: root/misc-utils/rename.c
diff options
context:
space:
mode:
authorG.raud Meyer2018-03-29 13:28:10 +0200
committerG.raud Meyer2018-04-09 14:37:56 +0200
commit5454df9c3110f7742500e5d2af1ea300924c0120 (patch)
tree171d965d22097e8c0e24a7e7bfc9cc50b0d91ba0 /misc-utils/rename.c
parentrename: prevent --no-act from setting --no-overwrite (diff)
downloadkernel-qcow2-util-linux-5454df9c3110f7742500e5d2af1ea300924c0120.tar.gz
kernel-qcow2-util-linux-5454df9c3110f7742500e5d2af1ea300924c0120.tar.xz
kernel-qcow2-util-linux-5454df9c3110f7742500e5d2af1ea300924c0120.zip
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).
Diffstat (limited to 'misc-utils/rename.c')
-rw-r--r--misc-utils/rename.c12
1 files changed, 12 insertions, 0 deletions
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 <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);