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