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);
32 sub call { # the main "lei import" method
33 my ($cls, $lei, @argv) = @_;
34 my $sto = $lei->_lei_store(1);
35 $sto->write_prepare($lei);
36 $lei->{opt}->{kw} //= 1;
37 my $fmt = $lei->{opt}->{'format'};
38 my $self = $lei->{imp} = bless {}, $cls;
41 if (-f $x) { push @f, $x }
42 elsif (-d _) { require PublicInbox::MdirReader }
45 return $lei->fail("--format unset for regular file(s):\n@f");
46 if (@f && $fmt ne 'eml') {
47 require PublicInbox::MboxReader;
48 PublicInbox::MboxReader->can($fmt) or
49 return $lei->fail( "--format=$fmt unrecognized\n");
51 $self->{0} = $lei->{0} if $lei->{opt}->{stdin};
53 '!' => [ $lei->can('fail_handler'), $lei ],
54 'x_it' => [ $lei->can('x_it'), $lei ],
55 'child_error' => [ $lei->can('child_error'), $lei ],
56 '' => [ \&import_done, $lei ],
58 ($lei->{pkt_op_c}, $lei->{pkt_op_p}) = PublicInbox::PktOp->pair($ops);
59 my $j = $lei->{opt}->{jobs} // scalar(@argv) || 1;
60 my $nproc = $self->detect_nproc;
61 $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};
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 ipc_atfork_child {
78 $self->{lei}->lei_atfork_child;
79 $self->SUPER::ipc_atfork_child;
83 my ($lei, $fh, $x) = @_;
84 my $set_kw = $lei->{opt}->{kw};
85 my $fmt = $lei->{opt}->{'format'};
88 my $buf = do { local $/; <$fh> } //
89 return $lei->child_error(1 >> 8, <<"");
92 my $eml = PublicInbox::Eml->new(\$buf);
93 _import_eml($eml, $lei->{sto}, $set_kw);
94 } else { # some mbox (->can already checked in call);
95 my $cb = PublicInbox::MboxReader->can($fmt) //
96 die "BUG: bad fmt=$fmt";
97 $cb->(undef, $fh, \&_import_eml, $lei->{sto}, $set_kw);
100 $lei->child_error(1 >> 8, "<stdin>: $@") if $@;
103 sub _import_maildir { # maildir_each_file cb
104 my ($f, $sto, $set_kw) = @_;
105 $sto->ipc_do('set_eml_from_maildir', $f, $set_kw);
108 sub import_path_url {
110 my $lei = $self->{lei};
113 open my $fh, '<', $x or return $lei->child_error(1 >> 8, <<"");
114 unable to open $x: $!
116 _import_fh($lei, $fh, $x);
117 } elsif (-d _ && (-d "$x/cur" || -d "$x/new")) {
118 PublicInbox::MdirReader::maildir_each_file($x,
120 $lei->{sto}, $lei->{opt}->{kw});
122 $lei->fail("$x unsupported (TODO)");
128 _import_fh($self->{lei}, $self->{0}, '<stdin>');