From 3d022151d73ca2bb9d6caf5c2151247cc61f64fa Mon Sep 17 00:00:00 2001 From: Ward Wouts Date: Tue, 5 Feb 2002 21:11:23 +0000 Subject: [PATCH] clean up locks a little better --- mvwrap/mvwrap | 57 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/mvwrap/mvwrap b/mvwrap/mvwrap index 2e4cde8..20d917c 100755 --- a/mvwrap/mvwrap +++ b/mvwrap/mvwrap @@ -14,9 +14,10 @@ use File::Copy; use POSIX qw(tmpnam); use Getopt::Long; +&lock; + &cmdline; unless (defined @source) { @source = &read_cur_dir; } -&lock; if (@pattern) { @target = @source; @@ -51,7 +52,7 @@ if (@unsafe = &check_safety(\@source, \@target)) { #Read current dir sub read_cur_dir { my (@dir, $filename); - opendir(DIRHANDLE, ".") or die "couldn't open .: $!"; + opendir(DIRHANDLE, ".") or &cdie("couldn't open .: $!\n"); while ( defined ($filename = readdir(DIRHANDLE)) ) { unless ($filename eq "." | $filename eq "..") { chomp ($filename); @@ -69,7 +70,7 @@ sub edit { @editor = (defined $ENV{EDITOR} ? $ENV{EDITOR} : "vi", "$filename"); if ($opt_e) { @editor = ($opt_e, "$filename") } system(@editor) == 0 - or die "System @editor failed: $!"; + or &cdie("System @editor failed: $!"); } # make tempfiles and install handler to remove them @@ -77,7 +78,7 @@ sub open_temp { my $target; do { $target_name = tmpnam() } 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 &cdie("Couldn't unlink $target_name: $!");} } foreach (@source) { print $target "$_\n"; } @@ -87,7 +88,7 @@ sub open_temp { sub read_temp { my $target_name = shift; - open TARGET, $target_name or die "Couldn't open tempfile: $target_name: $!"; + open TARGET, $target_name or &cdie("Couldn't open tempfile: $target_name: $!"); foreach () { chomp; push @target, $_; @@ -110,7 +111,7 @@ sub move_files { } unless ($opt_n) { move("$source", "$target") - or die "move failed: $!"; + or &cdie("move failed: $!"); } } } @@ -118,7 +119,7 @@ sub move_files { # read pattern file sub read_pattern { - open PAT, "< $opt_f" or die "Couldn't open pattern file: $!\n"; + open PAT, "< $opt_f" or &cdie("Couldn't open pattern file: $!\n"); @pattern = ; close PAT; } @@ -139,20 +140,17 @@ sub pattern_edit { sub run_checks { my $line; unless ( scalar(@source) == scalar(@target) ) { - if ( -e ".mv_wrap" ) { unlink (".mv_wrap"); } - die "Aborting. Source and target list don't have the same number of lines.\n"; + &cdie("Aborting. Source and target list don't have the same number of lines.\n"); } foreach $line (@target) { if ( $line =~ m/^$/ ) { - if ( -e ".mv_wrap" ) { unlink (".mv_wrap"); } - die "Aborting. You can't move to empty names.\n"; + &cdie("Aborting. You can't move to empty names.\n"); } } if ( &check_unique(@target) ) { - if ( -e ".mv_wrap" ) { unlink (".mv_wrap"); } - die "Aborting. You're trying to move multiple files to the same name.\n"; + &cdie("Aborting. You're trying to move multiple files to the same name.\n"); } } @@ -219,7 +217,7 @@ sub safety_belt { } unless ($opt_n) { move("$filename", "$rand") - or die "move failed: $!"; + or &cdie("move failed: $!\n"); } $source->[$filenr] = "$rand"; } @@ -236,27 +234,41 @@ sub paranoia { foreach $csource (@$source) { if ($ctarget eq $csource) { $safe = 1; } } - if (! $safe && -e $ctarget) { die "That would overwrite files\n"; } + if (! $safe && -e $ctarget) { &cdie("That would overwrite files\n"); } } } +# lock, unlock and cdie are the only subs that should use die; +# all others should use &cdie. A handler for this would probably +# be nicer. + +# place lockfile in dir, or exit if one exists. sub lock { - if ( -e ".mv_wrap") { - die "Another mv_wrap process is active in this direcory\n" + if ( -e ".mvwrap") { + die "Another mvwrap process is active in this direcory\n" } else { - open LOCK, "> .mv_wrap" or die "Couldn't open .mv_wrap for writing: $!\n"; + open LOCK, "> .mvwrap" or die "Couldn't open .mvwrap for writing: $!\n"; print LOCK $$; close LOCK; } } +# clean up lockfile sub unlock { - if ( -e ".mv_wrap" ) { - unlink(".mv_wrap") or die "Couldn't remove lock file: $!\n"; + if ( -e ".mvwrap" ) { + unlink(".mvwrap") or die "Couldn't remove lock file: $!\n"; } } +# clean up lock & die +sub cdie { + $message = shift; + &unlock; + die "$message"; +} + +# parse commandline sub cmdline { %optctl = (); &GetOptions("e=s", "h", "p=s", \@pattern, "f=s", "v", "n"); @@ -272,6 +284,7 @@ sub cmdline { } } +# show help message sub help { $opt_h = 1; # just get rid of that stupid message from -w print << "EOT"; @@ -291,9 +304,7 @@ At the moment it takes the following options: -v verbose; shows file moves -n test; doesn't move, implies -v -Specifying files on the command line is still unsafe! There are no checks -to prevent overwriting non-specified files! - EOT +&unlock; exit; }