user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
Search results ordered by [date|relevance]  view[summary|nested|Atom feed]
thread overview below | download mbox.gz: |
* [PATCH 03/30] lei_mirror: use autodie for most `open' calls
  2023-10-17 23:37  7% [PATCH 00/30] autodie-ification and code simplifications Eric Wong
@ 2023-10-17 23:37  4% ` Eric Wong
  0 siblings, 0 replies; 2+ results
From: Eric Wong @ 2023-10-17 23:37 UTC (permalink / raw)
  To: meta

We'll also drop most `print' checks since we need to rely on
`close' or IO::Handle->flush for error checking, anyways.
---
 lib/PublicInbox/LeiMirror.pm | 62 +++++++++++++++++-------------------
 1 file changed, 29 insertions(+), 33 deletions(-)

diff --git a/lib/PublicInbox/LeiMirror.pm b/lib/PublicInbox/LeiMirror.pm
index 84344f37..b402eb5f 100644
--- a/lib/PublicInbox/LeiMirror.pm
+++ b/lib/PublicInbox/LeiMirror.pm
@@ -21,7 +21,8 @@ use PublicInbox::LeiCurl;
 use PublicInbox::OnDestroy;
 use PublicInbox::SHA qw(sha256_hex sha1_hex);
 use POSIX qw(strftime);
-use autodie qw(chmod close pipe readlink seek sysopen truncate unlink);
+use autodie qw(chdir chmod close open pipe readlink seek symlink sysopen
+		truncate unlink);
 
 our $LIVE; # pid => callback
 our $FGRP_TODO; # objstore -> [[ to resume ], [ to clone ]]
@@ -185,7 +186,7 @@ sub _write_inbox_config {
 	my $f = "$dst/inbox.config.example";
 	my $mtime = delete $self->{'mtime._/text/config/raw'};
 	if (CORE::sysopen(my $fh, $f, O_CREAT|O_EXCL|O_WRONLY)) {
-		print $fh $buf or die "print: $!";
+		print $fh $buf;
 		chmod(0444 & ~umask, $fh);
 		$fh->flush or die "flush($f): $!";
 		if (defined $mtime) {
@@ -381,12 +382,12 @@ sub fgrpv_done {
 		my $src = [ 'git', "--git-dir=$fgrp->{-osdir}", 'for-each-ref',
 			"--format=refs/%(refname:lstrip=3)%00%(objectname)",
 			"refs/remotes/$rn/" ];
-		open(my $sfh, '+>', undef) or die "open(src): $!";
+		open(my $sfh, '+>', undef);
 		$fgrp->{srcfh} = $sfh;
 		start_cmd($fgrp, $src, { %opt, 1 => $sfh }, $update_ref);
 		my $dst = [ 'git', "--git-dir=$fgrp->{cur_dst}", 'for-each-ref',
 			'--format=%(refname)%00%(objectname)' ];
-		open(my $dfh, '+>', undef) or die "open(dst): $!";
+		open(my $dfh, '+>', undef);
 		$fgrp->{dstfh} = $dfh;
 		start_cmd($fgrp, $dst, { %opt, 1 => $dfh }, $update_ref);
 	}
@@ -449,7 +450,7 @@ sub fgrp_fetch_all {
 			# update the config atomically via O_APPEND while
 			# respecting git-config locking
 			sysopen(my $lk, "$f.lock", O_CREAT|O_EXCL|O_WRONLY);
-			open my $fh, '>>', $f or die "open(>>$f): $!";
+			open my $fh, '>>', $f;
 			$fh->autoflush(1);
 			my $buf = '';
 			if (@$old) {
@@ -482,9 +483,8 @@ sub forkgroup_prep {
 	my $dir = "$os/$fg.git";
 	if (!-d $dir && !$self->{dry_run}) {
 		PublicInbox::Import::init_bare($dir);
-		my $f = "$dir/config";
-		open my $fh, '+>>', $f or die "open:($f): $!";
-		print $fh <<EOM or die "print($f): $!";
+		open my $fh, '+>>', "$dir/config";
+		print $fh <<EOM;
 [repack]
 	useDeltaIslands = true
 [pack]
@@ -496,9 +496,8 @@ EOM
 	my $rn = substr(sha256_hex($key), 0, 16);
 	if (!-d $self->{cur_dst} && !$self->{dry_run}) {
 		PublicInbox::Import::init_bare($self->{cur_dst});
-		my $f = "$self->{cur_dst}/config";
-		open my $fh, '+>>', $f or die "open:($f): $!";
-		print $fh <<EOM or die "print($f): $!";
+		open my $fh, '+>>', "$self->{cur_dst}/config";
+		print $fh <<EOM;
 ; rely on the "$rn" remote in the
 ; $fg fork group for fetches
 ; only uncomment the following iff you detach from fork groups
@@ -514,11 +513,11 @@ EOM
 		my $o = "$self->{cur_dst}/objects";
 		my $f = "$o/info/alternates";
 		my $l = File::Spec->abs2rel($alt, File::Spec->rel2abs($o));
-		open my $fh, '+>>', $f or die "open($f): $!";
+		open my $fh, '+>>', $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): $!";
+			say $fh $l;
 		}
 		close $fh;
 	}
@@ -556,7 +555,7 @@ sub cmp_fp_do {
 	my $dst = $self->{cur_dst} // $self->{dst};
 	my $cmd = ['git', "--git-dir=$dst", 'show-ref'];
 	my $opt = { 2 => $self->{lei}->{2} };
-	open($opt->{1}, '+>', undef) or die "open(tmp): $!";
+	open($opt->{1}, '+>', undef);
 	$self->{-show_ref} = $opt->{1};
 	do_reap($self);
 	$self->{lei}->qerr("# @$cmd");
@@ -695,8 +694,8 @@ sub init_placeholder ($$$) {
 	my ($src, $edst, $ent) = @_;
 	PublicInbox::Import::init_bare($edst);
 	my $f = "$edst/config";
-	open my $fh, '>>', $f or die "open($f): $!";
-	print $fh <<EOM or die "print($f): $!";
+	open my $fh, '>>', $f;
+	print $fh <<EOM;
 [remote "origin"]
 	url = $src
 	fetch = +refs/*:refs/*
@@ -706,20 +705,17 @@ sub init_placeholder ($$$) {
 ; will not fetch updates for it unless write permission is added.
 ; Hint: chmod +w $edst
 EOM
-	if (defined($ent->{owner})) {
-		print $fh <<EOM or die "print($f): $!";
+	print $fh <<EOM if defined($ent->{owner});
 [gitweb]
 	owner = $ent->{owner}
 EOM
-	}
 	close $fh;
 	my %map = (head => 'HEAD', description => undef);
 	while (my ($key, $fn) = each %map) {
 		my $val = $ent->{$key} // next;
 		$fn //= $key;
-		$fn = "$edst/$fn";
-		open $fh, '>', $fn or die "open($fn): $!";
-		print $fh $val, "\n" or die "print($fn): $!";
+		open $fh, '>', "$edst/$fn";
+		say $fh $val;
 		close $fh;
 	}
 }
@@ -747,7 +743,7 @@ sub up_fp_done {
 sub atomic_write ($$$) {
 	my ($dn, $bn, $raw) = @_;
 	my $ft = File::Temp->new(DIR => $dn, TEMPLATE => "$bn-XXXX");
-	print $ft $raw or die "print($ft): $!";
+	print $ft $raw;
 	$ft->flush or die "flush($ft): $!";
 	ft_rename($ft, "$dn/$bn", 0666);
 }
@@ -778,7 +774,7 @@ sub update_ent {
 	if (defined($new) && $new ne $cur) {
 		my $cmd = ['git', "--git-dir=$dst", 'show-ref'];
 		my $opt = { 2 => $self->{lei}->{2} };
-		open($opt->{1}, '+>', undef) or die "open(tmp): $!";
+		open($opt->{1}, '+>', undef);
 		$self->{-show_ref_up} = $opt->{1};
 		my $done = PublicInbox::OnDestroy->new($$, \&up_fp_done, $self);
 		start_cmd($self, $cmd, $opt, $done);
@@ -816,7 +812,7 @@ sub update_ent {
 					push @{$self->{chg}->{badlink}}, $p;
 				}
 			}
-			symlink($tgt, $ln) or die "symlink($tgt, $ln): $!";
+			symlink($tgt, $ln);
 			++$self->{chg}->{nr_chg};
 		}
 	}
@@ -844,7 +840,7 @@ sub v1_done { # called via OnDestroy
 	unlink_fetch_head($dst);
 	update_ent($self) if $self->{-ent};
 	my $o = "$dst/objects";
-	if (open(my $fh, '<', my $fn = "$o/info/alternates")) {;
+	if (CORE::open(my $fh, '<', my $fn = "$o/info/alternates")) {;
 		my $base = File::Spec->rel2abs($o);
 		my @l = <$fh>;
 		my $ft;
@@ -855,7 +851,7 @@ sub v1_done { # called via OnDestroy
 						DIR => "$o/info");
 		}
 		if ($ft) {
-			print $ft @l or die "print($ft): $!";
+			print $ft @l;
 			$ft->flush or die "flush($ft): $!";
 			ft_rename($ft, $fn, 0666, $fh);
 		}
@@ -964,7 +960,7 @@ sub decode_manifest ($$$) {
 sub load_current_manifest ($) {
 	my ($self) = @_;
 	my $fn = $self->{-manifest} // return;
-	if (open(my $fh, '<', $fn)) {
+	if (CORE::open(my $fh, '<', $fn)) {
 		decode_manifest($fh, $fn, $fn);
 	} elsif ($!{ENOENT}) { # non-fatal, we can just do it slowly
 		warn "open($fn): $!\n" if !$self->{-initial_clone};
@@ -1102,12 +1098,12 @@ sub dump_project_list ($$) {
 	my $old = defined($f) ? PublicInbox::Git::try_cat($f) : '';
 	my %new;
 
-	open my $dh, '<', '.' or die "open(.): $!";
+	open my $dh, '<', '.';
 	if (!$self->{dry_run} || -d $self->{dst}) {
-		chdir($self->{dst}) or die "chdir($self->{dst}): $!";
+		chdir($self->{dst});
 	}
 	my @local = grep { -e $_ ? ($new{$_} = undef) : 1 } split(/\n/s, $old);
-	chdir($dh) or die "chdir(restore): $!";
+	chdir($dh);
 
 	$new{substr($_, 1)} = 1 for keys %$m; # drop leading '/'
 	my @list = sort keys %new;
@@ -1345,7 +1341,7 @@ sub write_makefile {
 	my ($dir, $ibx_ver) = @_;
 	my $f = "$dir/Makefile";
 	if (CORE::sysopen my $fh, $f, O_CREAT|O_EXCL|O_WRONLY) {
-		print $fh <<EOM or die "print($f) $!";
+		print $fh <<EOM;
 # 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
 # intended as a familiar wrapper for users unfamiliar with
@@ -1359,7 +1355,7 @@ sub write_makefile {
 # so you may edit it freely with your own convenience targets
 # and notes.  public-inbox-fetch will recreate it if removed.
 EOM
-		print $fh <<'EOM' or die "print($f): $!";
+		print $fh <<'EOM';
 # the default target:
 help :
 	@echo Common targets:

^ permalink raw reply related	[relevance 4%]

* [PATCH 00/30] autodie-ification and code simplifications
@ 2023-10-17 23:37  7% Eric Wong
  2023-10-17 23:37  4% ` [PATCH 03/30] lei_mirror: use autodie for most `open' calls Eric Wong
  0 siblings, 1 reply; 2+ results
From: Eric Wong @ 2023-10-17 23:37 UTC (permalink / raw)
  To: meta

Noisy code is less pleasant to work on, so use autodie more and
a few more simplifications.  There's a couple of small bugfixes
discovered along the way, too.

Eric Wong (30):
  lei_mirror: start converting to autodie
  lei_mirror: autodie most `close' calls
  lei_mirror: use autodie for most `open' calls
  git: introduce read_all function
  import: use read_all to detect short reads
  lei_mirror: use read_all
  use read_all in more places to improve safety
  xap_helper*: use autodie in more places
  xap_helper: die more easily in both implementations
  xap_helper: simplify SIGTERM exit checks
  xap_helper: autodie for getsockopt
  xap_client: autodie for pipe and socketpair
  xt/git-http-backend: remove Net::HTTP usage
  ds: introduce and use do_fork helper
  ds: get rid of SetLoopTimeout
  cindex: drop some unused functions
  syscall: common $F_SETPIPE_SZ definition
  t/lei-up: additional diagnostics for match failures
  test_common: use autodie and read_all where possible
  test_common: only hide TCP port in messages
  test_common: use $cwdfh for every run_script command
  init: drop extraneous `+'
  init: use autodie to reduce distractions
  xt/mem-imapd-tls: remove unused/broken epoll imports
  xt/mem-imapd-tls: reduce FDs for lsof use
  lei: use autodie where appropriate
  lei_auth: update comments and use v5.12
  lei_config: drop redundant open check
  convert: use read_all to simplify error checks
  idx_stack: use autodie + read_all

 lib/PublicInbox/CidxLogP.pm       |   4 +-
 lib/PublicInbox/CodeSearchIdx.pm  |   5 --
 lib/PublicInbox/DS.pm             |  36 ++++----
 lib/PublicInbox/Daemon.pm         |  16 ++--
 lib/PublicInbox/EOFpipe.pm        |   6 +-
 lib/PublicInbox/Gcf2.pm           |   7 +-
 lib/PublicInbox/Git.pm            |  19 +++--
 lib/PublicInbox/IPC.pm            |  12 +--
 lib/PublicInbox/IdxStack.pm       |  20 ++---
 lib/PublicInbox/Import.pm         |   8 +-
 lib/PublicInbox/InboxWritable.pm  |   6 +-
 lib/PublicInbox/LEI.pm            |  48 +++++------
 lib/PublicInbox/LeiALE.pm         |  11 +--
 lib/PublicInbox/LeiAuth.pm        |   7 +-
 lib/PublicInbox/LeiBlob.pm        |   6 +-
 lib/PublicInbox/LeiConfig.pm      |   4 +-
 lib/PublicInbox/LeiMailSync.pm    |   5 +-
 lib/PublicInbox/LeiMirror.pm      | 131 ++++++++++++++----------------
 lib/PublicInbox/LeiSucks.pm       |   5 +-
 lib/PublicInbox/LeiXSearch.pm     |   2 +-
 lib/PublicInbox/MultiGit.pm       |   3 +-
 lib/PublicInbox/SearchIdxShard.pm |  14 ++--
 lib/PublicInbox/Syscall.pm        |  16 ++--
 lib/PublicInbox/TestCommon.pm     |  85 +++++++++----------
 lib/PublicInbox/ViewVCS.pm        |  12 ++-
 lib/PublicInbox/WWW.pm            |   4 +-
 lib/PublicInbox/Watch.pm          |  11 +--
 lib/PublicInbox/XapClient.pm      |  11 +--
 lib/PublicInbox/XapHelper.pm      |  24 ++----
 lib/PublicInbox/XapHelperCxx.pm   |  11 +--
 lib/PublicInbox/Xapcmd.pm         |   5 +-
 lib/PublicInbox/xap_helper.h      |  60 ++++++--------
 script/public-inbox-convert       |   8 +-
 script/public-inbox-edit          |   4 +-
 script/public-inbox-init          |  30 +++----
 t/dir_idle.t                      |   2 +-
 t/ds-leak.t                       |   4 +-
 t/gcf2.t                          |   5 +-
 t/init.t                          |   7 ++
 t/lei-sigpipe.t                   |   7 +-
 t/lei-up.t                        |   4 +-
 xt/git-http-backend.t             |  30 +++----
 xt/mem-imapd-tls.t                |  21 ++---
 xt/mem-nntpd-tls.t                |   8 +-
 44 files changed, 335 insertions(+), 409 deletions(-)

^ permalink raw reply	[relevance 7%]

Results 1-2 of 2 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2023-10-17 23:37  7% [PATCH 00/30] autodie-ification and code simplifications Eric Wong
2023-10-17 23:37  4% ` [PATCH 03/30] lei_mirror: use autodie for most `open' calls Eric Wong

Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

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).