use strict;
use v5.10.1;
use parent qw(PublicInbox::IPC);
-use PublicInbox::MboxReader;
use PublicInbox::Eml;
+use PublicInbox::InboxWritable qw(eml_from_path);
+use PublicInbox::PktOp;
sub _import_eml { # MboxReader callback
my ($eml, $sto, $set_kw) = @_;
$imp->wq_wait_old(\&import_done_wait, $lei);
}
-sub call { # the main "lei import" method
- my ($cls, $lei, @argv) = @_;
- my $sto = $lei->_lei_store(1);
- $sto->write_prepare($lei);
- $lei->{opt}->{kw} //= 1;
+sub check_fmt ($;$) {
+ my ($lei, $f) = @_;
my $fmt = $lei->{opt}->{'format'};
- my $self = $lei->{imp} = bless {}, $cls;
- return $lei->fail('--format unspecified') if !$fmt;
- $self->{0} = $lei->{0} if $lei->{opt}->{stdin};
+ if (!$fmt) {
+ my $err = $f ? "regular file(s):\n@$f" : '--stdin';
+ return $lei->fail("--format unset for $err");
+ }
+ return 1 if $fmt eq 'eml';
+ require PublicInbox::MboxReader;
+ PublicInbox::MboxReader->can($fmt) ||
+ $lei->fail( "--format=$fmt unrecognized\n");
+}
+
+sub do_import {
+ my ($lei) = @_;
my $ops = {
'!' => [ $lei->can('fail_handler'), $lei ],
'x_it' => [ $lei->can('x_it'), $lei ],
'' => [ \&import_done, $lei ],
};
($lei->{pkt_op_c}, $lei->{pkt_op_p}) = PublicInbox::PktOp->pair($ops);
- my $j = $lei->{opt}->{jobs} // scalar(@argv) || 1;
- my $nproc = $self->detect_nproc;
- $j = $nproc if $j > $nproc;
+ my $self = $lei->{imp};
+ my $j = $lei->{opt}->{jobs} // scalar(@{$self->{argv}}) || 1;
+ if (my $nrd = $lei->{nrd}) {
+ # $j = $nrd->net_concurrency($j); TODO
+ } else {
+ my $nproc = $self->detect_nproc;
+ $j = $nproc if $j > $nproc;
+ }
$self->wq_workers_start('lei_import', $j, $lei->oldset, {lei => $lei});
my $op = delete $lei->{pkt_op_c};
delete $lei->{pkt_op_p};
$self->wq_io_do('import_stdin', []) if $self->{0};
- for my $x (@argv) {
+ for my $x (@{$self->{argv}}) {
$self->wq_io_do('import_path_url', [], $x);
}
$self->wq_close(1);
}
}
+sub call { # the main "lei import" method
+ my ($cls, $lei, @argv) = @_;
+ my $sto = $lei->_lei_store(1);
+ $sto->write_prepare($lei);
+ $lei->{opt}->{kw} //= 1;
+ my $self = $lei->{imp} = bless { argv => \@argv }, $cls;
+ if ($lei->{opt}->{stdin}) {
+ @argv and return
+ $lei->fail("--stdin and locations (@argv) do not mix");
+ check_fmt($lei) or return;
+ $self->{0} = $lei->{0};
+ } else {
+ my @f;
+ for my $x (@argv) {
+ if (-f $x) { push @f, $x }
+ elsif (-d _) { require PublicInbox::MdirReader }
+ else {
+ require PublicInbox::NetReader;
+ $lei->{nrd} //= PublicInbox::NetReader->new;
+ $lei->{nrd}->add_url($x);
+ }
+ }
+ if (@f) { check_fmt($lei, \@f) or return }
+ if ($lei->{nrd} && (my @err = $lei->{nrd}->errors)) {
+ return $lei->fail(@err);
+ }
+ }
+ do_import($lei);
+}
+
sub ipc_atfork_child {
my ($self) = @_;
$self->{lei}->lei_atfork_child;
if ($fmt eq 'eml') {
my $buf = do { local $/; <$fh> } //
return $lei->child_error(1 >> 8, <<"");
- error reading $x: $!
+error reading $x: $!
my $eml = PublicInbox::Eml->new(\$buf);
_import_eml($eml, $lei->{sto}, $set_kw);
- } else { # some mbox
- my $cb = PublicInbox::MboxReader->can($fmt);
- $cb or return $lei->child_error(1 >> 8, <<"");
- --format $fmt unsupported for $x
-
+ } else { # some mbox (->can already checked in call);
+ my $cb = PublicInbox::MboxReader->can($fmt) //
+ die "BUG: bad fmt=$fmt";
$cb->(undef, $fh, \&_import_eml, $lei->{sto}, $set_kw);
}
};
$lei->child_error(1 >> 8, "<stdin>: $@") if $@;
}
+sub _import_maildir { # maildir_each_file cb
+ my ($f, $sto, $set_kw) = @_;
+ $sto->ipc_do('set_eml_from_maildir', $f, $set_kw);
+}
+
sub import_path_url {
my ($self, $x) = @_;
my $lei = $self->{lei};
unable to open $x: $!
_import_fh($lei, $fh, $x);
+ } elsif (-d _ && (-d "$x/cur" || -d "$x/new")) {
+ PublicInbox::MdirReader::maildir_each_file($x,
+ \&_import_maildir,
+ $lei->{sto}, $lei->{opt}->{kw});
} else {
$lei->fail("$x unsupported (TODO)");
}