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 2/6] lei q: implement import-before default for --save
Date: Mon, 19 Apr 2021 08:52:11 +0000	[thread overview]
Message-ID: <20210419085215.14742-3-e@80x24.org> (raw)
In-Reply-To: <20210419085215.14742-1-e@80x24.org>

This makes "lei q --save" as safe as "lei q" to prevent against
accidental data loss when clobbering an existing output,
---
 lib/PublicInbox/LeiToMail.pm |  6 +++---
 lib/PublicInbox/LeiUp.pm     |  2 +-
 t/lei-q-save.t               | 21 +++++++++++++++++++++
 3 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/lib/PublicInbox/LeiToMail.pm b/lib/PublicInbox/LeiToMail.pm
index daa8084b..46a82a4b 100644
--- a/lib/PublicInbox/LeiToMail.pm
+++ b/lib/PublicInbox/LeiToMail.pm
@@ -375,7 +375,7 @@ sub _pre_augment_maildir {
 
 sub _do_augment_maildir {
 	my ($self, $lei) = @_;
-	return if defined($lei->{opt}->{save});
+	return if ($lei->{opt}->{save} // 0) < 0;
 	my $dst = $lei->{ovv}->{dst};
 	my $lse = $lei->{opt}->{'import-before'} ? $lei->{lse} : undef;
 	my $mdr = PublicInbox::MdirReader->new;
@@ -406,7 +406,7 @@ sub _imap_augment_or_delete { # PublicInbox::NetReader::imap_each cb
 
 sub _do_augment_imap {
 	my ($self, $lei) = @_;
-	return if defined($lei->{opt}->{save});
+	return if ($lei->{opt}->{save} // 0) < 0;
 	my $net = $lei->{net};
 	my $lse = $lei->{opt}->{'import-before'} ? $lei->{lse} : undef;
 	if ($lei->{opt}->{augment}) {
@@ -477,7 +477,7 @@ sub _do_augment_mbox {
 	my ($self, $lei) = @_;
 	return unless $self->{seekable};
 	my $opt = $lei->{opt};
-	return if defined($opt->{save});
+	return if ($opt->{save} // 0) < 0;
 	my $out = $lei->{1};
 	my ($fmt, $dst) = @{$lei->{ovv}}{qw(fmt dst)};
 	return unless -s $out;
diff --git a/lib/PublicInbox/LeiUp.pm b/lib/PublicInbox/LeiUp.pm
index 73286ea2..e4cbc825 100644
--- a/lib/PublicInbox/LeiUp.pm
+++ b/lib/PublicInbox/LeiUp.pm
@@ -38,7 +38,7 @@ sub lei_up {
 	$lei->{lss} = $lss; # for LeiOverview->new
 	my $lxs = $lei->lxs_prepare or return;
 	$lei->ale->refresh_externals($lxs);
-	$lei->{opt}->{save} = 1;
+	$lei->{opt}->{save} = -1;
 	$lei->_start_query;
 }
 
diff --git a/t/lei-q-save.t b/t/lei-q-save.t
index 4e6ed642..5bc8fb74 100644
--- a/t/lei-q-save.t
+++ b/t/lei-q-save.t
@@ -10,6 +10,15 @@ $doc2->header_set('Date', PublicInbox::Smsg::date({ds => time - (86400 * 4)}));
 my $doc3 = eml_load('t/msg_iter-order.eml');
 $doc3->header_set('Date', PublicInbox::Smsg::date({ds => time - (86400 * 4)}));
 
+my $pre_existing = <<'EOF';
+From x Mon Sep 17 00:00:00 2001
+Message-ID: <import-before@example.com>
+Subject: pre-existing
+Date: Sat, 02 Oct 2010 00:00:00 +0000
+
+blah
+EOF
+
 test_lei(sub {
 	my $home = $ENV{HOME};
 	my $in = $doc1->as_string;
@@ -74,5 +83,17 @@ test_lei(sub {
 		'mbcl2 output shown despite unlink');
 	lei_ok([qw(up mbcl2)], undef, { -C => $home, %$lei_opt });
 	ok(-f "$home/mbcl2"  && -s _ == 0, 'up recreates on missing output');
+
+	open my $mb, '>', "$home/mbrd" or xbail "open $!";
+	print $mb $pre_existing;
+	close $mb or xbail "close: $!";
+	lei_ok(qw(q --save -o mboxrd:mbrd m:qp@example.com -C), $home);
+	chdir($dh) or xbail "fchdir . $!";
+	open $mb, '<', "$home/mbrd" or xbail "open $!";
+	is_deeply([grep(/pre-existing/, <$mb>)], [],
+		'pre-existing messsage gone w/o augment');
+	lei_ok(qw(q m:import-before@example.com));
+	is(json_utf8->decode($lei_out)->[0]->{'s'},
+		'pre-existing', '--save imported before clobbering');
 });
 done_testing;

  parent reply	other threads:[~2021-04-19  8:52 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-19  8:52 [PATCH 0/6] lei saved search improvements Eric Wong
2021-04-19  8:52 ` [PATCH 1/6] lei: support unlinked/missing saved searches Eric Wong
2021-04-19  8:52 ` Eric Wong [this message]
2021-04-19  8:52 ` [PATCH 3/6] lei_saved_search: avoid needless var shadowing Eric Wong
2021-04-19  8:52 ` [PATCH 4/6] config: git_config_dump blesses Eric Wong
2021-04-19  8:52 ` [PATCH 5/6] lei_saved_search: split "lei q --save" and "lei up" init paths Eric Wong
2021-04-19  8:52 ` [PATCH 6/6] lei q: --save and --augment may be combined 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=20210419085215.14742-3-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    --subject='Re: [PATCH 2/6] lei q: implement import-before default for --save' \
    /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

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