-# Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
+# Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
# License: GPLv2 or later (https://www.gnu.org/licenses/gpl-2.0.txt)
# This is based on code in Git.pm which is GPLv2, but modified to avoid
# dependence on environment variables for compatibility with mod_perl.
package PublicInbox::GitCatFile;
use strict;
use warnings;
-use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC);
use POSIX qw(dup2);
sub new {
bless { git_dir => $git_dir }, $class;
}
-sub set_cloexec {
- my ($fh) = @_;
- my $flags = fcntl($fh, F_GETFD, 0) or die "fcntl(F_GETFD): $!\n";
- fcntl($fh, F_SETFD, $flags | FD_CLOEXEC) or die "fcntl(F_SETFD): $!\n";
-}
-
sub _cat_file_begin {
my ($self) = @_;
return if $self->{pid};
my ($in_r, $in_w, $out_r, $out_w);
pipe($in_r, $in_w) or die "pipe failed: $!\n";
- set_cloexec($_) foreach ($in_r, $in_w);
pipe($out_r, $out_w) or die "pipe failed: $!\n";
- set_cloexec($_) foreach ($out_r, $out_w);
my @cmd = ('git', "--git-dir=$self->{git_dir}", qw(cat-file --batch));
my $pid = fork;
my $size = $1;
my $bytes_left = $size;
- my $buf;
+ my $offset = 0;
my $rv = '';
while ($bytes_left) {
- my $read = read($in, $buf, $bytes_left);
- defined($read) or die "read pipe failed: $!\n";
- $rv .= $buf;
+ my $read = read($in, $rv, $bytes_left, $offset);
+ defined($read) or die "sysread pipe failed: $!\n";
$bytes_left -= $read;
+ $offset += $read;
}
- my $read = read($in, $buf, 1);
+ my $read = read($in, my $buf, 1);
defined($read) or die "read pipe failed: $!\n";
if ($read != 1 || $buf ne "\n") {
die "newline missing after blob\n";