clean up locks a little better

This commit is contained in:
Ward Wouts 2002-02-05 21:11:23 +00:00
parent b888072fb2
commit 3d022151d7

View file

@ -14,9 +14,10 @@ use File::Copy;
use POSIX qw(tmpnam); use POSIX qw(tmpnam);
use Getopt::Long; use Getopt::Long;
&lock;
&cmdline; &cmdline;
unless (defined @source) { @source = &read_cur_dir; } unless (defined @source) { @source = &read_cur_dir; }
&lock;
if (@pattern) { if (@pattern) {
@target = @source; @target = @source;
@ -51,7 +52,7 @@ if (@unsafe = &check_safety(\@source, \@target)) {
#Read current dir #Read current dir
sub read_cur_dir { sub read_cur_dir {
my (@dir, $filename); my (@dir, $filename);
opendir(DIRHANDLE, ".") or die "couldn't open .: $!"; opendir(DIRHANDLE, ".") or &cdie("couldn't open .: $!\n");
while ( defined ($filename = readdir(DIRHANDLE)) ) { while ( defined ($filename = readdir(DIRHANDLE)) ) {
unless ($filename eq "." | $filename eq "..") { unless ($filename eq "." | $filename eq "..") {
chomp ($filename); chomp ($filename);
@ -69,7 +70,7 @@ sub edit {
@editor = (defined $ENV{EDITOR} ? $ENV{EDITOR} : "vi", "$filename"); @editor = (defined $ENV{EDITOR} ? $ENV{EDITOR} : "vi", "$filename");
if ($opt_e) { @editor = ($opt_e, "$filename") } if ($opt_e) { @editor = ($opt_e, "$filename") }
system(@editor) == 0 system(@editor) == 0
or die "System @editor failed: $!"; or &cdie("System @editor failed: $!");
} }
# make tempfiles and install handler to remove them # make tempfiles and install handler to remove them
@ -77,7 +78,7 @@ sub open_temp {
my $target; my $target;
do { $target_name = tmpnam() } do { $target_name = tmpnam() }
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 &cdie("Couldn't unlink $target_name: $!");} }
foreach (@source) { foreach (@source) {
print $target "$_\n"; print $target "$_\n";
} }
@ -87,7 +88,7 @@ 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 &cdie("Couldn't open tempfile: $target_name: $!");
foreach (<TARGET>) { foreach (<TARGET>) {
chomp; chomp;
push @target, $_; push @target, $_;
@ -110,7 +111,7 @@ sub move_files {
} }
unless ($opt_n) { unless ($opt_n) {
move("$source", "$target") move("$source", "$target")
or die "move failed: $!"; or &cdie("move failed: $!");
} }
} }
} }
@ -118,7 +119,7 @@ sub move_files {
# read pattern file # read pattern file
sub read_pattern { 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 = <PAT>; @pattern = <PAT>;
close PAT; close PAT;
} }
@ -139,20 +140,17 @@ sub pattern_edit {
sub run_checks { sub run_checks {
my $line; my $line;
unless ( scalar(@source) == scalar(@target) ) { unless ( scalar(@source) == scalar(@target) ) {
if ( -e ".mv_wrap" ) { unlink (".mv_wrap"); } &cdie("Aborting. Source and target list don't have the same number of lines.\n");
die "Aborting. Source and target list don't have the same number of lines.\n";
} }
foreach $line (@target) { foreach $line (@target) {
if ( $line =~ m/^$/ ) { if ( $line =~ m/^$/ ) {
if ( -e ".mv_wrap" ) { unlink (".mv_wrap"); } &cdie("Aborting. You can't move to empty names.\n");
die "Aborting. You can't move to empty names.\n";
} }
} }
if ( &check_unique(@target) ) { if ( &check_unique(@target) ) {
if ( -e ".mv_wrap" ) { unlink (".mv_wrap"); } &cdie("Aborting. You're trying to move multiple files to the same name.\n");
die "Aborting. You're trying to move multiple files to the same name.\n";
} }
} }
@ -219,7 +217,7 @@ sub safety_belt {
} }
unless ($opt_n) { unless ($opt_n) {
move("$filename", "$rand") move("$filename", "$rand")
or die "move failed: $!"; or &cdie("move failed: $!\n");
} }
$source->[$filenr] = "$rand"; $source->[$filenr] = "$rand";
} }
@ -236,27 +234,41 @@ sub paranoia {
foreach $csource (@$source) { foreach $csource (@$source) {
if ($ctarget eq $csource) { $safe = 1; } 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 { sub lock {
if ( -e ".mv_wrap") { if ( -e ".mvwrap") {
die "Another mv_wrap process is active in this direcory\n" die "Another mvwrap process is active in this direcory\n"
} }
else { 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 $$; print LOCK $$;
close LOCK; close LOCK;
} }
} }
# clean up lockfile
sub unlock { sub unlock {
if ( -e ".mv_wrap" ) { if ( -e ".mvwrap" ) {
unlink(".mv_wrap") or die "Couldn't remove lock file: $!\n"; 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 { sub cmdline {
%optctl = (); %optctl = ();
&GetOptions("e=s", "h", "p=s", \@pattern, "f=s", "v", "n"); &GetOptions("e=s", "h", "p=s", \@pattern, "f=s", "v", "n");
@ -272,6 +284,7 @@ sub cmdline {
} }
} }
# show help message
sub help { sub help {
$opt_h = 1; # just get rid of that stupid message from -w $opt_h = 1; # just get rid of that stupid message from -w
print << "EOT"; print << "EOT";
@ -291,9 +304,7 @@ At the moment it takes the following options:
-v verbose; shows file moves -v verbose; shows file moves
-n test; doesn't move, implies -v -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 EOT
&unlock;
exit; exit;
} }