]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiMirror.pm
clone: support --include and --exclude with multi-clone
[public-inbox.git] / lib / PublicInbox / LeiMirror.pm
1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # "lei add-external --mirror" support (also "public-inbox-clone");
5 package PublicInbox::LeiMirror;
6 use strict;
7 use v5.10.1;
8 use parent qw(PublicInbox::IPC);
9 use PublicInbox::Config;
10 use PublicInbox::AutoReap;
11 use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
12 use IO::Compress::Gzip qw(gzip $GzipError);
13 use PublicInbox::Spawn qw(popen_rd spawn);
14 use File::Temp ();
15 use Fcntl qw(SEEK_SET O_CREAT O_EXCL O_WRONLY);
16 use Carp qw(croak);
17
18 sub _wq_done_wait { # dwaitpid callback (via wq_eof)
19         my ($arg, $pid) = @_;
20         my ($mrr, $lei) = @$arg;
21         my $f = "$mrr->{dst}/mirror.done";
22         if ($?) {
23                 $lei->child_error($?);
24         } elsif (!unlink($f)) {
25                 warn("unlink($f): $!\n") unless $!{ENOENT};
26         } else {
27                 if ($lei->{cmd} ne 'public-inbox-clone') {
28                         # calls _finish_add_external
29                         $lei->lazy_cb('add-external', '_finish_'
30                                         )->($lei, $mrr->{dst});
31                 }
32                 $lei->qerr("# mirrored $mrr->{src} => $mrr->{dst}");
33         }
34         $lei->dclose;
35 }
36
37 # for old installations without manifest.js.gz
38 sub try_scrape {
39         my ($self) = @_;
40         my $uri = URI->new($self->{src});
41         my $lei = $self->{lei};
42         my $curl = $self->{curl} //= PublicInbox::LeiCurl->new($lei) or return;
43         my $cmd = $curl->for_uri($lei, $uri, '--compressed');
44         my $opt = { 0 => $lei->{0}, 2 => $lei->{2} };
45         my $fh = popen_rd($cmd, undef, $opt);
46         my $html = do { local $/; <$fh> } // die "read(curl $uri): $!";
47         close($fh) or return $lei->child_error($?, "@$cmd failed");
48
49         # we grep with URL below, we don't want Subject/From headers
50         # making us clone random URLs
51         my @html = split(/<hr>/, $html);
52         my @urls = ($html[-1] =~ m!\bgit clone --mirror ([a-z\+]+://\S+)!g);
53         my $url = $uri->as_string;
54         chop($url) eq '/' or die "BUG: $uri not canonicalized";
55
56         # since this is for old instances w/o manifest.js.gz, try v1 first
57         return clone_v1($self) if grep(m!\A\Q$url\E/*\z!, @urls);
58         if (my @v2_urls = grep(m!\A\Q$url\E/[0-9]+\z!, @urls)) {
59                 my %v2_epochs = map {
60                         my ($n) = (m!/([0-9]+)\z!);
61                         $n => [ URI->new($_), '' ]
62                 } @v2_urls; # uniq
63                 return clone_v2($self, \%v2_epochs);
64         }
65
66         # filter out common URLs served by WWW (e.g /$MSGID/T/)
67         if (@urls && $url =~ s!/+[^/]+\@[^/]+/.*\z!! &&
68                         grep(m!\A\Q$url\E/*\z!, @urls)) {
69                 die <<"";
70 E: confused by scraping <$uri>, did you mean <$url>?
71
72         }
73         @urls and die <<"";
74 E: confused by scraping <$uri>, got ambiguous results:
75 @urls
76
77         die "E: scraping <$uri> revealed nothing\n";
78 }
79
80 sub clone_cmd {
81         my ($lei, $opt) = @_;
82         my @cmd = qw(git);
83         $opt->{$_} = $lei->{$_} for (0..2);
84         # we support "-c $key=$val" for arbitrary git config options
85         # e.g.: git -c http.proxy=socks5h://127.0.0.1:9050
86         push(@cmd, '-c', $_) for @{$lei->{opt}->{c} // []};
87         push @cmd, qw(clone --mirror);
88         push @cmd, '-q' if $lei->{opt}->{quiet};
89         push @cmd, '-v' if $lei->{opt}->{verbose};
90         # XXX any other options to support?
91         # --reference is tricky with multiple epochs...
92         @cmd;
93 }
94
95 sub ft_rename ($$$) {
96         my ($ft, $dst, $open_mode) = @_;
97         my $fn = $ft->filename;
98         my @st = stat($dst);
99         my $mode = @st ? ($st[2] & 07777) : ($open_mode & ~umask);
100         chmod($mode, $ft) or croak "E: chmod $fn: $!";
101         rename($fn, $dst) or croak "E: rename($fn => $ft): $!";
102         $ft->unlink_on_destroy(0);
103 }
104
105 sub _get_txt { # non-fatal
106         my ($self, $endpoint, $file, $mode) = @_;
107         my $uri = URI->new($self->{cur_src} // $self->{src});
108         my $lei = $self->{lei};
109         my $path = $uri->path;
110         chop($path) eq '/' or die "BUG: $uri not canonicalized";
111         $uri->path("$path/$endpoint");
112         my $dst = $self->{cur_dst} // $self->{dst};
113         my $ft = File::Temp->new(TEMPLATE => "$file-XXXX", DIR => $dst);
114         my $opt = { 0 => $lei->{0}, 1 => $lei->{1}, 2 => $lei->{2} };
115         my $cmd = $self->{curl}->for_uri($lei, $uri,
116                                         qw(--compressed -R -o), $ft->filename);
117         my $cerr = run_reap($lei, $cmd, $opt);
118         return "$uri missing" if ($cerr >> 8) == 22;
119         return "# @$cmd failed (non-fatal)" if $cerr;
120         ft_rename($ft, "$dst/$file", $mode);
121         undef; # success
122 }
123
124 # tries the relatively new /$INBOX/_/text/config/raw endpoint
125 sub _try_config {
126         my ($self) = @_;
127         my $dst = $self->{cur_dst} // $self->{dst};
128         if (!-d $dst || !mkdir($dst)) {
129                 require File::Path;
130                 File::Path::mkpath($dst);
131                 -d $dst or die "mkpath($dst): $!\n";
132         }
133         my $err = _get_txt($self,
134                         qw(_/text/config/raw inbox.config.example), 0444);
135         return warn($err, "\n") if $err;
136         my $f = "$dst/inbox.config.example";
137         my $cfg = PublicInbox::Config->git_config_dump($f, $self->{lei}->{2});
138         my $ibx = $self->{ibx} = {};
139         for my $sec (grep(/\Apublicinbox\./, @{$cfg->{-section_order}})) {
140                 for (qw(address newsgroup nntpmirror)) {
141                         $ibx->{$_} = $cfg->{"$sec.$_"};
142                 }
143         }
144 }
145
146 sub set_description ($) {
147         my ($self) = @_;
148         my $dst = $self->{cur_dst} // $self->{dst};
149         my $f = "$dst/description";
150         open my $fh, '+>>', $f or die "open($f): $!";
151         seek($fh, 0, SEEK_SET) or die "seek($f): $!";
152         chomp(my $d = do { local $/; <$fh> } // die "read($f): $!");
153         if ($d eq '($INBOX_DIR/description missing)' ||
154                         $d =~ /^Unnamed repository/ || $d !~ /\S/) {
155                 seek($fh, 0, SEEK_SET) or die "seek($f): $!";
156                 truncate($fh, 0) or die "truncate($f): $!";
157                 my $src = $self->{cur_src} // $self->{src};
158                 print $fh "mirror of $src\n" or die "print($f): $!";
159                 close $fh or die "close($f): $!";
160         }
161 }
162
163 sub index_cloned_inbox {
164         my ($self, $iv) = @_;
165         my $lei = $self->{lei};
166         my $err = _get_txt($self, qw(description description), 0666);
167         warn($err, "\n") if $err; # non fatal
168         eval { set_description($self) };
169         warn $@ if $@;
170
171         # n.b. public-inbox-clone works w/o (SQLite || Xapian)
172         # lei is useless without Xapian + SQLite
173         if ($lei->{cmd} ne 'public-inbox-clone') {
174                 my $ibx = delete($self->{ibx}) // {
175                         address => [ 'lei@example.com' ],
176                         version => $iv,
177                 };
178                 $ibx->{inboxdir} = $self->{cur_dst} // $self->{dst};
179                 PublicInbox::Inbox->new($ibx);
180                 PublicInbox::InboxWritable->new($ibx);
181                 my $opt = {};
182                 for my $sw ($lei->index_opt) {
183                         my ($k) = ($sw =~ /\A([\w-]+)/);
184                         $opt->{$k} = $lei->{opt}->{$k};
185                 }
186                 # force synchronous dwaitpid for v2:
187                 local $PublicInbox::DS::in_loop = 0;
188                 my $cfg = PublicInbox::Config->new(undef, $lei->{2});
189                 my $env = PublicInbox::Admin::index_prepare($opt, $cfg);
190                 local %ENV = (%ENV, %$env) if $env;
191                 PublicInbox::Admin::progress_prepare($opt, $lei->{2});
192                 PublicInbox::Admin::index_inbox($ibx, undef, $opt);
193         }
194         return if defined $self->{cur_dst};
195         open my $x, '>', "$self->{dst}/mirror.done"; # for _wq_done_wait
196 }
197
198 sub run_reap {
199         my ($lei, $cmd, $opt) = @_;
200         $lei->qerr("# @$cmd");
201         my $ar = PublicInbox::AutoReap->new(spawn($cmd, undef, $opt));
202         $ar->join;
203         my $ret = $?;
204         $? = 0; # don't let it influence normal exit
205         $ret;
206 }
207
208 sub clone_v1 {
209         my ($self) = @_;
210         my $lei = $self->{lei};
211         my $curl = $self->{curl} //= PublicInbox::LeiCurl->new($lei) or return;
212         my $uri = URI->new($self->{cur_src} // $self->{src});
213         defined($lei->{opt}->{epoch}) and
214                 die "$uri is a v1 inbox, --epoch is not supported\n";
215         my $pfx = $curl->torsocks($lei, $uri) or return;
216         my $dst = $self->{cur_dst} // $self->{dst};
217         my $cmd = [ @$pfx, clone_cmd($lei, my $opt = {}),
218                         $uri->as_string, $dst ];
219         my $cerr = run_reap($lei, $cmd, $opt);
220         return $lei->child_error($cerr, "@$cmd failed") if $cerr;
221         _try_config($self);
222         write_makefile($dst, 1);
223         index_cloned_inbox($self, 1);
224 }
225
226 sub parse_epochs ($$) {
227         my ($opt_epochs, $v2_epochs) = @_; # $epochs "LOW..HIGH"
228         $opt_epochs // return; # undef => all epochs
229         my ($lo, $dotdot, $hi, @extra) = split(/(\.\.)/, $opt_epochs);
230         undef($lo) if ($lo // '') eq '';
231         my $re = qr/\A~?[0-9]+\z/;
232         if (@extra || (($lo // '0') !~ $re) ||
233                         (($hi // '0') !~ $re) ||
234                         !(grep(defined, $lo, $hi))) {
235                 die <<EOM;
236 --epoch=$opt_epochs not in the form of `LOW..HIGH', `LOW..', nor `..HIGH'
237 EOM
238         }
239         my @n = sort { $a <=> $b } keys %$v2_epochs;
240         for (grep(defined, $lo, $hi)) {
241                 if (/\A[0-9]+\z/) {
242                         $_ > $n[-1] and die
243 "`$_' exceeds maximum available epoch ($n[-1])\n";
244                         $_ < $n[0] and die
245 "`$_' is lower than minimum available epoch ($n[0])\n";
246                 } elsif (/\A~([0-9]+)/) {
247                         my $off = -$1 - 1;
248                         $n[$off] // die "`$_' is out of range\n";
249                         $_ = $n[$off];
250                 } else { die "`$_' not understood\n" }
251         }
252         defined($lo) && defined($hi) && $lo > $hi and die
253 "low value (`$lo') exceeds high (`$hi')\n";
254         $lo //= $n[0] if $dotdot;
255         $hi //= $n[-1] if $dotdot;
256         $hi //= $lo;
257         my $want = {};
258         for ($lo..$hi) {
259                 if (defined $v2_epochs->{$_}) {
260                         $want->{$_} = 1;
261                 } else {
262                         warn
263 "# epoch $_ is not available (non-fatal, $lo..$hi)\n";
264                 }
265         }
266         $want
267 }
268
269 sub init_placeholder ($$) {
270         my ($src, $edst) = @_;
271         PublicInbox::Import::init_bare($edst);
272         my $f = "$edst/config";
273         open my $fh, '>>', $f or die "open($f): $!";
274         print $fh <<EOM or die "print($f): $!";
275 [remote "origin"]
276         url = $src
277         fetch = +refs/*:refs/*
278         mirror = true
279
280 ; This git epoch was created read-only and "public-inbox-fetch"
281 ; will not fetch updates for it unless write permission is added.
282 EOM
283         close $fh or die "close:($f): $!";
284 }
285
286 sub clone_v2 ($$;$) {
287         my ($self, $v2_epochs, $m) = @_; # $m => manifest.js.gz hashref
288         my $lei = $self->{lei};
289         my $curl = $self->{curl} //= PublicInbox::LeiCurl->new($lei) or return;
290         my $first_uri = (map { $_->[0] } values %$v2_epochs)[0];
291         my $pfx = $curl->torsocks($lei, $first_uri) or return;
292         my $dst = $self->{cur_dst} // $self->{dst};
293         my $want = parse_epochs($lei->{opt}->{epoch}, $v2_epochs);
294         my (@src_edst, @read_only, @skip);
295         for my $nr (sort { $a <=> $b } keys %$v2_epochs) {
296                 my ($uri, $key) = @{$v2_epochs->{$nr}};
297                 my $src = $uri->as_string;
298                 my $edst = $dst;
299                 $src =~ m!/([0-9]+)(?:\.git)?\z! or die <<"";
300 failed to extract epoch number from $src
301
302                 $1 + 0 == $nr or die "BUG: <$uri> miskeyed $1 != $nr";
303                 $edst .= "/git/$nr.git";
304                 if (!$want || $want->{$nr}) {
305                         push @src_edst, $src, $edst;
306                 } else { # create a placeholder so users only need to chmod +w
307                         init_placeholder($src, $edst);
308                         push @read_only, $edst;
309                         push @skip, $key;
310                 }
311         }
312         # filter out the epochs we skipped
313         $self->{-culled_manifest} = 1 if delete(@$m{@skip});
314         my $lk = bless { lock_path => "$dst/inbox.lock" }, 'PublicInbox::Lock';
315         _try_config($self);
316         my $on_destroy = $lk->lock_for_scope($$);
317         my @cmd = clone_cmd($lei, my $opt = {});
318         while (my ($src, $edst) = splice(@src_edst, 0, 2)) {
319                 my $cmd = [ @$pfx, @cmd, $src, $edst ];
320                 my $cerr = run_reap($lei, $cmd, $opt);
321                 return $lei->child_error($cerr, "@$cmd failed") if $cerr;
322         }
323         require PublicInbox::MultiGit;
324         my $mg = PublicInbox::MultiGit->new($dst, 'all.git', 'git');
325         $mg->fill_alternates;
326         for my $i ($mg->git_epochs) { $mg->epoch_cfg_set($i) }
327         for my $edst (@read_only) {
328                 my @st = stat($edst) or die "stat($edst): $!";
329                 chmod($st[2] & 0555, $edst) or die "chmod(a-w, $edst): $!";
330         }
331         write_makefile($dst, 2);
332         undef $on_destroy; # unlock
333         index_cloned_inbox($self, 2);
334 }
335
336 sub decode_manifest ($$$) {
337         my ($fh, $fn, $uri) = @_;
338         my $js;
339         my $gz = do { local $/; <$fh> } // die "slurp($fn): $!";
340         gunzip(\$gz => \$js, MultiStream => 1) or
341                 die "gunzip($uri): $GunzipError\n";
342         my $m = eval { PublicInbox::Config->json->decode($js) };
343         die "$uri: error decoding `$js': $@\n" if $@;
344         ref($m) eq 'HASH' or die "$uri unknown type: ".ref($m);
345         $m;
346 }
347
348 sub multi_inbox ($$$) {
349         my ($self, $path, $m) = @_;
350         my $incl = $self->{lei}->{opt}->{include};
351         my $excl = $self->{lei}->{opt}->{exclude};
352
353         # assuming everything not v2 is v1, for now
354         my @v1 = sort grep(!m!.+/git/[0-9]+\.git\z!, keys %$m);
355         my @v2_epochs = sort grep(m!.+/git/[0-9]+\.git\z!, keys %$m);
356         my $v2 = {};
357
358         for (@v2_epochs) {
359                 m!\A(/.+)/git/[0-9]+\.git\z! or die "BUG: $_";
360                 push @{$v2->{$1}}, $_;
361         }
362         my $n = scalar(keys %$v2) + scalar(@v1);
363         my @orig = defined($incl // $excl) ? (keys %$v2, @v1) : ();
364         if (defined $incl) {
365                 my $re = '(?:'.join('|', map {
366                                 $self->{lei}->glob2re($_) // qr/\A\Q$_\E\z/
367                         } @$incl).')';
368                 my @gone = delete @$v2{grep(!/$re/, keys %$v2)};
369                 delete @$m{map { @$_ } @gone} and $self->{-culled_manifest} = 1;
370                 delete @$m{grep(!/$re/, @v1)} and $self->{-culled_manifest} = 1;
371                 @v1 = grep(/$re/, @v1);
372         }
373         if (defined $excl) {
374                 my $re = '(?:'.join('|', map {
375                                 $self->{lei}->glob2re($_) // qr/\A\Q$_\E\z/
376                         } @$excl).')';
377                 my @gone = delete @$v2{grep(/$re/, keys %$v2)};
378                 delete @$m{map { @$_ } @gone} and $self->{-culled_manifest} = 1;
379                 delete @$m{grep(/$re/, @v1)} and $self->{-culled_manifest} = 1;
380                 @v1 = grep(!/$re/, @v1);
381         }
382         my $ret; # { v1 => [ ... ], v2 => { "/$inbox_name" => [ epochs ] }}
383         $ret->{v1} = \@v1 if @v1;
384         $ret->{v2} = $v2 if keys %$v2;
385         $ret //= @orig ? "Nothing to clone, available repositories:\n\t".
386                                 join("\n\t", sort @orig)
387                         : "Nothing available to clone\n";
388         my $path_pfx = '';
389
390         # PSGI mount prefixes and manifest.js.gz prefixes don't always align...
391         if (@v2_epochs) {
392                 until (grep(m!\A\Q$$path\E/git/[0-9]+\.git\z!,
393                                 @v2_epochs) == @v2_epochs) {
394                         $$path =~ s!\A(/[^/]+)/!/! or last;
395                         $path_pfx .= $1;
396                 }
397         } elsif (@v1) {
398                 while (!defined($m->{$$path}) && $$path =~ s!\A(/[^/]+)/!/!) {
399                         $path_pfx .= $1;
400                 }
401         }
402         ($path_pfx, $n, $ret);
403 }
404
405 # FIXME: this gets confused by single inbox instance w/ global manifest.js.gz
406 sub try_manifest {
407         my ($self) = @_;
408         my $uri = URI->new($self->{src});
409         my $lei = $self->{lei};
410         my $curl = $self->{curl} //= PublicInbox::LeiCurl->new($lei) or return;
411         my $path = $uri->path;
412         chop($path) eq '/' or die "BUG: $uri not canonicalized";
413         $uri->path($path . '/manifest.js.gz');
414         my $pdir = $lei->rel2abs($self->{dst});
415         $pdir =~ s!/[^/]+/?\z!!;
416         my $ft = File::Temp->new(TEMPLATE => 'm-XXXX',
417                                 UNLINK => 1, DIR => $pdir, SUFFIX => '.tmp');
418         my $fn = $ft->filename;
419         my ($bn) = ($fn =~ m!/([^/]+)\z!);
420         my $cmd = $curl->for_uri($lei, $uri, '-R', '-o', $bn);
421         my $opt = { -C => $pdir };
422         $opt->{$_} = $lei->{$_} for (0..2);
423         my $cerr = run_reap($lei, $cmd, $opt);
424         if ($cerr) {
425                 return try_scrape($self) if ($cerr >> 8) == 22; # 404 missing
426                 return $lei->child_error($cerr, "@$cmd failed");
427         }
428         my $m = eval { decode_manifest($ft, $fn, $uri) };
429         if ($@) {
430                 warn $@;
431                 return try_scrape($self);
432         }
433         my ($path_pfx, $n, $multi) = multi_inbox($self, \$path, $m);
434         return $lei->child_error(1, $multi) if !ref($multi);
435         if (my $v2 = delete $multi->{v2}) {
436                 for my $name (sort keys %$v2) {
437                         my $epochs = delete $v2->{$name};
438                         my %v2_epochs = map {
439                                 $uri->path($n > 1 ? $path_pfx.$path.$_
440                                                 : $path_pfx.$_);
441                                 my ($e) = ("$uri" =~ m!/([0-9]+)\.git\z!);
442                                 $e // die "no [0-9]+\.git in `$uri'";
443                                 $e => [ $uri->clone, $_ ];
444                         } @$epochs;
445                         ("$uri" =~ m!\A(.+/)git/[0-9]+\.git\z!) or
446                                 die "BUG: `$uri' !~ m!/git/[0-9]+.git!";
447                         local $self->{cur_src} = $1;
448                         local $self->{cur_dst} = $self->{dst};
449                         if ($n > 1 && $uri->path =~ m!\A\Q$path_pfx$path\E/(.+)/
450                                                         git/[0-9]+\.git\z!x) {
451                                 $self->{cur_dst} .= "/$1";
452                         }
453                         index($self->{cur_dst}, "\n") >= 0 and die <<EOM;
454 E: `$self->{cur_dst}' must not contain newline
455 EOM
456                         clone_v2($self, \%v2_epochs, $m);
457                 }
458         }
459         if (my $v1 = delete $multi->{v1}) {
460                 my $p = $path_pfx.$path;
461                 chop($p) if substr($p, -1, 1) eq '/';
462                 $uri->path($p);
463                 for my $name (@$v1) {
464                         local $self->{cur_src} = "$uri";
465                         local $self->{cur_dst} = $self->{dst};
466                         if ($n > 1) {
467                                 $self->{cur_dst} .= $name;
468                                 $self->{cur_src} .= $name;
469                         }
470                         index($self->{cur_dst}, "\n") >= 0 and die <<EOM;
471 E: `$self->{cur_dst}' must not contain newline
472 EOM
473                         $self->{cur_src} .= '/';
474                         clone_v1($self, 1);
475                 }
476         }
477         if (delete $self->{-culled_manifest}) { # set by clone_v2/-I/--exclude
478                 # write the smaller manifest if epochs were skipped so
479                 # users won't have to delete manifest if they +w an
480                 # epoch they no longer want to skip
481                 my $json = PublicInbox::Config->json->encode($m);
482                 my $mtime = (stat($fn))[9];
483                 gzip(\$json => $fn) or die "gzip: $GzipError";
484                 utime($mtime, $mtime, $fn) or die "utime(..., $fn): $!";
485         }
486         ft_rename($ft, "$self->{dst}/manifest.js.gz", 0666);
487         open my $x, '>', "$self->{dst}/mirror.done"; # for _wq_done_wait
488 }
489
490 sub start_clone_url {
491         my ($self) = @_;
492         return try_manifest($self) if $self->{src} =~ m!\Ahttps?://!;
493         die "TODO: non-HTTP/HTTPS clone of $self->{src} not supported, yet";
494 }
495
496 sub do_mirror { # via wq_io_do
497         my ($self) = @_;
498         my $lei = $self->{lei};
499         umask($lei->{client_umask}) if defined $lei->{client_umask};
500         eval {
501                 my $iv = $lei->{opt}->{'inbox-version'};
502                 if (defined $iv) {
503                         return clone_v1($self) if $iv == 1;
504                         return try_scrape($self) if $iv == 2;
505                         die "bad --inbox-version=$iv\n";
506                 }
507                 return start_clone_url($self) if $self->{src} =~ m!://!;
508                 die "TODO: cloning local directories not supported, yet";
509         };
510         $lei->fail($@) if $@;
511 }
512
513 sub start {
514         my ($cls, $lei, $src, $dst) = @_;
515         my $self = bless { src => $src, dst => $dst }, $cls;
516         if ($src =~ m!https?://!) {
517                 require URI;
518                 require PublicInbox::LeiCurl;
519         }
520         require PublicInbox::Lock;
521         require PublicInbox::Inbox;
522         require PublicInbox::Admin;
523         require PublicInbox::InboxWritable;
524         $lei->request_umask;
525         my ($op_c, $ops) = $lei->workers_start($self, 1);
526         $lei->{wq1} = $self;
527         $self->wq_io_do('do_mirror', []);
528         $self->wq_close;
529         $lei->wait_wq_events($op_c, $ops);
530 }
531
532 sub ipc_atfork_child {
533         my ($self) = @_;
534         $self->{lei}->_lei_atfork_child;
535         $self->SUPER::ipc_atfork_child;
536 }
537
538 sub write_makefile {
539         my ($dir, $ibx_ver) = @_;
540         my $f = "$dir/Makefile";
541         if (sysopen my $fh, $f, O_CREAT|O_EXCL|O_WRONLY) {
542                 print $fh <<EOM or die "print($f) $!";
543 # This is a v$ibx_ver public-inbox, see the public-inbox-v$ibx_ver-format(5)
544 # manpage for more information on the format.  This Makefile is
545 # intended as a familiar wrapper for users unfamiliar with
546 # public-inbox-* commands.
547 #
548 # See the respective manpages for public-inbox-fetch(1),
549 # public-inbox-index(1), etc for more information on
550 # some of the commands used by this Makefile.
551 #
552 # This Makefile will not be modified nor read by public-inbox,
553 # so you may edit it freely with your own convenience targets
554 # and notes.  public-inbox-fetch will recreate it if removed.
555 EOM
556                 print $fh <<'EOM' or die "print($f): $!";
557 # the default target:
558 help :
559         @echo Common targets:
560         @echo '    make fetch        - fetch from remote git repostorie(s)'
561         @echo '    make update       - fetch and update index '
562         @echo
563         @echo Rarely needed targets:
564         @echo '    make reindex      - may be needed for new features/bugfixes'
565         @echo '    make compact      - rewrite Xapian storage to save space'
566
567 fetch :
568         public-inbox-fetch
569 update :
570         @if ! public-inbox-fetch --exit-code; \
571         then \
572                 c=$$?; \
573                 test $$c -eq 127 && exit 0; \
574                 exit $$c; \
575         elif test -f msgmap.sqlite3 || test -f public-inbox/msgmap.sqlite3; \
576         then \
577                 public-inbox-index; \
578         else \
579                 echo 'public-inbox index not initialized'; \
580                 echo 'see public-inbox-index(1) man page'; \
581         fi
582 reindex :
583         public-inbox-index --reindex
584 compact :
585         public-inbox-compact
586
587 .PHONY : help fetch update reindex compact
588 EOM
589                 close $fh or die "close($f): $!";
590         } else {
591                 die "open($f): $!" unless $!{EEXIST};
592         }
593 }
594
595 1;