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