user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 11/14] treewide: check alternates writes with eof + autodie
Date: Thu,  2 Nov 2023 09:35:36 +0000	[thread overview]
Message-ID: <20231102093539.2067470-12-e@80x24.org> (raw)
In-Reply-To: <20231102093539.2067470-1-e@80x24.org>

We must use eof() combined with close() to detect errors
in situations involving the readline()' op since `readline'
(and most buffered I/O libraries) have weak error detection
support.

This fixes error detection for files opened for read/write
access.  The next commit will fix error detection for read-only
files.
---
 lib/PublicInbox/LeiMirror.pm | 12 +++++-------
 lib/PublicInbox/LeiToMail.pm | 29 +++++++++++++++--------------
 2 files changed, 20 insertions(+), 21 deletions(-)

diff --git a/lib/PublicInbox/LeiMirror.pm b/lib/PublicInbox/LeiMirror.pm
index 8542c587..fb6517bd 100644
--- a/lib/PublicInbox/LeiMirror.pm
+++ b/lib/PublicInbox/LeiMirror.pm
@@ -506,14 +506,12 @@ EOM
 	if (!$self->{dry_run}) {
 		my $alt = File::Spec->rel2abs("$dir/objects");
 		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;
-		seek($fh, 0, SEEK_SET);
-		chomp(my @cur = <$fh>);
-		if (!grep(/\A\Q$l\E\z/, @cur)) {
-			say $fh $l;
-		}
+		open my $fh, '+>>', my $f = "$o/info/alternates";
+		seek($fh, 0, SEEK_SET); # Perl did SEEK_END when it saw '>>'
+		my $seen = grep(/\A\Q$l\E\n/, <$fh>); # error check with eof
+		eof($fh) or die "not at `$f' EOF ($!)"; # $! was set by readline
+		print $fh "$l\n" if !$seen;
 		close $fh;
 	}
 	bless {
diff --git a/lib/PublicInbox/LeiToMail.pm b/lib/PublicInbox/LeiToMail.pm
index b07c2c90..e80163e2 100644
--- a/lib/PublicInbox/LeiToMail.pm
+++ b/lib/PublicInbox/LeiToMail.pm
@@ -12,6 +12,7 @@ use PublicInbox::Spawn qw(spawn);
 use IO::Handle; # ->autoflush
 use Fcntl qw(SEEK_SET SEEK_END O_CREAT O_EXCL O_WRONLY);
 use PublicInbox::Syscall qw(rename_noreplace);
+use autodie qw(open seek close);
 
 my %kw2char = ( # Maildir characters
 	draft => 'D',
@@ -255,7 +256,7 @@ sub _buf2maildir ($$$$) {
 		$tmp = $dst.'tmp/'.$rand.$common;
 	} while (!($ok = sysopen($fh, $tmp, O_CREAT|O_EXCL|O_WRONLY)) &&
 		$!{EEXIST} && ($rand = _rand.','));
-	if ($ok && print $fh $$buf and close($fh)) {
+	if ($ok && print $fh $$buf and $fh->close) {
 		$dst .= $dir; # 'new/' or 'cur/'
 		$rand = '';
 		do {
@@ -458,7 +459,7 @@ sub _pre_augment_maildir {
 	require File::Path;
 	File::Path::make_path(map { $dst.$_ } qw(tmp new cur));
 	# for utime, so no opendir
-	open $self->{poke_dh}, '<', "${dst}cur" or die "open ${dst}cur: $!";
+	open $self->{poke_dh}, '<', "${dst}cur";
 }
 
 sub clobber_dst_prepare ($;$) {
@@ -538,11 +539,11 @@ sub _pre_augment_text {
 		$out = $lei->{$devfd};
 	} else { # normal-looking path
 		if (-p $dst) {
-			open $out, '>', $dst or die "open($dst): $!";
+			open $out, '>', $dst;
 		} elsif (-f _ || !-e _) {
 			# text allows augment, HTML/Atom won't
 			my $mode = $lei->{opt}->{augment} ? '>>' : '>';
-			open $out, $mode, $dst or die "open($mode, $dst): $!";
+			open $out, $mode, $dst;
 		} else {
 			die "$dst is not a file or FIFO\n";
 		}
@@ -561,7 +562,7 @@ sub _pre_augment_mbox {
 		$out = $lei->{$devfd};
 	} else { # normal-looking path
 		if (-p $dst) {
-			open $out, '>', $dst or die "open($dst): $!";
+			open $out, '>', $dst;
 		} elsif (-f _ || !-e _) {
 			require PublicInbox::MboxLock;
 			my $m = $lei->{opt}->{'lock'} //
@@ -574,7 +575,7 @@ sub _pre_augment_mbox {
 		$lei->{old_1} = $lei->{1}; # keep for spawning MUA
 	}
 	# Perl does SEEK_END even with O_APPEND :<
-	$self->{seekable} = seek($out, 0, SEEK_SET);
+	$self->{seekable} = $out->seek(0, SEEK_SET);
 	if (!$self->{seekable} && !$!{ESPIPE} && !defined($devfd)) {
 		die "seek($dst): $!\n";
 	}
@@ -616,7 +617,7 @@ sub _do_augment_mbox {
 	if (my $zsfx = $self->{zsfx}) {
 		$rd = PublicInbox::MboxReader::zsfxcat($out, $zsfx, $lei);
 	} else {
-		open($rd, '+>>&', $out) or die "dup: $!";
+		open($rd, '+>>&', $out);
 	}
 	my $dedupe;
 	if ($opt->{augment}) {
@@ -636,7 +637,7 @@ sub _do_augment_mbox {
 		PublicInbox::MboxReader->$fmt($rd, \&_augment, $lei);
 	}
 	# maybe some systems don't honor O_APPEND, Perl does this:
-	seek($out, 0, SEEK_END) or die "seek $dst: $!";
+	seek($out, 0, SEEK_END);
 	$dedupe->pause_dedupe if $dedupe;
 }
 
@@ -672,12 +673,12 @@ sub _pre_augment_v2 {
 	$lei->{v2w} = $v2w;
 	return if !$lei->{opt}->{shared};
 	my $d = "$lei->{ale}->{git}->{git_dir}/objects";
-	my $al = "$dir/git/0.git/objects/info/alternates";
-	open my $fh, '+>>', $al or die "open($al): $!";
-	seek($fh, 0, SEEK_SET) or die "seek($al): $!";
-	grep(/\A\Q$d\E\n/, <$fh>) and return;
-	print $fh "$d\n" or die "print($al): $!";
-	close $fh or die "close($al): $!";
+	open my $fh, '+>>', my $f = "$dir/git/0.git/objects/info/alternates";
+	seek($fh, 0, SEEK_SET); # Perl did SEEK_END when it saw '>>'
+	my $seen = grep(/\A\Q$d\E\n/, <$fh>);
+	eof($fh) or die "not at `$f' EOF ($!)"; # $! was set by readline
+	print $fh "$d\n" if !$seen;
+	close $fh;
 }
 
 sub pre_augment { # fast (1 disk seek), runs in same process as post_augment

  parent reply	other threads:[~2023-11-02  9:35 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-02  9:35 [PATCH 00/14] IO/IPC-related cleanups Eric Wong
2023-11-02  9:35 ` [PATCH 01/14] xap_helper.pm: use do_fork to Reset and reseed Eric Wong
2023-11-02  9:35 ` [PATCH 02/14] ds: replace FD map hash table with array Eric Wong
2023-11-02  9:35 ` [PATCH 03/14] treewide: use ->close method rather than CORE::close Eric Wong
2023-11-02 21:35   ` [PATCH 15/14] ds: don't try ->close after ->accept_SSL failure Eric Wong
2023-11-02  9:35 ` [PATCH 04/14] cindex: drop redundant close on regular FH Eric Wong
2023-11-02  9:35 ` [PATCH 05/14] treewide: use ->close to call ProcessIO->CLOSE Eric Wong
2023-11-02  9:35 ` [PATCH 06/14] multi_git: use autodie Eric Wong
2023-11-02  9:35 ` [PATCH 07/14] git_credential: use autodie where appropriate Eric Wong
2023-11-02  9:35 ` [PATCH 08/14] replace ProcessIO with untied PublicInbox::IO Eric Wong
2023-11-02  9:35 ` [PATCH 09/14] io: introduce write_file helper sub Eric Wong
2023-11-02  9:35 ` [PATCH 10/14] spawn: support PerlIO layer in scalar redirects Eric Wong
2023-11-02  9:35 ` Eric Wong [this message]
2023-11-02  9:35 ` [PATCH 12/14] treewide: use eof and close to detect readline errors Eric Wong
2023-11-02  9:35 ` [PATCH 13/14] move read_all, try_cat, and poll_in to PublicInbox::IO Eric Wong
2023-11-02 20:59   ` www: squash read_all usage fix Eric Wong
2023-11-02  9:35 ` [PATCH 14/14] t/cindex+extsearch: use write_file, autodie, etc 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=20231102093539.2067470-12-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.
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).