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);
34 my $fmt = $lei->{opt}->{'format'};
36 my $err = $f ? "regular file(s):\n@$f" : '--stdin';
37 return $lei->fail("--format unset for $err");
39 return 1 if $fmt eq 'eml';
40 require PublicInbox::MboxReader;
41 PublicInbox::MboxReader->can($fmt) ||
42 $lei->fail( "--format=$fmt unrecognized\n");
48 '!' => [ $lei->can('fail_handler'), $lei ],
49 'x_it' => [ $lei->can('x_it'), $lei ],
50 'child_error' => [ $lei->can('child_error'), $lei ],
51 '' => [ \&import_done, $lei ],
53 ($lei->{pkt_op_c}, $lei->{pkt_op_p}) = PublicInbox::PktOp->pair($ops);
54 my $self = $lei->{imp};
55 my $j = $lei->{opt}->{jobs} // scalar(@{$self->{argv}}) || 1;
56 if (my $nrd = $lei->{nrd}) {
57 # $j = $nrd->net_concurrency($j); TODO
59 my $nproc = $self->detect_nproc;
60 $j = $nproc if $j > $nproc;
62 $self->wq_workers_start('lei_import', $j, $lei->oldset, {lei => $lei});
63 my $op = delete $lei->{pkt_op_c};
64 delete $lei->{pkt_op_p};
65 $self->wq_io_do('import_stdin', []) if $self->{0};
66 for my $x (@{$self->{argv}}) {
67 $self->wq_io_do('import_path_url', [], $x);
70 $lei->event_step_init; # wait for shutdowns
71 if ($lei->{oneshot}) {
72 while ($op->{sock}) { $op->event_step }
76 sub call { # the main "lei import" method
77 my ($cls, $lei, @argv) = @_;
78 my $sto = $lei->_lei_store(1);
79 $sto->write_prepare($lei);
80 $lei->{opt}->{kw} //= 1;
81 my $self = $lei->{imp} = bless { argv => \@argv }, $cls;
82 if ($lei->{opt}->{stdin}) {
84 $lei->fail("--stdin and locations (@argv) do not mix");
85 check_fmt($lei) or return;
86 $self->{0} = $lei->{0};
90 if (-f $x) { push @f, $x }
91 elsif (-d _) { require PublicInbox::MdirReader }
93 require PublicInbox::NetReader;
94 $lei->{nrd} //= PublicInbox::NetReader->new;
95 $lei->{nrd}->add_url($x);
98 if (@f) { check_fmt($lei, \@f) or return }
99 if ($lei->{nrd} && (my @err = $lei->{nrd}->errors)) {
100 return $lei->fail(@err);
106 sub ipc_atfork_child {
108 $self->{lei}->lei_atfork_child;
109 $self->SUPER::ipc_atfork_child;
113 my ($lei, $fh, $x) = @_;
114 my $set_kw = $lei->{opt}->{kw};
115 my $fmt = $lei->{opt}->{'format'};
118 my $buf = do { local $/; <$fh> } //
119 return $lei->child_error(1 >> 8, <<"");
122 my $eml = PublicInbox::Eml->new(\$buf);
123 _import_eml($eml, $lei->{sto}, $set_kw);
124 } else { # some mbox (->can already checked in call);
125 my $cb = PublicInbox::MboxReader->can($fmt) //
126 die "BUG: bad fmt=$fmt";
127 $cb->(undef, $fh, \&_import_eml, $lei->{sto}, $set_kw);
130 $lei->child_error(1 >> 8, "<stdin>: $@") if $@;
133 sub _import_maildir { # maildir_each_file cb
134 my ($f, $sto, $set_kw) = @_;
135 $sto->ipc_do('set_eml_from_maildir', $f, $set_kw);
138 sub import_path_url {
140 my $lei = $self->{lei};
143 open my $fh, '<', $x or return $lei->child_error(1 >> 8, <<"");
144 unable to open $x: $!
146 _import_fh($lei, $fh, $x);
147 } elsif (-d _ && (-d "$x/cur" || -d "$x/new")) {
148 PublicInbox::MdirReader::maildir_each_file($x,
150 $lei->{sto}, $lei->{opt}->{kw});
152 $lei->fail("$x unsupported (TODO)");
158 _import_fh($self->{lei}, $self->{0}, '<stdin>');