]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiToMail.pm
lei index: new command to index mail w/o git storage
[public-inbox.git] / lib / PublicInbox / LeiToMail.pm
index a546ab421eeb7c72282f1e72de3a811fbad1f2ed..da3a95d20d2e1869124f77d1e4b0f14b17d28ec6 100644 (file)
@@ -137,9 +137,15 @@ sub eml2mboxcl2 {
 
 sub git_to_mail { # git->cat_async callback
        my ($bref, $oid, $type, $size, $arg) = @_;
+       my ($write_cb, $smsg) = @$arg;
+       if ($type eq 'missing' && $smsg->{-lms_ro}) {
+               if ($bref = $smsg->{-lms_ro}->local_blob($oid, 1)) {
+                       $type = 'blob';
+                       $size = length($$bref);
+               }
+       }
        return warn("W: $oid is $type (!= blob)\n") if $type ne 'blob';
        return warn("E: $oid is empty\n") unless $size;
-       my ($write_cb, $smsg) = @$arg;
        die "BUG: expected=$smsg->{blob} got=$oid" if $smsg->{blob} ne $oid;
        $write_cb->($bref, $smsg);
 }
@@ -242,7 +248,7 @@ sub _buf2maildir {
        my $kw = $smsg->{kw} // [];
        my $sfx = join('', sort(map { $kw2char{$_} // () } @$kw));
        my $rand = ''; # chosen by die roll :P
-       my ($tmp, $fh, $final, $ok);
+       my ($tmp, $fh, $base, $ok);
        my $common = $smsg->{blob} // _rand;
        if (defined(my $pct = $smsg->{pct})) { $common .= "=$pct" }
        do {
@@ -257,11 +263,12 @@ sub _buf2maildir {
                $dst .= 'cur/';
                $rand = '';
                do {
-                       $final = $dst.$rand.$common.':2,'.$sfx;
-               } while (!($ok = link($tmp, $final)) && $!{EEXIST} &&
+                       $base = $rand.$common.':2,'.$sfx
+               } while (!($ok = link($tmp, $dst.$base)) && $!{EEXIST} &&
                        ($rand = _rand.','));
-               die "link($tmp, $final): $!" unless $ok;
+               die "link($tmp, $dst$base): $!" unless $ok;
                unlink($tmp) or warn "W: failed to unlink $tmp: $!\n";
+               \$base;
        } else {
                my $err = "Error writing $smsg->{blob} to $dst: $!\n";
                $_[0] = undef; # clobber dst
@@ -276,13 +283,17 @@ sub _maildir_write_cb ($$) {
        $dedupe->prepare_dedupe if $dedupe;
        my $dst = $lei->{ovv}->{dst};
        my $lse = $lei->{lse}; # may be undef
+       my $sto = $lei->{opt}->{'mail-sync'} ? $lei->{sto} : undef;
+       my $out = $sto ? 'maildir:'.$lei->rel2abs($dst) : undef;
        sub { # for git_to_mail
                my ($bref, $smsg, $eml) = @_;
                $dst // return $lei->fail; # dst may be undef-ed in last run
                return if $dedupe && $dedupe->is_dup($eml //
-                                               PublicInbox::Eml->new($$bref));
+                                               PublicInbox::Eml->new($$bref),
+                                               $smsg);
                $lse->xsmsg_vmd($smsg) if $lse;
-               _buf2maildir($dst, $bref // \($eml->as_string), $smsg);
+               my $n = _buf2maildir($dst, $bref // \($eml->as_string), $smsg);
+               $sto->ipc_do('set_sync_info', $smsg->{blob}, $out, $n) if $sto;
                ++$lei->{-nr_write};
        }
 }
@@ -291,21 +302,28 @@ sub _imap_write_cb ($$) {
        my ($self, $lei) = @_;
        my $dedupe = $lei->{dedupe};
        $dedupe->prepare_dedupe if $dedupe;
-       my $imap_append = $lei->{net}->can('imap_append');
+       my $append = $lei->{net}->can('imap_append');
        my $mic = $lei->{net}->mic_get($self->{uri});
        my $folder = $self->{uri}->mailbox;
        my $lse = $lei->{lse}; # may be undef
+       my $sto = $lei->{opt}->{'mail-sync'} ? $lei->{sto} : undef;
+       my $out = $lei->{ovv}->{dst};
        sub { # for git_to_mail
                my ($bref, $smsg, $eml) = @_;
                $mic // return $lei->fail; # mic may be undef-ed in last run
                return if $dedupe && $dedupe->is_dup($eml //
-                                               PublicInbox::Eml->new($$bref));
+                                               PublicInbox::Eml->new($$bref),
+                                               $smsg);
                $lse->xsmsg_vmd($smsg) if $lse;
-               eval { $imap_append->($mic, $folder, $bref, $smsg, $eml) };
+               my $uid = eval { $append->($mic, $folder, $bref, $smsg, $eml) };
                if (my $err = $@) {
                        undef $mic;
                        die $err;
                }
+               # imap_append returns UID if IMAP server has UIDPLUS extension
+               ($sto && $uid =~ /\A[0-9]+\z/) and
+                       $sto->ipc_do('set_sync_info',
+                                       $smsg->{blob}, $out, $uid + 0);
                ++$lei->{-nr_write};
        }
 }
@@ -632,6 +650,7 @@ sub ipc_atfork_child {
        my ($self) = @_;
        my $lei = $self->{lei};
        $lei->_lei_atfork_child;
+       $self->{-lms_ro} = $lei->{lse}->lms if $lei->{lse};
        $lei->{auth}->do_auth_atfork($self) if $lei->{auth};
        $SIG{__WARN__} = PublicInbox::Eml::warn_ignore_cb();
        $self->SUPER::ipc_atfork_child;
@@ -653,6 +672,7 @@ sub poke_dst {
 sub write_mail { # via ->wq_io_do
        my ($self, $smsg, $eml) = @_;
        return $self->{wcb}->(undef, $smsg, $eml) if $eml;
+       $smsg->{-lms_ro} = $self->{-lms_ro};
        $self->{lei}->{ale}->git->cat_async($smsg->{blob}, \&git_to_mail,
                                [$self->{wcb}, $smsg]);
 }
@@ -669,7 +689,7 @@ sub wq_atexit_child {
 }
 
 # called in top-level lei-daemon when LeiAuth is done
-sub net_merge_complete {
+sub net_merge_all_done {
        my ($self) = @_;
        $self->wq_broadcast('do_post_auth');
        $self->wq_close(1);