+sub _pre_augment_text {
+ my ($self, $lei) = @_;
+ my $dst = $lei->{ovv}->{dst};
+ my $out;
+ my $devfd = $lei->path_to_fd($dst) // die "bad $dst";
+ if ($devfd >= 0) {
+ $out = $lei->{$devfd};
+ } else { # normal-looking path
+ if (-p $dst) {
+ open $out, '>', $dst or die "open($dst): $!";
+ } elsif (-f _ || !-e _) {
+ # text allows augment, HTML/Atom won't
+ my $mode = $lei->{opt}->{augment} ? '>>' : '>';
+ open $out, $mode, $dst or die "open($mode, $dst): $!";
+ } else {
+ die "$dst is not a file or FIFO\n";
+ }
+ }
+ $lei->{ovv}->ovv_out_lk_init if !$lei->{ovv}->{lock_path};
+ $lei->{1} = $out;
+ undef;
+}
+
+sub _pre_augment_mbox {
+ my ($self, $lei) = @_;
+ my $dst = $lei->{ovv}->{dst};
+ my $out;
+ my $devfd = $lei->path_to_fd($dst) // die "bad $dst";
+ if ($devfd >= 0) {
+ $out = $lei->{$devfd};
+ } else { # normal-looking path
+ 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};
+ } else {
+ die "$dst is not a file or FIFO\n";
+ }
+ $lei->{old_1} = $lei->{1}; # keep for spawning MUA
+ }
+ # Perl does SEEK_END even with O_APPEND :<
+ $self->{seekable} = seek($out, 0, SEEK_SET);
+ if (!$self->{seekable} && !$!{ESPIPE} && !defined($devfd)) {
+ die "seek($dst): $!\n";
+ }
+ if (!$self->{seekable}) {
+ my $imp_before = $lei->{opt}->{'import-before'};
+ die "--import-before specified but $dst is not seekable\n"
+ if $imp_before && !ref($imp_before);
+ die "--augment specified but $dst is not seekable\n" if
+ $lei->{opt}->{augment};
+ die "cannot --save with unseekable $dst\n" if
+ $lei->{dedupe} && $lei->{dedupe}->can('reset_dedupe');
+ }
+ if ($self->{zsfx} = PublicInbox::MboxReader::zsfx($dst)) {
+ pipe(my ($r, $w)) or die "pipe: $!";
+ $lei->{zpipe} = [ $r, $w ];
+ $lei->{ovv}->{lock_path} and
+ die 'BUG: unexpected {ovv}->{lock_path}';
+ $lei->{ovv}->ovv_out_lk_init;
+ } elsif (!$self->{seekable} && !$lei->{ovv}->{lock_path}) {
+ $lei->{ovv}->ovv_out_lk_init;
+ }
+ $lei->{1} = $out;
+ undef;
+}
+
+sub _do_augment_mbox {
+ my ($self, $lei) = @_;
+ return unless $self->{seekable};
+ my $opt = $lei->{opt};
+ return if $lei->{cmd} eq 'up';
+ my $out = $lei->{1};
+ my ($fmt, $dst) = @{$lei->{ovv}}{qw(fmt dst)};
+ return clobber_dst_prepare($lei) unless -s $out;
+ unless ($opt->{augment} || $opt->{'import-before'}) {
+ truncate($out, 0) or die "truncate($dst): $!";
+ return;
+ }
+ my $rd;
+ if (my $zsfx = $self->{zsfx}) {
+ $rd = PublicInbox::MboxReader::zsfxcat($out, $zsfx, $lei);
+ } else {
+ open($rd, '+>>&', $out) or die "dup: $!";
+ }
+ my $dedupe;
+ if ($opt->{augment}) {
+ $dedupe = $lei->{dedupe};
+ $dedupe->prepare_dedupe if $dedupe;
+ } else {
+ clobber_dst_prepare($lei);
+ }
+ if ($opt->{'import-before'}) { # the default
+ my $lse = $lei->{lse};
+ PublicInbox::MboxReader->$fmt($rd, \&_mbox_augment_kw_maybe,
+ $lei, $lse, $opt->{augment});
+ if (!$opt->{augment} and !truncate($out, 0)) {
+ die "truncate($dst): $!";
+ }
+ } else { # --augment --no-import-before
+ PublicInbox::MboxReader->$fmt($rd, \&_augment, $lei);
+ }
+ # maybe some systems don't honor O_APPEND, Perl does this:
+ seek($out, 0, SEEK_END) or die "seek $dst: $!";
+ $dedupe->pause_dedupe if $dedupe;
+}
+
+sub v2w_done_wait { # dwaitpid callback
+ my ($arg, $pid) = @_;
+ my ($v2w, $lei) = @$arg;
+ $lei->child_error($?, "error for $v2w->{ibx}->{inboxdir}") if $?;
+}
+
+sub _pre_augment_v2 {
+ my ($self, $lei) = @_;
+ my $dir = $self->{dst};
+ require PublicInbox::InboxWritable;
+ my ($ibx, @creat);
+ if (-d $dir) {
+ my $opt = { -min_inbox_version => 2 };
+ require PublicInbox::Admin;
+ my @ibx = PublicInbox::Admin::resolve_inboxes([ $dir ], $opt);
+ $ibx = $ibx[0] or die "$dir is not a v2 inbox\n";
+ } else {
+ $creat[0] = {};
+ $ibx = PublicInbox::Inbox->new({
+ name => 'lei-result', # XXX configurable
+ inboxdir => $dir,
+ version => 2,
+ address => [ 'lei@example.com' ],
+ });
+ }
+ PublicInbox::InboxWritable->new($ibx, @creat);
+ $ibx->init_inbox if @creat;
+ my $v2w = $ibx->importer;
+ $v2w->wq_workers_start("lei/v2w $dir", 1, $lei->oldset, {lei => $lei});
+ $v2w->wq_wait_async(\&v2w_done_wait, $lei);
+ $lei->{v2w} = $v2w;
+ return if !$lei->{opt}->{shared};
+ my $d = "$lei->{ale}->{git}->{git_dir}/objects";
+ my $al = "$dir/git/0.git/objects/info/alternates";
+ open my $fh, '+>>', $al or die "open($al): $!";
+ seek($fh, 0, SEEK_SET) or die "seek($al): $!";
+ grep(/\A\Q$d\E\n/, <$fh>) and return;
+ print $fh "$d\n" or die "print($al): $!";
+ close $fh or die "close($al): $!";
+}
+
+sub pre_augment { # fast (1 disk seek), runs in same process as post_augment
+ my ($self, $lei) = @_;
+ # _pre_augment_maildir, _pre_augment_mbox, _pre_augment_v2
+ my $m = $self->can("_pre_augment_$self->{base_type}") or return;
+ $m->($self, $lei);
+}
+
+sub do_augment { # slow, runs in wq worker
+ my ($self, $lei) = @_;
+ # _do_augment_maildir, _do_augment_mbox, or _do_augment_imap
+ my $m = $self->can("_do_augment_$self->{base_type}") or return;
+ $m->($self, $lei);
+}
+
+# fast (spawn compressor or mkdir), runs in same process as pre_augment
+sub post_augment {
+ my ($self, $lei, @args) = @_;
+ $self->{-au_noted}++ and $lei->qerr("# writing to $self->{dst} ...");
+
+ my $wait = $lei->{opt}->{'import-before'} ?
+ $lei->{sto}->wq_do('checkpoint', 1) : 0;
+ # _post_augment_mbox
+ my $m = $self->can("_post_augment_$self->{base_type}") or return;
+ $m->($self, $lei, @args);
+}
+
+# called by every single l2m worker process
+sub do_post_auth {
+ my ($self) = @_;
+ my $lei = $self->{lei};
+ # lei_xsearch can start as soon as all l2m workers get here
+ $lei->{pkt_op_p}->pkt_do('incr_start_query') or
+ die "incr_start_query: $!";
+ my $aug;
+ if (lock_free($self)) { # all workers do_augment
+ my $mod = $self->{-wq_nr_workers};
+ my $shard = $self->{-wq_worker_nr};
+ if (my $net = $lei->{net}) {
+ $net->{shard_info} = [ $mod, $shard ];
+ } else { # Maildir
+ $self->{shard_info} = [ $mod, $shard ];
+ }
+ $aug = 'incr_post_augment';
+ } elsif ($self->{-wq_worker_nr} == 0) { # 1st worker do_augment
+ $aug = 'do_post_augment';
+ }
+ if ($aug) {
+ local $0 = 'do_augment';
+ eval { do_augment($self, $lei) };
+ $lei->fail($@) if $@;
+ $lei->{pkt_op_p}->pkt_do($aug) or die "pkt_do($aug): $!";
+ }
+ # done augmenting, connect the compressor pipe for each worker
+ if (my $zpipe = delete $lei->{zpipe}) {
+ $lei->{1} = $zpipe->[1];
+ close $zpipe->[0];
+ }
+ my $au_peers = delete $self->{au_peers};
+ if ($au_peers) { # wait for peer l2m to finish augmenting:
+ $au_peers->[1] = undef;
+ sysread($au_peers->[0], my $barrier1, 1);
+ }
+ $self->{wcb} = $self->write_cb($lei);
+ if ($au_peers) { # wait for peer l2m to set write_cb
+ $au_peers->[3] = undef;
+ sysread($au_peers->[2], my $barrier2, 1);
+ }
+}
+
+sub ipc_atfork_child {
+ my ($self) = @_;
+ my $lei = $self->{lei};
+ $lei->_lei_atfork_child;
+ $lei->{auth}->do_auth_atfork($self) if $lei->{auth};
+ $SIG{__WARN__} = PublicInbox::Eml::warn_ignore_cb();
+ $self->SUPER::ipc_atfork_child;
+}
+
+sub lock_free {
+ $_[0]->{base_type} =~ /\A(?:maildir|imap|jmap)\z/ ? 1 : 0;
+}
+
+# wakes up the MUA when complete so it can refresh messages list
+sub poke_dst {
+ my ($self) = @_;
+ if ($self->{base_type} eq 'maildir') {
+ my $t = time + 1;
+ utime($t, $t, $self->{poke_dh}) or warn "futimes: $!";
+ }
+}
+
+sub write_mail { # via ->wq_io_do
+ my ($self, $smsg, $eml) = @_;
+ return $self->{wcb}->(undef, $smsg, $eml) if $eml;
+ $smsg->{-lms_rw} = $self->{-lms_rw};
+ $self->{lei}->{ale}->git->cat_async($smsg->{blob}, \&git_to_mail,
+ [$self->{wcb}, $smsg]);
+}
+
+sub wq_atexit_child {
+ my ($self) = @_;
+ local $PublicInbox::DS::in_loop = 0; # waitpid synchronously
+ my $lei = $self->{lei};
+ delete $self->{wcb};
+ $lei->{ale}->git->async_wait_all;
+ my $nr = delete($lei->{-nr_write}) or return;
+ return if $lei->{early_mua} || !$lei->{-progress} || !$lei->{pkt_op_p};
+ $lei->{pkt_op_p}->pkt_do('l2m_progress', $nr);
+}
+
+# runs on a 1s timer in lei-daemon
+sub augment_inprogress {
+ my ($err, $opt, $dst, $au_noted) = @_;
+ eval {
+ return if $$au_noted++ || !$err || !defined(fileno($err));
+ print $err '# '.($opt->{'import-before'} ?
+ "importing non-external contents of $dst" : (
+ ($opt->{dedupe} // 'content') ne 'none') ?
+ "scanning old contents of $dst for dedupe" :
+ "removing old contents of $dst")." ...\n";
+ };
+ warn "E: $@ ($dst)" if $@;
+}
+
+# called in top-level lei-daemon when LeiAuth is done
+sub net_merge_all_done {
+ my ($self, $lei) = @_;
+ if ($PublicInbox::DS::in_loop &&
+ $self->can("_do_augment_$self->{base_type}") &&
+ !$lei->{opt}->{quiet}) {
+ $self->{-au_noted} = 0;
+ PublicInbox::DS::add_timer(1, \&augment_inprogress,
+ $lei->{2}, $lei->{opt},
+ $self->{dst}, \$self->{-au_noted});