about summary refs log tree commit homepage
path: root/lib/PublicInbox/Spawn.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2023-11-30 11:41:02 +0000
committerEric Wong <e@80x24.org>2023-11-30 21:36:56 +0000
commit646ba6c2e1a21307e03c54341c49188aa1758349 (patch)
treeda5b5679900b94cdadb5345736d33597ce191cfd /lib/PublicInbox/Spawn.pm
parent8b1e05e5a4dfec73d593a549f7ed0ffae98209fb (diff)
downloadpublic-inbox-646ba6c2e1a21307e03c54341c49188aa1758349.tar.gz
When setting up stdin for commands, the write_file API is
convenient enough nowadays to not be worth having special
support with process spawning.

When reading stdout of commands, we should probably be using
utf8_maybe everywhere since there'll always be legacy encodings
in git repos.

Reading regular files with :utf8 also results in worse memory
management since the file size cannot be used as a hint.
Diffstat (limited to 'lib/PublicInbox/Spawn.pm')
-rw-r--r--lib/PublicInbox/Spawn.pm32
1 files changed, 11 insertions, 21 deletions
diff --git a/lib/PublicInbox/Spawn.pm b/lib/PublicInbox/Spawn.pm
index 9c680690..e6b12994 100644
--- a/lib/PublicInbox/Spawn.pm
+++ b/lib/PublicInbox/Spawn.pm
@@ -332,18 +332,6 @@ sub which ($) {
         undef;
 }
 
-sub scalar_redirect {
-        my ($layer, $opt, $child_fd, $bref) = @_;
-        open my $fh, '+>'.$layer, undef;
-        $opt->{"fh.$child_fd"} = $fh;
-        if ($child_fd == 0) {
-                print $fh $$bref;
-                $fh->flush or die "flush: $!";
-                sysseek($fh, 0, SEEK_SET);
-        }
-        fileno($fh);
-}
-
 sub spawn ($;$$) {
         my ($cmd, $env, $opt) = @_;
         my $f = which($cmd->[0]) // die "$cmd->[0]: command not found\n";
@@ -354,14 +342,18 @@ sub spawn ($;$$) {
         }
         for my $child_fd (0..2) {
                 my $pfd = $opt->{$child_fd};
-                if ('ARRAY' eq ref($pfd)) {
-                        my ($layer, $bref) = @$pfd;
-                        $pfd = scalar_redirect($layer, $opt, $child_fd, $bref)
-                } elsif ('SCALAR' eq ref($pfd)) {
-                        $pfd = scalar_redirect('', $opt, $child_fd, $pfd);
+                if ('SCALAR' eq ref($pfd)) {
+                        open my $fh, '+>', undef;
+                        $opt->{"fh.$child_fd"} = $fh; # for read_out_err
+                        if ($child_fd == 0) {
+                                print $fh $$pfd;
+                                $fh->flush or die "flush: $!";
+                                sysseek($fh, 0, SEEK_SET);
+                        }
+                        $pfd = fileno($fh);
                 } elsif (defined($pfd) && $pfd !~ /\A[0-9]+\z/) {
                         my $fd = fileno($pfd) //
-                                        die "$pfd not an IO GLOB? $!";
+                                        croak "BUG: $pfd not an IO GLOB? $!";
                         $pfd = $fd;
                 }
                 $rdr[$child_fd] = $pfd // $child_fd;
@@ -399,9 +391,7 @@ sub read_out_err ($) {
         for my $fd (1, 2) { # read stdout/stderr
                 my $fh = delete($opt->{"fh.$fd"}) // next;
                 seek($fh, 0, SEEK_SET);
-                my $dst = $opt->{$fd};
-                $dst = $opt->{$fd} = $dst->[1] if ref($dst) eq 'ARRAY';
-                PublicInbox::IO::read_all $fh, 0, $dst
+                PublicInbox::IO::read_all $fh, undef, $opt->{$fd};
         }
 }