]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-compact
v2: respect core.sharedRepository in git configs
[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::V2Writable;
8 use PublicInbox::Search;
9 use PublicInbox::Config;
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 = PublicInbox::Config->new;
17 my $ibx;
18 $dir = abs_path($dir);
19 $config->each_inbox(sub {
20         $ibx = $_[0] if abs_path($_[0]->{mainrepo}) eq $dir
21 });
22 unless ($ibx) {
23         warn "W: $dir not configured in ".
24                 PublicInbox::Config::default_file() . "\n";
25         $ibx = {
26                 mainrepo => $dir,
27                 name => 'ignored',
28                 address => [ 'old@example.com' ],
29         };
30         $ibx = PublicInbox::Inbox->new($ibx);
31 }
32 my $v = ($ibx->{version} || 1);
33 $ibx = PublicInbox::InboxWritable->new($ibx);
34 $ibx->umask_prepare;
35
36 sub commit_changes ($$$) {
37         my ($im, $old, $new) = @_;
38         my @st = stat($old) or die "failed to stat($old): $!\n";
39         rename($old, "$new/old") or die "rename $old => $new/old: $!\n";
40         chmod($st[2] & 07777, $new) or die "chmod $old: $!\n";
41         rename($new, $old) or die "rename $new => $old: $!\n";
42         $im->lock_release;
43         remove_tree("$old/old") or die "failed to remove $old/old: $!\n";
44 }
45
46 if ($v == 2) {
47         require PublicInbox::V2Writable;
48         my $v2w = PublicInbox::V2Writable->new($ibx);
49         my $xap_v = 'xap'.PublicInbox::Search::SCHEMA_VERSION;
50         my $old = "$dir/$xap_v";
51         opendir my $dh, $old or die "Failed to opendir $old: $!\n";
52         my $new = tempdir('compact-XXXXXXXX', CLEANUP => 1, DIR => $dir);
53         $ibx->with_umask(sub {
54                 $v2w->lock_acquire;
55                 my @parts;
56                 my $skel;
57                 while (defined(my $dn = readdir($dh))) {
58                         if ($dn =~ /\A\d+\z/) {
59                                 push @parts, "$old/$dn";
60                         } elsif ($dn eq 'skel') {
61                                 $skel = "$old/$dn";
62                         } elsif ($dn eq '.' || $dn eq '..') {
63                         } else {
64                                 warn "W: skipping unknown Xapian DB: $old/$dn\n"
65                         }
66                 }
67                 close $dh;
68                 my %pids;
69
70                 if (@parts) {
71                         my $pid = spawn(['xapian-compact', @parts, "$new/0" ]);
72                         defined $pid or die "compact failed: $?\n";
73                         $pids{$pid} = 'xapian-compact (parts)';
74                 } else {
75                         warn "No parts found in $old\n";
76                 }
77                 if (defined $skel) {
78                         my $pid = spawn(['xapian-compact', $skel, "$new/skel"]);
79                         defined $pid or die "compact failed: $?\n";
80                         $pids{$pid} = 'xapian-compact (skel)';
81                 } else {
82                         warn "$old/skel missing\n";
83                 }
84                 scalar keys %pids or
85                         die "No xapian-compact processes running\n";
86                 while (scalar keys %pids) {
87                         my $pid = waitpid(-1, 0);
88                         my $desc = delete $pids{$pid};
89                         die "$desc failed: $?\n" if $?;
90                 }
91                 commit_changes($v2w, $old, $new);
92         });
93 } elsif ($v == 1) {
94         require PublicInbox::Import;
95         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
96         my $xap_v = 'xapian'.PublicInbox::Search::SCHEMA_VERSION;
97         my $v1_root = "$dir/public-inbox";
98         my $old = "$v1_root/$xap_v";
99         -d $old or die "$old does not exist\n";
100         my $new = tempdir('compact-XXXXXXXX', CLEANUP => 1, DIR => $v1_root);
101         $ibx->with_umask(sub {
102                 $im->lock_acquire;
103                 PublicInbox::Import::run_die(['xapian-compact', $old, $new]);
104                 commit_changes($im, $old, $new);
105         });
106 } else {
107         die "Unsupported inbox version: $v\n";
108 }