]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-purge
admin: beef up resolve_inboxes to handle purge options
[public-inbox.git] / script / public-inbox-purge
1 #!/usr/bin/perl -w
2 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 #
5 # Used for purging messages entirely from a public-inbox.  Currently
6 # supports v2 inboxes only, for now.
7 use strict;
8 use warnings;
9 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
10 use PublicInbox::AdminEdit;
11 PublicInbox::Admin::check_require('-index');
12 require PublicInbox::Filter::Base;
13 require PublicInbox::MIME;
14 require PublicInbox::V2Writable;
15
16 { no warnings 'once'; *REJECT = *PublicInbox::Filter::Base::REJECT }
17
18 my $usage = "$0 [--all] [INBOX_DIRS] </path/to/message";
19 my $opt = { verbose => 1, all => 0, -min_inbox_version => 2 };
20 GetOptions($opt, @PublicInbox::AdminEdit::OPT) or
21         die "bad command-line args\n$usage\n";
22
23 my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt);
24
25 foreach my $ibx (@ibxs) {
26         my $lvl = $ibx->{indexlevel};
27         if (defined $lvl) {
28                 PublicInbox::Admin::indexlevel_ok_or_die($lvl);
29                 next;
30         }
31
32         # Undefined indexlevel, so `full'...
33         # Search::Xapian exists and the DB can be read, at least, fine
34         $ibx->search and next;
35
36         # it's possible for a Xapian directory to exist, but Search::Xapian
37         # to go missing/broken.  Make sure it's purged in that case:
38         $ibx->over or die "no over.sqlite3 in $ibx->{mainrepo}\n";
39
40         # $ibx->{search} is populated by $ibx->over call
41         my $xdir_ro = $ibx->{search}->xdir(1);
42         my $npart = 0;
43         foreach my $part (<$xdir_ro/*>) {
44                 if (-d $part && $part =~ m!/[0-9]+\z!) {
45                         my $bytes = 0;
46                         $bytes += -s $_ foreach glob("$part/*");
47                         $npart++ if $bytes;
48                 }
49         }
50         if ($npart) {
51                 PublicInbox::Admin::require_or_die('-search');
52         } else {
53                 # somebody could "rm -r" all the Xapian directories;
54                 # let them purge the overview, at least
55                 $ibx->{indexlevel} ||= 'basic';
56         }
57 }
58
59 my $data = do { local $/; scalar <STDIN> };
60 $data =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
61 my $n_purged = 0;
62
63 foreach my $ibx (@ibxs) {
64         my $mime = PublicInbox::MIME->new($data);
65         my $v2w = PublicInbox::V2Writable->new($ibx, 0);
66
67         my $commits = $v2w->purge($mime) || [];
68
69         if (my $scrub = $ibx->filter($v2w)) {
70                 my $scrubbed = $scrub->scrub($mime, 1);
71
72                 if ($scrubbed && $scrubbed != REJECT()) {
73                         my $scrub_commits = $v2w->purge($scrubbed);
74                         push @$commits, @$scrub_commits if $scrub_commits;
75                 }
76         }
77
78         $v2w->done;
79
80         if ($opt->{verbose}) { # should we consider this machine-parseable?
81                 print "$ibx->{mainrepo}:";
82                 if (scalar @$commits) {
83                         print join("\n\t", '', @$commits), "\n";
84                 } else {
85                         print " NONE\n";
86                 }
87         }
88         $n_purged += scalar @$commits;
89 }
90
91 # behave like "rm -f"
92 exit(0) if ($opt->{force} || $n_purged);
93
94 warn "Not found\n" if $opt->{verbose};
95 exit(1);