From: Eric Gillespie Date: Fri, 5 Dec 2014 23:43:34 +0000 (-0800) Subject: use lkopen_fcntl in mark X-Git-Url: https://diplodocus.org/git/minc/commitdiff_plain/853abbb751ad7d11d0a82f8dc1410335915ea694?hp=9296d92aad740e5025c392b9b7079480e63d97c8 use lkopen_fcntl in mark --- diff --git a/minc b/minc index cce6766..f054159 100755 --- a/minc +++ b/minc @@ -318,19 +318,16 @@ sub mark { my $folder = shift; my $msgnum = shift; my $seq = shift; + my $fn = "$mh/$folder/.mh_sequences"; - my $fh; - my @sequences; + my ($fh, $e) = lkopen_fcntl($fn, O_RDWR | O_CREAT, 0600); - if (not open($fh, $fn)) { - $!{ENOENT} or die("open($fn): $!"); - } else { - @sequences = <$fh>; - chomp(@sequences); - } + my @sequences = <$fh>; + chomp(@sequences); + + truncate($fh, 0) or die("truncate($fn): $!"); my $marked = 0; - open($fh, '>', $fn) or die("open(>$fn): $!"); for $_ (@sequences) { if (/^$seq: (.*)/) { my @parts; @@ -380,17 +377,17 @@ sub mark { # Based on nmh's lkopen_fcntl # Return 0 for success, errno on failure. sub lkopen_fcntl { - my $file = shift; + my $fn = shift; my $access = shift; my $mode = shift; - my $errno = 0; + my $errno; # The assumption here is that if you open the file for writing, you # need an exclusive lock. my $tries = LOCK_TRIES; for (;;) { - sysopen(my $fh, $file, $access, $mode) or die("sysopen($file): $!"); + sysopen(my $fh, $fn, $access, $mode) or die("sysopen($fn): $!"); my $flk = File::FcntlLock->new; $flk->l_start(0); @@ -400,11 +397,11 @@ sub lkopen_fcntl { # Really should only retry on EAGAIN and EINTR... if ($flk->lock($fh, F_SETLK)) { - return $fh, undef; + return $fh; } $errno = $flk->lock_errno; - close($fh) or die("close($file): $!"); + close($fh) or die("close($fn): $!"); if (--$tries == 0) { last; @@ -412,7 +409,8 @@ sub lkopen_fcntl { sleep(1); } - return undef, $errno; + local $! = $errno; + die("failed to lock $fn: $!"); } sub store_message {