]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiStore.pm
lei refresh-mail-sync: replace prune-mail-sync
[public-inbox.git] / lib / PublicInbox / LeiStore.pm
1 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # Local storage (cache/memo) for lei(1), suitable for personal/private
5 # mail iff on encrypted device/FS.  Based on v2, but only deduplicates
6 # git storage based on git OID (index deduplication is done in ContentHash)
7 #
8 # for xref3, the following are constant: $eidx_key = '.', $xnum = -1
9 #
10 # We rely on the synchronous IPC API for this in lei-daemon and
11 # multiple lei clients to write to it at once.  This allows the
12 # lei/store IPC process to be decoupled from network latency in
13 # lei WQ workers.
14 package PublicInbox::LeiStore;
15 use strict;
16 use v5.10.1;
17 use parent qw(PublicInbox::Lock PublicInbox::IPC);
18 use PublicInbox::ExtSearchIdx;
19 use PublicInbox::Eml;
20 use PublicInbox::Import;
21 use PublicInbox::InboxWritable qw(eml_from_path);
22 use PublicInbox::V2Writable;
23 use PublicInbox::ContentHash qw(content_hash);
24 use PublicInbox::MID qw(mids);
25 use PublicInbox::LeiSearch;
26 use PublicInbox::MDA;
27 use PublicInbox::Spawn qw(spawn);
28 use PublicInbox::MdirReader;
29 use PublicInbox::LeiToMail;
30 use File::Temp ();
31 use POSIX ();
32 use IO::Handle (); # ->autoflush
33 use Sys::Syslog qw(syslog openlog);
34 use Errno qw(EEXIST ENOENT);
35
36 sub new {
37         my (undef, $dir, $opt) = @_;
38         my $eidx = PublicInbox::ExtSearchIdx->new($dir, $opt);
39         my $self = bless { priv_eidx => $eidx }, __PACKAGE__;
40         eidx_init($self)->done if $opt->{creat};
41         $self;
42 }
43
44 sub git { $_[0]->{priv_eidx}->git } # read-only
45
46 sub packing_factor { $PublicInbox::V2Writable::PACKING_FACTOR }
47
48 sub rotate_bytes {
49         $_[0]->{rotate_bytes} // ((1024 * 1024 * 1024) / $_[0]->packing_factor)
50 }
51
52 sub git_ident ($) {
53         my ($git) = @_;
54         my $rdr = {};
55         open $rdr->{2}, '>', '/dev/null' or die "open /dev/null: $!";
56         chomp(my $i = $git->qx([qw(var GIT_COMMITTER_IDENT)], undef, $rdr));
57         $i =~ /\A(.+) <([^>]+)> [0-9]+ [-\+]?[0-9]+$/ and return ($1, $2);
58         my ($user, undef, undef, undef, undef, undef, $gecos) = getpwuid($<);
59         ($user) = (($user // $ENV{USER} // '') =~ /([\w\-\.\+]+)/);
60         $user //= 'lei-user';
61         ($gecos) = (($gecos // '') =~ /([\w\-\.\+ \t]+)/);
62         $gecos //= 'lei user';
63         require Sys::Hostname;
64         my ($host) = (Sys::Hostname::hostname() =~ /([\w\-\.]+)/);
65         $host //= 'localhost';
66         ($gecos, "$user\@$host")
67 }
68
69 sub importer {
70         my ($self) = @_;
71         my $max;
72         my $im = $self->{im};
73         if ($im) {
74                 return $im if $im->{bytes_added} < $self->rotate_bytes;
75
76                 delete $self->{im};
77                 $im->done;
78                 undef $im;
79                 $self->checkpoint;
80                 $max = $self->{priv_eidx}->{mg}->git_epochs + 1;
81         }
82         my (undef, $tl) = eidx_init($self); # acquire lock
83         $max //= $self->{priv_eidx}->{mg}->git_epochs;
84         while (1) {
85                 my $latest = $self->{priv_eidx}->{mg}->add_epoch($max);
86                 my $git = PublicInbox::Git->new($latest);
87                 $self->done; # unlock
88                 # re-acquire lock, update alternates for new epoch
89                 (undef, $tl) = eidx_init($self);
90                 my $packed_bytes = $git->packed_bytes;
91                 my $unpacked_bytes = $packed_bytes / $self->packing_factor;
92                 if ($unpacked_bytes >= $self->rotate_bytes) {
93                         $max++;
94                         next;
95                 }
96                 my ($n, $e) = git_ident($git);
97                 $self->{im} = $im = PublicInbox::Import->new($git, $n, $e);
98                 $im->{bytes_added} = int($packed_bytes / $self->packing_factor);
99                 $im->{lock_path} = undef;
100                 $im->{path_type} = 'v2';
101                 return $im;
102         }
103 }
104
105 sub search {
106         PublicInbox::LeiSearch->new($_[0]->{priv_eidx}->{topdir});
107 }
108
109 # follows the stderr file
110 sub _tail_err {
111         my ($self) = @_;
112         print { $self->{-err_wr} } readline($self->{-tmp_err});
113 }
114
115 sub eidx_init {
116         my ($self) = @_;
117         my $eidx = $self->{priv_eidx};
118         my $tl = wantarray && $self->{-err_wr} ?
119                         PublicInbox::OnDestroy->new($$, \&_tail_err, $self) :
120                         undef;
121         $eidx->idx_init({-private => 1}); # acquires lock
122         wantarray ? ($eidx, $tl) : $eidx;
123 }
124
125 sub _docids_for ($$) {
126         my ($self, $eml) = @_;
127         my %docids;
128         my $eidx = $self->{priv_eidx};
129         my ($chash, $mids) = PublicInbox::LeiSearch::content_key($eml);
130         my $oidx = $eidx->{oidx};
131         my $im = $self->{im};
132         for my $mid (@$mids) {
133                 my ($id, $prev);
134                 while (my $cur = $oidx->next_by_mid($mid, \$id, \$prev)) {
135                         next if $cur->{bytes} == 0; # external-only message
136                         my $oid = $cur->{blob};
137                         my $docid = $cur->{num};
138                         my $bref = $im ? $im->cat_blob($oid) : undef;
139                         $bref //= $eidx->git->cat_file($oid) //
140                                 _lms_rw($self)->local_blob($oid, 1) // do {
141                                 warn "W: $oid (#$docid) <$mid> not found\n";
142                                 next;
143                         };
144                         local $self->{current_info} = $oid;
145                         my $x = PublicInbox::Eml->new($bref);
146                         $docids{$docid} = $docid if content_hash($x) eq $chash;
147                 }
148         }
149         sort { $a <=> $b } values %docids;
150 }
151
152 # n.b. similar to LeiExportKw->export_kw_md, but this is for a single eml
153 sub export1_kw_md ($$$$$) {
154         my ($self, $mdir, $bn, $oidbin, $vmdish) = @_; # vmd/vmd_mod
155         my $orig = $bn;
156         my (@try, $unkn, $kw);
157         if ($bn =~ s/:2,([a-zA-Z]*)\z//) {
158                 ($kw, $unkn) = PublicInbox::MdirReader::flags2kw($1);
159                 if (my $set = $vmdish->{kw}) {
160                         $kw = $set;
161                 } elsif (my $add = $vmdish->{'+kw'}) {
162                         @$kw{@$add} = ();
163                 } elsif (my $del = $vmdish->{-kw}) {
164                         delete @$kw{@$del};
165                 } # else no changes...
166                 @try = qw(cur new);
167         } else { # no keywords, yet, could be in new/
168                 @try = qw(new cur);
169                 $unkn = [];
170                 if (my $set = $vmdish->{kw}) {
171                         $kw = $set;
172                 } elsif (my $add = $vmdish->{'+kw'}) {
173                         @$kw{@$add} = (); # auto-vivify
174                 } else { # ignore $vmdish->{-kw}
175                         $kw = [];
176                 }
177         }
178         $kw = [ keys %$kw ] if ref($kw) eq 'HASH';
179         $bn .= ':2,'. PublicInbox::LeiToMail::kw2suffix($kw, @$unkn);
180         return if $orig eq $bn; # no change
181
182         # we use link(2) + unlink(2) since rename(2) may
183         # inadvertently clobber if the "uniquefilename" part wasn't
184         # actually unique.
185         my $dst = "$mdir/cur/$bn";
186         for my $d (@try) {
187                 my $src = "$mdir/$d/$orig";
188                 if (link($src, $dst)) {
189                         if (!unlink($src) and $! != ENOENT) {
190                                 syslog('warning', "unlink($src): $!");
191                         }
192                         # TODO: verify oidbin?
193                         lms_mv_src($self, "maildir:$mdir",
194                                         $oidbin, \$orig, $bn);
195                         return;
196                 } elsif ($! == EEXIST) { # lost race with "lei export-kw"?
197                         return;
198                 } elsif ($! != ENOENT) {
199                         syslog('warning', "link($src -> $dst): $!");
200                 }
201         }
202         for (@try) { return if -e "$mdir/$_/$orig" };
203         lms_clear_src($self, "maildir:$mdir", \$orig);
204 }
205
206 sub sto_export_kw ($$$) {
207         my ($self, $docid, $vmdish) = @_; # vmdish (vmd or vmd_mod)
208         my ($eidx, $tl) = eidx_init($self);
209         my $lms = _lms_rw($self) // return;
210         my $xr3 = $eidx->{oidx}->get_xref3($docid, 1);
211         for my $row (@$xr3) {
212                 my (undef, undef, $oidbin) = @$row;
213                 my $locs = $lms->locations_for($oidbin) // next;
214                 while (my ($loc, $ids) = each %$locs) {
215                         if ($loc =~ s!\Amaildir:!!i) {
216                                 for my $id (@$ids) {
217                                         export1_kw_md($self, $loc, $id,
218                                                         $oidbin, $vmdish);
219                                 }
220                         }
221                         # TODO: IMAP
222                 }
223         }
224 }
225
226 # vmd = { kw => [ qw(seen ...) ], L => [ qw(inbox ...) ] }
227 sub set_eml_vmd {
228         my ($self, $eml, $vmd, $docids) = @_;
229         my ($eidx, $tl) = eidx_init($self);
230         $docids //= [ _docids_for($self, $eml) ];
231         for my $docid (@$docids) {
232                 $eidx->idx_shard($docid)->ipc_do('set_vmd', $docid, $vmd);
233                 sto_export_kw($self, $docid, $vmd);
234         }
235         $docids;
236 }
237
238 sub add_eml_vmd {
239         my ($self, $eml, $vmd) = @_;
240         my ($eidx, $tl) = eidx_init($self);
241         my @docids = _docids_for($self, $eml);
242         for my $docid (@docids) {
243                 $eidx->idx_shard($docid)->ipc_do('add_vmd', $docid, $vmd);
244         }
245         \@docids;
246 }
247
248 sub remove_eml_vmd { # remove just the VMD
249         my ($self, $eml, $vmd) = @_;
250         my ($eidx, $tl) = eidx_init($self);
251         my @docids = _docids_for($self, $eml);
252         for my $docid (@docids) {
253                 $eidx->idx_shard($docid)->ipc_do('remove_vmd', $docid, $vmd);
254         }
255         \@docids;
256 }
257
258 sub _lms_rw ($) {
259         my ($self) = @_;
260         my ($eidx, $tl) = eidx_init($self);
261         $self->{lms} //= do {
262                 require PublicInbox::LeiMailSync;
263                 my $f = "$self->{priv_eidx}->{topdir}/mail_sync.sqlite3";
264                 my $lms = PublicInbox::LeiMailSync->new($f);
265                 $lms->lms_write_prepare;
266                 $lms;
267         };
268 }
269
270 sub lms_clear_src {
271         my ($self, $folder, $id) = @_;
272         _lms_rw($self)->clear_src($folder, $id);
273 }
274
275 sub lms_mv_src {
276         my ($self, $folder, $oidbin, $id, $newbn) = @_;
277         _lms_rw($self)->mv_src($folder, $oidbin, $id, $newbn);
278 }
279
280 sub lms_forget_folders {
281         my ($self, @folders) = @_;
282         my $lms = _lms_rw($self);
283         for my $f (@folders) { $lms->forget_folder($f) }
284 }
285
286 sub lms_rename_folder {
287         my ($self, $old, $new) = @_;
288         _lms_rw($self)->rename_folder($old, $new);
289 }
290
291 sub set_sync_info {
292         my ($self, $oidhex, $folder, $id) = @_;
293         _lms_rw($self)->set_src(pack('H*', $oidhex), $folder, $id);
294 }
295
296 sub lms_set_src {
297         my ($self, $oidbin, $folder, $id) = @_;
298         _lms_rw($self)->set_src($oidbin, $folder, $id);
299 }
300
301 sub _remove_if_local { # git->cat_async arg
302         my ($bref, $oidhex, $type, $size, $self) = @_;
303         $self->{im}->remove($bref) if $bref;
304 }
305
306 sub remove_docids ($;@) {
307         my ($self, @docids) = @_;
308         my $eidx = eidx_init($self);
309         for my $docid (@docids) {
310                 $eidx->idx_shard($docid)->ipc_do('xdb_remove', $docid);
311                 $eidx->{oidx}->delete_by_num($docid);
312                 $eidx->{oidx}->{dbh}->do(<<EOF, undef, $docid);
313 DELETE FROM xref3 WHERE docid = ?
314 EOF
315         }
316 }
317
318 # remove the entire message from the index, does not touch mail_sync.sqlite3
319 sub remove_eml {
320         my ($self, $eml) = @_;
321         my $im = $self->importer; # may create new epoch
322         my ($eidx, $tl) = eidx_init($self);
323         my $oidx = $eidx->{oidx};
324         my @docids = _docids_for($self, $eml);
325         my $git = $eidx->git;
326         for my $docid (@docids) {
327                 my $xr3 = $oidx->get_xref3($docid, 1);
328                 for my $row (@$xr3) {
329                         my (undef, undef, $oidbin) = @$row;
330                         my $oidhex = unpack('H*', $oidbin);
331                         $git->cat_async($oidhex, \&_remove_if_local, $self);
332                 }
333         }
334         $git->cat_async_wait;
335         remove_docids($self, @docids);
336         \@docids;
337 }
338
339 sub oid2docid ($$) {
340         my ($self, $oid) = @_;
341         my $eidx = eidx_init($self);
342         my ($docid, @cull) = $eidx->{oidx}->blob_exists($oid);
343         if (@cull) { # fixup old bugs...
344                 warn <<EOF;
345 W: $oid indexed as multiple docids: $docid @cull, culling to fixup old bugs
346 EOF
347                 remove_docids($self, @cull);
348         }
349         $docid;
350 }
351
352 sub _add_vmd ($$$$) {
353         my ($self, $idx, $docid, $vmd) = @_;
354         $idx->ipc_do('add_vmd', $docid, $vmd);
355         sto_export_kw($self, $docid, $vmd);
356 }
357
358 sub add_eml {
359         my ($self, $eml, $vmd, $xoids) = @_;
360         my $im = $self->{-fake_im} // $self->importer; # may create new epoch
361         my ($eidx, $tl) = eidx_init($self);
362         my $oidx = $eidx->{oidx}; # PublicInbox::Import::add checks this
363         my $smsg = bless { -oidx => $oidx }, 'PublicInbox::Smsg';
364         $smsg->{-eidx_git} = $eidx->git if !$self->{-fake_im};
365         my $im_mark = $im->add($eml, undef, $smsg);
366         if ($vmd && $vmd->{sync_info}) {
367                 set_sync_info($self, $smsg->{blob}, @{$vmd->{sync_info}});
368         }
369         $im_mark or return; # duplicate blob returns undef
370
371         local $self->{current_info} = $smsg->{blob};
372         my $vivify_xvmd = delete($smsg->{-vivify_xvmd}) // []; # exact matches
373         if ($xoids) { # fuzzy matches from externals in ale->xoids_for
374                 delete $xoids->{$smsg->{blob}}; # added later
375                 if (scalar keys %$xoids) {
376                         my %docids = map { $_ => 1 } @$vivify_xvmd;
377                         for my $oid (keys %$xoids) {
378                                 my $docid = oid2docid($self, $oid);
379                                 $docids{$docid} = $docid if defined($docid);
380                         }
381                         @$vivify_xvmd = sort { $a <=> $b } keys(%docids);
382                 }
383         }
384         if (@$vivify_xvmd) { # docids list
385                 $xoids //= {};
386                 $xoids->{$smsg->{blob}} = 1;
387                 for my $docid (@$vivify_xvmd) {
388                         my $cur = $oidx->get_art($docid);
389                         my $idx = $eidx->idx_shard($docid);
390                         if (!$cur || $cur->{bytes} == 0) { # really vivifying
391                                 $smsg->{num} = $docid;
392                                 $oidx->add_overview($eml, $smsg);
393                                 $smsg->{-merge_vmd} = 1;
394                                 $idx->index_eml($eml, $smsg);
395                         } else { # lse fuzzy hit off ale
396                                 $idx->ipc_do('add_eidx_info', $docid, '.', $eml);
397                         }
398                         for my $oid (keys %$xoids) {
399                                 $oidx->add_xref3($docid, -1, $oid, '.');
400                         }
401                         _add_vmd($self, $idx, $docid, $vmd) if $vmd;
402                 }
403                 $vivify_xvmd;
404         } elsif (my @docids = _docids_for($self, $eml)) {
405                 # fuzzy match from within lei/store
406                 for my $docid (@docids) {
407                         my $idx = $eidx->idx_shard($docid);
408                         $oidx->add_xref3($docid, -1, $smsg->{blob}, '.');
409                         # add_eidx_info for List-Id
410                         $idx->ipc_do('add_eidx_info', $docid, '.', $eml);
411                         _add_vmd($self, $idx, $docid, $vmd) if $vmd;
412                 }
413                 \@docids;
414         } else { # totally new message
415                 delete $smsg->{-oidx}; # for IPC-friendliness
416                 $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
417                 $oidx->add_overview($eml, $smsg);
418                 $oidx->add_xref3($smsg->{num}, -1, $smsg->{blob}, '.');
419                 my $idx = $eidx->idx_shard($smsg->{num});
420                 $idx->index_eml($eml, $smsg);
421                 _add_vmd($self, $idx, $smsg->{num}, $vmd) if $vmd;
422                 $smsg;
423         }
424 }
425
426 sub set_eml {
427         my ($self, $eml, $vmd, $xoids) = @_;
428         add_eml($self, $eml, $vmd, $xoids) //
429                 set_eml_vmd($self, $eml, $vmd);
430 }
431
432 sub index_eml_only {
433         my ($self, $eml, $vmd, $xoids) = @_;
434         require PublicInbox::FakeImport;
435         local $self->{-fake_im} = PublicInbox::FakeImport->new;
436         set_eml($self, $eml, $vmd, $xoids);
437 }
438
439 # store {kw} / {L} info for a message which is only in an external
440 sub _external_only ($$$) {
441         my ($self, $xoids, $eml) = @_;
442         my $eidx = $self->{priv_eidx};
443         my $oidx = $eidx->{oidx} // die 'BUG: {oidx} missing';
444         my $smsg = bless { blob => '' }, 'PublicInbox::Smsg';
445         $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
446         # save space for an externals-only message
447         my $hdr = $eml->header_obj;
448         $smsg->populate($hdr); # sets lines == 0
449         $smsg->{bytes} = 0;
450         delete @$smsg{qw(From Subject)};
451         $smsg->{to} = $smsg->{cc} = $smsg->{from} = '';
452         $oidx->add_overview($hdr, $smsg); # subject+references for threading
453         $smsg->{subject} = '';
454         for my $oid (keys %$xoids) {
455                 $oidx->add_xref3($smsg->{num}, -1, $oid, '.');
456         }
457         my $idx = $eidx->idx_shard($smsg->{num});
458         $idx->index_eml(PublicInbox::Eml->new("\n\n"), $smsg);
459         ($smsg, $idx);
460 }
461
462 sub update_xvmd {
463         my ($self, $xoids, $eml, $vmd_mod) = @_;
464         my ($eidx, $tl) = eidx_init($self);
465         my $oidx = $eidx->{oidx};
466         my %seen;
467         for my $oid (keys %$xoids) {
468                 my $docid = oid2docid($self, $oid) // next;
469                 delete $xoids->{$oid};
470                 next if $seen{$docid}++;
471                 my $idx = $eidx->idx_shard($docid);
472                 $idx->ipc_do('update_vmd', $docid, $vmd_mod);
473                 sto_export_kw($self, $docid, $vmd_mod);
474         }
475         return unless scalar(keys(%$xoids));
476
477         # see if it was indexed, but with different OID(s)
478         if (my @docids = _docids_for($self, $eml)) {
479                 for my $docid (@docids) {
480                         next if $seen{$docid};
481                         for my $oid (keys %$xoids) {
482                                 $oidx->add_xref3($docid, -1, $oid, '.');
483                         }
484                         my $idx = $eidx->idx_shard($docid);
485                         $idx->ipc_do('update_vmd', $docid, $vmd_mod);
486                         sto_export_kw($self, $docid, $vmd_mod);
487                 }
488                 return;
489         }
490         # totally unseen
491         my ($smsg, $idx) = _external_only($self, $xoids, $eml);
492         $idx->ipc_do('update_vmd', $smsg->{num}, $vmd_mod);
493         sto_export_kw($self, $smsg->{num}, $vmd_mod);
494 }
495
496 # set or update keywords for external message, called via ipc_do
497 sub set_xvmd {
498         my ($self, $xoids, $eml, $vmd) = @_;
499
500         my ($eidx, $tl) = eidx_init($self);
501         my $oidx = $eidx->{oidx};
502         my %seen;
503
504         # see if we can just update existing docs
505         for my $oid (keys %$xoids) {
506                 my $docid = oid2docid($self, $oid) // next;
507                 delete $xoids->{$oid}; # all done with this oid
508                 next if $seen{$docid}++;
509                 my $idx = $eidx->idx_shard($docid);
510                 $idx->ipc_do('set_vmd', $docid, $vmd);
511                 sto_export_kw($self, $docid, $vmd);
512         }
513         return unless scalar(keys(%$xoids));
514
515         # n.b. we don't do _docids_for here, we expect the caller
516         # already checked $lse->kw_changed before calling this sub
517
518         return unless (@{$vmd->{kw} // []}) || (@{$vmd->{L} // []});
519         # totally unseen:
520         my ($smsg, $idx) = _external_only($self, $xoids, $eml);
521         $idx->ipc_do('add_vmd', $smsg->{num}, $vmd);
522         sto_export_kw($self, $smsg->{num}, $vmd);
523 }
524
525 sub checkpoint {
526         my ($self, $wait) = @_;
527         if (my $im = $self->{im}) {
528                 $wait ? $im->barrier : $im->checkpoint;
529         }
530         delete $self->{lms};
531         $self->{priv_eidx}->checkpoint($wait);
532 }
533
534 sub xchg_stderr {
535         my ($self) = @_;
536         _tail_err($self) if $self->{-err_wr};
537         my $dir = $self->{priv_eidx}->{topdir};
538         return unless -e $dir;
539         my $old = delete $self->{-tmp_err};
540         my $pfx = POSIX::strftime('%Y%m%d%H%M%S', gmtime(time));
541         my $err = File::Temp->new(TEMPLATE => "$pfx.$$.lei_storeXXXX",
542                                 SUFFIX => '.err', DIR => $dir);
543         open STDERR, '>>', $err->filename or die "dup2: $!";
544         STDERR->autoflush(1); # shared with shard subprocesses
545         $self->{-tmp_err} = $err; # separate file description for RO access
546         undef;
547 }
548
549 sub done {
550         my ($self, $sock_ref) = @_;
551         my $err = '';
552         if (my $im = delete($self->{im})) {
553                 eval { $im->done };
554                 if ($@) {
555                         $err .= "import done: $@\n";
556                         warn $err;
557                 }
558         }
559         delete $self->{lms};
560         $self->{priv_eidx}->done; # V2Writable::done
561         xchg_stderr($self);
562         die $err if $err;
563
564         # notify clients ->done has been issued
565         defined($sock_ref) and
566                 $self->{s2d_op_p}->pkt_do('sto_done_complete', $sock_ref);
567 }
568
569 sub ipc_atfork_child {
570         my ($self) = @_;
571         my $lei = $self->{lei};
572         $lei->_lei_atfork_child(1) if $lei;
573         xchg_stderr($self);
574         if (my $to_close = delete($self->{to_close})) {
575                 close($_) for @$to_close;
576         }
577         openlog('lei/store', 'pid,nowait,nofatal,ndelay', 'user');
578         $self->SUPER::ipc_atfork_child;
579 }
580
581 sub write_prepare {
582         my ($self, $lei) = @_;
583         $lei // die 'BUG: $lei not passed';
584         unless ($self->{-ipc_req}) {
585                 # s2d => store-to-daemon messages
586                 require PublicInbox::PktOp;
587                 my ($s2d_op_c, $s2d_op_p) = PublicInbox::PktOp->pair;
588                 my $dir = $lei->store_path;
589                 $self->ipc_lock_init("$dir/ipc.lock");
590                 substr($dir, -length('/lei/store'), 10, '');
591                 pipe(my ($r, $w)) or die "pipe: $!";
592                 $w->autoflush(1);
593                 # Mail we import into lei are private, so headers filtered out
594                 # by -mda for public mail are not appropriate
595                 local @PublicInbox::MDA::BAD_HEADERS = ();
596                 $self->ipc_worker_spawn("lei/store $dir", $lei->oldset, {
597                                         lei => $lei,
598                                         -err_wr => $w,
599                                         to_close => [ $r, $s2d_op_c->{sock} ],
600                                         s2d_op_p => $s2d_op_p,
601                                 });
602                 require PublicInbox::LeiStoreErr;
603                 PublicInbox::LeiStoreErr->new($r, $lei);
604                 $s2d_op_c->{ops} = {
605                         sto_done_complete => [ $lei->can('sto_done_complete') ]
606                 };
607         }
608         $lei->{sto} = $self;
609 }
610
611 # called by lei-daemon before lei->refresh_watches
612 sub add_sync_folders {
613         my ($self, @folders) = @_;
614         my $lms = _lms_rw($self);
615         for my $f (@folders) { $lms->fid_for($f, 1) }
616 }
617
618 1;