X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FLeiSavedSearch.pm;h=92ced28bd9a48114456d5b1c88e872ca132c2b6a;hb=d98abf13946beaf685804e4ec2d79019129415b9;hp=ebc63091e075075296b3e738e49dfcf001af5d23;hpb=17244996655fcbc47fb54c915360657367366c46;p=public-inbox.git diff --git a/lib/PublicInbox/LeiSavedSearch.pm b/lib/PublicInbox/LeiSavedSearch.pm index ebc63091..92ced28b 100644 --- a/lib/PublicInbox/LeiSavedSearch.pm +++ b/lib/PublicInbox/LeiSavedSearch.pm @@ -6,52 +6,153 @@ package PublicInbox::LeiSavedSearch; use strict; use v5.10.1; use parent qw(PublicInbox::Lock); +use PublicInbox::Git; use PublicInbox::OverIdx; use PublicInbox::LeiSearch; 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); +my $LOCAL_PFX = qr!\A(?:maildir|mh|mbox.+|mmdf):!i; # TODO: put in LeiToMail? -sub new { - my ($cls, $lei, $dir) = @_; - my $self = bless { ale => $lei->ale, -cfg => {} }, $cls; - if (defined $dir) { # updating existing saved search via "lei up" - my $f = $self->{'-f'} = "$dir/lei.saved-search"; - -f $f && -r _ or - return $lei->fail("$f non-existent or unreadable"); - $self->{-cfg} = PublicInbox::Config::git_config_dump($f); - } else { # new saved search "lei q --save" - my $saved_dir = $lei->share_path . '/saved-searches/'; - my (@n) = ($lei->{ovv}->{dst} =~ m{([\w\-\.]+)/*\z}); - my $q = $lei->{mset_opt}->{q_raw} // die 'BUG: {q_raw} missing'; - push @n, sha256_hex("$lei->{ovv}->{fmt}\0$lei->{ovv}->{dst}"); - - $dir = $saved_dir . join('-', @n); - require File::Path; - File::Path::make_path($dir); # raises on error - $self->{'-f'} = "$dir/lei.saved-search"; - if (ref $q) { - cfg_set($self, '--add', 'lei.q', $_) for @$q; - } else { - cfg_set($self, 'lei.q', $q); - } - my $fmt = $lei->{opt}->{'format'}; - cfg_set($self, 'lei.q.format', $fmt) if defined $fmt; - cfg_set($self, 'lei.q.output', $lei->{opt}->{output}); - for my $k (qw(only include exclude)) { - my $ary = $lei->{opt}->{$k} // next; - for my $x (@$ary) { - cfg_set($self, '--add', "lei.q.$k", $x); +# 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; +} + +sub ARRAY_FIELDS () { qw(only include exclude) } +sub BOOL_FIELDS () { + qw(external local remote import-remote import-before threads) +} + +sub lss_dir_for ($$;$) { + my ($lei, $dstref, $on_fs) = @_; + my @n; + if ($$dstref =~ m,\Aimaps?://,i) { # already canonicalized + require PublicInbox::URIimap; + my $uri = PublicInbox::URIimap->new($$dstref)->canonical; + $$dstref = $$uri; + @n = ($uri->mailbox); + } else { + # can't use Cwd::abs_path since dirname($$dstref) may not exist + $$dstref = $lei->rel2abs($$dstref); + # Maildirs have trailing '/' internally + $$dstref .= '/' if -d $$dstref; + $$dstref =~ tr!/!/!s; + @n = ($$dstref =~ m{([^/]+)/*\z}); # basename + } + push @n, sha256_hex($$dstref); + my $lss_dir = $lei->share_path . '/saved-searches/'; + my $d = $lss_dir . join('-', @n); + + # fall-back to looking up by st_ino + st_dev in case we're in + # a symlinked or bind-mounted path + if ($on_fs && !-d $d && -e $$dstref) { + my @cur = stat(_); + my $want = pack('dd', @cur[1,0]); # st_ino + st_dev + my ($c, $o, @st); + for my $g ("$n[0]-*", '*') { + my @maybe = glob("$lss_dir$g/lei.saved-search"); + for my $f (@maybe) { + $c = PublicInbox::Config->git_config_dump($f); + $o = $c->{'lei.q.output'} // next; + $o =~ s!$LOCAL_PFX!! or next; + @st = stat($o) or next; + next if pack('dd', @st[1,0]) ne $want; + $f =~ m!\A(.+?)/[^/]+\z! and return $1; } } - for my $k (qw(external local remote import-remote - import-before threads)) { - my $val = $lei->{opt}->{$k} // next; - cfg_set($self, "lei.q.$k", $val); + } + $d; +} + +sub list { + my ($lei, $pfx) = @_; + 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: $!"; + 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 = PublicInbox::Config->git_config_dump($f); + unlink($f); + my $out = $cfg->get_all('lei.q.output') or return (); + map {; + s!$LOCAL_PFX!!; + $_; + } @$out +} + +sub translate_dedupe ($$$) { + my ($self, $lei, $dd) = @_; + $dd //= '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"); +} + +sub up { # updating existing saved search via "lei up" + my ($cls, $lei, $dst) = @_; + my $f; + 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); + $self->{-ovf} = "$dir/over.sqlite3"; + $self->{'-f'} = $f; + $self->{lock_path} = "$self->{-f}.flock"; + $self; +} + +sub new { # new saved search "lei q --save" + my ($cls, $lei) = @_; + my $self = bless { ale => $lei->ale }, $cls; + my $dst = $lei->{ovv}->{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; + 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'; + if (ref $q) { + $q = join("\n", map { "\tq = ".cquote_val($_) } @$q); + } else { + $q = "\tq = ".cquote_val($q); + } + $dst = "$lei->{ovv}->{fmt}:$dst" if $dst !~ m!\Aimaps?://!i; + print $fh <{opt}->{$k} // next; + for my $x (@$ary) { + print $fh "\t$k = ".cquote_val($x)."\n"; } } - bless $self->{-cfg}, 'PublicInbox::Config'; + for my $k (BOOL_FIELDS) { + my $val = $lei->{opt}->{$k} // next; + print $fh "\t$k = ".($val ? 1 : 0)."\n"; + } + close($fh) or return $lei->fail("close $f: $!"); $self->{lock_path} = "$self->{-f}.flock"; $self->{-ovf} = "$dir/over.sqlite3"; $self; @@ -59,7 +160,7 @@ sub new { sub description { $_[0]->{qstr} } # for WWW -sub cfg_set { +sub cfg_set { # called by LeiXSearch my ($self, @args) = @_; my $lk = $self->lock_for_scope; # git-config doesn't wait run_die([qw(git config -f), $self->{'-f'}, @args]); @@ -69,15 +170,27 @@ sub cfg_set { sub is_dup { my ($self, $eml, $smsg) = @_; my $oidx = $self->{oidx} // die 'BUG: no {oidx}'; - my $blob = $smsg ? $smsg->{blob} : undef; - return 1 if $blob && $oidx->blob_exists($blob); - my $lk = $self->lock_for_scope_fast; + my $lk; + if ($self->{-dedupe_mid}) { + $lk //= $self->lock_for_scope_fast; + for my $mid (@{mids_for_index($eml)}) { + my ($id, $prv); + return 1 if $oidx->next_by_mid($mid, \$id, \$prv); + } + } + my $blob = $smsg ? $smsg->{blob} : git_sha(1, $eml)->hexdigest; + $lk //= $self->lock_for_scope_fast; + return 1 if $oidx->blob_exists($blob); if (my $xoids = PublicInbox::LeiSearch::xoids_for($self, $eml, 1)) { for my $docid (values %$xoids) { $oidx->add_xref3($docid, -1, $blob, '.'); } $oidx->commit_lazy; - 1; + if ($self->{-dedupe_oid}) { + exists $xoids->{$blob} ? 1 : undef; + } else { + 1; + } } else { # n.b. above xoids_for fills out eml->{-lei_fake_mid} if needed unless ($smsg) { @@ -85,11 +198,11 @@ sub is_dup { $smsg->{bytes} = 0; $smsg->populate($eml); } + $smsg->{blob} //= $blob; $oidx->begin_lazy; $smsg->{num} = $oidx->adj_counter('eidx_docid', '+'); - $smsg->{blob} //= git_sha(1, $eml)->hexdigest; $oidx->add_overview($eml, $smsg); - $oidx->add_xref3($smsg->{num}, -1, $smsg->{blob}, '.'); + $oidx->add_xref3($smsg->{num}, -1, $blob, '.'); $oidx->commit_lazy; undef; } @@ -113,11 +226,14 @@ sub prepare_dedupe { sub over { $_[0]->{oidx} } # for xoids_for -sub git { $_[0]->{ale}->git } +# don't use ale->git directly since is_dup is called inside +# ale->git->cat_async callbacks +sub git { $_[0]->{git} //= PublicInbox::Git->new($_[0]->{ale}->git->{git_dir}) } sub pause_dedupe { my ($self) = @_; - $self->{ale}->git->cleanup; + git($self)->cleanup; + my $lockfh = delete $self->{lockfh}; # from lock_for_scope_fast; my $oidx = delete($self->{oidx}) // return; $oidx->commit_lazy; } @@ -127,6 +243,74 @@ sub mm { undef } sub altid_map { {} } sub cloneurl { [] } + +# find existing directory containing a `lei.saved-search' file based on +# $dir_ref which is an output +sub output2lssdir { + my ($self, $lei, $dir_ref, $fn_ref) = @_; + my $dst = $$dir_ref; # imap://$MAILBOX, /path/to/maildir, /path/to/mbox + 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); + $$dir_ref = $dir; + $$fn_ref = $f; + return 1; + } + 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(<