]> diplodocus.org Git - minc/blobdiff - minc
Oops, broke so mark() never runs!
[minc] / minc
diff --git a/minc b/minc
index 224e56a..92924ca 100755 (executable)
--- a/minc
+++ b/minc
@@ -39,13 +39,17 @@ $SIG{'PIPE'} = 'IGNORE';
 
 use Data::Dumper;
 use Errno;
-use Fcntl qw(O_WRONLY O_EXCL O_CREAT);
+use Fcntl;
 use FileHandle;
+use File::FcntlLock;
 use File::Temp qw(tempfile);
 use File::stat;
 use Getopt::Long qw(:config gnu_getopt no_ignore_case);
+use POSIX qw(WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG);
 use Pod::Usage;
 
+use constant LOCK_TRIES => 60;
+
 our $VERSION = 1;
 
 # If a filter set's header is $MAGIC_TO_TOKEN, that set is compared
@@ -104,7 +108,6 @@ GetOptions(
            'r' => \$rebuild_dot_folders,
           ) or pod2usage();
 $help and pod2usage(-exitstatus=>0, -verbose=>1);
-@ARGV == 0 or pod2usage();
 }
 
 our $run = !$norun;
@@ -138,9 +141,6 @@ $COLUMNS ||= 80;
 if (not $HOME) {
     die("HOME environment variable must be set.\n");
 }
-if (not $MAILDIR) {
-    $MAILDIR = "$HOME/Maildir";
-}
 
 =head1 FILES
 
@@ -182,15 +182,35 @@ our $folder_sorter = sub { sort_by_list(@_, @folder_sort_list) };
 
 my $mh;
 
-$mh = `mhpath +`;
+$mh = `mhpath +`; $? >= 0 or die;
 chomp($mh);
+if (!$mh) {
+    die('mhpath did not give MH mail path: ' . exit_msg($?));
+}
 
 $ENV{"MHCONTEXT"} = $mh . '/.minc.context';
 
+# TODO: Support dot-locking too.  This works on FreeBSD and Linux.
+my $locking = `mhparam datalocking`;
+$? < 0 && die;
+$? == 0 || die('mhparam datalocking: ' . exit_msg($?));
+chomp($locking);
+if ($locking ne 'fcntl') {
+    die("mh locking $locking not supported; only fcntl");
+}
+
 \f
 ###############################################################################
 # Utility procedures
 
+sub exit_msg {
+    my $status = shift;
+    WIFEXITED($status) && return 'exited with status ' . WEXITSTATUS($status);
+    WIFSIGNALED($status) && return 'killed with signal ' . WTERMSIG($status);
+    # WTF
+    "died ($status)";
+}
+
 sub sort_by_list {
     my $a = shift;
     my $b = shift;
@@ -303,19 +323,17 @@ 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);
+
+    seek($fh, 0, SEEK_SET) or die("seek($fn): $!");
+    truncate($fh, 0) or die("truncate($fn): $!");
 
     my $marked = 0;
