]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiXSearch.pm
lei: add-external --mirror support
[public-inbox.git] / lib / PublicInbox / LeiXSearch.pm
index e7f0ef6369e3d6fa400ad332a97314357c5e9ee9..1e5d7ca6b2f106ffdfcd10614545a5dacce6ad06 100644 (file)
@@ -8,9 +8,8 @@ package PublicInbox::LeiXSearch;
 use strict;
 use v5.10.1;
 use parent qw(PublicInbox::LeiSearch PublicInbox::IPC);
-use PublicInbox::DS qw(dwaitpid now);
+use PublicInbox::DS qw(now);
 use PublicInbox::PktOp qw(pkt_do);
-use PublicInbox::Import;
 use File::Temp 0.19 (); # 0.19 for ->newdir
 use File::Spec ();
 use PublicInbox::Search qw(xap_terms);
@@ -213,7 +212,6 @@ sub query_remote_mboxrd {
        my ($opt, $env) = @$lei{qw(opt env)};
        my @qform = (q => $lei->{mset_opt}->{qstr}, x => 'm');
        push(@qform, t => 1) if $opt->{thread};
-       my @cmd = ($self->{curl}, qw(-sSf -d), '');
        my $verbose = $opt->{verbose};
        my $reap;
        my $cerr = File::Temp->new(TEMPLATE => 'curl.err-XXXX', TMPDIR => 1);
@@ -224,43 +222,18 @@ sub query_remote_mboxrd {
                # spawn a process to force line-buffering, otherwise curl
                # will write 1 character at-a-time and parallel outputs
                # mmmaaayyy llloookkk llliiikkkeee ttthhhiiisss
-               push @cmd, '-v';
                my $o = { 1 => $lei->{2}, 2 => $lei->{2} };
                my $pid = spawn(['tail', '-f', $cerr->filename], undef, $o);
                $reap = PublicInbox::OnDestroy->new(\&kill_reap, $pid);
        }
-       for my $o ($lei->curl_opt) {
-               $o =~ s/\|[a-z0-9]\b//i; # remove single char short option
-               if ($o =~ s/=[is]@\z//) {
-                       my $ary = $opt->{$o} or next;
-                       push @cmd, map { ("--$o", $_) } @$ary;
-               } elsif ($o =~ s/=[is]\z//) {
-                       my $val = $opt->{$o} // next;
-                       push @cmd, "--$o", $val;
-               } elsif ($opt->{$o}) {
-                       push @cmd, "--$o";
-               }
-       }
-       $opt->{torsocks} = 'false' if $opt->{'no-torsocks'};
-       my $tor = $opt->{torsocks} //= 'auto';
+       my $curl = PublicInbox::LeiCurl->new($lei, $self->{curl}) or return;
+       push @$curl, '-s', '-d', '';
        my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei);
        for my $uri (@$uris) {
                $lei->{-current_url} = $uri->as_string;
                $lei->{-nr_remote_eml} = 0;
                $uri->query_form(@qform);
-               my $cmd = [ @cmd, $uri->as_string ];
-               if ($tor eq 'auto' && substr($uri->host, -6) eq '.onion' &&
-                               (($env->{LD_PRELOAD}//'') !~ /torsocks/)) {
-                       unshift @$cmd, which('torsocks');
-               } elsif (PublicInbox::Config::git_bool($tor)) {
-                       unshift @$cmd, which('torsocks');
-               }
-
-               # continue anyways if torsocks is missing; a proxy may be
-               # specified via CLI, curlrc, environment variable, or even
-               # firewall rule
-               shift(@$cmd) if !$cmd->[0];
-
+               my $cmd = $curl->for_uri($lei, $uri);
                $lei->err("# @$cmd") if $verbose;
                my ($fh, $pid) = popen_rd($cmd, $env, $rdr);
                $fh = IO::Uncompress::Gunzip->new($fh);
@@ -287,12 +260,15 @@ sub query_remote_mboxrd {
        $lei->{ovv}->ovv_atexit_child($lei);
 }
 
-sub git {
+# called by LeiOverview::each_smsg_cb
+sub git { $_[0]->{git_tmp} // die 'BUG: caller did not set {git_tmp}' }
+
+sub git_tmp ($) {
        my ($self) = @_;
        my (%seen, @dirs);
-       my $tmp = File::Temp->newdir('lei_xsrch_git-XXXXXXXX', TMPDIR => 1);
-       for my $ibx (@{$self->{shard2ibx} // []}) {
-               my $d = File::Spec->canonpath($ibx->git->{git_dir});
+       my $tmp = File::Temp->newdir("lei_xsearch_git.$$-XXXX", TMPDIR => 1);
+       for my $ibxish (locals($self)) {
+               my $d = File::Spec->canonpath($ibxish->git->{git_dir});
                $seen{$d} //= push @dirs, "$d/objects\n"
        }
        my $git_dir = $tmp->dirname;
@@ -390,25 +366,11 @@ sub query_prepare { # called by wq_do
        pkt_do($lei->{pkt_op_p}, '.') == 1 or die "do_post_augment trigger: $!"
 }
 
-sub fail_handler ($;$$) {
-       my ($lei, $code, $io) = @_;
-       for my $f (qw(lxs l2m)) {
-               my $wq = delete $lei->{$f} or next;
-               $wq->wq_wait_old($lei) if $wq->wq_kill_old; # lei-daemon
-       }
-       close($io) if $io; # needed to avoid warnings on SIGPIPE
-       $lei->x_it($code // (1 >> 8));
-}
-
-sub sigpipe_handler { # handles SIGPIPE from l2m/lxs workers
-       fail_handler($_[0], 13, delete $_[0]->{1});
-}
-
 sub do_query {
        my ($self, $lei) = @_;
        my $ops = {
-               '|' => [ \&sigpipe_handler, $lei ],
-               '!' => [ \&fail_handler, $lei ],
+               '|' => [ $lei->can('sigpipe_handler'), $lei ],
+               '!' => [ $lei->can('fail_handler'), $lei ],
                '.' => [ \&do_post_augment, $lei ],
                '' => [ \&query_done, $lei ],
                'mset_progress' => [ \&mset_progress, $lei ],
@@ -428,6 +390,11 @@ sub do_query {
                # 1031: F_SETPIPE_SZ
                fcntl($lei->{startq}, 1031, 4096) if $^O eq 'linux';
        }
+       if (!$lei->{opt}->{thread} && locals($self)) { # for query_mset
+               # lei->{git_tmp} is set for wq_wait_old so we don't
+               # delete until all lei2mail + lei_xsearch workers are reaped
+               $lei->{git_tmp} = $self->{git_tmp} = git_tmp($self);
+       }
        $self->wq_workers_start('lei_xsearch', $self->{jobs},
                                $lei->oldset, { lei => $lei });
        my $op = delete $lei->{pkt_op_c};
@@ -447,6 +414,7 @@ sub add_uri {
        if (my $curl = $self->{curl} //= which('curl') // 0) {
                require PublicInbox::MboxReader;
                require IO::Uncompress::Gunzip;
+               require PublicInbox::LeiCurl;
                push @{$self->{remotes}}, $uri;
        } else {
                warn "curl missing, ignoring $uri\n";