public-inbox.git  about / heads / tags
an "archives first" approach to mailing lists
blob 968d7d6ff7202958403c5817ee688984fba1414a 1883 bytes (raw)
$ git show HEAD:lib/PublicInbox/Emergency.pm	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
 
# 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 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) = @_;
	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->{-host} //= (split(/\./, hostname))[0] // 'localhost';
	my $now = time;
	my $n;
	if ($self->{t} != $now) {
		$self->{t} = $now;
		$n = $self->{cnt} = 0;
	} else {
		$n = ++$self->{cnt};
	}
	"$self->{dir}/$dir/$self->{t}.$pid"."_$n.$host";
}

sub prepare {
	my ($self, $strref) = @_;
	my $pid = $$;
	my $tmp_key = "tmp.$pid";
	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 "print: $!";
	$fh->flush or die "flush: $!";
	$self->{fh} = $fh;
	$self->{$tmp_key} = $tmp;
}

sub abort {
	my ($self) = @_;
	delete $self->{fh};
	my $tmp = delete $self->{"tmp.$$"} or return;
	unlink($tmp) or warn "W: unlink($tmp): $!";
	undef;
}

sub fh {
	my ($self) = @_;
	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;
}

sub commit {
	my ($self) = @_;
	my $pid = $$;
	my $tmp = delete $self->{"tmp.$pid"} or return;
	delete $self->{fh};
	my ($new, $ok);
	do {
		$new = _fn_in($self, $pid, 'new');
	} while (!($ok = link($tmp, $new)) && $! == EEXIST);
	die "link($tmp, $new): $!" unless $ok;
	unlink($tmp) or warn "W: unlink($tmp): $!";
}

sub DESTROY { commit($_[0]) }

1;

git clone https://public-inbox.org/public-inbox.git
git clone http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/public-inbox.git