-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTempdir.pm
186 lines (156 loc) · 4.22 KB
/
Tempdir.pm
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#
# Copyright (c) 2003-2021 Christian Jaeger, [email protected]
#
# This is free software, offered under either the same terms as perl 5
# or the terms of the Artistic License version 2 or the terms of the
# MIT License (Expat version). See the file COPYING.md that came
# bundled with this file.
#
=head1 NAME
Chj::IO::Tempdir
=head1 SYNOPSIS
=head1 DESCRIPTION
=head1 NOTE
This is alpha software! Read the status section in the package README
or on the L<website|http://functional-perl.org/>.
=cut
package Chj::IO::Tempdir;
@ISA = "Chj::IO::Dir";
require Chj::IO::Dir;
use strict;
use warnings;
use warnings FATAL => 'uninitialized';
use Carp;
use Errno qw(EEXIST EINTR);
use FP::Carp;
use overload
'""' => \&stringify,
'0+' => \&numify,
;
our $MAXTRIES = 10;
our $DEFAULT_AUTOCLEAN = 1;
# 0=no, 1=yes and warn if not present in DESTROY, 2=yes but don't warn.
# XX or just boolean?
my %meta;
sub numify {
my $self = shift;
my $class = ref $self;
bless $self,
"Chj::IO::Tempdir::FOOOOOOO"; # this is even how overload::StrVal works.
my $num = $self +0;
bless $self, $class;
$num
}
sub stringify {
my $self = shift;
$self->path
}
sub _Addslash {
@_ == 1 or fp_croak_arity 1;
my ($str) = @_;
$str =~ m|/$|s ? $str : $str . "/"
}
sub xtmpdir {
my $class = shift;
@_ <= 2 or fp_croak_arity "<= 2";
my ($opt_basepath, $opt_mask) = @_;
my $basepath = (
$opt_basepath // do {
$ENV{CHJ_TEMPDIR_BASEPATH}
or $ENV{CHJ_TEMPDIR}
? _Addslash($ENV{CHJ_TEMPDIR})
: "/tmp/"
}
);
my $mask
= defined($opt_mask)
? $opt_mask
: 0700; # 0777 would be the perl default
my $item;
my $n = $MAXTRIES;
TRY: {
$item = int(rand(999) * 1000 + rand(999));
my $path = "$basepath$item";
if (mkdir $path, $mask) {
my $self = $class->SUPER::new;
$self->set(path => $path, autoclean => $DEFAULT_AUTOCLEAN);
return $self;
} elsif ($! == EEXIST or $! == EINTR) {
if (--$n > 0) {
redo TRY;
} else {
croak "xtmpdir: too many attempts to create a "
. "tempfile starting with path '$basepath'";
}
} else {
croak "xtmpdir: could not create dir '$path': $!";
}
}
}
sub set {
my $self = shift;
%{ $meta{ pack "I", $self } } = @_ # XX does this delete old keys?
}
sub path {
my $self = shift;
my $key = pack "I", $self;
if (@_) {
($meta{$key}{path}) = @_
} else {
$meta{$key}{path};
}
}
sub autoclean {
my $self = shift;
if (@_) {
($meta{ pack "I", $self }{autoclean}) = @_;
} else {
$meta{ pack "I", $self }{autoclean}
}
}
sub xtmpfile {
my $self = shift;
my ($mode, $autoclean) = @_;
defined(my $path = $self->path)
or die "xtmpfile: can't create tmpfile inside undefined dir";
require Chj::IO::Tempfile;
my $ret = Chj::IO::Tempfile->xtmpfile($path . "/", $mode, $autoclean);
$ret->attribute("parent_dir_obj", $self);
$ret
}
# useful for when other code creates files in it (not tmpfiles): "rm -rf"
sub rmrf {
my $s = shift;
require Chj::Shelllike::Rmrf;
Chj::Shelllike::Rmrf::Rmrf($s->path);
# to avoid warning, and since recreation later on should be
# understood as independent process anyway, ok?:
$s->autoclean(0)
}
sub push_on_destruction {
my $self = shift;
@_ == 1 or die;
my $key = pack "I", $self;
my ($handler) = @_;
push @{ $meta{$key}{on_destruction} }, $handler
}
sub DESTROY {
my $self = shift;
#warn "DESTROY $self";
local ($@, $!, $?, $_);
my $str = pack "I", $self;
if (my $arr = $meta{$str}{on_destruction}) {
&$_($self) for @$arr
}
if (my $autoclean = $meta{$str}{autoclean}) {
rmdir $meta{$str}{path} or do {
warn "autoclean: could not remove dir '$meta{$str}{path}': $!"
unless $autoclean
and $autoclean == 2
# XX how to do right order with cleaning contained tmpfiles?
};
}
delete $meta{$str};
#warn "/DESTROY $self";
}
1;