]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Gcf2.pm
gcf2: fix loading at runtime
[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 IO::Handle; # autoflush
12 BEGIN {
13         my (%CFG, $c_src);
14         # PublicInbox::Spawn will set PERL_INLINE_DIRECTORY
15         # to ~/.cache/public-inbox/inline-c if it exists
16         my $inline_dir = $ENV{PERL_INLINE_DIRECTORY} //
17                 die 'PERL_INLINE_DIRECTORY not defined';
18         my $f = "$inline_dir/.public-inbox.lock";
19         open my $fh, '+>', $f or die "open($f): $!";
20
21         # CentOS 7.x ships Inline 0.53, 0.64+ has built-in locking
22         flock($fh, LOCK_EX) or die "LOCK_EX($f): $!\n";
23
24         my $pc = which($ENV{PKG_CONFIG} // 'pkg-config') //
25                 die "pkg-config missing for libgit2";
26         my ($dir) = (__FILE__ =~ m!\A(.+?)/[^/]+\z!);
27         my $ef = "$inline_dir/.public-inbox.pkg-config.err";
28         open my $err, '+>', $ef or die "open($ef): $!";
29         for my $x (qw(libgit2)) {
30                 my $rdr = { 2 => $err };
31                 my ($l, $pid) = popen_rd([$pc, '--libs', $x], undef, $rdr);
32                 $l = do { local $/; <$l> };
33                 waitpid($pid, 0);
34                 next if $?;
35                 (my $c, $pid) = popen_rd([$pc, '--cflags', $x], undef, $rdr);
36                 $c = do { local $/; <$c> };
37                 waitpid($pid, 0);
38                 next if $?;
39
40                 # note: we name C source files .h to prevent
41                 # ExtUtils::MakeMaker from automatically trying to
42                 # build them.
43                 my $f = "$dir/gcf2_$x.h";
44                 open(my $src, '<', $f) or die "E: open($f): $!";
45                 chomp($l, $c);
46                 local $/;
47                 defined($c_src = <$src>) or die "read $f: $!";
48                 $CFG{LIBS} = $l;
49                 $CFG{CCFLAGSEX} = $c;
50                 last;
51         }
52         unless ($c_src) {
53                 seek($err, 0, SEEK_SET);
54                 $err = do { local $/; <$err> };
55                 die "E: libgit2 not installed: $err\n";
56         }
57         open my $oldout, '>&', \*STDOUT or die "dup(1): $!";
58         open my $olderr, '>&', \*STDERR or die "dup(2): $!";
59         open STDOUT, '>&', $fh or die "1>$f: $!";
60         open STDERR, '>&', $fh or die "2>$f: $!";
61         STDERR->autoflush(1);
62         STDOUT->autoflush(1);
63
64         # we use Capitalized and ALLCAPS for compatibility with old Inline::C
65         eval <<'EOM';
66 use Inline C => Config => %CFG, BOOT => q[git_libgit2_init();];
67 use Inline C => $c_src, BUILD_NOISY => 1;
68 EOM
69         $err = $@;
70         open(STDERR, '>&', $olderr) or warn "restore stderr: $!";
71         open(STDOUT, '>&', $oldout) or warn "restore stdout: $!";
72         if ($err) {
73                 seek($fh, 0, SEEK_SET);
74                 my @msg = <$fh>;
75                 die "Inline::C Gcf2 build failed:\n", $err, "\n", @msg;
76         }
77 }
78
79 sub add_alt ($$) {
80         my ($gcf2, $objdir) = @_;
81
82         # libgit2 (tested 0.27.7+dfsg.1-0.2 and 0.28.3+dfsg.1-1~bpo10+1
83         # in Debian) doesn't handle relative epochs properly when nested
84         # multiple levels.  Add all the absolute paths to workaround it,
85         # since $EXTINDEX_DIR/ALL.git/objects/info/alternates uses absolute
86         # paths to reference $V2INBOX_DIR/all.git/objects and
87         # $V2INBOX_DIR/all.git/objects/info/alternates uses relative paths
88         # to refer to $V2INBOX_DIR/git/$EPOCH.git/objects
89         #
90         # See https://bugs.debian.org/975607
91         if (open(my $fh, '<', "$objdir/info/alternates")) {
92                 chomp(my @abs_alt = grep(m!^/!, <$fh>));
93                 $gcf2->add_alternate($_) for @abs_alt;
94         }
95         $gcf2->add_alternate($objdir);
96         1;
97 }
98
99 # Usage: $^X -MPublicInbox::Gcf2 -e PublicInbox::Gcf2::loop
100 # (see lib/PublicInbox/Gcf2Client.pm)
101 sub loop () {
102         my $gcf2 = new();
103         my %seen;
104         STDERR->autoflush(1);
105         STDOUT->autoflush(1);
106
107         while (<STDIN>) {
108                 chomp;
109                 my ($oid, $git_dir) = split(/ /, $_, 2);
110                 $seen{$git_dir} //= add_alt($gcf2, "$git_dir/objects");
111                 if (!$gcf2->cat_oid(1, $oid)) {
112                         # retry once if missing.  We only get unabbreviated OIDs
113                         # from SQLite or Xapian DBs, here, so malicious clients
114                         # can't trigger excessive retries:
115                         warn "I: $$ $oid missing, retrying in $git_dir\n";
116
117                         $gcf2 = new();
118                         %seen = ($git_dir => add_alt($gcf2,"$git_dir/objects"));
119
120                         if ($gcf2->cat_oid(1, $oid)) {
121                                 warn "I: $$ $oid found after retry\n";
122                         } else {
123                                 warn "W: $$ $oid missing after retry\n";
124                                 print "$oid missing\n"; # mimic git-cat-file
125                         }
126                 }
127         }
128 }
129
130 1;