From 94c9dee82ececdf4908097660ec430f3d57949d9 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 18 Jan 2025 01:26:11 +0000 Subject: [PATCH] treewide: use autodie for seek+sysseek The underlying lseek(2) syscall won't fail due to HW errors, only due to usage errors (ESPIPE, EINVAL); so don't waste code on error checking ourselves and just let autodie check things during development. --- Documentation/common.perl | 3 ++- lib/PublicInbox/Emergency.pm | 5 +++-- lib/PublicInbox/HTTP.pm | 5 +---- lib/PublicInbox/SolverGit.pm | 4 ++-- lib/PublicInbox/ViewVCS.pm | 9 ++++----- lib/PublicInbox/WwwStatic.pm | 6 +++--- t/check-www-inbox.perl | 3 ++- t/gcf2.t | 12 +++++++----- t/gzip_filter.t | 2 +- t/httpd-corner.t | 4 ++-- t/import.t | 3 ++- t/ipc.t | 3 ++- t/lei-externals.t | 5 +++-- t/lei-p2q.t | 5 +++-- t/lei-sigpipe.t | 4 ++-- t/mbox_reader.t | 6 +++--- 16 files changed, 42 insertions(+), 37 deletions(-) diff --git a/Documentation/common.perl b/Documentation/common.perl index 53bae4959..98a06ee14 100755 --- a/Documentation/common.perl +++ b/Documentation/common.perl @@ -2,6 +2,7 @@ # Copyright (C) all contributors # License: AGPL-3.0+ use strict; +use autodie qw(seek); use Fcntl qw(SEEK_SET); use PublicInbox::Search; my $addr = 'meta@public-inbox.org'; @@ -38,7 +39,7 @@ L my $t = time; utime($t, $t, $fh); } else { - seek($fh, 0, SEEK_SET) or die "seek: $!"; + seek $fh, 0, SEEK_SET; truncate($fh, 0) or die "truncate: $!"; print $fh $s or die "print: $!"; close $fh or die "close: $!"; diff --git a/lib/PublicInbox/Emergency.pm b/lib/PublicInbox/Emergency.pm index 968d7d6ff..d3a83408f 100644 --- a/lib/PublicInbox/Emergency.pm +++ b/lib/PublicInbox/Emergency.pm @@ -9,6 +9,7 @@ use Sys::Hostname qw(hostname); use IO::Handle; # ->flush use Errno qw(EEXIST); use File::Path (); +use autodie qw(seek sysseek); sub new { my ($class, $dir) = @_; @@ -57,8 +58,8 @@ sub abort { sub fh { my ($self) = @_; my $fh = $self->{fh} or die "BUG: {fh} not open"; - seek($fh, 0, SEEK_SET) or die "seek: $!"; - sysseek($fh, 0, SEEK_SET) or die "sysseek: $!"; + seek $fh, 0, SEEK_SET; + sysseek $fh, 0, SEEK_SET; $fh; } diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm index 88cab5447..80ebad164 100644 --- a/lib/PublicInbox/HTTP.pm +++ b/lib/PublicInbox/HTTP.pm @@ -147,10 +147,7 @@ sub app_dispatch { $host =~ s/:([0-9]+)\z// and $env->{SERVER_PORT} = $1 + 0; $env->{SERVER_NAME} = $host; } - if (defined $input) { - sysseek($input, 0, SEEK_SET) or - die "BUG: psgi.input seek failed: $!"; - } + sysseek($input, 0, SEEK_SET) if defined $input; # note: NOT $self->{sock}, we want our close (+ PublicInbox::DS::close), # to do proper cleanup: $env->{'psgix.io'} = $self; # for ->close or async_pass diff --git a/lib/PublicInbox/SolverGit.pm b/lib/PublicInbox/SolverGit.pm index d94657711..e30761e25 100644 --- a/lib/PublicInbox/SolverGit.pm +++ b/lib/PublicInbox/SolverGit.pm @@ -11,7 +11,7 @@ package PublicInbox::SolverGit; use strict; use v5.10.1; use File::Temp 0.19 (); # 0.19 for ->newdir -use autodie qw(mkdir); +use autodie qw(mkdir sysseek); use Fcntl qw(SEEK_SET); use PublicInbox::Git qw(git_unquote git_quote git_exe); use PublicInbox::IO qw(write_file); @@ -342,7 +342,7 @@ sub prepare_index ($) { my $in = tmpfile("update-index.$oid_full") or die "tmpfile: $!"; print $in "$mode_a $oid_full\t$path_a\0" or die "print: $!"; $in->flush or die "flush: $!"; - sysseek($in, 0, SEEK_SET) or die "seek: $!"; + sysseek $in, 0, SEEK_SET; dbg($self, 'preparing index'); my $rdr = { 0 => $in }; diff --git a/lib/PublicInbox/ViewVCS.pm b/lib/PublicInbox/ViewVCS.pm index d9df671c2..8d937cff0 100644 --- a/lib/PublicInbox/ViewVCS.pm +++ b/lib/PublicInbox/ViewVCS.pm @@ -65,11 +65,10 @@ sub html_page ($$;@) { sub dbg_log ($) { my ($ctx) = @_; my $log = delete $ctx->{lh} // die 'BUG: already captured debug log'; - if (!CORE::seek($log, 0, SEEK_SET)) { - warn "seek(log): $!"; - return '
debug log seek error
'; - } - $log = eval { PublicInbox::IO::read_all $log } // do { + $log = eval { + seek $log, 0, SEEK_SET; + PublicInbox::IO::read_all $log; + } // do { warn "read(log): $@"; return '
debug log read error
'; }; diff --git a/lib/PublicInbox/WwwStatic.pm b/lib/PublicInbox/WwwStatic.pm index d89021930..af4eb9601 100644 --- a/lib/PublicInbox/WwwStatic.pm +++ b/lib/PublicInbox/WwwStatic.pm @@ -1,4 +1,4 @@ -# Copyright (C) 2016-2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ # This package can either be a PSGI response body for a static file @@ -11,6 +11,7 @@ package PublicInbox::WwwStatic; use strict; use v5.10.1; use parent qw(Exporter); +use autodie qw(sysseek); use Fcntl qw(SEEK_SET O_RDONLY O_NONBLOCK); use HTTP::Date qw(time2str); use HTTP::Status qw(status_message); @@ -178,8 +179,7 @@ sub getline { my $len = $self->{len} or return; # undef, tells server we're done my $n = 8192; $n = $len if $len < $n; - sysseek($self->{in}, $self->{off}, SEEK_SET) or - die "sysseek ($self->{path}): $!"; + sysseek $self->{in}, $self->{off}, SEEK_SET; my $r = sysread($self->{in}, my $buf, $n); if (defined $r && $r > 0) { # success! $self->{len} = $len - $r; diff --git a/t/check-www-inbox.perl b/t/check-www-inbox.perl index 46f9ce1e8..1b9db49e2 100644 --- a/t/check-www-inbox.perl +++ b/t/check-www-inbox.perl @@ -4,7 +4,8 @@ # Parallel WWW checker my $usage = "$0 [-j JOBS] [-s SLOW_THRESHOLD] URL_OF_INBOX\n"; use strict; -use warnings; +use v5.10.1; +use autodie qw(sysseek); use File::Temp qw(tempfile); use GDBM_File; use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev); diff --git a/t/gcf2.t b/t/gcf2.t index 33f3bbca5..9f9e8e200 100644 --- a/t/gcf2.t +++ b/t/gcf2.t @@ -1,7 +1,9 @@ #!perl -w -# Copyright (C) 2020-2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ use strict; +use v5.10.1; +use autodie qw(seek); use PublicInbox::TestCommon; use Test::More; use Fcntl qw(:seek); @@ -80,18 +82,18 @@ SKIP: { $fh->autoflush(1); ok(!$gcf2->cat_oid(fileno($fh), 'invalid'), 'invalid fails'); - seek($fh, 0, SEEK_SET) or BAIL_OUT "seek: $!"; + seek $fh, 0, SEEK_SET; is(do { local $/; <$fh> }, '', 'nothing written'); open $fh, '+>', undef or BAIL_OUT "open: $!"; ok(!$gcf2->cat_oid(fileno($fh), '0'x40), 'z40 fails'); - seek($fh, 0, SEEK_SET) or BAIL_OUT "seek: $!"; + seek $fh, 0, SEEK_SET; is(do { local $/; <$fh> }, '', 'nothing written for z40'); open $fh, '+>', undef or BAIL_OUT "open: $!"; my $ck_copying = sub { my ($desc) = @_; - seek($fh, 0, SEEK_SET) or BAIL_OUT "seek: $!"; + seek $fh, 0, SEEK_SET; is(<$fh>, "$COPYING blob 34520\n", "got expected header $desc"); my $buf = do { local $/; <$fh> }; is(chop($buf), "\n", 'got trailing \\n'); @@ -113,7 +115,7 @@ SKIP: { fcntl($w, $F_SETPIPE_SZ, 4096) or skip('Linux too old for F_SETPIPE_SZ', 14); $w->blocking($blk); - seek($fh, 0, SEEK_SET) or BAIL_OUT "seek: $!"; + seek $fh, 0, SEEK_SET; truncate($fh, 0) or BAIL_OUT "truncate: $!"; my $pid = fork // BAIL_OUT "fork: $!"; if ($pid == 0) { diff --git a/t/gzip_filter.t b/t/gzip_filter.t index 97eac2d04..f827510f4 100644 --- a/t/gzip_filter.t +++ b/t/gzip_filter.t @@ -16,7 +16,7 @@ require_ok 'PublicInbox::GzipFilter'; ok($filter->write("hello"), 'wrote something'); ok($filter->write("world"), 'wrote more'); $filter->close; - seek($fh, 0, SEEK_SET) or die; + seek $fh, 0, SEEK_SET; IO::Uncompress::Gunzip::gunzip($fh => \(my $buf)); is($buf, 'helloworld', 'buffer matches'); } diff --git a/t/httpd-corner.t b/t/httpd-corner.t index 125610d60..e653f1e1a 100644 --- a/t/httpd-corner.t +++ b/t/httpd-corner.t @@ -5,7 +5,7 @@ # generic PSGI/Plack apps. use v5.12; use PublicInbox::TestCommon; use Time::HiRes qw(gettimeofday tv_interval); -use autodie qw(getsockopt setsockopt); +use autodie qw(getsockopt seek setsockopt); use PublicInbox::Spawn qw(spawn popen_rd); require_mods '-httpd'; use PublicInbox::SHA qw(sha1_hex); @@ -676,7 +676,7 @@ SKIP: { $req = GET('http://example.com/psgi-yield-enoent'); $res = $cb->($req); is($res->code, 500, 'got error on ENOENT'); - seek($tmperr, 0, SEEK_SET) or die; + seek $tmperr, 0, SEEK_SET; my $errbuf = do { local $/; <$tmperr> }; like($errbuf, qr/this-better-not-exist/, 'error logged about missing command'); diff --git a/t/import.t b/t/import.t index 7e2432e7e..48416eb2e 100644 --- a/t/import.t +++ b/t/import.t @@ -3,6 +3,7 @@ # License: AGPL-3.0+ use v5.10.1; use strict; +use autodie qw(seek); use PublicInbox::Eml; use PublicInbox::Smsg; use PublicInbox::Git; @@ -36,7 +37,7 @@ SKIP: { open my $in, '+<', undef or BAIL_OUT "open(+<): $!"; print $in $mime->as_string or die "write failed: $!"; $in->flush or die "flush failed: $!"; - seek($in, 0, SEEK_SET) or die "seek: $!"; + seek $in, 0, SEEK_SET; chomp(my $hashed_obj = xqx(\@cmd, undef, { 0 => $in })); is($?, 0, 'hash-object'); is($hashed_obj, $smsg->{blob}, "blob object_id matches exp"); diff --git a/t/ipc.t b/t/ipc.t index 23ae2e7b2..fc6f96a23 100644 --- a/t/ipc.t +++ b/t/ipc.t @@ -2,6 +2,7 @@ # Copyright (C) all contributors # License: AGPL-3.0+ use v5.12; +use autodie qw(seek); use PublicInbox::TestCommon; use Fcntl qw(SEEK_SET); use PublicInbox::SHA qw(sha1_hex); @@ -176,7 +177,7 @@ SKIP: { $ipc->wq_close; SKIP: { skip 'Socket::MsgHdr or Inline::C missing', 11 if !$ppids[0]; - seek($warn, 0, SEEK_SET) or BAIL_OUT; + seek $warn, 0, SEEK_SET; my @warn = <$warn>; is(scalar(@warn), 2, 'warned 3 times'); like($warn[0], qr/ wq_worker: /, '2nd warned from wq_worker'); diff --git a/t/lei-externals.t b/t/lei-externals.t index 4f2dd6baf..32e1255b8 100644 --- a/t/lei-externals.t +++ b/t/lei-externals.t @@ -1,8 +1,9 @@ #!perl -w -# Copyright (C) 2020-2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ use strict; use v5.10.1; use PublicInbox::TestCommon; use Fcntl qw(SEEK_SET); +use autodie qw(seek); require_git 2.6; require_mods(qw(json DBD::SQLite Xapian)); use POSIX qw(WTERMSIG WIFSIGNALED SIGPIPE); @@ -200,7 +201,7 @@ test_lei(sub { open my $fh, '+>', undef or BAIL_OUT $!; $fh->autoflush(1); print $fh 's:use d:..5.days.from.now' or BAIL_OUT $!; - seek($fh, 0, SEEK_SET) or BAIL_OUT $!; + seek $fh, 0, SEEK_SET; lei_ok([qw(q -q --stdin)], undef, { %$lei_opt, 0 => $fh }, \'--stdin on regular file works'); like($lei_out, qr/use boolean/, '--stdin on regular file'); diff --git a/t/lei-p2q.t b/t/lei-p2q.t index 44f37d195..778aa68f1 100644 --- a/t/lei-p2q.t +++ b/t/lei-p2q.t @@ -1,7 +1,8 @@ #!perl -w -# Copyright (C) 2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ use strict; use v5.10.1; use PublicInbox::TestCommon; +use autodie qw(sysseek); require_git 2.6; require_mods(qw(json DBD::SQLite Xapian)); @@ -15,7 +16,7 @@ test_lei(sub { lei_ok([qw(p2q -w dfpost -)], undef, { %$lei_opt, 0 => $fh }); is($lei_out, "dfpost:6e006fd73b1d\n", '--stdin') or diag $lei_err; - sysseek($fh, 0, 0) or xbail "lseek: $!"; + sysseek $fh, 0, 0; lei_ok([qw(p2q -w dfpost)], undef, { %$lei_opt, 0 => $fh }); is($lei_out, "dfpost:6e006fd73b1d\n", 'implicit --stdin'); diff --git a/t/lei-sigpipe.t b/t/lei-sigpipe.t index c01d9f836..db4f9288c 100644 --- a/t/lei-sigpipe.t +++ b/t/lei-sigpipe.t @@ -55,7 +55,7 @@ EOM close $w; vec(my $rvec = '', fileno($r), 1) = 1; if (!select($rvec, undef, undef, 30)) { - seek($errfh, 0, 0); + seek $errfh, 0, 0; my $s = do { local $/; <$errfh> }; xbail "lei q had no output after 30s, stderr=$s"; } @@ -64,7 +64,7 @@ EOM $tp->join; ok(WIFSIGNALED($?), "signaled @$out"); is(WTERMSIG($?), SIGPIPE, "got SIGPIPE @$out"); - seek($errfh, 0, 0); + seek $errfh, 0, 0; my $s = do { local $/; <$errfh> }; is($s, '', "quiet after sigpipe @$out"); } diff --git a/t/mbox_reader.t b/t/mbox_reader.t index 14248a2dd..1fa9068e3 100644 --- a/t/mbox_reader.t +++ b/t/mbox_reader.t @@ -1,9 +1,9 @@ #!perl -w -# Copyright (C) 2020-2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ use strict; use v5.10.1; -use Test::More; +use autodie qw(seek); use PublicInbox::TestCommon; use List::Util qw(shuffle); use PublicInbox::Eml; @@ -52,7 +52,7 @@ my $check_fmt = sub { my $buf = $eml2mbox->($eml); print $fh $$buf or BAIL_OUT "print $!"; } - seek($fh, 0, SEEK_SET) or BAIL_OUT "seek: $!"; + seek $fh, 0, SEEK_SET; $reader->$fmt($fh, sub { my ($eml) = @_; $eml->header_set('Status'); -- 2.47.3