2 # Copyright (C) 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 Xapian));
12 my ($tmpdir, $for_destroy) = tmpdir();
13 my $ibx = create_inbox 'v1', indexlevel => 'medium', tmpdir => "$tmpdir/v1",
16 PublicInbox::Import::init_bare($inboxdir);
17 xsys_e(qw(git) , "--git-dir=$inboxdir",
18 qw(config core.sharedRepository 0644));
22 umask(077) or BAIL_OUT "umask: $!";
23 $_[0] = $im = $ibx->importer(0);
24 my $eml = PublicInbox::Eml->new(<<'EOF');
27 Subject: this is a subject
29 Date: Fri, 02 Oct 1993 00:00:00 +0000
33 $im->add($eml) or BAIL_OUT '->add';
34 $im->remove($eml) or BAIL_OUT '->remove';
35 $im->add($eml) or BAIL_OUT '->add';
37 umask(077) or BAIL_OUT "umask: $!";
38 oct_is(((stat("$ibx->{inboxdir}/public-inbox"))[2]) & 05777, 0755,
39 'sharedRepository respected for v1');
40 oct_is(((stat("$ibx->{inboxdir}/public-inbox/msgmap.sqlite3"))[2]) & 05777,
41 0644, 'sharedRepository respected for v1 msgmap');
42 my @xdir = glob("$ibx->{inboxdir}/public-inbox/xap*/*");
45 oct_is($st[2] & 05777, -f _ ? 0644 : 0755,
46 'sharedRepository respected on file after convert');
49 local $ENV{PI_CONFIG} = '/dev/null';
50 my ($out, $err) = ('', '');
51 my $rdr = { 1 => \$out, 2 => \$err };
53 my $cmd = [ '-compact', $ibx->{inboxdir} ];
54 ok(run_script($cmd, undef, $rdr), 'v1 compact works');
56 @xdir = glob("$ibx->{inboxdir}/public-inbox/xap*");
57 is(scalar(@xdir), 1, 'got one xapian directory after compact');
58 oct_is(((stat($xdir[0]))[2]) & 05777, 0755,
59 'sharedRepository respected on v1 compact');
66 ok(defined($hwm) && $hwm > 0, "highwater mark set #$hwm");
68 $cmd = [ '-convert', '--no-index', $ibx->{inboxdir}, "$tmpdir/no-index" ];
69 ok(run_script($cmd, undef, $rdr), 'convert --no-index works');
71 $cmd = [ '-convert', $ibx->{inboxdir}, "$tmpdir/x/v2" ];
72 ok(run_script($cmd, undef, $rdr), 'convert works');
73 @xdir = glob("$tmpdir/x/v2/xap*/*");
74 for (@xdir) { # TODO: should public-inbox-convert preserve S_ISGID bit?
76 oct_is($st[2] & 07777, -f _ ? 0644 : 0755,
77 'sharedRepository respected after convert');
80 $cmd = [ '-compact', "$tmpdir/x/v2" ];
81 my $env = { NPROC => 2 };
82 ok(run_script($cmd, $env, $rdr), 'v2 compact works');
83 $ibx->{inboxdir} = "$tmpdir/x/v2";
85 is($ibx->mm->num_highwater, $hwm, 'highwater mark unchanged in v2 inbox');
87 @xdir = glob("$tmpdir/x/v2/xap*/*");
90 oct_is($st[2] & 07777, -f _ ? 0644 : 0755,
91 'sharedRepository respected after v2 compact');
93 oct_is(((stat("$tmpdir/x/v2/msgmap.sqlite3"))[2]) & 07777, 0644,
94 'sharedRepository respected for v2 msgmap');
96 @xdir = (glob("$tmpdir/x/v2/git/*.git/objects/*/*"),
97 glob("$tmpdir/x/v2/git/*.git/objects/pack/*"));
100 oct_is($st[2] & 07777, -f _ ? 0444 : 0755,
101 'sharedRepository respected after v2 compact');
103 my $msgs = $ibx->over->recent({limit => 1000});
104 is($msgs->[0]->{mid}, 'a-mid@b', 'message exists in history');
105 is(scalar @$msgs, 1, 'only one message in history');
109 $cmd = [ qw(-index -j0 --reindex -c), "$tmpdir/x/v2" ];
110 ok(run_script($cmd, undef, $rdr), '--reindex -c');
111 like($err, qr/xapian-compact/, 'xapian-compact ran (-c)');
113 $rdr->{2} = \(my $err2 = '');
114 $cmd = [ qw(-index -j0 --reindex -cc), "$tmpdir/x/v2" ];
115 ok(run_script($cmd, undef, $rdr), '--reindex -c -c');
116 like($err2, qr/xapian-compact/, 'xapian-compact ran (-c -c)');
117 ok(($err2 =~ tr/\n/\n/) > ($err =~ tr/\n/\n/), '-compacted twice');