about summary refs log tree commit homepage
path: root/lib/PublicInbox/LeiToMail.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2021-02-25 22:41:38 -1100
committerEric Wong <e@80x24.org>2021-02-26 16:50:50 -0400
commit3104d7492aa4aee07455dcad7449f786188afdf5 (patch)
tree037d7e28dadca739e6d48ec98e7fa08a4ac2327b /lib/PublicInbox/LeiToMail.pm
parent5d31cff1f1b3d59e4c2be534183aeda3725a7649 (diff)
downloadpublic-inbox-3104d7492aa4aee07455dcad7449f786188afdf5.tar.gz
While this diverges from from mairix(1) behavior, it's the safer
option.  We'll follow Debian policy by supporting fcntl and
dotlocks by default (in that order).  Users who do not want
locking can use "--lock=none"

This will be used in a read-only capacity for watching
mailboxes for keyword updates via inotify or EVFILT_VNODE.
Diffstat (limited to 'lib/PublicInbox/LeiToMail.pm')
-rw-r--r--lib/PublicInbox/LeiToMail.pm16
1 files changed, 12 insertions, 4 deletions
diff --git a/lib/PublicInbox/LeiToMail.pm b/lib/PublicInbox/LeiToMail.pm
index 630da67c..de640657 100644
--- a/lib/PublicInbox/LeiToMail.pm
+++ b/lib/PublicInbox/LeiToMail.pm
@@ -463,11 +463,19 @@ sub _pre_augment_mbox {
         my ($self, $lei) = @_;
         my $dst = $lei->{ovv}->{dst};
         if ($dst ne '/dev/stdout') {
-                my $mode = -p $dst ? '>' : '+>>';
-                if (-f _ && !$lei->{opt}->{augment} and !unlink($dst)) {
-                        $! == ENOENT or die "unlink($dst): $!";
+                my $out;
+                if (-p $dst) {
+                        open $out, '>', $dst or die "open($dst): $!";
+                } elsif (-f _ || !-e _) {
+                        require PublicInbox::MboxLock;
+                        my $m = $lei->{opt}->{'lock'} //
+                                        PublicInbox::MboxLock->defaults;
+                        $self->{mbl} = PublicInbox::MboxLock->acq($dst, 1, $m);
+                        $out = $self->{mbl}->{fh};
+                        if (!$lei->{opt}->{augment} and !truncate($out, 0)) {
+                                die "truncate($dst): $!";
+                        }
                 }
-                open my $out, $mode, $dst or die "open($dst): $!";
                 $lei->{old_1} = $lei->{1}; # keep for spawning MUA
                 $lei->{1} = $out;
         }