1 # Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # common stuff for administrative command-line tools
5 # Unstable internal API
6 package PublicInbox::Admin;
8 use parent qw(Exporter);
9 our @EXPORT_OK = qw(setup_signals);
10 use PublicInbox::Config;
11 use PublicInbox::Inbox;
12 use PublicInbox::Spawn qw(popen_rd);
14 *rel2abs_collapsed = \&PublicInbox::Config::rel2abs_collapsed;
17 my ($cb, $arg) = @_; # optional
20 # we call exit() here instead of _exit() so DESTROY methods
21 # get called (e.g. File::Temp::Dir and PublicInbox::Msgmap)
22 $SIG{INT} = $SIG{HUP} = $SIG{PIPE} = $SIG{TERM} = sub {
24 # https://www.tldp.org/LDP/abs/html/exitcodes.html
25 eval { $cb->($sig, $arg) } if $cb;
27 exit(128 + POSIX->$sig);
35 while (1) { # favor v2, first
36 if (-f "$try/ei.lock") {
37 return rel2abs_collapsed($try);
40 $root_dev_ino //= do {
41 my @root = stat('/') or die "stat /: $!\n";
44 return undef if "$try[0]\0$try[1]" eq $root_dev_ino;
45 $try .= '/..'; # continue, cd up
47 die "`$try' is not a directory\n";
52 sub resolve_inboxdir {
56 while (1) { # favor v2, first
57 if (-f "$try/inbox.lock") {
59 return rel2abs_collapsed($try);
62 $root_dev_ino //= do {
63 my @root = stat('/') or die "stat /: $!\n";
66 last if "$try[0]\0$try[1]" eq $root_dev_ino;
67 $try .= '/..'; # continue, cd up
69 die "`$try' is not a directory\n";
72 # try v1 bare git dirs
73 my $cmd = [ qw(git rev-parse --git-dir) ];
74 my $fh = popen_rd($cmd, undef, {-C => $cd});
75 my $dir = do { local $/; <$fh> };
76 close $fh or die "error in @$cmd (cwd:${\($cd // '.')}): $!\n";
79 rel2abs_collapsed($dir eq '.' ? ($cd // $dir) : $dir);
82 # for unconfigured inboxes
83 sub detect_indexlevel ($) {
86 my $over = $ibx->over;
87 my $srch = $ibx->search;
88 delete @$ibx{qw(over search)}; # don't leave open FDs lying around
90 # brand new or never before indexed inboxes default to full
91 return 'full' unless $over;
93 return $l unless $srch;
94 if (my $xdb = $srch->xdb) {
96 my $m = $xdb->get_metadata('indexlevel');
101 $ibx->{inboxdir} has unexpected indexlevel in Xapian: $m
104 $ibx->{-skip_docdata} = 1 if $xdb->get_metadata('skip_docdata');
109 sub unconfigured_ibx ($$) {
111 my $name = "unconfigured-$i";
112 PublicInbox::Inbox->new({
114 address => [ "$name\@example.com" ],
116 # consumers (-convert) warn on this:
121 sub resolve_inboxes ($;$$) {
122 my ($argv, $opt, $cfg) = @_;
125 $cfg //= PublicInbox::Config->new;
127 my $cfgfile = PublicInbox::Config::default_file();
128 $cfg or die "--all specified, but $cfgfile not readable\n";
129 @$argv and die "--all specified, but directories specified\n";
131 my (@old, @ibxs, @eidx);
132 if ($opt->{-eidx_ok}) {
133 require PublicInbox::ExtSearchIdx;
137 if (defined(my $ei = resolve_eidxdir($_))) {
138 $ei = PublicInbox::ExtSearchIdx->new($ei, $opt);
146 my $min_ver = $opt->{-min_inbox_version} || 0;
147 # lookup inboxes by st_dev + st_ino instead of {inboxdir} pathnames,
148 # pathnames are not unique due to symlinks and bind mounts
150 $cfg->each_inbox(sub {
152 if (-e $ibx->{inboxdir}) {
153 push(@ibxs, $ibx) if $ibx->version >= $min_ver;
155 warn "W: $ibx->{name} $ibx->{inboxdir}: $!\n";
158 } else { # directories specified on the command-line
160 push @dirs, '.' if !@dirs && $opt->{-use_cwd};
161 my %s2i; # "st_dev\0st_ino" => array index
162 for (my $i = 0; $i <= $#dirs; $i++) {
164 my @st = stat($dir) or die "stat($dir): $!\n";
165 $dir = $dirs[$i] = resolve_inboxdir($dir, \(my $ver));
166 if ($ver >= $min_ver) {
167 $s2i{"$st[0]\0$st[1]"} //= $i;
174 $cfg->each_inbox(sub {
176 return if $ibx->version < $min_ver;
177 my $dir = $ibx->{inboxdir};
178 if (my @s = stat $dir) {
179 my $i = delete($s2i{"$s[0]\0$s[1]"})
182 die $done if !keys(%s2i);
184 warn "W: $ibx->{name} $dir: $!\n";
188 die $@ if $@ && $@ ne $done;
189 for my $i (sort { $a <=> $b } values %s2i) {
190 $ibxs[$i] = unconfigured_ibx($dirs[$i], $i);
192 @ibxs = grep { defined } @ibxs; # duplicates are undef
195 die "-V$min_ver inboxes not supported by $0\n\t",
196 join("\n\t", @old), "\n";
198 $opt->{-eidx_ok} ? (\@ibxs, \@eidx) : @ibxs;
202 my @over_mod = qw(DBD::SQLite DBI);
204 -index => [ @base_mod, @over_mod ],
206 -search => [ @base_mod, @over_mod, 'Search::Xapian' ],
209 sub scan_ibx_modules ($$) {
210 my ($mods, $ibx) = @_;
211 if (!$ibx->{indexlevel} || $ibx->{indexlevel} ne 'basic') {
212 $mods->{'Search::Xapian'} = 1;
214 $mods->{$_} = 1 foreach @over_mod;
221 while (my $mod = shift @mods) {
222 if (my $groups = $mod_groups{$mod}) {
223 push @mods, @$groups;
224 } elsif ($mod eq 'Search::Xapian') {
225 require PublicInbox::Search;
226 PublicInbox::Search::load_xapian() or
227 $err->{'Search::Xapian || Xapian'} = $@;
230 $err->{$mod} = $@ if $@;
233 scalar keys %$err ? $err : undef;
236 sub missing_mod_msg {
238 my @mods = map { "`$_'" } sort keys %$err;
239 my $last = pop @mods;
240 @mods ? (join(', ', @mods)."' and $last") : $last
244 my $err = check_require(@_) or return;
245 die missing_mod_msg($err)." required for $0\n";
248 sub indexlevel_ok_or_die ($) {
249 my ($indexlevel) = @_;
251 if ($indexlevel eq 'basic') {
253 } elsif ($indexlevel =~ /\A(?:medium|full)\z/) {
257 invalid indexlevel=$indexlevel (must be `basic', `medium', or `full')
260 my $err = check_require($req) or return;
261 die missing_mod_msg($err) ." required for indexlevel=$indexlevel\n";
264 sub index_terminate {
265 my (undef, $ibx) = @_; # $_[0] = signal name
270 my ($ibx, $im, $opt) = @_;
271 require PublicInbox::InboxWritable;
272 my $jobs = delete $opt->{jobs} if $opt;
273 if (my $pr = $opt->{-progress}) {
274 $pr->("indexing $ibx->{inboxdir} ...\n");
276 local @SIG{keys %SIG} = values %SIG;
277 setup_signals(\&index_terminate, $ibx);
278 my $idx = { current_info => $ibx->{inboxdir} };
279 local $SIG{__WARN__} = sub {
280 return if PublicInbox::Eml::warn_ignore(@_);
281 warn($idx->{current_info}, ': ', @_);
283 if ($ibx->version == 2) {
284 eval { require PublicInbox::V2Writable };
285 die "v2 requirements not met: $@\n" if $@;
286 $ibx->{-creat_opt}->{nproc} = $jobs;
287 my $v2w = $im // $ibx->importer($opt->{reindex} // $jobs);
290 $v2w->{parallel} = 0;
292 my $n = $v2w->{shards};
293 if ($jobs < ($n + 1) && !$opt->{reshard}) {
295 Unable to respect --jobs=$jobs on index, inbox was created with $n shards
302 require PublicInbox::SearchIdx;
303 $idx = PublicInbox::SearchIdx->new($ibx, 1);
305 $idx->index_sync($opt);
306 $idx->{nidx} // 0; # returns number processed
309 sub progress_prepare ($;$) {
310 my ($opt, $dst) = @_;
312 # public-inbox-index defaults to quiet, -xcpdb and -compact do not
313 if (defined($opt->{quiet}) && $opt->{quiet} < 0) {
314 $opt->{quiet} = !$opt->{verbose};
317 open my $null, '>', '/dev/null' or
318 die "failed to open /dev/null: $!\n";
319 $opt->{1} = $null; # suitable for spawn() redirect
321 $opt->{verbose} ||= 1;
322 $dst //= *STDERR{GLOB};
323 $opt->{-progress} = sub { print $dst @_ };
327 # same unit factors as git:
328 sub parse_unsigned ($) {
331 $$val =~ /\A([0-9]+)([kmg])?\z/i or return;
332 my ($n, $unit_factor) = ($1, $2 // '');
333 my %u = ( k => 1024, m => 1024**2, g => 1024**3 );
334 $$val = $n * ($u{lc($unit_factor)} // 1);
338 sub index_prepare ($$) {
339 my ($opt, $cfg) = @_;
341 if ($opt->{compact}) {
342 require PublicInbox::Xapcmd;
343 PublicInbox::Xapcmd::check_compact();
344 $opt->{compact_opt} = { -coarse_lock => 1, compact => 1 };
345 if (defined(my $jobs = $opt->{jobs})) {
346 $opt->{compact_opt}->{jobs} = $jobs;
349 for my $k (qw(max_size batch_size)) {
350 my $git_key = "publicInbox.index".ucfirst($k);
351 $git_key =~ s/_([a-z])/\U$1/g;
352 defined(my $v = $opt->{$k} // $cfg->{lc($git_key)}) or next;
353 parse_unsigned(\$v) or die "`$git_key=$v' not parsed\n";
354 $v > 0 or die "`$git_key=$v' must be positive\n";
358 # out-of-the-box builds of Xapian 1.4.x are still limited to 32-bit
359 # https://getting-started-with-xapian.readthedocs.io/en/latest/concepts/indexing/limitations.html
360 $opt->{batch_size} and
361 $env = { XAPIAN_FLUSH_THRESHOLD => '4294967295' };
363 for my $k (qw(sequential-shard)) {
364 my $git_key = "publicInbox.index".ucfirst($k);
365 $git_key =~ s/-([a-z])/\U$1/g;
366 defined(my $s = $opt->{$k} // $cfg->{lc($git_key)}) or next;
367 defined(my $v = $cfg->git_bool($s))
368 or die "`$git_key=$s' not boolean\n";
375 my $chdir = $_[0] // return;
376 for my $d (@$chdir) {
377 next if $d eq ''; # same as git(1)
378 chdir $d or die "cd $d: $!";