about summary refs log tree commit homepage
path: root/lib/PublicInbox/Emergency.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/PublicInbox/Emergency.pm')
-rw-r--r--lib/PublicInbox/Emergency.pm34
1 files changed, 13 insertions, 21 deletions
diff --git a/lib/PublicInbox/Emergency.pm b/lib/PublicInbox/Emergency.pm
index 5a1ed1d7..968d7d6f 100644
--- a/lib/PublicInbox/Emergency.pm
+++ b/lib/PublicInbox/Emergency.pm
@@ -1,32 +1,24 @@
-# Copyright (C) 2016-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # Emergency Maildir delivery for MDA
 package PublicInbox::Emergency;
-use strict;
-use v5.10.1;
+use v5.12;
 use Fcntl qw(:DEFAULT SEEK_SET);
 use Sys::Hostname qw(hostname);
 use IO::Handle; # ->flush
 use Errno qw(EEXIST);
+use File::Path ();
 
 sub new {
         my ($class, $dir) = @_;
-
-        foreach (qw(new tmp cur)) {
-                my $d = "$dir/$_";
-                next if -d $d;
-                require File::Path;
-                if (!File::Path::mkpath($d) && !-d $d) {
-                        die "failed to mkpath($d): $!\n";
-                }
-        }
+        File::Path::make_path(map { $dir.$_ } qw(/tmp /new /cur));
         bless { dir => $dir, t => 0 }, $class;
 }
 
 sub _fn_in {
         my ($self, $pid, $dir) = @_;
-        my $host = $self->{short_host} //= (split(/\./, hostname))[0];
+        my $host = $self->{-host} //= (split(/\./, hostname))[0] // 'localhost';
         my $now = time;
         my $n;
         if ($self->{t} != $now) {
@@ -42,14 +34,14 @@ sub prepare {
         my ($self, $strref) = @_;
         my $pid = $$;
         my $tmp_key = "tmp.$pid";
-        die "already in transaction: $self->{$tmp_key}" if $self->{$tmp_key};
+        die "BUG: in transaction: $self->{$tmp_key}" if $self->{$tmp_key};
         my ($tmp, $fh);
         do {
                 $tmp = _fn_in($self, $pid, 'tmp');
                 $! = undef;
         } while (!sysopen($fh, $tmp, O_CREAT|O_EXCL|O_RDWR) and $! == EEXIST);
-        print $fh $$strref or die "write failed: $!";
-        $fh->flush or die "flush failed: $!";
+        print $fh $$strref or die "print: $!";
+        $fh->flush or die "flush: $!";
         $self->{fh} = $fh;
         $self->{$tmp_key} = $tmp;
 }
@@ -58,15 +50,15 @@ sub abort {
         my ($self) = @_;
         delete $self->{fh};
         my $tmp = delete $self->{"tmp.$$"} or return;
-        unlink($tmp) or warn "Failed to unlink $tmp: $!";
+        unlink($tmp) or warn "W: unlink($tmp): $!";
         undef;
 }
 
 sub fh {
         my ($self) = @_;
-        my $fh = $self->{fh} or die "{fh} not open!\n";
-        seek($fh, 0, SEEK_SET) or die "seek(fh) failed: $!";
-        sysseek($fh, 0, SEEK_SET) or die "sysseek(fh) failed: $!";
+        my $fh = $self->{fh} or die "BUG: {fh} not open";
+        seek($fh, 0, SEEK_SET) or die "seek: $!";
+        sysseek($fh, 0, SEEK_SET) or die "sysseek: $!";
         $fh;
 }
 
@@ -80,7 +72,7 @@ sub commit {
                 $new = _fn_in($self, $pid, 'new');
         } while (!($ok = link($tmp, $new)) && $! == EEXIST);
         die "link($tmp, $new): $!" unless $ok;
-        unlink($tmp) or warn "Failed to unlink $tmp: $!";
+        unlink($tmp) or warn "W: unlink($tmp): $!";
 }
 
 sub DESTROY { commit($_[0]) }