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] mbox_reader: do not blindly pass --rsyncable to gzip
Date: Mon,  1 Nov 2021 23:46:20 +0000	[thread overview]
Message-ID: <20211101234620.2247-1-e@80x24.org> (raw)

FreeBSD gzip does not support --rsyncable, though my VM
usually has pigz installed.
---
 lib/PublicInbox/MboxReader.pm | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/lib/PublicInbox/MboxReader.pm b/lib/PublicInbox/MboxReader.pm
index 5a754cb8..beffabe8 100644
--- a/lib/PublicInbox/MboxReader.pm
+++ b/lib/PublicInbox/MboxReader.pm
@@ -144,7 +144,7 @@ sub reads {
 # { foo => '' } means "--foo" is passed to the command-line,
 # otherwise { foo => '--bar' } passes "--bar"
 my %zsfx2cmd = (
-	gz => [ qw(GZIP pigz gzip), { rsyncable => '' } ],
+	gz => [ qw(GZIP pigz gzip) ],
 	bz2 => [ 'bzip2', {} ],
 	xz => [ 'xz', {} ],
 	# don't add new entries here unless MUA support is widely available
@@ -160,7 +160,7 @@ sub zsfx2cmd ($$$) {
 	my ($zsfx, $decompress, $lei) = @_;
 	my $x = $zsfx2cmd{$zsfx} // die "BUG: no support for suffix=.$zsfx";
 	my @info = @$x;
-	my $cmd_opt = pop @info;
+	my $cmd_opt = ref($info[-1]) ? pop(@info) : undef;
 	my @cmd = (undef, $decompress ? qw(-dc) : qw(-c));
 	require PublicInbox::Spawn;
 	for my $exe (@info) {
@@ -172,9 +172,23 @@ sub zsfx2cmd ($$$) {
 		$cmd[0] = PublicInbox::Spawn::which($exe) and last;
 	}
 	$cmd[0] // die join(' or ', @info)." missing for .$zsfx";
-	# push @cmd, @{$cmd_opt->{-default}} if $cmd_opt->{-default};
-	for my $bool (qw(rsyncable)) {
-		my $switch = $cmd_opt->{rsyncable} // next;
+
+	# not all gzip support --rsyncable, FreeBSD gzip doesn't even exit
+	# with an error code
+	if (!$decompress && $cmd[0] =~ m!/gzip\z! && !defined($cmd_opt)) {
+		pipe(my ($r, $w)) or die "pipe: $!";
+		open my $null, '+>', '/dev/null' or die "open: $!";
+		my $rdr = { 0 => $null, 1 => $null, 2 => $w };
+		my $tst = [ $cmd[0], '--rsyncable' ];
+		my $pid = PublicInbox::Spawn::spawn($tst, undef, $rdr);
+		close $w;
+		my $err = do { local $/; <$r> };
+		waitpid($pid, 0) == $pid or die "BUG: waitpid: $!";
+		$cmd_opt = $err ? {} : { rsyncable => '' };
+		push(@$x, $cmd_opt);
+	}
+	for my $bool (keys %$cmd_opt) {
+		my $switch = $cmd_opt->{$bool} // next;
 		push @cmd, '--'.($switch || $bool);
 	}
 	for my $key (qw(rsyncable)) { # support compression level?

                 reply	other threads:[~2021-11-01 23:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20211101234620.2247-1-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).