]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiImport.pm
lei import: move check_input_format to lei
[public-inbox.git] / lib / PublicInbox / LeiImport.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # front-end for the "lei import" sub-command
5 package PublicInbox::LeiImport;
6 use strict;
7 use v5.10.1;
8 use parent qw(PublicInbox::IPC);
9 use PublicInbox::Eml;
10 use PublicInbox::InboxWritable qw(eml_from_path);
11 use PublicInbox::PktOp;
12
13 sub _import_eml { # MboxReader callback
14         my ($eml, $sto, $set_kw) = @_;
15         $sto->ipc_do('set_eml', $eml, $set_kw ? $sto->mbox_keywords($eml) : ());
16 }
17
18 sub import_done_wait { # dwaitpid callback
19         my ($arg, $pid) = @_;
20         my ($imp, $lei) = @$arg;
21         $lei->child_error($?, 'non-fatal errors during import') if $?;
22         my $ign = $lei->{sto}->ipc_do('done'); # PublicInbox::LeiStore::done
23         $lei->dclose;
24 }
25
26 sub import_done { # EOF callback for main daemon
27         my ($lei) = @_;
28         my $imp = delete $lei->{imp} or return;
29         $imp->wq_wait_old(\&import_done_wait, $lei);
30 }
31
32 sub do_import {
33         my ($lei) = @_;
34         my $ops = {
35                 '!' => [ $lei->can('fail_handler'), $lei ],
36                 'x_it' => [ $lei->can('x_it'), $lei ],
37                 'child_error' => [ $lei->can('child_error'), $lei ],
38                 '' => [ \&import_done, $lei ],
39         };
40         ($lei->{pkt_op_c}, $lei->{pkt_op_p}) = PublicInbox::PktOp->pair($ops);
41         my $self = $lei->{imp};
42         my $j = $lei->{opt}->{jobs} // scalar(@{$self->{argv}}) || 1;
43         if (my $nrd = $lei->{nrd}) {
44                 # $j = $nrd->net_concurrency($j); TODO
45         } else {
46                 my $nproc = $self->detect_nproc;
47                 $j = $nproc if $j > $nproc;
48         }
49         $self->wq_workers_start('lei_import', $j, $lei->oldset, {lei => $lei});
50         my $op = delete $lei->{pkt_op_c};
51         delete $lei->{pkt_op_p};
52         $self->wq_io_do('import_stdin', []) if $self->{0};
53         for my $x (@{$self->{argv}}) {
54                 $self->wq_io_do('import_path_url', [], $x);
55         }
56         $self->wq_close(1);
57         $lei->event_step_init; # wait for shutdowns
58         if ($lei->{oneshot}) {
59                 while ($op->{sock}) { $op->event_step }
60         }
61 }
62
63 sub call { # the main "lei import" method
64         my ($cls, $lei, @argv) = @_;
65         my $sto = $lei->_lei_store(1);
66         $sto->write_prepare($lei);
67         $lei->{opt}->{kw} //= 1;
68         my $self = $lei->{imp} = bless { argv => \@argv }, $cls;
69         if ($lei->{opt}->{stdin}) {
70                 @argv and return
71                         $lei->fail("--stdin and locations (@argv) do not mix");
72                 $lei->check_input_format or return;
73                 $self->{0} = $lei->{0};
74         } else {
75                 my @f;
76                 for my $x (@argv) {
77                         if (-f $x) { push @f, $x }
78                         elsif (-d _) { require PublicInbox::MdirReader }
79                         else {
80                                 require PublicInbox::NetReader;
81                                 $lei->{nrd} //= PublicInbox::NetReader->new;
82                                 $lei->{nrd}->add_url($x);
83                         }
84                 }
85                 if (@f) { $lei->check_input_format(\@f) or return }
86                 if ($lei->{nrd} && (my @err = $lei->{nrd}->errors)) {
87                         return $lei->fail(@err);
88                 }
89         }
90         do_import($lei);
91 }
92
93 sub ipc_atfork_child {
94         my ($self) = @_;
95         $self->{lei}->lei_atfork_child;
96         $self->SUPER::ipc_atfork_child;
97 }
98
99 sub _import_fh {
100         my ($lei, $fh, $x) = @_;
101         my $set_kw = $lei->{opt}->{kw};
102         my $fmt = $lei->{opt}->{'format'};
103         eval {
104                 if ($fmt eq 'eml') {
105                         my $buf = do { local $/; <$fh> } //
106                                 return $lei->child_error(1 >> 8, <<"");
107 error reading $x: $!
108
109                         my $eml = PublicInbox::Eml->new(\$buf);
110                         _import_eml($eml, $lei->{sto}, $set_kw);
111                 } else { # some mbox (->can already checked in call);
112                         my $cb = PublicInbox::MboxReader->can($fmt) //
113                                 die "BUG: bad fmt=$fmt";
114                         $cb->(undef, $fh, \&_import_eml, $lei->{sto}, $set_kw);
115                 }
116         };
117         $lei->child_error(1 >> 8, "<stdin>: $@") if $@;
118 }
119
120 sub _import_maildir { # maildir_each_file cb
121         my ($f, $sto, $set_kw) = @_;
122         $sto->ipc_do('set_eml_from_maildir', $f, $set_kw);
123 }
124
125 sub import_path_url {
126         my ($self, $x) = @_;
127         my $lei = $self->{lei};
128         # TODO auto-detect?
129         if (-f $x) {
130                 open my $fh, '<', $x or return $lei->child_error(1 >> 8, <<"");
131 unable to open $x: $!
132
133                 _import_fh($lei, $fh, $x);
134         } elsif (-d _ && (-d "$x/cur" || -d "$x/new")) {
135                 PublicInbox::MdirReader::maildir_each_file($x,
136                                         \&_import_maildir,
137                                         $lei->{sto}, $lei->{opt}->{kw});
138         } else {
139                 $lei->fail("$x unsupported (TODO)");
140         }
141 }
142
143 sub import_stdin {
144         my ($self) = @_;
145         _import_fh($self->{lei}, $self->{0}, '<stdin>');
146 }
147
148 1;