Nog effectiever ipv 29 sec op 5300 files nu nog maar:

mvwrap -n  0.46s user 0.10s system 12% cpu 4.635 total

whee. chomp is blijkbaar duur
This commit is contained in:
Ward Wouts 2002-02-05 15:46:52 +00:00
parent 26c1f324d4
commit b888072fb2

View file

@ -15,7 +15,7 @@ use POSIX qw(tmpnam);
use Getopt::Long; use Getopt::Long;
&cmdline; &cmdline;
unless (defined @source) { &read_cur_dir; } unless (defined @source) { @source = &read_cur_dir; }
&lock; &lock;
if (@pattern) { if (@pattern) {
@ -41,7 +41,7 @@ if (@unsafe = &check_safety(\@source, \@target)) {
&move_files(\@source, \@target); &move_files(\@source, \@target);
if ( -e ".mv_wrap" ) { unlink (".mv_wrap"); } &unlock;
######################################################## ########################################################
@ -54,11 +54,12 @@ sub read_cur_dir {
opendir(DIRHANDLE, ".") or die "couldn't open .: $!"; opendir(DIRHANDLE, ".") or die "couldn't open .: $!";
while ( defined ($filename = readdir(DIRHANDLE)) ) { while ( defined ($filename = readdir(DIRHANDLE)) ) {
unless ($filename eq "." | $filename eq "..") { unless ($filename eq "." | $filename eq "..") {
push @dir, "$filename\n"; chomp ($filename);
push @dir, $filename;
} }
} }
closedir(DIRHANDLE); closedir(DIRHANDLE);
@source = sort @dir; return sort @dir;
} }
# call EDITOR or vi with filename # call EDITOR or vi with filename
@ -78,7 +79,7 @@ sub open_temp {
until $target = IO::File->new($target_name, O_RDWR|O_CREAT|O_EXCL); until $target = IO::File->new($target_name, O_RDWR|O_CREAT|O_EXCL);
END { if ($opt_e) { unlink($target_name) or die "Couldn't unlink $target_name: $!";} } END { if ($opt_e) { unlink($target_name) or die "Couldn't unlink $target_name: $!";} }
foreach (@source) { foreach (@source) {
print $target $_; print $target "$_\n";
} }
close ($target); close ($target);
return $target_name; return $target_name;
@ -87,7 +88,10 @@ sub open_temp {
sub read_temp { sub read_temp {
my $target_name = shift; my $target_name = shift;
open TARGET, $target_name or die "Couldn't open tempfile: $target_name: $!"; open TARGET, $target_name or die "Couldn't open tempfile: $target_name: $!";
@target = <TARGET>; foreach (<TARGET>) {
chomp;
push @target, $_;
}
close TARGET; close TARGET;
} }
@ -100,8 +104,6 @@ sub move_files {
for ( $i=0; $i < scalar(@$from); $i++ ) { for ( $i=0; $i < scalar(@$from); $i++ ) {
$source=$from->[$i]; $source=$from->[$i];
$target=$to->[$i]; $target=$to->[$i];
chomp($source);
chomp($target);
unless ( $source eq $target ) { unless ( $source eq $target ) {
if ($opt_v||$opt_n) { if ($opt_v||$opt_n) {
print "mv \"$source\" \"$target\"\n"; print "mv \"$source\" \"$target\"\n";
@ -172,15 +174,17 @@ sub check_safety {
my ($from, $to) = @_; my ($from, $to) = @_;
my ($i, $j, @changed, @danger, @unique, %seen); my ($i, $j, @changed, @danger, @unique, %seen);
my ($a, $b); my ($a, $b);
my ($n, %to);
# $n=0; %to = map { ($_, $n++) } @to;
for ($i=0; $i < scalar(@$from); $i++){ for ($i=0; $i < scalar(@$from); $i++){
$a = $from->[$i]; chomp($a); $a = $from->[$i];
$b = $to->[$i]; chomp($b); $b = $to->[$i];
push @changed, $i if ($a ne $b); push @changed, $i if ($a ne $b);
} }
foreach $i (@changed) { foreach $i (@changed) {
$a = $from->[$i]; chomp($a); $a = $from->[$i];
for ($j=0; $j < scalar(@$to); $j++){ for ($j=0; $j < scalar(@$to); $j++){
$b = $to->[$j]; chomp($b); $b = $to->[$j];
if (($a eq $b) && ($i != $j)) { if (($a eq $b) && ($i != $j)) {
push @danger, $i; push @danger, $i;
push @danger, $j; push @danger, $j;
@ -209,7 +213,6 @@ sub safety_belt {
my($filenr, $filename, $rand); my($filenr, $filename, $rand);
foreach $filenr (@$unsafe) { foreach $filenr (@$unsafe) {
$filename = $source->[$filenr]; $filename = $source->[$filenr];
chomp $filename;
$rand = &rand_file; $rand = &rand_file;
if ($opt_v||$opt_n) { if ($opt_v||$opt_n) {
print "mv \"$filename\" \"$rand\"\n"; print "mv \"$filename\" \"$rand\"\n";
@ -218,7 +221,7 @@ sub safety_belt {
move("$filename", "$rand") move("$filename", "$rand")
or die "move failed: $!"; or die "move failed: $!";
} }
$source->[$filenr] = "$rand\n"; $source->[$filenr] = "$rand";
} }
} }
@ -233,7 +236,6 @@ sub paranoia {
foreach $csource (@$source) { foreach $csource (@$source) {
if ($ctarget eq $csource) { $safe = 1; } if ($ctarget eq $csource) { $safe = 1; }
} }
chomp $ctarget;
if (! $safe && -e $ctarget) { die "That would overwrite files\n"; } if (! $safe && -e $ctarget) { die "That would overwrite files\n"; }
} }
} }
@ -250,8 +252,10 @@ sub lock {
} }
sub unlock { sub unlock {
if ( -e ".mv_wrap" ) {
unlink(".mv_wrap") or die "Couldn't remove lock file: $!\n"; unlink(".mv_wrap") or die "Couldn't remove lock file: $!\n";
} }
}
sub cmdline { sub cmdline {
%optctl = (); %optctl = ();
@ -260,9 +264,9 @@ sub cmdline {
&help if $opt_h; &help if $opt_h;
if ($opt_f) { &read_pattern; } if ($opt_f) { &read_pattern; }
if (defined @ARGV) { if (scalar(@ARGV)>0) {
foreach (@ARGV) { foreach (@ARGV) {
push @source, "$_\n"; push @source, "$_";
} }
$paranoia = 1; $paranoia = 1;
} }