user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
* [PATCH] lei q: fix --import-before default and FIFO output
@ 2021-03-05  1:38 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2021-03-05  1:38 UTC (permalink / raw)
  To: meta

commit 6c551bffd75afb41d9b5e4774068abe7e06ed0e7
("lei q: --import-augment for mbox and mbox.gz") added a check to
in _pre_augment_mbox for the option being a ref() to distinguish
between default values and user-supplied values (which are
non-ref SCALARs from Getopt::Long).

However, LeiQuery failed to use a SCALAR ref as the default
value, making the check in _pre_augment_mbox useless.  We
now update LeiQuery to use \1 instead of 1 as the default
value so "lei q -f mboxrd ..." to stdout works once again.

Unfortunately, testing with redirects pointed to regular
files didn't trigger the code paths being updated.  Testing
with a FIFO revealed further bugs in the FIFO handling code
which are also fixed in this commit.

We'll also update the $lei->out error message to be
less-specific about "stdout" and use the term "output", instead,
since LeiToMail replaces stdout for all mbox outputs.
---
 lib/PublicInbox/LEI.pm       |  2 +-
 lib/PublicInbox/LeiQuery.pm  |  2 +-
 lib/PublicInbox/LeiToMail.pm |  8 ++++++--
 t/lei-q-kw.t                 | 25 +++++++++++++++++++------
 4 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index 50276a50..50c0a885 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -431,7 +431,7 @@ sub out ($;@) {
 	my $self = shift;
 	return if print { $self->{1} // return } @_; # likely
 	return note_sigpipe($self, 1) if $! == EPIPE;
-	my $err = "error writing to stdout: $!";
+	my $err = "error writing to output: $!";
 	delete $self->{1};
 	fail($self, $err);
 }
diff --git a/lib/PublicInbox/LeiQuery.pm b/lib/PublicInbox/LeiQuery.pm
index 493a8382..623b92cd 100644
--- a/lib/PublicInbox/LeiQuery.pm
+++ b/lib/PublicInbox/LeiQuery.pm
@@ -52,7 +52,7 @@ sub lei_q {
 	my $sto = $self->_lei_store(1);
 	my $lse = $sto->search;
 	if (($opt->{'import-remote'} //= 1) |
-			($opt->{'import-before'} //= 1)) {
+			(($opt->{'import-before'} //= \1) ? 1 : 0)) {
 		$sto->write_prepare($self);
 	}
 	if ($opt->{'local'} //= scalar(@only) ? 0 : 1) {
diff --git a/lib/PublicInbox/LeiToMail.pm b/lib/PublicInbox/LeiToMail.pm
index 1e2060fe..13b4f672 100644
--- a/lib/PublicInbox/LeiToMail.pm
+++ b/lib/PublicInbox/LeiToMail.pm
@@ -223,8 +223,6 @@ sub _post_augment_mbox { # open a compressor process
 	$dup->{$_} = $lei->{$_} for qw(2 sock);
 	tie *$pp, 'PublicInbox::ProcessPipe', $pid, $w, \&reap_compress, $dup;
 	$lei->{1} = $pp;
-	die 'BUG: unexpected {ovv}->{lock_path}' if $lei->{ovv}->{lock_path};
-	$lei->{ovv}->ovv_out_lk_init;
 }
 
 sub decompress_src ($$$) {
@@ -495,6 +493,7 @@ sub _pre_augment_mbox {
 	my $out = $lei->{1};
 	if ($dst ne '/dev/stdout') {
 		if (-p $dst) {
+			$out = undef;
 			open $out, '>', $dst or die "open($dst): $!";
 		} elsif (-f _ || !-e _) {
 			require PublicInbox::MboxLock;
@@ -521,6 +520,11 @@ sub _pre_augment_mbox {
 	if (($self->{zsfx}) = ($dst =~ /\.($zsfx_allow)\z/)) {
 		pipe(my ($r, $w)) or die "pipe: $!";
 		$lei->{zpipe} = [ $r, $w ];
+		$lei->{ovv}->{lock_path} and
+			die 'BUG: unexpected {ovv}->{lock_path}';
+		$lei->{ovv}->ovv_out_lk_init;
+	} elsif (!$self->{seekable} && !$lei->{ovv}->{lock_path}) {
+		$lei->{ovv}->ovv_out_lk_init;
 	}
 	$lei->{1} = $out;
 	undef;
diff --git a/t/lei-q-kw.t b/t/lei-q-kw.t
index 9daeb5b1..917a2c53 100644
--- a/t/lei-q-kw.t
+++ b/t/lei-q-kw.t
@@ -7,7 +7,15 @@ use Fcntl qw(SEEK_SET O_RDONLY O_NONBLOCK);
 use IO::Uncompress::Gunzip qw(gunzip);
 use IO::Compress::Gzip qw(gzip);
 use PublicInbox::MboxReader;
+use PublicInbox::LeiToMail;
 use PublicInbox::Spawn qw(popen_rd);
+my $exp = {
+	'<qp@example.com>' => eml_load('t/plack-qp.eml'),
+	'<testmessage@example.com>' => eml_load('t/utf8.eml'),
+};
+$exp->{'<qp@example.com>'}->header_set('Status', 'OR');
+$exp->{'<testmessage@example.com>'}->header_set('Status', 'O');
+
 test_lei(sub {
 lei_ok(qw(import -F eml t/plack-qp.eml));
 my $o = "$ENV{HOME}/dst";
@@ -42,6 +50,17 @@ SKIP: {
 	ok(!lei(qw(q --import-before bogus -o), "mboxrd:$o"),
 		'--import-before fails on non-seekable output');
 	is(do { local $/; <$cat> }, '', 'no output on FIFO');
+	close $cat;
+	$cat = popen_rd(['cat', $o]);
+	lei_ok(qw(q m:qp@example.com -o), "mboxrd:$o");
+	my $buf = do { local $/; <$cat> };
+	open my $fh, '<', \$buf or BAIL_OUT $!;
+	PublicInbox::MboxReader->mboxrd($fh, sub {
+		my ($eml) = @_;
+		$eml->header_set('Status', 'OR');
+		is_deeply($eml, $exp->{'<qp@example.com>'},
+			'FIFO output works as expected');
+	});
 };
 
 lei_ok qw(import -F eml t/utf8.eml), \'for augment test';
@@ -66,12 +85,6 @@ my $write_file = sub {
 	}
 };
 
-my $exp = {
-	'<qp@example.com>' => eml_load('t/plack-qp.eml'),
-	'<testmessage@example.com>' => eml_load('t/utf8.eml'),
-};
-$exp->{'<qp@example.com>'}->header_set('Status', 'OR');
-$exp->{'<testmessage@example.com>'}->header_set('Status', 'O');
 for my $sfx ('', '.gz') {
 	$o = "$ENV{HOME}/dst.mboxrd$sfx";
 	lei_ok(qw(q -o), "mboxrd:$o", qw(m:qp@example.com));

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-03-05  1:38 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-05  1:38 [PATCH] lei q: fix --import-before default and FIFO output Eric Wong

user/dev discussion of public-inbox itself

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://public-inbox.org/meta
	git clone --mirror http://czquwvybam4bgbro.onion/meta
	git clone --mirror http://hjrcffqmbrq6wope.onion/meta
	git clone --mirror http://ou63pmih66umazou.onion/meta

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 meta meta/ https://public-inbox.org/meta \
		meta@public-inbox.org
	public-inbox-index meta

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.mail.public-inbox.meta
	nntp://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/inbox.comp.mail.public-inbox.meta
	nntp://ie5yzdi7fg72h7s4sdcztq5evakq23rdt33mfyfcddc5u3ndnw24ogqd.onion/inbox.comp.mail.public-inbox.meta
	nntp://4uok3hntl7oi7b4uf4rtfwefqeexfzil2w6kgk2jn5z2f764irre7byd.onion/inbox.comp.mail.public-inbox.meta
	nntp://news.gmane.io/gmane.mail.public-inbox.general
 note: .onion URLs require Tor: https://www.torproject.org/

code repositories for project(s) associated with this inbox:

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

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git