]> diplodocus.org Git - minc/commitdiff
use lkopen_fcntl in mark
authorEric Gillespie <epg@google.com>
Fri, 5 Dec 2014 23:43:34 +0000 (15:43 -0800)
committerEric Gillespie <epg@google.com>
Fri, 5 Dec 2014 23:43:34 +0000 (15:43 -0800)
minc

diff --git a/minc b/minc
index cce6766..f054159 100755 (executable)
--- a/minc
+++ b/minc
@@ -318,19 +318,16 @@ sub mark {
     my $folder = shift;
     my $msgnum = shift;
     my $seq = shift;
     my $folder = shift;
     my $msgnum = shift;
     my $seq = shift;
+
     my $fn = "$mh/$folder/.mh_sequences";
     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;
 
     my $marked = 0;
-    open($fh, '>', $fn) or die("open(>$fn): $!");
     for $_ (@sequences) {
         if (/^$seq: (.*)/) {
             my @parts;
     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 {
 # 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 $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 (;;) {
 
     # 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);
 
         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)) {
 
         # Really should only retry on EAGAIN and EINTR...
         if ($flk->lock($fh, F_SETLK)) {
-            return $fh, undef;
+            return $fh;
         }
 
         $errno = $flk->lock_errno;
         }
 
         $errno = $flk->lock_errno;
-        close($fh) or die("close($file): $!");
+        close($fh) or die("close($fn): $!");
 
         if (--$tries == 0) {
             last;
 
         if (--$tries == 0) {
             last;
@@ -412,7 +409,8 @@ sub lkopen_fcntl {
         sleep(1);
     }
 
         sleep(1);
     }
 
-    return undef, $errno;
+    local $! = $errno;
+    die("failed to lock $fn: $!");
 }
 
 sub store_message {
 }
 
 sub store_message {