From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id A59AC1F513 for ; Tue, 17 Oct 2023 23:38:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1697585896; bh=+dqdYVJsuR5PvVwJnjmVbxR6uVBzOm7F/hNXP0AjMIQ=; h=From:To:Subject:Date:In-Reply-To:References:From; b=gJxHYgGkPhBpHSTO0I45q9tQbUpq9r4UFNegKLpIw3Hg7WFOZgfEwxKnikiw106Ro IWG4fZW9jxD8CFN+oucJBpNUUVCLgvwLd8ssSrw7K+/L92mrh+v/jOePSqKMuwJ6jA 90s7Ng/7qkvGGXLT30IGQMTaQednzyzZOnNjKdcs= From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 04/30] git: introduce read_all function Date: Tue, 17 Oct 2023 23:37:49 +0000 Message-ID: <20231017233815.1637932-5-e@80x24.org> In-Reply-To: <20231017233815.1637932-1-e@80x24.org> References: <20231017233815.1637932-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: This makes it easier to improve error checking, since the `do { local $/; readline(FH) }' construct does not detect errors (autodie does not cover `readline' or `'). I'm not sure exactly where this should be, but PublicInbox::Git is used nearly everywhere in our code base and it's probably not worth creating a new package for it. --- lib/PublicInbox/Git.pm | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/PublicInbox/Git.pm b/lib/PublicInbox/Git.pm index 448cfaf7..35bd10ef 100644 --- a/lib/PublicInbox/Git.pm +++ b/lib/PublicInbox/Git.pm @@ -10,7 +10,7 @@ package PublicInbox::Git; use strict; use v5.10.1; use parent qw(Exporter PublicInbox::DS); -use autodie qw(socketpair); +use autodie qw(socketpair read); use POSIX (); use Socket qw(AF_UNIX SOCK_STREAM); use PublicInbox::Syscall qw(EPOLLIN EPOLLET); @@ -26,7 +26,7 @@ use Carp qw(croak carp); use PublicInbox::SHA (); our %HEXLEN2SHA = (40 => 1, 64 => 256); our %OFMT2HEXLEN = (sha1 => 40, sha256 => 64); -our @EXPORT_OK = qw(git_unquote git_quote %HEXLEN2SHA %OFMT2HEXLEN); +our @EXPORT_OK = qw(git_unquote git_quote %HEXLEN2SHA %OFMT2HEXLEN read_all); our $in_cleanup; our $RDTIMEO = 60_000; # milliseconds our $async_warn; # true in read-only daemons @@ -548,11 +548,19 @@ sub modified ($;$) { (split(/ /, <$fh> // time))[0] + 0; # integerize for JSON } +# read_all/try_cat can probably be moved somewhere else... + +sub read_all ($;$) { + my ($fh, $len) = @_; + my $r = read($fh, my $buf, $len //= -s $fh); + croak("$fh read ($r != $len)") if $len != $r; + $buf; +} + sub try_cat { my ($path) = @_; open(my $fh, '<', $path) or return ''; - local $/; - <$fh> // ''; + read_all($fh); } sub cat_desc ($) { @@ -606,7 +614,7 @@ sub manifest_entry { } } my $dig = PublicInbox::SHA->new(1); - while (read($sr, $buf, 65536)) { $dig->add($buf) } + while (CORE::read($sr, $buf, 65536)) { $dig->add($buf) } CORE::close $sr or return; # empty, uninitialized git repo $ent->{fingerprint} = $dig->hexdigest; $ent->{modified} = modified(undef, $mod);