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