From: "Eric Wong (Contractor, The Linux Foundation)" <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 4/4] mda: support v2 inboxes
Date: Thu, 29 Mar 2018 20:17:20 +0000 [thread overview]
Message-ID: <20180329201720.7165-5-e@80x24.org> (raw)
In-Reply-To: <20180329201720.7165-1-e@80x24.org>
I mainly focus on -watch for mirroring busy mailing lists, but
using -mda should remain an option.
---
MANIFEST | 1 +
lib/PublicInbox/Emergency.pm | 3 ++-
script/public-inbox-mda | 14 +++++++++--
t/v2mda.t | 59 ++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 74 insertions(+), 3 deletions(-)
create mode 100644 t/v2mda.t
diff --git a/MANIFEST b/MANIFEST
index ce6cd11..ad145f7 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -186,6 +186,7 @@ t/spawn.t
t/thread-all.t
t/thread-cycle.t
t/utf8.mbox
+t/v2mda.t
t/v2reindex.t
t/v2writable.t
t/view.t
diff --git a/lib/PublicInbox/Emergency.pm b/lib/PublicInbox/Emergency.pm
index 231b419..66adc63 100644
--- a/lib/PublicInbox/Emergency.pm
+++ b/lib/PublicInbox/Emergency.pm
@@ -18,7 +18,7 @@ sub new {
next if -d $d;
-d $d or mkdir($d) or die "failed to mkdir($d): $!\n";
}
- bless { dir => $dir, files => {}, t => 0, cnt => 0 }, $class;
+ bless { dir => $dir, files => {}, t => 0, cnt => 0, pid => $$ }, $class;
}
sub _fn_in {
@@ -75,6 +75,7 @@ sub fh {
sub commit {
my ($self) = @_;
+ $$ == $self->{pid} or return; # no-op in forked child
delete $self->{fh};
my $tmp = delete $self->{tmp} or return;
diff --git a/script/public-inbox-mda b/script/public-inbox-mda
index f1eaf62..766d58a 100755
--- a/script/public-inbox-mda
+++ b/script/public-inbox-mda
@@ -78,8 +78,18 @@ if (ref($ret) && $ret->isa('Email::MIME')) { # filter altered message
} # else { accept
PublicInbox::MDA->set_list_headers($mime, $dst);
-my $git = PublicInbox::Git->new($main_repo);
-my $im = PublicInbox::Import->new($git, $dst->{name}, $recipient);
+my $v = $dst->{version} || 1;
+my $im;
+if ($v == 2) {
+ require PublicInbox::V2Writable;
+ $im = PublicInbox::V2Writable->new($dst);
+ $im->{parallel} = 0; # pointless to be parallel for a single message
+} elsif ($v == 1) {
+ my $git = $dst->git;
+ $im = PublicInbox::Import->new($git, $dst->{name}, $recipient, $dst);
+} else {
+ die "Unsupported inbox version: $v\n";
+}
if (defined $im->add($mime)) {
$emm = $emm->abort;
} else {
diff --git a/t/v2mda.t b/t/v2mda.t
new file mode 100644
index 0000000..be27ca0
--- /dev/null
+++ b/t/v2mda.t
@@ -0,0 +1,59 @@
+# Copyright (C) 2018 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+use strict;
+use warnings;
+use Test::More;
+use PublicInbox::MIME;
+use File::Temp qw/tempdir/;
+use Fcntl qw(SEEK_SET);
+use Cwd;
+
+foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
+ eval "require $mod";
+ plan skip_all => "$mod missing for v2mda.t" if $@;
+}
+use_ok 'PublicInbox::V2Writable';
+my $tmpdir = tempdir('pi-v2mda-XXXXXX', TMPDIR => 1, CLEANUP => 1);
+my $ibx = {
+ mainrepo => "$tmpdir/inbox",
+ name => 'test-v2writable',
+ address => [ 'test@example.com' ],
+};
+my $mime = PublicInbox::MIME->create(
+ header => [
+ From => 'a@example.com',
+ To => 'test@example.com',
+ Subject => 'this is a subject',
+ Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
+ 'Message-ID' => '<foo@bar>',
+ 'List-ID' => '<test.example.com>',
+ ],
+ body => "hello world\n",
+);
+
+my $mda = "blib/script/public-inbox-mda";
+ok(-f "blib/script/public-inbox-mda", '-mda exists');
+my $main_bin = getcwd()."/t/main-bin";
+local $ENV{PI_DIR} = "$tmpdir/foo";
+local $ENV{PATH} = "$main_bin:blib/script:$ENV{PATH}";
+my @cmd = (qw(public-inbox-init -V2), $ibx->{name},
+ $ibx->{mainrepo}, 'http://localhost/test',
+ $ibx->{address}->[0]);
+ok(PublicInbox::Import::run_die(\@cmd), 'initialized v2 inbox');
+
+open my $tmp, '+>', undef or die "failed to open anonymous tempfile: $!";
+ok($tmp->print($mime->as_string), 'wrote to temporary file');
+ok($tmp->flush, 'flushed temporary file');
+ok($tmp->sysseek(0, SEEK_SET), 'seeked');
+
+my $rdr = { 0 => fileno($tmp) };
+local $ENV{ORIGINAL_RECIPIENT} = 'test@example.com';
+ok(PublicInbox::Import::run_die(['public-inbox-mda'], undef, $rdr),
+ 'mda delivered a message');
+
+$ibx = PublicInbox::Inbox->new($ibx);
+my $res = $ibx->search->query('');
+my $saved = $ibx->smsg_mime($res->{msgs}->[0]);
+is($saved->{mime}->as_string, $mime->as_string, 'injected message');
+
+done_testing();
--
EW
prev parent reply other threads:[~2018-03-29 20:17 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-03-29 20:17 [PATCH 0/4] tooling updates for v2 Eric Wong (Contractor, The Linux Foundation)
2018-03-29 20:17 ` [PATCH 1/4] import: run_die supports redirects as spawn does Eric Wong (Contractor, The Linux Foundation)
2018-03-29 20:17 ` [PATCH 2/4] v2writable: initializing an existing inbox is idempotent Eric Wong (Contractor, The Linux Foundation)
2018-03-29 20:17 ` [PATCH 3/4] public-inbox-compact: new tool for driving xapian-compact Eric Wong (Contractor, The Linux Foundation)
2018-03-29 20:17 ` Eric Wong (Contractor, The Linux Foundation) [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://public-inbox.org/README
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180329201720.7165-5-e@80x24.org \
--to=e@80x24.org \
--cc=meta@public-inbox.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/public-inbox.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).