+# slightly easier-to-manage manage than END{} blocks
+package PublicInbox::Xtmpdirs;
+use strict;
+use warnings;
+use File::Path qw(remove_tree);
+my %owner;
+
+sub new {
+ # http://www.tldp.org/LDP/abs/html/exitcodes.html
+ $SIG{INT} = sub { exit(130) };
+ $SIG{HUP} = $SIG{PIPE} = $SIG{TERM} = sub { exit(1) };
+ my $self = bless {}, $_[0]; # old partition => new (tmp) partition
+ $owner{"$self"} = $$;
+ $self;
+}
+
+sub done {
+ my ($self) = @_;
+ delete $owner{"$self"};
+ $SIG{INT} = $SIG{HUP} = $SIG{PIPE} = $SIG{TERM} = 'DEFAULT';
+ %$self = ();
+}
+
+sub DESTROY {
+ my ($self) = @_;
+ my $owner_pid = delete $owner{"$self"} or return;
+ return if $owner_pid != $$;
+ foreach my $new (values %$self) {
+ remove_tree($new) unless -d "$new/old";
+ }
+ $SIG{INT} = $SIG{HUP} = $SIG{PIPE} = $SIG{TERM} = 'DEFAULT';
+}
+