public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 1/2] rename: allow renaming in subdirectories
Date: Sun, 25 May 2014 22:58:22 +0100	[thread overview]
Message-ID: <1401055103-28558-1-git-send-email-kerolasa@iki.fi> (raw)

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


             reply	other threads:[~2014-05-25 21:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-25 21:58 Sami Kerola [this message]
2014-05-25 21:58 ` [PATCH 2/2] tests: add rename(1) checks Sami Kerola
2014-05-26  9:57   ` Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1401055103-28558-1-git-send-email-kerolasa@iki.fi \
    --to=kerolasa@iki.fi \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox