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::TestCommon;
9 require_mods(qw(DBD::SQLite Search::Xapian));
10 have_xapian_compact or
11 plan skip_all => 'xapian-compact missing for '.__FILE__;
13 use_ok 'PublicInbox::V2Writable';
14 use PublicInbox::Import;
15 my ($tmpdir, $for_destroy) = tmpdir();
17 inboxdir => "$tmpdir/v1",
19 -primary_address => 'test@example.com',
22 PublicInbox::Import::init_bare($ibx->{inboxdir});
23 ok(umask(077), 'set restrictive umask');
24 xsys_e(qw(git) , "--git-dir=$ibx->{inboxdir}",
25 qw(config core.sharedRepository 0644));
26 $ibx = PublicInbox::Inbox->new($ibx);
27 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
28 my $mime = PublicInbox::Eml->new(<<'EOF');
31 Subject: this is a subject
33 Date: Fri, 02 Oct 1993 00:00:00 +0000
38 ok($im->add($mime), 'added one message');
39 ok($im->remove($mime), 'remove message');
40 ok($im->add($mime), 'added message again');
43 eval { PublicInbox::SearchIdx->new($ibx, 1)->index_sync; };
44 is($@, '', 'no errors syncing');
47 is(((stat("$ibx->{inboxdir}/public-inbox"))[2]) & 07777, 0755,
48 'sharedRepository respected for v1');
49 is(((stat("$ibx->{inboxdir}/public-inbox/msgmap.sqlite3"))[2]) & 07777, 0644,
50 'sharedRepository respected for v1 msgmap');
51 my @xdir = glob("$ibx->{inboxdir}/public-inbox/xap*/*");
54 is($st[2] & 07777, -f _ ? 0644 : 0755,
55 'sharedRepository respected on file after convert');
58 local $ENV{PI_CONFIG} = '/dev/null';
59 my ($out, $err) = ('', '');
60 my $rdr = { 1 => \$out, 2 => \$err };
62 my $cmd = [ '-compact', $ibx->{inboxdir} ];
63 ok(run_script($cmd, undef, $rdr), 'v1 compact works');
65 @xdir = glob("$ibx->{inboxdir}/public-inbox/xap*");
66 is(scalar(@xdir), 1, 'got one xapian directory after compact');
67 is(((stat($xdir[0]))[2]) & 07777, 0755,
68 'sharedRepository respected on v1 compact');
75 ok(defined($hwm) && $hwm > 0, "highwater mark set #$hwm");
77 $cmd = [ '-convert', '--no-index', $ibx->{inboxdir}, "$tmpdir/no-index" ];
78 ok(run_script($cmd, undef, $rdr), 'convert --no-index works');
80 $cmd = [ '-convert', $ibx->{inboxdir}, "$tmpdir/x/v2" ];
81 ok(run_script($cmd, undef, $rdr), 'convert works');
82 @xdir = glob("$tmpdir/x/v2/xap*/*");
85 is($st[2] & 07777, -f _ ? 0644 : 0755,
86 'sharedRepository respected after convert');
89 $cmd = [ '-compact', "$tmpdir/x/v2" ];
90 my $env = { NPROC => 2 };
91 ok(run_script($cmd, $env, $rdr), 'v2 compact works');
92 $ibx->{inboxdir} = "$tmpdir/x/v2";
94 is($ibx->mm->num_highwater, $hwm, 'highwater mark unchanged in v2 inbox');
96 @xdir = glob("$tmpdir/x/v2/xap*/*");
99 is($st[2] & 07777, -f _ ? 0644 : 0755,
100 'sharedRepository respected after v2 compact');
102 is(((stat("$tmpdir/x/v2/msgmap.sqlite3"))[2]) & 07777, 0644,
103 'sharedRepository respected for v2 msgmap');
105 @xdir = (glob("$tmpdir/x/v2/git/*.git/objects/*/*"),
106 glob("$tmpdir/x/v2/git/*.git/objects/pack/*"));
109 is($st[2] & 07777, -f _ ? 0444 : 0755,
110 'sharedRepository respected after v2 compact');
112 my $msgs = $ibx->recent({limit => 1000});
113 is($msgs->[0]->{mid}, 'a-mid@b', 'message exists in history');
114 is(scalar @$msgs, 1, 'only one message in history');
118 $cmd = [ qw(-index -j0 --reindex -c), "$tmpdir/x/v2" ];
119 ok(run_script($cmd, undef, $rdr), '--reindex -c');
120 like($err, qr/xapian-compact/, 'xapian-compact ran (-c)');
122 $rdr->{2} = \(my $err2 = '');
123 $cmd = [ qw(-index -j0 --reindex -cc), "$tmpdir/x/v2" ];
124 ok(run_script($cmd, undef, $rdr), '--reindex -c -c');
125 like($err2, qr/xapian-compact/, 'xapian-compact ran (-c -c)');
126 ok(($err2 =~ tr/\n/\n/) > ($err =~ tr/\n/\n/), '-compacted twice');