line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package File::Temp; # git description: v0.2310-3-gc7148fe |
2
|
|
|
|
|
|
|
# ABSTRACT: return name and handle of a temporary file safely |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
our $VERSION = '0.2311'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
#pod =begin :__INTERNALS |
7
|
|
|
|
|
|
|
#pod |
8
|
|
|
|
|
|
|
#pod =head1 PORTABILITY |
9
|
|
|
|
|
|
|
#pod |
10
|
|
|
|
|
|
|
#pod This section is at the top in order to provide easier access to |
11
|
|
|
|
|
|
|
#pod porters. It is not expected to be rendered by a standard pod |
12
|
|
|
|
|
|
|
#pod formatting tool. Please skip straight to the SYNOPSIS section if you |
13
|
|
|
|
|
|
|
#pod are not trying to port this module to a new platform. |
14
|
|
|
|
|
|
|
#pod |
15
|
|
|
|
|
|
|
#pod This module is designed to be portable across operating systems and it |
16
|
|
|
|
|
|
|
#pod currently supports Unix, VMS, DOS, OS/2, Windows and Mac OS |
17
|
|
|
|
|
|
|
#pod (Classic). When porting to a new OS there are generally three main |
18
|
|
|
|
|
|
|
#pod issues that have to be solved: |
19
|
|
|
|
|
|
|
#pod |
20
|
|
|
|
|
|
|
#pod =over 4 |
21
|
|
|
|
|
|
|
#pod |
22
|
|
|
|
|
|
|
#pod =item * |
23
|
|
|
|
|
|
|
#pod |
24
|
|
|
|
|
|
|
#pod Can the OS unlink an open file? If it can not then the |
25
|
|
|
|
|
|
|
#pod C<_can_unlink_opened_file> method should be modified. |
26
|
|
|
|
|
|
|
#pod |
27
|
|
|
|
|
|
|
#pod =item * |
28
|
|
|
|
|
|
|
#pod |
29
|
|
|
|
|
|
|
#pod Are the return values from C reliable? By default all the |
30
|
|
|
|
|
|
|
#pod return values from C are compared when unlinking a temporary |
31
|
|
|
|
|
|
|
#pod file using the filename and the handle. Operating systems other than |
32
|
|
|
|
|
|
|
#pod unix do not always have valid entries in all fields. If utility function |
33
|
|
|
|
|
|
|
#pod C fails then the C comparison should be |
34
|
|
|
|
|
|
|
#pod modified accordingly. |
35
|
|
|
|
|
|
|
#pod |
36
|
|
|
|
|
|
|
#pod =item * |
37
|
|
|
|
|
|
|
#pod |
38
|
|
|
|
|
|
|
#pod Security. Systems that can not support a test for the sticky bit |
39
|
|
|
|
|
|
|
#pod on a directory can not use the MEDIUM and HIGH security tests. |
40
|
|
|
|
|
|
|
#pod The C<_can_do_level> method should be modified accordingly. |
41
|
|
|
|
|
|
|
#pod |
42
|
|
|
|
|
|
|
#pod =back |
43
|
|
|
|
|
|
|
#pod |
44
|
|
|
|
|
|
|
#pod =end :__INTERNALS |
45
|
|
|
|
|
|
|
#pod |
46
|
|
|
|
|
|
|
#pod =head1 SYNOPSIS |
47
|
|
|
|
|
|
|
#pod |
48
|
|
|
|
|
|
|
#pod use File::Temp qw/ tempfile tempdir /; |
49
|
|
|
|
|
|
|
#pod |
50
|
|
|
|
|
|
|
#pod $fh = tempfile(); |
51
|
|
|
|
|
|
|
#pod ($fh, $filename) = tempfile(); |
52
|
|
|
|
|
|
|
#pod |
53
|
|
|
|
|
|
|
#pod ($fh, $filename) = tempfile( $template, DIR => $dir); |
54
|
|
|
|
|
|
|
#pod ($fh, $filename) = tempfile( $template, SUFFIX => '.dat'); |
55
|
|
|
|
|
|
|
#pod ($fh, $filename) = tempfile( $template, TMPDIR => 1 ); |
56
|
|
|
|
|
|
|
#pod |
57
|
|
|
|
|
|
|
#pod binmode( $fh, ":utf8" ); |
58
|
|
|
|
|
|
|
#pod |
59
|
|
|
|
|
|
|
#pod $dir = tempdir( CLEANUP => 1 ); |
60
|
|
|
|
|
|
|
#pod ($fh, $filename) = tempfile( DIR => $dir ); |
61
|
|
|
|
|
|
|
#pod |
62
|
|
|
|
|
|
|
#pod Object interface: |
63
|
|
|
|
|
|
|
#pod |
64
|
|
|
|
|
|
|
#pod require File::Temp; |
65
|
|
|
|
|
|
|
#pod use File::Temp (); |
66
|
|
|
|
|
|
|
#pod use File::Temp qw/ :seekable /; |
67
|
|
|
|
|
|
|
#pod |
68
|
|
|
|
|
|
|
#pod $fh = File::Temp->new(); |
69
|
|
|
|
|
|
|
#pod $fname = $fh->filename; |
70
|
|
|
|
|
|
|
#pod |
71
|
|
|
|
|
|
|
#pod $fh = File::Temp->new(TEMPLATE => $template); |
72
|
|
|
|
|
|
|
#pod $fname = $fh->filename; |
73
|
|
|
|
|
|
|
#pod |
74
|
|
|
|
|
|
|
#pod $tmp = File::Temp->new( UNLINK => 0, SUFFIX => '.dat' ); |
75
|
|
|
|
|
|
|
#pod print $tmp "Some data\n"; |
76
|
|
|
|
|
|
|
#pod print "Filename is $tmp\n"; |
77
|
|
|
|
|
|
|
#pod $tmp->seek( 0, SEEK_END ); |
78
|
|
|
|
|
|
|
#pod |
79
|
|
|
|
|
|
|
#pod $dir = File::Temp->newdir(); # CLEANUP => 1 by default |
80
|
|
|
|
|
|
|
#pod |
81
|
|
|
|
|
|
|
#pod The following interfaces are provided for compatibility with |
82
|
|
|
|
|
|
|
#pod existing APIs. They should not be used in new code. |
83
|
|
|
|
|
|
|
#pod |
84
|
|
|
|
|
|
|
#pod MkTemp family: |
85
|
|
|
|
|
|
|
#pod |
86
|
|
|
|
|
|
|
#pod use File::Temp qw/ :mktemp /; |
87
|
|
|
|
|
|
|
#pod |
88
|
|
|
|
|
|
|
#pod ($fh, $file) = mkstemp( "tmpfileXXXXX" ); |
89
|
|
|
|
|
|
|
#pod ($fh, $file) = mkstemps( "tmpfileXXXXXX", $suffix); |
90
|
|
|
|
|
|
|
#pod |
91
|
|
|
|
|
|
|
#pod $tmpdir = mkdtemp( $template ); |
92
|
|
|
|
|
|
|
#pod |
93
|
|
|
|
|
|
|
#pod $unopened_file = mktemp( $template ); |
94
|
|
|
|
|
|
|
#pod |
95
|
|
|
|
|
|
|
#pod POSIX functions: |
96
|
|
|
|
|
|
|
#pod |
97
|
|
|
|
|
|
|
#pod use File::Temp qw/ :POSIX /; |
98
|
|
|
|
|
|
|
#pod |
99
|
|
|
|
|
|
|
#pod $file = tmpnam(); |
100
|
|
|
|
|
|
|
#pod $fh = tmpfile(); |
101
|
|
|
|
|
|
|
#pod |
102
|
|
|
|
|
|
|
#pod ($fh, $file) = tmpnam(); |
103
|
|
|
|
|
|
|
#pod |
104
|
|
|
|
|
|
|
#pod Compatibility functions: |
105
|
|
|
|
|
|
|
#pod |
106
|
|
|
|
|
|
|
#pod $unopened_file = File::Temp::tempnam( $dir, $pfx ); |
107
|
|
|
|
|
|
|
#pod |
108
|
|
|
|
|
|
|
#pod =head1 DESCRIPTION |
109
|
|
|
|
|
|
|
#pod |
110
|
|
|
|
|
|
|
#pod C can be used to create and open temporary files in a safe |
111
|
|
|
|
|
|
|
#pod way. There is both a function interface and an object-oriented |
112
|
|
|
|
|
|
|
#pod interface. The File::Temp constructor or the tempfile() function can |
113
|
|
|
|
|
|
|
#pod be used to return the name and the open filehandle of a temporary |
114
|
|
|
|
|
|
|
#pod file. The tempdir() function can be used to create a temporary |
115
|
|
|
|
|
|
|
#pod directory. |
116
|
|
|
|
|
|
|
#pod |
117
|
|
|
|
|
|
|
#pod The security aspect of temporary file creation is emphasized such that |
118
|
|
|
|
|
|
|
#pod a filehandle and filename are returned together. This helps guarantee |
119
|
|
|
|
|
|
|
#pod that a race condition can not occur where the temporary file is |
120
|
|
|
|
|
|
|
#pod created by another process between checking for the existence of the |
121
|
|
|
|
|
|
|
#pod file and its opening. Additional security levels are provided to |
122
|
|
|
|
|
|
|
#pod check, for example, that the sticky bit is set on world writable |
123
|
|
|
|
|
|
|
#pod directories. See L<"safe_level"> for more information. |
124
|
|
|
|
|
|
|
#pod |
125
|
|
|
|
|
|
|
#pod For compatibility with popular C library functions, Perl implementations of |
126
|
|
|
|
|
|
|
#pod the mkstemp() family of functions are provided. These are, mkstemp(), |
127
|
|
|
|
|
|
|
#pod mkstemps(), mkdtemp() and mktemp(). |
128
|
|
|
|
|
|
|
#pod |
129
|
|
|
|
|
|
|
#pod Additionally, implementations of the standard L |
130
|
|
|
|
|
|
|
#pod tmpnam() and tmpfile() functions are provided if required. |
131
|
|
|
|
|
|
|
#pod |
132
|
|
|
|
|
|
|
#pod Implementations of mktemp(), tmpnam(), and tempnam() are provided, |
133
|
|
|
|
|
|
|
#pod but should be used with caution since they return only a filename |
134
|
|
|
|
|
|
|
#pod that was valid when function was called, so cannot guarantee |
135
|
|
|
|
|
|
|
#pod that the file will not exist by the time the caller opens the filename. |
136
|
|
|
|
|
|
|
#pod |
137
|
|
|
|
|
|
|
#pod Filehandles returned by these functions support the seekable methods. |
138
|
|
|
|
|
|
|
#pod |
139
|
|
|
|
|
|
|
#pod =cut |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
# Toolchain targets v5.8.1, but we'll try to support back to v5.6 anyway. |
142
|
|
|
|
|
|
|
# It might be possible to make this v5.5, but many v5.6isms are creeping |
143
|
|
|
|
|
|
|
# into the code and tests. |
144
|
13
|
|
|
13
|
|
492225
|
use 5.006; |
|
13
|
|
|
|
|
143
|
|
145
|
13
|
|
|
13
|
|
82
|
use strict; |
|
13
|
|
|
|
|
25
|
|
|
13
|
|
|
|
|
334
|
|
146
|
13
|
|
|
13
|
|
60
|
use Carp; |
|
13
|
|
|
|
|
71
|
|
|
13
|
|
|
|
|
1164
|
|
147
|
13
|
|
|
13
|
|
81
|
use File::Spec 0.8; |
|
13
|
|
|
|
|
304
|
|
|
13
|
|
|
|
|
340
|
|
148
|
13
|
|
|
13
|
|
68
|
use Cwd (); |
|
13
|
|
|
|
|
23
|
|
|
13
|
|
|
|
|
358
|
|
149
|
13
|
|
|
13
|
|
66
|
use File::Path 2.06 qw/ rmtree /; |
|
13
|
|
|
|
|
228
|
|
|
13
|
|
|
|
|
908
|
|
150
|
13
|
|
|
13
|
|
86
|
use Fcntl 1.03; |
|
13
|
|
|
|
|
267
|
|
|
13
|
|
|
|
|
2953
|
|
151
|
13
|
|
|
13
|
|
5672
|
use IO::Seekable; # For SEEK_* |
|
13
|
|
|
|
|
86103
|
|
|
13
|
|
|
|
|
650
|
|
152
|
13
|
|
|
13
|
|
5423
|
use Errno; |
|
13
|
|
|
|
|
16454
|
|
|
13
|
|
|
|
|
618
|
|
153
|
13
|
|
|
13
|
|
87
|
use Scalar::Util 'refaddr'; |
|
13
|
|
|
|
|
23
|
|
|
13
|
|
|
|
|
1623
|
|
154
|
|
|
|
|
|
|
require VMS::Stdio if $^O eq 'VMS'; |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
# pre-emptively load Carp::Heavy. If we don't when we run out of file |
157
|
|
|
|
|
|
|
# handles and attempt to call croak() we get an error message telling |
158
|
|
|
|
|
|
|
# us that Carp::Heavy won't load rather than an error telling us we |
159
|
|
|
|
|
|
|
# have run out of file handles. We either preload croak() or we |
160
|
|
|
|
|
|
|
# switch the calls to croak from _gettemp() to use die. |
161
|
|
|
|
|
|
|
eval { require Carp::Heavy; }; |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
# Need the Symbol package if we are running older perl |
164
|
|
|
|
|
|
|
require Symbol if $] < 5.006; |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
### For the OO interface |
167
|
13
|
|
|
13
|
|
5133
|
use parent 0.221 qw/ IO::Handle IO::Seekable /; |
|
13
|
|
|
|
|
4006
|
|
|
13
|
|
|
|
|
71
|
|
168
|
13
|
|
|
|
|
77
|
use overload '""' => "STRINGIFY", '0+' => "NUMIFY", |
169
|
13
|
|
|
13
|
|
15224
|
fallback => 1; |
|
13
|
|
|
|
|
20319
|
|
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
our $DEBUG = 0; |
172
|
|
|
|
|
|
|
our $KEEP_ALL = 0; |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
# We are exporting functions |
175
|
|
|
|
|
|
|
|
176
|
13
|
|
|
13
|
|
1457
|
use Exporter 5.57 'import'; # 5.57 lets us import 'import' |
|
13
|
|
|
|
|
237
|
|
|
13
|
|
|
|
|
1561
|
|
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
# Export list - to allow fine tuning of export table |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
our @EXPORT_OK = qw{ |
181
|
|
|
|
|
|
|
tempfile |
182
|
|
|
|
|
|
|
tempdir |
183
|
|
|
|
|
|
|
tmpnam |
184
|
|
|
|
|
|
|
tmpfile |
185
|
|
|
|
|
|
|
mktemp |
186
|
|
|
|
|
|
|
mkstemp |
187
|
|
|
|
|
|
|
mkstemps |
188
|
|
|
|
|
|
|
mkdtemp |
189
|
|
|
|
|
|
|
unlink0 |
190
|
|
|
|
|
|
|
cleanup |
191
|
|
|
|
|
|
|
SEEK_SET |
192
|
|
|
|
|
|
|
SEEK_CUR |
193
|
|
|
|
|
|
|
SEEK_END |
194
|
|
|
|
|
|
|
}; |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
# Groups of functions for export |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( |
199
|
|
|
|
|
|
|
'POSIX' => [qw/ tmpnam tmpfile /], |
200
|
|
|
|
|
|
|
'mktemp' => [qw/ mktemp mkstemp mkstemps mkdtemp/], |
201
|
|
|
|
|
|
|
'seekable' => [qw/ SEEK_SET SEEK_CUR SEEK_END /], |
202
|
|
|
|
|
|
|
); |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
# add contents of these tags to @EXPORT |
205
|
|
|
|
|
|
|
Exporter::export_tags('POSIX','mktemp','seekable'); |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
# This is a list of characters that can be used in random filenames |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
my @CHARS = (qw/ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z |
210
|
|
|
|
|
|
|
a b c d e f g h i j k l m n o p q r s t u v w x y z |
211
|
|
|
|
|
|
|
0 1 2 3 4 5 6 7 8 9 _ |
212
|
|
|
|
|
|
|
/); |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
# Maximum number of tries to make a temp file before failing |
215
|
|
|
|
|
|
|
|
216
|
13
|
|
|
13
|
|
89
|
use constant MAX_TRIES => 1000; |
|
13
|
|
|
|
|
22
|
|
|
13
|
|
|
|
|
1167
|
|
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
# Minimum number of X characters that should be in a template |
219
|
13
|
|
|
13
|
|
87
|
use constant MINX => 4; |
|
13
|
|
|
|
|
36
|
|
|
13
|
|
|
|
|
1026
|
|
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
# Default template when no template supplied |
222
|
|
|
|
|
|
|
|
223
|
13
|
|
|
13
|
|
115
|
use constant TEMPXXX => 'X' x 10; |
|
13
|
|
|
|
|
23
|
|
|
13
|
|
|
|
|
667
|
|
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
# Constants for the security level |
226
|
|
|
|
|
|
|
|
227
|
13
|
|
|
13
|
|
67
|
use constant STANDARD => 0; |
|
13
|
|
|
|
|
23
|
|
|
13
|
|
|
|
|
523
|
|
228
|
13
|
|
|
13
|
|
67
|
use constant MEDIUM => 1; |
|
13
|
|
|
|
|
24
|
|
|
13
|
|
|
|
|
513
|
|
229
|
13
|
|
|
13
|
|
65
|
use constant HIGH => 2; |
|
13
|
|
|
|
|
25
|
|
|
13
|
|
|
|
|
1055
|
|
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
# OPENFLAGS. If we defined the flag to use with Sysopen here this gives |
232
|
|
|
|
|
|
|
# us an optimisation when many temporary files are requested |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
my $OPENFLAGS = O_CREAT | O_EXCL | O_RDWR; |
235
|
|
|
|
|
|
|
my $LOCKFLAG; |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
unless ($^O eq 'MacOS') { |
238
|
|
|
|
|
|
|
for my $oflag (qw/ NOFOLLOW BINARY LARGEFILE NOINHERIT /) { |
239
|
|
|
|
|
|
|
my ($bit, $func) = (0, "Fcntl::O_" . $oflag); |
240
|
13
|
|
|
13
|
|
76
|
no strict 'refs'; |
|
13
|
|
|
|
|
25
|
|
|
13
|
|
|
|
|
2695
|
|
241
|
|
|
|
|
|
|
$OPENFLAGS |= $bit if eval { |
242
|
|
|
|
|
|
|
# Make sure that redefined die handlers do not cause problems |
243
|
|
|
|
|
|
|
# e.g. CGI::Carp |
244
|
|
|
|
|
|
|
local $SIG{__DIE__} = sub {}; |
245
|
|
|
|
|
|
|
local $SIG{__WARN__} = sub {}; |
246
|
|
|
|
|
|
|
$bit = &$func(); |
247
|
|
|
|
|
|
|
1; |
248
|
|
|
|
|
|
|
}; |
249
|
|
|
|
|
|
|
} |
250
|
|
|
|
|
|
|
# Special case O_EXLOCK |
251
|
|
|
|
|
|
|
$LOCKFLAG = eval { |
252
|
|
|
|
|
|
|
local $SIG{__DIE__} = sub {}; |
253
|
|
|
|
|
|
|
local $SIG{__WARN__} = sub {}; |
254
|
|
|
|
|
|
|
&Fcntl::O_EXLOCK(); |
255
|
|
|
|
|
|
|
}; |
256
|
|
|
|
|
|
|
} |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
# On some systems the O_TEMPORARY flag can be used to tell the OS |
259
|
|
|
|
|
|
|
# to automatically remove the file when it is closed. This is fine |
260
|
|
|
|
|
|
|
# in most cases but not if tempfile is called with UNLINK=>0 and |
261
|
|
|
|
|
|
|
# the filename is requested -- in the case where the filename is to |
262
|
|
|
|
|
|
|
# be passed to another routine. This happens on windows. We overcome |
263
|
|
|
|
|
|
|
# this by using a second open flags variable |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
my $OPENTEMPFLAGS = $OPENFLAGS; |
266
|
|
|
|
|
|
|
unless ($^O eq 'MacOS') { |
267
|
|
|
|
|
|
|
for my $oflag (qw/ TEMPORARY /) { |
268
|
|
|
|
|
|
|
my ($bit, $func) = (0, "Fcntl::O_" . $oflag); |
269
|
|
|
|
|
|
|
local($@); |
270
|
13
|
|
|
13
|
|
94
|
no strict 'refs'; |
|
13
|
|
|
|
|
18
|
|
|
13
|
|
|
|
|
9577
|
|
271
|
|
|
|
|
|
|
$OPENTEMPFLAGS |= $bit if eval { |
272
|
|
|
|
|
|
|
# Make sure that redefined die handlers do not cause problems |
273
|
|
|
|
|
|
|
# e.g. CGI::Carp |
274
|
|
|
|
|
|
|
local $SIG{__DIE__} = sub {}; |
275
|
|
|
|
|
|
|
local $SIG{__WARN__} = sub {}; |
276
|
|
|
|
|
|
|
$bit = &$func(); |
277
|
|
|
|
|
|
|
1; |
278
|
|
|
|
|
|
|
}; |
279
|
|
|
|
|
|
|
} |
280
|
|
|
|
|
|
|
} |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
# Private hash tracking which files have been created by each process id via the OO interface |
283
|
|
|
|
|
|
|
my %FILES_CREATED_BY_OBJECT; |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
# INTERNAL ROUTINES - not to be used outside of package |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
# Generic routine for getting a temporary filename |
288
|
|
|
|
|
|
|
# modelled on OpenBSD _gettemp() in mktemp.c |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
# The template must contain X's that are to be replaced |
291
|
|
|
|
|
|
|
# with the random values |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
# Arguments: |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
# TEMPLATE - string containing the XXXXX's that is converted |
296
|
|
|
|
|
|
|
# to a random filename and opened if required |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
# Optionally, a hash can also be supplied containing specific options |
299
|
|
|
|
|
|
|
# "open" => if true open the temp file, else just return the name |
300
|
|
|
|
|
|
|
# default is 0 |
301
|
|
|
|
|
|
|
# "mkdir"=> if true, we are creating a temp directory rather than tempfile |
302
|
|
|
|
|
|
|
# default is 0 |
303
|
|
|
|
|
|
|
# "suffixlen" => number of characters at end of PATH to be ignored. |
304
|
|
|
|
|
|
|
# default is 0. |
305
|
|
|
|
|
|
|
# "unlink_on_close" => indicates that, if possible, the OS should remove |
306
|
|
|
|
|
|
|
# the file as soon as it is closed. Usually indicates |
307
|
|
|
|
|
|
|
# use of the O_TEMPORARY flag to sysopen. |
308
|
|
|
|
|
|
|
# Usually irrelevant on unix |
309
|
|
|
|
|
|
|
# "use_exlock" => Indicates that O_EXLOCK should be used. Default is false. |
310
|
|
|
|
|
|
|
# "file_permissions" => file permissions for sysopen(). Default is 0600. |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
# Optionally a reference to a scalar can be passed into the function |
313
|
|
|
|
|
|
|
# On error this will be used to store the reason for the error |
314
|
|
|
|
|
|
|
# "ErrStr" => \$errstr |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
# "open" and "mkdir" can not both be true |
317
|
|
|
|
|
|
|
# "unlink_on_close" is not used when "mkdir" is true. |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
# The default options are equivalent to mktemp(). |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
# Returns: |
322
|
|
|
|
|
|
|
# filehandle - open file handle (if called with doopen=1, else undef) |
323
|
|
|
|
|
|
|
# temp name - name of the temp file or directory |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
# For example: |
326
|
|
|
|
|
|
|
# ($fh, $name) = _gettemp($template, "open" => 1); |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
# for the current version, failures are associated with |
329
|
|
|
|
|
|
|
# stored in an error string and returned to give the reason whilst debugging |
330
|
|
|
|
|
|
|
# This routine is not called by any external function |
331
|
|
|
|
|
|
|
sub _gettemp { |
332
|
|
|
|
|
|
|
|
333
|
46
|
50
|
|
46
|
|
124
|
croak 'Usage: ($fh, $name) = _gettemp($template, OPTIONS);' |
334
|
|
|
|
|
|
|
unless scalar(@_) >= 1; |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
# the internal error string - expect it to be overridden |
337
|
|
|
|
|
|
|
# Need this in case the caller decides not to supply us a value |
338
|
|
|
|
|
|
|
# need an anonymous scalar |
339
|
46
|
|
|
|
|
66
|
my $tempErrStr; |
340
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
# Default options |
342
|
46
|
|
|
|
|
321
|
my %options = ( |
343
|
|
|
|
|
|
|
"open" => 0, |
344
|
|
|
|
|
|
|
"mkdir" => 0, |
345
|
|
|
|
|
|
|
"suffixlen" => 0, |
346
|
|
|
|
|
|
|
"unlink_on_close" => 0, |
347
|
|
|
|
|
|
|
"use_exlock" => 0, |
348
|
|
|
|
|
|
|
"ErrStr" => \$tempErrStr, |
349
|
|
|
|
|
|
|
"file_permissions" => undef, |
350
|
|
|
|
|
|
|
); |
351
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
# Read the template |
353
|
46
|
|
|
|
|
87
|
my $template = shift; |
354
|
46
|
50
|
|
|
|
105
|
if (ref($template)) { |
355
|
|
|
|
|
|
|
# Use a warning here since we have not yet merged ErrStr |
356
|
0
|
|
|
|
|
0
|
carp "File::Temp::_gettemp: template must not be a reference"; |
357
|
0
|
|
|
|
|
0
|
return (); |
358
|
|
|
|
|
|
|
} |
359
|
|
|
|
|
|
|
|
360
|
|
|
|
|
|
|
# Check that the number of entries on stack are even |
361
|
46
|
50
|
|
|
|
124
|
if (scalar(@_) % 2 != 0) { |
362
|
|
|
|
|
|
|
# Use a warning here since we have not yet merged ErrStr |
363
|
0
|
|
|
|
|
0
|
carp "File::Temp::_gettemp: Must have even number of options"; |
364
|
0
|
|
|
|
|
0
|
return (); |
365
|
|
|
|
|
|
|
} |
366
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
# Read the options and merge with defaults |
368
|
46
|
50
|
|
|
|
343
|
%options = (%options, @_) if @_; |
369
|
|
|
|
|
|
|
|
370
|
|
|
|
|
|
|
# Make sure the error string is set to undef |
371
|
46
|
|
|
|
|
87
|
${$options{ErrStr}} = undef; |
|
46
|
|
|
|
|
101
|
|
372
|
|
|
|
|
|
|
|
373
|
|
|
|
|
|
|
# Can not open the file and make a directory in a single call |
374
|
46
|
50
|
66
|
|
|
217
|
if ($options{"open"} && $options{"mkdir"}) { |
375
|
0
|
|
|
|
|
0
|
${$options{ErrStr}} = "doopen and domkdir can not both be true\n"; |
|
0
|
|
|
|
|
0
|
|
376
|
0
|
|
|
|
|
0
|
return (); |
377
|
|
|
|
|
|
|
} |
378
|
|
|
|
|
|
|
|
379
|
|
|
|
|
|
|
# Find the start of the end of the Xs (position of last X) |
380
|
|
|
|
|
|
|
# Substr starts from 0 |
381
|
46
|
|
|
|
|
162
|
my $start = length($template) - 1 - $options{"suffixlen"}; |
382
|
|
|
|
|
|
|
|
383
|
|
|
|
|
|
|
# Check that we have at least MINX x X (e.g. 'XXXX") at the end of the string |
384
|
|
|
|
|
|
|
# (taking suffixlen into account). Any fewer is insecure. |
385
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
# Do it using substr - no reason to use a pattern match since |
387
|
|
|
|
|
|
|
# we know where we are looking and what we are looking for |
388
|
|
|
|
|
|
|
|
389
|
46
|
50
|
|
|
|
143
|
if (substr($template, $start - MINX + 1, MINX) ne 'X' x MINX) { |
390
|
0
|
|
|
|
|
0
|
${$options{ErrStr}} = "The template must end with at least ". |
|
0
|
|
|
|
|
0
|
|
391
|
|
|
|
|
|
|
MINX . " 'X' characters\n"; |
392
|
0
|
|
|
|
|
0
|
return (); |
393
|
|
|
|
|
|
|
} |
394
|
|
|
|
|
|
|
|
395
|
|
|
|
|
|
|
# Replace all the X at the end of the substring with a |
396
|
|
|
|
|
|
|
# random character or just all the XX at the end of a full string. |
397
|
|
|
|
|
|
|
# Do it as an if, since the suffix adjusts which section to replace |
398
|
|
|
|
|
|
|
# and suffixlen=0 returns nothing if used in the substr directly |
399
|
|
|
|
|
|
|
# and generate a full path from the template |
400
|
|
|
|
|
|
|
|
401
|
46
|
|
|
|
|
110
|
my $path = _replace_XX($template, $options{"suffixlen"}); |
402
|
|
|
|
|
|
|
|
403
|
|
|
|
|
|
|
|
404
|
|
|
|
|
|
|
# Split the path into constituent parts - eventually we need to check |
405
|
|
|
|
|
|
|
# whether the directory exists |
406
|
|
|
|
|
|
|
# We need to know whether we are making a temp directory |
407
|
|
|
|
|
|
|
# or a tempfile |
408
|
|
|
|
|
|
|
|
409
|
46
|
|
|
|
|
121
|
my ($volume, $directories, $file); |
410
|
46
|
|
|
|
|
0
|
my $parent; # parent directory |
411
|
46
|
100
|
|
|
|
120
|
if ($options{"mkdir"}) { |
412
|
|
|
|
|
|
|
# There is no filename at the end |
413
|
10
|
|
|
|
|
68
|
($volume, $directories, $file) = File::Spec->splitpath( $path, 1); |
414
|
|
|
|
|
|
|
|
415
|
|
|
|
|
|
|
# The parent is then $directories without the last directory |
416
|
|
|
|
|
|
|
# Split the directory and put it back together again |
417
|
10
|
|
|
|
|
61
|
my @dirs = File::Spec->splitdir($directories); |
418
|
|
|
|
|
|
|
|
419
|
|
|
|
|
|
|
# If @dirs only has one entry (i.e. the directory template) that means |
420
|
|
|
|
|
|
|
# we are in the current directory |
421
|
10
|
100
|
|
|
|
28
|
if ($#dirs == 0) { |
422
|
5
|
|
|
|
|
15
|
$parent = File::Spec->curdir; |
423
|
|
|
|
|
|
|
} else { |
424
|
|
|
|
|
|
|
|
425
|
5
|
50
|
|
|
|
18
|
if ($^O eq 'VMS') { # need volume to avoid relative dir spec |
426
|
0
|
|
|
|
|
0
|
$parent = File::Spec->catdir($volume, @dirs[0..$#dirs-1]); |
427
|
0
|
0
|
|
|
|
0
|
$parent = 'sys$disk:[]' if $parent eq ''; |
428
|
|
|
|
|
|
|
} else { |
429
|
|
|
|
|
|
|
|
430
|
|
|
|
|
|
|
# Put it back together without the last one |
431
|
5
|
|
|
|
|
39
|
$parent = File::Spec->catdir(@dirs[0..$#dirs-1]); |
432
|
|
|
|
|
|
|
|
433
|
|
|
|
|
|
|
# ...and attach the volume (no filename) |
434
|
5
|
|
|
|
|
36
|
$parent = File::Spec->catpath($volume, $parent, ''); |
435
|
|
|
|
|
|
|
} |
436
|
|
|
|
|
|
|
|
437
|
|
|
|
|
|
|
} |
438
|
|
|
|
|
|
|
|
439
|
|
|
|
|
|
|
} else { |
440
|
|
|
|
|
|
|
|
441
|
|
|
|
|
|
|
# Get rid of the last filename (use File::Basename for this?) |
442
|
36
|
|
|
|
|
804
|
($volume, $directories, $file) = File::Spec->splitpath( $path ); |
443
|
|
|
|
|
|
|
|
444
|
|
|
|
|
|
|
# Join up without the file part |
445
|
36
|
|
|
|
|
380
|
$parent = File::Spec->catpath($volume,$directories,''); |
446
|
|
|
|
|
|
|
|
447
|
|
|
|
|
|
|
# If $parent is empty replace with curdir |
448
|
36
|
100
|
|
|
|
135
|
$parent = File::Spec->curdir |
449
|
|
|
|
|
|
|
unless $directories ne ''; |
450
|
|
|
|
|
|
|
|
451
|
|
|
|
|
|
|
} |
452
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
# Check that the parent directories exist |
454
|
|
|
|
|
|
|
# Do this even for the case where we are simply returning a name |
455
|
|
|
|
|
|
|
# not a file -- no point returning a name that includes a directory |
456
|
|
|
|
|
|
|
# that does not exist or is not writable |
457
|
|
|
|
|
|
|
|
458
|
46
|
50
|
|
|
|
806
|
unless (-e $parent) { |
459
|
0
|
|
|
|
|
0
|
${$options{ErrStr}} = "Parent directory ($parent) does not exist"; |
|
0
|
|
|
|
|
0
|
|
460
|
0
|
|
|
|
|
0
|
return (); |
461
|
|
|
|
|
|
|
} |
462
|
46
|
50
|
|
|
|
492
|
unless (-d $parent) { |
463
|
0
|
|
|
|
|
0
|
${$options{ErrStr}} = "Parent directory ($parent) is not a directory"; |
|
0
|
|
|
|
|
0
|
|
464
|
0
|
|
|
|
|
0
|
return (); |
465
|
|
|
|
|
|
|
} |
466
|
|
|
|
|
|
|
|
467
|
|
|
|
|
|
|
# Check the stickiness of the directory and chown giveaway if required |
468
|
|
|
|
|
|
|
# If the directory is world writable the sticky bit |
469
|
|
|
|
|
|
|
# must be set |
470
|
|
|
|
|
|
|
|
471
|
46
|
100
|
|
|
|
260
|
if (File::Temp->safe_level == MEDIUM) { |
|
|
100
|
|
|
|
|
|
472
|
1
|
|
|
|
|
2
|
my $safeerr; |
473
|
1
|
50
|
|
|
|
3
|
unless (_is_safe($parent,\$safeerr)) { |
474
|
0
|
|
|
|
|
0
|
${$options{ErrStr}} = "Parent directory ($parent) is not safe ($safeerr)"; |
|
0
|
|
|
|
|
0
|
|
475
|
0
|
|
|
|
|
0
|
return (); |
476
|
|
|
|
|
|
|
} |
477
|
|
|
|
|
|
|
} elsif (File::Temp->safe_level == HIGH) { |
478
|
1
|
|
|
|
|
2
|
my $safeerr; |
479
|
1
|
50
|
|
|
|
4
|
unless (_is_verysafe($parent, \$safeerr)) { |
480
|
0
|
|
|
|
|
0
|
${$options{ErrStr}} = "Parent directory ($parent) is not safe ($safeerr)"; |
|
0
|
|
|
|
|
0
|
|
481
|
0
|
|
|
|
|
0
|
return (); |
482
|
|
|
|
|
|
|
} |
483
|
|
|
|
|
|
|
} |
484
|
|
|
|
|
|
|
|
485
|
46
|
|
|
|
|
89
|
my $perms = $options{file_permissions}; |
486
|
46
|
|
|
|
|
79
|
my $has_perms = defined $perms; |
487
|
46
|
100
|
|
|
|
109
|
$perms = 0600 unless $has_perms; |
488
|
|
|
|
|
|
|
|
489
|
|
|
|
|
|
|
# Now try MAX_TRIES time to open the file |
490
|
46
|
|
|
|
|
153
|
for (my $i = 0; $i < MAX_TRIES; $i++) { |
491
|
|
|
|
|
|
|
|
492
|
|
|
|
|
|
|
# Try to open the file if requested |
493
|
46
|
100
|
|
|
|
97
|
if ($options{"open"}) { |
|
|
100
|
|
|
|
|
|
494
|
34
|
|
|
|
|
43
|
my $fh; |
495
|
|
|
|
|
|
|
|
496
|
|
|
|
|
|
|
# If we are running before perl5.6.0 we can not auto-vivify |
497
|
34
|
50
|
|
|
|
85
|
if ($] < 5.006) { |
498
|
0
|
|
|
|
|
0
|
$fh = &Symbol::gensym; |
499
|
|
|
|
|
|
|
} |
500
|
|
|
|
|
|
|
|
501
|
|
|
|
|
|
|
# Try to make sure this will be marked close-on-exec |
502
|
|
|
|
|
|
|
# XXX: Win32 doesn't respect this, nor the proper fcntl, |
503
|
|
|
|
|
|
|
# but may have O_NOINHERIT. This may or may not be in Fcntl. |
504
|
34
|
|
|
|
|
172
|
local $^F = 2; |
505
|
|
|
|
|
|
|
|
506
|
|
|
|
|
|
|
# Attempt to open the file |
507
|
34
|
|
|
|
|
65
|
my $open_success = undef; |
508
|
34
|
50
|
0
|
|
|
119
|
if ( $^O eq 'VMS' and $options{"unlink_on_close"} && !$KEEP_ALL) { |
|
|
|
33
|
|
|
|
|
509
|
|
|
|
|
|
|
# make it auto delete on close by setting FAB$V_DLT bit |
510
|
0
|
|
|
|
|
0
|
$fh = VMS::Stdio::vmssysopen($path, $OPENFLAGS, $perms, 'fop=dlt'); |
511
|
0
|
|
|
|
|
0
|
$open_success = $fh; |
512
|
|
|
|
|
|
|
} else { |
513
|
34
|
100
|
66
|
|
|
119
|
my $flags = ( ($options{"unlink_on_close"} && !$KEEP_ALL) ? |
514
|
|
|
|
|
|
|
$OPENTEMPFLAGS : |
515
|
|
|
|
|
|
|
$OPENFLAGS ); |
516
|
34
|
0
|
33
|
|
|
72
|
$flags |= $LOCKFLAG if (defined $LOCKFLAG && $options{use_exlock}); |
517
|
34
|
|
|
|
|
3496
|
$open_success = sysopen($fh, $path, $flags, $perms); |
518
|
|
|
|
|
|
|
} |
519
|
34
|
50
|
|
|
|
213
|
if ( $open_success ) { |
520
|
|
|
|
|
|
|
|
521
|
|
|
|
|
|
|
# in case of odd umask force rw |
522
|
34
|
100
|
|
|
|
703
|
chmod($perms, $path) unless $has_perms; |
523
|
|
|
|
|
|
|
|
524
|
|
|
|
|
|
|
# Opened successfully - return file handle and name |
525
|
34
|
|
|
|
|
332
|
return ($fh, $path); |
526
|
|
|
|
|
|
|
|
527
|
|
|
|
|
|
|
} else { |
528
|
|
|
|
|
|
|
|
529
|
|
|
|
|
|
|
# Error opening file - abort with error |
530
|
|
|
|
|
|
|
# if the reason was anything but EEXIST |
531
|
0
|
0
|
|
|
|
0
|
unless ($!{EEXIST}) { |
532
|
0
|
|
|
|
|
0
|
${$options{ErrStr}} = "Could not create temp file $path: $!"; |
|
0
|
|
|
|
|
0
|
|
533
|
0
|
|
|
|
|
0
|
return (); |
534
|
|
|
|
|
|
|
} |
535
|
|
|
|
|
|
|
|
536
|
|
|
|
|
|
|
# Loop round for another try |
537
|
|
|
|
|
|
|
|
538
|
|
|
|
|
|
|
} |
539
|
|
|
|
|
|
|
} elsif ($options{"mkdir"}) { |
540
|
|
|
|
|
|
|
|
541
|
|
|
|
|
|
|
# Open the temp directory |
542
|
10
|
50
|
|
|
|
605
|
if (mkdir( $path, 0700)) { |
543
|
|
|
|
|
|
|
# in case of odd umask |
544
|
10
|
|
|
|
|
142
|
chmod(0700, $path); |
545
|
|
|
|
|
|
|
|
546
|
10
|
|
|
|
|
77
|
return undef, $path; |
547
|
|
|
|
|
|
|
} else { |
548
|
|
|
|
|
|
|
|
549
|
|
|
|
|
|
|
# Abort with error if the reason for failure was anything |
550
|
|
|
|
|
|
|
# except EEXIST |
551
|
0
|
0
|
|
|
|
0
|
unless ($!{EEXIST}) { |
552
|
0
|
|
|
|
|
0
|
${$options{ErrStr}} = "Could not create directory $path: $!"; |
|
0
|
|
|
|
|
0
|
|
553
|
0
|
|
|
|
|
0
|
return (); |
554
|
|
|
|
|
|
|
} |
555
|
|
|
|
|
|
|
|
556
|
|
|
|
|
|
|
# Loop round for another try |
557
|
|
|
|
|
|
|
|
558
|
|
|
|
|
|
|
} |
559
|
|
|
|
|
|
|
|
560
|
|
|
|
|
|
|
} else { |
561
|
|
|
|
|
|
|
|
562
|
|
|
|
|
|
|
# Return true if the file can not be found |
563
|
|
|
|
|
|
|
# Directory has been checked previously |
564
|
|
|
|
|
|
|
|
565
|
2
|
50
|
|
|
|
115
|
return (undef, $path) unless -e $path; |
566
|
|
|
|
|
|
|
|
567
|
|
|
|
|
|
|
# Try again until MAX_TRIES |
568
|
|
|
|
|
|
|
|
569
|
|
|
|
|
|
|
} |
570
|
|
|
|
|
|
|
|
571
|
|
|
|
|
|
|
# Did not successfully open the tempfile/dir |
572
|
|
|
|
|
|
|
# so try again with a different set of random letters |
573
|
|
|
|
|
|
|
# No point in trying to increment unless we have only |
574
|
|
|
|
|
|
|
# 1 X say and the randomness could come up with the same |
575
|
|
|
|
|
|
|
# file MAX_TRIES in a row. |
576
|
|
|
|
|
|
|
|
577
|
|
|
|
|
|
|
# Store current attempt - in principle this implies that the |
578
|
|
|
|
|
|
|
# 3rd time around the open attempt that the first temp file |
579
|
|
|
|
|
|
|
# name could be generated again. Probably should store each |
580
|
|
|
|
|
|
|
# attempt and make sure that none are repeated |
581
|
|
|
|
|
|
|
|
582
|
0
|
|
|
|
|
0
|
my $original = $path; |
583
|
0
|
|
|
|
|
0
|
my $counter = 0; # Stop infinite loop |
584
|
0
|
|
|
|
|
0
|
my $MAX_GUESS = 50; |
585
|
|
|
|
|
|
|
|
586
|
0
|
|
0
|
|
|
0
|
do { |
587
|
|
|
|
|
|
|
|
588
|
|
|
|
|
|
|
# Generate new name from original template |
589
|
0
|
|
|
|
|
0
|
$path = _replace_XX($template, $options{"suffixlen"}); |
590
|
|
|
|
|
|
|
|
591
|
0
|
|
|
|
|
0
|
$counter++; |
592
|
|
|
|
|
|
|
|
593
|
|
|
|
|
|
|
} until ($path ne $original || $counter > $MAX_GUESS); |
594
|
|
|
|
|
|
|
|
595
|
|
|
|
|
|
|
# Check for out of control looping |
596
|
0
|
0
|
|
|
|
0
|
if ($counter > $MAX_GUESS) { |
597
|
0
|
|
|
|
|
0
|
${$options{ErrStr}} = "Tried to get a new temp name different to the previous value $MAX_GUESS times.\nSomething wrong with template?? ($template)"; |
|
0
|
|
|
|
|
0
|
|
598
|
0
|
|
|
|
|
0
|
return (); |
599
|
|
|
|
|
|
|
} |
600
|
|
|
|
|
|
|
|
601
|
|
|
|
|
|
|
} |
602
|
|
|
|
|
|
|
|
603
|
|
|
|
|
|
|
# If we get here, we have run out of tries |
604
|
0
|
|
|
|
|
0
|
${ $options{ErrStr} } = "Have exceeded the maximum number of attempts (" |
|
0
|
|
|
|
|
0
|
|
605
|
|
|
|
|
|
|
. MAX_TRIES . ") to open temp file/dir"; |
606
|
|
|
|
|
|
|
|
607
|
0
|
|
|
|
|
0
|
return (); |
608
|
|
|
|
|
|
|
|
609
|
|
|
|
|
|
|
} |
610
|
|
|
|
|
|
|
|
611
|
|
|
|
|
|
|
# Internal routine to replace the XXXX... with random characters |
612
|
|
|
|
|
|
|
# This has to be done by _gettemp() every time it fails to |
613
|
|
|
|
|
|
|
# open a temp file/dir |
614
|
|
|
|
|
|
|
|
615
|
|
|
|
|
|
|
# Arguments: $template (the template with XXX), |
616
|
|
|
|
|
|
|
# $ignore (number of characters at end to ignore) |
617
|
|
|
|
|
|
|
|
618
|
|
|
|
|
|
|
# Returns: modified template |
619
|
|
|
|
|
|
|
|
620
|
|
|
|
|
|
|
sub _replace_XX { |
621
|
|
|
|
|
|
|
|
622
|
46
|
50
|
|
46
|
|
148
|
croak 'Usage: _replace_XX($template, $ignore)' |
623
|
|
|
|
|
|
|
unless scalar(@_) == 2; |
624
|
|
|
|
|
|
|
|
625
|
46
|
|
|
|
|
119
|
my ($path, $ignore) = @_; |
626
|
|
|
|
|
|
|
|
627
|
|
|
|
|
|
|
# Do it as an if, since the suffix adjusts which section to replace |
628
|
|
|
|
|
|
|
# and suffixlen=0 returns nothing if used in the substr directly |
629
|
|
|
|
|
|
|
# Alternatively, could simply set $ignore to length($path)-1 |
630
|
|
|
|
|
|
|
# Don't want to always use substr when not required though. |
631
|
46
|
50
|
|
|
|
114
|
my $end = ( $] >= 5.006 ? "\\z" : "\\Z" ); |
632
|
|
|
|
|
|
|
|
633
|
46
|
100
|
|
|
|
355
|
if ($ignore) { |
634
|
9
|
|
|
|
|
122
|
substr($path, 0, - $ignore) =~ s/X(?=X*$end)/$CHARS[ int( rand( @CHARS ) ) ]/ge; |
|
74
|
|
|
|
|
275
|
|
635
|
|
|
|
|
|
|
} else { |
636
|
37
|
|
|
|
|
513
|
$path =~ s/X(?=X*$end)/$CHARS[ int( rand( @CHARS ) ) ]/ge; |
|
304
|
|
|
|
|
1444
|
|
637
|
|
|
|
|
|
|
} |
638
|
46
|
|
|
|
|
175
|
return $path; |
639
|
|
|
|
|
|
|
} |
640
|
|
|
|
|
|
|
|
641
|
|
|
|
|
|
|
# Internal routine to force a temp file to be writable after |
642
|
|
|
|
|
|
|
# it is created so that we can unlink it. Windows seems to occasionally |
643
|
|
|
|
|
|
|
# force a file to be readonly when written to certain temp locations |
644
|
|
|
|
|
|
|
sub _force_writable { |
645
|
21
|
|
|
21
|
|
48
|
my $file = shift; |
646
|
21
|
|
|
|
|
302
|
chmod 0600, $file; |
647
|
|
|
|
|
|
|
} |
648
|
|
|
|
|
|
|
|
649
|
|
|
|
|
|
|
|
650
|
|
|
|
|
|
|
# internal routine to check to see if the directory is safe |
651
|
|
|
|
|
|
|
# First checks to see if the directory is not owned by the |
652
|
|
|
|
|
|
|
# current user or root. Then checks to see if anyone else |
653
|
|
|
|
|
|
|
# can write to the directory and if so, checks to see if |
654
|
|
|
|
|
|
|
# it has the sticky bit set |
655
|
|
|
|
|
|
|
|
656
|
|
|
|
|
|
|
# Will not work on systems that do not support sticky bit |
657
|
|
|
|
|
|
|
|
658
|
|
|
|
|
|
|
#Args: directory path to check |
659
|
|
|
|
|
|
|
# Optionally: reference to scalar to contain error message |
660
|
|
|
|
|
|
|
# Returns true if the path is safe and false otherwise. |
661
|
|
|
|
|
|
|
# Returns undef if can not even run stat() on the path |
662
|
|
|
|
|
|
|
|
663
|
|
|
|
|
|
|
# This routine based on version written by Tom Christiansen |
664
|
|
|
|
|
|
|
|
665
|
|
|
|
|
|
|
# Presumably, by the time we actually attempt to create the |
666
|
|
|
|
|
|
|
# file or directory in this directory, it may not be safe |
667
|
|
|
|
|
|
|
# anymore... Have to run _is_safe directly after the open. |
668
|
|
|
|
|
|
|
|
669
|
|
|
|
|
|
|
sub _is_safe { |
670
|
|
|
|
|
|
|
|
671
|
2
|
|
|
2
|
|
4
|
my $path = shift; |
672
|
2
|
|
|
|
|
4
|
my $err_ref = shift; |
673
|
|
|
|
|
|
|
|
674
|
|
|
|
|
|
|
# Stat path |
675
|
2
|
|
|
|
|
23
|
my @info = stat($path); |
676
|
2
|
50
|
|
|
|
7
|
unless (scalar(@info)) { |
677
|
0
|
|
|
|
|
0
|
$$err_ref = "stat(path) returned no values"; |
678
|
0
|
|
|
|
|
0
|
return 0; |
679
|
|
|
|
|
|
|
} |
680
|
|
|
|
|
|
|
; |
681
|
2
|
50
|
|
|
|
7
|
return 1 if $^O eq 'VMS'; # owner delete control at file level |
682
|
|
|
|
|
|
|
|
683
|
|
|
|
|
|
|
# Check to see whether owner is neither superuser (or a system uid) nor me |
684
|
|
|
|
|
|
|
# Use the effective uid from the $> variable |
685
|
|
|
|
|
|
|
# UID is in [4] |
686
|
2
|
50
|
33
|
|
|
7
|
if ($info[4] > File::Temp->top_system_uid() && $info[4] != $>) { |
687
|
|
|
|
|
|
|
|
688
|
0
|
|
|
|
|
0
|
Carp::cluck(sprintf "uid=$info[4] topuid=%s euid=$> path='$path'", |
689
|
|
|
|
|
|
|
File::Temp->top_system_uid()); |
690
|
|
|
|
|
|
|
|
691
|
0
|
0
|
|
|
|
0
|
$$err_ref = "Directory owned neither by root nor the current user" |
692
|
|
|
|
|
|
|
if ref($err_ref); |
693
|
0
|
|
|
|
|
0
|
return 0; |
694
|
|
|
|
|
|
|
} |
695
|
|
|
|
|
|
|
|
696
|
|
|
|
|
|
|
# check whether group or other can write file |
697
|
|
|
|
|
|
|
# use 066 to detect either reading or writing |
698
|
|
|
|
|
|
|
# use 022 to check writability |
699
|
|
|
|
|
|
|
# Do it with S_IWOTH and S_IWGRP for portability (maybe) |
700
|
|
|
|
|
|
|
# mode is in info[2] |
701
|
2
|
50
|
33
|
|
|
10
|
if (($info[2] & &Fcntl::S_IWGRP) || # Is group writable? |
702
|
|
|
|
|
|
|
($info[2] & &Fcntl::S_IWOTH) ) { # Is world writable? |
703
|
|
|
|
|
|
|
# Must be a directory |
704
|
2
|
50
|
|
|
|
20
|
unless (-d $path) { |
705
|
0
|
0
|
|
|
|
0
|
$$err_ref = "Path ($path) is not a directory" |
706
|
|
|
|
|
|
|
if ref($err_ref); |
707
|
0
|
|
|
|
|
0
|
return 0; |
708
|
|
|
|
|
|
|
} |
709
|
|
|
|
|
|
|
# Must have sticky bit set |
710
|
2
|
50
|
|
|
|
19
|
unless (-k $path) { |
711
|
0
|
0
|
|
|
|
0
|
$$err_ref = "Sticky bit not set on $path when dir is group|world writable" |
712
|
|
|
|
|
|
|
if ref($err_ref); |
713
|
0
|
|
|
|
|
0
|
return 0; |
714
|
|
|
|
|
|
|
} |
715
|
|
|
|
|
|
|
} |
716
|
|
|
|
|
|
|
|
717
|
2
|
|
|
|
|
11
|
return 1; |
718
|
|
|
|
|
|
|
} |
719
|
|
|
|
|
|
|
|
720
|
|
|
|
|
|
|
# Internal routine to check whether a directory is safe |
721
|
|
|
|
|
|
|
# for temp files. Safer than _is_safe since it checks for |
722
|
|
|
|
|
|
|
# the possibility of chown giveaway and if that is a possibility |
723
|
|
|
|
|
|
|
# checks each directory in the path to see if it is safe (with _is_safe) |
724
|
|
|
|
|
|
|
|
725
|
|
|
|
|
|
|
# If _PC_CHOWN_RESTRICTED is not set, does the full test of each |
726
|
|
|
|
|
|
|
# directory anyway. |
727
|
|
|
|
|
|
|
|
728
|
|
|
|
|
|
|
# Takes optional second arg as scalar ref to error reason |
729
|
|
|
|
|
|
|
|
730
|
|
|
|
|
|
|
sub _is_verysafe { |
731
|
|
|
|
|
|
|
|
732
|
|
|
|
|
|
|
# Need POSIX - but only want to bother if really necessary due to overhead |
733
|
1
|
|
|
1
|
|
487
|
require POSIX; |
734
|
|
|
|
|
|
|
|
735
|
1
|
|
|
|
|
5249
|
my $path = shift; |
736
|
1
|
50
|
|
|
|
4
|
print "_is_verysafe testing $path\n" if $DEBUG; |
737
|
1
|
50
|
|
|
|
4
|
return 1 if $^O eq 'VMS'; # owner delete control at file level |
738
|
|
|
|
|
|
|
|
739
|
1
|
|
|
|
|
2
|
my $err_ref = shift; |
740
|
|
|
|
|
|
|
|
741
|
|
|
|
|
|
|
# Should Get the value of _PC_CHOWN_RESTRICTED if it is defined |
742
|
|
|
|
|
|
|
# and If it is not there do the extensive test |
743
|
1
|
|
|
|
|
2
|
local($@); |
744
|
1
|
|
|
|
|
6
|
my $chown_restricted; |
745
|
|
|
|
|
|
|
$chown_restricted = &POSIX::_PC_CHOWN_RESTRICTED() |
746
|
1
|
50
|
|
|
|
2
|
if eval { &POSIX::_PC_CHOWN_RESTRICTED(); 1}; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
4
|
|
747
|
|
|
|
|
|
|
|
748
|
|
|
|
|
|
|
# If chown_resticted is set to some value we should test it |
749
|
1
|
50
|
|
|
|
2
|
if (defined $chown_restricted) { |
750
|
|
|
|
|
|
|
|
751
|
|
|
|
|
|
|
# Return if the current directory is safe |
752
|
1
|
50
|
|
|
|
43
|
return _is_safe($path,$err_ref) if POSIX::sysconf( $chown_restricted ); |
753
|
|
|
|
|
|
|
|
754
|
|
|
|
|
|
|
} |
755
|
|
|
|
|
|
|
|
756
|
|
|
|
|
|
|
# To reach this point either, the _PC_CHOWN_RESTRICTED symbol |
757
|
|
|
|
|
|
|
# was not available or the symbol was there but chown giveaway |
758
|
|
|
|
|
|
|
# is allowed. Either way, we now have to test the entire tree for |
759
|
|
|
|
|
|
|
# safety. |
760
|
|
|
|
|
|
|
|
761
|
|
|
|
|
|
|
# Convert path to an absolute directory if required |
762
|
0
|
0
|
|
|
|
0
|
unless (File::Spec->file_name_is_absolute($path)) { |
763
|
0
|
|
|
|
|
0
|
$path = File::Spec->rel2abs($path); |
764
|
|
|
|
|
|
|
} |
765
|
|
|
|
|
|
|
|
766
|
|
|
|
|
|
|
# Split directory into components - assume no file |
767
|
0
|
|
|
|
|
0
|
my ($volume, $directories, undef) = File::Spec->splitpath( $path, 1); |
768
|
|
|
|
|
|
|
|
769
|
|
|
|
|
|
|
# Slightly less efficient than having a function in File::Spec |
770
|
|
|
|
|
|
|
# to chop off the end of a directory or even a function that |
771
|
|
|
|
|
|
|
# can handle ../ in a directory tree |
772
|
|
|
|
|
|
|
# Sometimes splitdir() returns a blank at the end |
773
|
|
|
|
|
|
|
# so we will probably check the bottom directory twice in some cases |
774
|
0
|
|
|
|
|
0
|
my @dirs = File::Spec->splitdir($directories); |
775
|
|
|
|
|
|
|
|
776
|
|
|
|
|
|
|
# Concatenate one less directory each time around |
777
|
0
|
|
|
|
|
0
|
foreach my $pos (0.. $#dirs) { |
778
|
|
|
|
|
|
|
# Get a directory name |
779
|
0
|
|
|
|
|
0
|
my $dir = File::Spec->catpath($volume, |
780
|
|
|
|
|
|
|
File::Spec->catdir(@dirs[0.. $#dirs - $pos]), |
781
|
|
|
|
|
|
|
'' |
782
|
|
|
|
|
|
|
); |
783
|
|
|
|
|
|
|
|
784
|
0
|
0
|
|
|
|
0
|
print "TESTING DIR $dir\n" if $DEBUG; |
785
|
|
|
|
|
|
|
|
786
|
|
|
|
|
|
|
# Check the directory |
787
|
0
|
0
|
|
|
|
0
|
return 0 unless _is_safe($dir,$err_ref); |
788
|
|
|
|
|
|
|
|
789
|
|
|
|
|
|
|
} |
790
|
|
|
|
|
|
|
|
791
|
0
|
|
|
|
|
0
|
return 1; |
792
|
|
|
|
|
|
|
} |
793
|
|
|
|
|
|
|
|
794
|
|
|
|
|
|
|
|
795
|
|
|
|
|
|
|
|
796
|
|
|
|
|
|
|
# internal routine to determine whether unlink works on this |
797
|
|
|
|
|
|
|
# platform for files that are currently open. |
798
|
|
|
|
|
|
|
# Returns true if we can, false otherwise. |
799
|
|
|
|
|
|
|
|
800
|
|
|
|
|
|
|
# Currently WinNT, OS/2 and VMS can not unlink an opened file |
801
|
|
|
|
|
|
|
# On VMS this is because the O_EXCL flag is used to open the |
802
|
|
|
|
|
|
|
# temporary file. Currently I do not know enough about the issues |
803
|
|
|
|
|
|
|
# on VMS to decide whether O_EXCL is a requirement. |
804
|
|
|
|
|
|
|
|
805
|
|
|
|
|
|
|
sub _can_unlink_opened_file { |
806
|
|
|
|
|
|
|
|
807
|
5
|
50
|
|
5
|
|
28
|
if (grep $^O eq $_, qw/MSWin32 os2 VMS dos MacOS haiku/) { |
808
|
0
|
|
|
|
|
0
|
return 0; |
809
|
|
|
|
|
|
|
} else { |
810
|
5
|
|
|
|
|
13
|
return 1; |
811
|
|
|
|
|
|
|
} |
812
|
|
|
|
|
|
|
|
813
|
|
|
|
|
|
|
} |
814
|
|
|
|
|
|
|
|
815
|
|
|
|
|
|
|
# internal routine to decide which security levels are allowed |
816
|
|
|
|
|
|
|
# see safe_level() for more information on this |
817
|
|
|
|
|
|
|
|
818
|
|
|
|
|
|
|
# Controls whether the supplied security level is allowed |
819
|
|
|
|
|
|
|
|
820
|
|
|
|
|
|
|
# $cando = _can_do_level( $level ) |
821
|
|
|
|
|
|
|
|
822
|
|
|
|
|
|
|
sub _can_do_level { |
823
|
|
|
|
|
|
|
|
824
|
|
|
|
|
|
|
# Get security level |
825
|
3
|
|
|
3
|
|
4
|
my $level = shift; |
826
|
|
|
|
|
|
|
|
827
|
|
|
|
|
|
|
# Always have to be able to do STANDARD |
828
|
3
|
100
|
|
|
|
9
|
return 1 if $level == STANDARD; |
829
|
|
|
|
|
|
|
|
830
|
|
|
|
|
|
|
# Currently, the systems that can do HIGH or MEDIUM are identical |
831
|
2
|
50
|
33
|
|
|
23
|
if ( $^O eq 'MSWin32' || $^O eq 'os2' || $^O eq 'cygwin' || $^O eq 'dos' || $^O eq 'MacOS' || $^O eq 'mpeix') { |
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
832
|
0
|
|
|
|
|
0
|
return 0; |
833
|
|
|
|
|
|
|
} else { |
834
|
2
|
|
|
|
|
5
|
return 1; |
835
|
|
|
|
|
|
|
} |
836
|
|
|
|
|
|
|
|
837
|
|
|
|
|
|
|
} |
838
|
|
|
|
|
|
|
|
839
|
|
|
|
|
|
|
# This routine sets up a deferred unlinking of a specified |
840
|
|
|
|
|
|
|
# filename and filehandle. It is used in the following cases: |
841
|
|
|
|
|
|
|
# - Called by unlink0 if an opened file can not be unlinked |
842
|
|
|
|
|
|
|
# - Called by tempfile() if files are to be removed on shutdown |
843
|
|
|
|
|
|
|
# - Called by tempdir() if directories are to be removed on shutdown |
844
|
|
|
|
|
|
|
|
845
|
|
|
|
|
|
|
# Arguments: |
846
|
|
|
|
|
|
|
# _deferred_unlink( $fh, $fname, $isdir ); |
847
|
|
|
|
|
|
|
# |
848
|
|
|
|
|
|
|
# - filehandle (so that it can be explicitly closed if open |
849
|
|
|
|
|
|
|
# - filename (the thing we want to remove) |
850
|
|
|
|
|
|
|
# - isdir (flag to indicate that we are being given a directory) |
851
|
|
|
|
|
|
|
# [and hence no filehandle] |
852
|
|
|
|
|
|
|
|
853
|
|
|
|
|
|
|
# Status is not referred to since all the magic is done with an END block |
854
|
|
|
|
|
|
|
|
855
|
|
|
|
|
|
|
{ |
856
|
|
|
|
|
|
|
# Will set up two lexical variables to contain all the files to be |
857
|
|
|
|
|
|
|
# removed. One array for files, another for directories They will |
858
|
|
|
|
|
|
|
# only exist in this block. |
859
|
|
|
|
|
|
|
|
860
|
|
|
|
|
|
|
# This means we only have to set up a single END block to remove |
861
|
|
|
|
|
|
|
# all files. |
862
|
|
|
|
|
|
|
|
863
|
|
|
|
|
|
|
# in order to prevent child processes inadvertently deleting the parent |
864
|
|
|
|
|
|
|
# temp files we use a hash to store the temp files and directories |
865
|
|
|
|
|
|
|
# created by a particular process id. |
866
|
|
|
|
|
|
|
|
867
|
|
|
|
|
|
|
# %files_to_unlink contains values that are references to an array of |
868
|
|
|
|
|
|
|
# array references containing the filehandle and filename associated with |
869
|
|
|
|
|
|
|
# the temp file. |
870
|
|
|
|
|
|
|
my (%files_to_unlink, %dirs_to_unlink); |
871
|
|
|
|
|
|
|
|
872
|
|
|
|
|
|
|
# Set up an end block to use these arrays |
873
|
|
|
|
|
|
|
END { |
874
|
13
|
|
|
13
|
|
10628
|
local($., $@, $!, $^E, $?); |
875
|
13
|
|
|
|
|
132
|
cleanup(at_exit => 1); |
876
|
|
|
|
|
|
|
} |
877
|
|
|
|
|
|
|
|
878
|
|
|
|
|
|
|
# Cleanup function. Always triggered on END (with at_exit => 1) but |
879
|
|
|
|
|
|
|
# can be invoked manually. |
880
|
|
|
|
|
|
|
sub cleanup { |
881
|
13
|
|
|
13
|
1
|
131
|
my %h = @_; |
882
|
13
|
|
|
|
|
67
|
my $at_exit = delete $h{at_exit}; |
883
|
13
|
50
|
|
|
|
139
|
$at_exit = 0 if not defined $at_exit; |
884
|
13
|
50
|
|
|
|
43
|
{ my @k = sort keys %h; die "unrecognized parameters: @k" if @k } |
|
13
|
|
|
|
|
113
|
|
|
13
|
|
|
|
|
76
|
|
885
|
|
|
|
|
|
|
|
886
|
13
|
50
|
|
|
|
68
|
if (!$KEEP_ALL) { |
887
|
|
|
|
|
|
|
# Files |
888
|
|
|
|
|
|
|
my @files = (exists $files_to_unlink{$$} ? |
889
|
13
|
100
|
|
|
|
86
|
@{ $files_to_unlink{$$} } : () ); |
|
3
|
|
|
|
|
12
|
|
890
|
13
|
|
|
|
|
80
|
foreach my $file (@files) { |
891
|
|
|
|
|
|
|
# close the filehandle without checking its state |
892
|
|
|
|
|
|
|
# in order to make real sure that this is closed |
893
|
|
|
|
|
|
|
# if its already closed then I don't care about the answer |
894
|
|
|
|
|
|
|
# probably a better way to do this |
895
|
8
|
|
|
|
|
78
|
close($file->[0]); # file handle is [0] |
896
|
|
|
|
|
|
|
|
897
|
8
|
50
|
|
|
|
107
|
if (-f $file->[1]) { # file name is [1] |
898
|
8
|
|
|
|
|
38
|
_force_writable( $file->[1] ); # for windows |
899
|
8
|
50
|
|
|
|
240
|
unlink $file->[1] or warn "Error removing ".$file->[1]; |
900
|
|
|
|
|
|
|
} |
901
|
|
|
|
|
|
|
} |
902
|
|
|
|
|
|
|
# Dirs |
903
|
|
|
|
|
|
|
my @dirs = (exists $dirs_to_unlink{$$} ? |
904
|
13
|
100
|
|
|
|
113
|
@{ $dirs_to_unlink{$$} } : () ); |
|
2
|
|
|
|
|
14
|
|
905
|
13
|
|
|
|
|
40
|
my ($cwd, $cwd_to_remove); |
906
|
13
|
|
|
|
|
43
|
foreach my $dir (@dirs) { |
907
|
4
|
50
|
|
|
|
62
|
if (-d $dir) { |
908
|
|
|
|
|
|
|
# Some versions of rmtree will abort if you attempt to remove |
909
|
|
|
|
|
|
|
# the directory you are sitting in. For automatic cleanup |
910
|
|
|
|
|
|
|
# at program exit, we avoid this by chdir()ing out of the way |
911
|
|
|
|
|
|
|
# first. If not at program exit, it's best not to mess with the |
912
|
|
|
|
|
|
|
# current directory, so just let it fail with a warning. |
913
|
4
|
50
|
|
|
|
10
|
if ($at_exit) { |
914
|
4
|
100
|
|
|
|
47
|
$cwd = Cwd::abs_path(File::Spec->curdir) if not defined $cwd; |
915
|
4
|
|
|
|
|
112
|
my $abs = Cwd::abs_path($dir); |
916
|
4
|
100
|
|
|
|
16
|
if ($abs eq $cwd) { |
917
|
1
|
|
|
|
|
2
|
$cwd_to_remove = $dir; |
918
|
1
|
|
|
|
|
3
|
next; |
919
|
|
|
|
|
|
|
} |
920
|
|
|
|
|
|
|
} |
921
|
3
|
|
|
|
|
8
|
eval { rmtree($dir, $DEBUG, 0); }; |
|
3
|
|
|
|
|
1022
|
|
922
|
3
|
0
|
33
|
|
|
26
|
warn $@ if ($@ && $^W); |
923
|
|
|
|
|
|
|
} |
924
|
|
|
|
|
|
|
} |
925
|
|
|
|
|
|
|
|
926
|
13
|
100
|
|
|
|
44
|
if (defined $cwd_to_remove) { |
927
|
|
|
|
|
|
|
# We do need to clean up the current directory, and everything |
928
|
|
|
|
|
|
|
# else is done, so get out of there and remove it. |
929
|
1
|
50
|
|
|
|
8
|
chdir $cwd_to_remove or die "cannot chdir to $cwd_to_remove: $!"; |
930
|
1
|
|
|
|
|
13
|
my $updir = File::Spec->updir; |
931
|
1
|
50
|
|
|
|
9
|
chdir $updir or die "cannot chdir to $updir: $!"; |
932
|
1
|
|
|
|
|
2
|
eval { rmtree($cwd_to_remove, $DEBUG, 0); }; |
|
1
|
|
|
|
|
148
|
|
933
|
1
|
0
|
33
|
|
|
6
|
warn $@ if ($@ && $^W); |
934
|
|
|
|
|
|
|
} |
935
|
|
|
|
|
|
|
|
936
|
|
|
|
|
|
|
# clear the arrays |
937
|
3
|
|
|
|
|
11
|
@{ $files_to_unlink{$$} } = () |
938
|
13
|
100
|
|
|
|
102
|
if exists $files_to_unlink{$$}; |
939
|
2
|
|
|
|
|
40
|
@{ $dirs_to_unlink{$$} } = () |
940
|
13
|
100
|
|
|
|
188
|
if exists $dirs_to_unlink{$$}; |
941
|
|
|
|
|
|
|
} |
942
|
|
|
|
|
|
|
} |
943
|
|
|
|
|
|
|
|
944
|
|
|
|
|
|
|
|
945
|
|
|
|
|
|
|
# This is the sub called to register a file for deferred unlinking |
946
|
|
|
|
|
|
|
# This could simply store the input parameters and defer everything |
947
|
|
|
|
|
|
|
# until the END block. For now we do a bit of checking at this |
948
|
|
|
|
|
|
|
# point in order to make sure that (1) we have a file/dir to delete |
949
|
|
|
|
|
|
|
# and (2) we have been called with the correct arguments. |
950
|
|
|
|
|
|
|
sub _deferred_unlink { |
951
|
|
|
|
|
|
|
|
952
|
14
|
50
|
|
14
|
|
104
|
croak 'Usage: _deferred_unlink($fh, $fname, $isdir)' |
953
|
|
|
|
|
|
|
unless scalar(@_) == 3; |
954
|
|
|
|
|
|
|
|
955
|
14
|
|
|
|
|
46
|
my ($fh, $fname, $isdir) = @_; |
956
|
|
|
|
|
|
|
|
957
|
14
|
50
|
|
|
|
102
|
warn "Setting up deferred removal of $fname\n" |
958
|
|
|
|
|
|
|
if $DEBUG; |
959
|
|
|
|
|
|
|
|
960
|
|
|
|
|
|
|
# make sure we save the absolute path for later cleanup |
961
|
|
|
|
|
|
|
# OK to untaint because we only ever use this internally |
962
|
|
|
|
|
|
|
# as a file path, never interpolating into the shell |
963
|
14
|
|
|
|
|
325
|
$fname = Cwd::abs_path($fname); |
964
|
14
|
|
|
|
|
130
|
($fname) = $fname =~ /^(.*)$/; |
965
|
|
|
|
|
|
|
|
966
|
|
|
|
|
|
|
# If we have a directory, check that it is a directory |
967
|
14
|
100
|
|
|
|
81
|
if ($isdir) { |
968
|
|
|
|
|
|
|
|
969
|
4
|
50
|
|
|
|
40
|
if (-d $fname) { |
970
|
|
|
|
|
|
|
|
971
|
|
|
|
|
|
|
# Directory exists so store it |
972
|
|
|
|
|
|
|
# first on VMS turn []foo into [.foo] for rmtree |
973
|
4
|
50
|
|
|
|
17
|
$fname = VMS::Filespec::vmspath($fname) if $^O eq 'VMS'; |
974
|
|
|
|
|
|
|
$dirs_to_unlink{$$} = [] |
975
|
4
|
100
|
|
|
|
16
|
unless exists $dirs_to_unlink{$$}; |
976
|
4
|
|
|
|
|
6
|
push (@{ $dirs_to_unlink{$$} }, $fname); |
|
4
|
|
|
|
|
14
|
|
977
|
|
|
|
|
|
|
|
978
|
|
|
|
|
|
|
} else { |
979
|
0
|
0
|
|
|
|
0
|
carp "Request to remove directory $fname could not be completed since it does not exist!\n" if $^W; |
980
|
|
|
|
|
|
|
} |
981
|
|
|
|
|
|
|
|
982
|
|
|
|
|
|
|
} else { |
983
|
|
|
|
|
|
|
|
984
|
10
|
50
|
|
|
|
149
|
if (-f $fname) { |
985
|
|
|
|
|
|
|
|
986
|
|
|
|
|
|
|
# file exists so store handle and name for later removal |
987
|
|
|
|
|
|
|
$files_to_unlink{$$} = [] |
988
|
10
|
100
|
|
|
|
133
|
unless exists $files_to_unlink{$$}; |
989
|
10
|
|
|
|
|
37
|
push(@{ $files_to_unlink{$$} }, [$fh, $fname]); |
|
10
|
|
|
|
|
120
|
|
990
|
|
|
|
|
|
|
|
991
|
|
|
|
|
|
|
} else { |
992
|
0
|
0
|
|
|
|
0
|
carp "Request to remove file $fname could not be completed since it is not there!\n" if $^W; |
993
|
|
|
|
|
|
|
} |
994
|
|
|
|
|
|
|
|
995
|
|
|
|
|
|
|
} |
996
|
|
|
|
|
|
|
|
997
|
|
|
|
|
|
|
} |
998
|
|
|
|
|
|
|
|
999
|
|
|
|
|
|
|
|
1000
|
|
|
|
|
|
|
} |
1001
|
|
|
|
|
|
|
|
1002
|
|
|
|
|
|
|
# normalize argument keys to upper case and do consistent handling |
1003
|
|
|
|
|
|
|
# of leading template vs TEMPLATE |
1004
|
|
|
|
|
|
|
sub _parse_args { |
1005
|
59
|
100
|
|
59
|
|
217
|
my $leading_template = (scalar(@_) % 2 == 1 ? shift(@_) : '' ); |
1006
|
59
|
|
|
|
|
148
|
my %args = @_; |
1007
|
59
|
|
|
|
|
336
|
%args = map +(uc($_) => $args{$_}), keys %args; |
1008
|
|
|
|
|
|
|
|
1009
|
|
|
|
|
|
|
# template (store it in an array so that it will |
1010
|
|
|
|
|
|
|
# disappear from the arg list of tempfile) |
1011
|
|
|
|
|
|
|
my @template = ( |
1012
|
|
|
|
|
|
|
exists $args{TEMPLATE} ? $args{TEMPLATE} : |
1013
|
59
|
100
|
|
|
|
223
|
$leading_template ? $leading_template : () |
|
|
100
|
|
|
|
|
|
1014
|
|
|
|
|
|
|
); |
1015
|
59
|
|
|
|
|
84
|
delete $args{TEMPLATE}; |
1016
|
|
|
|
|
|
|
|
1017
|
59
|
|
|
|
|
173
|
return( \@template, \%args ); |
1018
|
|
|
|
|
|
|
} |
1019
|
|
|
|
|
|
|
|
1020
|
|
|
|
|
|
|
#pod =head1 OBJECT-ORIENTED INTERFACE |
1021
|
|
|
|
|
|
|
#pod |
1022
|
|
|
|
|
|
|
#pod This is the primary interface for interacting with |
1023
|
|
|
|
|
|
|
#pod C. Using the OO interface a temporary file can be created |
1024
|
|
|
|
|
|
|
#pod when the object is constructed and the file can be removed when the |
1025
|
|
|
|
|
|
|
#pod object is no longer required. |
1026
|
|
|
|
|
|
|
#pod |
1027
|
|
|
|
|
|
|
#pod Note that there is no method to obtain the filehandle from the |
1028
|
|
|
|
|
|
|
#pod C object. The object itself acts as a filehandle. The object |
1029
|
|
|
|
|
|
|
#pod isa C and isa C so all those methods are |
1030
|
|
|
|
|
|
|
#pod available. |
1031
|
|
|
|
|
|
|
#pod |
1032
|
|
|
|
|
|
|
#pod Also, the object is configured such that it stringifies to the name of the |
1033
|
|
|
|
|
|
|
#pod temporary file and so can be compared to a filename directly. It numifies |
1034
|
|
|
|
|
|
|
#pod to the C the same as other handles and so can be compared to other |
1035
|
|
|
|
|
|
|
#pod handles with C<==>. |
1036
|
|
|
|
|
|
|
#pod |
1037
|
|
|
|
|
|
|
#pod $fh eq $filename # as a string |
1038
|
|
|
|
|
|
|
#pod $fh != \*STDOUT # as a number |
1039
|
|
|
|
|
|
|
#pod |
1040
|
|
|
|
|
|
|
#pod Available since 0.14. |
1041
|
|
|
|
|
|
|
#pod |
1042
|
|
|
|
|
|
|
#pod =over 4 |
1043
|
|
|
|
|
|
|
#pod |
1044
|
|
|
|
|
|
|
#pod =item B |
1045
|
|
|
|
|
|
|
#pod |
1046
|
|
|
|
|
|
|
#pod Create a temporary file object. |
1047
|
|
|
|
|
|
|
#pod |
1048
|
|
|
|
|
|
|
#pod my $tmp = File::Temp->new(); |
1049
|
|
|
|
|
|
|
#pod |
1050
|
|
|
|
|
|
|
#pod by default the object is constructed as if C |
1051
|
|
|
|
|
|
|
#pod was called without options, but with the additional behaviour |
1052
|
|
|
|
|
|
|
#pod that the temporary file is removed by the object destructor |
1053
|
|
|
|
|
|
|
#pod if UNLINK is set to true (the default). |
1054
|
|
|
|
|
|
|
#pod |
1055
|
|
|
|
|
|
|
#pod Supported arguments are the same as for C: UNLINK |
1056
|
|
|
|
|
|
|
#pod (defaulting to true), DIR, EXLOCK, PERMS and SUFFIX. |
1057
|
|
|
|
|
|
|
#pod Additionally, the filename |
1058
|
|
|
|
|
|
|
#pod template is specified using the TEMPLATE option. The OPEN option |
1059
|
|
|
|
|
|
|
#pod is not supported (the file is always opened). |
1060
|
|
|
|
|
|
|
#pod |
1061
|
|
|
|
|
|
|
#pod $tmp = File::Temp->new( TEMPLATE => 'tempXXXXX', |
1062
|
|
|
|
|
|
|
#pod DIR => 'mydir', |
1063
|
|
|
|
|
|
|
#pod SUFFIX => '.dat'); |
1064
|
|
|
|
|
|
|
#pod |
1065
|
|
|
|
|
|
|
#pod Arguments are case insensitive. |
1066
|
|
|
|
|
|
|
#pod |
1067
|
|
|
|
|
|
|
#pod Can call croak() if an error occurs. |
1068
|
|
|
|
|
|
|
#pod |
1069
|
|
|
|
|
|
|
#pod Available since 0.14. |
1070
|
|
|
|
|
|
|
#pod |
1071
|
|
|
|
|
|
|
#pod TEMPLATE available since 0.23 |
1072
|
|
|
|
|
|
|
#pod |
1073
|
|
|
|
|
|
|
#pod =cut |
1074
|
|
|
|
|
|
|
|
1075
|
|
|
|
|
|
|
sub new { |
1076
|
15
|
|
|
15
|
1
|
1323
|
my $proto = shift; |
1077
|
15
|
|
33
|
|
|
85
|
my $class = ref($proto) || $proto; |
1078
|
|
|
|
|
|
|
|
1079
|
15
|
|
|
|
|
45
|
my ($maybe_template, $args) = _parse_args(@_); |
1080
|
|
|
|
|
|
|
|
1081
|
|
|
|
|
|
|
# see if they are unlinking (defaulting to yes) |
1082
|
15
|
100
|
|
|
|
39
|
my $unlink = (exists $args->{UNLINK} ? $args->{UNLINK} : 1 ); |
1083
|
15
|
|
|
|
|
23
|
delete $args->{UNLINK}; |
1084
|
|
|
|
|
|
|
|
1085
|
|
|
|
|
|
|
# Protect OPEN |
1086
|
15
|
|
|
|
|
22
|
delete $args->{OPEN}; |
1087
|
|
|
|
|
|
|
|
1088
|
|
|
|
|
|
|
# Open the file and retain file handle and file name |
1089
|
15
|
|
|
|
|
48
|
my ($fh, $path) = tempfile( @$maybe_template, %$args ); |
1090
|
|
|
|
|
|
|
|
1091
|
15
|
50
|
|
|
|
43
|
print "Tmp: $fh - $path\n" if $DEBUG; |
1092
|
|
|
|
|
|
|
|
1093
|
|
|
|
|
|
|
# Store the filename in the scalar slot |
1094
|
15
|
|
|
|
|
23
|
${*$fh} = $path; |
|
15
|
|
|
|
|
54
|
|
1095
|
|
|
|
|
|
|
|
1096
|
|
|
|
|
|
|
# Cache the filename by pid so that the destructor can decide whether to remove it |
1097
|
15
|
|
|
|
|
84
|
$FILES_CREATED_BY_OBJECT{$$}{$path} = 1; |
1098
|
|
|
|
|
|
|
|
1099
|
|
|
|
|
|
|
# Store unlink information in hash slot (plus other constructor info) |
1100
|
15
|
|
|
|
|
31
|
%{*$fh} = %$args; |
|
15
|
|
|
|
|
45
|
|
1101
|
|
|
|
|
|
|
|
1102
|
|
|
|
|
|
|
# create the object |
1103
|
15
|
|
|
|
|
63
|
bless $fh, $class; |
1104
|
|
|
|
|
|
|
|
1105
|
|
|
|
|
|
|
# final method-based configuration |
1106
|
15
|
|
|
|
|
51
|
$fh->unlink_on_destroy( $unlink ); |
1107
|
|
|
|
|
|
|
|
1108
|
15
|
|
|
|
|
60
|
return $fh; |
1109
|
|
|
|
|
|
|
} |
1110
|
|
|
|
|
|
|
|
1111
|
|
|
|
|
|
|
#pod =item B |
1112
|
|
|
|
|
|
|
#pod |
1113
|
|
|
|
|
|
|
#pod Create a temporary directory using an object oriented interface. |
1114
|
|
|
|
|
|
|
#pod |
1115
|
|
|
|
|
|
|
#pod $dir = File::Temp->newdir(); |
1116
|
|
|
|
|
|
|
#pod |
1117
|
|
|
|
|
|
|
#pod By default the directory is deleted when the object goes out of scope. |
1118
|
|
|
|
|
|
|
#pod |
1119
|
|
|
|
|
|
|
#pod Supports the same options as the C function. Note that directories |
1120
|
|
|
|
|
|
|
#pod created with this method default to CLEANUP => 1. |
1121
|
|
|
|
|
|
|
#pod |
1122
|
|
|
|
|
|
|
#pod $dir = File::Temp->newdir( $template, %options ); |
1123
|
|
|
|
|
|
|
#pod |
1124
|
|
|
|
|
|
|
#pod A template may be specified either with a leading template or |
1125
|
|
|
|
|
|
|
#pod with a TEMPLATE argument. |
1126
|
|
|
|
|
|
|
#pod |
1127
|
|
|
|
|
|
|
#pod Available since 0.19. |
1128
|
|
|
|
|
|
|
#pod |
1129
|
|
|
|
|
|
|
#pod TEMPLATE available since 0.23. |
1130
|
|
|
|
|
|
|
#pod |
1131
|
|
|
|
|
|
|
#pod =cut |
1132
|
|
|
|
|
|
|
|
1133
|
|
|
|
|
|
|
sub newdir { |
1134
|
5
|
|
|
5
|
1
|
706
|
my $self = shift; |
1135
|
|
|
|
|
|
|
|
1136
|
5
|
|
|
|
|
17
|
my ($maybe_template, $args) = _parse_args(@_); |
1137
|
|
|
|
|
|
|
|
1138
|
|
|
|
|
|
|
# handle CLEANUP without passing CLEANUP to tempdir |
1139
|
5
|
50
|
|
|
|
13
|
my $cleanup = (exists $args->{CLEANUP} ? $args->{CLEANUP} : 1 ); |
1140
|
5
|
|
|
|
|
9
|
delete $args->{CLEANUP}; |
1141
|
|
|
|
|
|
|
|
1142
|
5
|
|
|
|
|
16
|
my $tempdir = tempdir( @$maybe_template, %$args); |
1143
|
|
|
|
|
|
|
|
1144
|
|
|
|
|
|
|
# get a safe absolute path for cleanup, just like |
1145
|
|
|
|
|
|
|
# happens in _deferred_unlink |
1146
|
5
|
|
|
|
|
92
|
my $real_dir = Cwd::abs_path( $tempdir ); |
1147
|
5
|
|
|
|
|
36
|
($real_dir) = $real_dir =~ /^(.*)$/; |
1148
|
|
|
|
|
|
|
|
1149
|
5
|
|
|
|
|
52
|
return bless { DIRNAME => $tempdir, |
1150
|
|
|
|
|
|
|
REALNAME => $real_dir, |
1151
|
|
|
|
|
|
|
CLEANUP => $cleanup, |
1152
|
|
|
|
|
|
|
LAUNCHPID => $$, |
1153
|
|
|
|
|
|
|
}, "File::Temp::Dir"; |
1154
|
|
|
|
|
|
|
} |
1155
|
|
|
|
|
|
|
|
1156
|
|
|
|
|
|
|
#pod =item B |
1157
|
|
|
|
|
|
|
#pod |
1158
|
|
|
|
|
|
|
#pod Return the name of the temporary file associated with this object |
1159
|
|
|
|
|
|
|
#pod (if the object was created using the "new" constructor). |
1160
|
|
|
|
|
|
|
#pod |
1161
|
|
|
|
|
|
|
#pod $filename = $tmp->filename; |
1162
|
|
|
|
|
|
|
#pod |
1163
|
|
|
|
|
|
|
#pod This method is called automatically when the object is used as |
1164
|
|
|
|
|
|
|
#pod a string. |
1165
|
|
|
|
|
|
|
#pod |
1166
|
|
|
|
|
|
|
#pod Current API available since 0.14 |
1167
|
|
|
|
|
|
|
#pod |
1168
|
|
|
|
|
|
|
#pod =cut |
1169
|
|
|
|
|
|
|
|
1170
|
|
|
|
|
|
|
sub filename { |
1171
|
60
|
|
|
60
|
1
|
276
|
my $self = shift; |
1172
|
60
|
|
|
|
|
136
|
return ${*$self}; |
|
60
|
|
|
|
|
1370
|
|
1173
|
|
|
|
|
|
|
} |
1174
|
|
|
|
|
|
|
|
1175
|
|
|
|
|
|
|
sub STRINGIFY { |
1176
|
36
|
|
|
36
|
0
|
1274
|
my $self = shift; |
1177
|
36
|
|
|
|
|
66
|
return $self->filename; |
1178
|
|
|
|
|
|
|
} |
1179
|
|
|
|
|
|
|
|
1180
|
|
|
|
|
|
|
# For reference, can't use '0+'=>\&Scalar::Util::refaddr directly because |
1181
|
|
|
|
|
|
|
# refaddr() demands one parameter only, whereas overload.pm calls with three |
1182
|
|
|
|
|
|
|
# even for unary operations like '0+'. |
1183
|
|
|
|
|
|
|
sub NUMIFY { |
1184
|
10
|
|
|
10
|
0
|
46
|
return refaddr($_[0]); |
1185
|
|
|
|
|
|
|
} |
1186
|
|
|
|
|
|
|
|
1187
|
|
|
|
|
|
|
#pod =item B |
1188
|
|
|
|
|
|
|
#pod |
1189
|
|
|
|
|
|
|
#pod Return the name of the temporary directory associated with this |
1190
|
|
|
|
|
|
|
#pod object (if the object was created using the "newdir" constructor). |
1191
|
|
|
|
|
|
|
#pod |
1192
|
|
|
|
|
|
|
#pod $dirname = $tmpdir->dirname; |
1193
|
|
|
|
|
|
|
#pod |
1194
|
|
|
|
|
|
|
#pod This method is called automatically when the object is used in string context. |
1195
|
|
|
|
|
|
|
#pod |
1196
|
|
|
|
|
|
|
#pod =item B |
1197
|
|
|
|
|
|
|
#pod |
1198
|
|
|
|
|
|
|
#pod Control whether the file is unlinked when the object goes out of scope. |
1199
|
|
|
|
|
|
|
#pod The file is removed if this value is true and $KEEP_ALL is not. |
1200
|
|
|
|
|
|
|
#pod |
1201
|
|
|
|
|
|
|
#pod $fh->unlink_on_destroy( 1 ); |
1202
|
|
|
|
|
|
|
#pod |
1203
|
|
|
|
|
|
|
#pod Default is for the file to be removed. |
1204
|
|
|
|
|
|
|
#pod |
1205
|
|
|
|
|
|
|
#pod Current API available since 0.15 |
1206
|
|
|
|
|
|
|
#pod |
1207
|
|
|
|
|
|
|
#pod =cut |
1208
|
|
|
|
|
|
|
|
1209
|
|
|
|
|
|
|
sub unlink_on_destroy { |
1210
|
18
|
|
|
18
|
1
|
32
|
my $self = shift; |
1211
|
18
|
100
|
|
|
|
50
|
if (@_) { |
1212
|
15
|
|
|
|
|
18
|
${*$self}{UNLINK} = shift; |
|
15
|
|
|
|
|
493
|
|
1213
|
|
|
|
|
|
|
} |
1214
|
18
|
|
|
|
|
32
|
return ${*$self}{UNLINK}; |
|
18
|
|
|
|
|
42
|
|
1215
|
|
|
|
|
|
|
} |
1216
|
|
|
|
|
|
|
|
1217
|
|
|
|
|
|
|
#pod =item B |
1218
|
|
|
|
|
|
|
#pod |
1219
|
|
|
|
|
|
|
#pod When the object goes out of scope, the destructor is called. This |
1220
|
|
|
|
|
|
|
#pod destructor will attempt to unlink the file (using L) |
1221
|
|
|
|
|
|
|
#pod if the constructor was called with UNLINK set to 1 (the default state |
1222
|
|
|
|
|
|
|
#pod if UNLINK is not specified). |
1223
|
|
|
|
|
|
|
#pod |
1224
|
|
|
|
|
|
|
#pod No error is given if the unlink fails. |
1225
|
|
|
|
|
|
|
#pod |
1226
|
|
|
|
|
|
|
#pod If the object has been passed to a child process during a fork, the |
1227
|
|
|
|
|
|
|
#pod file will be deleted when the object goes out of scope in the parent. |
1228
|
|
|
|
|
|
|
#pod |
1229
|
|
|
|
|
|
|
#pod For a temporary directory object the directory will be removed unless |
1230
|
|
|
|
|
|
|
#pod the CLEANUP argument was used in the constructor (and set to false) or |
1231
|
|
|
|
|
|
|
#pod C was modified after creation. Note that if a temp |
1232
|
|
|
|
|
|
|
#pod directory is your current directory, it cannot be removed - a warning |
1233
|
|
|
|
|
|
|
#pod will be given in this case. C out of the directory before |
1234
|
|
|
|
|
|
|
#pod letting the object go out of scope. |
1235
|
|
|
|
|
|
|
#pod |
1236
|
|
|
|
|
|
|
#pod If the global variable $KEEP_ALL is true, the file or directory |
1237
|
|
|
|
|
|
|
#pod will not be removed. |
1238
|
|
|
|
|
|
|
#pod |
1239
|
|
|
|
|
|
|
#pod =cut |
1240
|
|
|
|
|
|
|
|
1241
|
|
|
|
|
|
|
sub DESTROY { |
1242
|
14
|
|
|
14
|
|
6522970
|
local($., $@, $!, $^E, $?); |
1243
|
14
|
|
|
|
|
138
|
my $self = shift; |
1244
|
|
|
|
|
|
|
|
1245
|
|
|
|
|
|
|
# Make sure we always remove the file from the global hash |
1246
|
|
|
|
|
|
|
# on destruction. This prevents the hash from growing uncontrollably |
1247
|
|
|
|
|
|
|
# and post-destruction there is no reason to know about the file. |
1248
|
14
|
|
|
|
|
151
|
my $file = $self->filename; |
1249
|
14
|
|
|
|
|
107
|
my $was_created_by_proc; |
1250
|
14
|
100
|
|
|
|
282
|
if (exists $FILES_CREATED_BY_OBJECT{$$}{$file}) { |
1251
|
10
|
|
|
|
|
20
|
$was_created_by_proc = 1; |
1252
|
10
|
|
|
|
|
36
|
delete $FILES_CREATED_BY_OBJECT{$$}{$file}; |
1253
|
|
|
|
|
|
|
} |
1254
|
|
|
|
|
|
|
|
1255
|
14
|
100
|
100
|
|
|
64
|
if (${*$self}{UNLINK} && !$KEEP_ALL) { |
|
14
|
|
|
|
|
264
|
|
1256
|
11
|
50
|
|
|
|
145
|
print "# ---------> Unlinking $self\n" if $DEBUG; |
1257
|
|
|
|
|
|
|
|
1258
|
|
|
|
|
|
|
# only delete if this process created it |
1259
|
11
|
100
|
|
|
|
1108
|
return unless $was_created_by_proc; |
1260
|
|
|
|
|
|
|
|
1261
|
|
|
|
|
|
|
# The unlink1 may fail if the file has been closed |
1262
|
|
|
|
|
|
|
# by the caller. This leaves us with the decision |
1263
|
|
|
|
|
|
|
# of whether to refuse to remove the file or simply |
1264
|
|
|
|
|
|
|
# do an unlink without test. Seems to be silly |
1265
|
|
|
|
|
|
|
# to do this when we are trying to be careful |
1266
|
|
|
|
|
|
|
# about security |
1267
|
7
|
|
|
|
|
35
|
_force_writable( $file ); # for windows |
1268
|
7
|
100
|
|
|
|
37
|
unlink1( $self, $file ) |
1269
|
|
|
|
|
|
|
or unlink($file); |
1270
|
|
|
|
|
|
|
} |
1271
|
|
|
|
|
|
|
} |
1272
|
|
|
|
|
|
|
|
1273
|
|
|
|
|
|
|
#pod =back |
1274
|
|
|
|
|
|
|
#pod |
1275
|
|
|
|
|
|
|
#pod =head1 FUNCTIONS |
1276
|
|
|
|
|
|
|
#pod |
1277
|
|
|
|
|
|
|
#pod This section describes the recommended interface for generating |
1278
|
|
|
|
|
|
|
#pod temporary files and directories. |
1279
|
|
|
|
|
|
|
#pod |
1280
|
|
|
|
|
|
|
#pod =over 4 |
1281
|
|
|
|
|
|
|
#pod |
1282
|
|
|
|
|
|
|
#pod =item B |
1283
|
|
|
|
|
|
|
#pod |
1284
|
|
|
|
|
|
|
#pod This is the basic function to generate temporary files. |
1285
|
|
|
|
|
|
|
#pod The behaviour of the file can be changed using various options: |
1286
|
|
|
|
|
|
|
#pod |
1287
|
|
|
|
|
|
|
#pod $fh = tempfile(); |
1288
|
|
|
|
|
|
|
#pod ($fh, $filename) = tempfile(); |
1289
|
|
|
|
|
|
|
#pod |
1290
|
|
|
|
|
|
|
#pod Create a temporary file in the directory specified for temporary |
1291
|
|
|
|
|
|
|
#pod files, as specified by the tmpdir() function in L. |
1292
|
|
|
|
|
|
|
#pod |
1293
|
|
|
|
|
|
|
#pod ($fh, $filename) = tempfile($template); |
1294
|
|
|
|
|
|
|
#pod |
1295
|
|
|
|
|
|
|
#pod Create a temporary file in the current directory using the supplied |
1296
|
|
|
|
|
|
|
#pod template. Trailing `X' characters are replaced with random letters to |
1297
|
|
|
|
|
|
|
#pod generate the filename. At least four `X' characters must be present |
1298
|
|
|
|
|
|
|
#pod at the end of the template. |
1299
|
|
|
|
|
|
|
#pod |
1300
|
|
|
|
|
|
|
#pod ($fh, $filename) = tempfile($template, SUFFIX => $suffix) |
1301
|
|
|
|
|
|
|
#pod |
1302
|
|
|
|
|
|
|
#pod Same as previously, except that a suffix is added to the template |
1303
|
|
|
|
|
|
|
#pod after the `X' translation. Useful for ensuring that a temporary |
1304
|
|
|
|
|
|
|
#pod filename has a particular extension when needed by other applications. |
1305
|
|
|
|
|
|
|
#pod But see the WARNING at the end. |
1306
|
|
|
|
|
|
|
#pod |
1307
|
|
|
|
|
|
|
#pod ($fh, $filename) = tempfile($template, DIR => $dir); |
1308
|
|
|
|
|
|
|
#pod |
1309
|
|
|
|
|
|
|
#pod Translates the template as before except that a directory name |
1310
|
|
|
|
|
|
|
#pod is specified. |
1311
|
|
|
|
|
|
|
#pod |
1312
|
|
|
|
|
|
|
#pod ($fh, $filename) = tempfile($template, TMPDIR => 1); |
1313
|
|
|
|
|
|
|
#pod |
1314
|
|
|
|
|
|
|
#pod Equivalent to specifying a DIR of "File::Spec->tmpdir", writing the file |
1315
|
|
|
|
|
|
|
#pod into the same temporary directory as would be used if no template was |
1316
|
|
|
|
|
|
|
#pod specified at all. |
1317
|
|
|
|
|
|
|
#pod |
1318
|
|
|
|
|
|
|
#pod ($fh, $filename) = tempfile($template, UNLINK => 1); |
1319
|
|
|
|
|
|
|
#pod |
1320
|
|
|
|
|
|
|
#pod Return the filename and filehandle as before except that the file is |
1321
|
|
|
|
|
|
|
#pod automatically removed when the program exits (dependent on |
1322
|
|
|
|
|
|
|
#pod $KEEP_ALL). Default is for the file to be removed if a file handle is |
1323
|
|
|
|
|
|
|
#pod requested and to be kept if the filename is requested. In a scalar |
1324
|
|
|
|
|
|
|
#pod context (where no filename is returned) the file is always deleted |
1325
|
|
|
|
|
|
|
#pod either (depending on the operating system) on exit or when it is |
1326
|
|
|
|
|
|
|
#pod closed (unless $KEEP_ALL is true when the temp file is created). |
1327
|
|
|
|
|
|
|
#pod |
1328
|
|
|
|
|
|
|
#pod Use the object-oriented interface if fine-grained control of when |
1329
|
|
|
|
|
|
|
#pod a file is removed is required. |
1330
|
|
|
|
|
|
|
#pod |
1331
|
|
|
|
|
|
|
#pod If the template is not specified, a template is always |
1332
|
|
|
|
|
|
|
#pod automatically generated. This temporary file is placed in tmpdir() |
1333
|
|
|
|
|
|
|
#pod (L) unless a directory is specified explicitly with the |
1334
|
|
|
|
|
|
|
#pod DIR option. |
1335
|
|
|
|
|
|
|
#pod |
1336
|
|
|
|
|
|
|
#pod $fh = tempfile( DIR => $dir ); |
1337
|
|
|
|
|
|
|
#pod |
1338
|
|
|
|
|
|
|
#pod If called in scalar context, only the filehandle is returned and the |
1339
|
|
|
|
|
|
|
#pod file will automatically be deleted when closed on operating systems |
1340
|
|
|
|
|
|
|
#pod that support this (see the description of tmpfile() elsewhere in this |
1341
|
|
|
|
|
|
|
#pod document). This is the preferred mode of operation, as if you only |
1342
|
|
|
|
|
|
|
#pod have a filehandle, you can never create a race condition by fumbling |
1343
|
|
|
|
|
|
|
#pod with the filename. On systems that can not unlink an open file or can |
1344
|
|
|
|
|
|
|
#pod not mark a file as temporary when it is opened (for example, Windows |
1345
|
|
|
|
|
|
|
#pod NT uses the C flag) the file is marked for deletion when |
1346
|
|
|
|
|
|
|
#pod the program ends (equivalent to setting UNLINK to 1). The C |
1347
|
|
|
|
|
|
|
#pod flag is ignored if present. |
1348
|
|
|
|
|
|
|
#pod |
1349
|
|
|
|
|
|
|
#pod (undef, $filename) = tempfile($template, OPEN => 0); |
1350
|
|
|
|
|
|
|
#pod |
1351
|
|
|
|
|
|
|
#pod This will return the filename based on the template but |
1352
|
|
|
|
|
|
|
#pod will not open this file. Cannot be used in conjunction with |
1353
|
|
|
|
|
|
|
#pod UNLINK set to true. Default is to always open the file |
1354
|
|
|
|
|
|
|
#pod to protect from possible race conditions. A warning is issued |
1355
|
|
|
|
|
|
|
#pod if warnings are turned on. Consider using the tmpnam() |
1356
|
|
|
|
|
|
|
#pod and mktemp() functions described elsewhere in this document |
1357
|
|
|
|
|
|
|
#pod if opening the file is not required. |
1358
|
|
|
|
|
|
|
#pod |
1359
|
|
|
|
|
|
|
#pod To open the temporary filehandle with O_EXLOCK (open with exclusive |
1360
|
|
|
|
|
|
|
#pod file lock) use C<< EXLOCK=>1 >>. This is supported only by some |
1361
|
|
|
|
|
|
|
#pod operating systems (most notably BSD derived systems). By default |
1362
|
|
|
|
|
|
|
#pod EXLOCK will be false. Former C versions set EXLOCK to |
1363
|
|
|
|
|
|
|
#pod true, so to be sure to get an unlocked filehandle also with older |
1364
|
|
|
|
|
|
|
#pod versions, explicitly set C<< EXLOCK=>0 >>. |
1365
|
|
|
|
|
|
|
#pod |
1366
|
|
|
|
|
|
|
#pod ($fh, $filename) = tempfile($template, EXLOCK => 1); |
1367
|
|
|
|
|
|
|
#pod |
1368
|
|
|
|
|
|
|
#pod By default, the temp file is created with 0600 file permissions. |
1369
|
|
|
|
|
|
|
#pod Use C to change this: |
1370
|
|
|
|
|
|
|
#pod |
1371
|
|
|
|
|
|
|
#pod ($fh, $filename) = tempfile($template, PERMS => 0666); |
1372
|
|
|
|
|
|
|
#pod |
1373
|
|
|
|
|
|
|
#pod Options can be combined as required. |
1374
|
|
|
|
|
|
|
#pod |
1375
|
|
|
|
|
|
|
#pod Will croak() if there is an error. |
1376
|
|
|
|
|
|
|
#pod |
1377
|
|
|
|
|
|
|
#pod Available since 0.05. |
1378
|
|
|
|
|
|
|
#pod |
1379
|
|
|
|
|
|
|
#pod UNLINK flag available since 0.10. |
1380
|
|
|
|
|
|
|
#pod |
1381
|
|
|
|
|
|
|
#pod TMPDIR flag available since 0.19. |
1382
|
|
|
|
|
|
|
#pod |
1383
|
|
|
|
|
|
|
#pod EXLOCK flag available since 0.19. |
1384
|
|
|
|
|
|
|
#pod |
1385
|
|
|
|
|
|
|
#pod PERMS flag available since 0.2310. |
1386
|
|
|
|
|
|
|
#pod |
1387
|
|
|
|
|
|
|
#pod =cut |
1388
|
|
|
|
|
|
|
|
1389
|
|
|
|
|
|
|
sub tempfile { |
1390
|
31
|
100
|
100
|
31
|
1
|
4997
|
if ( @_ && $_[0] eq 'File::Temp' ) { |
1391
|
1
|
|
|
|
|
218
|
croak "'tempfile' can't be called as a method"; |
1392
|
|
|
|
|
|
|
} |
1393
|
|
|
|
|
|
|
# Can not check for argument count since we can have any |
1394
|
|
|
|
|
|
|
# number of args |
1395
|
|
|
|
|
|
|
|
1396
|
|
|
|
|
|
|
# Default options |
1397
|
30
|
|
|
|
|
267
|
my %options = ( |
1398
|
|
|
|
|
|
|
"DIR" => undef, # Directory prefix |
1399
|
|
|
|
|
|
|
"SUFFIX" => '', # Template suffix |
1400
|
|
|
|
|
|
|
"UNLINK" => 0, # Do not unlink file on exit |
1401
|
|
|
|
|
|
|
"OPEN" => 1, # Open file |
1402
|
|
|
|
|
|
|
"TMPDIR" => 0, # Place tempfile in tempdir if template specified |
1403
|
|
|
|
|
|
|
"EXLOCK" => 0, # Open file with O_EXLOCK |
1404
|
|
|
|
|
|
|
"PERMS" => undef, # File permissions |
1405
|
|
|
|
|
|
|
); |
1406
|
|
|
|
|
|
|
|
1407
|
|
|
|
|
|
|
# Check to see whether we have an odd or even number of arguments |
1408
|
30
|
|
|
|
|
91
|
my ($maybe_template, $args) = _parse_args(@_); |
1409
|
30
|
100
|
|
|
|
91
|
my $template = @$maybe_template ? $maybe_template->[0] : undef; |
1410
|
|
|
|
|
|
|
|
1411
|
|
|
|
|
|
|
# Read the options and merge with defaults |
1412
|
30
|
|
|
|
|
167
|
%options = (%options, %$args); |
1413
|
|
|
|
|
|
|
|
1414
|
|
|
|
|
|
|
# First decision is whether or not to open the file |
1415
|
30
|
50
|
|
|
|
107
|
if (! $options{"OPEN"}) { |
1416
|
|
|
|
|
|
|
|
1417
|
0
|
0
|
|
|
|
0
|
warn "tempfile(): temporary filename requested but not opened.\nPossibly unsafe, consider using tempfile() with OPEN set to true\n" |
1418
|
|
|
|
|
|
|
if $^W; |
1419
|
|
|
|
|
|
|
|
1420
|
|
|
|
|
|
|
} |
1421
|
|
|
|
|
|
|
|
1422
|
30
|
50
|
66
|
|
|
121
|
if ($options{"DIR"} and $^O eq 'VMS') { |
1423
|
|
|
|
|
|
|
|
1424
|
|
|
|
|
|
|
# on VMS turn []foo into [.foo] for concatenation |
1425
|
0
|
|
|
|
|
0
|
$options{"DIR"} = VMS::Filespec::vmspath($options{"DIR"}); |
1426
|
|
|
|
|
|
|
} |
1427
|
|
|
|
|
|
|
|
1428
|
|
|
|
|
|
|
# Construct the template |
1429
|
|
|
|
|
|
|
|
1430
|
|
|
|
|
|
|
# Have a choice of trying to work around the mkstemp/mktemp/tmpnam etc |
1431
|
|
|
|
|
|
|
# functions or simply constructing a template and using _gettemp() |
1432
|
|
|
|
|
|
|
# explicitly. Go for the latter |
1433
|
|
|
|
|
|
|
|
1434
|
|
|
|
|
|
|
# First generate a template if not defined and prefix the directory |
1435
|
|
|
|
|
|
|
# If no template must prefix the temp directory |
1436
|
30
|
100
|
|
|
|
63
|
if (defined $template) { |
1437
|
|
|
|
|
|
|
# End up with current directory if neither DIR not TMPDIR are set |
1438
|
11
|
100
|
|
|
|
25
|
if ($options{"DIR"}) { |
|
|
50
|
|
|
|
|
|
1439
|
|
|
|
|
|
|
|
1440
|
8
|
|
|
|
|
65
|
$template = File::Spec->catfile($options{"DIR"}, $template); |
1441
|
|
|
|
|
|
|
|
1442
|
|
|
|
|
|
|
} elsif ($options{TMPDIR}) { |
1443
|
|
|
|
|
|
|
|
1444
|
0
|
|
|
|
|
0
|
$template = File::Spec->catfile(_wrap_file_spec_tmpdir(), $template ); |
1445
|
|
|
|
|
|
|
|
1446
|
|
|
|
|
|
|
} |
1447
|
|
|
|
|
|
|
|
1448
|
|
|
|
|
|
|
} else { |
1449
|
|
|
|
|
|
|
|
1450
|
19
|
100
|
|
|
|
53
|
if ($options{"DIR"}) { |
1451
|
|
|
|
|
|
|
|
1452
|
6
|
|
|
|
|
41
|
$template = File::Spec->catfile($options{"DIR"}, TEMPXXX); |
1453
|
|
|
|
|
|
|
|
1454
|
|
|
|
|
|
|
} else { |
1455
|
|
|
|
|
|
|
|
1456
|
13
|
|
|
|
|
61
|
$template = File::Spec->catfile(_wrap_file_spec_tmpdir(), TEMPXXX); |
1457
|
|
|
|
|
|
|
|
1458
|
|
|
|
|
|
|
} |
1459
|
|
|
|
|
|
|
|
1460
|
|
|
|
|
|
|
} |
1461
|
|
|
|
|
|
|
|
1462
|
|
|
|
|
|
|
# Now add a suffix |
1463
|
30
|
|
|
|
|
95
|
$template .= $options{"SUFFIX"}; |
1464
|
|
|
|
|
|
|
|
1465
|
|
|
|
|
|
|
# Determine whether we should tell _gettemp to unlink the file |
1466
|
|
|
|
|
|
|
# On unix this is irrelevant and can be worked out after the file is |
1467
|
|
|
|
|
|
|
# opened (simply by unlinking the open filehandle). On Windows or VMS |
1468
|
|
|
|
|
|
|
# we have to indicate temporary-ness when we open the file. In general |
1469
|
|
|
|
|
|
|
# we only want a true temporary file if we are returning just the |
1470
|
|
|
|
|
|
|
# filehandle - if the user wants the filename they probably do not |
1471
|
|
|
|
|
|
|
# want the file to disappear as soon as they close it (which may be |
1472
|
|
|
|
|
|
|
# important if they want a child process to use the file) |
1473
|
|
|
|
|
|
|
# For this reason, tie unlink_on_close to the return context regardless |
1474
|
|
|
|
|
|
|
# of OS. |
1475
|
30
|
100
|
|
|
|
86
|
my $unlink_on_close = ( wantarray ? 0 : 1); |
1476
|
|
|
|
|
|
|
|
1477
|
|
|
|
|
|
|
# Create the file |
1478
|
30
|
|
|
|
|
57
|
my ($fh, $path, $errstr); |
1479
|
|
|
|
|
|
|
croak "Error in tempfile() using template $template: $errstr" |
1480
|
|
|
|
|
|
|
unless (($fh, $path) = _gettemp($template, |
1481
|
|
|
|
|
|
|
"open" => $options{OPEN}, |
1482
|
|
|
|
|
|
|
"mkdir" => 0, |
1483
|
|
|
|
|
|
|
"unlink_on_close" => $unlink_on_close, |
1484
|
|
|
|
|
|
|
"suffixlen" => length($options{SUFFIX}), |
1485
|
|
|
|
|
|
|
"ErrStr" => \$errstr, |
1486
|
|
|
|
|
|
|
"use_exlock" => $options{EXLOCK}, |
1487
|
|
|
|
|
|
|
"file_permissions" => $options{PERMS}, |
1488
|
30
|
50
|
|
|
|
256
|
) ); |
1489
|
|
|
|
|
|
|
|
1490
|
|
|
|
|
|
|
# Set up an exit handler that can do whatever is right for the |
1491
|
|
|
|
|
|
|
# system. This removes files at exit when requested explicitly or when |
1492
|
|
|
|
|
|
|
# system is asked to unlink_on_close but is unable to do so because |
1493
|
|
|
|
|
|
|
# of OS limitations. |
1494
|
|
|
|
|
|
|
# The latter should be achieved by using a tied filehandle. |
1495
|
|
|
|
|
|
|
# Do not check return status since this is all done with END blocks. |
1496
|
30
|
100
|
|
|
|
144
|
_deferred_unlink($fh, $path, 0) if $options{"UNLINK"}; |
1497
|
|
|
|
|
|
|
|
1498
|
|
|
|
|
|
|
# Return |
1499
|
30
|
100
|
|
|
|
76
|
if (wantarray()) { |
1500
|
|
|
|
|
|
|
|
1501
|
29
|
50
|
|
|
|
123
|
if ($options{'OPEN'}) { |
1502
|
29
|
|
|
|
|
165
|
return ($fh, $path); |
1503
|
|
|
|
|
|
|
} else { |
1504
|
0
|
|
|
|
|
0
|
return (undef, $path); |
1505
|
|
|
|
|
|
|
} |
1506
|
|
|
|
|
|
|
|
1507
|
|
|
|
|
|
|
} else { |
1508
|
|
|
|
|
|
|
|
1509
|
|
|
|
|
|
|
# Unlink the file. It is up to unlink0 to decide what to do with |
1510
|
|
|
|
|
|
|
# this (whether to unlink now or to defer until later) |
1511
|
1
|
50
|
|
|
|
5
|
unlink0($fh, $path) or croak "Error unlinking file $path using unlink0"; |
1512
|
|
|
|
|
|
|
|
1513
|
|
|
|
|
|
|
# Return just the filehandle. |
1514
|
1
|
|
|
|
|
7
|
return $fh; |
1515
|
|
|
|
|
|
|
} |
1516
|
|
|
|
|
|
|
|
1517
|
|
|
|
|
|
|
|
1518
|
|
|
|
|
|
|
} |
1519
|
|
|
|
|
|
|
|
1520
|
|
|
|
|
|
|
# On Windows under taint mode, File::Spec could suggest "C:\" as a tempdir |
1521
|
|
|
|
|
|
|
# which might not be writable. If that is the case, we fallback to a |
1522
|
|
|
|
|
|
|
# user directory. See https://rt.cpan.org/Ticket/Display.html?id=60340 |
1523
|
|
|
|
|
|
|
|
1524
|
|
|
|
|
|
|
{ |
1525
|
|
|
|
|
|
|
my ($alt_tmpdir, $checked); |
1526
|
|
|
|
|
|
|
|
1527
|
|
|
|
|
|
|
sub _wrap_file_spec_tmpdir { |
1528
|
26
|
50
|
33
|
26
|
|
4117
|
return File::Spec->tmpdir unless $^O eq "MSWin32" && ${^TAINT}; |
1529
|
|
|
|
|
|
|
|
1530
|
0
|
0
|
|
|
|
0
|
if ( $checked ) { |
1531
|
0
|
0
|
|
|
|
0
|
return $alt_tmpdir ? $alt_tmpdir : File::Spec->tmpdir; |
1532
|
|
|
|
|
|
|
} |
1533
|
|
|
|
|
|
|
|
1534
|
|
|
|
|
|
|
# probe what File::Spec gives and find a fallback |
1535
|
0
|
|
|
|
|
0
|
my $xxpath = _replace_XX( "X" x 10, 0 ); |
1536
|
|
|
|
|
|
|
|
1537
|
|
|
|
|
|
|
# First, see if File::Spec->tmpdir is writable |
1538
|
0
|
|
|
|
|
0
|
my $tmpdir = File::Spec->tmpdir; |
1539
|
0
|
|
|
|
|
0
|
my $testpath = File::Spec->catdir( $tmpdir, $xxpath ); |
1540
|
0
|
0
|
|
|
|
0
|
if (mkdir( $testpath, 0700) ) { |
1541
|
0
|
|
|
|
|
0
|
$checked = 1; |
1542
|
0
|
|
|
|
|
0
|
rmdir $testpath; |
1543
|
0
|
|
|
|
|
0
|
return $tmpdir; |
1544
|
|
|
|
|
|
|
} |
1545
|
|
|
|
|
|
|
|
1546
|
|
|
|
|
|
|
# Next, see if CSIDL_LOCAL_APPDATA is writable |
1547
|
0
|
|
|
|
|
0
|
require Win32; |
1548
|
0
|
|
|
|
|
0
|
my $local_app = File::Spec->catdir( |
1549
|
|
|
|
|
|
|
Win32::GetFolderPath( Win32::CSIDL_LOCAL_APPDATA() ), 'Temp' |
1550
|
|
|
|
|
|
|
); |
1551
|
0
|
|
|
|
|
0
|
$testpath = File::Spec->catdir( $local_app, $xxpath ); |
1552
|
0
|
0
|
0
|
|
|
0
|
if ( -e $local_app or mkdir( $local_app, 0700 ) ) { |
1553
|
0
|
0
|
|
|
|
0
|
if (mkdir( $testpath, 0700) ) { |
1554
|
0
|
|
|
|
|
0
|
$checked = 1; |
1555
|
0
|
|
|
|
|
0
|
rmdir $testpath; |
1556
|
0
|
|
|
|
|
0
|
return $alt_tmpdir = $local_app; |
1557
|
|
|
|
|
|
|
} |
1558
|
|
|
|
|
|
|
} |
1559
|
|
|
|
|
|
|
|
1560
|
|
|
|
|
|
|
# Can't find something writable |
1561
|
0
|
|
|
|
|
0
|
croak << "HERE"; |
1562
|
|
|
|
|
|
|
Couldn't find a writable temp directory in taint mode. Tried: |
1563
|
|
|
|
|
|
|
$tmpdir |
1564
|
|
|
|
|
|
|
$local_app |
1565
|
|
|
|
|
|
|
|
1566
|
|
|
|
|
|
|
Try setting and untainting the TMPDIR environment variable. |
1567
|
|
|
|
|
|
|
HERE |
1568
|
|
|
|
|
|
|
|
1569
|
|
|
|
|
|
|
} |
1570
|
|
|
|
|
|
|
} |
1571
|
|
|
|
|
|
|
|
1572
|
|
|
|
|
|
|
#pod =item B |
1573
|
|
|
|
|
|
|
#pod |
1574
|
|
|
|
|
|
|
#pod This is the recommended interface for creation of temporary |
1575
|
|
|
|
|
|
|
#pod directories. By default the directory will not be removed on exit |
1576
|
|
|
|
|
|
|
#pod (that is, it won't be temporary; this behaviour can not be changed |
1577
|
|
|
|
|
|
|
#pod because of issues with backwards compatibility). To enable removal |
1578
|
|
|
|
|
|
|
#pod either use the CLEANUP option which will trigger removal on program |
1579
|
|
|
|
|
|
|
#pod exit, or consider using the "newdir" method in the object interface which |
1580
|
|
|
|
|
|
|
#pod will allow the directory to be cleaned up when the object goes out of |
1581
|
|
|
|
|
|
|
#pod scope. |
1582
|
|
|
|
|
|
|
#pod |
1583
|
|
|
|
|
|
|
#pod The behaviour of the function depends on the arguments: |
1584
|
|
|
|
|
|
|
#pod |
1585
|
|
|
|
|
|
|
#pod $tempdir = tempdir(); |
1586
|
|
|
|
|
|
|
#pod |
1587
|
|
|
|
|
|
|
#pod Create a directory in tmpdir() (see L). |
1588
|
|
|
|
|
|
|
#pod |
1589
|
|
|
|
|
|
|
#pod $tempdir = tempdir( $template ); |
1590
|
|
|
|
|
|
|
#pod |
1591
|
|
|
|
|
|
|
#pod Create a directory from the supplied template. This template is |
1592
|
|
|
|
|
|
|
#pod similar to that described for tempfile(). `X' characters at the end |
1593
|
|
|
|
|
|
|
#pod of the template are replaced with random letters to construct the |
1594
|
|
|
|
|
|
|
#pod directory name. At least four `X' characters must be in the template. |
1595
|
|
|
|
|
|
|
#pod |
1596
|
|
|
|
|
|
|
#pod $tempdir = tempdir ( DIR => $dir ); |
1597
|
|
|
|
|
|
|
#pod |
1598
|
|
|
|
|
|
|
#pod Specifies the directory to use for the temporary directory. |
1599
|
|
|
|
|
|
|
#pod The temporary directory name is derived from an internal template. |
1600
|
|
|
|
|
|
|
#pod |
1601
|
|
|
|
|
|
|
#pod $tempdir = tempdir ( $template, DIR => $dir ); |
1602
|
|
|
|
|
|
|
#pod |
1603
|
|
|
|
|
|
|
#pod Prepend the supplied directory name to the template. The template |
1604
|
|
|
|
|
|
|
#pod should not include parent directory specifications itself. Any parent |
1605
|
|
|
|
|
|
|
#pod directory specifications are removed from the template before |
1606
|
|
|
|
|
|
|
#pod prepending the supplied directory. |
1607
|
|
|
|
|
|
|
#pod |
1608
|
|
|
|
|
|
|
#pod $tempdir = tempdir ( $template, TMPDIR => 1 ); |
1609
|
|
|
|
|
|
|
#pod |
1610
|
|
|
|
|
|
|
#pod Using the supplied template, create the temporary directory in |
1611
|
|
|
|
|
|
|
#pod a standard location for temporary files. Equivalent to doing |
1612
|
|
|
|
|
|
|
#pod |
1613
|
|
|
|
|
|
|
#pod $tempdir = tempdir ( $template, DIR => File::Spec->tmpdir); |
1614
|
|
|
|
|
|
|
#pod |
1615
|
|
|
|
|
|
|
#pod but shorter. Parent directory specifications are stripped from the |
1616
|
|
|
|
|
|
|
#pod template itself. The C option is ignored if C is set |
1617
|
|
|
|
|
|
|
#pod explicitly. Additionally, C is implied if neither a template |
1618
|
|
|
|
|
|
|
#pod nor a directory are supplied. |
1619
|
|
|
|
|
|
|
#pod |
1620
|
|
|
|
|
|
|
#pod $tempdir = tempdir( $template, CLEANUP => 1); |
1621
|
|
|
|
|
|
|
#pod |
1622
|
|
|
|
|
|
|
#pod Create a temporary directory using the supplied template, but |
1623
|
|
|
|
|
|
|
#pod attempt to remove it (and all files inside it) when the program |
1624
|
|
|
|
|
|
|
#pod exits. Note that an attempt will be made to remove all files from |
1625
|
|
|
|
|
|
|
#pod the directory even if they were not created by this module (otherwise |
1626
|
|
|
|
|
|
|
#pod why ask to clean it up?). The directory removal is made with |
1627
|
|
|
|
|
|
|
#pod the rmtree() function from the L module. |
1628
|
|
|
|
|
|
|
#pod Of course, if the template is not specified, the temporary directory |
1629
|
|
|
|
|
|
|
#pod will be created in tmpdir() and will also be removed at program exit. |
1630
|
|
|
|
|
|
|
#pod |
1631
|
|
|
|
|
|
|
#pod Will croak() if there is an error. |
1632
|
|
|
|
|
|
|
#pod |
1633
|
|
|
|
|
|
|
#pod Current API available since 0.05. |
1634
|
|
|
|
|
|
|
#pod |
1635
|
|
|
|
|
|
|
#pod =cut |
1636
|
|
|
|
|
|
|
|
1637
|
|
|
|
|
|
|
# ' |
1638
|
|
|
|
|
|
|
|
1639
|
|
|
|
|
|
|
sub tempdir { |
1640
|
10
|
100
|
100
|
10
|
1
|
3522
|
if ( @_ && $_[0] eq 'File::Temp' ) { |
1641
|
1
|
|
|
|
|
94
|
croak "'tempdir' can't be called as a method"; |
1642
|
|
|
|
|
|
|
} |
1643
|
|
|
|
|
|
|
|
1644
|
|
|
|
|
|
|
# Can not check for argument count since we can have any |
1645
|
|
|
|
|
|
|
# number of args |
1646
|
|
|
|
|
|
|
|
1647
|
|
|
|
|
|
|
# Default options |
1648
|
9
|
|
|
|
|
32
|
my %options = ( |
1649
|
|
|
|
|
|
|
"CLEANUP" => 0, # Remove directory on exit |
1650
|
|
|
|
|
|
|
"DIR" => '', # Root directory |
1651
|
|
|
|
|
|
|
"TMPDIR" => 0, # Use tempdir with template |
1652
|
|
|
|
|
|
|
); |
1653
|
|
|
|
|
|
|
|
1654
|
|
|
|
|
|
|
# Check to see whether we have an odd or even number of arguments |
1655
|
9
|
|
|
|
|
20
|
my ($maybe_template, $args) = _parse_args(@_); |
1656
|
9
|
100
|
|
|
|
34
|
my $template = @$maybe_template ? $maybe_template->[0] : undef; |
1657
|
|
|
|
|
|
|
|
1658
|
|
|
|
|
|
|
# Read the options and merge with defaults |
1659
|
9
|
|
|
|
|
32
|
%options = (%options, %$args); |
1660
|
|
|
|
|
|
|
|
1661
|
|
|
|
|
|
|
# Modify or generate the template |
1662
|
|
|
|
|
|
|
|
1663
|
|
|
|
|
|
|
# Deal with the DIR and TMPDIR options |
1664
|
9
|
100
|
|
|
|
23
|
if (defined $template) { |
1665
|
|
|
|
|
|
|
|
1666
|
|
|
|
|
|
|
# Need to strip directory path if using DIR or TMPDIR |
1667
|
5
|
100
|
66
|
|
|
26
|
if ($options{'TMPDIR'} || $options{'DIR'}) { |
1668
|
|
|
|
|
|
|
|
1669
|
|
|
|
|
|
|
# Strip parent directory from the filename |
1670
|
|
|
|
|
|
|
# |
1671
|
|
|
|
|
|
|
# There is no filename at the end |
1672
|
3
|
50
|
|
|
|
11
|
$template = VMS::Filespec::vmspath($template) if $^O eq 'VMS'; |
1673
|
3
|
|
|
|
|
18
|
my ($volume, $directories, undef) = File::Spec->splitpath( $template, 1); |
1674
|
|
|
|
|
|
|
|
1675
|
|
|
|
|
|
|
# Last directory is then our template |
1676
|
3
|
|
|
|
|
16
|
$template = (File::Spec->splitdir($directories))[-1]; |
1677
|
|
|
|
|
|
|
|
1678
|
|
|
|
|
|
|
# Prepend the supplied directory or temp dir |
1679
|
3
|
50
|
|
|
|
7
|
if ($options{"DIR"}) { |
|
|
0
|
|
|
|
|
|
1680
|
|
|
|
|
|
|
|
1681
|
3
|
|
|
|
|
18
|
$template = File::Spec->catdir($options{"DIR"}, $template); |
1682
|
|
|
|
|
|
|
|
1683
|
|
|
|
|
|
|
} elsif ($options{TMPDIR}) { |
1684
|
|
|
|
|
|
|
|
1685
|
|
|
|
|
|
|
# Prepend tmpdir |
1686
|
0
|
|
|
|
|
0
|
$template = File::Spec->catdir(_wrap_file_spec_tmpdir(), $template); |
1687
|
|
|
|
|
|
|
|
1688
|
|
|
|
|
|
|
} |
1689
|
|
|
|
|
|
|
|
1690
|
|
|
|
|
|
|
} |
1691
|
|
|
|
|
|
|
|
1692
|
|
|
|
|
|
|
} else { |
1693
|
|
|
|
|
|
|
|
1694
|
4
|
100
|
|
|
|
13
|
if ($options{"DIR"}) { |
1695
|
|
|
|
|
|
|
|
1696
|
1
|
|
|
|
|
11
|
$template = File::Spec->catdir($options{"DIR"}, TEMPXXX); |
1697
|
|
|
|
|
|
|
|
1698
|
|
|
|
|
|
|
} else { |
1699
|
|
|
|
|
|
|
|
1700
|
3
|
|
|
|
|
9
|
$template = File::Spec->catdir(_wrap_file_spec_tmpdir(), TEMPXXX); |
1701
|
|
|
|
|
|
|
|
1702
|
|
|
|
|
|
|
} |
1703
|
|
|
|
|
|
|
|
1704
|
|
|
|
|
|
|
} |
1705
|
|
|
|
|
|
|
|
1706
|
|
|
|
|
|
|
# Create the directory |
1707
|
9
|
|
|
|
|
19
|
my $tempdir; |
1708
|
9
|
|
|
|
|
13
|
my $suffixlen = 0; |
1709
|
9
|
50
|
|
|
|
24
|
if ($^O eq 'VMS') { # dir names can end in delimiters |
1710
|
0
|
|
|
|
|
0
|
$template =~ m/([\.\]:>]+)$/; |
1711
|
0
|
|
|
|
|
0
|
$suffixlen = length($1); |
1712
|
|
|
|
|
|
|
} |
1713
|
9
|
50
|
33
|
|
|
25
|
if ( ($^O eq 'MacOS') && (substr($template, -1) eq ':') ) { |
1714
|
|
|
|
|
|
|
# dir name has a trailing ':' |
1715
|
0
|
|
|
|
|
0
|
++$suffixlen; |
1716
|
|
|
|
|
|
|
} |
1717
|
|
|
|
|
|
|
|
1718
|
9
|
|
|
|
|
13
|
my $errstr; |
1719
|
9
|
50
|
|
|
|
26
|
croak "Error in tempdir() using $template: $errstr" |
1720
|
|
|
|
|
|
|
unless ((undef, $tempdir) = _gettemp($template, |
1721
|
|
|
|
|
|
|
"open" => 0, |
1722
|
|
|
|
|
|
|
"mkdir"=> 1 , |
1723
|
|
|
|
|
|
|
"suffixlen" => $suffixlen, |
1724
|
|
|
|
|
|
|
"ErrStr" => \$errstr, |
1725
|
|
|
|
|
|
|
) ); |
1726
|
|
|
|
|
|
|
|
1727
|
|
|
|
|
|
|
# Install exit handler; must be dynamic to get lexical |
1728
|
9
|
100
|
66
|
|
|
70
|
if ( $options{'CLEANUP'} && -d $tempdir) { |
1729
|
4
|
|
|
|
|
13
|
_deferred_unlink(undef, $tempdir, 1); |
1730
|
|
|
|
|
|
|
} |
1731
|
|
|
|
|
|
|
|
1732
|
|
|
|
|
|
|
# Return the dir name |
1733
|
9
|
|
|
|
|
38
|
return $tempdir; |
1734
|
|
|
|
|
|
|
|
1735
|
|
|
|
|
|
|
} |
1736
|
|
|
|
|
|
|
|
1737
|
|
|
|
|
|
|
#pod =back |
1738
|
|
|
|
|
|
|
#pod |
1739
|
|
|
|
|
|
|
#pod =head1 MKTEMP FUNCTIONS |
1740
|
|
|
|
|
|
|
#pod |
1741
|
|
|
|
|
|
|
#pod The following functions are Perl implementations of the |
1742
|
|
|
|
|
|
|
#pod mktemp() family of temp file generation system calls. |
1743
|
|
|
|
|
|
|
#pod |
1744
|
|
|
|
|
|
|
#pod =over 4 |
1745
|
|
|
|
|
|
|
#pod |
1746
|
|
|
|
|
|
|
#pod =item B |
1747
|
|
|
|
|
|
|
#pod |
1748
|
|
|
|
|
|
|
#pod Given a template, returns a filehandle to the temporary file and the name |
1749
|
|
|
|
|
|
|
#pod of the file. |
1750
|
|
|
|
|
|
|
#pod |
1751
|
|
|
|
|
|
|
#pod ($fh, $name) = mkstemp( $template ); |
1752
|
|
|
|
|
|
|
#pod |
1753
|
|
|
|
|
|
|
#pod In scalar context, just the filehandle is returned. |
1754
|
|
|
|
|
|
|
#pod |
1755
|
|
|
|
|
|
|
#pod The template may be any filename with some number of X's appended |
1756
|
|
|
|
|
|
|
#pod to it, for example F. The trailing X's are replaced |
1757
|
|
|
|
|
|
|
#pod with unique alphanumeric combinations. |
1758
|
|
|
|
|
|
|
#pod |
1759
|
|
|
|
|
|
|
#pod Will croak() if there is an error. |
1760
|
|
|
|
|
|
|
#pod |
1761
|
|
|
|
|
|
|
#pod Current API available since 0.05. |
1762
|
|
|
|
|
|
|
#pod |
1763
|
|
|
|
|
|
|
#pod =cut |
1764
|
|
|
|
|
|
|
|
1765
|
|
|
|
|
|
|
|
1766
|
|
|
|
|
|
|
|
1767
|
|
|
|
|
|
|
sub mkstemp { |
1768
|
|
|
|
|
|
|
|
1769
|
3
|
50
|
|
3
|
1
|
16
|
croak "Usage: mkstemp(template)" |
1770
|
|
|
|
|
|
|
if scalar(@_) != 1; |
1771
|
|
|
|
|
|
|
|
1772
|
3
|
|
|
|
|
5
|
my $template = shift; |
1773
|
|
|
|
|
|
|
|
1774
|
3
|
|
|
|
|
4
|
my ($fh, $path, $errstr); |
1775
|
3
|
50
|
|
|
|
12
|
croak "Error in mkstemp using $template: $errstr" |
1776
|
|
|
|
|
|
|
unless (($fh, $path) = _gettemp($template, |
1777
|
|
|
|
|
|
|
"open" => 1, |
1778
|
|
|
|
|
|
|
"mkdir"=> 0 , |
1779
|
|
|
|
|
|
|
"suffixlen" => 0, |
1780
|
|
|
|
|
|
|
"ErrStr" => \$errstr, |
1781
|
|
|
|
|
|
|
) ); |
1782
|
|
|
|
|
|
|
|
1783
|
3
|
50
|
|
|
|
8
|
if (wantarray()) { |
1784
|
3
|
|
|
|
|
11
|
return ($fh, $path); |
1785
|
|
|
|
|
|
|
} else { |
1786
|
0
|
|
|
|
|
0
|
return $fh; |
1787
|
|
|
|
|
|
|
} |
1788
|
|
|
|
|
|
|
|
1789
|
|
|
|
|
|
|
} |
1790
|
|
|
|
|
|
|
|
1791
|
|
|
|
|
|
|
|
1792
|
|
|
|
|
|
|
#pod =item B |
1793
|
|
|
|
|
|
|
#pod |
1794
|
|
|
|
|
|
|
#pod Similar to mkstemp(), except that an extra argument can be supplied |
1795
|
|
|
|
|
|
|
#pod with a suffix to be appended to the template. |
1796
|
|
|
|
|
|
|
#pod |
1797
|
|
|
|
|
|
|
#pod ($fh, $name) = mkstemps( $template, $suffix ); |
1798
|
|
|
|
|
|
|
#pod |
1799
|
|
|
|
|
|
|
#pod For example a template of C and suffix of C<.dat> |
1800
|
|
|
|
|
|
|
#pod would generate a file similar to F. |
1801
|
|
|
|
|
|
|
#pod |
1802
|
|
|
|
|
|
|
#pod Returns just the filehandle alone when called in scalar context. |
1803
|
|
|
|
|
|
|
#pod |
1804
|
|
|
|
|
|
|
#pod Will croak() if there is an error. |
1805
|
|
|
|
|
|
|
#pod |
1806
|
|
|
|
|
|
|
#pod Current API available since 0.05. |
1807
|
|
|
|
|
|
|
#pod |
1808
|
|
|
|
|
|
|
#pod =cut |
1809
|
|
|
|
|
|
|
|
1810
|
|
|
|
|
|
|
sub mkstemps { |
1811
|
|
|
|
|
|
|
|
1812
|
1
|
50
|
|
1
|
1
|
257
|
croak "Usage: mkstemps(template, suffix)" |
1813
|
|
|
|
|
|
|
if scalar(@_) != 2; |
1814
|
|
|
|
|
|
|
|
1815
|
|
|
|
|
|
|
|
1816
|
1
|
|
|
|
|
2
|
my $template = shift; |
1817
|
1
|
|
|
|
|
2
|
my $suffix = shift; |
1818
|
|
|
|
|
|
|
|
1819
|
1
|
|
|
|
|
3
|
$template .= $suffix; |
1820
|
|
|
|
|
|
|
|
1821
|
1
|
|
|
|
|
1
|
my ($fh, $path, $errstr); |
1822
|
1
|
50
|
|
|
|
5
|
croak "Error in mkstemps using $template: $errstr" |
1823
|
|
|
|
|
|
|
unless (($fh, $path) = _gettemp($template, |
1824
|
|
|
|
|
|
|
"open" => 1, |
1825
|
|
|
|
|
|
|
"mkdir"=> 0 , |
1826
|
|
|
|
|
|
|
"suffixlen" => length($suffix), |
1827
|
|
|
|
|
|
|
"ErrStr" => \$errstr, |
1828
|
|
|
|
|
|
|
) ); |
1829
|
|
|
|
|
|
|
|
1830
|
1
|
50
|
|
|
|
4
|
if (wantarray()) { |
1831
|
1
|
|
|
|
|
5
|
return ($fh, $path); |
1832
|
|
|
|
|
|
|
} else { |
1833
|
0
|
|
|
|
|
0
|
return $fh; |
1834
|
|
|
|
|
|
|
} |
1835
|
|
|
|
|
|
|
|
1836
|
|
|
|
|
|
|
} |
1837
|
|
|
|
|
|
|
|
1838
|
|
|
|
|
|
|
#pod =item B |
1839
|
|
|
|
|
|
|
#pod |
1840
|
|
|
|
|
|
|
#pod Create a directory from a template. The template must end in |
1841
|
|
|
|
|
|
|
#pod X's that are replaced by the routine. |
1842
|
|
|
|
|
|
|
#pod |
1843
|
|
|
|
|
|
|
#pod $tmpdir_name = mkdtemp($template); |
1844
|
|
|
|
|
|
|
#pod |
1845
|
|
|
|
|
|
|
#pod Returns the name of the temporary directory created. |
1846
|
|
|
|
|
|
|
#pod |
1847
|
|
|
|
|
|
|
#pod Directory must be removed by the caller. |
1848
|
|
|
|
|
|
|
#pod |
1849
|
|
|
|
|
|
|
#pod Will croak() if there is an error. |
1850
|
|
|
|
|
|
|
#pod |
1851
|
|
|
|
|
|
|
#pod Current API available since 0.05. |
1852
|
|
|
|
|
|
|
#pod |
1853
|
|
|
|
|
|
|
#pod =cut |
1854
|
|
|
|
|
|
|
|
1855
|
|
|
|
|
|
|
#' # for emacs |
1856
|
|
|
|
|
|
|
|
1857
|
|
|
|
|
|
|
sub mkdtemp { |
1858
|
|
|
|
|
|
|
|
1859
|
1
|
50
|
|
1
|
1
|
9
|
croak "Usage: mkdtemp(template)" |
1860
|
|
|
|
|
|
|
if scalar(@_) != 1; |
1861
|
|
|
|
|
|
|
|
1862
|
1
|
|
|
|
|
2
|
my $template = shift; |
1863
|
1
|
|
|
|
|
2
|
my $suffixlen = 0; |
1864
|
1
|
50
|
|
|
|
4
|
if ($^O eq 'VMS') { # dir names can end in delimiters |
1865
|
0
|
|
|
|
|
0
|
$template =~ m/([\.\]:>]+)$/; |
1866
|
0
|
|
|
|
|
0
|
$suffixlen = length($1); |
1867
|
|
|
|
|
|
|
} |
1868
|
1
|
50
|
33
|
|
|
9
|
if ( ($^O eq 'MacOS') && (substr($template, -1) eq ':') ) { |
1869
|
|
|
|
|
|
|
# dir name has a trailing ':' |
1870
|
0
|
|
|
|
|
0
|
++$suffixlen; |
1871
|
|
|
|
|
|
|
} |
1872
|
1
|
|
|
|
|
3
|
my ($junk, $tmpdir, $errstr); |
1873
|
1
|
50
|
|
|
|
3
|
croak "Error creating temp directory from template $template\: $errstr" |
1874
|
|
|
|
|
|
|
unless (($junk, $tmpdir) = _gettemp($template, |
1875
|
|
|
|
|
|
|
"open" => 0, |
1876
|
|
|
|
|
|
|
"mkdir"=> 1 , |
1877
|
|
|
|
|
|
|
"suffixlen" => $suffixlen, |
1878
|
|
|
|
|
|
|
"ErrStr" => \$errstr, |
1879
|
|
|
|
|
|
|
) ); |
1880
|
|
|
|
|
|
|
|
1881
|
1
|
|
|
|
|
4
|
return $tmpdir; |
1882
|
|
|
|
|
|
|
|
1883
|
|
|
|
|
|
|
} |
1884
|
|
|
|
|
|
|
|
1885
|
|
|
|
|
|
|
#pod =item B |
1886
|
|
|
|
|
|
|
#pod |
1887
|
|
|
|
|
|
|
#pod Returns a valid temporary filename but does not guarantee |
1888
|
|
|
|
|
|
|
#pod that the file will not be opened by someone else. |
1889
|
|
|
|
|
|
|
#pod |
1890
|
|
|
|
|
|
|
#pod $unopened_file = mktemp($template); |
1891
|
|
|
|
|
|
|
#pod |
1892
|
|
|
|
|
|
|
#pod Template is the same as that required by mkstemp(). |
1893
|
|
|
|
|
|
|
#pod |
1894
|
|
|
|
|
|
|
#pod Will croak() if there is an error. |
1895
|
|
|
|
|
|
|
#pod |
1896
|
|
|
|
|
|
|
#pod Current API available since 0.05. |
1897
|
|
|
|
|
|
|
#pod |
1898
|
|
|
|
|
|
|
#pod =cut |
1899
|
|
|
|
|
|
|
|
1900
|
|
|
|
|
|
|
sub mktemp { |
1901
|
|
|
|
|
|
|
|
1902
|
2
|
50
|
|
2
|
1
|
11
|
croak "Usage: mktemp(template)" |
1903
|
|
|
|
|
|
|
if scalar(@_) != 1; |
1904
|
|
|
|
|
|
|
|
1905
|
2
|
|
|
|
|
4
|
my $template = shift; |
1906
|
|
|
|
|
|
|
|
1907
|
2
|
|
|
|
|
4
|
my ($tmpname, $junk, $errstr); |
1908
|
2
|
50
|
|
|
|
9
|
croak "Error getting name to temp file from template $template: $errstr" |
1909
|
|
|
|
|
|
|
unless (($junk, $tmpname) = _gettemp($template, |
1910
|
|
|
|
|
|
|
"open" => 0, |
1911
|
|
|
|
|
|
|
"mkdir"=> 0 , |
1912
|
|
|
|
|
|
|
"suffixlen" => 0, |
1913
|
|
|
|
|
|
|
"ErrStr" => \$errstr, |
1914
|
|
|
|
|
|
|
) ); |
1915
|
|
|
|
|
|
|
|
1916
|
2
|
|
|
|
|
10
|
return $tmpname; |
1917
|
|
|
|
|
|
|
} |
1918
|
|
|
|
|
|
|
|
1919
|
|
|
|
|
|
|
#pod =back |
1920
|
|
|
|
|
|
|
#pod |
1921
|
|
|
|
|
|
|
#pod =head1 POSIX FUNCTIONS |
1922
|
|
|
|
|
|
|
#pod |
1923
|
|
|
|
|
|
|
#pod This section describes the re-implementation of the tmpnam() |
1924
|
|
|
|
|
|
|
#pod and tmpfile() functions described in L |
1925
|
|
|
|
|
|
|
#pod using the mkstemp() from this module. |
1926
|
|
|
|
|
|
|
#pod |
1927
|
|
|
|
|
|
|
#pod Unlike the L implementations, the directory used |
1928
|
|
|
|
|
|
|
#pod for the temporary file is not specified in a system include |
1929
|
|
|
|
|
|
|
#pod file (C) but simply depends on the choice of tmpdir() |
1930
|
|
|
|
|
|
|
#pod returned by L. On some implementations this |
1931
|
|
|
|
|
|
|
#pod location can be set using the C environment variable, which |
1932
|
|
|
|
|
|
|
#pod may not be secure. |
1933
|
|
|
|
|
|
|
#pod If this is a problem, simply use mkstemp() and specify a template. |
1934
|
|
|
|
|
|
|
#pod |
1935
|
|
|
|
|
|
|
#pod =over 4 |
1936
|
|
|
|
|
|
|
#pod |
1937
|
|
|
|
|
|
|
#pod =item B |
1938
|
|
|
|
|
|
|
#pod |
1939
|
|
|
|
|
|
|
#pod When called in scalar context, returns the full name (including path) |
1940
|
|
|
|
|
|
|
#pod of a temporary file (uses mktemp()). The only check is that the file does |
1941
|
|
|
|
|
|
|
#pod not already exist, but there is no guarantee that that condition will |
1942
|
|
|
|
|
|
|
#pod continue to apply. |
1943
|
|
|
|
|
|
|
#pod |
1944
|
|
|
|
|
|
|
#pod $file = tmpnam(); |
1945
|
|
|
|
|
|
|
#pod |
1946
|
|
|
|
|
|
|
#pod When called in list context, a filehandle to the open file and |
1947
|
|
|
|
|
|
|
#pod a filename are returned. This is achieved by calling mkstemp() |
1948
|
|
|
|
|
|
|
#pod after constructing a suitable template. |
1949
|
|
|
|
|
|
|
#pod |
1950
|
|
|
|
|
|
|
#pod ($fh, $file) = tmpnam(); |
1951
|
|
|
|
|
|
|
#pod |
1952
|
|
|
|
|
|
|
#pod If possible, this form should be used to prevent possible |
1953
|
|
|
|
|
|
|
#pod race conditions. |
1954
|
|
|
|
|
|
|
#pod |
1955
|
|
|
|
|
|
|
#pod See L for information on the choice of temporary |
1956
|
|
|
|
|
|
|
#pod directory for a particular operating system. |
1957
|
|
|
|
|
|
|
#pod |
1958
|
|
|
|
|
|
|
#pod Will croak() if there is an error. |
1959
|
|
|
|
|
|
|
#pod |
1960
|
|
|
|
|
|
|
#pod Current API available since 0.05. |
1961
|
|
|
|
|
|
|
#pod |
1962
|
|
|
|
|
|
|
#pod =cut |
1963
|
|
|
|
|
|
|
|
1964
|
|
|
|
|
|
|
sub tmpnam { |
1965
|
|
|
|
|
|
|
|
1966
|
|
|
|
|
|
|
# Retrieve the temporary directory name |
1967
|
3
|
|
|
3
|
1
|
832
|
my $tmpdir = _wrap_file_spec_tmpdir(); |
1968
|
|
|
|
|
|
|
|
1969
|
|
|
|
|
|
|
# XXX I don't know under what circumstances this occurs, -- xdg 2016-04-02 |
1970
|
3
|
50
|
|
|
|
9
|
croak "Error temporary directory is not writable" |
1971
|
|
|
|
|
|
|
if $tmpdir eq ''; |
1972
|
|
|
|
|
|
|
|
1973
|
|
|
|
|
|
|
# Use a ten character template and append to tmpdir |
1974
|
3
|
|
|
|
|
26
|
my $template = File::Spec->catfile($tmpdir, TEMPXXX); |
1975
|
|
|
|
|
|
|
|
1976
|
3
|
100
|
|
|
|
9
|
if (wantarray() ) { |
1977
|
2
|
|
|
|
|
6
|
return mkstemp($template); |
1978
|
|
|
|
|
|
|
} else { |
1979
|
1
|
|
|
|
|
5
|
return mktemp($template); |
1980
|
|
|
|
|
|
|
} |
1981
|
|
|
|
|
|
|
|
1982
|
|
|
|
|
|
|
} |
1983
|
|
|
|
|
|
|
|
1984
|
|
|
|
|
|
|
#pod =item B |
1985
|
|
|
|
|
|
|
#pod |
1986
|
|
|
|
|
|
|
#pod Returns the filehandle of a temporary file. |
1987
|
|
|
|
|
|
|
#pod |
1988
|
|
|
|
|
|
|
#pod $fh = tmpfile(); |
1989
|
|
|
|
|
|
|
#pod |
1990
|
|
|
|
|
|
|
#pod The file is removed when the filehandle is closed or when the program |
1991
|
|
|
|
|
|
|
#pod exits. No access to the filename is provided. |
1992
|
|
|
|
|
|
|
#pod |
1993
|
|
|
|
|
|
|
#pod If the temporary file can not be created undef is returned. |
1994
|
|
|
|
|
|
|
#pod Currently this command will probably not work when the temporary |
1995
|
|
|
|
|
|
|
#pod directory is on an NFS file system. |
1996
|
|
|
|
|
|
|
#pod |
1997
|
|
|
|
|
|
|
#pod Will croak() if there is an error. |
1998
|
|
|
|
|
|
|
#pod |
1999
|
|
|
|
|
|
|
#pod Available since 0.05. |
2000
|
|
|
|
|
|
|
#pod |
2001
|
|
|
|
|
|
|
#pod Returning undef if unable to create file added in 0.12. |
2002
|
|
|
|
|
|
|
#pod |
2003
|
|
|
|
|
|
|
#pod =cut |
2004
|
|
|
|
|
|
|
|
2005
|
|
|
|
|
|
|
sub tmpfile { |
2006
|
|
|
|
|
|
|
|
2007
|
|
|
|
|
|
|
# Simply call tmpnam() in a list context |
2008
|
1
|
|
|
1
|
1
|
248
|
my ($fh, $file) = tmpnam(); |
2009
|
|
|
|
|
|
|
|
2010
|
|
|
|
|
|
|
# Make sure file is removed when filehandle is closed |
2011
|
|
|
|
|
|
|
# This will fail on NFS |
2012
|
1
|
50
|
|
|
|
30
|
unlink0($fh, $file) |
2013
|
|
|
|
|
|
|
or return undef; |
2014
|
|
|
|
|
|
|
|
2015
|
1
|
|
|
|
|
46
|
return $fh; |
2016
|
|
|
|
|
|
|
|
2017
|
|
|
|
|
|
|
} |
2018
|
|
|
|
|
|
|
|
2019
|
|
|
|
|
|
|
#pod =back |
2020
|
|
|
|
|
|
|
#pod |
2021
|
|
|
|
|
|
|
#pod =head1 ADDITIONAL FUNCTIONS |
2022
|
|
|
|
|
|
|
#pod |
2023
|
|
|
|
|
|
|
#pod These functions are provided for backwards compatibility |
2024
|
|
|
|
|
|
|
#pod with common tempfile generation C library functions. |
2025
|
|
|
|
|
|
|
#pod |
2026
|
|
|
|
|
|
|
#pod They are not exported and must be addressed using the full package |
2027
|
|
|
|
|
|
|
#pod name. |
2028
|
|
|
|
|
|
|
#pod |
2029
|
|
|
|
|
|
|
#pod =over 4 |
2030
|
|
|
|
|
|
|
#pod |
2031
|
|
|
|
|
|
|
#pod =item B |
2032
|
|
|
|
|
|
|
#pod |
2033
|
|
|
|
|
|
|
#pod Return the name of a temporary file in the specified directory |
2034
|
|
|
|
|
|
|
#pod using a prefix. The file is guaranteed not to exist at the time |
2035
|
|
|
|
|
|
|
#pod the function was called, but such guarantees are good for one |
2036
|
|
|
|
|
|
|
#pod clock tick only. Always use the proper form of C |
2037
|
|
|
|
|
|
|
#pod with C if you must open such a filename. |
2038
|
|
|
|
|
|
|
#pod |
2039
|
|
|
|
|
|
|
#pod $filename = File::Temp::tempnam( $dir, $prefix ); |
2040
|
|
|
|
|
|
|
#pod |
2041
|
|
|
|
|
|
|
#pod Equivalent to running mktemp() with $dir/$prefixXXXXXXXX |
2042
|
|
|
|
|
|
|
#pod (using unix file convention as an example) |
2043
|
|
|
|
|
|
|
#pod |
2044
|
|
|
|
|
|
|
#pod Because this function uses mktemp(), it can suffer from race conditions. |
2045
|
|
|
|
|
|
|
#pod |
2046
|
|
|
|
|
|
|
#pod Will croak() if there is an error. |
2047
|
|
|
|
|
|
|
#pod |
2048
|
|
|
|
|
|
|
#pod Current API available since 0.05. |
2049
|
|
|
|
|
|
|
#pod |
2050
|
|
|
|
|
|
|
#pod =cut |
2051
|
|
|
|
|
|
|
|
2052
|
|
|
|
|
|
|
sub tempnam { |
2053
|
|
|
|
|
|
|
|
2054
|
0
|
0
|
|
0
|
1
|
0
|
croak 'Usage tempnam($dir, $prefix)' unless scalar(@_) == 2; |
2055
|
|
|
|
|
|
|
|
2056
|
0
|
|
|
|
|
0
|
my ($dir, $prefix) = @_; |
2057
|
|
|
|
|
|
|
|
2058
|
|
|
|
|
|
|
# Add a string to the prefix |
2059
|
0
|
|
|
|
|
0
|
$prefix .= 'XXXXXXXX'; |
2060
|
|
|
|
|
|
|
|
2061
|
|
|
|
|
|
|
# Concatenate the directory to the file |
2062
|
0
|
|
|
|
|
0
|
my $template = File::Spec->catfile($dir, $prefix); |
2063
|
|
|
|
|
|
|
|
2064
|
0
|
|
|
|
|
0
|
return mktemp($template); |
2065
|
|
|
|
|
|
|
|
2066
|
|
|
|
|
|
|
} |
2067
|
|
|
|
|
|
|
|
2068
|
|
|
|
|
|
|
#pod =back |
2069
|
|
|
|
|
|
|
#pod |
2070
|
|
|
|
|
|
|
#pod =head1 UTILITY FUNCTIONS |
2071
|
|
|
|
|
|
|
#pod |
2072
|
|
|
|
|
|
|
#pod Useful functions for dealing with the filehandle and filename. |
2073
|
|
|
|
|
|
|
#pod |
2074
|
|
|
|
|
|
|
#pod =over 4 |
2075
|
|
|
|
|
|
|
#pod |
2076
|
|
|
|
|
|
|
#pod =item B |
2077
|
|
|
|
|
|
|
#pod |
2078
|
|
|
|
|
|
|
#pod Given an open filehandle and the associated filename, make a safe |
2079
|
|
|
|
|
|
|
#pod unlink. This is achieved by first checking that the filename and |
2080
|
|
|
|
|
|
|
#pod filehandle initially point to the same file and that the number of |
2081
|
|
|
|
|
|
|
#pod links to the file is 1 (all fields returned by stat() are compared). |
2082
|
|
|
|
|
|
|
#pod Then the filename is unlinked and the filehandle checked once again to |
2083
|
|
|
|
|
|
|
#pod verify that the number of links on that file is now 0. This is the |
2084
|
|
|
|
|
|
|
#pod closest you can come to making sure that the filename unlinked was the |
2085
|
|
|
|
|
|
|
#pod same as the file whose descriptor you hold. |
2086
|
|
|
|
|
|
|
#pod |
2087
|
|
|
|
|
|
|
#pod unlink0($fh, $path) |
2088
|
|
|
|
|
|
|
#pod or die "Error unlinking file $path safely"; |
2089
|
|
|
|
|
|
|
#pod |
2090
|
|
|
|
|
|
|
#pod Returns false on error but croaks() if there is a security |
2091
|
|
|
|
|
|
|
#pod anomaly. The filehandle is not closed since on some occasions this is |
2092
|
|
|
|
|
|
|
#pod not required. |
2093
|
|
|
|
|
|
|
#pod |
2094
|
|
|
|
|
|
|
#pod On some platforms, for example Windows NT, it is not possible to |
2095
|
|
|
|
|
|
|
#pod unlink an open file (the file must be closed first). On those |
2096
|
|
|
|
|
|
|
#pod platforms, the actual unlinking is deferred until the program ends and |
2097
|
|
|
|
|
|
|
#pod good status is returned. A check is still performed to make sure that |
2098
|
|
|
|
|
|
|
#pod the filehandle and filename are pointing to the same thing (but not at |
2099
|
|
|
|
|
|
|
#pod the time the end block is executed since the deferred removal may not |
2100
|
|
|
|
|
|
|
#pod have access to the filehandle). |
2101
|
|
|
|
|
|
|
#pod |
2102
|
|
|
|
|
|
|
#pod Additionally, on Windows NT not all the fields returned by stat() can |
2103
|
|
|
|
|
|
|
#pod be compared. For example, the C and C fields seem to be |
2104
|
|
|
|
|
|
|
#pod different. Also, it seems that the size of the file returned by stat() |
2105
|
|
|
|
|
|
|
#pod does not always agree, with C being more accurate than |
2106
|
|
|
|
|
|
|
#pod C, presumably because of caching issues even when |
2107
|
|
|
|
|
|
|
#pod using autoflush (this is usually overcome by waiting a while after |
2108
|
|
|
|
|
|
|
#pod writing to the tempfile before attempting to C it). |
2109
|
|
|
|
|
|
|
#pod |
2110
|
|
|
|
|
|
|
#pod Finally, on NFS file systems the link count of the file handle does |
2111
|
|
|
|
|
|
|
#pod not always go to zero immediately after unlinking. Currently, this |
2112
|
|
|
|
|
|
|
#pod command is expected to fail on NFS disks. |
2113
|
|
|
|
|
|
|
#pod |
2114
|
|
|
|
|
|
|
#pod This function is disabled if the global variable $KEEP_ALL is true |
2115
|
|
|
|
|
|
|
#pod and an unlink on open file is supported. If the unlink is to be deferred |
2116
|
|
|
|
|
|
|
#pod to the END block, the file is still registered for removal. |
2117
|
|
|
|
|
|
|
#pod |
2118
|
|
|
|
|
|
|
#pod This function should not be called if you are using the object oriented |
2119
|
|
|
|
|
|
|
#pod interface since the it will interfere with the object destructor deleting |
2120
|
|
|
|
|
|
|
#pod the file. |
2121
|
|
|
|
|
|
|
#pod |
2122
|
|
|
|
|
|
|
#pod Available Since 0.05. |
2123
|
|
|
|
|
|
|
#pod |
2124
|
|
|
|
|
|
|
#pod If can not unlink open file, defer removal until later available since 0.06. |
2125
|
|
|
|
|
|
|
#pod |
2126
|
|
|
|
|
|
|
#pod =cut |
2127
|
|
|
|
|
|
|
|
2128
|
|
|
|
|
|
|
sub unlink0 { |
2129
|
|
|
|
|
|
|
|
2130
|
5
|
50
|
|
5
|
1
|
5148
|
croak 'Usage: unlink0(filehandle, filename)' |
2131
|
|
|
|
|
|
|
unless scalar(@_) == 2; |
2132
|
|
|
|
|
|
|
|
2133
|
|
|
|
|
|
|
# Read args |
2134
|
5
|
|
|
|
|
11
|
my ($fh, $path) = @_; |
2135
|
|
|
|
|
|
|
|
2136
|
5
|
50
|
|
|
|
19
|
cmpstat($fh, $path) or return 0; |
2137
|
|
|
|
|
|
|
|
2138
|
|
|
|
|
|
|
# attempt remove the file (does not work on some platforms) |
2139
|
5
|
50
|
|
|
|
15
|
if (_can_unlink_opened_file()) { |
2140
|
|
|
|
|
|
|
|
2141
|
|
|
|
|
|
|
# return early (Without unlink) if we have been instructed to retain files. |
2142
|
5
|
50
|
|
|
|
12
|
return 1 if $KEEP_ALL; |
2143
|
|
|
|
|
|
|
|
2144
|
|
|
|
|
|
|
# XXX: do *not* call this on a directory; possible race |
2145
|
|
|
|
|
|
|
# resulting in recursive removal |
2146
|
5
|
50
|
|
|
|
47
|
croak "unlink0: $path has become a directory!" if -d $path; |
2147
|
5
|
50
|
|
|
|
142
|
unlink($path) or return 0; |
2148
|
|
|
|
|
|
|
|
2149
|
|
|
|
|
|
|
# Stat the filehandle |
2150
|
5
|
|
|
|
|
45
|
my @fh = stat $fh; |
2151
|
|
|
|
|
|
|
|
2152
|
5
|
50
|
|
|
|
15
|
print "Link count = $fh[3] \n" if $DEBUG; |
2153
|
|
|
|
|
|
|
|
2154
|
|
|
|
|
|
|
# Make sure that the link count is zero |
2155
|
|
|
|
|
|
|
# - Cygwin provides deferred unlinking, however, |
2156
|
|
|
|
|
|
|
# on Win9x the link count remains 1 |
2157
|
|
|
|
|
|
|
# On NFS the link count may still be 1 but we can't know that |
2158
|
|
|
|
|
|
|
# we are on NFS. Since we can't be sure, we'll defer it |
2159
|
|
|
|
|
|
|
|
2160
|
5
|
50
|
33
|
|
|
28
|
return 1 if $fh[3] == 0 || $^O eq 'cygwin'; |
2161
|
|
|
|
|
|
|
} |
2162
|
|
|
|
|
|
|
# fall-through if we can't unlink now |
2163
|
0
|
|
|
|
|
0
|
_deferred_unlink($fh, $path, 0); |
2164
|
0
|
|
|
|
|
0
|
return 1; |
2165
|
|
|
|
|
|
|
} |
2166
|
|
|
|
|
|
|
|
2167
|
|
|
|
|
|
|
#pod =item B |
2168
|
|
|
|
|
|
|
#pod |
2169
|
|
|
|
|
|
|
#pod Compare C of filehandle with C of provided filename. This |
2170
|
|
|
|
|
|
|
#pod can be used to check that the filename and filehandle initially point |
2171
|
|
|
|
|
|
|
#pod to the same file and that the number of links to the file is 1 (all |
2172
|
|
|
|
|
|
|
#pod fields returned by stat() are compared). |
2173
|
|
|
|
|
|
|
#pod |
2174
|
|
|
|
|
|
|
#pod cmpstat($fh, $path) |
2175
|
|
|
|
|
|
|
#pod or die "Error comparing handle with file"; |
2176
|
|
|
|
|
|
|
#pod |
2177
|
|
|
|
|
|
|
#pod Returns false if the stat information differs or if the link count is |
2178
|
|
|
|
|
|
|
#pod greater than 1. Calls croak if there is a security anomaly. |
2179
|
|
|
|
|
|
|
#pod |
2180
|
|
|
|
|
|
|
#pod On certain platforms, for example Windows, not all the fields returned by stat() |
2181
|
|
|
|
|
|
|
#pod can be compared. For example, the C and C fields seem to be |
2182
|
|
|
|
|
|
|
#pod different in Windows. Also, it seems that the size of the file |
2183
|
|
|
|
|
|
|
#pod returned by stat() does not always agree, with C being more |
2184
|
|
|
|
|
|
|
#pod accurate than C, presumably because of caching issues |
2185
|
|
|
|
|
|
|
#pod even when using autoflush (this is usually overcome by waiting a while |
2186
|
|
|
|
|
|
|
#pod after writing to the tempfile before attempting to C it). |
2187
|
|
|
|
|
|
|
#pod |
2188
|
|
|
|
|
|
|
#pod Not exported by default. |
2189
|
|
|
|
|
|
|
#pod |
2190
|
|
|
|
|
|
|
#pod Current API available since 0.14. |
2191
|
|
|
|
|
|
|
#pod |
2192
|
|
|
|
|
|
|
#pod =cut |
2193
|
|
|
|
|
|
|
|
2194
|
|
|
|
|
|
|
sub cmpstat { |
2195
|
|
|
|
|
|
|
|
2196
|
12
|
50
|
|
12
|
1
|
40
|
croak 'Usage: cmpstat(filehandle, filename)' |
2197
|
|
|
|
|
|
|
unless scalar(@_) == 2; |
2198
|
|
|
|
|
|
|
|
2199
|
|
|
|
|
|
|
# Read args |
2200
|
12
|
|
|
|
|
23
|
my ($fh, $path) = @_; |
2201
|
|
|
|
|
|
|
|
2202
|
12
|
50
|
|
|
|
43
|
warn "Comparing stat\n" |
2203
|
|
|
|
|
|
|
if $DEBUG; |
2204
|
|
|
|
|
|
|
|
2205
|
|
|
|
|
|
|
# Stat the filehandle - which may be closed if someone has manually |
2206
|
|
|
|
|
|
|
# closed the file. Can not turn off warnings without using $^W |
2207
|
|
|
|
|
|
|
# unless we upgrade to 5.006 minimum requirement |
2208
|
12
|
|
|
|
|
32
|
my @fh; |
2209
|
|
|
|
|
|
|
{ |
2210
|
12
|
|
|
|
|
19
|
local ($^W) = 0; |
|
12
|
|
|
|
|
52
|
|
2211
|
12
|
|
|
|
|
142
|
@fh = stat $fh; |
2212
|
|
|
|
|
|
|
} |
2213
|
12
|
100
|
|
|
|
79
|
return unless @fh; |
2214
|
|
|
|
|
|
|
|
2215
|
11
|
0
|
33
|
|
|
32
|
if ($fh[3] > 1 && $^W) { |
2216
|
0
|
0
|
|
|
|
0
|
carp "unlink0: fstat found too many links; SB=@fh" if $^W; |
2217
|
|
|
|
|
|
|
} |
2218
|
|
|
|
|
|
|
|
2219
|
|
|
|
|
|
|
# Stat the path |
2220
|
11
|
|
|
|
|
117
|
my @path = stat $path; |
2221
|
|
|
|
|
|
|
|
2222
|
11
|
50
|
|
|
|
73
|
unless (@path) { |
2223
|
0
|
0
|
|
|
|
0
|
carp "unlink0: $path is gone already" if $^W; |
2224
|
0
|
|
|
|
|
0
|
return; |
2225
|
|
|
|
|
|
|
} |
2226
|
|
|
|
|
|
|
|
2227
|
|
|
|
|
|
|
# this is no longer a file, but may be a directory, or worse |
2228
|
11
|
50
|
|
|
|
105
|
unless (-f $path) { |
2229
|
0
|
|
|
|
|
0
|
confess "panic: $path is no longer a file: SB=@fh"; |
2230
|
|
|
|
|
|
|
} |
2231
|
|
|
|
|
|
|
|
2232
|
|
|
|
|
|
|
# Do comparison of each member of the array |
2233
|
|
|
|
|
|
|
# On WinNT dev and rdev seem to be different |
2234
|
|
|
|
|
|
|
# depending on whether it is a file or a handle. |
2235
|
|
|
|
|
|
|
# Cannot simply compare all members of the stat return |
2236
|
|
|
|
|
|
|
# Select the ones we can use |
2237
|
11
|
|
|
|
|
48
|
my @okstat = (0..$#fh); # Use all by default |
2238
|
11
|
50
|
|
|
|
110
|
if ($^O eq 'MSWin32') { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
2239
|
0
|
|
|
|
|
0
|
@okstat = (1,2,3,4,5,7,8,9,10); |
2240
|
|
|
|
|
|
|
} elsif ($^O eq 'os2') { |
2241
|
0
|
|
|
|
|
0
|
@okstat = (0, 2..$#fh); |
2242
|
|
|
|
|
|
|
} elsif ($^O eq 'VMS') { # device and file ID are sufficient |
2243
|
0
|
|
|
|
|
0
|
@okstat = (0, 1); |
2244
|
|
|
|
|
|
|
} elsif ($^O eq 'dos') { |
2245
|
0
|
|
|
|
|
0
|
@okstat = (0,2..7,11..$#fh); |
2246
|
|
|
|
|
|
|
} elsif ($^O eq 'mpeix') { |
2247
|
0
|
|
|
|
|
0
|
@okstat = (0..4,8..10); |
2248
|
|
|
|
|
|
|
} |
2249
|
|
|
|
|
|
|
|
2250
|
|
|
|
|
|
|
# Now compare each entry explicitly by number |
2251
|
11
|
|
|
|
|
28
|
for (@okstat) { |
2252
|
143
|
50
|
|
|
|
192
|
print "Comparing: $_ : $fh[$_] and $path[$_]\n" if $DEBUG; |
2253
|
|
|
|
|
|
|
# Use eq rather than == since rdev, blksize, and blocks (6, 11, |
2254
|
|
|
|
|
|
|
# and 12) will be '' on platforms that do not support them. This |
2255
|
|
|
|
|
|
|
# is fine since we are only comparing integers. |
2256
|
143
|
50
|
|
|
|
284
|
unless ($fh[$_] eq $path[$_]) { |
2257
|
0
|
0
|
|
|
|
0
|
warn "Did not match $_ element of stat\n" if $DEBUG; |
2258
|
0
|
|
|
|
|
0
|
return 0; |
2259
|
|
|
|
|
|
|
} |
2260
|
|
|
|
|
|
|
} |
2261
|
|
|
|
|
|
|
|
2262
|
11
|
|
|
|
|
114
|
return 1; |
2263
|
|
|
|
|
|
|
} |
2264
|
|
|
|
|
|
|
|
2265
|
|
|
|
|
|
|
#pod =item B |
2266
|
|
|
|
|
|
|
#pod |
2267
|
|
|
|
|
|
|
#pod Similar to C except after file comparison using cmpstat, the |
2268
|
|
|
|
|
|
|
#pod filehandle is closed prior to attempting to unlink the file. This |
2269
|
|
|
|
|
|
|
#pod allows the file to be removed without using an END block, but does |
2270
|
|
|
|
|
|
|
#pod mean that the post-unlink comparison of the filehandle state provided |
2271
|
|
|
|
|
|
|
#pod by C is not available. |
2272
|
|
|
|
|
|
|
#pod |
2273
|
|
|
|
|
|
|
#pod unlink1($fh, $path) |
2274
|
|
|
|
|
|
|
#pod or die "Error closing and unlinking file"; |
2275
|
|
|
|
|
|
|
#pod |
2276
|
|
|
|
|
|
|
#pod Usually called from the object destructor when using the OO interface. |
2277
|
|
|
|
|
|
|
#pod |
2278
|
|
|
|
|
|
|
#pod Not exported by default. |
2279
|
|
|
|
|
|
|
#pod |
2280
|
|
|
|
|
|
|
#pod This function is disabled if the global variable $KEEP_ALL is true. |
2281
|
|
|
|
|
|
|
#pod |
2282
|
|
|
|
|
|
|
#pod Can call croak() if there is a security anomaly during the stat() |
2283
|
|
|
|
|
|
|
#pod comparison. |
2284
|
|
|
|
|
|
|
#pod |
2285
|
|
|
|
|
|
|
#pod Current API available since 0.14. |
2286
|
|
|
|
|
|
|
#pod |
2287
|
|
|
|
|
|
|
#pod =cut |
2288
|
|
|
|
|
|
|
|
2289
|
|
|
|
|
|
|
sub unlink1 { |
2290
|
7
|
50
|
|
7
|
1
|
32
|
croak 'Usage: unlink1(filehandle, filename)' |
2291
|
|
|
|
|
|
|
unless scalar(@_) == 2; |
2292
|
|
|
|
|
|
|
|
2293
|
|
|
|
|
|
|
# Read args |
2294
|
7
|
|
|
|
|
24
|
my ($fh, $path) = @_; |
2295
|
|
|
|
|
|
|
|
2296
|
7
|
100
|
|
|
|
29
|
cmpstat($fh, $path) or return 0; |
2297
|
|
|
|
|
|
|
|
2298
|
|
|
|
|
|
|
# Close the file |
2299
|
6
|
50
|
|
|
|
81
|
close( $fh ) or return 0; |
2300
|
|
|
|
|
|
|
|
2301
|
|
|
|
|
|
|
# Make sure the file is writable (for windows) |
2302
|
6
|
|
|
|
|
24
|
_force_writable( $path ); |
2303
|
|
|
|
|
|
|
|
2304
|
|
|
|
|
|
|
# return early (without unlink) if we have been instructed to retain files. |
2305
|
6
|
50
|
|
|
|
23
|
return 1 if $KEEP_ALL; |
2306
|
|
|
|
|
|
|
|
2307
|
|
|
|
|
|
|
# remove the file |
2308
|
6
|
|
|
|
|
470
|
return unlink($path); |
2309
|
|
|
|
|
|
|
} |
2310
|
|
|
|
|
|
|
|
2311
|
|
|
|
|
|
|
#pod =item B |
2312
|
|
|
|
|
|
|
#pod |
2313
|
|
|
|
|
|
|
#pod Calling this function will cause any temp files or temp directories |
2314
|
|
|
|
|
|
|
#pod that are registered for removal to be removed. This happens automatically |
2315
|
|
|
|
|
|
|
#pod when the process exits but can be triggered manually if the caller is sure |
2316
|
|
|
|
|
|
|
#pod that none of the temp files are required. This method can be registered as |
2317
|
|
|
|
|
|
|
#pod an Apache callback. |
2318
|
|
|
|
|
|
|
#pod |
2319
|
|
|
|
|
|
|
#pod Note that if a temp directory is your current directory, it cannot be |
2320
|
|
|
|
|
|
|
#pod removed. C out of the directory first before calling |
2321
|
|
|
|
|
|
|
#pod C. (For the cleanup at program exit when the CLEANUP flag |
2322
|
|
|
|
|
|
|
#pod is set, this happens automatically.) |
2323
|
|
|
|
|
|
|
#pod |
2324
|
|
|
|
|
|
|
#pod On OSes where temp files are automatically removed when the temp file |
2325
|
|
|
|
|
|
|
#pod is closed, calling this function will have no effect other than to remove |
2326
|
|
|
|
|
|
|
#pod temporary directories (which may include temporary files). |
2327
|
|
|
|
|
|
|
#pod |
2328
|
|
|
|
|
|
|
#pod File::Temp::cleanup(); |
2329
|
|
|
|
|
|
|
#pod |
2330
|
|
|
|
|
|
|
#pod Not exported by default. |
2331
|
|
|
|
|
|
|
#pod |
2332
|
|
|
|
|
|
|
#pod Current API available since 0.15. |
2333
|
|
|
|
|
|
|
#pod |
2334
|
|
|
|
|
|
|
#pod =back |
2335
|
|
|
|
|
|
|
#pod |
2336
|
|
|
|
|
|
|
#pod =head1 PACKAGE VARIABLES |
2337
|
|
|
|
|
|
|
#pod |
2338
|
|
|
|
|
|
|
#pod These functions control the global state of the package. |
2339
|
|
|
|
|
|
|
#pod |
2340
|
|
|
|
|
|
|
#pod =over 4 |
2341
|
|
|
|
|
|
|
#pod |
2342
|
|
|
|
|
|
|
#pod =item B |
2343
|
|
|
|
|
|
|
#pod |
2344
|
|
|
|
|
|
|
#pod Controls the lengths to which the module will go to check the safety of the |
2345
|
|
|
|
|
|
|
#pod temporary file or directory before proceeding. |
2346
|
|
|
|
|
|
|
#pod Options are: |
2347
|
|
|
|
|
|
|
#pod |
2348
|
|
|
|
|
|
|
#pod =over 8 |
2349
|
|
|
|
|
|
|
#pod |
2350
|
|
|
|
|
|
|
#pod =item STANDARD |
2351
|
|
|
|
|
|
|
#pod |
2352
|
|
|
|
|
|
|
#pod Do the basic security measures to ensure the directory exists and is |
2353
|
|
|
|
|
|
|
#pod writable, that temporary files are opened only if they do not already |
2354
|
|
|
|
|
|
|
#pod exist, and that possible race conditions are avoided. Finally the |
2355
|
|
|
|
|
|
|
#pod L function is used to remove files safely. |
2356
|
|
|
|
|
|
|
#pod |
2357
|
|
|
|
|
|
|
#pod =item MEDIUM |
2358
|
|
|
|
|
|
|
#pod |
2359
|
|
|
|
|
|
|
#pod In addition to the STANDARD security, the output directory is checked |
2360
|
|
|
|
|
|
|
#pod to make sure that it is owned either by root or the user running the |
2361
|
|
|
|
|
|
|
#pod program. If the directory is writable by group or by other, it is then |
2362
|
|
|
|
|
|
|
#pod checked to make sure that the sticky bit is set. |
2363
|
|
|
|
|
|
|
#pod |
2364
|
|
|
|
|
|
|
#pod Will not work on platforms that do not support the C<-k> test |
2365
|
|
|
|
|
|
|
#pod for sticky bit. |
2366
|
|
|
|
|
|
|
#pod |
2367
|
|
|
|
|
|
|
#pod =item HIGH |
2368
|
|
|
|
|
|
|
#pod |
2369
|
|
|
|
|
|
|
#pod In addition to the MEDIUM security checks, also check for the |
2370
|
|
|
|
|
|
|
#pod possibility of ``chown() giveaway'' using the L |
2371
|
|
|
|
|
|
|
#pod sysconf() function. If this is a possibility, each directory in the |
2372
|
|
|
|
|
|
|
#pod path is checked in turn for safeness, recursively walking back to the |
2373
|
|
|
|
|
|
|
#pod root directory. |
2374
|
|
|
|
|
|
|
#pod |
2375
|
|
|
|
|
|
|
#pod For platforms that do not support the L |
2376
|
|
|
|
|
|
|
#pod C<_PC_CHOWN_RESTRICTED> symbol (for example, Windows NT) it is |
2377
|
|
|
|
|
|
|
#pod assumed that ``chown() giveaway'' is possible and the recursive test |
2378
|
|
|
|
|
|
|
#pod is performed. |
2379
|
|
|
|
|
|
|
#pod |
2380
|
|
|
|
|
|
|
#pod =back |
2381
|
|
|
|
|
|
|
#pod |
2382
|
|
|
|
|
|
|
#pod The level can be changed as follows: |
2383
|
|
|
|
|
|
|
#pod |
2384
|
|
|
|
|
|
|
#pod File::Temp->safe_level( File::Temp::HIGH ); |
2385
|
|
|
|
|
|
|
#pod |
2386
|
|
|
|
|
|
|
#pod The level constants are not exported by the module. |
2387
|
|
|
|
|
|
|
#pod |
2388
|
|
|
|
|
|
|
#pod Currently, you must be running at least perl v5.6.0 in order to |
2389
|
|
|
|
|
|
|
#pod run with MEDIUM or HIGH security. This is simply because the |
2390
|
|
|
|
|
|
|
#pod safety tests use functions from L that are not |
2391
|
|
|
|
|
|
|
#pod available in older versions of perl. The problem is that the version |
2392
|
|
|
|
|
|
|
#pod number for Fcntl is the same in perl 5.6.0 and in 5.005_03 even though |
2393
|
|
|
|
|
|
|
#pod they are different versions. |
2394
|
|
|
|
|
|
|
#pod |
2395
|
|
|
|
|
|
|
#pod On systems that do not support the HIGH or MEDIUM safety levels |
2396
|
|
|
|
|
|
|
#pod (for example Win NT or OS/2) any attempt to change the level will |
2397
|
|
|
|
|
|
|
#pod be ignored. The decision to ignore rather than raise an exception |
2398
|
|
|
|
|
|
|
#pod allows portable programs to be written with high security in mind |
2399
|
|
|
|
|
|
|
#pod for the systems that can support this without those programs failing |
2400
|
|
|
|
|
|
|
#pod on systems where the extra tests are irrelevant. |
2401
|
|
|
|
|
|
|
#pod |
2402
|
|
|
|
|
|
|
#pod If you really need to see whether the change has been accepted |
2403
|
|
|
|
|
|
|
#pod simply examine the return value of C. |
2404
|
|
|
|
|
|
|
#pod |
2405
|
|
|
|
|
|
|
#pod $newlevel = File::Temp->safe_level( File::Temp::HIGH ); |
2406
|
|
|
|
|
|
|
#pod die "Could not change to high security" |
2407
|
|
|
|
|
|
|
#pod if $newlevel != File::Temp::HIGH; |
2408
|
|
|
|
|
|
|
#pod |
2409
|
|
|
|
|
|
|
#pod Available since 0.05. |
2410
|
|
|
|
|
|
|
#pod |
2411
|
|
|
|
|
|
|
#pod =cut |
2412
|
|
|
|
|
|
|
|
2413
|
|
|
|
|
|
|
{ |
2414
|
|
|
|
|
|
|
# protect from using the variable itself |
2415
|
|
|
|
|
|
|
my $LEVEL = STANDARD; |
2416
|
|
|
|
|
|
|
sub safe_level { |
2417
|
94
|
|
|
94
|
1
|
1722
|
my $self = shift; |
2418
|
94
|
100
|
|
|
|
183
|
if (@_) { |
2419
|
3
|
|
|
|
|
5
|
my $level = shift; |
2420
|
3
|
50
|
100
|
|
|
15
|
if (($level != STANDARD) && ($level != MEDIUM) && ($level != HIGH)) { |
|
|
|
66
|
|
|
|
|
2421
|
0
|
0
|
|
|
|
0
|
carp "safe_level: Specified level ($level) not STANDARD, MEDIUM or HIGH - ignoring\n" if $^W; |
2422
|
|
|
|
|
|
|
} else { |
2423
|
|
|
|
|
|
|
# Don't allow this on perl 5.005 or earlier |
2424
|
3
|
50
|
33
|
|
|
8
|
if ($] < 5.006 && $level != STANDARD) { |
2425
|
|
|
|
|
|
|
# Cant do MEDIUM or HIGH checks |
2426
|
0
|
|
|
|
|
0
|
croak "Currently requires perl 5.006 or newer to do the safe checks"; |
2427
|
|
|
|
|
|
|
} |
2428
|
|
|
|
|
|
|
# Check that we are allowed to change level |
2429
|
|
|
|
|
|
|
# Silently ignore if we can not. |
2430
|
3
|
50
|
|
|
|
8
|
$LEVEL = $level if _can_do_level($level); |
2431
|
|
|
|
|
|
|
} |
2432
|
|
|
|
|
|
|
} |
2433
|
94
|
|
|
|
|
269
|
return $LEVEL; |
2434
|
|
|
|
|
|
|
} |
2435
|
|
|
|
|
|
|
} |
2436
|
|
|
|
|
|
|
|
2437
|
|
|
|
|
|
|
#pod =item TopSystemUID |
2438
|
|
|
|
|
|
|
#pod |
2439
|
|
|
|
|
|
|
#pod This is the highest UID on the current system that refers to a root |
2440
|
|
|
|
|
|
|
#pod UID. This is used to make sure that the temporary directory is |
2441
|
|
|
|
|
|
|
#pod owned by a system UID (C, C, C etc) rather than |
2442
|
|
|
|
|
|
|
#pod simply by root. |
2443
|
|
|
|
|
|
|
#pod |
2444
|
|
|
|
|
|
|
#pod This is required since on many unix systems C is not owned |
2445
|
|
|
|
|
|
|
#pod by root. |
2446
|
|
|
|
|
|
|
#pod |
2447
|
|
|
|
|
|
|
#pod Default is to assume that any UID less than or equal to 10 is a root |
2448
|
|
|
|
|
|
|
#pod UID. |
2449
|
|
|
|
|
|
|
#pod |
2450
|
|
|
|
|
|
|
#pod File::Temp->top_system_uid(10); |
2451
|
|
|
|
|
|
|
#pod my $topid = File::Temp->top_system_uid; |
2452
|
|
|
|
|
|
|
#pod |
2453
|
|
|
|
|
|
|
#pod This value can be adjusted to reduce security checking if required. |
2454
|
|
|
|
|
|
|
#pod The value is only relevant when C is set to MEDIUM or higher. |
2455
|
|
|
|
|
|
|
#pod |
2456
|
|
|
|
|
|
|
#pod Available since 0.05. |
2457
|
|
|
|
|
|
|
#pod |
2458
|
|
|
|
|
|
|
#pod =cut |
2459
|
|
|
|
|
|
|
|
2460
|
|
|
|
|
|
|
{ |
2461
|
|
|
|
|
|
|
my $TopSystemUID = 10; |
2462
|
|
|
|
|
|
|
$TopSystemUID = 197108 if $^O eq 'interix'; # "Administrator" |
2463
|
|
|
|
|
|
|
sub top_system_uid { |
2464
|
5
|
|
|
5
|
0
|
1163
|
my $self = shift; |
2465
|
5
|
50
|
|
|
|
11
|
if (@_) { |
2466
|
0
|
|
|
|
|
0
|
my $newuid = shift; |
2467
|
0
|
0
|
|
|
|
0
|
croak "top_system_uid: UIDs should be numeric" |
2468
|
|
|
|
|
|
|
unless $newuid =~ /^\d+$/s; |
2469
|
0
|
|
|
|
|
0
|
$TopSystemUID = $newuid; |
2470
|
|
|
|
|
|
|
} |
2471
|
5
|
|
|
|
|
30
|
return $TopSystemUID; |
2472
|
|
|
|
|
|
|
} |
2473
|
|
|
|
|
|
|
} |
2474
|
|
|
|
|
|
|
|
2475
|
|
|
|
|
|
|
#pod =item B<$KEEP_ALL> |
2476
|
|
|
|
|
|
|
#pod |
2477
|
|
|
|
|
|
|
#pod Controls whether temporary files and directories should be retained |
2478
|
|
|
|
|
|
|
#pod regardless of any instructions in the program to remove them |
2479
|
|
|
|
|
|
|
#pod automatically. This is useful for debugging but should not be used in |
2480
|
|
|
|
|
|
|
#pod production code. |
2481
|
|
|
|
|
|
|
#pod |
2482
|
|
|
|
|
|
|
#pod $File::Temp::KEEP_ALL = 1; |
2483
|
|
|
|
|
|
|
#pod |
2484
|
|
|
|
|
|
|
#pod Default is for files to be removed as requested by the caller. |
2485
|
|
|
|
|
|
|
#pod |
2486
|
|
|
|
|
|
|
#pod In some cases, files will only be retained if this variable is true |
2487
|
|
|
|
|
|
|
#pod when the file is created. This means that you can not create a temporary |
2488
|
|
|
|
|
|
|
#pod file, set this variable and expect the temp file to still be around |
2489
|
|
|
|
|
|
|
#pod when the program exits. |
2490
|
|
|
|
|
|
|
#pod |
2491
|
|
|
|
|
|
|
#pod =item B<$DEBUG> |
2492
|
|
|
|
|
|
|
#pod |
2493
|
|
|
|
|
|
|
#pod Controls whether debugging messages should be enabled. |
2494
|
|
|
|
|
|
|
#pod |
2495
|
|
|
|
|
|
|
#pod $File::Temp::DEBUG = 1; |
2496
|
|
|
|
|
|
|
#pod |
2497
|
|
|
|
|
|
|
#pod Default is for debugging mode to be disabled. |
2498
|
|
|
|
|
|
|
#pod |
2499
|
|
|
|
|
|
|
#pod Available since 0.15. |
2500
|
|
|
|
|
|
|
#pod |
2501
|
|
|
|
|
|
|
#pod =back |
2502
|
|
|
|
|
|
|
#pod |
2503
|
|
|
|
|
|
|
#pod =head1 WARNING |
2504
|
|
|
|
|
|
|
#pod |
2505
|
|
|
|
|
|
|
#pod For maximum security, endeavour always to avoid ever looking at, |
2506
|
|
|
|
|
|
|
#pod touching, or even imputing the existence of the filename. You do not |
2507
|
|
|
|
|
|
|
#pod know that that filename is connected to the same file as the handle |
2508
|
|
|
|
|
|
|
#pod you have, and attempts to check this can only trigger more race |
2509
|
|
|
|
|
|
|
#pod conditions. It's far more secure to use the filehandle alone and |
2510
|
|
|
|
|
|
|
#pod dispense with the filename altogether. |
2511
|
|
|
|
|
|
|
#pod |
2512
|
|
|
|
|
|
|
#pod If you need to pass the handle to something that expects a filename |
2513
|
|
|
|
|
|
|
#pod then on a unix system you can use C<"/dev/fd/" . fileno($fh)> for |
2514
|
|
|
|
|
|
|
#pod arbitrary programs. Perl code that uses the 2-argument version of |
2515
|
|
|
|
|
|
|
#pod C<< open >> can be passed C<< "+<=&" . fileno($fh) >>. Otherwise you |
2516
|
|
|
|
|
|
|
#pod will need to pass the filename. You will have to clear the |
2517
|
|
|
|
|
|
|
#pod close-on-exec bit on that file descriptor before passing it to another |
2518
|
|
|
|
|
|
|
#pod process. |
2519
|
|
|
|
|
|
|
#pod |
2520
|
|
|
|
|
|
|
#pod use Fcntl qw/F_SETFD F_GETFD/; |
2521
|
|
|
|
|
|
|
#pod fcntl($tmpfh, F_SETFD, 0) |
2522
|
|
|
|
|
|
|
#pod or die "Can't clear close-on-exec flag on temp fh: $!\n"; |
2523
|
|
|
|
|
|
|
#pod |
2524
|
|
|
|
|
|
|
#pod =head2 Temporary files and NFS |
2525
|
|
|
|
|
|
|
#pod |
2526
|
|
|
|
|
|
|
#pod Some problems are associated with using temporary files that reside |
2527
|
|
|
|
|
|
|
#pod on NFS file systems and it is recommended that a local filesystem |
2528
|
|
|
|
|
|
|
#pod is used whenever possible. Some of the security tests will most probably |
2529
|
|
|
|
|
|
|
#pod fail when the temp file is not local. Additionally, be aware that |
2530
|
|
|
|
|
|
|
#pod the performance of I/O operations over NFS will not be as good as for |
2531
|
|
|
|
|
|
|
#pod a local disk. |
2532
|
|
|
|
|
|
|
#pod |
2533
|
|
|
|
|
|
|
#pod =head2 Forking |
2534
|
|
|
|
|
|
|
#pod |
2535
|
|
|
|
|
|
|
#pod In some cases files created by File::Temp are removed from within an |
2536
|
|
|
|
|
|
|
#pod END block. Since END blocks are triggered when a child process exits |
2537
|
|
|
|
|
|
|
#pod (unless C is used by the child) File::Temp takes care |
2538
|
|
|
|
|
|
|
#pod to only remove those temp files created by a particular process ID. This |
2539
|
|
|
|
|
|
|
#pod means that a child will not attempt to remove temp files created by the |
2540
|
|
|
|
|
|
|
#pod parent process. |
2541
|
|
|
|
|
|
|
#pod |
2542
|
|
|
|
|
|
|
#pod If you are forking many processes in parallel that are all creating |
2543
|
|
|
|
|
|
|
#pod temporary files, you may need to reset the random number seed using |
2544
|
|
|
|
|
|
|
#pod srand(EXPR) in each child else all the children will attempt to walk |
2545
|
|
|
|
|
|
|
#pod through the same set of random file names and may well cause |
2546
|
|
|
|
|
|
|
#pod themselves to give up if they exceed the number of retry attempts. |
2547
|
|
|
|
|
|
|
#pod |
2548
|
|
|
|
|
|
|
#pod =head2 Directory removal |
2549
|
|
|
|
|
|
|
#pod |
2550
|
|
|
|
|
|
|
#pod Note that if you have chdir'ed into the temporary directory and it is |
2551
|
|
|
|
|
|
|
#pod subsequently cleaned up (either in the END block or as part of object |
2552
|
|
|
|
|
|
|
#pod destruction), then you will get a warning from File::Path::rmtree(). |
2553
|
|
|
|
|
|
|
#pod |
2554
|
|
|
|
|
|
|
#pod =head2 Taint mode |
2555
|
|
|
|
|
|
|
#pod |
2556
|
|
|
|
|
|
|
#pod If you need to run code under taint mode, updating to the latest |
2557
|
|
|
|
|
|
|
#pod L is highly recommended. On Windows, if the directory |
2558
|
|
|
|
|
|
|
#pod given by L isn't writable, File::Temp will attempt |
2559
|
|
|
|
|
|
|
#pod to fallback to the user's local application data directory or croak |
2560
|
|
|
|
|
|
|
#pod with an error. |
2561
|
|
|
|
|
|
|
#pod |
2562
|
|
|
|
|
|
|
#pod =head2 BINMODE |
2563
|
|
|
|
|
|
|
#pod |
2564
|
|
|
|
|
|
|
#pod The file returned by File::Temp will have been opened in binary mode |
2565
|
|
|
|
|
|
|
#pod if such a mode is available. If that is not correct, use the C |
2566
|
|
|
|
|
|
|
#pod function to change the mode of the filehandle. |
2567
|
|
|
|
|
|
|
#pod |
2568
|
|
|
|
|
|
|
#pod Note that you can modify the encoding of a file opened by File::Temp |
2569
|
|
|
|
|
|
|
#pod also by using C. |
2570
|
|
|
|
|
|
|
#pod |
2571
|
|
|
|
|
|
|
#pod =head1 HISTORY |
2572
|
|
|
|
|
|
|
#pod |
2573
|
|
|
|
|
|
|
#pod Originally began life in May 1999 as an XS interface to the system |
2574
|
|
|
|
|
|
|
#pod mkstemp() function. In March 2000, the OpenBSD mkstemp() code was |
2575
|
|
|
|
|
|
|
#pod translated to Perl for total control of the code's |
2576
|
|
|
|
|
|
|
#pod security checking, to ensure the presence of the function regardless of |
2577
|
|
|
|
|
|
|
#pod operating system and to help with portability. The module was shipped |
2578
|
|
|
|
|
|
|
#pod as a standard part of perl from v5.6.1. |
2579
|
|
|
|
|
|
|
#pod |
2580
|
|
|
|
|
|
|
#pod Thanks to Tom Christiansen for suggesting that this module |
2581
|
|
|
|
|
|
|
#pod should be written and providing ideas for code improvements and |
2582
|
|
|
|
|
|
|
#pod security enhancements. |
2583
|
|
|
|
|
|
|
#pod |
2584
|
|
|
|
|
|
|
#pod =head1 SEE ALSO |
2585
|
|
|
|
|
|
|
#pod |
2586
|
|
|
|
|
|
|
#pod L, L, L, L |
2587
|
|
|
|
|
|
|
#pod |
2588
|
|
|
|
|
|
|
#pod See L and L, L for |
2589
|
|
|
|
|
|
|
#pod different implementations of temporary file handling. |
2590
|
|
|
|
|
|
|
#pod |
2591
|
|
|
|
|
|
|
#pod See L for an alternative object-oriented wrapper for |
2592
|
|
|
|
|
|
|
#pod the C function. |
2593
|
|
|
|
|
|
|
#pod |
2594
|
|
|
|
|
|
|
#pod =cut |
2595
|
|
|
|
|
|
|
|
2596
|
|
|
|
|
|
|
package ## hide from PAUSE |
2597
|
|
|
|
|
|
|
File::Temp::Dir; |
2598
|
|
|
|
|
|
|
|
2599
|
|
|
|
|
|
|
our $VERSION = '0.2311'; |
2600
|
|
|
|
|
|
|
|
2601
|
13
|
|
|
13
|
|
66441
|
use File::Path qw/ rmtree /; |
|
13
|
|
|
|
|
29
|
|
|
13
|
|
|
|
|
1081
|
|
2602
|
13
|
|
|
13
|
|
103
|
use strict; |
|
13
|
|
|
|
|
17
|
|
|
13
|
|
|
|
|
535
|
|
2603
|
13
|
|
|
|
|
100
|
use overload '""' => "STRINGIFY", |
2604
|
|
|
|
|
|
|
'0+' => \&File::Temp::NUMIFY, |
2605
|
13
|
|
|
13
|
|
68
|
fallback => 1; |
|
13
|
|
|
|
|
18
|
|
2606
|
|
|
|
|
|
|
|
2607
|
|
|
|
|
|
|
# private class specifically to support tempdir objects |
2608
|
|
|
|
|
|
|
# created by File::Temp->newdir |
2609
|
|
|
|
|
|
|
|
2610
|
|
|
|
|
|
|
# ostensibly the same method interface as File::Temp but without |
2611
|
|
|
|
|
|
|
# inheriting all the IO::Seekable methods and other cruft |
2612
|
|
|
|
|
|
|
|
2613
|
|
|
|
|
|
|
# Read-only - returns the name of the temp directory |
2614
|
|
|
|
|
|
|
|
2615
|
|
|
|
|
|
|
sub dirname { |
2616
|
10
|
|
|
10
|
|
14
|
my $self = shift; |
2617
|
10
|
|
|
|
|
47
|
return $self->{DIRNAME}; |
2618
|
|
|
|
|
|
|
} |
2619
|
|
|
|
|
|
|
|
2620
|
|
|
|
|
|
|
sub STRINGIFY { |
2621
|
10
|
|
|
10
|
|
1037
|
my $self = shift; |
2622
|
10
|
|
|
|
|
21
|
return $self->dirname; |
2623
|
|
|
|
|
|
|
} |
2624
|
|
|
|
|
|
|
|
2625
|
|
|
|
|
|
|
sub unlink_on_destroy { |
2626
|
5
|
|
|
5
|
|
9
|
my $self = shift; |
2627
|
5
|
50
|
|
|
|
16
|
if (@_) { |
2628
|
0
|
|
|
|
|
0
|
$self->{CLEANUP} = shift; |
2629
|
|
|
|
|
|
|
} |
2630
|
5
|
|
|
|
|
37
|
return $self->{CLEANUP}; |
2631
|
|
|
|
|
|
|
} |
2632
|
|
|
|
|
|
|
|
2633
|
|
|
|
|
|
|
sub DESTROY { |
2634
|
5
|
|
|
5
|
|
603
|
my $self = shift; |
2635
|
5
|
|
|
|
|
48
|
local($., $@, $!, $^E, $?); |
2636
|
5
|
50
|
33
|
|
|
14
|
if ($self->unlink_on_destroy && |
|
|
|
33
|
|
|
|
|
2637
|
|
|
|
|
|
|
$$ == $self->{LAUNCHPID} && !$File::Temp::KEEP_ALL) { |
2638
|
5
|
50
|
|
|
|
76
|
if (-d $self->{REALNAME}) { |
2639
|
|
|
|
|
|
|
# Some versions of rmtree will abort if you attempt to remove |
2640
|
|
|
|
|
|
|
# the directory you are sitting in. We protect that and turn it |
2641
|
|
|
|
|
|
|
# into a warning. We do this because this occurs during object |
2642
|
|
|
|
|
|
|
# destruction and so can not be caught by the user. |
2643
|
5
|
|
|
|
|
22
|
eval { rmtree($self->{REALNAME}, $File::Temp::DEBUG, 0); }; |
|
5
|
|
|
|
|
1202
|
|
2644
|
5
|
0
|
33
|
|
|
184
|
warn $@ if ($@ && $^W); |
2645
|
|
|
|
|
|
|
} |
2646
|
|
|
|
|
|
|
} |
2647
|
|
|
|
|
|
|
} |
2648
|
|
|
|
|
|
|
|
2649
|
|
|
|
|
|
|
1; |
2650
|
|
|
|
|
|
|
|
2651
|
|
|
|
|
|
|
|
2652
|
|
|
|
|
|
|
# vim: ts=2 sts=2 sw=2 et: |
2653
|
|
|
|
|
|
|
|
2654
|
|
|
|
|
|
|
__END__ |