]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Gcf2.pm
gcf2: avoid excessive checks for unlinked files
[public-inbox.git] / lib / PublicInbox / Gcf2.pm
1 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # backend for a git-cat-file-workalike based on libgit2,
5 # other libgit2 stuff may go here, too.
6 package PublicInbox::Gcf2;
7 use strict;
8 use v5.10.1;
9 use PublicInbox::Spawn qw(which popen_rd); # may set PERL_INLINE_DIRECTORY
10 use Fcntl qw(LOCK_EX SEEK_SET);
11 use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
12 use IO::Handle; # autoflush
13 BEGIN {
14         my (%CFG, $c_src);
15         # PublicInbox::Spawn will set PERL_INLINE_DIRECTORY
16         # to ~/.cache/public-inbox/inline-c if it exists
17         my $inline_dir = $ENV{PERL_INLINE_DIRECTORY} //
18                 die 'PERL_INLINE_DIRECTORY not defined';
19         my $f = "$inline_dir/.public-inbox.lock";
20         open my $fh, '+>', $f or die "open($f): $!";
21
22         # CentOS 7.x ships Inline 0.53, 0.64+ has built-in locking
23         flock($fh, LOCK_EX) or die "LOCK_EX($f): $!\n";
24
25         my $pc = which($ENV{PKG_CONFIG} // 'pkg-config') //
26                 die "pkg-config missing for libgit2";
27         my ($dir) = (__FILE__ =~ m!\A(.+?)/[^/]+\z!);
28         my $ef = "$inline_dir/.public-inbox.pkg-config.err";
29         open my $err, '+>', $ef or die "open($ef): $!";
30         for my $x (qw(libgit2)) {
31                 my $rdr = { 2 => $err };
32                 my ($l, $pid) = popen_rd([$pc, '--libs', $x], undef, $rdr);
33                 $l = do { local $/; <$l> };
34                 waitpid($pid, 0);
35                 next if $?;
36                 (my $c, $pid) = popen_rd([$pc, '--cflags', $x], undef, $rdr);
37                 $c = do { local $/; <$c> };
38                 waitpid($pid, 0);
39                 next if $?;
40
41                 # note: we name C source files .h to prevent
42                 # ExtUtils::MakeMaker from automatically trying to
43                 # build them.
44                 my $f = "$dir/gcf2_$x.h";
45                 open(my $src, '<', $f) or die "E: open($f): $!";
46                 chomp($l, $c);
47                 local $/;
48                 defined($c_src = <$src>) or die "read $f: $!";
49                 $CFG{LIBS} = $l;
50                 $CFG{CCFLAGSEX} = $c;
51                 last;
52         }
53         unless ($c_src) {
54                 seek($err, 0, SEEK_SET);
55                 $err = do { local $/; <$err> };
56                 die "E: libgit2 not installed: $err\n";
57         }
58         open my $oldout, '>&', \*STDOUT or die "dup(1): $!";
59         open my $olderr, '>&', \*STDERR or die "dup(2): $!";
60         open STDOUT, '>&', $fh or die "1>$f: $!";
61         open STDERR, '>&', $fh or die "2>$f: $!";
62         STDERR->autoflush(1);
63         STDOUT->autoflush(1);
64
65         # we use Capitalized and ALLCAPS for compatibility with old Inline::C
66         eval <<'EOM';
67 use Inline C => Config => %CFG, BOOT => q[git_libgit2_init();];
68 use Inline C => $c_src, BUILD_NOISY => 1;
69 EOM
70         $err = $@;
71         open(STDERR, '>&', $olderr) or warn "restore stderr: $!";
72         open(STDOUT, '>&', $oldout) or warn "restore stdout: $!";
73         if ($err) {
74                 seek($fh, 0, SEEK_SET);
75                 my @msg = <$fh>;
76                 die "Inline::C Gcf2 build failed:\n", $err, "\n", @msg;
77         }
78 }
79
80 sub add_alt ($$) {
81         my ($gcf2, $objdir) = @_;
82
83         # libgit2 (tested 0.27.7+dfsg.1-0.2 and 0.28.3+dfsg.1-1~bpo10+1
84         # in Debian) doesn't handle relative epochs properly when nested
85         # multiple levels.  Add all the absolute paths to workaround it,
86         # since $EXTINDEX_DIR/ALL.git/objects/info/alternates uses absolute
87         # paths to reference $V2INBOX_DIR/all.git/objects and
88         # $V2INBOX_DIR/all.git/objects/info/alternates uses relative paths
89         # to refer to $V2INBOX_DIR/git/$EPOCH.git/objects
90         #
91         # See https://bugs.debian.org/975607
92         if (open(my $fh, '<', "$objdir/info/alternates")) {
93                 chomp(my @abs_alt = grep(m!^/!, <$fh>));
94                 $gcf2->add_alternate($_) for @abs_alt;
95         }
96         $gcf2->add_alternate($objdir);
97         1;
98 }
99
100 sub have_unlinked_files () {
101         # FIXME: port gcf2-like over to git.git so we won't need to
102         # deal with libgit2
103         return 1 if $^O ne 'linux';
104         open my $fh, '<', "/proc/$$/maps" or return;
105         while (<$fh>) { return 1 if /\.(?:idx|pack) \(deleted\)$/ }
106         undef;
107 }
108
109 # Usage: $^X -MPublicInbox::Gcf2 -e PublicInbox::Gcf2::loop [EXPIRE-TIMEOUT]
110 # (see lib/PublicInbox/Gcf2Client.pm)
111 sub loop (;$) {
112         my $exp = $_[0] || $ARGV[0] || 60; # seconds
113         my $gcf2 = new();
114         my (%seen, $check_at);
115         STDERR->autoflush(1);
116         STDOUT->autoflush(1);
117
118         while (<STDIN>) {
119                 chomp;
120                 my ($oid, $git_dir) = split(/ /, $_, 2);
121                 $seen{$git_dir} //= add_alt($gcf2, "$git_dir/objects");
122                 if (!$gcf2->cat_oid(1, $oid)) {
123                         # retry once if missing.  We only get unabbreviated OIDs
124                         # from SQLite or Xapian DBs, here, so malicious clients
125                         # can't trigger excessive retries:
126                         warn "I: $$ $oid missing, retrying in $git_dir\n";
127
128                         $gcf2 = new();
129                         %seen = ($git_dir => add_alt($gcf2,"$git_dir/objects"));
130                         $check_at = clock_gettime(CLOCK_MONOTONIC) + $exp;
131
132                         if ($gcf2->cat_oid(1, $oid)) {
133                                 warn "I: $$ $oid found after retry\n";
134                         } else {
135                                 warn "W: $$ $oid missing after retry\n";
136                                 print "$oid missing\n"; # mimic git-cat-file
137                         }
138                 } else { # check expiry to deal with deleted pack files
139                         my $now = clock_gettime(CLOCK_MONOTONIC);
140                         $check_at //= $now + $exp;
141                         if ($now > $check_at) {
142                                 undef $check_at;
143                                 if (have_unlinked_files()) {
144                                         $gcf2 = new();
145                                         %seen = ();
146                                 }
147                         }
148                 }
149         }
150 }
151
152 1;