PATCH: File::Temp fix on WindowsNT/VMS
[p5sagit/p5-mst-13.2.git] / lib / File / Temp.pm
CommitLineData
262eb13a 1package File::Temp;
2
3=head1 NAME
4
5File::Temp - return name and handle of a temporary file safely
6
e77f578c 7=begin __INTERNALS
8
9=head1 PORTABILITY
10
11This module is designed to be portable across operating systems
12and it currently supports Unix, VMS, DOS, OS/2 and Windows. When
13porting to a new OS there are generally three main issues
14that have to be solved:
15
16=over 4
17
18=item *
19
20Can the OS unlink an open file? If it can't then the
21C<_can_unlink_opened_file> method should be modified.
22
23=item *
24
25Are the return values from C<stat> reliable? By default all the
26return values from C<stat> are compared when unlinking a temporary
27file using the filename and the handle. Operating systems other than
28unix do not always have valid entries in all fields. If C<unlink0> fails
29then the C<stat> comparison should be modified accordingly.
30
31=item *
32
33Security. Systems that can not support a test for the sticky bit
34on a directory can not use the MEDIUM and HIGH security tests.
35The C<_can_do_level> method should be modified accordingly.
36
37=back
38
39=end __INTERNALS
40
262eb13a 41=head1 SYNOPSIS
42
43 use File::Temp qw/ tempfile tempdir /;
44
45 $dir = tempdir( CLEANUP => 1 );
46 ($fh, $filename) = tempfile( DIR => $dir );
47
48 ($fh, $filename) = tempfile( $template, DIR => $dir);
49 ($fh, $filename) = tempfile( $template, SUFFIX => '.dat');
50
51 $fh = tempfile();
52
53MkTemp family:
54
55 use File::Temp qw/ :mktemp /;
56
57 ($fh, $file) = mkstemp( "tmpfileXXXXX" );
58 ($fh, $file) = mkstemps( "tmpfileXXXXXX", $suffix);
59
60 $tmpdir = mkdtemp( $template );
61
62 $unopened_file = mktemp( $template );
63
64POSIX functions:
65
66 use File::Temp qw/ :POSIX /;
67
68 $file = tmpnam();
69 $fh = tmpfile();
70
71 ($fh, $file) = tmpnam();
72 ($fh, $file) = tmpfile();
73
74
75Compatibility functions:
76
77 $unopened_file = File::Temp::tempnam( $dir, $pfx );
78
79=begin later
80
81Objects (NOT YET IMPLEMENTED):
82
83 require File::Temp;
84
85 $fh = new File::Temp($template);
86 $fname = $fh->filename;
87
88=end later
89
90=head1 DESCRIPTION
91
92C<File::Temp> can be used to create and open temporary files in a safe way.
93The tempfile() function can be used to return the name and the open
94filehandle of a temporary file. The tempdir() function can
95be used to create a temporary directory.
96
97The security aspect of temporary file creation is emphasized such that
781948c1 98a filehandle and filename are returned together. This helps guarantee
99that a race condition can not occur where the temporary file is
100created by another process between checking for the existence of the
101file and its opening. Additional security levels are provided to
102check, for example, that the sticky bit is set on world writable
103directories. See L<"safe_level"> for more information.
262eb13a 104
105For compatibility with popular C library functions, Perl implementations of
106the mkstemp() family of functions are provided. These are, mkstemp(),
107mkstemps(), mkdtemp() and mktemp().
108
109Additionally, implementations of the standard L<POSIX|POSIX>
110tmpnam() and tmpfile() functions are provided if required.
111
112Implementations of mktemp(), tmpnam(), and tempnam() are provided,
113but should be used with caution since they return only a filename
114that was valid when function was called, so cannot guarantee
115that the file will not exist by the time the caller opens the filename.
116
117=cut
118
119# 5.6.0 gives us S_IWOTH, S_IWGRP, our and auto-vivifying filehandls
120# People would like a version on 5.005 so give them what they want :-)
121use 5.005;
122use strict;
123use Carp;
124use File::Spec 0.8;
125use File::Path qw/ rmtree /;
126use Fcntl 1.03;
11d7f64f 127use Errno;
51fc852f 128require VMS::Stdio if $^O eq 'VMS';
262eb13a 129
51fc852f 130# Need the Symbol package if we are running older perl
1c19c868 131require Symbol if $] < 5.006;
132
133
262eb13a 134# use 'our' on v5.6.0
135use vars qw($VERSION @EXPORT_OK %EXPORT_TAGS $DEBUG);
136
137$DEBUG = 0;
138
139# We are exporting functions
140
262eb13a 141use base qw/Exporter/;
142
143# Export list - to allow fine tuning of export table
144
145@EXPORT_OK = qw{
146 tempfile
147 tempdir
148 tmpnam
149 tmpfile
150 mktemp
669b450a 151 mkstemp
262eb13a 152 mkstemps
153 mkdtemp
154 unlink0
155 };
156
157# Groups of functions for export
158
159%EXPORT_TAGS = (
160 'POSIX' => [qw/ tmpnam tmpfile /],
161 'mktemp' => [qw/ mktemp mkstemp mkstemps mkdtemp/],
162 );
163
164# add contents of these tags to @EXPORT
165Exporter::export_tags('POSIX','mktemp');
166
167# Version number
168
09d7a2f9 169$VERSION = '0.11';
262eb13a 170
171# This is a list of characters that can be used in random filenames
172
173my @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
174 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
669b450a 175 0 1 2 3 4 5 6 7 8 9 _
262eb13a 176 /);
177
178# Maximum number of tries to make a temp file before failing
179
180use constant MAX_TRIES => 10;
181
182# Minimum number of X characters that should be in a template
183use constant MINX => 4;
184
185# Default template when no template supplied
186
187use constant TEMPXXX => 'X' x 10;
188
189# Constants for the security level
190
191use constant STANDARD => 0;
192use constant MEDIUM => 1;
193use constant HIGH => 2;
194
1c19c868 195# OPENFLAGS. If we defined the flag to use with Sysopen here this gives
196# us an optimisation when many temporary files are requested
197
198my $OPENFLAGS = O_CREAT | O_EXCL | O_RDWR;
199
51fc852f 200for my $oflag (qw/ FOLLOW BINARY LARGEFILE EXLOCK NOINHERIT /) {
1c19c868 201 my ($bit, $func) = (0, "Fcntl::O_" . $oflag);
202 no strict 'refs';
203 $OPENFLAGS |= $bit if eval { $bit = &$func(); 1 };
204}
205
51fc852f 206# On some systems the O_TEMPORARY flag can be used to tell the OS
207# to automatically remove the file when it is closed. This is fine
208# in most cases but not if tempfile is called with UNLINK=>0 and
209# the filename is requested -- in the case where the filename is to
210# be passed to another routine. This happens on windows. We overcome
211# this by using a second open flags variable
212
213my $OPENTEMPFLAGS = $OPENFLAGS;
214for my $oflag (qw/ TEMPORARY /) {
215 my ($bit, $func) = (0, "Fcntl::O_" . $oflag);
216 no strict 'refs';
217 $OPENTEMPFLAGS |= $bit if eval { $bit = &$func(); 1 };
218}
1c19c868 219
220
262eb13a 221# INTERNAL ROUTINES - not to be used outside of package
222
223# Generic routine for getting a temporary filename
224# modelled on OpenBSD _gettemp() in mktemp.c
225
669b450a 226# The template must contain X's that are to be replaced
262eb13a 227# with the random values
228
229# Arguments:
230
231# TEMPLATE - string containing the XXXXX's that is converted
232# to a random filename and opened if required
233
234# Optionally, a hash can also be supplied containing specific options
235# "open" => if true open the temp file, else just return the name
236# default is 0
237# "mkdir"=> if true, we are creating a temp directory rather than tempfile
238# default is 0
239# "suffixlen" => number of characters at end of PATH to be ignored.
240# default is 0.
51fc852f 241# "unlink_on_close" => indicates that, if possible, the OS should remove
242# the file as soon as it is closed. Usually indicates
243# use of the O_TEMPORARY flag to sysopen.
244# Usually irrelevant on unix
245
262eb13a 246# "open" and "mkdir" can not both be true
51fc852f 247# "unlink_on_close" is not used when "mkdir" is true.
262eb13a 248
249# The default options are equivalent to mktemp().
250
251# Returns:
252# filehandle - open file handle (if called with doopen=1, else undef)
253# temp name - name of the temp file or directory
254
255# For example:
256# ($fh, $name) = _gettemp($template, "open" => 1);
257
258# for the current version, failures are associated with
259# a carp to give the reason whilst debugging
260
261sub _gettemp {
262
263 croak 'Usage: ($fh, $name) = _gettemp($template, OPTIONS);'
264 unless scalar(@_) >= 1;
265
266 # Default options
267 my %options = (
268 "open" => 0,
269 "mkdir" => 0,
270 "suffixlen" => 0,
51fc852f 271 "unlink_on_close" => 0,
262eb13a 272 );
273
274 # Read the template
275 my $template = shift;
276 if (ref($template)) {
277 carp "File::Temp::_gettemp: template must not be a reference";
278 return ();
279 }
280
281 # Check that the number of entries on stack are even
282 if (scalar(@_) % 2 != 0) {
283 carp "File::Temp::_gettemp: Must have even number of options";
284 return ();
285 }
286
287 # Read the options and merge with defaults
288 %options = (%options, @_) if @_;
669b450a 289
262eb13a 290 # Can not open the file and make a directory in a single call
291 if ($options{"open"} && $options{"mkdir"}) {
292 carp "File::Temp::_gettemp: doopen and domkdir can not both be true\n";
293 return ();
294 }
295
296 # Find the start of the end of the Xs (position of last X)
297 # Substr starts from 0
298 my $start = length($template) - 1 - $options{"suffixlen"};
299
300 # Check that we have at least MINX x X (eg 'XXXX") at the end of the string
301 # (taking suffixlen into account). Any fewer is insecure.
302
303 # Do it using substr - no reason to use a pattern match since
304 # we know where we are looking and what we are looking for
305
306 if (substr($template, $start - MINX + 1, MINX) ne 'X' x MINX) {
307 carp "File::Temp::_gettemp: The template must contain at least ". MINX ." 'X' characters\n";
308 return ();
309 }
310
311 # Replace all the X at the end of the substring with a
312 # random character or just all the XX at the end of a full string.
313 # Do it as an if, since the suffix adjusts which section to replace
314 # and suffixlen=0 returns nothing if used in the substr directly
315 # and generate a full path from the template
316
317 my $path = _replace_XX($template, $options{"suffixlen"});
318
319
320 # Split the path into constituent parts - eventually we need to check
321 # whether the directory exists
322 # We need to know whether we are making a temp directory
323 # or a tempfile
324
325 my ($volume, $directories, $file);
326 my $parent; # parent directory
327 if ($options{"mkdir"}) {
328 # There is no filename at the end
329 ($volume, $directories, $file) = File::Spec->splitpath( $path, 1);
330
331 # The parent is then $directories without the last directory
332 # Split the directory and put it back together again
333 my @dirs = File::Spec->splitdir($directories);
334
335 # If @dirs only has one entry that means we are in the current
336 # directory
337 if ($#dirs == 0) {
338 $parent = File::Spec->curdir;
339 } else {
340
669b450a 341 if ($^O eq 'VMS') { # need volume to avoid relative dir spec
342 $parent = File::Spec->catdir($volume, @dirs[0..$#dirs-1]);
e4dfc136 343 $parent = 'sys$disk:[]' if $parent eq '';
669b450a 344 } else {
345
346 # Put it back together without the last one
347 $parent = File::Spec->catdir(@dirs[0..$#dirs-1]);
262eb13a 348
669b450a 349 # ...and attach the volume (no filename)
350 $parent = File::Spec->catpath($volume, $parent, '');
351 }
262eb13a 352
353 }
354
355 } else {
356
357 # Get rid of the last filename (use File::Basename for this?)
358 ($volume, $directories, $file) = File::Spec->splitpath( $path );
359
360 # Join up without the file part
361 $parent = File::Spec->catpath($volume,$directories,'');
362
363 # If $parent is empty replace with curdir
364 $parent = File::Spec->curdir
365 unless $directories ne '';
366
367 }
368
369 # Check that the parent directories exist
370 # Do this even for the case where we are simply returning a name
371 # not a file -- no point returning a name that includes a directory
372 # that does not exist or is not writable
373
374 unless (-d $parent && -w _) {
669b450a 375 carp "File::Temp::_gettemp: Parent directory ($parent) is not a directory"
262eb13a 376 . " or is not writable\n";
377 return ();
378 }
379
380 # Check the stickiness of the directory and chown giveaway if required
381 # If the directory is world writable the sticky bit
382 # must be set
383
384 if (File::Temp->safe_level == MEDIUM) {
385 unless (_is_safe($parent)) {
386 carp "File::Temp::_gettemp: Parent directory ($parent) is not safe (sticky bit not set when world writable?)";
387 return ();
388 }
389 } elsif (File::Temp->safe_level == HIGH) {
390 unless (_is_verysafe($parent)) {
391 carp "File::Temp::_gettemp: Parent directory ($parent) is not safe (sticky bit not set when world writable?)";
392 return ();
393 }
394 }
395
396
262eb13a 397 # Now try MAX_TRIES time to open the file
398 for (my $i = 0; $i < MAX_TRIES; $i++) {
399
400 # Try to open the file if requested
401 if ($options{"open"}) {
402 my $fh;
403
404 # If we are running before perl5.6.0 we can not auto-vivify
405 if ($] < 5.006) {
262eb13a 406 $fh = &Symbol::gensym;
407 }
408
409 # Try to make sure this will be marked close-on-exec
410 # XXX: Win32 doesn't respect this, nor the proper fcntl,
411 # but may have O_NOINHERIT. This may or may not be in Fcntl.
412 local $^F = 2;
413
414 # Store callers umask
415 my $umask = umask();
416
417 # Set a known umask
418 umask(066);
419
420 # Attempt to open the file
51fc852f 421 my $open_success = undef;
f826e675 422 if ( $^O eq 'VMS' and $options{"unlink_on_close"} ) {
423 # make it auto delete on close by setting FAB$V_DLT bit
51fc852f 424 $fh = VMS::Stdio::vmssysopen($path, $OPENFLAGS, 0600, 'fop=dlt');
425 $open_success = $fh;
426 } else {
427 my $flags = ( $options{"unlink_on_close"} ?
428 $OPENTEMPFLAGS :
429 $OPENFLAGS );
430 $open_success = sysopen($fh, $path, $flags, 0600);
431 }
432 if ( $open_success ) {
262eb13a 433
434 # Reset umask
435 umask($umask);
436
437 # Opened successfully - return file handle and name
438 return ($fh, $path);
439
440 } else {
441 # Reset umask
442 umask($umask);
443
444 # Error opening file - abort with error
445 # if the reason was anything but EEXIST
11d7f64f 446 unless ($!{EEXIST}) {
262eb13a 447 carp "File::Temp: Could not create temp file $path: $!";
448 return ();
449 }
450
451 # Loop round for another try
452
453 }
454 } elsif ($options{"mkdir"}) {
455
456 # Store callers umask
457 my $umask = umask();
458
459 # Set a known umask
460 umask(066);
461
462 # Open the temp directory
463 if (mkdir( $path, 0700)) {
464 # created okay
465 # Reset umask
466 umask($umask);
467
468 return undef, $path;
469 } else {
470
471 # Reset umask
472 umask($umask);
473
474 # Abort with error if the reason for failure was anything
475 # except EEXIST
11d7f64f 476 unless ($!{EEXIST}) {
262eb13a 477 carp "File::Temp: Could not create directory $path: $!";
478 return ();
479 }
480
481 # Loop round for another try
482
483 }
484
485 } else {
486
487 # Return true if the file can not be found
488 # Directory has been checked previously
489
490 return (undef, $path) unless -e $path;
491
669b450a 492 # Try again until MAX_TRIES
262eb13a 493
494 }
669b450a 495
262eb13a 496 # Did not successfully open the tempfile/dir
497 # so try again with a different set of random letters
498 # No point in trying to increment unless we have only
499 # 1 X say and the randomness could come up with the same
500 # file MAX_TRIES in a row.
501
502 # Store current attempt - in principal this implies that the
503 # 3rd time around the open attempt that the first temp file
504 # name could be generated again. Probably should store each
505 # attempt and make sure that none are repeated
506
507 my $original = $path;
508 my $counter = 0; # Stop infinite loop
509 my $MAX_GUESS = 50;
510
511 do {
512
513 # Generate new name from original template
514 $path = _replace_XX($template, $options{"suffixlen"});
515
516 $counter++;
517
518 } until ($path ne $original || $counter > $MAX_GUESS);
519
520 # Check for out of control looping
521 if ($counter > $MAX_GUESS) {
1c19c868 522 carp "Tried to get a new temp name different to the previous value $MAX_GUESS times.\nSomething wrong with template?? ($template)";
262eb13a 523 return ();
524 }
525
526 }
527
528 # If we get here, we have run out of tries
529 carp "Have exceeded the maximum number of attempts (".MAX_TRIES .
530 ") to open temp file/dir";
531
532 return ();
533
534}
535
536# Internal routine to return a random character from the
537# character list. Does not do an srand() since rand()
538# will do one automatically
539
540# No arguments. Return value is the random character
541
1c19c868 542# No longer called since _replace_XX runs a few percent faster if
543# I inline the code. This is important if we are creating thousands of
544# temporary files.
545
262eb13a 546sub _randchar {
547
548 $CHARS[ int( rand( $#CHARS ) ) ];
549
550}
551
552# Internal routine to replace the XXXX... with random characters
553# This has to be done by _gettemp() every time it fails to
554# open a temp file/dir
555
556# Arguments: $template (the template with XXX),
557# $ignore (number of characters at end to ignore)
558
559# Returns: modified template
560
561sub _replace_XX {
562
563 croak 'Usage: _replace_XX($template, $ignore)'
564 unless scalar(@_) == 2;
565
566 my ($path, $ignore) = @_;
567
568 # Do it as an if, since the suffix adjusts which section to replace
569 # and suffixlen=0 returns nothing if used in the substr directly
570 # Alternatively, could simply set $ignore to length($path)-1
571 # Don't want to always use substr when not required though.
572
573 if ($ignore) {
1c19c868 574 substr($path, 0, - $ignore) =~ s/X(?=X*\z)/$CHARS[ int( rand( $#CHARS ) ) ]/ge;
262eb13a 575 } else {
1c19c868 576 $path =~ s/X(?=X*\z)/$CHARS[ int( rand( $#CHARS ) ) ]/ge;
262eb13a 577 }
578
579 return $path;
580}
581
582# internal routine to check to see if the directory is safe
669b450a 583# First checks to see if the directory is not owned by the
262eb13a 584# current user or root. Then checks to see if anyone else
669b450a 585# can write to the directory and if so, checks to see if
262eb13a 586# it has the sticky bit set
587
588# Will not work on systems that do not support sticky bit
589
590#Args: directory path to check
591# Returns true if the path is safe and false otherwise.
592# Returns undef if can not even run stat() on the path
593
594# This routine based on version written by Tom Christiansen
595
596# Presumably, by the time we actually attempt to create the
597# file or directory in this directory, it may not be safe
598# anymore... Have to run _is_safe directly after the open.
599
600sub _is_safe {
601
602 my $path = shift;
603
604 # Stat path
605 my @info = stat($path);
606 return 0 unless scalar(@info);
669b450a 607 return 1 if $^O eq 'VMS'; # owner delete control at file level
262eb13a 608
609 # Check to see whether owner is neither superuser (or a system uid) nor me
610 # Use the real uid from the $< variable
611 # UID is in [4]
73f754d1 612 if ($info[4] > File::Temp->top_system_uid() && $info[4] != $<) {
613
614 Carp::cluck(sprintf "uid=$info[4] topuid=%s \$<=$< path='$path'",
615 File::Temp->top_system_uid());
616
617 carp "Directory owned neither by root nor the current user.";
262eb13a 618 return 0;
619 }
620
621 # check whether group or other can write file
622 # use 066 to detect either reading or writing
623 # use 022 to check writability
624 # Do it with S_IWOTH and S_IWGRP for portability (maybe)
625 # mode is in info[2]
626 if (($info[2] & &Fcntl::S_IWGRP) || # Is group writable?
627 ($info[2] & &Fcntl::S_IWOTH) ) { # Is world writable?
628 return 0 unless -d _; # Must be a directory
629 return 0 unless -k _; # Must be sticky
630 }
631
632 return 1;
633}
634
635# Internal routine to check whether a directory is safe
636# for temp files. Safer than _is_safe since it checks for
637# the possibility of chown giveaway and if that is a possibility
638# checks each directory in the path to see if it is safe (with _is_safe)
639
640# If _PC_CHOWN_RESTRICTED is not set, does the full test of each
641# directory anyway.
642
643sub _is_verysafe {
644
645 # Need POSIX - but only want to bother if really necessary due to overhead
646 require POSIX;
647
648 my $path = shift;
669b450a 649 return 1 if $^O eq 'VMS'; # owner delete control at file level
262eb13a 650
651 # Should Get the value of _PC_CHOWN_RESTRICTED if it is defined
652 # and If it is not there do the extensive test
653 my $chown_restricted;
654 $chown_restricted = &POSIX::_PC_CHOWN_RESTRICTED()
655 if eval { &POSIX::_PC_CHOWN_RESTRICTED(); 1};
656
657 # If chown_resticted is set to some value we should test it
658 if (defined $chown_restricted) {
659
660 # Return if the current directory is safe
661 return _is_safe($path) if POSIX::sysconf( $chown_restricted );
662
663 }
664
665 # To reach this point either, the _PC_CHOWN_RESTRICTED symbol
666 # was not avialable or the symbol was there but chown giveaway
667 # is allowed. Either way, we now have to test the entire tree for
668 # safety.
669
670 # Convert path to an absolute directory if required
671 unless (File::Spec->file_name_is_absolute($path)) {
672 $path = File::Spec->rel2abs($path);
673 }
674
675 # Split directory into components - assume no file
676 my ($volume, $directories, undef) = File::Spec->splitpath( $path, 1);
677
678 # Slightly less efficient than having a a function in File::Spec
679 # to chop off the end of a directory or even a function that
680 # can handle ../ in a directory tree
681 # Sometimes splitdir() returns a blank at the end
682 # so we will probably check the bottom directory twice in some cases
683 my @dirs = File::Spec->splitdir($directories);
684
685 # Concatenate one less directory each time around
686 foreach my $pos (0.. $#dirs) {
687 # Get a directory name
688 my $dir = File::Spec->catpath($volume,
689 File::Spec->catdir(@dirs[0.. $#dirs - $pos]),
690 ''
691 );
692
693 print "TESTING DIR $dir\n" if $DEBUG;
694
695 # Check the directory
696 return 0 unless _is_safe($dir);
697
698 }
699
700 return 1;
701}
702
703
704
705# internal routine to determine whether unlink works on this
706# platform for files that are currently open.
707# Returns true if we can, false otherwise.
708
669b450a 709# Currently WinNT, OS/2 and VMS can not unlink an opened file
710# On VMS this is because the O_EXCL flag is used to open the
711# temporary file. Currently I do not know enough about the issues
712# on VMS to decide whether O_EXCL is a requirement.
262eb13a 713
714sub _can_unlink_opened_file {
715
e77f578c 716 if ($^O eq 'MSWin32' || $^O eq 'os2' || $^O eq 'VMS' || $^O eq 'dos') {
1c19c868 717 return 0;
718 } else {
719 return 1;
720 }
262eb13a 721
722}
723
1c19c868 724# internal routine to decide which security levels are allowed
725# see safe_level() for more information on this
726
727# Controls whether the supplied security level is allowed
728
729# $cando = _can_do_level( $level )
730
731sub _can_do_level {
732
733 # Get security level
734 my $level = shift;
735
736 # Always have to be able to do STANDARD
737 return 1 if $level == STANDARD;
738
739 # Currently, the systems that can do HIGH or MEDIUM are identical
6bbf1b34 740 if ( $^O eq 'MSWin32' || $^O eq 'os2' || $^O eq 'cygwin' || $^O eq 'dos') {
1c19c868 741 return 0;
742 } else {
743 return 1;
744 }
745
746}
262eb13a 747
748# This routine sets up a deferred unlinking of a specified
749# filename and filehandle. It is used in the following cases:
669b450a 750# - Called by unlink0 if an opened file can not be unlinked
262eb13a 751# - Called by tempfile() if files are to be removed on shutdown
752# - Called by tempdir() if directories are to be removed on shutdown
753
754# Arguments:
755# _deferred_unlink( $fh, $fname, $isdir );
756#
757# - filehandle (so that it can be expclicitly closed if open
758# - filename (the thing we want to remove)
759# - isdir (flag to indicate that we are being given a directory)
760# [and hence no filehandle]
761
51fc852f 762# Status is not referred to since all the magic is done with an END block
262eb13a 763
1c19c868 764{
765 # Will set up two lexical variables to contain all the files to be
766 # removed. One array for files, another for directories
767 # They will only exist in this block
768 # This means we only have to set up a single END block to remove all files
769 # @files_to_unlink contains an array ref with the filehandle and filename
770 my (@files_to_unlink, @dirs_to_unlink);
771
772 # Set up an end block to use these arrays
773 END {
774 # Files
775 foreach my $file (@files_to_unlink) {
776 # close the filehandle without checking its state
777 # in order to make real sure that this is closed
778 # if its already closed then I dont care about the answer
779 # probably a better way to do this
780 close($file->[0]); # file handle is [0]
781
782 if (-f $file->[1]) { # file name is [1]
783 unlink $file->[1] or warn "Error removing ".$file->[1];
784 }
785 }
786 # Dirs
787 foreach my $dir (@dirs_to_unlink) {
788 if (-d $dir) {
789 rmtree($dir, $DEBUG, 1);
790 }
791 }
262eb13a 792
262eb13a 793
1c19c868 794 }
262eb13a 795
1c19c868 796 # This is the sub called to register a file for deferred unlinking
797 # This could simply store the input parameters and defer everything
798 # until the END block. For now we do a bit of checking at this
799 # point in order to make sure that (1) we have a file/dir to delete
800 # and (2) we have been called with the correct arguments.
801 sub _deferred_unlink {
802
803 croak 'Usage: _deferred_unlink($fh, $fname, $isdir)'
804 unless scalar(@_) == 3;
669b450a 805
1c19c868 806 my ($fh, $fname, $isdir) = @_;
262eb13a 807
1c19c868 808 warn "Setting up deferred removal of $fname\n"
809 if $DEBUG;
669b450a 810
1c19c868 811 # If we have a directory, check that it is a directory
812 if ($isdir) {
262eb13a 813
1c19c868 814 if (-d $fname) {
262eb13a 815
1c19c868 816 # Directory exists so store it
51fc852f 817 # first on VMS turn []foo into [.foo] for rmtree
818 $fname = VMS::Filespec::vmspath($fname) if $^O eq 'VMS';
1c19c868 819 push (@dirs_to_unlink, $fname);
262eb13a 820
1c19c868 821 } else {
51fc852f 822 carp "Request to remove directory $fname could not be completed since it does not exist!\n";
1c19c868 823 }
824
262eb13a 825 } else {
262eb13a 826
1c19c868 827 if (-f $fname) {
262eb13a 828
1c19c868 829 # file exists so store handle and name for later removal
830 push(@files_to_unlink, [$fh, $fname]);
262eb13a 831
1c19c868 832 } else {
833 carp "Request to remove file $fname could not be completed since it is not there!\n";
834 }
262eb13a 835
262eb13a 836 }
837
262eb13a 838 }
839
262eb13a 840
1c19c868 841}
262eb13a 842
843=head1 FUNCTIONS
844
845This section describes the recommended interface for generating
846temporary files and directories.
847
848=over 4
849
850=item B<tempfile>
851
852This is the basic function to generate temporary files.
853The behaviour of the file can be changed using various options:
854
855 ($fh, $filename) = tempfile();
856
857Create a temporary file in the directory specified for temporary
858files, as specified by the tmpdir() function in L<File::Spec>.
859
860 ($fh, $filename) = tempfile($template);
861
862Create a temporary file in the current directory using the supplied
863template. Trailing `X' characters are replaced with random letters to
864generate the filename. At least four `X' characters must be present
865in the template.
866
867 ($fh, $filename) = tempfile($template, SUFFIX => $suffix)
868
869Same as previously, except that a suffix is added to the template
870after the `X' translation. Useful for ensuring that a temporary
871filename has a particular extension when needed by other applications.
872But see the WARNING at the end.
873
874 ($fh, $filename) = tempfile($template, DIR => $dir);
875
876Translates the template as before except that a directory name
877is specified.
878
51fc852f 879 ($fh, $filename) = tempfile($template, UNLINK => 1);
880
881Return the filename and filehandle as before except that the file is
882automatically removed when the program exits. Default is for the file
883to be removed if a file handle is requested and to be kept if the
09d7a2f9 884filename is requested. In a scalar context (where no filename is
885returned) the file is always deleted either on exit or when it is closed.
51fc852f 886
262eb13a 887If the template is not specified, a template is always
888automatically generated. This temporary file is placed in tmpdir()
889(L<File::Spec>) unless a directory is specified explicitly with the
890DIR option.
891
892 $fh = tempfile( $template, DIR => $dir );
893
894If called in scalar context, only the filehandle is returned
895and the file will automatically be deleted when closed (see
896the description of tmpfile() elsewhere in this document).
897This is the preferred mode of operation, as if you only
898have a filehandle, you can never create a race condition
899by fumbling with the filename. On systems that can not unlink
09d7a2f9 900an open file or can not mark a file as temporary when it is opened
901(for example, Windows NT uses the C<O_TEMPORARY> flag))
902the file is marked for deletion when the program ends (equivalent
903to setting UNLINK to 1). The C<UNLINK> flag is ignored if present.
904
262eb13a 905
906 (undef, $filename) = tempfile($template, OPEN => 0);
907
908This will return the filename based on the template but
909will not open this file. Cannot be used in conjunction with
910UNLINK set to true. Default is to always open the file
911to protect from possible race conditions. A warning is issued
912if warnings are turned on. Consider using the tmpnam()
913and mktemp() functions described elsewhere in this document
914if opening the file is not required.
915
51fc852f 916Options can be combined as required.
917
262eb13a 918=cut
919
920sub tempfile {
921
922 # Can not check for argument count since we can have any
923 # number of args
924
925 # Default options
926 my %options = (
927 "DIR" => undef, # Directory prefix
f826e675 928 "SUFFIX" => '', # Template suffix
929 "UNLINK" => 0, # Do not unlink file on exit
930 "OPEN" => 1, # Open file
262eb13a 931 );
932
933 # Check to see whether we have an odd or even number of arguments
934 my $template = (scalar(@_) % 2 == 1 ? shift(@_) : undef);
935
936 # Read the options and merge with defaults
937 %options = (%options, @_) if @_;
938
939 # First decision is whether or not to open the file
940 if (! $options{"OPEN"}) {
941
942 warn "tempfile(): temporary filename requested but not opened.\nPossibly unsafe, consider using tempfile() with OPEN set to true\n"
943 if $^W;
944
945 }
946
f826e675 947 if ($options{"DIR"} and $^O eq 'VMS') {
948
949 # on VMS turn []foo into [.foo] for concatenation
950 $options{"DIR"} = VMS::Filespec::vmspath($options{"DIR"});
951 }
952
669b450a 953 # Construct the template
262eb13a 954
955 # Have a choice of trying to work around the mkstemp/mktemp/tmpnam etc
956 # functions or simply constructing a template and using _gettemp()
957 # explicitly. Go for the latter
958
959 # First generate a template if not defined and prefix the directory
960 # If no template must prefix the temp directory
961 if (defined $template) {
962 if ($options{"DIR"}) {
963
964 $template = File::Spec->catfile($options{"DIR"}, $template);
965
966 }
967
968 } else {
969
970 if ($options{"DIR"}) {
971
972 $template = File::Spec->catfile($options{"DIR"}, TEMPXXX);
973
974 } else {
669b450a 975
262eb13a 976 $template = File::Spec->catfile(File::Spec->tmpdir, TEMPXXX);
977
978 }
669b450a 979
262eb13a 980 }
981
982 # Now add a suffix
983 $template .= $options{"SUFFIX"};
984
09d7a2f9 985 # Determine whether we should tell _gettemp to unlink the file
986 # On unix this is irrelevant and can be worked out after the file is
987 # opened (simply by unlinking the open filehandle). On Windows or VMS
988 # we have to indicate temporary-ness when we open the file. In general
989 # we only want a true temporary file if we are returning just the
990 # filehandle - if the user wants the filename they probably do not
991 # want the file to disappear as soon as they close it.
992 # For this reason, tie unlink_on_close to the return context regardless
993 # of OS.
994 my $unlink_on_close = ( wantarray ? 0 : 1);
995
262eb13a 996 # Create the file
997 my ($fh, $path);
998 croak "Error in tempfile() using $template"
999 unless (($fh, $path) = _gettemp($template,
51fc852f 1000 "open" => $options{'OPEN'},
262eb13a 1001 "mkdir"=> 0 ,
09d7a2f9 1002 "unlink_on_close" => $unlink_on_close,
262eb13a 1003 "suffixlen" => length($options{'SUFFIX'}),
669b450a 1004 ) );
262eb13a 1005
1006 # Set up an exit handler that can do whatever is right for the
09d7a2f9 1007 # system. This removes files at exit when requested explicitly or when
1008 # system is asked to unlink_on_close but is unable to do so because
1009 # of OS limitations.
1010 # The latter should be achieved by using a tied filehandle.
1011 # Do not check return status since this is all done with END blocks.
262eb13a 1012 _deferred_unlink($fh, $path, 0) if $options{"UNLINK"};
669b450a 1013
262eb13a 1014 # Return
1015 if (wantarray()) {
1016
1017 if ($options{'OPEN'}) {
1018 return ($fh, $path);
1019 } else {
1020 return (undef, $path);
1021 }
1022
1023 } else {
1024
1025 # Unlink the file. It is up to unlink0 to decide what to do with
1026 # this (whether to unlink now or to defer until later)
1027 unlink0($fh, $path) or croak "Error unlinking file $path using unlink0";
669b450a 1028
262eb13a 1029 # Return just the filehandle.
1030 return $fh;
1031 }
1032
1033
1034}
1035
1036=item B<tempdir>
1037
1038This is the recommended interface for creation of temporary directories.
1039The behaviour of the function depends on the arguments:
1040
1041 $tempdir = tempdir();
1042
1043Create a directory in tmpdir() (see L<File::Spec|File::Spec>).
1044
1045 $tempdir = tempdir( $template );
1046
1047Create a directory from the supplied template. This template is
1048similar to that described for tempfile(). `X' characters at the end
1049of the template are replaced with random letters to construct the
1050directory name. At least four `X' characters must be in the template.
1051
1052 $tempdir = tempdir ( DIR => $dir );
1053
1054Specifies the directory to use for the temporary directory.
1055The temporary directory name is derived from an internal template.
1056
1057 $tempdir = tempdir ( $template, DIR => $dir );
1058
1059Prepend the supplied directory name to the template. The template
1060should not include parent directory specifications itself. Any parent
1061directory specifications are removed from the template before
1062prepending the supplied directory.
1063
1064 $tempdir = tempdir ( $template, TMPDIR => 1 );
1065
1066Using the supplied template, creat the temporary directory in
1067a standard location for temporary files. Equivalent to doing
1068
1069 $tempdir = tempdir ( $template, DIR => File::Spec->tmpdir);
1070
1071but shorter. Parent directory specifications are stripped from the
1072template itself. The C<TMPDIR> option is ignored if C<DIR> is set
1073explicitly. Additionally, C<TMPDIR> is implied if neither a template
1074nor a directory are supplied.
1075
1076 $tempdir = tempdir( $template, CLEANUP => 1);
1077
1078Create a temporary directory using the supplied template, but
1079attempt to remove it (and all files inside it) when the program
1080exits. Note that an attempt will be made to remove all files from
1081the directory even if they were not created by this module (otherwise
1082why ask to clean it up?). The directory removal is made with
1083the rmtree() function from the L<File::Path|File::Path> module.
1084Of course, if the template is not specified, the temporary directory
1085will be created in tmpdir() and will also be removed at program exit.
1086
1087=cut
1088
1089# '
1090
1091sub tempdir {
1092
1093 # Can not check for argument count since we can have any
1094 # number of args
1095
1096 # Default options
1097 my %options = (
1098 "CLEANUP" => 0, # Remove directory on exit
1099 "DIR" => '', # Root directory
1100 "TMPDIR" => 0, # Use tempdir with template
1101 );
1102
1103 # Check to see whether we have an odd or even number of arguments
1104 my $template = (scalar(@_) % 2 == 1 ? shift(@_) : undef );
1105
1106 # Read the options and merge with defaults
1107 %options = (%options, @_) if @_;
1108
1109 # Modify or generate the template
1110
1111 # Deal with the DIR and TMPDIR options
1112 if (defined $template) {
1113
1114 # Need to strip directory path if using DIR or TMPDIR
1115 if ($options{'TMPDIR'} || $options{'DIR'}) {
1116
1117 # Strip parent directory from the filename
51fc852f 1118 #
262eb13a 1119 # There is no filename at the end
51fc852f 1120 $template = VMS::Filespec::vmspath($template) if $^O eq 'VMS';
262eb13a 1121 my ($volume, $directories, undef) = File::Spec->splitpath( $template, 1);
1122
1123 # Last directory is then our template
1124 $template = (File::Spec->splitdir($directories))[-1];
1125
1126 # Prepend the supplied directory or temp dir
1127 if ($options{"DIR"}) {
1128
e4dfc136 1129 $template = File::Spec->catdir($options{"DIR"}, $template);
262eb13a 1130
1131 } elsif ($options{TMPDIR}) {
1132
1133 # Prepend tmpdir
1134 $template = File::Spec->catdir(File::Spec->tmpdir, $template);
1135
1136 }
1137
1138 }
1139
1140 } else {
1141
1142 if ($options{"DIR"}) {
1143
1144 $template = File::Spec->catdir($options{"DIR"}, TEMPXXX);
1145
1146 } else {
669b450a 1147
262eb13a 1148 $template = File::Spec->catdir(File::Spec->tmpdir, TEMPXXX);
1149
1150 }
669b450a 1151
262eb13a 1152 }
1153
1154 # Create the directory
1155 my $tempdir;
669b450a 1156 my $suffixlen = 0;
1157 if ($^O eq 'VMS') { # dir names can end in delimiters
1158 $template =~ m/([\.\]:>]+)$/;
1159 $suffixlen = length($1);
1160 }
262eb13a 1161 croak "Error in tempdir() using $template"
1162 unless ((undef, $tempdir) = _gettemp($template,
669b450a 1163 "open" => 0,
262eb13a 1164 "mkdir"=> 1 ,
669b450a 1165 "suffixlen" => $suffixlen,
1166 ) );
1167
262eb13a 1168 # Install exit handler; must be dynamic to get lexical
669b450a 1169 if ( $options{'CLEANUP'} && -d $tempdir) {
262eb13a 1170 _deferred_unlink(undef, $tempdir, 1);
669b450a 1171 }
262eb13a 1172
1173 # Return the dir name
1174 return $tempdir;
1175
1176}
1177
1178=back
1179
1180=head1 MKTEMP FUNCTIONS
1181
1182The following functions are Perl implementations of the
1183mktemp() family of temp file generation system calls.
1184
1185=over 4
1186
1187=item B<mkstemp>
1188
1189Given a template, returns a filehandle to the temporary file and the name
1190of the file.
1191
1192 ($fh, $name) = mkstemp( $template );
1193
1194In scalar context, just the filehandle is returned.
1195
1196The template may be any filename with some number of X's appended
1197to it, for example F</tmp/temp.XXXX>. The trailing X's are replaced
1198with unique alphanumeric combinations.
1199
1200=cut
1201
1202
1203
1204sub mkstemp {
1205
1206 croak "Usage: mkstemp(template)"
1207 if scalar(@_) != 1;
1208
1209 my $template = shift;
1210
1211 my ($fh, $path);
1212 croak "Error in mkstemp using $template"
669b450a 1213 unless (($fh, $path) = _gettemp($template,
1214 "open" => 1,
262eb13a 1215 "mkdir"=> 0 ,
1216 "suffixlen" => 0,
1217 ) );
1218
1219 if (wantarray()) {
1220 return ($fh, $path);
1221 } else {
1222 return $fh;
1223 }
1224
1225}
1226
1227
1228=item B<mkstemps>
1229
1230Similar to mkstemp(), except that an extra argument can be supplied
1231with a suffix to be appended to the template.
1232
1233 ($fh, $name) = mkstemps( $template, $suffix );
1234
1235For example a template of C<testXXXXXX> and suffix of C<.dat>
1236would generate a file similar to F<testhGji_w.dat>.
1237
1238Returns just the filehandle alone when called in scalar context.
1239
1240=cut
1241
1242sub mkstemps {
1243
1244 croak "Usage: mkstemps(template, suffix)"
1245 if scalar(@_) != 2;
1246
1247
1248 my $template = shift;
1249 my $suffix = shift;
1250
1251 $template .= $suffix;
669b450a 1252
262eb13a 1253 my ($fh, $path);
1254 croak "Error in mkstemps using $template"
1255 unless (($fh, $path) = _gettemp($template,
1256 "open" => 1,
1257 "mkdir"=> 0 ,
1258 "suffixlen" => length($suffix),
1259 ) );
1260
1261 if (wantarray()) {
1262 return ($fh, $path);
1263 } else {
1264 return $fh;
1265 }
1266
1267}
1268
1269=item B<mkdtemp>
1270
1271Create a directory from a template. The template must end in
1272X's that are replaced by the routine.
1273
1274 $tmpdir_name = mkdtemp($template);
1275
1276Returns the name of the temporary directory created.
1277Returns undef on failure.
1278
1279Directory must be removed by the caller.
1280
1281=cut
1282
1283#' # for emacs
1284
1285sub mkdtemp {
1286
1287 croak "Usage: mkdtemp(template)"
1288 if scalar(@_) != 1;
262eb13a 1289
669b450a 1290 my $template = shift;
1291 my $suffixlen = 0;
1292 if ($^O eq 'VMS') { # dir names can end in delimiters
1293 $template =~ m/([\.\]:>]+)$/;
1294 $suffixlen = length($1);
1295 }
262eb13a 1296 my ($junk, $tmpdir);
1297 croak "Error creating temp directory from template $template\n"
1298 unless (($junk, $tmpdir) = _gettemp($template,
669b450a 1299 "open" => 0,
262eb13a 1300 "mkdir"=> 1 ,
669b450a 1301 "suffixlen" => $suffixlen,
262eb13a 1302 ) );
1303
1304 return $tmpdir;
1305
1306}
1307
1308=item B<mktemp>
1309
1310Returns a valid temporary filename but does not guarantee
1311that the file will not be opened by someone else.
1312
1313 $unopened_file = mktemp($template);
1314
1315Template is the same as that required by mkstemp().
1316
1317=cut
1318
1319sub mktemp {
1320
1321 croak "Usage: mktemp(template)"
1322 if scalar(@_) != 1;
1323
1324 my $template = shift;
1325
1326 my ($tmpname, $junk);
1327 croak "Error getting name to temp file from template $template\n"
1328 unless (($junk, $tmpname) = _gettemp($template,
669b450a 1329 "open" => 0,
262eb13a 1330 "mkdir"=> 0 ,
1331 "suffixlen" => 0,
1332 ) );
1333
1334 return $tmpname;
1335}
1336
1337=back
1338
1339=head1 POSIX FUNCTIONS
1340
1341This section describes the re-implementation of the tmpnam()
1342and tmpfile() functions described in L<POSIX>
1343using the mkstemp() from this module.
1344
1345Unlike the L<POSIX|POSIX> implementations, the directory used
1346for the temporary file is not specified in a system include
1347file (C<P_tmpdir>) but simply depends on the choice of tmpdir()
1348returned by L<File::Spec|File::Spec>. On some implementations this
1349location can be set using the C<TMPDIR> environment variable, which
1350may not be secure.
1351If this is a problem, simply use mkstemp() and specify a template.
1352
1353=over 4
1354
1355=item B<tmpnam>
1356
1357When called in scalar context, returns the full name (including path)
1358of a temporary file (uses mktemp()). The only check is that the file does
1359not already exist, but there is no guarantee that that condition will
1360continue to apply.
1361
1362 $file = tmpnam();
1363
1364When called in list context, a filehandle to the open file and
1365a filename are returned. This is achieved by calling mkstemp()
1366after constructing a suitable template.
1367
1368 ($fh, $file) = tmpnam();
1369
1370If possible, this form should be used to prevent possible
1371race conditions.
1372
1373See L<File::Spec/tmpdir> for information on the choice of temporary
1374directory for a particular operating system.
1375
1376=cut
1377
1378sub tmpnam {
1379
1380 # Retrieve the temporary directory name
1381 my $tmpdir = File::Spec->tmpdir;
1382
1383 croak "Error temporary directory is not writable"
1384 if $tmpdir eq '';
1385
1386 # Use a ten character template and append to tmpdir
1387 my $template = File::Spec->catfile($tmpdir, TEMPXXX);
669b450a 1388
262eb13a 1389 if (wantarray() ) {
1390 return mkstemp($template);
1391 } else {
1392 return mktemp($template);
1393 }
1394
1395}
1396
1397=item B<tmpfile>
1398
1399In scalar context, returns the filehandle of a temporary file.
1400
1401 $fh = tmpfile();
1402
1403The file is removed when the filehandle is closed or when the program
1404exits. No access to the filename is provided.
1405
1406=cut
1407
1408sub tmpfile {
1409
91e74348 1410 # Simply call tmpnam() in a list context
262eb13a 1411 my ($fh, $file) = tmpnam();
1412
1413 # Make sure file is removed when filehandle is closed
1414 unlink0($fh, $file) or croak "Unable to unlink temporary file: $!";
1415
1416 return $fh;
1417
1418}
1419
1420=back
1421
1422=head1 ADDITIONAL FUNCTIONS
1423
1424These functions are provided for backwards compatibility
1425with common tempfile generation C library functions.
1426
1427They are not exported and must be addressed using the full package
1428name.
1429
1430=over 4
1431
1432=item B<tempnam>
1433
1434Return the name of a temporary file in the specified directory
1435using a prefix. The file is guaranteed not to exist at the time
1436the function was called, but such guarantees are good for one
1437clock tick only. Always use the proper form of C<sysopen>
1438with C<O_CREAT | O_EXCL> if you must open such a filename.
1439
1440 $filename = File::Temp::tempnam( $dir, $prefix );
1441
1442Equivalent to running mktemp() with $dir/$prefixXXXXXXXX
1443(using unix file convention as an example)
1444
1445Because this function uses mktemp(), it can suffer from race conditions.
1446
1447=cut
1448
1449sub tempnam {
1450
1451 croak 'Usage tempnam($dir, $prefix)' unless scalar(@_) == 2;
1452
1453 my ($dir, $prefix) = @_;
1454
1455 # Add a string to the prefix
1456 $prefix .= 'XXXXXXXX';
1457
1458 # Concatenate the directory to the file
1459 my $template = File::Spec->catfile($dir, $prefix);
1460
1461 return mktemp($template);
1462
1463}
1464
1465=back
1466
1467=head1 UTILITY FUNCTIONS
1468
1469Useful functions for dealing with the filehandle and filename.
1470
1471=over 4
1472
1473=item B<unlink0>
1474
1475Given an open filehandle and the associated filename, make a safe
1476unlink. This is achieved by first checking that the filename and
1477filehandle initially point to the same file and that the number of
1478links to the file is 1 (all fields returned by stat() are compared).
1479Then the filename is unlinked and the filehandle checked once again to
1480verify that the number of links on that file is now 0. This is the
1481closest you can come to making sure that the filename unlinked was the
1482same as the file whose descriptor you hold.
1483
1484 unlink0($fh, $path) or die "Error unlinking file $path safely";
1485
1486Returns false on error. The filehandle is not closed since on some
1487occasions this is not required.
1488
1489On some platforms, for example Windows NT, it is not possible to
1490unlink an open file (the file must be closed first). On those
1c19c868 1491platforms, the actual unlinking is deferred until the program ends and
1492good status is returned. A check is still performed to make sure that
1493the filehandle and filename are pointing to the same thing (but not at
1494the time the end block is executed since the deferred removal may not
1495have access to the filehandle).
262eb13a 1496
1497Additionally, on Windows NT not all the fields returned by stat() can
51fc852f 1498be compared. For example, the C<dev> and C<rdev> fields seem to be
1499different. Also, it seems that the size of the file returned by stat()
262eb13a 1500does not always agree, with C<stat(FH)> being more accurate than
1501C<stat(filename)>, presumably because of caching issues even when
1502using autoflush (this is usually overcome by waiting a while after
1503writing to the tempfile before attempting to C<unlink0> it).
1504
1c19c868 1505Finally, on NFS file systems the link count of the file handle does
1506not always go to zero immediately after unlinking. Currently, this
1507command is expected to fail on NFS disks.
1508
262eb13a 1509=cut
1510
1511sub unlink0 {
1512
1513 croak 'Usage: unlink0(filehandle, filename)'
1514 unless scalar(@_) == 2;
1515
1516 # Read args
1517 my ($fh, $path) = @_;
1518
1519 warn "Unlinking $path using unlink0\n"
1520 if $DEBUG;
1521
1522 # Stat the filehandle
1523 my @fh = stat $fh;
1524
1525 if ($fh[3] > 1 && $^W) {
1526 carp "unlink0: fstat found too many links; SB=@fh";
669b450a 1527 }
262eb13a 1528
1529 # Stat the path
1530 my @path = stat $path;
1531
1532 unless (@path) {
1533 carp "unlink0: $path is gone already" if $^W;
1534 return;
669b450a 1535 }
262eb13a 1536
1537 # this is no longer a file, but may be a directory, or worse
1538 unless (-f _) {
1539 confess "panic: $path is no longer a file: SB=@fh";
669b450a 1540 }
262eb13a 1541
1542 # Do comparison of each member of the array
1543 # On WinNT dev and rdev seem to be different
1544 # depending on whether it is a file or a handle.
1545 # Cannot simply compare all members of the stat return
1546 # Select the ones we can use
1547 my @okstat = (0..$#fh); # Use all by default
1548 if ($^O eq 'MSWin32') {
1549 @okstat = (1,2,3,4,5,7,8,9,10);
669b450a 1550 } elsif ($^O eq 'os2') {
d62e1b7f 1551 @okstat = (0, 2..$#fh);
51fc852f 1552 } elsif ($^O eq 'VMS') { # device and file ID are sufficient
1553 @okstat = (0, 1);
6bbf1b34 1554 } elsif ($^O eq 'dos') {
1555 @okstat = (0,2..7,11..$#fh);
262eb13a 1556 }
1557
1558 # Now compare each entry explicitly by number
1559 for (@okstat) {
1560 print "Comparing: $_ : $fh[$_] and $path[$_]\n" if $DEBUG;
d62e1b7f 1561 # Use eq rather than == since rdev, blksize, and blocks (6, 11,
1562 # and 12) will be '' on platforms that do not support them. This
1563 # is fine since we are only comparing integers.
669b450a 1564 unless ($fh[$_] eq $path[$_]) {
262eb13a 1565 warn "Did not match $_ element of stat\n" if $DEBUG;
1566 return 0;
1567 }
1568 }
669b450a 1569
262eb13a 1570 # attempt remove the file (does not work on some platforms)
1571 if (_can_unlink_opened_file()) {
1572 # XXX: do *not* call this on a directory; possible race
1573 # resulting in recursive removal
1574 croak "unlink0: $path has become a directory!" if -d $path;
1575 unlink($path) or return 0;
1576
1577 # Stat the filehandle
1578 @fh = stat $fh;
1579
1580 print "Link count = $fh[3] \n" if $DEBUG;
1581
1582 # Make sure that the link count is zero
ceb977da 1583 # - Cygwin provides deferred unlinking, however,
1584 # on Win9x the link count remains 1
1585 return ( $fh[3] == 0 or $^O eq 'cygwin' ? 1 : 0);
262eb13a 1586
1587 } else {
1588 _deferred_unlink($fh, $path, 0);
1589 return 1;
1590 }
1591
1592}
1593
1594=back
1595
1596=head1 PACKAGE VARIABLES
1597
1598These functions control the global state of the package.
1599
1600=over 4
1601
1602=item B<safe_level>
1603
1604Controls the lengths to which the module will go to check the safety of the
1605temporary file or directory before proceeding.
1606Options are:
1607
1608=over 8
1609
1610=item STANDARD
1611
1612Do the basic security measures to ensure the directory exists and
1613is writable, that the umask() is fixed before opening of the file,
1614that temporary files are opened only if they do not already exist, and
1615that possible race conditions are avoided. Finally the L<unlink0|"unlink0">
1616function is used to remove files safely.
1617
1618=item MEDIUM
1619
1620In addition to the STANDARD security, the output directory is checked
1621to make sure that it is owned either by root or the user running the
1622program. If the directory is writable by group or by other, it is then
1623checked to make sure that the sticky bit is set.
1624
1625Will not work on platforms that do not support the C<-k> test
1626for sticky bit.
1627
1628=item HIGH
1629
1630In addition to the MEDIUM security checks, also check for the
1631possibility of ``chown() giveaway'' using the L<POSIX|POSIX>
1632sysconf() function. If this is a possibility, each directory in the
1633path is checked in turn for safeness, recursively walking back to the
1634root directory.
1635
1636For platforms that do not support the L<POSIX|POSIX>
1637C<_PC_CHOWN_RESTRICTED> symbol (for example, Windows NT) it is
1638assumed that ``chown() giveaway'' is possible and the recursive test
1639is performed.
1640
1641=back
1642
1643The level can be changed as follows:
1644
1645 File::Temp->safe_level( File::Temp::HIGH );
1646
1647The level constants are not exported by the module.
1648
1649Currently, you must be running at least perl v5.6.0 in order to
1650run with MEDIUM or HIGH security. This is simply because the
1651safety tests use functions from L<Fcntl|Fcntl> that are not
1652available in older versions of perl. The problem is that the version
1653number for Fcntl is the same in perl 5.6.0 and in 5.005_03 even though
1c19c868 1654they are different versions.
1655
1656On systems that do not support the HIGH or MEDIUM safety levels
1657(for example Win NT or OS/2) any attempt to change the level will
1658be ignored. The decision to ignore rather than raise an exception
1659allows portable programs to be written with high security in mind
1660for the systems that can support this without those programs failing
1661on systems where the extra tests are irrelevant.
1662
1663If you really need to see whether the change has been accepted
1664simply examine the return value of C<safe_level>.
1665
1666 $newlevel = File::Temp->safe_level( File::Temp::HIGH );
1667 die "Could not change to high security"
1668 if $newlevel != File::Temp::HIGH;
262eb13a 1669
1670=cut
1671
1672{
1673 # protect from using the variable itself
1674 my $LEVEL = STANDARD;
1675 sub safe_level {
1676 my $self = shift;
1677 if (@_) {
1678 my $level = shift;
1679 if (($level != STANDARD) && ($level != MEDIUM) && ($level != HIGH)) {
1680 carp "safe_level: Specified level ($level) not STANDARD, MEDIUM or HIGH - ignoring\n";
1681 } else {
1c19c868 1682 # Dont allow this on perl 5.005 or earlier
262eb13a 1683 if ($] < 5.006 && $level != STANDARD) {
1684 # Cant do MEDIUM or HIGH checks
1685 croak "Currently requires perl 5.006 or newer to do the safe checks";
1686 }
1c19c868 1687 # Check that we are allowed to change level
1688 # Silently ignore if we can not.
1689 $LEVEL = $level if _can_do_level($level);
262eb13a 1690 }
1691 }
1692 return $LEVEL;
1693 }
1694}
1695
1696=item TopSystemUID
1697
1698This is the highest UID on the current system that refers to a root
1699UID. This is used to make sure that the temporary directory is
1700owned by a system UID (C<root>, C<bin>, C<sys> etc) rather than
1701simply by root.
1702
1703This is required since on many unix systems C</tmp> is not owned
1704by root.
1705
1706Default is to assume that any UID less than or equal to 10 is a root
1707UID.
1708
1709 File::Temp->top_system_uid(10);
1710 my $topid = File::Temp->top_system_uid;
1711
1712This value can be adjusted to reduce security checking if required.
1713The value is only relevant when C<safe_level> is set to MEDIUM or higher.
1714
1715=back
1716
1717=cut
1718
1719{
1720 my $TopSystemUID = 10;
1721 sub top_system_uid {
1722 my $self = shift;
1723 if (@_) {
1724 my $newuid = shift;
1725 croak "top_system_uid: UIDs should be numeric"
1726 unless $newuid =~ /^\d+$/s;
1727 $TopSystemUID = $newuid;
1728 }
1729 return $TopSystemUID;
1730 }
1731}
1732
1733=head1 WARNING
1734
1735For maximum security, endeavour always to avoid ever looking at,
1736touching, or even imputing the existence of the filename. You do not
1737know that that filename is connected to the same file as the handle
1738you have, and attempts to check this can only trigger more race
1739conditions. It's far more secure to use the filehandle alone and
1740dispense with the filename altogether.
1741
1742If you need to pass the handle to something that expects a filename
1743then, on a unix system, use C<"/dev/fd/" . fileno($fh)> for arbitrary
1744programs, or more generally C<< "+<=&" . fileno($fh) >> for Perl
1745programs. You will have to clear the close-on-exec bit on that file
1746descriptor before passing it to another process.
1747
1748 use Fcntl qw/F_SETFD F_GETFD/;
1749 fcntl($tmpfh, F_SETFD, 0)
1750 or die "Can't clear close-on-exec flag on temp fh: $!\n";
1751
09d7a2f9 1752=head2 Temporary files and NFS
1753
1754Some problems are associated with using temporary files that reside
1755on NFS file systems and it is recommended that a local filesystem
1756is used whenever possible. Some of the security tests will most probably
1757fail when the temp file is not local. Additionally, be aware that
1758the performance of I/O operations over NFS will not be as good as for
1759a local disk.
1760
262eb13a 1761=head1 HISTORY
1762
1763Originally began life in May 1999 as an XS interface to the system
e77f578c 1764mkstemp() function. In March 2000, the OpenBSD mkstemp() code was
262eb13a 1765translated to Perl for total control of the code's
1766security checking, to ensure the presence of the function regardless of
1767operating system and to help with portability.
1768
1769=head1 SEE ALSO
1770
1771L<POSIX/tmpnam>, L<POSIX/tmpfile>, L<File::Spec>, L<File::Path>
1772
09d7a2f9 1773See L<IO::File> and L<File::MkTemp> for different implementations of
1774temporary file handling.
262eb13a 1775
1776=head1 AUTHOR
1777
1778Tim Jenness E<lt>t.jenness@jach.hawaii.eduE<gt>
1779
1780Copyright (C) 1999, 2000 Tim Jenness and the UK Particle Physics and
1781Astronomy Research Council. All Rights Reserved. This program is free
1782software; you can redistribute it and/or modify it under the same
1783terms as Perl itself.
1784
1785Original Perl implementation loosely based on the OpenBSD C code for
1786mkstemp(). Thanks to Tom Christiansen for suggesting that this module
1787should be written and providing ideas for code improvements and
1788security enhancements.
1789
1790=cut
1791
1792
17931;