1 # Copyright (C) 2018-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use PublicInbox::Spawn qw(which);
8 use PublicInbox::TestCommon;
10 require_mods(qw(DBD::SQLite Search::Xapian));
11 which('xapian-compact') or
12 plan skip_all => 'xapian-compact missing for '.__FILE__;
14 use_ok 'PublicInbox::V2Writable';
15 use PublicInbox::Import;
16 my ($tmpdir, $for_destroy) = tmpdir();
18 inboxdir => "$tmpdir/v1",
20 -primary_address => 'test@example.com',
23 ok(PublicInbox::Import::run_die([qw(git init --bare -q), $ibx->{inboxdir}]),
24 'initialized v1 repo');
25 ok(umask(077), 'set restrictive umask');
26 ok(PublicInbox::Import::run_die([qw(git) , "--git-dir=$ibx->{inboxdir}",
27 qw(config core.sharedRepository 0644)]), 'set sharedRepository');
28 $ibx = PublicInbox::Inbox->new($ibx);
29 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
30 my $mime = PublicInbox::MIME->create(
32 From => 'a@example.com',
33 To => 'test@example.com',
34 Subject => 'this is a subject',
35 'Message-ID' => '<a-mid@b>',
36 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
38 body => "hello world\n",
40 ok($im->add($mime), 'added one message');
41 ok($im->remove($mime), 'remove message');
42 ok($im->add($mime), 'added message again');
45 eval { PublicInbox::SearchIdx->new($ibx, 1)->index_sync; };
46 is($@, '', 'no errors syncing');
49 is(((stat("$ibx->{inboxdir}/public-inbox"))[2]) & 07777, 0755,
50 'sharedRepository respected for v1');
51 is(((stat("$ibx->{inboxdir}/public-inbox/msgmap.sqlite3"))[2]) & 07777, 0644,
52 'sharedRepository respected for v1 msgmap');
53 my @xdir = glob("$ibx->{inboxdir}/public-inbox/xap*/*");
56 is($st[2] & 07777, -f _ ? 0644 : 0755,
57 'sharedRepository respected on file after convert');
60 local $ENV{PI_CONFIG} = '/dev/null';
61 my ($out, $err) = ('', '');
62 my $rdr = { 1 => \$out, 2 => \$err };
64 my $cmd = [ '-compact', $ibx->{inboxdir} ];
65 ok(run_script($cmd, undef, $rdr), 'v1 compact works');
67 @xdir = glob("$ibx->{inboxdir}/public-inbox/xap*");
68 is(scalar(@xdir), 1, 'got one xapian directory after compact');
69 is(((stat($xdir[0]))[2]) & 07777, 0755,
70 'sharedRepository respected on v1 compact');
77 ok(defined($hwm) && $hwm > 0, "highwater mark set #$hwm");
79 $cmd = [ '-convert', '--no-index', $ibx->{inboxdir}, "$tmpdir/no-index" ];
80 ok(run_script($cmd, undef, $rdr), 'convert --no-index works');
82 $cmd = [ '-convert', $ibx->{inboxdir}, "$tmpdir/v2" ];
83 ok(run_script($cmd, undef, $rdr), 'convert works');
84 @xdir = glob("$tmpdir/v2/xap*/*");
87 is($st[2] & 07777, -f _ ? 0644 : 0755,
88 'sharedRepository respected after convert');
91 $cmd = [ '-compact', "$tmpdir/v2" ];
92 my $env = { NPROC => 2 };
93 ok(run_script($cmd, $env, $rdr), 'v2 compact works');
94 $ibx->{inboxdir} = "$tmpdir/v2";
96 is($ibx->mm->num_highwater, $hwm, 'highwater mark unchanged in v2 inbox');
98 @xdir = glob("$tmpdir/v2/xap*/*");
101 is($st[2] & 07777, -f _ ? 0644 : 0755,
102 'sharedRepository respected after v2 compact');
104 is(((stat("$tmpdir/v2/msgmap.sqlite3"))[2]) & 07777, 0644,
105 'sharedRepository respected for v2 msgmap');
107 @xdir = (glob("$tmpdir/v2/git/*.git/objects/*/*"),
108 glob("$tmpdir/v2/git/*.git/objects/pack/*"));
111 is($st[2] & 07777, -f _ ? 0444 : 0755,
112 'sharedRepository respected after v2 compact');
114 my $msgs = $ibx->recent({limit => 1000});
115 is($msgs->[0]->{mid}, 'a-mid@b', 'message exists in history');
116 is(scalar @$msgs, 1, 'only one message in history');
120 $cmd = [ qw(-index --reindex -c), "$tmpdir/v2" ];
121 ok(run_script($cmd, undef, $rdr), '--reindex -c');
122 like($err, qr/xapian-compact/, 'xapian-compact ran (-c)');
124 $rdr->{2} = \(my $err2 = '');
125 $cmd = [ qw(-index --reindex -cc), "$tmpdir/v2" ];
126 ok(run_script($cmd, undef, $rdr), '--reindex -c -c');
127 like($err2, qr/xapian-compact/, 'xapian-compact ran (-c -c)');
128 ok(scalar(split(/\n/, $err2)) > scalar(split(/\n/, $err)),