From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 375641F9F3 for ; Mon, 19 Apr 2021 08:52:16 +0000 (UTC) From: Eric Wong 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 Message-Id: <20210419085215.14742-3-e@80x24.org> In-Reply-To: <20210419085215.14742-1-e@80x24.org> References: <20210419085215.14742-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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: +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;