public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] rename: allow renaming in subdirectories
@ 2014-05-25 21:58 Sami Kerola
  2014-05-25 21:58 ` [PATCH 2/2] tests: add rename(1) checks Sami Kerola
  0 siblings, 1 reply; 3+ messages in thread
From: Sami Kerola @ 2014-05-25 21:58 UTC (permalink / raw)
  To: util-linux; +Cc: kerolasa

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 | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/misc-utils/rename.c b/misc-utils/rename.c
index 9db85fb..b945aaa 100644
--- a/misc-utils/rename.c
+++ b/misc-utils/rename.c
@@ -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;
-- 
1.9.3


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-05-26  9:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-05-25 21:58 [PATCH 1/2] rename: allow renaming in subdirectories Sami Kerola
2014-05-25 21:58 ` [PATCH 2/2] tests: add rename(1) checks Sami Kerola
2014-05-26  9:57   ` Karel Zak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox