]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiImport.pm
lei import|convert: support mbox locking on reads
[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::PktOp qw(pkt_do);
11
12 sub _import_eml { # MboxReader callback
13         my ($eml, $sto, $set_kw) = @_;
14         $sto->ipc_do('set_eml', $eml, $set_kw ? $sto->mbox_keywords($eml) : ());
15 }
16
17 sub import_done_wait { # dwaitpid callback
18         my ($arg, $pid) = @_;
19         my ($imp, $lei) = @$arg;
20         $lei->child_error($?, 'non-fatal errors during import') if $?;
21         my $ign = $lei->{sto}->ipc_do('done'); # PublicInbox::LeiStore::done
22         $lei->dclose;
23 }
24
25 sub import_done { # EOF callback for main daemon
26         my ($lei) = @_;
27         my $imp = delete $lei->{imp} or return;
28         $imp->wq_wait_old(\&import_done_wait, $lei);
29 }
30
31 sub net_merge_complete { # callback used by LeiAuth
32         my ($self) = @_;
33         for my $input (@{$self->{inputs}}) {
34                 $self->wq_io_do('import_path_url', [], $input);
35         }
36         $self->wq_close(1);
37 }
38
39 sub import_start {
40         my ($lei) = @_;
41         my $self = $lei->{imp};
42         my $j = $lei->{opt}->{jobs} // scalar(@{$self->{inputs}}) || 1;
43         if (my $net = $lei->{net}) {
44                 # $j = $net->net_concurrency($j); TODO
45         } else {
46                 my $nproc = $self->detect_nproc;
47                 $j = $nproc if $j > $nproc;
48         }
49         my $ops = { '' => [ \&import_done, $lei ] };
50         $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
51         $self->{-wq_nr_workers} = $j // 1; # locked
52         my $op = $lei->workers_start($self, 'lei_import', undef, $ops);
53         $self->wq_io_do('import_stdin', []) if $self->{0};
54         net_merge_complete($self) unless $lei->{auth};
55         while ($op && $op->{sock}) { $op->event_step }
56 }
57
58 sub call { # the main "lei import" method
59         my ($cls, $lei, @inputs) = @_;
60         my $sto = $lei->_lei_store(1);
61         $sto->write_prepare($lei);
62         my ($net, @f, @d);
63         $lei->{opt}->{kw} //= 1;
64         my $self = $lei->{imp} = bless { inputs => \@inputs }, $cls;
65         if ($lei->{opt}->{stdin}) {
66                 @inputs and return $lei->fail("--stdin and @inputs do not mix");
67                 $lei->check_input_format or return;
68                 $self->{0} = $lei->{0};
69         }
70
71         my $fmt = $lei->{opt}->{'in-format'};
72         # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
73         for my $input (@inputs) {
74                 my $input_path = $input;
75                 if ($input =~ m!\A(?:imaps?|nntps?|s?news)://!i) {
76                         require PublicInbox::NetReader;
77                         $net //= PublicInbox::NetReader->new;
78                         $net->add_url($input);
79                 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
80                         my $ifmt = lc $1;
81                         if (($fmt // $ifmt) ne $ifmt) {
82                                 return $lei->fail(<<"");
83 --in-format=$fmt and `$ifmt:' conflict
84
85                         }
86                         if (-f $input_path) {
87                                 require PublicInbox::MboxLock;
88                                 require PublicInbox::MboxReader;
89                                 PublicInbox::MboxReader->can($ifmt) or return
90                                         $lei->fail("$ifmt not supported");
91                         } elsif (-d _) {
92                                 require PublicInbox::MdirReader;
93                                 $ifmt eq 'maildir' or return
94                                         $lei->fail("$ifmt not supported");
95                         } else {
96                                 return $lei->fail("Unable to handle $input");
97                         }
98                 } elsif (-f $input) { push @f, $input
99                 } elsif (-d _) { push @d, $input
100                 } else { return $lei->fail("Unable to handle $input") }
101         }
102         if (@f) { $lei->check_input_format(\@f) or return }
103         if (@d) { # TODO: check for MH vs Maildir, here
104                 require PublicInbox::MdirReader;
105         }
106         $self->{inputs} = \@inputs;
107         if ($net) {
108                 if (my $err = $net->errors) {
109                         return $lei->fail($err);
110                 }
111                 $net->{quiet} = $lei->{opt}->{quiet};
112                 $lei->{net} = $net;
113                 require PublicInbox::LeiAuth;
114                 $lei->{auth} = PublicInbox::LeiAuth->new;
115         }
116         import_start($lei);
117 }
118
119 sub ipc_atfork_child {
120         my ($self) = @_;
121         my $lei = $self->{lei};
122         delete $lei->{imp}; # drop circular ref
123         $lei->lei_atfork_child;
124         $self->SUPER::ipc_atfork_child;
125         $lei->{auth}->do_auth_atfork($self) if $lei->{auth};
126         undef;
127 }
128
129 sub _import_fh {
130         my ($lei, $fh, $input, $ifmt) = @_;
131         my $set_kw = $lei->{opt}->{kw};
132         eval {
133                 if ($ifmt eq 'eml') {
134                         my $buf = do { local $/; <$fh> } //
135                                 return $lei->child_error(1 << 8, <<"");
136 error reading $input: $!
137
138                         my $eml = PublicInbox::Eml->new(\$buf);
139                         _import_eml($eml, $lei->{sto}, $set_kw);
140                 } else { # some mbox (->can already checked in call);
141                         my $cb = PublicInbox::MboxReader->can($ifmt) //
142                                 die "BUG: bad fmt=$ifmt";
143                         $cb->(undef, $fh, \&_import_eml, $lei->{sto}, $set_kw);
144                 }
145         };
146         $lei->child_error(1 << 8, "$input: $@") if $@;
147 }
148
149 sub _import_maildir { # maildir_each_file cb
150         my ($f, $sto, $set_kw) = @_;
151         $sto->ipc_do('set_eml_from_maildir', $f, $set_kw);
152 }
153
154 sub _import_net { # imap_each, nntp_each cb
155         my ($url, $uid, $kw, $eml, $sto, $set_kw) = @_;
156         $sto->ipc_do('set_eml', $eml, $set_kw ? @$kw : ());
157 }
158
159 sub import_path_url {
160         my ($self, $input) = @_;
161         my $lei = $self->{lei};
162         my $ifmt = lc($lei->{opt}->{'in-format'} // '');
163         # TODO auto-detect?
164         if ($input =~ m!\Aimaps?://!i) {
165                 $lei->{net}->imap_each($input, \&_import_net, $lei->{sto},
166                                         $lei->{opt}->{kw});
167                 return;
168         } elsif ($input =~ m!\A(?:nntps?|s?news)://!i) {
169                 $lei->{net}->nntp_each($input, \&_import_net, $lei->{sto}, 0);
170                 return;
171         } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
172                 $ifmt = lc $1;
173         }
174         if (-f $input) {
175                 my $m = $lei->{opt}->{'lock'} // ($ifmt eq 'eml' ? ['none'] :
176                                 PublicInbox::MboxLock->defaults);
177                 my $mbl = PublicInbox::MboxLock->acq($input, 0, $m);
178                 _import_fh($lei, $mbl->{fh}, $input, $ifmt);
179         } elsif (-d _ && (-d "$input/cur" || -d "$input/new")) {
180                 return $lei->fail(<<EOM) if $ifmt && $ifmt ne 'maildir';
181 $input appears to a be a maildir, not $ifmt
182 EOM
183                 PublicInbox::MdirReader::maildir_each_file($input,
184                                         \&_import_maildir,
185                                         $lei->{sto}, $lei->{opt}->{kw});
186         } else {
187                 $lei->fail("$input unsupported (TODO)");
188         }
189 }
190
191 sub import_stdin {
192         my ($self) = @_;
193         my $lei = $self->{lei};
194         _import_fh($lei, delete $self->{0}, '<stdin>', $lei->{opt}->{'in-format'});
195 }
196
197 no warnings 'once'; # the following works even when LeiAuth is lazy-loaded
198 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;
199 1;