]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei_to_mail: unlink mboxes if not augmenting
authorEric Wong <e@80x24.org>
Thu, 31 Dec 2020 13:51:40 +0000 (13:51 +0000)
committerEric Wong <e@80x24.org>
Fri, 1 Jan 2021 05:00:39 +0000 (05:00 +0000)
This matches mairix(1) behavior and may be safer if there's
concurrent readers on the existing mbox, especially since
we don't do currently implement mbox locking (nor does mairix).

lib/PublicInbox/LeiToMail.pm

index 0b2685b08e7be3b4a14a0523b3cf6c9e3b0686e6..5b80eb2706ac0fba9d5f756b178b09638e09f38d 100644 (file)
@@ -13,7 +13,7 @@ use PublicInbox::LeiDedupe;
 use Symbol qw(gensym);
 use IO::Handle; # ->autoflush
 use Fcntl qw(SEEK_SET SEEK_END O_CREAT O_EXCL O_WRONLY);
-use Errno qw(EEXIST ESPIPE);
+use Errno qw(EEXIST ESPIPE ENOENT);
 
 my %kw2char = ( # Maildir characters
        draft => 'D',
@@ -230,7 +230,10 @@ sub _mbox_write_cb ($$$$) {
        # XXX should we support /dev/stdout.gz ?
        if ($dst eq '/dev/stdout') {
                $out = $lei->{1};
-       } else { # TODO: mbox locking
+       } else { # TODO: mbox locking (but mairix doesn't...)
+               if (!$lei->{opt}->{augment} && -f $dst and !unlink($dst)) {
+                       die "unlink $dst: $!" if $! != ENOENT;
+               }
                open $out, '+>>', $dst or die "open $dst: $!";
                # Perl does SEEK_END even with O_APPEND :<
                $seekable = seek($out, 0, SEEK_SET);
@@ -251,8 +254,6 @@ sub _mbox_write_cb ($$$$) {
                # maybe some systems don't honor O_APPEND, Perl does this:
                seek($out, 0, SEEK_END) or die "seek $dst: $!";
                $dedupe->pause_dedupe if $jobs; # are we forking?
-       } elsif ($seekable) {
-               truncate($out, 0) or die "truncate $dst: $!";
        }
        $dedupe->prepare_dedupe if !$jobs;
        ($out, $pipe_lk) = compress_dst($out, $zsfx, $lei) if $zsfx;