]> Sergey Matveev's repositories - public-inbox.git/commitdiff
inboxwritable: add assert_usable_dir sub
authorEric Wong <e@80x24.org>
Mon, 28 Oct 2019 10:45:24 +0000 (10:45 +0000)
committerEric Wong <e@80x24.org>
Wed, 30 Oct 2019 08:48:19 +0000 (08:48 +0000)
And use it for mda, since "0" could be a usable directory
if somebody insists on using relative paths...

lib/PublicInbox/InboxWritable.pm
lib/PublicInbox/V2Writable.pm
script/public-inbox-mda
t/import.t
t/v2writable.t

index ab7b0ed5a7ba3fa82e62de4bcbe6704af70a0c3c..9eab394d2d4072e88ece8c864e36bf08b39e4e9d 100644 (file)
@@ -30,12 +30,19 @@ sub new {
        $self;
 }
 
+sub assert_usable_dir {
+       my ($self) = @_;
+       my $dir = $self->{inboxdir};
+       return $dir if defined($dir) && $dir ne '';
+       die "no inboxdir defined for $self->{name}\n";
+}
+
 sub init_inbox {
        my ($self, $shards, $skip_epoch, $skip_artnum) = @_;
        # TODO: honor skip_artnum
        my $v = $self->{version} || 1;
        if ($v == 1) {
-               my $dir = $self->{inboxdir} or die "no inboxdir in inbox\n";
+               my $dir = assert_usable_dir($self);
                PublicInbox::Import::init_bare($dir);
        } else {
                my $v2w = importer($self);
index ad2e8e62309ab18d3a1979435d8ed539ffa7fe78..1825da2cf69f6ed2dc87ab8ea15b9bb1343e3308 100644 (file)
@@ -77,7 +77,8 @@ sub new {
        # $creat may be any true value, or 0/undef.  A hashref is true,
        # and $creat->{nproc} may be set to an integer
        my ($class, $v2ibx, $creat) = @_;
-       my $dir = $v2ibx->{inboxdir} or die "no inboxdir in inbox\n";
+       $v2ibx = PublicInbox::InboxWritable->new($v2ibx);
+       my $dir = $v2ibx->assert_usable_dir;
        unless (-d $dir) {
                if ($creat) {
                        require File::Path;
@@ -86,8 +87,6 @@ sub new {
                        die "$dir does not exist\n";
                }
        }
-
-       $v2ibx = PublicInbox::InboxWritable->new($v2ibx);
        $v2ibx->umask_prepare;
 
        my $xpfx = "$dir/xap" . PublicInbox::Search::SCHEMA_VERSION;
index 6935461662e6e900adddfa569d427272df097a24..c122984f49bdb06dc3e64d6c208e6517ce06f8f2 100755 (executable)
@@ -49,8 +49,10 @@ if (!defined $dst) {
        }
        defined $dst or do_exit(67); # EX_NOUSER 5.1.1 user unknown
 }
-$dst->{inboxdir} or do_exit(67);
+
 $dst = PublicInbox::InboxWritable->new($dst);
+eval { $dst->assert_usable_dir };
+do_exit(67) if $@;
 
 # pre-check, MDA has stricter rules than an importer might;
 if ($precheck && !PublicInbox::MDA->precheck($simple, $dst->{address})) {
index 4ec3c4f32e2b060a5e22956e734bc6bd4482de08..d309eec52de1bb63cbc9739960f2bd7abd4e6718 100644 (file)
@@ -96,4 +96,12 @@ is(undef, $im->checkpoint, 'checkpoint works before ->done');
 $im->done;
 is(undef, $im->checkpoint, 'checkpoint works after ->done');
 $im->checkpoint;
+
+my $nogit = PublicInbox::Git->new("$dir/non-existent/dir");
+eval {
+       my $nope = PublicInbox::Import->new($nogit, 'nope', 'no@example.com');
+       $nope->add($mime);
+};
+ok($@, 'Import->add fails on non-existent dir');
+
 done_testing();
index c2daac2f8484b4feb72ea752948af6cf9ac2cd2f..06dafe98e1ed65fdbd2fd7337ecc6c5bd34f25de 100644 (file)
@@ -260,4 +260,16 @@ EOF
        $im->done;
 }
 
+my $tmp = {
+       inboxdir => "$inboxdir/non-existent/subdir",
+       name => 'nope',
+       version => 2,
+       -primary_address => 'test@example.com',
+};
+eval {
+       my $nope = PublicInbox::V2Writable->new($tmp);
+       $nope->add($mime);
+};
+ok($@, 'V2Writable fails on non-existent dir');
+
 done_testing();