user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
* [PATCH 0/4] tooling updates for v2
@ 2018-03-29 20:17 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)
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Eric Wong (Contractor, The Linux Foundation) @ 2018-03-29 20:17 UTC (permalink / raw)
  To: meta

I suppose people will use -mda for large mailing lists, too
(as opposed to -watch); so it now supports v2.  After importing
a giant archives into a v2 inbox, the new -compact wrapper
can consolidate and reduce Xapian space (and FD) overhead.

Eric Wong (Contractor, The Linux Foundation) (4):
  import: run_die supports redirects as spawn does
  v2writable: initializing an existing inbox is idempotent
  public-inbox-compact: new tool for driving xapian-compact
  mda: support v2 inboxes

 Documentation/public-inbox-compact.pod | 50 ++++++++++++++++++
 MANIFEST                               |  4 ++
 lib/PublicInbox/Emergency.pm           |  3 +-
 lib/PublicInbox/Import.pm              |  6 +--
 lib/PublicInbox/V2Writable.pm          |  1 -
 script/public-inbox-compact            | 94 ++++++++++++++++++++++++++++++++++
 script/public-inbox-init               | 17 +++++-
 script/public-inbox-mda                | 14 ++++-
 t/convert-compact.t                    | 57 +++++++++++++++++++++
 t/init.t                               | 32 +++++++++++-
 t/v2mda.t                              | 59 +++++++++++++++++++++
 11 files changed, 327 insertions(+), 10 deletions(-)
 create mode 100644 Documentation/public-inbox-compact.pod
 create mode 100755 script/public-inbox-compact
 create mode 100644 t/convert-compact.t
 create mode 100644 t/v2mda.t

-- 
EW

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/4] import: run_die supports redirects as spawn does
  2018-03-29 20:17 [PATCH 0/4] tooling updates for v2 Eric Wong (Contractor, The Linux Foundation)
