about summary refs log tree commit homepage
path: root/lib/PublicInbox/SharedKV.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2021-01-13 19:06:20 -1200
committerEric Wong <e@80x24.org>2021-01-14 23:14:08 +0000
commit8591e28bd9acfb44bd07d9bf75c055dfe3edf1a7 (patch)
tree59b1cf8063b4f93d8415f7dbebc445ddf93660c7 /lib/PublicInbox/SharedKV.pm
parent14055cedd9fc6ec593528a243ff52b6684df70c0 (diff)
downloadpublic-inbox-8591e28bd9acfb44bd07d9bf75c055dfe3edf1a7.tar.gz
We'll be passing these objects via PublicInbox::IPC which uses
Storable (or Sereal), so ensure they're safe to use after
serialization.
Diffstat (limited to 'lib/PublicInbox/SharedKV.pm')
-rw-r--r--lib/PublicInbox/SharedKV.pm12
1 files changed, 9 insertions, 3 deletions
diff --git a/lib/PublicInbox/SharedKV.pm b/lib/PublicInbox/SharedKV.pm
index d75d8998..072c94ca 100644
--- a/lib/PublicInbox/SharedKV.pm
+++ b/lib/PublicInbox/SharedKV.pm
@@ -8,9 +8,10 @@ package PublicInbox::SharedKV;
 use strict;
 use v5.10.1;
 use parent qw(PublicInbox::Lock);
-use File::Temp 0.19 (); # 0.19 for ->newdir
+use File::Temp qw(tempdir);
 use DBI ();
 use PublicInbox::Spawn;
+use File::Path qw(rmtree);
 
 sub dbh {
         my ($self, $lock) = @_;
@@ -44,8 +45,8 @@ sub new {
         my ($cls, $dir, $base, $opt) = @_;
         my $self = bless { opt => $opt }, $cls;
         unless (defined $dir) {
-                $self->{tmp} = File::Temp->newdir('kv-XXXXXX', TMPDIR => 1);
-                $dir = $self->{tmp}->dirname;
+                $self->{tmpdir} = $dir = tempdir('skv-XXXXXX', TMPDIR => 1);
+                $self->{tmpid} = "$$.$self";
         }
         -d $dir or mkdir($dir) or die "mkdir($dir): $!";
         $base //= '';
@@ -145,4 +146,9 @@ SELECT COUNT(k) FROM kv
         $sth->fetchrow_array;
 }
 
+sub DESTROY {
+        my ($self) = @_;
+        rmtree($self->{tmpdir}) if ($self->{tmpid} // '') eq "$$.$self";
+}
+
 1;