user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 8/9] emergency: implement new emergency Maildir delivery
Date: Wed, 15 Jun 2016 00:37:41 +0000	[thread overview]
Message-ID: <20160615003742.22538-9-e@80x24.org> (raw)
In-Reply-To: <20160615003742.22538-1-e@80x24.org>

This is transactional and hopefully safer in case we hit SIGSEGV
or SIGKILL during processing, as the tmp/ copy will remain on
the FS even if DESTROY/END handlers are not called.
---
 lib/PublicInbox/Emergency.pm | 96 ++++++++++++++++++++++++++++++++++++++++++++
 t/emergency.t                | 53 ++++++++++++++++++++++++
 2 files changed, 149 insertions(+)
 create mode 100644 lib/PublicInbox/Emergency.pm
 create mode 100644 t/emergency.t

diff --git a/lib/PublicInbox/Emergency.pm b/lib/PublicInbox/Emergency.pm
new file mode 100644
index 0000000..e402d30
--- /dev/null
+++ b/lib/PublicInbox/Emergency.pm
@@ -0,0 +1,96 @@
+# Copyright (C) 2016 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 warnings;
+use Fcntl qw(:DEFAULT SEEK_SET);
+use Sys::Hostname qw(hostname);
+use IO::Handle;
+
+sub new {
+	my ($class, $dir) = @_;
+
+	foreach (qw(new tmp cur)) {
+		my $d = "$dir/$_";
+		next if -d $d;
+		require File::Path;
+		File::Path::mkpath($d); # croaks on fatal errors
+	}
+	bless { dir => $dir, files => {}, t => 0, cnt => 0 }, $class;
+}
+
+sub _fn_in {
+	my ($self, $dir) = @_;
+	my @host = split(/\./, hostname);
+	my $now = time;
+	if ($self->{t} != $now) {
+		$self->{t} = $now;
+		$self->{cnt} = 0;
+	} else {
+		$self->{cnt}++;
+	}
+
+	my $f;
+	do {
+		$f = "$self->{dir}/$dir/$self->{t}.$$"."_$self->{cnt}.$host[0]";
+		$self->{cnt}++;
+	} while (-e $f);
+	$f;
+}
+
+sub prepare {
+	my ($self, $strref) = @_;
+
+	die "already in transaction: $self->{tmp}" if $self->{tmp};
+	my ($tmp, $fh);
+	do {
+		$tmp = _fn_in($self, 'tmp');
+		$! = undef;
+	} while (!sysopen($fh, $tmp, O_CREAT|O_EXCL|O_RDWR) && $!{EEXIST});
+	print $fh $$strref or die "write failed: $!";
+	$fh->flush or die "flush failed: $!";
+	$fh->autoflush(1);
+	$self->{fh} = $fh;
+	$self->{tmp} = $tmp;
+}
+
+sub abort {
+	my ($self) = @_;
+	delete $self->{fh};
+	my $tmp = delete $self->{tmp} or return;
+
+	unlink($tmp) or warn "Failed to 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: $!";
+	$fh;
+}
+
+sub commit {
+	my ($self) = @_;
+
+	delete $self->{fh};
+	my $tmp = delete $self->{tmp} or return;
+	my $new;
+	do {
+		$new = _fn_in($self, 'new');
+	} while (!link($tmp, $new) && $!{EEXIST});
+	my @sn = stat($new) or die "stat $new failed: $!";
+	my @st = stat($tmp) or die "stat $tmp failed: $!";
+	if ($st[0] == $sn[0] && $st[1] == $sn[1]) {
+		unlink($tmp) or warn "Failed to unlink $tmp: $!";
+	} else {
+		warn "stat($new) and stat($tmp) differ";
+	}
+}
+
+sub DESTROY { commit($_[0]) }
+
+1;
diff --git a/t/emergency.t b/t/emergency.t
new file mode 100644
index 0000000..e480338
--- /dev/null
+++ b/t/emergency.t
@@ -0,0 +1,53 @@
+# Copyright (C) 2016 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/;
+my $tmpdir = tempdir('emergency-XXXXXX', TMPDIR => 1, CLEANUP => 1);
+use_ok 'PublicInbox::Emergency';
+
+{
+	my $md = "$tmpdir/a";
+	my $em = PublicInbox::Emergency->new($md);
+	ok(-d $md, 'Maildir a auto-created');
+	my @tmp = <$md/tmp/*>;
+	is(scalar @tmp, 0, 'no temporary files exist, yet');
+	$em->prepare(\"BLAH");
+	@tmp = <$md/tmp/*>;
+	is(scalar @tmp, 1, 'globbed one temporary file');
+	open my $fh, '<', $tmp[0] or die "failed to open: $!";
+	is("BLAH", <$fh>, 'wrote contents to temporary location');
+	my @new = <$md/new/*>;
+	is(scalar @new, 0, 'no new files exist, yet');
+	$em = undef;
+	@tmp = <$md/tmp/*>;
+	is(scalar @tmp, 0, 'temporary file no longer exists');
+	@new = <$md/new/*>;
+	is(scalar @new, 1, 'globbed one new file');
+	open $fh, '<', $new[0] or die "failed to open: $!";
+	is("BLAH", <$fh>, 'wrote contents to new location');
+}
+{
+	my $md = "$tmpdir/b";
+	my $em = PublicInbox::Emergency->new($md);
+	ok(-d $md, 'Maildir b auto-created');
+	my @tmp = <$md/tmp/*>;
+	is(scalar @tmp, 0, 'no temporary files exist, yet');
+	$em->prepare(\"BLAH");
+	@tmp = <$md/tmp/*>;
+	is(scalar @tmp, 1, 'globbed one temporary file');
+	open my $fh, '<', $tmp[0] or die "failed to open: $!";
+	is("BLAH", <$fh>, 'wrote contents to temporary location');
+	my @new = <$md/new/*>;
+	is(scalar @new, 0, 'no new files exist, yet');
+	is(sysread($em->fh, my $buf, 9), 4, 'read file handle exposed');
+	is($buf, 'BLAH', 'got expected data');
+	$em->abort;
+	@tmp = <$md/tmp/*>;
+	is(scalar @tmp, 0, 'temporary file no longer exists');
+	@new = <$md/new/*>;
+	is(scalar @new , 0, 'new file no longer exists');
+}
+
+done_testing();

  parent reply	other threads:[~2016-06-15  0:37 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-15  0:37 [PATCH 0/9] big mda filter changes Eric Wong
2016-06-15  0:37 ` [PATCH 1/9] drop dependency on File::Path::Expand Eric Wong
2016-06-15  0:37 ` [PATCH 2/9] t/feed.t: make IPC::Run usage optional Eric Wong
2016-06-15  0:37 ` [PATCH 3/9] learn: remove IPC::Run dependency Eric Wong
2016-06-15  0:37 ` [PATCH 4/9] t/mda.t: remove senseless use of Email::Filter Eric Wong
2016-06-15  0:37 ` [PATCH 5/9] t/mda: use only Maildir for testing Eric Wong
2016-06-15  0:37 ` [PATCH 6/9] mda: precheck no longer depends on Email::Filter Eric Wong
2016-06-15  0:37 ` [PATCH 7/9] filter: begin work on a new filter API Eric Wong
2016-06-15  0:37 ` Eric Wong [this message]
2016-06-15  0:37 ` [PATCH 9/9] mda: hook up new filter functionality Eric Wong

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=20160615003742.22538-9-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).