@ 2018-03-29 20:17 ` 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)
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Eric Wong (Contractor, The Linux Foundation) @ 2018-03-29 20:17 UTC (permalink / raw)
  To: meta

We'll be using it in more future tests and scripts.
---
 lib/PublicInbox/Import.pm | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/lib/PublicInbox/Import.pm b/lib/PublicInbox/Import.pm
index f00c260..463b44e 100644
--- a/lib/PublicInbox/Import.pm
+++ b/lib/PublicInbox/Import.pm
@@ -387,9 +387,9 @@ sub add {
 	$self->{tip} = ":$commit";
 }
 
-sub run_die ($;$) {
-	my ($cmd, $env) = @_;
-	my $pid = spawn($cmd, $env, undef);
+sub run_die ($;$$) {
+	my ($cmd, $env, $rdr) = @_;
+	my $pid = spawn($cmd, $env, $rdr);
 	defined $pid or die "spawning ".join(' ', @$cmd)." failed: $!";
 	waitpid($pid, 0) == $pid or die join(' ', @$cmd) .' did not finish';
 	$? == 0 or die join(' ', @$cmd) . " failed: $?\n";
-- 
EW


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/4] v2writable: initializing an existing inbox is idempotent
  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 ` 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 ` [PATCH 4/4] mda: support v2 inboxes Eric Wong (Contractor, The Linux Foundation)
  3 siblings, 0 replies; 5+ messages in thread
From: Eric Wong (Contractor, The Linux Foundation) @ 2018-03-29 20:17 UTC (permalink / raw)
  To: meta

And we do not want to start making confused repos if somebody
leaves out "-V2" the second time around.
---
 lib/PublicInbox/V2Writable.pm |  1 -
 script/public-inbox-init      | 17 ++++++++++++++++-
 t/init.t                      | 32 ++++++++++++++++++++++++++++++--
 3 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/lib/PublicInbox/V2Writable.pm b/lib/PublicInbox/V2Writable.pm
index b516278..4e7d6de 100644
--- a/lib/PublicInbox/V2Writable.pm
+++ b/lib/PublicInbox/V2Writable.pm
@@ -360,7 +360,6 @@ sub git_init {
 	my ($self, $new) = @_;
 	my $pfx = "$self->{-inbox}->{mainrepo}/git";
 	my $git_dir = "$pfx/$new.git";
-	die "$git_dir exists\n" if -e $git_dir;
 	my @cmd = (qw(git init --bare -q), $git_dir);
 	PublicInbox::Import::run_die(\@cmd);
 
diff --git a/script/public-inbox-init b/script/public-inbox-init
index 86cf8b5..d6a6482 100755
--- a/script/public-inbox-init
+++ b/script/public-inbox-init
@@ -15,7 +15,7 @@ use Cwd qw/abs_path/;
 
 sub x { system(@_) and die join(' ', @_). " failed: $?\n" }
 sub usage { print STDERR "Usage: $usage\n"; exit 1 }
-my $version = 1;
+my $version = undef;
 my %opts = ( 'V|version=i' => \$version );
 GetOptions(%opts) or usage();
 my $name = shift @ARGV or usage();
@@ -71,6 +71,21 @@ my $pfx = "publicinbox.$name";
 my @x = (qw/git config/, "--file=$pi_config_tmp");
 
 $mainrepo = abs_path($mainrepo);
+if (-f "$mainrepo/inbox.lock") {
+	if (!defined $version) {
+		$version = 2;
+	} elsif ($version != 2) {
+		die "$mainrepo is a -V2 repo, -V$version specified\n"
+	}
+} elsif (-d "$mainrepo/objects") {
+	if (!defined $version) {
+		$version = 1;
+	} elsif ($version != 1) {
+		die "$mainrepo is a -V1 repo, -V$version specified\n"
+	}
+}
+
+$version = 1 unless defined $version;
 
 if ($version >= 2) {
 	require PublicInbox::V2Writable;
diff --git a/t/init.t b/t/init.t
index 6ae608e..59f5481 100644
--- a/t/init.t
+++ b/t/init.t
@@ -7,6 +7,23 @@ use PublicInbox::Config;
 use File::Temp qw/tempdir/;
 my $tmpdir = tempdir('pi-init-XXXXXX', TMPDIR => 1, CLEANUP => 1);
 use constant pi_init => 'blib/script/public-inbox-init';
+use PublicInbox::Import;
+use File::Basename;
+open my $null, '>>', '/dev/null';
+my $rdr = { 2 => fileno($null) };
+sub quiet_fail {
+	my ($cmd, $msg) = @_;
+	# run_die doesn't take absolute paths:
+	my $path = $ENV{PATH};
+	if (index($cmd->[0], '/') >= 0) {
+		my ($dir, $base) = ($cmd->[0] =~ m!\A(.+)/([^/]+)\z!);
+		$path = "$dir:$path";
+		$cmd->[0] = $base;
+	}
+	local $ENV{PATH} = $path;
+	eval { PublicInbox::Import::run_die($cmd, undef, $rdr) };
+	isnt($@, '', $msg);
+}
 
 {
 	local $ENV{PI_DIR} = "$tmpdir/.public-inbox/";
@@ -23,6 +40,10 @@ use constant pi_init => 'blib/script/public-inbox-init';
 		   qw(http://example.com/clist clist@example.com));
 	is(system(@cmd), 0, 'public-inbox-init clist OK');
 	is((stat($cfgfile))[2] & 07777, 0666, "permissions preserved");
+
+	@cmd = (pi_init, 'clist', '-V2', "$tmpdir/clist",
+		   qw(http://example.com/clist clist@example.com));
+	quiet_fail(\@cmd, 'attempting to init V2 from V1 fails');
 }
 
 SKIP: {
@@ -38,8 +59,15 @@ SKIP: {
 	ok(-d "$tmpdir/v2list", 'v2list directory exists');
 	ok(-f "$tmpdir/v2list/msgmap.sqlite3", 'msgmap exists');
 	ok(-d "$tmpdir/v2list/all.git", 'catch-all.git directory exists');
-	@cmd = (qw(git config), "--file=$tmpdir/v2list/all.git/config",
-		qw(core.sharedRepository 0644));
+	@cmd = (pi_init, 'v2list', "$tmpdir/v2list",
+		   qw(http://example.com/v2list v2list@example.com));
+	is(system(@cmd), 0, 'public-inbox-init is idempotent');
+	ok(! -d "$tmpdir/public-inbox" && !-d "$tmpdir/objects",
+		'idempotent invocation w/o -V2 does not make inbox v1');
+
+	@cmd = (pi_init, 'v2list', "-V1", "$tmpdir/v2list",
+		   qw(http://example.com/v2list v2list@example.com));
+	quiet_fail(\@cmd, 'initializing V2 as V1 fails');
 }
 
 done_testing();
-- 
EW


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 3/4] public-inbox-compact: new tool for driving xapian-compact
  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 ` Eric Wong (Contractor, The Linux Foundation)
  2018-03-29 20:17 ` [PATCH 4/4] mda: support v2 inboxes Eric Wong (Contractor, The Linux Foundation)
  3 siblings, 0 replies; 5+ messages in thread
From: Eric Wong (Contractor, The Linux Foundation) @ 2018-03-29 20:17 UTC (permalink / raw)
  To: meta

Having multiple Xapian partitions is mostly pointless after
the initial import.  We can compact all the partitions into
one while keeping the skeleton separate.
---
 Documentation/public-inbox-compact.pod | 50 ++++++++++++++++++
 MANIFEST                               |  3 ++
 script/public-inbox-compact            | 94 ++++++++++++++++++++++++++++++++++
 t/convert-compact.t                    | 57 +++++++++++++++++++++
 4 files changed, 204 insertions(+)
 create mode 100644 Documentation/public-inbox-compact.pod
 create mode 100755 script/public-inbox-compact
 create mode 100644 t/convert-compact.t

diff --git a/Documentation/public-inbox-compact.pod b/Documentation/public-inbox-compact.pod
new file mode 100644
index 0000000..4a519ce
--- /dev/null
+++ b/Documentation/public-inbox-compact.pod
@@ -0,0 +1,50 @@
+=head1 NAME
+
+public-inbox-compact - compact Xapian DBs
+
+=head1 SYNOPSIS
+
+	public-inbox-compact INBOX_DIR
+
+=head1 DESCRIPTION
+
+public-inbox-compact is a wrapper for L<xapian-compact(1)>
+designed for "v2" inboxes.  It combines multiple Xapian
+partitions into one to reduce space overhead after an initial
+mass import (using multiple partitions) is done.
+
+It locks the inbox and prevents other processes such as
+L<public-inbox-watch(1)> from writing while it operates.
+
+It also supports "v1" (ssoma) inboxes with limited
+usefulness over L<xapian-compact(1)>
+
+=head1 ENVIRONMENT
+
+=over 8
+
+=item PI_CONFIG
+
+The default config file, normally "~/.public-inbox/config".
+See L<public-inbox-config(5)>
+
+=back
+
+=head1 UPGRADING
+
+=head1 CONTACT
+
+Feedback welcome via plain-text mail to L<mailto:meta@public-inbox.org>
+
+The mail archives are hosted at L<https://public-inbox.org/meta/>
+and L<http://hjrcffqmbrq6wope.onion/meta/>
+
+=head1 COPYRIGHT
+
+Copyright 2018 all contributors L<mailto:meta@public-inbox.org>
+
+License: AGPL-3.0+ L<https://www.gnu.org/licenses/agpl-3.0.txt>
+
+=head1 SEE ALSO
+
+L<xapian-compact(1)>, L<public-inbox-index(1)>
diff --git a/MANIFEST b/MANIFEST
index 1e48d3a..ce6cd11 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -7,6 +7,7 @@ Documentation/design_notes.txt
 Documentation/design_www.txt
 Documentation/hosted.txt
 Documentation/include.mk
+Documentation/public-inbox-compact.pod
 Documentation/public-inbox-config.pod
 Documentation/public-inbox-convert.pod
 Documentation/public-inbox-daemon.pod
@@ -110,6 +111,7 @@ sa_config/Makefile
 sa_config/README
 sa_config/root/etc/spamassassin/public-inbox.pre
 sa_config/user/.spamassassin/user_prefs
+script/public-inbox-compact
 script/public-inbox-convert
 script/public-inbox-httpd
 script/public-inbox-index
@@ -137,6 +139,7 @@ t/common.perl
 t/config.t
 t/config_limiter.t
 t/content_id.t
+t/convert-compact.t
 t/emergency.t
 t/fail-bin/spamc
 t/feed.t
diff --git a/script/public-inbox-compact b/script/public-inbox-compact
new file mode 100755
index 0000000..016873d
--- /dev/null
+++ b/script/public-inbox-compact
@@ -0,0 +1,94 @@
+#!/usr/bin/perl -w
+# 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 Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
+use PublicInbox::V2Writable;
+use PublicInbox::Search;
+use PublicInbox::Config;
+use Cwd 'abs_path';
+use File::Temp qw(tempdir);
+use File::Path qw(remove_tree);
+use PublicInbox::Spawn qw(spawn);
+my $usage = "Usage: public-inbox-compact REPO_DIR\n";
+my $dir = shift or die $usage;
+my $config = PublicInbox::Config->new;
+my $ibx;
+$config->each_inbox(sub {
+	$ibx = $_[0] if abs_path($_[0]->{mainrepo}) eq $dir
+});
+unless ($ibx) {
+	warn "W: $dir not configured in ".
+		PublicInbox::Config::default_file() . "\n";
+	$ibx = {
+		mainrepo => $dir,
+		name => 'ignored',
+		address => [ 'old@example.com' ],
+	};
+	$ibx = PublicInbox::Inbox->new($ibx);
+}
+my $v = ($ibx->{version} || 1);
+if ($v == 2) {
+	require PublicInbox::V2Writable;
+	my $v2w = PublicInbox::V2Writable->new($ibx);
+	my $xap_v = 'xap'.PublicInbox::Search::SCHEMA_VERSION;
+	my $xroot = "$ibx->{mainrepo}/$xap_v";
+	opendir my $dh, $xroot or die "Failed to opendir $xroot: $!\n";
+	$v2w->lock_acquire;
+	my $new = tempdir(CLEANUP => 1, DIR => $ibx->{mainrepo});
+	my @parts;
+	my $skel;
+	while (defined(my $dn = readdir($dh))) {
+		if ($dn =~ /\A\d+\z/) {
+			push @parts, "$xroot/$dn";
+		} elsif ($dn eq 'skel') {
+			$skel = "$xroot/$dn";
+		} elsif ($dn eq '.' || $dn eq '..') {
+		} else {
+			warn "W: skipping unknown Xapian DB: $xroot/$dn\n";
+		}
+	}
+	close $dh;
+	my %pids;
+	if (@parts) {
+		my $pid = spawn([ qw(xapian-compact), @parts, "$new/0" ]);
+		defined $pid or die "compact failed: $?\n";
+		$pids{$pid} = 'xapian-compact (parts)';
+	} else {
+		warn "No parts found in $xroot\n";
+	}
+	if (defined $skel) {
+		my $pid = spawn([ qw(xapian-compact), $skel, "$new/skel" ]);
+		defined $pid or die "compact failed: $?\n";
+		$pids{$pid} = 'xapian-compact (skel)';
+	} else {
+		warn "$xroot/skel missing\n";
+	}
+	die "No xapian-compact processes running\n" unless scalar keys %pids;
+	while (scalar keys %pids) {
+		my $pid = waitpid(-1, 0);
+		my $desc = delete $pids{$pid};
+		die "$desc failed: $?\n" if $?;
+	}
+	rename($xroot, "$new/old") or die "rename $xroot => $new/old: $!\n";
+	rename($new, $xroot) or die "rename $new => $xroot: $!\n";
+	$v2w->lock_release;
+	remove_tree("$xroot/old") or die "failed to remove $xroot/old: $!\n";
+} elsif ($v == 1) {
+	require PublicInbox::Import;
+	my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
+	my $xap_v = 'xapian'.PublicInbox::Search::SCHEMA_VERSION;
+	my $v1_root = "$ibx->{mainrepo}/public-inbox";
+	my $old = "$v1_root/$xap_v";
+	-d $old or die "$old does not exist\n";
+	my $new = tempdir(CLEANUP => 1, DIR => $v1_root);
+	$im->lock_acquire;
+	PublicInbox::Import::run_die([ qw(xapian-compact), $old, $new ]);
+	rename($old, "$new/old") or die "rename $old => $new: $!\n";
+	rename($new, $old) or die "rename $new => $old: $!\n";
+	$im->lock_release;
+	remove_tree("$old/old") or die "failed to remove $old/old: $!\n";
+} else {
+	die "Unsupported inbox version: $v\n";
+}
diff --git a/t/convert-compact.t b/t/convert-compact.t
new file mode 100644
index 0000000..922ec9c
--- /dev/null
+++ b/t/convert-compact.t
@@ -0,0 +1,57 @@
+# 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 File::Temp qw/tempdir/;
+use PublicInbox::MIME;
+my @mods = qw(DBD::SQLite Search::Xapian);
+foreach my $mod (@mods) {
+	eval "require $mod";
+	plan skip_all => "$mod missing for convert-compact.t" if $@;
+}
+use PublicInbox::V2Writable;
+use PublicInbox::Import;
+my $tmpdir = tempdir('convert-compact-XXXXXX', TMPDIR => 1, CLEANUP => 1);
+my $ibx = {
+	mainrepo => "$tmpdir/v1",
+	name => 'test-v1',
+	-primary_address => 'test@example.com',
+};
+
+ok(PublicInbox::Import::run_die([qw(git init --bare -q), $ibx->{mainrepo}]),
+	'initialized v1 repo');
+$ibx = PublicInbox::Inbox->new($ibx);
+my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
+my $mime = PublicInbox::MIME->create(
+	header => [
+		From => 'a@example.com',
+		To => 'test@example.com',
+		Subject => 'this is a subject',
+		'Message-ID' => '<a-mid@b>',
+		Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
+	],
+	body => "hello world\n",
+);
+ok($im->add($mime), 'added one message');
+$im->done;
+PublicInbox::SearchIdx->new($ibx, 1)->index_sync;
+local $ENV{PATH} = "blib/script:$ENV{PATH}";
+open my $err, '>>', "$tmpdir/err.log" or die "open: err.log $!\n";
+open my $out, '>>', "$tmpdir/out.log" or die "open: out.log $!\n";
+my $rdr = { 1 => fileno($out), 2 => fileno($err) };
+
+my $cmd = [ 'public-inbox-compact', $ibx->{mainrepo} ];
+ok(PublicInbox::Import::run_die($cmd, undef, $rdr), 'v1 compact works');
+
+$cmd = [ 'public-inbox-convert', $ibx->{mainrepo}, "$tmpdir/v2" ];
+ok(PublicInbox::Import::run_die($cmd, undef, $rdr), 'convert works');
+
+$cmd = [ 'public-inbox-compact', "$tmpdir/v2" ];
+my $env = { NPROC => 2 };
+ok(PublicInbox::Import::run_die($cmd, $env, $rdr), 'v2 compact works');
+$ibx->{mainrepo} = "$tmpdir/v2";
+my $v2w = PublicInbox::V2Writable->new($ibx);
+is($v2w->{partitions}, 1, "only one partition in compacted repo");
+
+done_testing();
-- 
EW


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 4/4] mda: support v2 inboxes
  2018-03-29 20:17 [PATCH 0/4] tooling updates for v2 Eric Wong (Contractor, The Linux Foundation)
                   ` (2 preceding siblings ...)
  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)
  3 siblings, 0 replies; 5+ messages in thread
From: Eric Wong (Contractor, The Linux Foundation) @ 2018-03-29 20:17 UTC (permalink / raw)
  To: meta

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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-03-29 20:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH 4/4] mda: support v2 inboxes Eric Wong (Contractor, The Linux Foundation)

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).