]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Git.pm
spawn: improve error checking for fork failures
[public-inbox.git] / lib / PublicInbox / Git.pm
1 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
2 # License: GPLv2 or later (https://www.gnu.org/licenses/gpl-2.0.txt)
3 #
4 # Used to read files from a git repository without excessive forking.
5 # Used in our web interfaces as well as our -nntpd server.
6 # This is based on code in Git.pm which is GPLv2+, but modified to avoid
7 # dependence on environment variables for compatibility with mod_perl.
8 # There are also API changes to simplify our usage and data set.
9 package PublicInbox::Git;
10 use strict;
11 use warnings;
12 use POSIX qw(dup2);
13 require IO::Handle;
14 use PublicInbox::Spawn qw(spawn popen_rd);
15
16 sub new {
17         my ($class, $git_dir) = @_;
18         bless { git_dir => $git_dir }, $class
19 }
20
21 sub _bidi_pipe {
22         my ($self, $batch, $in, $out, $pid) = @_;
23         return if $self->{$pid};
24         my ($in_r, $in_w, $out_r, $out_w);
25
26         pipe($in_r, $in_w) or fail($self, "pipe failed: $!");
27         pipe($out_r, $out_w) or fail($self, "pipe failed: $!");
28
29         my @cmd = ('git', "--git-dir=$self->{git_dir}", qw(cat-file), $batch);
30         my $redir = { 0 => fileno($out_r), 1 => fileno($in_w) };
31         my $p = spawn(\@cmd, undef, $redir);
32         defined $p or fail($self, "spawn failed: $!");
33         $self->{$pid} = $p;
34         $out_w->autoflush(1);
35         $self->{$out} = $out_w;
36         $self->{$in} = $in_r;
37 }
38
39 sub cat_file {
40         my ($self, $obj, $ref) = @_;
41
42         $self->_bidi_pipe(qw(--batch in out pid));
43         $self->{out}->print($obj, "\n") or fail($self, "write error: $!");
44
45         my $in = $self->{in};
46         local $/ = "\n";
47         my $head = $in->getline;
48         $head =~ / missing$/ and return undef;
49         $head =~ /^[0-9a-f]{40} \S+ (\d+)$/ or
50                 fail($self, "Unexpected result from git cat-file: $head");
51
52         my $size = $1;
53         my $ref_type = $ref ? ref($ref) : '';
54
55         my $rv;
56         my $left = $size;
57         $$ref = $size if ($ref_type eq 'SCALAR');
58         my $cb_err;
59
60         if ($ref_type eq 'CODE') {
61                 $rv = eval { $ref->($in, \$left) };
62                 $cb_err = $@;
63                 # drain the rest
64                 my $max = 8192;
65                 while ($left > 0) {
66                         my $r = read($in, my $x, $left > $max ? $max : $left);
67                         defined($r) or fail($self, "read failed: $!");
68                         $r == 0 and fail($self, 'exited unexpectedly');
69                         $left -= $r;
70                 }
71         } else {
72                 my $offset = 0;
73                 my $buf = '';
74                 while ($left > 0) {
75                         my $r = read($in, $buf, $left, $offset);
76                         defined($r) or fail($self, "read failed: $!");
77                         $r == 0 and fail($self, 'exited unexpectedly');
78                         $left -= $r;
79                         $offset += $r;
80                 }
81                 $rv = \$buf;
82         }
83
84         my $r = read($in, my $buf, 1);
85         defined($r) or fail($self, "read failed: $!");
86         fail($self, 'newline missing after blob') if ($r != 1 || $buf ne "\n");
87         die $cb_err if $cb_err;
88
89         $rv;
90 }
91
92 sub check {
93         my ($self, $obj) = @_;
94         $self->_bidi_pipe(qw(--batch-check in_c out_c pid_c));
95         $self->{out_c}->print($obj, "\n") or fail($self, "write error: $!");
96         local $/ = "\n";
97         chomp(my $line = $self->{in_c}->getline);
98         my ($hex, $type, $size) = split(' ', $line);
99         return if $type eq 'missing';
100         ($hex, $type, $size);
101 }
102
103 sub _destroy {
104         my ($self, $in, $out, $pid) = @_;
105         my $p = delete $self->{$pid} or return;
106         foreach my $f ($in, $out) {
107                 delete $self->{$f};
108         }
109         waitpid $p, 0;
110 }
111
112 sub fail {
113         my ($self, $msg) = @_;
114         cleanup($self);
115         die $msg;
116 }
117
118 sub popen {
119         my ($self, @cmd) = @_;
120         @cmd = ('git', "--git-dir=$self->{git_dir}", @cmd);
121         popen_rd(\@cmd);
122 }
123
124 sub qx {
125         my ($self, @cmd) = @_;
126         my $fh = $self->popen(@cmd);
127         defined $fh or return;
128         local $/ = "\n";
129         return <$fh> if wantarray;
130         local $/;
131         <$fh>
132 }
133
134 sub cleanup {
135         my ($self) = @_;
136         _destroy($self, qw(in out pid));
137         _destroy($self, qw(in_c out_c pid_c));
138 }
139
140 sub DESTROY { cleanup(@_) }
141
142 1;
143 __END__
144 =pod
145
146 =head1 NAME
147
148 PublicInbox::Git - git wrapper
149
150 =head1 VERSION
151
152 version 1.0
153
154 =head1 SYNOPSIS
155
156         use PublicInbox::Git;
157         chomp(my $git_dir = `git rev-parse --git-dir`);
158         $git_dir or die "GIT_DIR= must be specified\n";
159         my $git = PublicInbox::Git->new($git_dir);
160
161 =head1 DESCRIPTION
162
163 Unstable API outside of the L</new> method.
164 It requires L<git(1)> to be installed.
165
166 =head1 METHODS
167
168 =cut
169
170 =head2 new
171
172         my $git = PublicInbox::Git->new($git_dir);
173
174 Initialize a new PublicInbox::Git object for use with L<PublicInbox::Import>
175 This is the only public API method we support.  Everything else
176 in this module is subject to change.
177
178 =head1 SEE ALSO
179
180 L<Git>, L<PublicInbox::Import>
181
182 =head1 CONTACT
183
184 All feedback welcome via plain-text mail to L<mailto:meta@public-inbox.org>
185
186 The mail archives are hosted at L<https://public-inbox.org/meta/>
187
188 =head1 COPYRIGHT
189
190 Copyright (C) 2016 all contributors L<mailto:meta@public-inbox.org>
191
192 License: AGPL-3.0+ L<http://www.gnu.org/licenses/agpl-3.0.txt>
193
194 =cut