diff options
author | Joey Hess <joey@kodama.kitenet.net> | 2008-02-02 17:18:59 -0500 |
---|---|---|
committer | Joey Hess <joey@kodama.kitenet.net> | 2008-02-02 17:18:59 -0500 |
commit | bc053c5868a14bd166160ffd2b143dd47622aba5 (patch) | |
tree | 8aa23c4efbebbeff406ccc58ed3c365693115856 /vidir | |
parent | 6c5feb7e384bcf7bae50ccfbefff2db9f03de35c (diff) | |
download | moreutils-bc053c5868a14bd166160ffd2b143dd47622aba5.tar.gz |
* vidir: Applied patch from Stefan Fritsch (one part of #412176):
- Check for control characters (especially newlines) in filenames
and error out, since this can greatly confuse the editor or vidir.
- If the source of a rename does not exist (and thus the rename will fail
anyway), vidir should not move an existing target file to a tmpfile.
- If a directory is renamed, vidir should take that into account when
renaming files in this directory.
- If a directory name is passed as name/ to vidir, vidir should not
add second slash after the name.
* vidir: Add support for unlinking directories. To recursivly delete
a directory and its contents, pipe find to vidir, and delete the directory
and its contents in the editor. Closes: #412176
Diffstat (limited to 'vidir')
-rwxr-xr-x | vidir | 41 |
1 files changed, 35 insertions, 6 deletions
@@ -82,15 +82,20 @@ foreach my $item (@ARGV) { open(STDIN, "/dev/tty") || die "reopen: $!\n"; } elsif (-d $item) { + $item =~ s{/?$}{/}; opendir(DIR, $item) || die "$0: cannot read $item: $!\n"; - push @dir, map { "$item/$_" } sort readdir(DIR); + push @dir, map { "$item$_" } sort readdir(DIR); closedir DIR; } else { push @dir, $item; } } - + +if (grep(/[[:cntrl:]]/, @dir)) { + die "$0: control characters in filenames are not supported\n"; +} + my $tmp=File::Temp->new(TEMPLATE => "dirXXXXX", DIR => File::Spec->tmpdir); open (OUT, ">".$tmp->filename) || die "$0: cannot create ".$tmp->filename.": $!\n"; @@ -132,6 +137,12 @@ while (<IN>) { next unless length $name; my $src=$item{$num}; + if (! (-e $src || -l $src) ) { + print STDERR "$0: $src does not exist\n"; + delete $item{$num}; + next; + } + # deal with swaps if (-e $name || -l $name) { my $tmp=$name."~"; @@ -158,8 +169,15 @@ while (<IN>) { print STDERR "$0: failed to rename $src to $name: $!\n"; $error=1; } - elsif ($verbose) { - print "'$src' -> '$name'\n"; + else { + if (-d $name) { + foreach (values %item) { + s/^\Q$src\E/$name/; + } + } + if ($verbose) { + print "'$src' => '$name'\n"; + } } } delete $item{$num}; @@ -174,8 +192,19 @@ while (<IN>) { close IN || die "$0: cannot read ".$tmp->filename.": $!\n"; unlink($tmp.'~') if -e $tmp.'~'; -foreach my $item (sort values %item) { - if (! unlink($item)) { +sub rm { + my $file = shift; + + if (-d $file && ! -l $file) { + return rmdir $file; + } + else { + return unlink $file; + } +} + +foreach my $item (reverse sort values %item) { + if (! rm($item)) { print STDERR "$0: failed to remove $item: $!\n"; $error=1; } |