unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 01/30] lei_mirror: start converting to autodie
Date: Tue, 17 Oct 2023 23:37:46 +0000	[thread overview]
Message-ID: <20231017233815.1637932-2-e@80x24.org> (raw)
In-Reply-To: <20231017233815.1637932-1-e@80x24.org>

This code is too noisy and not critical for startup performance;
so autodie provides a nice noise reduction while improving error
reporting in most cases.

For places where failures are expected, the `CORE::' prefix
gives us an easy escape hatch to fall back to normal error
checking.
---
 lib/PublicInbox/LeiMirror.pm | 38 ++++++++++++++++++------------------
 1 file changed, 19 insertions(+), 19 deletions(-)

diff --git a/lib/PublicInbox/LeiMirror.pm b/lib/PublicInbox/LeiMirror.pm
index 9d8a8963..f4c16a9d 100644
--- a/lib/PublicInbox/LeiMirror.pm
+++ b/lib/PublicInbox/LeiMirror.pm
@@ -21,6 +21,7 @@ use PublicInbox::LeiCurl;
 use PublicInbox::OnDestroy;
 use PublicInbox::SHA qw(sha256_hex sha1_hex);
 use POSIX qw(strftime);
+use autodie qw(chmod pipe readlink seek sysopen truncate unlink);
 
 our $LIVE; # pid => callback
 our $FGRP_TODO; # objstore -> [[ to resume ], [ to clone ]]
@@ -122,7 +123,7 @@ sub ft_rename ($$$;$) {
 	my ($ft, $dst, $open_mode, $fh) = @_;
 	my @st = stat($fh // $dst);
 	my $mode = @st ? ($st[2] & 07777) : ($open_mode & ~umask);
-	chmod($mode, $ft) or croak "E: chmod($ft): $!";
+	chmod($mode, $ft);
 	require File::Copy;
 	File::Copy::mv($ft->filename, $dst) or croak "E: mv($ft => $dst): $!";
 	$ft->unlink_on_destroy(0);
@@ -170,7 +171,7 @@ sub _get_txt_done { # returns true on error (non-fatal), undef on success
 	$? = 0; # don't influence normal lei exit
 	return warn("$uri missing\n") if ($cerr >> 8) == 22;
 	return warn("# @$cmd failed (non-fatal)\n") if $cerr;
-	seek($fh, SEEK_SET, 0) or die "seek: $!";
+	seek($fh, SEEK_SET, 0);
 	$self->{"mtime.$endpoint"} = (stat($fh))[9];
 	local $/;
 	$self->{"txt.$endpoint"} = <$fh>;
@@ -183,9 +184,9 @@ sub _write_inbox_config {
 	my $dst = $self->{cur_dst} // $self->{dst};
 	my $f = "$dst/inbox.config.example";
 	my $mtime = delete $self->{'mtime._/text/config/raw'};
-	if (sysopen(my $fh, $f, O_CREAT|O_EXCL|O_WRONLY)) {
+	if (CORE::sysopen(my $fh, $f, O_CREAT|O_EXCL|O_WRONLY)) {
 		print $fh $buf or die "print: $!";
-		chmod(0444 & ~umask, $fh) or die "chmod($f): $!";
+		chmod(0444 & ~umask, $fh);
 		$fh->flush or die "flush($f): $!";
 		if (defined $mtime) {
 			utime($mtime, $mtime, $fh) or die "utime($f): $!";
@@ -289,7 +290,7 @@ sub upr { # feed `git update-ref --stdin -z' verbosely
 
 sub start_update_ref {
 	my ($fgrp) = @_;
-	pipe(my ($r, $w)) or die "pipe: $!";
+	pipe(my $r, my $w);
 	my $cmd = [ 'git', "--git-dir=$fgrp->{cur_dst}",
 		qw(update-ref --stdin -z) ];
 	my $pack = PublicInbox::OnDestroy->new($$, \&satellite_done, $fgrp);
@@ -305,8 +306,8 @@ sub fgrp_update {
 	return if !keep_going($fgrp);
 	my $srcfh = delete $fgrp->{srcfh} or return;
 	my $dstfh = delete $fgrp->{dstfh} or return;
-	seek($srcfh, SEEK_SET, 0) or die "seek(src): $!";
-	seek($dstfh, SEEK_SET, 0) or die "seek(dst): $!";
+	seek($srcfh, SEEK_SET, 0);
+	seek($dstfh, SEEK_SET, 0);
 	my %src = map { chomp; split(/\0/) } (<$srcfh>);
 	close $srcfh;
 	my %dst = map { chomp; split(/\0/) } (<$dstfh>);
@@ -359,7 +360,7 @@ sub pack_refs {
 
 sub unlink_fetch_head ($) {
 	my ($git_dir) = @_;
-	return if unlink("$git_dir/FETCH_HEAD") || $!{ENOENT};
+	return if CORE::unlink("$git_dir/FETCH_HEAD") || $!{ENOENT};
 	warn "W: unlink($git_dir/FETCH_HEAD): $!";
 }
 
@@ -447,8 +448,7 @@ sub fgrp_fetch_all {
 		if (!$self->{dry_run}) {
 			# update the config atomically via O_APPEND while
 			# respecting git-config locking
-			sysopen(my $lk, "$f.lock", O_CREAT|O_EXCL|O_WRONLY)
-				or die "open($f.lock): $!";
+			sysopen(my $lk, "$f.lock", O_CREAT|O_EXCL|O_WRONLY);
 			open my $fh, '>>', $f or die "open(>>$f): $!";
 			$fh->autoflush(1);
 			my $buf = '';
@@ -464,7 +464,7 @@ sub fgrp_fetch_all {
 				(map { "\t$grp = $_->{-remote}\n" } @$new));
 			print $fh $buf or die "print($f): $!";
 			close $fh or die "close($f): $!";
-			unlink("$f.lock") or die "unlink($f.lock): $!";
+			unlink("$f.lock");
 		}
 		$cmd  = [ @git, "--git-dir=$osdir", @fetch, $grp ];
 		push @$old, @$new;
@@ -515,7 +515,7 @@ EOM
 		my $f = "$o/info/alternates";
 		my $l = File::Spec->abs2rel($alt, File::Spec->rel2abs($o));
 		open my $fh, '+>>', $f or die "open($f): $!";
-		seek($fh, SEEK_SET, 0) or die "seek($f): $!";
+		seek($fh, SEEK_SET, 0);
 		chomp(my @cur = <$fh>);
 		if (!grep(/\A\Q$l\E\z/, @cur)) {
 			say $fh $l or die "say($f): $!";
@@ -535,7 +535,7 @@ sub fp_done {
 	}
 	return if !keep_going($self);
 	my $fh = delete $self->{-show_ref} // die 'BUG: no show-ref output';
-	seek($fh, SEEK_SET, 0) or die "seek(show_ref): $!";
+	seek($fh, SEEK_SET, 0);
 	$self->{-ent} // die 'BUG: no -ent';
 	my $A = $self->{-ent}->{fingerprint} // die 'BUG: no fingerprint';
 	my $B = sha1_hex(do { local $/; <$fh> } // die("read(show_ref): $!"));
@@ -735,7 +735,7 @@ sub up_fp_done {
 	my ($self) = @_;
 	return if !keep_going($self);
 	my $fh = delete $self->{-show_ref_up} // die 'BUG: no show-ref output';
-	seek($fh, SEEK_SET, 0) or die "seek(show_ref): $!";
+	seek($fh, SEEK_SET, 0);
 	$self->{-ent} // die 'BUG: no -ent';
 	my $A = $self->{-ent}->{fingerprint} // die 'BUG: no fingerprint';
 	my $B = sha1_hex(do { local $/; <$fh> } // die("read(show_ref): $!"));
@@ -811,7 +811,7 @@ sub update_ent {
 			if (lstat($ln)) {
 				if (-l _) {
 					next if readlink($ln) eq $tgt;
-					unlink($ln) or die "unlink($ln): $!";
+					unlink($ln);
 				} else {
 					push @{$self->{chg}->{badlink}}, $p;
 				}
@@ -882,7 +882,7 @@ sub v2_done { # called via OnDestroy
 	for my $i ($mg->git_epochs) { $mg->epoch_cfg_set($i) }
 	for my $edst (@{delete($self->{-read_only}) // []}) {
 		my @st = stat($edst) or die "stat($edst): $!";
-		chmod($st[2] & 0555, $edst) or die "chmod(a-w, $edst): $!";
+		chmod($st[2] & 0555, $edst);
 	}
 	write_makefile($dst, 2);
 	undef $lck; # unlock
@@ -1089,8 +1089,8 @@ sub dump_manifest ($$) {
 	# epoch they no longer want to skip
 	my $json = PublicInbox::Config->json->encode($m);
 	my $mtime = (stat($ft))[9];
-	seek($ft, SEEK_SET, 0) or die "seek($ft): $!";
-	truncate($ft, 0) or die "truncate($ft): $!";
+	seek($ft, SEEK_SET, 0);
+	truncate($ft, 0);
 	gzip(\$json => $ft) or die "gzip($ft): $GzipError";
 	$ft->flush or die "flush($ft): $!";
 	utime($mtime, $mtime, "$ft") or die "utime(..., $ft): $!";
@@ -1344,7 +1344,7 @@ sub ipc_atfork_child {
 sub write_makefile {
 	my ($dir, $ibx_ver) = @_;
 	my $f = "$dir/Makefile";
-	if (sysopen my $fh, $f, O_CREAT|O_EXCL|O_WRONLY) {
+	if (CORE::sysopen my $fh, $f, O_CREAT|O_EXCL|O_WRONLY) {
 		print $fh <<EOM or die "print($f) $!";
 # This is a v$ibx_ver public-inbox, see the public-inbox-v$ibx_ver-format(5)
 # manpage for more information on the format.  This Makefile is

  reply	other threads:[~2023-10-17 23:38 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-17 23:37 [PATCH 00/30] autodie-ification and code simplifications Eric Wong
2023-10-17 23:37 ` Eric Wong [this message]
2023-10-17 23:37 ` [PATCH 02/30] lei_mirror: autodie most `close' calls Eric Wong
2023-10-17 23:37 ` [PATCH 03/30] lei_mirror: use autodie for most `open' calls Eric Wong
2023-10-17 23:37 ` [PATCH 04/30] git: introduce read_all function Eric Wong
2023-10-17 23:37 ` [PATCH 05/30] import: use read_all to detect short reads Eric Wong
2023-10-17 23:37 ` [PATCH 06/30] lei_mirror: use read_all Eric Wong
2023-10-17 23:37 ` [PATCH 07/30] use read_all in more places to improve safety Eric Wong
2023-10-17 23:37 ` [PATCH 08/30] xap_helper*: use autodie in more places Eric Wong
2023-10-17 23:37 ` [PATCH 09/30] xap_helper: die more easily in both implementations Eric Wong
2023-10-17 23:37 ` [PATCH 10/30] xap_helper: simplify SIGTERM exit checks Eric Wong
2023-10-17 23:37 ` [PATCH 11/30] xap_helper: autodie for getsockopt Eric Wong
2023-10-17 23:37 ` [PATCH 12/30] xap_client: autodie for pipe and socketpair Eric Wong
2023-10-17 23:37 ` [PATCH 13/30] xt/git-http-backend: remove Net::HTTP usage Eric Wong
2023-10-17 23:37 ` [PATCH 14/30] ds: introduce and use do_fork helper Eric Wong
2023-10-17 23:38 ` [PATCH 15/30] ds: get rid of SetLoopTimeout Eric Wong
2023-10-17 23:38 ` [PATCH 16/30] cindex: drop some unused functions Eric Wong
2023-10-17 23:38 ` [PATCH 17/30] syscall: common $F_SETPIPE_SZ definition Eric Wong
2023-10-17 23:38 ` [PATCH 18/30] t/lei-up: additional diagnostics for match failures Eric Wong
2023-10-17 23:38 ` [PATCH 19/30] test_common: use autodie and read_all where possible Eric Wong
2023-10-17 23:38 ` [PATCH 20/30] test_common: only hide TCP port in messages Eric Wong
2023-10-17 23:38 ` [PATCH 21/30] test_common: use $cwdfh for every run_script command Eric Wong
2023-10-17 23:38 ` [PATCH 22/30] init: drop extraneous `+' Eric Wong
2023-10-17 23:38 ` [PATCH 23/30] init: use autodie to reduce distractions Eric Wong
2023-10-17 23:38 ` [PATCH 24/30] xt/mem-imapd-tls: remove unused/broken epoll imports Eric Wong
2023-10-17 23:38 ` [PATCH 25/30] xt/mem-imapd-tls: reduce FDs for lsof use Eric Wong
2023-10-17 23:38 ` [PATCH 26/30] lei: use autodie where appropriate Eric Wong
2023-10-17 23:38 ` [PATCH 27/30] lei_auth: update comments and use v5.12 Eric Wong
2023-10-17 23:38 ` [PATCH 28/30] lei_config: drop redundant open check Eric Wong
2023-10-17 23:38 ` [PATCH 29/30] convert: use read_all to simplify error checks Eric Wong
2023-10-17 23:38 ` [PATCH 30/30] idx_stack: use autodie + read_all Eric Wong
2023-10-19  1:14 ` [PATCH 31/30] lei: simplify startq/au_done wakeup notifications Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231017233815.1637932-2-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).