]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
rename: allow renaming in subdirectories
authorSami Kerola <kerolasa@iki.fi>
Sun, 25 May 2014 21:17:25 +0000 (22:17 +0100)
committerSami Kerola <kerolasa@iki.fi>
Fri, 18 Jul 2014 17:34:14 +0000 (18:34 +0100)
Earlier the rename(1) considered path as possible string to be renamed,
could lead to an issue with none existing destination.  See below for
demonstration of this issue.  After this change all directory elements
are ignored when the match finding happens.

$ cd $(mktemp -d)
$ mkdir aa ab
$ touch a{a,b}/aa
$ rename -v a x */aa
rename: aa/aa: rename to xa/aa failed: No such file or directory

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
misc-utils/rename.c

index 9db85fb1d05488ae9d5af4c1c4ccfcea5f714629..b945aaab7099746f2be7b0c37afd06b400ae0dad 100644 (file)
@@ -46,9 +46,14 @@ static int do_rename(char *from, char *to, char *s, int verbose, int symtarget)
 
                target[sb.st_size] = '\0';
                where = strstr(target, from);
-       } else
-               where = strstr(s, from);
+       } else {
+               char *file;
 
+               file = rindex(s, '/');
+               if (file == NULL)
+                       file = s;
+               where = strstr(file, from);
+       }
        if (where == NULL) {
                free(target);
                return 0;