1 # Copyright (C) 2020 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;
8 use PublicInbox::Spawn qw(which popen_rd);
10 use IO::Handle; # autoflush
11 my (%CFG, $c_src, $lockfh);
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!);
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> };
27 my $c = popen_rd([$pc, '--cflags', $x], undef, $rdr);
28 $c = do { local $/; <$c> };
31 # note: we name C source files .h to prevent
32 # ExtUtils::MakeMaker from automatically trying to
34 my $f = "$dir/gcf2_$x.h";
35 if (open(my $fh, '<', $f)) {
46 die "E: libgit2 not installed\n" unless $c_src;
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";
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;
59 # Usage: $^X -MPublicInbox::Gcf2 -e 'PublicInbox::Gcf2::loop()'
60 # (see lib/PublicInbox/Gcf2Client.pm)
68 my ($oid, $git_dir) = split(/ /, $_, 2);
69 $gcf2->add_alternate("$git_dir/objects");
70 if (!$gcf2->cat_oid(1, $oid)) {
71 # retry once if missing. We only get unabbreviated OIDs
72 # from SQLite or Xapian DBs, here, so malicious clients
73 # can't trigger excessive retries:
74 warn "I: $$ $oid missing, retrying in $git_dir\n";
77 $gcf2->add_alternate("$git_dir/objects");
79 if ($gcf2->cat_oid(1, $oid)) {
80 warn "I: $$ $oid found after retry\n";
82 warn "W: $$ $oid missing after retry\n";
83 print "$oid missing\n"; # mimic git-cat-file