]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/AdminEdit.pm
treewide: run update-copyrights from gnulib for 2019
[public-inbox.git] / lib / PublicInbox / AdminEdit.pm
1 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # common stuff between -edit, -purge (and maybe -learn in the future)
5 package PublicInbox::AdminEdit;
6 use strict;
7 use warnings;
8 use PublicInbox::Admin;
9 our @OPT = qw(all force|f verbose|v!);
10
11 sub check_editable ($) {
12         my ($ibxs) = @_;
13
14         foreach my $ibx (@$ibxs) {
15                 my $lvl = $ibx->{indexlevel};
16                 if (defined $lvl) {
17                         PublicInbox::Admin::indexlevel_ok_or_die($lvl);
18                         next;
19                 }
20
21                 # Undefined indexlevel, so `full'...
22                 # Search::Xapian exists and the DB can be read, at least, fine
23                 $ibx->search and next;
24
25                 # it's possible for a Xapian directory to exist,
26                 # but Search::Xapian to go missing/broken.
27                 # Make sure it's purged in that case:
28                 $ibx->over or die "no over.sqlite3 in $ibx->{inboxdir}\n";
29
30                 # $ibx->{search} is populated by $ibx->over call
31                 my $xdir_ro = $ibx->{search}->xdir(1);
32                 my $nshard = 0;
33                 foreach my $shard (<$xdir_ro/*>) {
34                         if (-d $shard && $shard =~ m!/[0-9]+\z!) {
35                                 my $bytes = 0;
36                                 $bytes += -s $_ foreach glob("$shard/*");
37                                 $nshard++ if $bytes;
38                         }
39                 }
40                 if ($nshard) {
41                         PublicInbox::Admin::require_or_die('-search');
42                 } else {
43                         # somebody could "rm -r" all the Xapian directories;
44                         # let them purge the overview, at least
45                         $ibx->{indexlevel} ||= 'basic';
46                 }
47         }
48 }
49
50 # takes the output of V2Writable::purge and V2Writable::replace
51 # $rewrites = [ array commits keyed by epoch ]
52 sub show_rewrites ($$$) {
53         my ($fh, $ibx, $rewrites) = @_;
54         print $fh "$ibx->{inboxdir}:";
55         if (scalar @$rewrites) {
56                 my $epoch = -1;
57                 my @out = map {;
58                         ++$epoch;
59                         "$epoch.git: ".(defined($_) ? $_ : '(unchanged)')
60                 } @$rewrites;
61                 print $fh join("\n\t", '', @out), "\n";
62         } else {
63                 print $fh " NONE\n";
64         }
65 }
66
67 1;