-    open($fh, '>', $fn) or die("open(>$fn): $!");
     for $_ (@sequences) {
         if (/^$seq: (.*)/) {
             my @parts;
@@ -362,17 +380,65 @@ sub mark {
     close($fh) or die("close(>$fn): $!");
 }
 
+# Based on nmh's lkopen_fcntl
+# Return 0 for success, errno on failure.
+sub lkopen_fcntl {
+    my $fn = shift;
+    my $access = shift;
+    my $mode = shift;
+    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, $fn, $access, $mode) or die("sysopen($fn): $!");
+
+        my $flk = File::FcntlLock->new;
+        $flk->l_start(0);
+        $flk->l_len(0);
+        $flk->l_type(($access & O_ACCMODE) == O_RDONLY ? F_RDLCK : F_WRLCK);
+        $flk->l_whence(SEEK_SET);
+
+        # Really should only retry on EAGAIN and EINTR...
+        if ($flk->lock($fh, F_SETLK)) {
+            return $fh;
+        }
+
+        $errno = $flk->lock_errno;
+        close($fh) or die("close($fn): $!");
+
+        if (--$tries == 0) {
+            last;
+        }
+        sleep(1);
+    }
+
+    local $! = $errno;
+    die("failed to lock $fn: $!");
+}
+
+my @filtered;
 sub store_message {
-    my $msg = shift;
+    my $inbox = shift;
+    my $msg = shift;            # rename $src_msg
     my $mhfolder = shift;
-    my $msgnum;
+    my ($msgnum, $mhmsg);       # rename $dst_msg and $dst_msgpath
     my $try;
-    my $mhmsg;
 
     # We must do this even in -n mode because later steps fail without
     # it.  This should be harmless.
     mkfolder($mhfolder);
 
+    if ("+$mhfolder" eq $inbox) {
+        # If @filtered is empty, this message already has the right number.
+        if (!($msgnum = shift(@filtered))) {
+            $msg =~ m|.*/(\d+)$|;
+            return $1;
+        }
+        $mhmsg = "$mh/$mhfolder/$msgnum";
+    } else {
     # This loop is a modified version of the maildir delivery algorithm.
     $msgnum = get_highest_msgnum($mhfolder);
     for ($try = 0; ; $try++) {
@@ -421,18 +487,22 @@ sub store_message {
         #sleep(2);
     }
 
+        if ($run) {
+            # Mark each message as soon as we store it and bomb if that
+            # fails.  While it is slow, it is not safe to store multiple
+            # messages and then have a failure before marking some (or
+            # all).
+            if ($mhfolder ne 'SPAM') {
+                mark($mhfolder, $msgnum, 'unseen');
+            }
+        }
+    }
+    push(@filtered, $msg);
+
     if ($run) {
         if (not rename($msg, $mhmsg)) {
             die("rename($msg, $mhmsg): $!");
         }
-
-        # Mark each message as soon as we store it and bomb if that
-        # fails.  While it is slow, it is not safe to store multiple
-        # messages and then have a failure before marking some (or
-        # all).
-        if ($mhfolder ne 'SPAM') {
-            mark($mhfolder, $msgnum, 'unseen');
-        }
     }
 
     return $msgnum;
@@ -574,6 +644,7 @@ sub scan_line {
 }
 
 sub filter_mail {
+    my $inbox = shift;
     @_ or return ();
     my $msgcount = @_ - 2; # don't count . and ..
     my $len = length($msgcount);
@@ -585,10 +656,6 @@ sub filter_mail {
     my $msgnum;
     my %folders;
 
-    if (-f "$HOME/.minc") {
-        require "$HOME/.minc";
-    }
-
     # XXX lame names
     my $nf = int($COLUMNS * $SCAN_P_FOLDER);
     my $nm = int($COLUMNS * $SCAN_P_MESSAGE);
@@ -622,7 +689,7 @@ sub filter_mail {
 
         defined($mhfolder) or ($mhfolder = find_mh_folder($msg, $headers));
 
-        $msgnum = store_message($msg, $mhfolder);
+        $msgnum = store_message($inbox, $msg, $mhfolder);
         $folders{$mhfolder}++;
 
         if ($tty) {
@@ -800,8 +867,45 @@ if (!caller()) {
 
     $rebuild_dot_folders and exit(create_dot_folders);
 
-    chdir("$MAILDIR/new") or die("chdir($MAILDIR/new): $!");
-    my %folders = filter_mail(map { $_->[1] }
+    if (-f "$HOME/.minc") {
+        require "$HOME/.minc";
+    }
+
+    my $inbox;
+    if ($MAILDIR) {
+        $inbox = $MAILDIR;
+    } elsif (-d "$HOME/Maildir") {
+        $inbox = "$HOME/Maildir";
+    }
+
+    if (@ARGV > 0) {
+        $inbox = shift(@ARGV);
+    }
+
+    my @files;
+    if (substr($inbox, 0, 1) eq '+') {
+        my @msgs = @ARGV;
+        if (!@msgs) {
+            @msgs = ('cur-last');
+        }
+        open(my $fh, '-|', 'mhpath', @msgs) or die("open(mhpath|): $!");
+        chomp(@files = <$fh>);
+        if (!close($fh)) {
+            $! == 0 or die("open(mhpath|): $!");
+            die('mhpath '. exit_msg($?));
+        }
+        @files > 0 or exit 0;
+    } else {
+        @ARGV == 0 or pod2usage();
+        chdir("$inbox/new") or die("chdir($inbox/new): $!");
+        @files = map { $_->[1] }
+          sort { $a->[0] <=> $b->[0] }
+          map {
+              $st = stat($_) or die("stat($_): $!");
+              [ $st->mtime, $_ ]
+          } getfiles('.');
+
+        @files = map { $_->[1] }
                 sort { $a->[0] <=> $b->[0] }
                 map {
                     if (not ($st = stat($_))) {
@@ -809,7 +913,10 @@ if (!caller()) {
                     }
                     [$st->mtime, $_]
                 }
-                getfiles('.'));
+                getfiles('.');
+    }
+
+    my %folders = filter_mail($inbox, @files);
 
     $run and %folders and update_dot_folders(\%folders);