diff options
Diffstat (limited to 'lib/PublicInbox/LeiSavedSearch.pm')
-rw-r--r-- | lib/PublicInbox/LeiSavedSearch.pm | 46 |
1 files changed, 24 insertions, 22 deletions
diff --git a/lib/PublicInbox/LeiSavedSearch.pm b/lib/PublicInbox/LeiSavedSearch.pm index 3e10f780..9ae9dcdb 100644 --- a/lib/PublicInbox/LeiSavedSearch.pm +++ b/lib/PublicInbox/LeiSavedSearch.pm @@ -1,10 +1,9 @@ -# Copyright (C) 2021 all contributors <meta@public-inbox.org> +# Copyright (C) all contributors <meta@public-inbox.org> # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt> # pretends to be like LeiDedupe and also PublicInbox::Inbox package PublicInbox::LeiSavedSearch; -use strict; -use v5.10.1; +use v5.12; use parent qw(PublicInbox::Lock); use PublicInbox::Git; use PublicInbox::OverIdx; @@ -13,7 +12,9 @@ use PublicInbox::Config; use PublicInbox::Spawn qw(run_die); use PublicInbox::ContentHash qw(git_sha); use PublicInbox::MID qw(mids_for_index); -use Digest::SHA qw(sha256_hex); +use PublicInbox::SHA qw(sha256_hex); +use File::Temp (); +use IO::Handle (); our $LOCAL_PFX = qr!\A(?:maildir|mh|mbox.+|mmdf|v2):!i; # TODO: put in LeiToMail? # move this to PublicInbox::Config if other things use it: @@ -21,6 +22,7 @@ my %cquote = ("\n" => '\\n', "\t" => '\\t', "\b" => '\\b'); sub cquote_val ($) { # cf. git-config(1) my ($val) = @_; $val =~ s/([\n\t\b])/$cquote{$1}/g; + $val =~ s/\"/\\\"/g; $val; } @@ -75,20 +77,17 @@ sub list { my $lss_dir = $lei->share_path.'/saved-searches'; return () unless -d $lss_dir; # TODO: persist the cache? Use another format? - my $f = $lei->cache_dir."/saved-tmp.$$.".time.'.config'; - open my $fh, '>', $f or die "open $f: $!"; + my $fh = File::Temp->new(TEMPLATE => 'lss_list-XXXX', TMPDIR => 1) or + die "File::Temp->new: $!"; print $fh "[include]\n"; for my $p (glob("$lss_dir/*/lei.saved-search")) { print $fh "\tpath = ", cquote_val($p), "\n"; } - close $fh or die "close $f: $!"; - my $cfg = $lei->cfg_dump($f); - unlink($f); + $fh->flush or die "flush: $fh"; + my $cfg = $lei->cfg_dump($fh->filename); my $out = $cfg ? $cfg->get_all('lei.q.output') : []; - map {; - s!$LOCAL_PFX!!; - $_; - } @$out + s!$LOCAL_PFX!! for @$out;; + @$out; } sub translate_dedupe ($$) { @@ -96,7 +95,7 @@ sub translate_dedupe ($$) { my $dd = $lei->{opt}->{dedupe} // 'content'; return 1 if $dd eq 'content'; # the default return $self->{"-dedupe_$dd"} = 1 if ($dd eq 'oid' || $dd eq 'mid'); - $lei->fail("--dedupe=$dd requires --no-save"); + die("--dedupe=$dd requires --no-save\n"); } sub up { # updating existing saved search via "lei up" @@ -105,9 +104,9 @@ sub up { # updating existing saved search via "lei up" my $self = bless { ale => $lei->ale }, $cls; my $dir = $dst; output2lssdir($self, $lei, \$dir, \$f) or - return $lei->fail("--no-save was used with $dst cwd=". - $lei->rel2abs('.')); - $self->{-cfg} = $lei->cfg_dump($f) // return $lei->fail; + return die("--no-save was used with $dst cwd=". + $lei->rel2abs('.')."\n"); + $self->{-cfg} = $lei->cfg_dump($f) // return $lei->child_error; $self->{-ovf} = "$dir/over.sqlite3"; $self->{'-f'} = $f; $self->{lock_path} = "$self->{-f}.flock"; @@ -162,6 +161,10 @@ EOM my $val = $lei->{opt}->{$k} // next; print $fh "\t$k = $val\n"; } + $lei->{opt}->{stdin} and print $fh <<EOM; +[lei "internal"] + rawstr = 1 # stdin was used initially +EOM close($fh) or return $lei->fail("close $f: $!"); $self->{lock_path} = "$self->{-f}.flock"; $self->{-ovf} = "$dir/over.sqlite3"; @@ -242,10 +245,10 @@ sub git { $_[0]->{git} //= PublicInbox::Git->new($_[0]->{ale}->git->{git_dir}) } sub pause_dedupe { my ($self) = @_; - git($self)->cleanup; - my $lockfh = delete $self->{lockfh}; # from lock_for_scope_fast; - my $oidx = delete($self->{oidx}) // return; - $oidx->commit_lazy; + my ($git, $oidx) = delete @$self{qw(git oidx)}; + $git->cleanup if $git; + $oidx->commit_lazy if $oidx; + delete $self->{lockfh}; # from lock_for_scope_fast; } sub reset_dedupe { @@ -294,7 +297,6 @@ no warnings 'once'; *smsg_by_mid = \&PublicInbox::Inbox::smsg_by_mid; *msg_by_mid = \&PublicInbox::Inbox::msg_by_mid; *modified = \&PublicInbox::Inbox::modified; -*recent = \&PublicInbox::Inbox::recent; *max_git_epoch = *nntp_usable = *msg_by_path = \&mm; # undef *isrch = *search = \&mm; # TODO *DESTROY = \&pause_dedupe; |