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>
4 # backend for a git-cat-file-workalike based on libgit2,
5 # other libgit2 stuff may go here, too.
6 package PublicInbox::Gcf2;
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
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): $!";
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";
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> };
36 (my $c, $pid) = popen_rd([$pc, '--cflags', $x], undef, $rdr);
37 $c = do { local $/; <$c> };
41 # note: we name C source files .h to prevent
42 # ExtUtils::MakeMaker from automatically trying to
44 my $f = "$dir/gcf2_$x.h";
45 open(my $src, '<', $f) or die "E: open($f): $!";
48 defined($c_src = <$src>) or die "read $f: $!";
54 seek($err, 0, SEEK_SET);
55 $err = do { local $/; <$err> };
56 die "E: libgit2 not installed: $err\n";
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: $!";
65 # we use Capitalized and ALLCAPS for compatibility with old Inline::C
67 use Inline C => Config => %CFG, BOOT => q[git_libgit2_init();];
68 use Inline C => $c_src, BUILD_NOISY => 1;
71 open(STDERR, '>&', $olderr) or warn "restore stderr: $!";
72 open(STDOUT, '>&', $oldout) or warn "restore stdout: $!";
74 seek($fh, 0, SEEK_SET);
76 die "Inline::C Gcf2 build failed:\n", $err, "\n", @msg;
81 my ($gcf2, $objdir) = @_;
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
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;
96 $gcf2->add_alternate($objdir);
100 sub have_unlinked_files () {
101 # FIXME: port gcf2-like over to git.git so we won't need to
103 return 1 if $^O ne 'linux';
104 open my $fh, '<', "/proc/$$/maps" or return;
105 while (<$fh>) { return 1 if /\.(?:idx|pack) \(deleted\)$/ }
109 # Usage: $^X -MPublicInbox::Gcf2 -e PublicInbox::Gcf2::loop [EXPIRE-TIMEOUT]
110 # (see lib/PublicInbox/Gcf2Client.pm)
112 my $exp = $_[0] || $ARGV[0] || 60; # seconds
114 my (%seen, $check_at);
115 STDERR->autoflush(1);
116 STDOUT->autoflush(1);
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";
129 %seen = ($git_dir => add_alt($gcf2,"$git_dir/objects"));
130 $check_at = clock_gettime(CLOCK_MONOTONIC) + $exp;
132 if ($gcf2->cat_oid(1, $oid)) {
133 warn "I: $$ $oid found after retry\n";
135 warn "W: $$ $oid missing after retry\n";
136 print "$oid missing\n"; # mimic git-cat-file
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 && have_unlinked_files()) {