]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiImport.pm
lei import: start rearranging code for IMAP support
[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 check_fmt ($;$) {
33         my ($lei, $f) = @_;
34         my $fmt = $lei->{opt}->{'format'};
35         if (!$fmt) {
36                 my $err = $f ? "regular file(s):\n@$f" : '--stdin';
37                 return $lei->fail("--format unset for $err");
38         }
39         return 1 if $fmt eq 'eml';
40         require PublicInbox::MboxReader;
41         PublicInbox::MboxReader->can($fmt) ||
42                                 $lei->fail( "--format=$fmt unrecognized\n");
43 }
44
45 sub do_import {
46         my ($lei) = @_;
47         my $ops = {
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 ],
52         };
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
58         } else {
59                 my $nproc = $self->detect_nproc;
60                 $j = $nproc if $j > $nproc;
61         }
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);
68         }
69         $self->wq_close(1);
70         $lei->event_step_init; # wait for shutdowns
71         if ($lei->{oneshot}) {
72                 while ($op->{sock}) { $op->event_step }
73         }
74 }
75
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}) {
83                 @argv and return
84                         $lei->fail("--stdin and locations (@argv) do not mix");
85                 check_fmt($lei) or return;
86                 $self->{0} = $lei->{0};
87         } else {
88                 my @f;
89                 for my $x (@argv) {
90                         if (-f $x) { push @f, $x }
91                         elsif (-d _) { require PublicInbox::MdirReader }
92                         else {
93                                 require PublicInbox::NetReader;
94                                 $lei->{nrd} //= PublicInbox::NetReader->new;
95                                 $lei->{nrd}->add_url($x);
96                         }
97                 }
98                 if (@f) { check_fmt($lei, \@f) or return }
99                 if ($lei->{nrd} && (my @err = $lei->{nrd}->errors)) {
100                         return $lei->fail(@err);
101                 }
102         }
103         do_import($lei);
104 }
105
106 sub ipc_atfork_child {
107         my ($self) = @_;
108         $self->{lei}->lei_atfork_child;
109         $self->SUPER::ipc_atfork_child;
110 }
111
112 sub _import_fh {
113         my ($lei, $fh, $x) = @_;
114         my $set_kw = $lei->{opt}->{kw};
115         my $fmt = $lei->{opt}->{'format'};
116         eval {
117                 if ($fmt eq 'eml') {
118                         my $buf = do { local $/; <$fh> } //
119                                 return $lei->child_error(1 >> 8, <<"");
120 error reading $x: $!
121
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);
128                 }
129         };
130         $lei->child_error(1 >> 8, "<stdin>: $@") if $@;
131 }
132
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);
136 }
137
138 sub import_path_url {
139         my ($self, $x) = @_;
140         my $lei = $self->{lei};
141         # TODO auto-detect?
142         if (-f $x) {
143                 open my $fh, '<', $x or return $lei->child_error(1 >> 8, <<"");
144 unable to open $x: $!
145
146                 _import_fh($lei, $fh, $x);
147         } elsif (-d _ && (-d "$x/cur" || -d "$x/new")) {
148                 PublicInbox::MdirReader::maildir_each_file($x,
149                                         \&_import_maildir,
150                                         $lei->{sto}, $lei->{opt}->{kw});
151         } else {
152                 $lei->fail("$x unsupported (TODO)");
153         }
154 }
155
156 sub import_stdin {
157         my ($self) = @_;
158         _import_fh($self->{lei}, $self->{0}, '<stdin>');
159 }
160
161 1;