From: Eric Wong Date: Tue, 29 Apr 2025 20:47:43 +0000 (+0000) Subject: treewide: use autodie::open where possible X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e03e833e2febd99dc89dbab0ddd47771db73d2ec;p=thirdparty%2Fpublic-inbox.git treewide: use autodie::open where possible Outside of development scripts to run on cfarm machines, we can rely on autodie being available to reduce noise in our code and improve error message consistency. --- diff --git a/lib/PublicInbox/GzipFilter.pm b/lib/PublicInbox/GzipFilter.pm index a2e82a2d3..2d164a4bf 100644 --- a/lib/PublicInbox/GzipFilter.pm +++ b/lib/PublicInbox/GzipFilter.pm @@ -16,6 +16,7 @@ package PublicInbox::GzipFilter; use strict; use parent qw(Exporter); +use autodie qw(open); use Compress::Raw::Zlib qw(Z_OK); use PublicInbox::CompressNoop; use PublicInbox::Eml; @@ -136,8 +137,7 @@ sub write { sub zfh { $_[0]->{zfh} // do { - open($_[0]->{zfh}, '>>', \($_[0]->{pbuf} //= '')) or - die "open: $!"; + open($_[0]->{zfh}, '>>', \($_[0]->{pbuf} //= '')); $_[0]->{zfh} }; } diff --git a/lib/PublicInbox/HlMod.pm b/lib/PublicInbox/HlMod.pm index f42ece80e..13ebe3f47 100644 --- a/lib/PublicInbox/HlMod.pm +++ b/lib/PublicInbox/HlMod.pm @@ -15,6 +15,7 @@ package PublicInbox::HlMod; use strict; use v5.10.1; +use autodie qw(open); use highlight; # SWIG-generated stuff use PublicInbox::Hval qw(src_escape ascii_html); my $hl; @@ -22,7 +23,7 @@ my $hl; sub _parse_filetypes ($) { my $ft_conf = $_[0]->getFiletypesConfPath('filetypes') or die 'filetypes.conf not found by highlight'; - open my $fh, '<', $ft_conf or die "failed to open($ft_conf): $!"; + open my $fh, '<', $ft_conf; local $/; my $cfg = <$fh>; my %ext2lang; diff --git a/lib/PublicInbox/LI2Wrap.pm b/lib/PublicInbox/LI2Wrap.pm index d4792b251..5b88a3cce 100644 --- a/lib/PublicInbox/LI2Wrap.pm +++ b/lib/PublicInbox/LI2Wrap.pm @@ -7,11 +7,12 @@ package PublicInbox::LI2Wrap; use v5.12; our @ISA = qw(Linux::Inotify2); +use autodie qw(open); sub wrapclose { my ($inot) = @_; my $fd = $inot->fileno; - open my $fh, '<&=', $fd or die "open <&= $fd $!"; + open my $fh, '<&=', $fd; bless $inot, __PACKAGE__; } diff --git a/lib/PublicInbox/LeiInput.pm b/lib/PublicInbox/LeiInput.pm index a9443f3ae..f9d0d1e56 100644 --- a/lib/PublicInbox/LeiInput.pm +++ b/lib/PublicInbox/LeiInput.pm @@ -4,6 +4,7 @@ # parent class for LeiImport, LeiConvert, LeiIndex package PublicInbox::LeiInput; use v5.12; +use autodie qw(open); use PublicInbox::DS; use PublicInbox::Git qw(git_exe); use PublicInbox::Spawn qw(which popen_rd); @@ -215,8 +216,7 @@ sub input_path_url { my $mhr = PublicInbox::MHreader->new($dn, $lei->{3}); $mhr->mh_read_one($n, $self->can('input_mh_cb'), $self); } elsif (-f $input && $ifmt eq 'eml') { - open my $fh, '<', $input or - return $lei->fail("open($input): $!"); + open my $fh, '<', $input; # autodie::open hits caller eval $self->input_fh($ifmt, $fh, $input, @args); } elsif (-f _) { my $m = $lei->{opt}->{'lock'} // diff --git a/lib/PublicInbox/LeiRediff.pm b/lib/PublicInbox/LeiRediff.pm index 6df2a96b0..876025b9d 100644 --- a/lib/PublicInbox/LeiRediff.pm +++ b/lib/PublicInbox/LeiRediff.pm @@ -6,6 +6,7 @@ package PublicInbox::LeiRediff; use strict; use v5.10.1; use parent qw(PublicInbox::IPC PublicInbox::LeiInput); +use autodie qw(open); use File::Temp 0.19 (); # 0.19 for ->newdir use PublicInbox::Spawn qw(run_wait popen_wr which); use PublicInbox::MsgIter qw(msg_part_text); @@ -49,7 +50,7 @@ sub solve_1 ($$$) { uarg => $self, inboxes => [ $self->{lxs}->locals, @{$self->{rmt}} ], }, 'PublicInbox::SolverGit'; - open my $log, '+>', \(my $log_buf = '') or die "PerlIO::scalar: $!"; + open my $log, '+>', \(my $log_buf = ''); $self->{lei}->{log_buf} = \$log_buf; local $PublicInbox::DS::in_loop = 0; # waitpid synchronously $solver->solve($self->{lei}->{env}, $log, $oid_want, $hints); diff --git a/t/lei-q-kw.t b/t/lei-q-kw.t index 63e460378..1a6e4b6ec 100644 --- a/t/lei-q-kw.t +++ b/t/lei-q-kw.t @@ -2,6 +2,7 @@ # Copyright (C) all contributors # License: AGPL-3.0+ use strict; use v5.10.1; use PublicInbox::TestCommon; +use autodie qw(open); use POSIX qw(mkfifo); use Fcntl qw(SEEK_SET O_RDONLY O_NONBLOCK); use IO::Uncompress::Gunzip qw(gunzip); @@ -70,7 +71,7 @@ SKIP: { $cat = popen_rd(['cat', $o]); lei_ok(qw(q m:qp@example.com -o), "mboxrd:$o"); my $buf = do { local $/; <$cat> }; - open my $fh, '<', \$buf or BAIL_OUT $!; + open my $fh, '<', \$buf; PublicInbox::MboxReader->mboxrd($fh, sub { my ($eml) = @_; $eml->header_set('Status', 'RO'); @@ -86,7 +87,7 @@ my $read_file = sub { BAIL_OUT 'gunzip'; $buf; } else { - open my $fh, '+<', $_[0] or BAIL_OUT $!; + open my $fh, '+<', $_[0]; do { local $/; <$fh> }; } }; @@ -115,7 +116,7 @@ for my $sfx ('', '.gz') { $write_file->($o, $buf); lei_ok(qw(q -a -o), "mboxrd:$o", qw(m:testmessage@example.com)); $buf = $read_file->($o); - open my $fh, '<', \$buf or BAIL_OUT "PerlIO::scalar $!"; + open my $fh, '<', \$buf; my %res; PublicInbox::MboxReader->mboxrd($fh, sub { my ($eml) = @_; @@ -166,18 +167,18 @@ $o = "$ENV{HOME}/kwmboxrd"; lei_ok(qw(q -o), "mboxrd:$o", "m:$m", @inc); # emulate MUA marking mboxrd message as unread -open my $fh, '<', $o or BAIL_OUT; +open my $fh, '<', $o; my $s = do { local $/; <$fh> }; $s =~ s/^Status: RO\n/Status: O\nX-Status: AF\n/sm or fail "failed to clear R flag in $s"; -open $fh, '>', $o or BAIL_OUT; +open $fh, '>', $o; print $fh $s or BAIL_OUT; close $fh or BAIL_OUT; lei_ok(qw(q -o), "mboxrd:$o", 'm:bogus', @inc, \'clobber mbox to import keywords'); lei_ok(qw(q -o), "mboxrd:$o", "m:$m", @inc); -open $fh, '<', $o or BAIL_OUT; +open $fh, '<', $o; $s = do { local $/; <$fh> }; like($s, qr/^Status: O\nX-Status: AF\n/ms, 'seen keyword gone in mbox, answered + flagged set'); @@ -212,7 +213,7 @@ my $in = $eml->as_string; lei_ok([qw(import -F eml --stdin)], undef, { 0 => \$in, %$lei_opt }); is($lei_err, '', 'no errors from import'); lei_ok(qw(q -f mboxrd), "m:$m"); -open $fh, '<', \$lei_out or BAIL_OUT $!; +open $fh, '<', \$lei_out; my @res; PublicInbox::MboxReader->mboxrd($fh, sub { push @res, shift }); is($res[0]->header('Status'), 'RO', 'seen kw set'); @@ -225,7 +226,7 @@ lei_ok([qw(import -F eml --stdin)], undef, { 0 => \$in, %$lei_opt }); is($lei_err, '', 'no errors from 2nd import'); lei_ok(qw(q -f mboxrd), "m:$m", 'l:another.example.com'); my @another; -open $fh, '<', \$lei_out or BAIL_OUT $!; +open $fh, '<', \$lei_out; PublicInbox::MboxReader->mboxrd($fh, sub { push @another, shift }); is($another[0]->header('Status'), 'RO', 'seen kw set'); @@ -246,7 +247,7 @@ is($another[0]->header('Status'), 'RO', 'seen kw set'); scalar(@p) == 1 or xbail('multiple when 1 expected', \@p); like($p[0], qr/,PS/, 'passed (Forwarded) flag kept'); lei_ok(qw(q -o), "mboxrd:$o.mboxrd", "m:$m"); - open $fh, '<', "$o.mboxrd" or xbail $!; + open $fh, '<', "$o.mboxrd"; my @res; PublicInbox::MboxReader->mboxrd($fh, sub { push @res, shift }); scalar(@res) == 1 or xbail('multiple when 1 expected', \@res); diff --git a/t/mbox_reader.t b/t/mbox_reader.t index 1fa9068e3..3c072b5c2 100644 --- a/t/mbox_reader.t +++ b/t/mbox_reader.t @@ -3,7 +3,7 @@ # License: AGPL-3.0+ use strict; use v5.10.1; -use autodie qw(seek); +use autodie qw(open seek); use PublicInbox::TestCommon; use List::Util qw(shuffle); use PublicInbox::Eml; @@ -46,7 +46,7 @@ my $check_fmt = sub { my $fmt = shift; my @order = shuffle(keys %raw); my $eml2mbox = PublicInbox::LeiToMail->can("eml2$fmt"); - open my $fh, '+>', undef or BAIL_OUT "open: $!"; + open my $fh, '+>', undef; for my $k (@order) { my $eml = PublicInbox::Eml->new($raw{$k}); my $buf = $eml2mbox->($eml); @@ -104,7 +104,7 @@ EOM # chop($no_blank_eom) eq "\n" or BAIL_OUT 'broken LF'; for my $variant (qw(mboxrd mboxo)) { my @x; - open my $fh, '<', \$no_blank_eom or BAIL_OUT 'PerlIO::scalar'; + open my $fh, '<', \$no_blank_eom; $reader->$variant($fh, sub { push @x, shift }); is_deeply($x[0]->{bdy}, \"body1\n", 'LF preserved in 1st'); is_deeply($x[1]->{bdy}, \"body2\n", 'no LF added in 2nd'); @@ -134,7 +134,7 @@ EOM for my $m (qw(mboxrd mboxcl mboxcl2 mboxo)) { my (@w, @x); local $SIG{__WARN__} = sub { push @w, @_ }; - open my $fh, '<', \$html or xbail 'PerlIO::scalar'; + open my $fh, '<', \$html; PublicInbox::MboxReader->$m($fh, sub { push @x, $_[0]->as_string });