]> Sergey Matveev's repositories - public-inbox.git/commitdiff
git cat-file wrapper uses syswrite for writes
authorEric Wong <e@80x24.org>
Fri, 2 May 2014 20:07:59 +0000 (20:07 +0000)
committerEric Wong <e@80x24.org>
Fri, 2 May 2014 20:07:59 +0000 (20:07 +0000)
The requests we make to git cat-file --batch are guaranteed to be
smaller than the 512 bytes required by PIPE_BUF, so there will be no
partial writes.  Bypass Perl IO layers and write directly to the
pipe to avoid needing IO::Handle here.

lib/PublicInbox/GitCatFile.pm

index b75389e2fdb3ebf5958c5366dffd5356dfbf22d6..8bc6a238d2ae2c8f09c5062091e2ae140245d0c6 100644 (file)
@@ -8,7 +8,6 @@ use strict;
 use warnings;
 use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC);
 use POSIX qw(dup2);
-require IO::Handle;
 
 sub new {
        my ($class, $git_dir) = @_;
@@ -42,7 +41,6 @@ sub _cat_file_begin {
        close $out_r or die "close failed: $!\n";
        close $in_w or die "close failed: $!\n";
 
-       $out_w->autoflush(1);
        $self->{in} = $in_r;
        $self->{out} = $out_w;
        $self->{pid} = $pid;
@@ -51,8 +49,16 @@ sub _cat_file_begin {
 sub cat_file {
        my ($self, $object) = @_;
 
+       $object .= "\n";
+       my $len = bytes::length($object);
+
        $self->_cat_file_begin;
-       print { $self->{out} } $object, "\n" or die "write error: $!\n";
+       my $written = syswrite($self->{out}, $object);
+       if (!defined $written) {
+               die "pipe write error: $!\n";
+       } elsif ($written != $len) {
+               die "wrote too little to pipe ($written < $len)\n";
+       }
 
        my $in = $self->{in};
        my $head = <$in>;