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: <[email protected]>
+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:[email protected] -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:[email protected]));
+       is(json_utf8->decode($lei_out)->[0]->{'s'},
+               'pre-existing', '--save imported before clobbering');
 });
 done_testing;
--
unsubscribe: one-click, see List-Unsubscribe header
archive: https://public-inbox.org/meta/

Reply via email to