]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
spawn: drop IO layer support from redirects
authorEric Wong <e@80x24.org>
Thu, 30 Nov 2023 11:41:02 +0000 (11:41 +0000)
committerEric Wong <e@80x24.org>
Thu, 30 Nov 2023 21:36:56 +0000 (21:36 +0000)
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.

lib/PublicInbox/MailDiff.pm
lib/PublicInbox/SearchIdx.pm
lib/PublicInbox/Spawn.pm

index e4e262efe472e9bf7cac90c93fcf5549d4dd98ed..125360fee6be6d5eb7d322efe4e2aea95e77213d 100644 (file)
@@ -65,6 +65,7 @@ sub next_smsg ($) {
 sub emit_msg_diff {
        my ($bref, $self) = @_; # bref is `git diff' output
        require PublicInbox::Hval;
+       PublicInbox::Hval::utf8_maybe($$bref);
 
        # will be escaped to `&#8226;' in HTML
        $self->{ctx}->{ibx}->{obfuscate} and
@@ -81,7 +82,7 @@ sub do_diff {
        my $dir = "$self->{tmp}/$n";
        $self->dump_eml($dir, $eml);
        my $cmd = [ qw(git diff --no-index --no-color -- a), $n ];
-       my $opt = { -C => "$self->{tmp}", quiet => 1, 1 => [':utf8', \my $o] };
+       my $opt = { -C => "$self->{tmp}", quiet => 1 };
        my $qsp = PublicInbox::Qspawn->new($cmd, undef, $opt);
        $qsp->psgi_qx($self->{ctx}->{env}, undef, \&emit_msg_diff, $self);
 }
index 17538027fda703bd8fcea28fb06b0abdf7d54dd8..86c435fde1d8124c8386245e37a590d40cd7e696 100644 (file)
@@ -355,8 +355,11 @@ sub index_body_text {
        my $rd;
        if ($$sref =~ /^(?:diff|---|\+\+\+) /ms) { # start patch-id in parallel
                my $git = ($self->{ibx} // $self->{eidx} // $self)->git;
+               my $fh = PublicInbox::IO::write_file '+>:utf8', undef, $$sref;
+               $fh->flush or die "flush: $!";
+               sysseek($fh, 0, SEEK_SET);
                $rd = popen_rd($git->cmd(qw(patch-id --stable)), undef,
-                               { 0 => [ ':utf8', $sref ] });
+                               { 0 => $fh });
        }
 
        # split off quoted and unquoted blocks:
index 9c6806906e7cbeca20fbf7c4d131f09ac2706f91..e6b12994039db3bfd6c268fae62bc7d7bc881863 100644 (file)
@@ -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};
        }
 }