+ my $dedupe;
+ if ($opt->{augment}) {
+ $dedupe = $lei->{dedupe};
+ $dedupe->prepare_dedupe if $dedupe;
+ }
+ 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 pre_augment { # fast (1 disk seek), runs in same process as post_augment
+ my ($self, $lei) = @_;
+ # _pre_augment_maildir, _pre_augment_mbox
+ 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 = "_do_augment_$self->{base_type}";
+ $self->$m($lei);
+}
+
+# fast (spawn compressor or mkdir), runs in same process as pre_augment
+sub post_augment {
+ my ($self, $lei, @args) = @_;
+ my $wait = $lei->{opt}->{'import-before'} ?
+ $lei->{sto}->ipc_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
+ pkt_do($lei->{pkt_op_p}, '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 $@;
+ pkt_do($lei->{pkt_op_p}, $aug) == 1 or
+ die "do_post_augment trigger: $!";
+ }
+ # done augmenting, connect the compressor pipe for each worker
+ if (my $zpipe = delete $lei->{zpipe}) {
+ $lei->{1} = $zpipe->[1];
+ close $zpipe->[0];
+ }
+ $self->{wcb} = $self->write_cb($lei);
+}
+
+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->{dst} . 'cur');
+ }
+}
+
+sub write_mail { # via ->wq_io_do
+ my ($self, $smsg, $eml) = @_;
+ return $self->{wcb}->(undef, $smsg, $eml) if $eml;
+ $self->{lei}->{ale}->git->cat_async($smsg->{blob}, \&git_to_mail,
+ [$self->{wcb}, $smsg]);
+}
+
+sub wq_atexit_child {
+ my ($self) = @_;
+ delete $self->{wcb};
+ $self->{lei}->{ale}->git->async_wait_all;
+}
+
+# called in top-level lei-daemon when LeiAuth is done
+sub net_merge_complete {
+ my ($self) = @_;
+ $self->wq_broadcast('do_post_auth');
+ $self->wq_close(1);