X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FLeiSavedSearch.pm;h=1d13aef6728a6a5ebcbcd942a93630f61872e1ed;hb=666dde69a3f64456321b7c701070712d6b85e359;hp=01b987d17d381896cc8a373126bf38e0e284bcf3;hpb=473b069c61b3d4936e2c5ed46fd7d348c9f6d1b7;p=public-inbox.git diff --git a/lib/PublicInbox/LeiSavedSearch.pm b/lib/PublicInbox/LeiSavedSearch.pm index 01b987d1..1d13aef6 100644 --- a/lib/PublicInbox/LeiSavedSearch.pm +++ b/lib/PublicInbox/LeiSavedSearch.pm @@ -14,13 +14,14 @@ 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); -my $LOCAL_PFX = qr!\A(?:maildir|mh|mbox.+|mmdf):!i; # TODO: put in LeiToMail? +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: 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; } @@ -29,23 +30,25 @@ sub BOOL_FIELDS () { qw(external local remote import-remote import-before threads) } +sub SINGLE_FIELDS () { qw(limit dedupe output) } + sub lss_dir_for ($$;$) { my ($lei, $dstref, $on_fs) = @_; - my @n; + my $pfx; if ($$dstref =~ m,\Aimaps?://,i) { # already canonicalized require PublicInbox::URIimap; my $uri = PublicInbox::URIimap->new($$dstref)->canonical; $$dstref = $$uri; - @n = ($uri->mailbox); + $pfx = $uri->mailbox; } else { # can't use Cwd::abs_path since dirname($$dstref) may not exist $$dstref = $lei->rel2abs($$dstref); $$dstref =~ tr!/!/!s; - @n = ($$dstref =~ m{([^/]+)/*\z}); # basename + $pfx = $$dstref; } - push @n, sha256_hex($$dstref); + ($pfx) = ($pfx =~ m{([^/]+)/*\z}); # basename my $lss_dir = $lei->share_path . '/saved-searches/'; - my $d = $lss_dir . join('-', @n); + my $d = "$lss_dir$pfx-".sha256_hex($$dstref); # fall-back to looking up by st_ino + st_dev in case we're in # a symlinked or bind-mounted path @@ -53,10 +56,10 @@ sub lss_dir_for ($$;$) { my @cur = stat(_); my $want = pack('dd', @cur[1,0]); # st_ino + st_dev my ($c, $o, @st); - for my $g ("$n[0]-*", '*') { + for my $g ("$pfx-*", '*') { my @maybe = glob("$lss_dir$g/lei.saved-search"); for my $f (@maybe) { - $c = PublicInbox::Config->git_config_dump($f); + $c = $lei->cfg_dump($f) // next; $o = $c->{'lei.q.output'} // next; $o =~ s!$LOCAL_PFX!! or next; @st = stat($o) or next; @@ -80,21 +83,21 @@ sub list { print $fh "\tpath = ", cquote_val($p), "\n"; } close $fh or die "close $f: $!"; - my $cfg = PublicInbox::Config->git_config_dump($f); + my $cfg = $lei->cfg_dump($f); unlink($f); - my $out = $cfg->get_all('lei.q.output') or return (); + my $out = $cfg ? $cfg->get_all('lei.q.output') : []; map {; s!$LOCAL_PFX!!; $_; } @$out } -sub translate_dedupe ($$$) { - my ($self, $lei, $dd) = @_; - $dd //= 'content'; +sub translate_dedupe ($$) { + my ($self, $lei) = @_; + 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 unsupported with --save"); + die("--dedupe=$dd requires --no-save\n"); } sub up { # updating existing saved search via "lei up" @@ -103,9 +106,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("--save was not used with $dst cwd=". - $lei->rel2abs('.')); - $self->{-cfg} = PublicInbox::Config->git_config_dump($f); + 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"; @@ -115,14 +118,20 @@ sub up { # updating existing saved search via "lei up" sub new { # new saved search "lei q --save" my ($cls, $lei) = @_; my $self = bless { ale => $lei->ale }, $cls; + require File::Path; my $dst = $lei->{ovv}->{dst}; + + # canonicalize away relative paths into the config + if ($lei->{ovv}->{fmt} eq 'maildir' && + $dst =~ m!(?:/*|\A)\.\.(?:/*|\z)! && !-d $dst) { + File::Path::make_path($dst); + $lei->{ovv}->{dst} = $dst = $lei->abs_path($dst); + } my $dir = lss_dir_for($lei, \$dst); - require File::Path; File::Path::make_path($dir); # raises on error $self->{-cfg} = {}; my $f = $self->{'-f'} = "$dir/lei.saved-search"; - my $dd = $lei->{opt}->{dedupe}; - translate_dedupe($self, $lei, $dd) or return; + translate_dedupe($self, $lei) or return; open my $fh, '>', $f or return $lei->fail("open $f: $!"); my $sq_dst = PublicInbox::Config::squote_maybe($dst); my $q = $lei->{mset_opt}->{q_raw} // die 'BUG: {q_raw} missing'; @@ -132,14 +141,14 @@ sub new { # new saved search "lei q --save" $q = "\tq = ".cquote_val($q); } $dst = "$lei->{ovv}->{fmt}:$dst" if $dst !~ m!\Aimaps?://!i; + $lei->{opt}->{output} = $dst; print $fh <{opt}->{$k} // next; for my $x (@$ary) { @@ -150,6 +159,14 @@ EOM my $val = $lei->{opt}->{$k} // next; print $fh "\t$k = ".($val ? 1 : 0)."\n"; } + for my $k (SINGLE_FIELDS) { + my $val = $lei->{opt}->{$k} // next; + print $fh "\t$k = $val\n"; + } + $lei->{opt}->{stdin} and print $fh <fail("close $f: $!"); $self->{lock_path} = "$self->{-f}.flock"; $self->{-ovf} = "$dir/over.sqlite3"; @@ -236,6 +253,16 @@ sub pause_dedupe { $oidx->commit_lazy; } +sub reset_dedupe { + my ($self) = @_; + prepare_dedupe($self); + my $lk = $self->lock_for_scope_fast; + for my $t (qw(xref3 over id2num)) { + $self->{oidx}->{dbh}->do("DELETE FROM $t"); + } + pause_dedupe($self); +} + sub mm { undef } sub altid_map { {} } @@ -250,7 +277,7 @@ sub output2lssdir { my $dir = lss_dir_for($lei, \$dst, 1); my $f = "$dir/lei.saved-search"; if (-f $f && -r _) { - $self->{-cfg} = PublicInbox::Config->git_config_dump($f); + $self->{-cfg} = $lei->cfg_dump($f) // return; $$dir_ref = $dir; $$fn_ref = $f; return 1; @@ -258,55 +285,11 @@ sub output2lssdir { undef; } -sub edit_begin { - my ($self, $lei) = @_; - if (ref($self->{-cfg}->{'lei.q.output'})) { - delete $self->{-cfg}->{'lei.q.output'}; # invalid - $lei->err(<{-f} has multiple values of lei.q.output -please remove redundant ones -EOM - } - $lei->{-lss_for_edit} = $self; -} - -sub edit_done { - my ($self, $lei) = @_; - my $cfg = PublicInbox::Config->git_config_dump($self->{'-f'}); - my $new_out = $cfg->{'lei.q.output'} // ''; - return $lei->fail(<{-f} has multiple values of lei.q.output -please edit again -EOM - return $lei->fail(<{-f} needs lei.q.output -please edit again -EOM - my $old_out = $self->{-cfg}->{'lei.q.output'} // ''; - return if $old_out eq $new_out; - my $old_path = $old_out; - my $new_path = $new_out; - s!$LOCAL_PFX!! for ($old_path, $new_path); - my $dir_old = lss_dir_for($lei, \$old_path, 1); - my $dir_new = lss_dir_for($lei, \$new_path); - return if $dir_new eq $dir_old; # no change, likely - return $lei->fail(<puts("lei.q.output changed from $old_sq to $new_sq"); - $lei->qerr("# lei convert $old_sq -o $new_sq"); - my $v = !$lei->{opt}->{quiet}; - $lei->{opt} = { output => $new_out, verbose => $v }; - require PublicInbox::LeiConvert; - PublicInbox::LeiConvert::lei_convert($lei, $old_out); - - $lei->fail(<has_entries +sub has_entries { + my $oidx = $_[0]->{oidx} // die 'BUG: no {oidx}'; + my @n = $oidx->{dbh}->selectrow_array('SELECT num FROM over LIMIT 1'); + scalar(@n) ? 1 : undef; } no warnings 'once';