From b888072fb2c0ef2b851b1b0fc81e5b1998d78a39 Mon Sep 17 00:00:00 2001 From: Ward Wouts Date: Tue, 5 Feb 2002 15:46:52 +0000 Subject: [PATCH] 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 --- mvwrap/mvwrap | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/mvwrap/mvwrap b/mvwrap/mvwrap index c459276..2e4cde8 100755 --- a/mvwrap/mvwrap +++ b/mvwrap/mvwrap @@ -15,7 +15,7 @@ use POSIX qw(tmpnam); use Getopt::Long; &cmdline; -unless (defined @source) { &read_cur_dir; } +unless (defined @source) { @source = &read_cur_dir; } &lock; if (@pattern) { @@ -41,7 +41,7 @@ if (@unsafe = &check_safety(\@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 .: $!"; while ( defined ($filename = readdir(DIRHANDLE)) ) { unless ($filename eq "." | $filename eq "..") { - push @dir, "$filename\n"; + chomp ($filename); + push @dir, $filename; } } closedir(DIRHANDLE); - @source = sort @dir; + return sort @dir; } # 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); END { if ($opt_e) { unlink($target_name) or die "Couldn't unlink $target_name: $!";} } foreach (@source) { - print $target $_; + print $target "$_\n"; } close ($target); return $target_name; @@ -87,7 +88,10 @@ sub open_temp { sub read_temp { my $target_name = shift; open TARGET, $target_name or die "Couldn't open tempfile: $target_name: $!"; - @target = ; + foreach () { + chomp; + push @target, $_; + } close TARGET; } @@ -100,8 +104,6 @@ sub move_files { for ( $i=0; $i < scalar(@$from); $i++ ) { $source=$from->[$i]; $target=$to->[$i]; - chomp($source); - chomp($target); unless ( $source eq $target ) { if ($opt_v||$opt_n) { print "mv \"$source\" \"$target\"\n"; @@ -172,15 +174,17 @@ sub check_safety { my ($from, $to) = @_; my ($i, $j, @changed, @danger, @unique, %seen); my ($a, $b); + my ($n, %to); + # $n=0; %to = map { ($_, $n++) } @to; for ($i=0; $i < scalar(@$from); $i++){ - $a = $from->[$i]; chomp($a); - $b = $to->[$i]; chomp($b); + $a = $from->[$i]; + $b = $to->[$i]; push @changed, $i if ($a ne $b); } foreach $i (@changed) { - $a = $from->[$i]; chomp($a); + $a = $from->[$i]; for ($j=0; $j < scalar(@$to); $j++){ - $b = $to->[$j]; chomp($b); + $b = $to->[$j]; if (($a eq $b) && ($i != $j)) { push @danger, $i; push @danger, $j; @@ -209,7 +213,6 @@ sub safety_belt { my($filenr, $filename, $rand); foreach $filenr (@$unsafe) { $filename = $source->[$filenr]; - chomp $filename; $rand = &rand_file; if ($opt_v||$opt_n) { print "mv \"$filename\" \"$rand\"\n"; @@ -218,7 +221,7 @@ sub safety_belt { move("$filename", "$rand") or die "move failed: $!"; } - $source->[$filenr] = "$rand\n"; + $source->[$filenr] = "$rand"; } } @@ -233,7 +236,6 @@ sub paranoia { foreach $csource (@$source) { if ($ctarget eq $csource) { $safe = 1; } } - chomp $ctarget; if (! $safe && -e $ctarget) { die "That would overwrite files\n"; } } } @@ -250,7 +252,9 @@ sub lock { } sub unlock { - unlink(".mv_wrap") or die "Couldn't remove lock file: $!\n"; + if ( -e ".mv_wrap" ) { + unlink(".mv_wrap") or die "Couldn't remove lock file: $!\n"; + } } sub cmdline { @@ -260,9 +264,9 @@ sub cmdline { &help if $opt_h; if ($opt_f) { &read_pattern; } - if (defined @ARGV) { + if (scalar(@ARGV)>0) { foreach (@ARGV) { - push @source, "$_\n"; + push @source, "$_"; } $paranoia = 1; }