]> Sergey Matveev's repositories - public-inbox.git/commitdiff
use popen_rd for bidirectional pipes
authorEric Wong <e@yhbt.net>
Sat, 11 Jan 2020 22:35:03 +0000 (22:35 +0000)
committerEric Wong <e@yhbt.net>
Mon, 13 Jan 2020 04:36:48 +0000 (04:36 +0000)
popen_rd accepts arbitrary redirects, so we can reuse its
code to setup the pipe end we want to read, saving each
caller a few lines of code compared to calling pipe+spawn.

lib/PublicInbox/Git.pm
lib/PublicInbox/Import.pm
lib/PublicInbox/V2Writable.pm
t/solver_git.t

index 15f53495166a6ed4dc43f732f6dbefd7a8064da7..4f230197186d9af21da28494d8a13ecc81af38f2 100644 (file)
@@ -12,7 +12,7 @@ use warnings;
 use POSIX qw(dup2);
 use IO::Handle; # ->autoflush
 use File::Glob qw(bsd_glob GLOB_NOSORT);
-use PublicInbox::Spawn qw(spawn popen_rd);
+use PublicInbox::Spawn qw(popen_rd);
 use PublicInbox::Tmpfile;
 use base qw(Exporter);
 our @EXPORT_OK = qw(git_unquote git_quote);
@@ -104,27 +104,24 @@ sub _bidi_pipe {
                }
                return;
        }
-       my ($in_r, $in_w, $out_r, $out_w);
-
-       pipe($in_r, $in_w) or fail($self, "pipe failed: $!");
+       my ($out_r, $out_w);
        pipe($out_r, $out_w) or fail($self, "pipe failed: $!");
-       if ($^O eq 'linux') { # 1031: F_SETPIPE_SZ
-               fcntl($out_w, 1031, 4096);
-               fcntl($in_w, 1031, 4096) if $batch eq '--batch-check';
-       }
-
        my @cmd = (qw(git), "--git-dir=$self->{git_dir}",
                        qw(-c core.abbrev=40 cat-file), $batch);
-       my $redir = { 0 => $out_r, 1 => $in_w };
+       my $redir = { 0 => $out_r };
        if ($err) {
                my $id = "git.$self->{git_dir}$batch.err";
                my $fh = tmpfile($id) or fail($self, "tmpfile($id): $!");
                $self->{$err} = $fh;
                $redir->{2} = $fh;
        }
-       my $p = spawn(\@cmd, undef, $redir);
+       my ($in_r, $p) = popen_rd(\@cmd, undef, $redir);
        $self->{$pid} = $p;
        $out_w->autoflush(1);
+       if ($^O eq 'linux') { # 1031: F_SETPIPE_SZ
+               fcntl($out_w, 1031, 4096);
+               fcntl($in_r, 1031, 4096) if $batch eq '--batch-check';
+       }
        $self->{$out} = $out_w;
        $self->{$in} = $in_r;
 }
index 6ac43d372d38d2e99d55bfb101f80e00c56333b6..d279fea1a43f25a823cd168801cc82bc24034e13 100644 (file)
@@ -9,7 +9,7 @@ package PublicInbox::Import;
 use strict;
 use warnings;
 use base qw(PublicInbox::Lock);
-use PublicInbox::Spawn qw(spawn);
+use PublicInbox::Spawn qw(spawn popen_rd);
 use PublicInbox::MID qw(mids mid2path);
 use PublicInbox::Address;
 use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
@@ -46,8 +46,7 @@ sub gfi_start {
 
        return ($self->{in}, $self->{out}) if $self->{pid};
 
-       my ($in_r, $in_w, $out_r, $out_w);
-       pipe($in_r, $in_w) or die "pipe failed: $!";
+       my ($out_r, $out_w);
        pipe($out_r, $out_w) or die "pipe failed: $!";
        my $git = $self->{git};
 
@@ -66,8 +65,7 @@ sub gfi_start {
        my $git_dir = $git->{git_dir};
        my @cmd = ('git', "--git-dir=$git_dir", qw(fast-import
                        --quiet --done --date-format=raw));
-       my $rdr = { 0 => $out_r, 1 => $in_w };
-       my $pid = spawn(\@cmd, undef, $rdr);
+       my ($in_r, $pid) = popen_rd(\@cmd, undef, { 0 => $out_r });
        $out_w->autoflush(1);
        $self->{in} = $in_r;
        $self->{out} = $out_w;
index 5179432617a561a1d034f550de06b3462c8b0b02..9073d9ef89a8db95dd3db2d7c1b76cc26b758194 100644 (file)
@@ -16,7 +16,7 @@ use PublicInbox::ContentId qw(content_id content_digest);
 use PublicInbox::Inbox;
 use PublicInbox::OverIdx;
 use PublicInbox::Msgmap;
-use PublicInbox::Spawn qw(spawn);
+use PublicInbox::Spawn qw(spawn popen_rd);
 use PublicInbox::SearchIdx;
 use IO::Handle; # ->autoflush
 use File::Temp qw(tempfile);
@@ -471,17 +471,12 @@ sub git_hash_raw ($$) {
        print $tmp_fh $$raw or die "print \$tmp_fh: $!";
        sysseek($tmp_fh, 0, 0) or die "seek failed: $!";
 
-       my ($r, $w);
-       pipe($r, $w) or die "failed to create pipe: $!";
-       my $rdr = { 0 => $tmp_fh, 1 => $w };
        my $git_dir = $self->{-inbox}->git->{git_dir};
        my $cmd = ['git', "--git-dir=$git_dir", qw(hash-object --stdin)];
-       my $pid = spawn($cmd, undef, $rdr);
-       close $w;
+       my $r = popen_rd($cmd, undef, { 0 => $tmp_fh });
        local $/ = "\n";
        chomp(my $oid = <$r>);
-       waitpid($pid, 0) == $pid or die "git hash-object did not finish";
-       die "git hash-object failed: $?" if $?;
+       close $r or die "git hash-object failed: $?";
        $oid =~ /\A[a-f0-9]{40}\z/ or die "OID not expected: $oid";
        $oid;
 }
index 98317faefce2238a7c33d668cdd990564b517504..92402c3a16768b88aea7347254cfcb9f2a78952f 100644 (file)
@@ -6,7 +6,7 @@ use Test::More;
 use Cwd qw(abs_path);
 use PublicInbox::TestCommon;
 require_git(2.6);
-use PublicInbox::Spawn qw(spawn);
+use PublicInbox::Spawn qw(popen_rd);
 require_mods(qw(DBD::SQLite Search::Xapian Plack::Util));
 chomp(my $git_dir = `git rev-parse --git-dir 2>/dev/null`);
 plan skip_all => "$0 must be run from a git working tree" if $?;
@@ -120,14 +120,13 @@ SKIP: {
        my $cmd = [ qw(git hash-object -w --stdin) ];
        my $env = { GIT_DIR => $binfoo };
        while (my ($label, $size) = each %bin) {
-               pipe(my ($rout, $wout)) or die;
                pipe(my ($rin, $win)) or die;
-               my $rdr = { 0 => $rin, 1 => $wout };
-               my $pid = spawn($cmd , $env, $rdr);
-               $wout = $rin = undef;
+               my $rout = popen_rd($cmd , $env, { 0 => $rin });
+               $rin = undef;
                print { $win } ("\0" x $size) or die;
                close $win or die;
                chomp($oid{$label} = <$rout>);
+               close $rout or die "$?";
        }
 
        # ensure the PSGI frontend (ViewVCS) works: