From d3c55d072839286efb2865fe20f2324a9e595e95 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 2 Nov 2023 09:35:30 +0000 Subject: treewide: use ->close to call ProcessIO->CLOSE This will open the door for us to drop `tie' usage from ProcessIO completely in favor of OO method dispatch. While OO method dispatches (e.g. `$fh->close') are slower than normal subroutine calls, it hardly matters in this case since process teardown is a fairly rare operation and we continue to use `close($fh)' for Maildir writes. --- lib/PublicInbox/Config.pm | 2 +- lib/PublicInbox/GitCredential.pm | 4 ++-- lib/PublicInbox/Import.pm | 6 +++--- lib/PublicInbox/LEI.pm | 8 ++++---- lib/PublicInbox/LeiInput.pm | 2 +- lib/PublicInbox/LeiRediff.pm | 2 +- lib/PublicInbox/LeiViewText.pm | 2 +- lib/PublicInbox/LeiXSearch.pm | 8 ++++---- lib/PublicInbox/MboxReader.pm | 4 ++-- lib/PublicInbox/SearchIdx.pm | 2 +- lib/PublicInbox/Spawn.pm | 2 +- lib/PublicInbox/V2Writable.pm | 2 +- 12 files changed, 22 insertions(+), 22 deletions(-) (limited to 'lib/PublicInbox') diff --git a/lib/PublicInbox/Config.pm b/lib/PublicInbox/Config.pm index 0a572103..01cb536d 100644 --- a/lib/PublicInbox/Config.pm +++ b/lib/PublicInbox/Config.pm @@ -192,7 +192,7 @@ sub git_config_dump { push(@cmd, '-f', $file) if !@opt_c && defined($file); my $fh = popen_rd(\@cmd, \%env, $opt); my $rv = config_fh_parse($fh, "\0", "\n"); - close $fh or die "@cmd failed: \$?=$?\n"; + $fh->close or die "@cmd failed: \$?=$?\n"; $rv->{-opt_c} = \@opt_c if @opt_c; # for ->urlmatch $rv->{-f} = $file; bless $rv, $class; diff --git a/lib/PublicInbox/GitCredential.pm b/lib/PublicInbox/GitCredential.pm index 10114a10..a4444e2c 100644 --- a/lib/PublicInbox/GitCredential.pm +++ b/lib/PublicInbox/GitCredential.pm @@ -30,7 +30,7 @@ sub run ($$;$) { close $in_w or die "close (git credential $op): $!"; return $out_r if $op eq 'fill'; <$out_r> and die "unexpected output from `git credential $op'\n"; - close $out_r or die "`git credential $op' failed: \$!=$! \$?=$?\n"; + $out_r->close or die "`git credential $op' failed: \$!=$! \$?=$?\n"; } sub check_netrc { @@ -61,7 +61,7 @@ sub fill { /\A([^=]+)=(.*)\z/ or die "bad line: $_\n"; $self->{$1} = $2; } - close $out_r or die "git credential fill failed: \$!=$! \$?=$?\n"; + $out_r->close or die "git credential fill failed: \$!=$! \$?=$?\n"; $self->{filled} = 1; } diff --git a/lib/PublicInbox/Import.pm b/lib/PublicInbox/Import.pm index 6eee8774..e12a56e8 100644 --- a/lib/PublicInbox/Import.pm +++ b/lib/PublicInbox/Import.pm @@ -476,7 +476,7 @@ sub done { my $io = delete $self->{io} or return; eval { print $io "done\n" or wfail; - close $io; # reaps and dies on error + $io->close or croak "close fast-import \$?=$?"; # reaps }; my $wait_err = $@; my $nchg = delete $self->{nchg}; @@ -489,7 +489,7 @@ sub done { die $wait_err if $wait_err; } -sub atfork_child { close(delete($_[0]->{io}) // return) } +sub atfork_child { (delete($_[0]->{io}) // return)->close } sub digest2mid ($$;$) { my ($dig, $hdr, $fallback_time) = @_; @@ -598,7 +598,7 @@ sub replace_oids { push @buf, $_; } } - close $rd; + $rd->close or die "E: git @export (\$?=$?)"; if (@buf) { print $io @buf or wfail; } diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm index 0f6f7f6f..2832db63 100644 --- a/lib/PublicInbox/LEI.pm +++ b/lib/PublicInbox/LEI.pm @@ -501,7 +501,7 @@ sub err ($;@) { my @eor = (substr($_[-1]//'', -1, 1) eq "\n" ? () : ("\n")); print $err @_, @eor and return; my $old_err = delete $self->{2}; - close($old_err) if $! == EPIPE && $old_err; + $old_err->close if $! == EPIPE && $old_err; $err = $self->{2} = ($self->{pgr} // [])->[2] // *STDERR{GLOB}; print $err @_, @eor or print STDERR @_, @eor; } @@ -516,7 +516,7 @@ sub qfin { # show message on finalization (LeiFinmsg) sub fail_handler ($;$$) { my ($lei, $code, $io) = @_; - close($io) if $io; # needed to avoid warnings on SIGPIPE + $io->close if $io; # needed to avoid warnings on SIGPIPE _drop_wq($lei); x_it($lei, $code // (1 << 8)); } @@ -565,7 +565,7 @@ sub child_error { # passes non-fatal curl exit codes to user sub note_sigpipe { # triggers sigpipe_handler my ($self, $fd) = @_; - close(delete($self->{$fd})); # explicit close silences Perl warning + delete($self->{$fd})->close; # explicit close silences Perl warning $self->{pkt_op_p}->pkt_do('sigpipe_handler') if $self->{pkt_op_p}; x_it($self, 13); } @@ -1129,7 +1129,7 @@ sub stop_pager { my ($self) = @_; my $pgr = delete($self->{pgr}) or return; $self->{2} = $pgr->[2]; - close(delete($self->{1})) if $self->{1}; + delete($self->{1})->close if $self->{1}; $self->{1} = $pgr->[1]; } diff --git a/lib/PublicInbox/LeiInput.pm b/lib/PublicInbox/LeiInput.pm index 28b73ca9..f7c3f573 100644 --- a/lib/PublicInbox/LeiInput.pm +++ b/lib/PublicInbox/LeiInput.pm @@ -246,7 +246,7 @@ sub input_path_url { my $fh = popen_rd($fp, undef, $rdr); eval { $self->input_fh('eml', $fh, $input, @args) }; my @err = ($@ ? $@ : ()); - close($fh) or push @err, "\$?=$?"; + $fh->close or push @err, "\$?=$?"; $lei->child_error($?, "@$fp failed: @err") if @err; } else { $self->folder_missing("$ifmt:$input"); diff --git a/lib/PublicInbox/LeiRediff.pm b/lib/PublicInbox/LeiRediff.pm index 230f3e83..fdff4b4b 100644 --- a/lib/PublicInbox/LeiRediff.pm +++ b/lib/PublicInbox/LeiRediff.pm @@ -126,7 +126,7 @@ EOM qw(fast-import --quiet --done --date-format=raw)], $lei->{env}, { 2 => $lei->{2} }); print $w $ta, "\n", $tb, "\ndone\n" or die "print fast-import: $!"; - close $w or die "close w fast-import: \$?=$? \$!=$!"; + $w->close or die "close w fast-import: \$?=$? \$!=$!"; my $cmd = [ 'diff' ]; _lei_diff_prepare($lei, $cmd); diff --git a/lib/PublicInbox/LeiViewText.pm b/lib/PublicInbox/LeiViewText.pm index 70441867..ce9f248e 100644 --- a/lib/PublicInbox/LeiViewText.pm +++ b/lib/PublicInbox/LeiViewText.pm @@ -75,7 +75,7 @@ sub new { my @cmd = qw(git config -z --includes -l); # reuse normal git config my $r = popen_rd(\@cmd, undef, { 2 => $lei->{2} }); my $cfg = PublicInbox::Config::config_fh_parse($r, "\0", "\n"); - if (!close($r)) { + if ($r->close) { warn "# @cmd failed, no color (non-fatal \$?=$?)\n"; return $self; } diff --git a/lib/PublicInbox/LeiXSearch.pm b/lib/PublicInbox/LeiXSearch.pm index 241b9dab..5443188d 100644 --- a/lib/PublicInbox/LeiXSearch.pm +++ b/lib/PublicInbox/LeiXSearch.pm @@ -353,14 +353,14 @@ print STDERR $_; $lei, $each_smsg); $lei->sto_done_request if delete($self->{-sto_imported}); my $nr = delete $lei->{-nr_remote_eml} // 0; - close $cfh; + $cfh->close; my $code = $?; if (!$code) { # don't update if no results, maybe MTA is down $lei->{lss}->cfg_set($key, $start) if $key && $nr; mset_progress($lei, $lei->{-current_url}, $nr, $nr); next; } - close(delete($rdr->{2})) if @lbf_tee; + delete($rdr->{2})->close if @lbf_tee; seek($cerr, 0, SEEK_SET); read($cerr, my $err, -s $cerr); truncate($cerr, 0); @@ -396,8 +396,8 @@ sub query_done { # EOF callback for main daemon $lei->{ovv}->ovv_end($lei); if ($l2m) { # close() calls LeiToMail reap_compress if (my $out = delete $lei->{old_1}) { - if (my $mbout = $lei->{1}) { - close($mbout) or die <<""; + if (my $mbout = $lei->{1}) { # compressor pipe process + $mbout->close or die <<""; Error closing $lei->{ovv}->{dst}: \$!=$! \$?=$? } diff --git a/lib/PublicInbox/MboxReader.pm b/lib/PublicInbox/MboxReader.pm index d67fb4eb..3d78ca23 100644 --- a/lib/PublicInbox/MboxReader.pm +++ b/lib/PublicInbox/MboxReader.pm @@ -29,7 +29,7 @@ sub _mbox_from { my @raw; while (defined(my $r = read($mbfh, $buf, 65536, length($buf)))) { if ($r == 0) { # close here to check for "curl --fail" - close($mbfh) or die "error closing mbox: \$?=$? $!"; + $mbfh->close or die "error closing mbox: \$?=$? $!"; @raw = ($buf); } else { @raw = split(/$from_strict/mos, $buf, -1); @@ -88,7 +88,7 @@ sub _mbox_cl ($$$;@) { my $buf = ''; while (defined(my $r = read($mbfh, $buf, 65536, length($buf)))) { if ($r == 0) { # detect "curl --fail" - close($mbfh) or + $mbfh->close or die "error closing mboxcl/mboxcl2: \$?=$? $!"; undef $mbfh; } diff --git a/lib/PublicInbox/SearchIdx.pm b/lib/PublicInbox/SearchIdx.pm index 3c64c715..78519b22 100644 --- a/lib/PublicInbox/SearchIdx.pm +++ b/lib/PublicInbox/SearchIdx.pm @@ -980,7 +980,7 @@ sub log2stack ($$$) { $stk->push_rec('m', $at, $ct, $oid, $cmt); } } - close $fh or die "git log failed: \$?=$?"; + $fh->close or die "git log failed: \$?=$?"; $stk //= PublicInbox::IdxStack->new; $stk->read_prepare; } diff --git a/lib/PublicInbox/Spawn.pm b/lib/PublicInbox/Spawn.pm index e7d2c6ea..b4f37bea 100644 --- a/lib/PublicInbox/Spawn.pm +++ b/lib/PublicInbox/Spawn.pm @@ -421,7 +421,7 @@ sub run_qx { local $/; $ret[0] = <$fh>; } - close $fh; # caller should check $? + $fh->close; # caller should check $? read_out_err($opt); wantarray ? @ret : $ret[0]; } diff --git a/lib/PublicInbox/V2Writable.pm b/lib/PublicInbox/V2Writable.pm index 191c5588..4d606dfe 100644 --- a/lib/PublicInbox/V2Writable.pm +++ b/lib/PublicInbox/V2Writable.pm @@ -1070,7 +1070,7 @@ sub unindex_todo ($$$) { /\A:\d{6} 100644 $OID ($OID) [AM]\tm$/o or next; $self->git->cat_async($1, $unindex_oid, { %$sync, oid => $1 }); } - close $fh or die "git log failed: \$?=$?"; + $fh->close or die "git log failed: \$?=$?"; $self->git->async_wait_all; return unless $sync->{-opt}->{prune}; -- cgit v1.2.3-24-ge0c7