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 2/2] git: ensure subclassed ->fail gets called
Date: Sun, 29 Nov 2020 10:52:30 +0000	[thread overview]
Message-ID: <20201129105230.12501-3-e@80x24.org> (raw)
In-Reply-To: <20201129105230.12501-1-e@80x24.org>

Some of these changes may not be strictly necessary, but it
makes code easier to maintain and change.  Hackers
using/modifying this code will no longer wonder if a particular
callsite needs to care about subclasses or not.
---
 lib/PublicInbox/Git.pm | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/lib/PublicInbox/Git.pm b/lib/PublicInbox/Git.pm
index d53427d7..08406925 100644
--- a/lib/PublicInbox/Git.pm
+++ b/lib/PublicInbox/Git.pm
@@ -96,9 +96,9 @@ sub alternates_changed {
 sub last_check_err {
 	my ($self) = @_;
 	my $fh = $self->{err_c} or return;
-	sysseek($fh, 0, 0) or fail($self, "sysseek failed: $!");
+	sysseek($fh, 0, 0) or $self->fail("sysseek failed: $!");
 	defined(sysread($fh, my $buf, -s $fh)) or
-			fail($self, "sysread failed: $!");
+			$self->fail("sysread failed: $!");
 	$buf;
 }
 
@@ -107,19 +107,19 @@ sub _bidi_pipe {
 	if ($self->{$pid}) {
 		if (defined $err) { # "err_c"
 			my $fh = $self->{$err};
-			sysseek($fh, 0, 0) or fail($self, "sysseek failed: $!");
-			truncate($fh, 0) or fail($self, "truncate failed: $!");
+			sysseek($fh, 0, 0) or $self->fail("sysseek failed: $!");
+			truncate($fh, 0) or $self->fail("truncate failed: $!");
 		}
 		return;
 	}
 	my ($out_r, $out_w);
-	pipe($out_r, $out_w) or fail($self, "pipe failed: $!");
+	pipe($out_r, $out_w) or $self->fail("pipe failed: $!");
 	my @cmd = (qw(git), "--git-dir=$self->{git_dir}",
 			qw(-c core.abbrev=40 cat-file), $batch);
 	my $redir = { 0 => $out_r };
 	if ($err) {
 		my $id = "git.$self->{git_dir}$batch.err";
-		my $fh = tmpfile($id) or fail($self, "tmpfile($id): $!");
+		my $fh = tmpfile($id) or $self->fail("tmpfile($id): $!");
 		$self->{$err} = $fh;
 		$redir->{2} = $fh;
 	}
@@ -187,7 +187,7 @@ sub cat_async_retry ($$$$$) {
 	for (my $i = 0; $i < @$inflight; $i += 3) {
 		$buf .= "$inflight->[$i]\n";
 	}
-	print { $self->{out} } $buf or fail($self, "write error: $!");
+	print { $self->{out} } $buf or $self->fail("write error: $!");
 	unshift(@$inflight, \$req, $cb, $arg); # \$ref to indicate retried
 
 	cat_async_step($self, $inflight); # take one step
@@ -265,7 +265,7 @@ sub check_async_step ($$) {
 	# https://public-inbox.org/git/20190118033845.s2vlrb3wd3m2jfzu@dcvr/T/
 	if ($hex eq 'dangling' || $hex eq 'notdir' || $hex eq 'loop') {
 		my $ret = my_read($self->{in_c}, $rbuf, $type + 1);
-		fail($self, defined($ret) ? 'read EOF' : "read: $!") if !$ret;
+		$self->fail(defined($ret) ? 'read EOF' : "read: $!") if !$ret;
 	}
 	$self->{chk_rbuf} = $rbuf if $$rbuf ne '';
 	eval { $cb->($hex, $type, $size, $arg, $self) };
@@ -294,7 +294,7 @@ sub check_async ($$$$) {
 	while (scalar(@$inflight_c) >= MAX_INFLIGHT) {
 		check_async_step($self, $inflight_c);
 	}
-	print { $self->{out_c} } $oid, "\n" or fail($self, "write error: $!");
+	print { $self->{out_c} } $oid, "\n" or $self->fail("write error: $!");
 	push(@$inflight_c, $oid, $cb, $arg);
 }
 
@@ -347,7 +347,7 @@ sub cat_async_abort ($) {
 	cleanup($self);
 }
 
-sub fail {
+sub fail { # may be augmented in subclasses
 	my ($self, $msg) = @_;
 	cat_async_abort($self);
 	croak(ref($self) . ' ' . ($self->{git_dir} // '') . ": $msg");
@@ -449,7 +449,7 @@ sub cat_async ($$$;$) {
 	while (scalar(@$inflight) >= MAX_INFLIGHT) {
 		cat_async_step($self, $inflight);
 	}
-	print { $self->{out} } $oid, "\n" or fail($self, "write error: $!");
+	print { $self->{out} } $oid, "\n" or $self->fail("write error: $!");
 	push(@$inflight, $oid, $cb, $arg);
 }
 
@@ -460,7 +460,7 @@ sub async_prefetch {
 		# but lets not allow one client to monopolize a git process
 		if (scalar(@$inflight) < int(MAX_INFLIGHT/2)) {
 			print { $self->{out} } $oid, "\n" or
-						fail($self, "write error: $!");
+						$self->fail("write error: $!");
 			return push(@$inflight, $oid, $cb, $arg);
 		}
 	}

      parent reply	other threads:[~2020-11-29 10:52 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-29 10:52 [PATCH 0/2] git: some safety fixes Eric Wong
2020-11-29 10:52 ` [PATCH 1/2] git: set non-blocking flag in case of other bugs Eric Wong
2020-11-29 10:52 ` Eric Wong [this message]

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=20201129105230.12501-3-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).