1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # front-end for the "lei import" sub-command
5 package PublicInbox::LeiImport;
8 use parent qw(PublicInbox::IPC);
10 use PublicInbox::InboxWritable qw(eml_from_path);
11 use PublicInbox::PktOp;
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) : ());
18 sub import_done_wait { # dwaitpid callback
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
26 sub import_done { # EOF callback for main daemon
28 my $imp = delete $lei->{imp} or return;
29 $imp->wq_wait_old(\&import_done_wait, $lei);
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 ],
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
46 my $nproc = $self->detect_nproc;
47 $j = $nproc if $j > $nproc;
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);
57 $lei->event_step_init; # wait for shutdowns
58 if ($lei->{oneshot}) {
59 while ($op->{sock}) { $op->event_step }
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}) {
71 $lei->fail("--stdin and locations (@argv) do not mix");
72 $lei->check_input_format or return;
73 $self->{0} = $lei->{0};
77 if (-f $x) { push @f, $x }
78 elsif (-d _) { require PublicInbox::MdirReader }
80 require PublicInbox::NetReader;
81 $lei->{nrd} //= PublicInbox::NetReader->new;
82 $lei->{nrd}->add_url($x);
85 if (@f) { $lei->check_input_format(\@f) or return }
86 if ($lei->{nrd} && (my @err = $lei->{nrd}->errors)) {
87 return $lei->fail(@err);
93 sub ipc_atfork_child {
95 $self->{lei}->lei_atfork_child;
96 $self->SUPER::ipc_atfork_child;
100 my ($lei, $fh, $x) = @_;
101 my $set_kw = $lei->{opt}->{kw};
102 my $fmt = $lei->{opt}->{'format'};
105 my $buf = do { local $/; <$fh> } //
106 return $lei->child_error(1 >> 8, <<"");
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);
117 $lei->child_error(1 >> 8, "<stdin>: $@") if $@;
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);
125 sub import_path_url {
127 my $lei = $self->{lei};
130 open my $fh, '<', $x or return $lei->child_error(1 >> 8, <<"");
131 unable to open $x: $!
133 _import_fh($lei, $fh, $x);
134 } elsif (-d _ && (-d "$x/cur" || -d "$x/new")) {
135 PublicInbox::MdirReader::maildir_each_file($x,
137 $lei->{sto}, $lei->{opt}->{kw});
139 $lei->fail("$x unsupported (TODO)");
145 _import_fh($self->{lei}, $self->{0}, '<stdin>');