+ # preserve $? for ->fail or ->x_it code
+}
+
+sub wq_done_wait { # dwaitpid callback
+ my ($arg, $pid) = @_;
+ my ($wq, $lei) = @$arg;
+ my $err_type = $lei->{-err_type};
+ $? and $lei->child_error($?,
+ $err_type ? "$err_type errors during $lei->{cmd}" : ());
+ $lei->dclose;
+}
+
+sub fchdir {
+ my ($lei) = @_;
+ my $dh = $lei->{3} // die 'BUG: lei->{3} (CWD) gone';
+ chdir($dh) || $lei->fail("fchdir: $!");
+}
+
+sub wq_eof { # EOF callback for main daemon
+ my ($lei) = @_;
+ my $wq1 = delete $lei->{wq1} // return $lei->fail; # already failed
+ $wq1->wq_wait_old(\&wq_done_wait, $lei);
+}
+
+sub watch_state_ok ($) {
+ my ($state) = $_[-1]; # $_[0] may be $self
+ $state =~ /\Apause|(?:import|index|tag)-(?:ro|rw)\z/;
+}
+
+sub cancel_maildir_watch ($$) {
+ my ($d, $cfg_f) = @_;
+ my $w = delete $MDIR2CFGPATH->{$d}->{$cfg_f};
+ scalar(keys %{$MDIR2CFGPATH->{$d}}) or
+ delete $MDIR2CFGPATH->{$d};
+ for my $x (@{$w // []}) { $x->cancel }
+}
+
+sub add_maildir_watch ($$) {
+ my ($d, $cfg_f) = @_;
+ if (!exists($MDIR2CFGPATH->{$d}->{$cfg_f})) {
+ my @w = $dir_idle->add_watches(["$d/cur", "$d/new"], 1);
+ push @{$MDIR2CFGPATH->{$d}->{$cfg_f}}, @w if @w;
+ }
+}
+
+sub refresh_watches {
+ my ($lei) = @_;
+ my $cfg = _lei_cfg($lei) or return;
+ my $old = $cfg->{-watches};
+ my $watches = $cfg->{-watches} //= {};
+ my %seen;
+ my $cfg_f = $cfg->{'-f'};
+ for my $w (grep(/\Awatch\..+\.state\z/, keys %$cfg)) {
+ my $url = substr($w, length('watch.'), -length('.state'));
+ require PublicInbox::LeiWatch;
+ $watches->{$url} //= PublicInbox::LeiWatch->new($url);
+ $seen{$url} = undef;
+ my $state = $cfg->get_1("watch.$url", 'state');
+ if (!watch_state_ok($state)) {
+ $lei->err("watch.$url.state=$state not supported");
+ next;
+ }
+ if ($url =~ /\Amaildir:(.+)/i) {
+ my $d = canonpath_harder($1);
+ if ($state eq 'pause') {
+ cancel_maildir_watch($d, $cfg_f);
+ } else {
+ add_maildir_watch($d, $cfg_f);
+ }
+ } else { # TODO: imap/nntp/jmap
+ $lei->child_error(1, "E: watch $url not supported, yet")
+ }
+ }
+
+ # add all known Maildir folders as implicit watches
+ my $sto = $lei->_lei_store;
+ my $renames = 0;
+ if (my $lms = $sto ? $sto->search->lms : undef) {
+ for my $d ($lms->folders('maildir:')) {
+ substr($d, 0, length('maildir:')) = '';
+ my $cd = canonpath_harder($d);
+ my $f = "maildir:$cd";
+
+ # fixup old bugs while we're iterating:
+ if ($d ne $cd) {
+ $sto->ipc_do('lms_rename_folder',
+ "maildir:$d", $f);
+ ++$renames;
+ }
+ next if $watches->{$f}; # may be set to pause
+ require PublicInbox::LeiWatch;
+ $watches->{$f} = PublicInbox::LeiWatch->new($f);
+ $seen{$f} = undef;
+ add_maildir_watch($cd, $cfg_f);
+ }
+ }
+ $lei->sto_done_request if $renames;
+ if ($old) { # cull old non-existent entries
+ for my $url (keys %$old) {
+ next if exists $seen{$url};
+ delete $old->{$url};
+ if ($url =~ /\Amaildir:(.+)/i) {
+ my $d = canonpath_harder($1);
+ cancel_maildir_watch($d, $cfg_f);
+ } else { # TODO: imap/nntp/jmap
+ $lei->child_error(1, "E: watch $url TODO");
+ }
+ }
+ }
+ if (scalar keys %$watches) {
+ $cfg->{-env} //= { %{$lei->{env}}, PWD => '/' }; # for cfg2lei
+ } else {
+ delete $cfg->{-watches};
+ }
+}
+
+sub git_blob_id {
+ my ($lei, $eml) = @_;
+ ($lei->{sto} // _lei_store($lei, 1))->git_blob_id($eml);
+}
+
+sub lms { # read-only LeiMailSync
+ my ($lei) = @_;
+ my $lse = $lei->{lse} // do {
+ my $sto = $lei->{sto} // _lei_store($lei);
+ $sto ? $sto->search : undef
+ };
+ $lse ? $lse->lms : undef;
+}
+
+sub sto_done_request { # only call this from lei-daemon process (not workers)
+ my ($lei, $sock) = @_;
+ if ($sock //= $lei->{sock}) {
+ $LIVE_SOCK{"$sock"} = $sock;
+ $lei->{sto}->ipc_do('done', "$sock"); # issue, async wait
+ } else { # forcibly wait
+ my $wait = $lei->{sto}->ipc_do('done');
+ }
+}
+
+sub sto_done_complete { # called in lei-daemon when LeiStore->done is complete
+ my ($sock_str) = @_;
+ delete $LIVE_SOCK{$sock_str}; # frees {sock} for waiting lei clients