]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-compact
convert+compact: fix when running without ~/.public-inbox/config
[public-inbox.git] / script / public-inbox-compact
1 #!/usr/bin/perl -w
2 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use warnings;
6 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
7 use PublicInbox::Search;
8 use PublicInbox::Config;
9 use PublicInbox::InboxWritable;
10 use Cwd 'abs_path';
11 use File::Temp qw(tempdir);
12 use File::Path qw(remove_tree);
13 use PublicInbox::Spawn qw(spawn);
14 my $usage = "Usage: public-inbox-compact REPO_DIR\n";
15 my $dir = shift or die $usage;
16 my $config = eval { PublicInbox::Config->new };
17 my $ibx;
18 $dir = abs_path($dir);
19 if ($config) {
20         $config->each_inbox(sub {
21                 $ibx = $_[0] if abs_path($_[0]->{mainrepo}) eq $dir
22         });
23 }
24 unless ($ibx) {
25         warn "W: $dir not configured in ".
26                 PublicInbox::Config::default_file() . "\n";
27         $ibx = {
28                 mainrepo => $dir,
29                 name => 'ignored',
30                 address => [ 'old@example.com' ],
31         };
32         $ibx = PublicInbox::Inbox->new($ibx);
33 }
34 my $v = ($ibx->{version} || 1);
35 $ibx = PublicInbox::InboxWritable->new($ibx);
36 $ibx->umask_prepare;
37
38 sub commit_changes ($$$) {
39         my ($im, $old, $new) = @_;
40         my @st = stat($old) or die "failed to stat($old): $!\n";
41
42         my $over = "$old/over.sqlite3";
43         if (-f $over) {
44                 require PublicInbox::Over;
45                 $over = PublicInbox::Over->new($over);
46                 $over->connect->sqlite_backup_to_file("$new/over.sqlite3");
47         }
48         rename($old, "$new/old") or die "rename $old => $new/old: $!\n";
49         chmod($st[2] & 07777, $new) or die "chmod $old: $!\n";
50         rename($new, $old) or die "rename $new => $old: $!\n";
51         $im->lock_release;
52         remove_tree("$old/old") or die "failed to remove $old/old: $!\n";
53 }
54 my @compact = qw(xapian-compact --no-renumber);
55 if ($v == 2) {
56         require PublicInbox::V2Writable;
57         my $v2w = PublicInbox::V2Writable->new($ibx);
58         my $xap_v = 'xap'.PublicInbox::Search::SCHEMA_VERSION;
59         my $old = "$dir/$xap_v";
60         opendir my $dh, $old or die "Failed to opendir $old: $!\n";
61         my $new = tempdir('compact-XXXXXXXX', CLEANUP => 1, DIR => $dir);
62         $ibx->with_umask(sub {
63                 $v2w->lock_acquire;
64                 my %pids;
65                 while (defined(my $dn = readdir($dh))) {
66                         if ($dn =~ /\A\d+\z/) {
67                                 my $cmd = [ @compact, "$old/$dn", "$new/$dn" ];
68                                 $pids{spawn($cmd)} = join(' ', @$cmd);
69                         } elsif ($dn eq '.' || $dn eq '..') {
70                         } elsif ($dn =~ /\Aover\.sqlite3/) {
71                         } else {
72                                 warn "W: skipping unknown Xapian DB: $old/$dn\n"
73                         }
74                 }
75                 close $dh;
76                 die "No Xapian parts found in $old\n" unless keys %pids;
77                 while (scalar keys %pids) {
78                         my $pid = waitpid(-1, 0);
79                         my $desc = delete $pids{$pid};
80                         die "$desc failed: $?\n" if $?;
81                 }
82                 commit_changes($v2w, $old, $new);
83         });
84 } elsif ($v == 1) {
85         require PublicInbox::Import;
86         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
87         my $xap_v = 'xapian'.PublicInbox::Search::SCHEMA_VERSION;
88         my $v1_root = "$dir/public-inbox";
89         my $old = "$v1_root/$xap_v";
90         -d $old or die "$old does not exist\n";
91         my $new = tempdir('compact-XXXXXXXX', CLEANUP => 1, DIR => $v1_root);
92         $ibx->with_umask(sub {
93                 $im->lock_acquire;
94                 PublicInbox::Import::run_die([@compact, $old, $new]);
95                 commit_changes($im, $old, $new);
96         });
97 } else {
98         die "Unsupported inbox version: $v\n";
99 }