]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/AdminEdit.pm
imap+nntp: share COMPRESS implementation
[public-inbox.git] / lib / PublicInbox / AdminEdit.pm
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>
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! help|h);
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                 require PublicInbox::Search;
31                 my $xdir_ro = PublicInbox::Search->new($ibx)->xdir(1);
32
33                 my $nshard = 0;
34                 foreach my $shard (<$xdir_ro/*>) {
35                         if (-d $shard && $shard =~ m!/[0-9]+\z!) {
36                                 my $bytes = 0;
37                                 $bytes += -s $_ foreach glob("$shard/*");
38                                 $nshard++ if $bytes;
39                         }
40                 }
41                 if ($nshard) {
42                         PublicInbox::Admin::require_or_die('-search');
43                 } else {
44                         # somebody could "rm -r" all the Xapian directories;
45                         # let them purge the overview, at least
46                         $ibx->{indexlevel} ||= 'basic';
47                 }
48         }
49 }
50
51 # takes the output of V2Writable::purge and V2Writable::replace
52 # $rewrites = [ array commits keyed by epoch ]
53 sub show_rewrites ($$$) {
54         my ($fh, $ibx, $rewrites) = @_;
55         print $fh "$ibx->{inboxdir}:";
56         if (scalar @$rewrites) {
57                 my $epoch = -1;
58                 my @out = map {;
59                         ++$epoch;
60                         "$epoch.git: ".(defined($_) ? $_ : '(unchanged)')
61                 } @$rewrites;
62                 print $fh join("\n\t", '', @out), "\n";
63         } else {
64                 print $fh " NONE\n";
65         }
66 }
67
68 1;