2 # Copyright (C) 2018-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use PublicInbox::TestCommon;
8 use PublicInbox::Import;
10 require_mods(qw(DBD::SQLite Search::Xapian));
11 have_xapian_compact or
12 plan skip_all => 'xapian-compact missing for '.__FILE__;
13 my ($tmpdir, $for_destroy) = tmpdir();
14 my $ibx = create_inbox 'v1', indexlevel => 'medium', tmpdir => "$tmpdir/v1",
17 PublicInbox::Import::init_bare($inboxdir);
18 xsys_e(qw(git) , "--git-dir=$inboxdir",
19 qw(config core.sharedRepository 0644));
23 umask(077) or BAIL_OUT "umask: $!";
24 $_[0] = $im = $ibx->importer(0);
25 my $eml = PublicInbox::Eml->new(<<'EOF');
28 Subject: this is a subject
30 Date: Fri, 02 Oct 1993 00:00:00 +0000
34 $im->add($eml) or BAIL_OUT '->add';
35 $im->remove($eml) or BAIL_OUT '->remove';
36 $im->add($eml) or BAIL_OUT '->add';
38 umask(077) or BAIL_OUT "umask: $!";
39 is(((stat("$ibx->{inboxdir}/public-inbox"))[2]) & 07777, 0755,
40 'sharedRepository respected for v1');
41 is(((stat("$ibx->{inboxdir}/public-inbox/msgmap.sqlite3"))[2]) & 07777, 0644,
42 'sharedRepository respected for v1 msgmap');
43 my @xdir = glob("$ibx->{inboxdir}/public-inbox/xap*/*");
46 is($st[2] & 07777, -f _ ? 0644 : 0755,
47 'sharedRepository respected on file after convert');
50 local $ENV{PI_CONFIG} = '/dev/null';
51 my ($out, $err) = ('', '');
52 my $rdr = { 1 => \$out, 2 => \$err };
54 my $cmd = [ '-compact', $ibx->{inboxdir} ];
55 ok(run_script($cmd, undef, $rdr), 'v1 compact works');
57 @xdir = glob("$ibx->{inboxdir}/public-inbox/xap*");
58 is(scalar(@xdir), 1, 'got one xapian directory after compact');
59 is(((stat($xdir[0]))[2]) & 07777, 0755,
60 'sharedRepository respected on v1 compact');
67 ok(defined($hwm) && $hwm > 0, "highwater mark set #$hwm");
69 $cmd = [ '-convert', '--no-index', $ibx->{inboxdir}, "$tmpdir/no-index" ];
70 ok(run_script($cmd, undef, $rdr), 'convert --no-index works');
72 $cmd = [ '-convert', $ibx->{inboxdir}, "$tmpdir/x/v2" ];
73 ok(run_script($cmd, undef, $rdr), 'convert works');
74 @xdir = glob("$tmpdir/x/v2/xap*/*");
77 is($st[2] & 07777, -f _ ? 0644 : 0755,
78 'sharedRepository respected after convert');
81 $cmd = [ '-compact', "$tmpdir/x/v2" ];
82 my $env = { NPROC => 2 };
83 ok(run_script($cmd, $env, $rdr), 'v2 compact works');
84 $ibx->{inboxdir} = "$tmpdir/x/v2";
86 is($ibx->mm->num_highwater, $hwm, 'highwater mark unchanged in v2 inbox');
88 @xdir = glob("$tmpdir/x/v2/xap*/*");
91 is($st[2] & 07777, -f _ ? 0644 : 0755,
92 'sharedRepository respected after v2 compact');
94 is(((stat("$tmpdir/x/v2/msgmap.sqlite3"))[2]) & 07777, 0644,
95 'sharedRepository respected for v2 msgmap');
97 @xdir = (glob("$tmpdir/x/v2/git/*.git/objects/*/*"),
98 glob("$tmpdir/x/v2/git/*.git/objects/pack/*"));
101 is($st[2] & 07777, -f _ ? 0444 : 0755,
102 'sharedRepository respected after v2 compact');
104 my $msgs = $ibx->over->recent({limit => 1000});
105 is($msgs->[0]->{mid}, 'a-mid@b', 'message exists in history');
106 is(scalar @$msgs, 1, 'only one message in history');
110 $cmd = [ qw(-index -j0 --reindex -c), "$tmpdir/x/v2" ];
111 ok(run_script($cmd, undef, $rdr), '--reindex -c');
112 like($err, qr/xapian-compact/, 'xapian-compact ran (-c)');
114 $rdr->{2} = \(my $err2 = '');
115 $cmd = [ qw(-index -j0 --reindex -cc), "$tmpdir/x/v2" ];
116 ok(run_script($cmd, undef, $rdr), '--reindex -c -c');
117 like($err2, qr/xapian-compact/, 'xapian-compact ran (-c -c)');
118 ok(($err2 =~ tr/\n/\n/) > ($err =~ tr/\n/\n/), '-compacted twice');