| Filename | /usr/share/perl/5.28/File/Temp.pm |
| Statements | Executed 307424 statements in 1.43s |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 4585 | 2 | 1 | 404ms | 1.16s | File::Temp::_gettemp |
| 4585 | 1 | 1 | 256ms | 343ms | File::Temp::_replace_XX |
| 4583 | 2 | 2 | 252ms | 1.64s | File::Temp::tempfile |
| 4583 | 1 | 1 | 222ms | 222ms | File::Temp::CORE:sysopen (opcode) |
| 50426 | 1 | 1 | 54.4ms | 54.4ms | File::Temp::CORE:substcont (opcode) |
| 4587 | 3 | 1 | 38.0ms | 38.0ms | File::Temp::_parse_args |
| 4585 | 2 | 1 | 32.9ms | 32.9ms | File::Temp::CORE:chmod (opcode) |
| 4585 | 1 | 1 | 29.1ms | 29.1ms | File::Temp::CORE:ftis (opcode) |
| 4585 | 1 | 1 | 22.8ms | 22.8ms | File::Temp::CORE:subst (opcode) |
| 9170 | 1 | 1 | 16.7ms | 16.7ms | File::Temp::safe_level |
| 4585 | 1 | 1 | 10.1ms | 10.1ms | File::Temp::CORE:regcomp (opcode) |
| 4585 | 1 | 1 | 6.17ms | 6.17ms | File::Temp::CORE:ftdir (opcode) |
| 1 | 1 | 1 | 722µs | 940µs | File::Temp::BEGIN@16 |
| 1 | 1 | 1 | 423µs | 525µs | File::Temp::BEGIN@15 |
| 25 | 1 | 1 | 258µs | 361µs | File::Temp::NUMIFY |
| 2 | 1 | 1 | 232µs | 232µs | File::Temp::CORE:mkdir (opcode) |
| 27 | 3 | 2 | 205µs | 205µs | File::Temp::Dir::dirname |
| 2 | 2 | 2 | 168µs | 981µs | File::Temp::newdir |
| 1 | 1 | 1 | 133µs | 105ms | File::Temp::Dir::DESTROY |
| 2 | 1 | 1 | 77µs | 750µs | File::Temp::tempdir |
| 1 | 1 | 1 | 36µs | 38µs | File::Temp::cleanup |
| 1 | 1 | 1 | 30µs | 69µs | File::Temp::END |
| 1 | 1 | 1 | 19µs | 27µs | File::Temp::BEGIN@11 |
| 1 | 1 | 1 | 18µs | 18µs | File::Temp::BEGIN@8 |
| 1 | 1 | 1 | 17µs | 29µs | File::Temp::BEGIN@43 |
| 1 | 1 | 1 | 15µs | 260µs | File::Temp::BEGIN@14 |
| 1 | 1 | 1 | 15µs | 51µs | File::Temp::BEGIN@13 |
| 1 | 1 | 1 | 15µs | 54µs | File::Temp::BEGIN@31 |
| 1 | 1 | 1 | 14µs | 79µs | File::Temp::Dir::BEGIN@1541 |
| 1 | 1 | 1 | 13µs | 13µs | File::Temp::Dir::CORE:ftdir (opcode) |
| 1 | 1 | 1 | 12µs | 20µs | File::Temp::BEGIN@137 |
| 1 | 1 | 1 | 10µs | 54µs | File::Temp::Dir::BEGIN@1543 |
| 1 | 1 | 1 | 10µs | 70µs | File::Temp::BEGIN@83 |
| 1 | 1 | 1 | 9µs | 47µs | File::Temp::BEGIN@32 |
| 1 | 1 | 1 | 9µs | 28µs | File::Temp::BEGIN@17 |
| 1 | 1 | 1 | 9µs | 14µs | File::Temp::Dir::BEGIN@1542 |
| 1 | 1 | 1 | 7µs | 7µs | File::Temp::Dir::unlink_on_destroy |
| 2 | 1 | 1 | 7µs | 7µs | File::Temp::CORE:match (opcode) |
| 1 | 1 | 1 | 6µs | 9µs | File::Temp::BEGIN@9 |
| 1 | 1 | 1 | 6µs | 23µs | File::Temp::BEGIN@107 |
| 1 | 1 | 1 | 6µs | 86µs | File::Temp::BEGIN@86 |
| 1 | 1 | 1 | 5µs | 53µs | File::Temp::BEGIN@36 |
| 1 | 1 | 1 | 5µs | 28µs | File::Temp::BEGIN@90 |
| 1 | 1 | 1 | 5µs | 35µs | File::Temp::BEGIN@10 |
| 1 | 1 | 1 | 5µs | 25µs | File::Temp::BEGIN@94 |
| 1 | 1 | 1 | 5µs | 25µs | File::Temp::BEGIN@96 |
| 1 | 1 | 1 | 4µs | 26µs | File::Temp::BEGIN@95 |
| 1 | 1 | 1 | 3µs | 3µs | File::Temp::BEGIN@12 |
| 1 | 1 | 1 | 2µs | 2µs | File::Temp::CORE:sort (opcode) |
| 1 | 1 | 1 | 800ns | 800ns | File::Temp::__ANON__[:111] |
| 1 | 1 | 1 | 700ns | 700ns | File::Temp::__ANON__[:119] |
| 1 | 1 | 1 | 600ns | 600ns | File::Temp::__ANON__ (xsub) |
| 1 | 1 | 1 | 500ns | 500ns | File::Temp::__ANON__[:141] |
| 0 | 0 | 0 | 0s | 0s | File::Temp::DESTROY |
| 0 | 0 | 0 | 0s | 0s | File::Temp::Dir::STRINGIFY |
| 0 | 0 | 0 | 0s | 0s | File::Temp::STRINGIFY |
| 0 | 0 | 0 | 0s | 0s | File::Temp::__ANON__[:112] |
| 0 | 0 | 0 | 0s | 0s | File::Temp::__ANON__[:120] |
| 0 | 0 | 0 | 0s | 0s | File::Temp::__ANON__[:142] |
| 0 | 0 | 0 | 0s | 0s | File::Temp::_can_do_level |
| 0 | 0 | 0 | 0s | 0s | File::Temp::_can_unlink_opened_file |
| 0 | 0 | 0 | 0s | 0s | File::Temp::_deferred_unlink |
| 0 | 0 | 0 | 0s | 0s | File::Temp::_force_writable |
| 0 | 0 | 0 | 0s | 0s | File::Temp::_is_safe |
| 0 | 0 | 0 | 0s | 0s | File::Temp::_is_verysafe |
| 0 | 0 | 0 | 0s | 0s | File::Temp::cmpstat |
| 0 | 0 | 0 | 0s | 0s | File::Temp::filename |
| 0 | 0 | 0 | 0s | 0s | File::Temp::mkdtemp |
| 0 | 0 | 0 | 0s | 0s | File::Temp::mkstemp |
| 0 | 0 | 0 | 0s | 0s | File::Temp::mkstemps |
| 0 | 0 | 0 | 0s | 0s | File::Temp::mktemp |
| 0 | 0 | 0 | 0s | 0s | File::Temp::new |
| 0 | 0 | 0 | 0s | 0s | File::Temp::tempnam |
| 0 | 0 | 0 | 0s | 0s | File::Temp::tmpfile |
| 0 | 0 | 0 | 0s | 0s | File::Temp::tmpnam |
| 0 | 0 | 0 | 0s | 0s | File::Temp::top_system_uid |
| 0 | 0 | 0 | 0s | 0s | File::Temp::unlink0 |
| 0 | 0 | 0 | 0s | 0s | File::Temp::unlink1 |
| 0 | 0 | 0 | 0s | 0s | File::Temp::unlink_on_destroy |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package File::Temp; | ||||
| 2 | # ABSTRACT: return name and handle of a temporary file safely | ||||
| 3 | 1 | 500ns | our $VERSION = '0.2304'; # VERSION | ||
| 4 | |||||
| 5 | # Toolchain targets v5.8.1, but we'll try to support back to v5.6 anyway. | ||||
| 6 | # It might be possible to make this v5.5, but many v5.6isms are creeping | ||||
| 7 | # into the code and tests. | ||||
| 8 | 2 | 46µs | 1 | 18µs | # spent 18µs within File::Temp::BEGIN@8 which was called:
# once (18µs+0s) by IO::CaptureOutput::_proxy::BEGIN@138 at line 8 # spent 18µs making 1 call to File::Temp::BEGIN@8 |
| 9 | 2 | 18µs | 2 | 11µs | # spent 9µs (6+2) within File::Temp::BEGIN@9 which was called:
# once (6µs+2µs) by IO::CaptureOutput::_proxy::BEGIN@138 at line 9 # spent 9µs making 1 call to File::Temp::BEGIN@9
# spent 2µs making 1 call to strict::import |
| 10 | 2 | 22µs | 2 | 64µs | # spent 35µs (5+30) within File::Temp::BEGIN@10 which was called:
# once (5µs+30µs) by IO::CaptureOutput::_proxy::BEGIN@138 at line 10 # spent 35µs making 1 call to File::Temp::BEGIN@10
# spent 30µs making 1 call to Exporter::import |
| 11 | 3 | 43µs | 3 | 35µs | # spent 27µs (19+8) within File::Temp::BEGIN@11 which was called:
# once (19µs+8µs) by IO::CaptureOutput::_proxy::BEGIN@138 at line 11 # spent 27µs making 1 call to File::Temp::BEGIN@11
# spent 7µs making 1 call to UNIVERSAL::VERSION
# spent 600ns making 1 call to File::Temp::__ANON__ |
| 12 | 2 | 20µs | 1 | 3µs | # spent 3µs within File::Temp::BEGIN@12 which was called:
# once (3µs+0s) by IO::CaptureOutput::_proxy::BEGIN@138 at line 12 # spent 3µs making 1 call to File::Temp::BEGIN@12 |
| 13 | 3 | 39µs | 3 | 87µs | # spent 51µs (15+36) within File::Temp::BEGIN@13 which was called:
# once (15µs+36µs) by IO::CaptureOutput::_proxy::BEGIN@138 at line 13 # spent 51µs making 1 call to File::Temp::BEGIN@13
# spent 31µs making 1 call to Exporter::import
# spent 5µs making 1 call to UNIVERSAL::VERSION |
| 14 | 3 | 38µs | 3 | 505µs | # spent 260µs (15+245) within File::Temp::BEGIN@14 which was called:
# once (15µs+245µs) by IO::CaptureOutput::_proxy::BEGIN@138 at line 14 # spent 260µs making 1 call to File::Temp::BEGIN@14
# spent 240µs making 1 call to Exporter::import
# spent 5µs making 1 call to UNIVERSAL::VERSION |
| 15 | 2 | 91µs | 2 | 549µs | # spent 525µs (423+102) within File::Temp::BEGIN@15 which was called:
# once (423µs+102µs) by IO::CaptureOutput::_proxy::BEGIN@138 at line 15 # spent 525µs making 1 call to File::Temp::BEGIN@15
# spent 24µs making 1 call to Exporter::import |
| 16 | 2 | 109µs | 2 | 954µs | # spent 940µs (722+218) within File::Temp::BEGIN@16 which was called:
# once (722µs+218µs) by IO::CaptureOutput::_proxy::BEGIN@138 at line 16 # spent 940µs making 1 call to File::Temp::BEGIN@16
# spent 14µs making 1 call to Exporter::import |
| 17 | 2 | 60µs | 2 | 46µs | # spent 28µs (9+19) within File::Temp::BEGIN@17 which was called:
# once (9µs+19µs) by IO::CaptureOutput::_proxy::BEGIN@138 at line 17 # spent 28µs making 1 call to File::Temp::BEGIN@17
# spent 19µs making 1 call to Exporter::import |
| 18 | 1 | 2µs | require VMS::Stdio if $^O eq 'VMS'; | ||
| 19 | |||||
| 20 | # pre-emptively load Carp::Heavy. If we don't when we run out of file | ||||
| 21 | # handles and attempt to call croak() we get an error message telling | ||||
| 22 | # us that Carp::Heavy won't load rather than an error telling us we | ||||
| 23 | # have run out of file handles. We either preload croak() or we | ||||
| 24 | # switch the calls to croak from _gettemp() to use die. | ||||
| 25 | 2 | 112µs | eval { require Carp::Heavy; }; | ||
| 26 | |||||
| 27 | # Need the Symbol package if we are running older perl | ||||
| 28 | 1 | 600ns | require Symbol if $] < 5.006; | ||
| 29 | |||||
| 30 | ### For the OO interface | ||||
| 31 | 3 | 50µs | 3 | 94µs | # spent 54µs (15+40) within File::Temp::BEGIN@31 which was called:
# once (15µs+40µs) by IO::CaptureOutput::_proxy::BEGIN@138 at line 31 # spent 54µs making 1 call to File::Temp::BEGIN@31
# spent 30µs making 1 call to parent::import
# spent 9µs making 1 call to UNIVERSAL::VERSION |
| 32 | 1 | 8µs | 1 | 38µs | # spent 47µs (9+38) within File::Temp::BEGIN@32 which was called:
# once (9µs+38µs) by IO::CaptureOutput::_proxy::BEGIN@138 at line 33 # spent 38µs making 1 call to overload::import |
| 33 | 1 | 23µs | 1 | 47µs | fallback => 1; # spent 47µs making 1 call to File::Temp::BEGIN@32 |
| 34 | |||||
| 35 | # use 'our' on v5.6.0 | ||||
| 36 | 2 | 35µs | 2 | 101µs | # spent 53µs (5+48) within File::Temp::BEGIN@36 which was called:
# once (5µs+48µs) by IO::CaptureOutput::_proxy::BEGIN@138 at line 36 # spent 53µs making 1 call to File::Temp::BEGIN@36
# spent 48µs making 1 call to vars::import |
| 37 | |||||
| 38 | 1 | 400ns | $DEBUG = 0; | ||
| 39 | 1 | 100ns | $KEEP_ALL = 0; | ||
| 40 | |||||
| 41 | # We are exporting functions | ||||
| 42 | |||||
| 43 | 3 | 102µs | 3 | 41µs | # spent 29µs (17+12) within File::Temp::BEGIN@43 which was called:
# once (17µs+12µs) by IO::CaptureOutput::_proxy::BEGIN@138 at line 43 # spent 29µs making 1 call to File::Temp::BEGIN@43
# spent 6µs making 1 call to Exporter::import
# spent 6µs making 1 call to UNIVERSAL::VERSION |
| 44 | |||||
| 45 | # Export list - to allow fine tuning of export table | ||||
| 46 | |||||
| 47 | 1 | 3µs | @EXPORT_OK = qw{ | ||
| 48 | tempfile | ||||
| 49 | tempdir | ||||
| 50 | tmpnam | ||||
| 51 | tmpfile | ||||
| 52 | mktemp | ||||
| 53 | mkstemp | ||||
| 54 | mkstemps | ||||
| 55 | mkdtemp | ||||
| 56 | unlink0 | ||||
| 57 | cleanup | ||||
| 58 | SEEK_SET | ||||
| 59 | SEEK_CUR | ||||
| 60 | SEEK_END | ||||
| 61 | }; | ||||
| 62 | |||||
| 63 | # Groups of functions for export | ||||
| 64 | |||||
| 65 | 1 | 4µs | %EXPORT_TAGS = ( | ||
| 66 | 'POSIX' => [qw/ tmpnam tmpfile /], | ||||
| 67 | 'mktemp' => [qw/ mktemp mkstemp mkstemps mkdtemp/], | ||||
| 68 | 'seekable' => [qw/ SEEK_SET SEEK_CUR SEEK_END /], | ||||
| 69 | ); | ||||
| 70 | |||||
| 71 | # add contents of these tags to @EXPORT | ||||
| 72 | 1 | 3µs | 1 | 32µs | Exporter::export_tags('POSIX','mktemp','seekable'); # spent 32µs making 1 call to Exporter::export_tags |
| 73 | |||||
| 74 | # This is a list of characters that can be used in random filenames | ||||
| 75 | |||||
| 76 | 1 | 5µs | 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 | ||
| 77 | 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 | ||||
| 78 | 0 1 2 3 4 5 6 7 8 9 _ | ||||
| 79 | /); | ||||
| 80 | |||||
| 81 | # Maximum number of tries to make a temp file before failing | ||||
| 82 | |||||
| 83 | 2 | 29µs | 2 | 130µs | # spent 70µs (10+60) within File::Temp::BEGIN@83 which was called:
# once (10µs+60µs) by IO::CaptureOutput::_proxy::BEGIN@138 at line 83 # spent 70µs making 1 call to File::Temp::BEGIN@83
# spent 60µs making 1 call to constant::import |
| 84 | |||||
| 85 | # Minimum number of X characters that should be in a template | ||||
| 86 | 2 | 28µs | 2 | 166µs | # spent 86µs (6+80) within File::Temp::BEGIN@86 which was called:
# once (6µs+80µs) by IO::CaptureOutput::_proxy::BEGIN@138 at line 86 # spent 86µs making 1 call to File::Temp::BEGIN@86
# spent 80µs making 1 call to constant::import |
| 87 | |||||
| 88 | # Default template when no template supplied | ||||
| 89 | |||||
| 90 | 2 | 26µs | 2 | 52µs | # spent 28µs (5+23) within File::Temp::BEGIN@90 which was called:
# once (5µs+23µs) by IO::CaptureOutput::_proxy::BEGIN@138 at line 90 # spent 28µs making 1 call to File::Temp::BEGIN@90
# spent 23µs making 1 call to constant::import |
| 91 | |||||
| 92 | # Constants for the security level | ||||
| 93 | |||||
| 94 | 2 | 26µs | 2 | 46µs | # spent 25µs (5+20) within File::Temp::BEGIN@94 which was called:
# once (5µs+20µs) by IO::CaptureOutput::_proxy::BEGIN@138 at line 94 # spent 25µs making 1 call to File::Temp::BEGIN@94
# spent 20µs making 1 call to constant::import |
| 95 | 2 | 20µs | 2 | 48µs | # spent 26µs (4+22) within File::Temp::BEGIN@95 which was called:
# once (4µs+22µs) by IO::CaptureOutput::_proxy::BEGIN@138 at line 95 # spent 26µs making 1 call to File::Temp::BEGIN@95
# spent 22µs making 1 call to constant::import |
| 96 | 2 | 58µs | 2 | 45µs | # spent 25µs (5+20) within File::Temp::BEGIN@96 which was called:
# once (5µs+20µs) by IO::CaptureOutput::_proxy::BEGIN@138 at line 96 # spent 25µs making 1 call to File::Temp::BEGIN@96
# spent 20µs making 1 call to constant::import |
| 97 | |||||
| 98 | # OPENFLAGS. If we defined the flag to use with Sysopen here this gives | ||||
| 99 | # us an optimisation when many temporary files are requested | ||||
| 100 | |||||
| 101 | 1 | 200ns | my $OPENFLAGS = O_CREAT | O_EXCL | O_RDWR; | ||
| 102 | 1 | 200ns | my $LOCKFLAG; | ||
| 103 | |||||
| 104 | 1 | 1µs | unless ($^O eq 'MacOS') { | ||
| 105 | 1 | 300ns | for my $oflag (qw/ NOFOLLOW BINARY LARGEFILE NOINHERIT /) { | ||
| 106 | 4 | 2µs | my ($bit, $func) = (0, "Fcntl::O_" . $oflag); | ||
| 107 | 2 | 176µs | 2 | 39µs | # spent 23µs (6+16) within File::Temp::BEGIN@107 which was called:
# once (6µs+16µs) by IO::CaptureOutput::_proxy::BEGIN@138 at line 107 # spent 23µs making 1 call to File::Temp::BEGIN@107
# spent 16µs making 1 call to strict::unimport |
| 108 | 4 | 3µs | $OPENFLAGS |= $bit if eval { | ||
| 109 | # Make sure that redefined die handlers do not cause problems | ||||
| 110 | # e.g. CGI::Carp | ||||
| 111 | 5 | 12µs | # spent 800ns within File::Temp::__ANON__[/usr/share/perl/5.28/File/Temp.pm:111] which was called:
# once (800ns+0s) by Fcntl::O_NOINHERIT at line 113 | ||
| 112 | 4 | 6µs | local $SIG{__WARN__} = sub {}; | ||
| 113 | 4 | 62µs | 5 | 47µs | $bit = &$func(); # spent 33µs making 1 call to Fcntl::O_NOINHERIT
# spent 6µs making 1 call to Fcntl::O_NOFOLLOW
# spent 4µs making 1 call to Fcntl::O_BINARY
# spent 3µs making 1 call to Fcntl::O_LARGEFILE
# spent 800ns making 1 call to File::Temp::__ANON__[File/Temp.pm:111] |
| 114 | 3 | 7µs | 1; | ||
| 115 | }; | ||||
| 116 | } | ||||
| 117 | # Special case O_EXLOCK | ||||
| 118 | 1 | 300ns | $LOCKFLAG = eval { | ||
| 119 | 2 | 5µs | # spent 700ns within File::Temp::__ANON__[/usr/share/perl/5.28/File/Temp.pm:119] which was called:
# once (700ns+0s) by Fcntl::O_EXLOCK at line 121 | ||
| 120 | 1 | 2µs | local $SIG{__WARN__} = sub {}; | ||
| 121 | 1 | 15µs | 2 | 14µs | &Fcntl::O_EXLOCK(); # spent 13µs making 1 call to Fcntl::O_EXLOCK
# spent 700ns making 1 call to File::Temp::__ANON__[File/Temp.pm:119] |
| 122 | }; | ||||
| 123 | } | ||||
| 124 | |||||
| 125 | # On some systems the O_TEMPORARY flag can be used to tell the OS | ||||
| 126 | # to automatically remove the file when it is closed. This is fine | ||||
| 127 | # in most cases but not if tempfile is called with UNLINK=>0 and | ||||
| 128 | # the filename is requested -- in the case where the filename is to | ||||
| 129 | # be passed to another routine. This happens on windows. We overcome | ||||
| 130 | # this by using a second open flags variable | ||||
| 131 | |||||
| 132 | 1 | 200ns | my $OPENTEMPFLAGS = $OPENFLAGS; | ||
| 133 | 1 | 600ns | unless ($^O eq 'MacOS') { | ||
| 134 | 1 | 300ns | for my $oflag (qw/ TEMPORARY /) { | ||
| 135 | 1 | 700ns | my ($bit, $func) = (0, "Fcntl::O_" . $oflag); | ||
| 136 | 1 | 200ns | local($@); | ||
| 137 | 2 | 682µs | 2 | 27µs | # spent 20µs (12+7) within File::Temp::BEGIN@137 which was called:
# once (12µs+7µs) by IO::CaptureOutput::_proxy::BEGIN@138 at line 137 # spent 20µs making 1 call to File::Temp::BEGIN@137
# spent 7µs making 1 call to strict::unimport |
| 138 | 1 | 300ns | $OPENTEMPFLAGS |= $bit if eval { | ||
| 139 | # Make sure that redefined die handlers do not cause problems | ||||
| 140 | # e.g. CGI::Carp | ||||
| 141 | 2 | 4µs | # spent 500ns within File::Temp::__ANON__[/usr/share/perl/5.28/File/Temp.pm:141] which was called:
# once (500ns+0s) by Fcntl::O_TEMPORARY at line 143 | ||
| 142 | 1 | 2µs | local $SIG{__WARN__} = sub {}; | ||
| 143 | 1 | 14µs | 2 | 13µs | $bit = &$func(); # spent 12µs making 1 call to Fcntl::O_TEMPORARY
# spent 500ns making 1 call to File::Temp::__ANON__[File/Temp.pm:141] |
| 144 | 1; | ||||
| 145 | }; | ||||
| 146 | } | ||||
| 147 | } | ||||
| 148 | |||||
| 149 | # Private hash tracking which files have been created by each process id via the OO interface | ||||
| 150 | 1 | 100ns | my %FILES_CREATED_BY_OBJECT; | ||
| 151 | |||||
| 152 | # INTERNAL ROUTINES - not to be used outside of package | ||||
| 153 | |||||
| 154 | # Generic routine for getting a temporary filename | ||||
| 155 | # modelled on OpenBSD _gettemp() in mktemp.c | ||||
| 156 | |||||
| 157 | # The template must contain X's that are to be replaced | ||||
| 158 | # with the random values | ||||
| 159 | |||||
| 160 | # Arguments: | ||||
| 161 | |||||
| 162 | # TEMPLATE - string containing the XXXXX's that is converted | ||||
| 163 | # to a random filename and opened if required | ||||
| 164 | |||||
| 165 | # Optionally, a hash can also be supplied containing specific options | ||||
| 166 | # "open" => if true open the temp file, else just return the name | ||||
| 167 | # default is 0 | ||||
| 168 | # "mkdir"=> if true, we are creating a temp directory rather than tempfile | ||||
| 169 | # default is 0 | ||||
| 170 | # "suffixlen" => number of characters at end of PATH to be ignored. | ||||
| 171 | # default is 0. | ||||
| 172 | # "unlink_on_close" => indicates that, if possible, the OS should remove | ||||
| 173 | # the file as soon as it is closed. Usually indicates | ||||
| 174 | # use of the O_TEMPORARY flag to sysopen. | ||||
| 175 | # Usually irrelevant on unix | ||||
| 176 | # "use_exlock" => Indicates that O_EXLOCK should be used. Default is true. | ||||
| 177 | |||||
| 178 | # Optionally a reference to a scalar can be passed into the function | ||||
| 179 | # On error this will be used to store the reason for the error | ||||
| 180 | # "ErrStr" => \$errstr | ||||
| 181 | |||||
| 182 | # "open" and "mkdir" can not both be true | ||||
| 183 | # "unlink_on_close" is not used when "mkdir" is true. | ||||
| 184 | |||||
| 185 | # The default options are equivalent to mktemp(). | ||||
| 186 | |||||
| 187 | # Returns: | ||||
| 188 | # filehandle - open file handle (if called with doopen=1, else undef) | ||||
| 189 | # temp name - name of the temp file or directory | ||||
| 190 | |||||
| 191 | # For example: | ||||
| 192 | # ($fh, $name) = _gettemp($template, "open" => 1); | ||||
| 193 | |||||
| 194 | # for the current version, failures are associated with | ||||
| 195 | # stored in an error string and returned to give the reason whilst debugging | ||||
| 196 | # This routine is not called by any external function | ||||
| 197 | sub _gettemp { | ||||
| 198 | |||||
| 199 | 4585 | 3.38ms | croak 'Usage: ($fh, $name) = _gettemp($template, OPTIONS);' | ||
| 200 | unless scalar(@_) >= 1; | ||||
| 201 | |||||
| 202 | # the internal error string - expect it to be overridden | ||||
| 203 | # Need this in case the caller decides not to supply us a value | ||||
| 204 | # need an anonymous scalar | ||||
| 205 | 4585 | 898µs | my $tempErrStr; | ||
| 206 | |||||
| 207 | # Default options | ||||
| 208 | 4585 | 19.6ms | my %options = ( | ||
| 209 | "open" => 0, | ||||
| 210 | "mkdir" => 0, | ||||
| 211 | "suffixlen" => 0, | ||||
| 212 | "unlink_on_close" => 0, | ||||
| 213 | "use_exlock" => 1, | ||||
| 214 | "ErrStr" => \$tempErrStr, | ||||
| 215 | ); | ||||
| 216 | |||||
| 217 | # Read the template | ||||
| 218 | 4585 | 2.25ms | my $template = shift; | ||
| 219 | 4585 | 1.78ms | if (ref($template)) { | ||
| 220 | # Use a warning here since we have not yet merged ErrStr | ||||
| 221 | carp "File::Temp::_gettemp: template must not be a reference"; | ||||
| 222 | return (); | ||||
| 223 | } | ||||
| 224 | |||||
| 225 | # Check that the number of entries on stack are even | ||||
| 226 | 4585 | 3.41ms | if (scalar(@_) % 2 != 0) { | ||
| 227 | # Use a warning here since we have not yet merged ErrStr | ||||
| 228 | carp "File::Temp::_gettemp: Must have even number of options"; | ||||
| 229 | return (); | ||||
| 230 | } | ||||
| 231 | |||||
| 232 | # Read the options and merge with defaults | ||||
| 233 | 4585 | 21.0ms | %options = (%options, @_) if @_; | ||
| 234 | |||||
| 235 | # Make sure the error string is set to undef | ||||
| 236 | 4585 | 4.45ms | ${$options{ErrStr}} = undef; | ||
| 237 | |||||
| 238 | # Can not open the file and make a directory in a single call | ||||
| 239 | 4585 | 3.52ms | if ($options{"open"} && $options{"mkdir"}) { | ||
| 240 | ${$options{ErrStr}} = "doopen and domkdir can not both be true\n"; | ||||
| 241 | return (); | ||||
| 242 | } | ||||
| 243 | |||||
| 244 | # Find the start of the end of the Xs (position of last X) | ||||
| 245 | # Substr starts from 0 | ||||
| 246 | 4585 | 4.56ms | my $start = length($template) - 1 - $options{"suffixlen"}; | ||
| 247 | |||||
| 248 | # Check that we have at least MINX x X (e.g. 'XXXX") at the end of the string | ||||
| 249 | # (taking suffixlen into account). Any fewer is insecure. | ||||
| 250 | |||||
| 251 | # Do it using substr - no reason to use a pattern match since | ||||
| 252 | # we know where we are looking and what we are looking for | ||||
| 253 | |||||
| 254 | 4585 | 6.94ms | if (substr($template, $start - MINX + 1, MINX) ne 'X' x MINX) { | ||
| 255 | ${$options{ErrStr}} = "The template must end with at least ". | ||||
| 256 | MINX . " 'X' characters\n"; | ||||
| 257 | return (); | ||||
| 258 | } | ||||
| 259 | |||||
| 260 | # Replace all the X at the end of the substring with a | ||||
| 261 | # random character or just all the XX at the end of a full string. | ||||
| 262 | # Do it as an if, since the suffix adjusts which section to replace | ||||
| 263 | # and suffixlen=0 returns nothing if used in the substr directly | ||||
| 264 | # and generate a full path from the template | ||||
| 265 | |||||
| 266 | 4585 | 10.2ms | 4585 | 343ms | my $path = _replace_XX($template, $options{"suffixlen"}); # spent 343ms making 4585 calls to File::Temp::_replace_XX, avg 75µs/call |
| 267 | |||||
| 268 | # Split the path into constituent parts - eventually we need to check | ||||
| 269 | # whether the directory exists | ||||
| 270 | # We need to know whether we are making a temp directory | ||||
| 271 | # or a tempfile | ||||
| 272 | |||||
| 273 | 4585 | 1.55ms | my ($volume, $directories, $file); | ||
| 274 | my $parent; # parent directory | ||||
| 275 | 4585 | 2.89ms | if ($options{"mkdir"}) { | ||
| 276 | # There is no filename at the end | ||||
| 277 | 2 | 17µs | 2 | 8µs | ($volume, $directories, $file) = File::Spec->splitpath( $path, 1); # spent 8µs making 2 calls to File::Spec::Unix::splitpath, avg 4µs/call |
| 278 | |||||
| 279 | # The parent is then $directories without the last directory | ||||
| 280 | # Split the directory and put it back together again | ||||
| 281 | 2 | 6µs | 2 | 5µs | my @dirs = File::Spec->splitdir($directories); # spent 5µs making 2 calls to File::Spec::Unix::splitdir, avg 3µs/call |
| 282 | |||||
| 283 | # If @dirs only has one entry (i.e. the directory template) that means | ||||
| 284 | # we are in the current directory | ||||
| 285 | 2 | 3µs | if ($#dirs == 0) { | ||
| 286 | $parent = File::Spec->curdir; | ||||
| 287 | } else { | ||||
| 288 | |||||
| 289 | 2 | 2µs | if ($^O eq 'VMS') { # need volume to avoid relative dir spec | ||
| 290 | $parent = File::Spec->catdir($volume, @dirs[0..$#dirs-1]); | ||||
| 291 | $parent = 'sys$disk:[]' if $parent eq ''; | ||||
| 292 | } else { | ||||
| 293 | |||||
| 294 | # Put it back together without the last one | ||||
| 295 | 2 | 43µs | 4 | 40µs | $parent = File::Spec->catdir(@dirs[0..$#dirs-1]); # spent 30µs making 2 calls to File::Spec::Unix::catdir, avg 15µs/call
# spent 10µs making 2 calls to File::Spec::Unix::canonpath, avg 5µs/call |
| 296 | |||||
| 297 | # ...and attach the volume (no filename) | ||||
| 298 | 2 | 6µs | 2 | 9µs | $parent = File::Spec->catpath($volume, $parent, ''); # spent 9µs making 2 calls to File::Spec::Unix::catpath, avg 5µs/call |
| 299 | } | ||||
| 300 | |||||
| 301 | } | ||||
| 302 | |||||
| 303 | } else { | ||||
| 304 | |||||
| 305 | # Get rid of the last filename (use File::Basename for this?) | ||||
| 306 | 4583 | 17.3ms | 4583 | 84.4ms | ($volume, $directories, $file) = File::Spec->splitpath( $path ); # spent 84.4ms making 4583 calls to File::Spec::Unix::splitpath, avg 18µs/call |
| 307 | |||||
| 308 | # Join up without the file part | ||||
| 309 | 4583 | 13.8ms | 4583 | 20.6ms | $parent = File::Spec->catpath($volume,$directories,''); # spent 20.6ms making 4583 calls to File::Spec::Unix::catpath, avg 4µs/call |
| 310 | |||||
| 311 | # If $parent is empty replace with curdir | ||||
| 312 | 4583 | 2.61ms | $parent = File::Spec->curdir | ||
| 313 | unless $directories ne ''; | ||||
| 314 | |||||
| 315 | } | ||||
| 316 | |||||
| 317 | # Check that the parent directories exist | ||||
| 318 | # Do this even for the case where we are simply returning a name | ||||
| 319 | # not a file -- no point returning a name that includes a directory | ||||
| 320 | # that does not exist or is not writable | ||||
| 321 | |||||
| 322 | 4585 | 53.3ms | 4585 | 29.1ms | unless (-e $parent) { # spent 29.1ms making 4585 calls to File::Temp::CORE:ftis, avg 6µs/call |
| 323 | ${$options{ErrStr}} = "Parent directory ($parent) does not exist"; | ||||
| 324 | return (); | ||||
| 325 | } | ||||
| 326 | 4585 | 18.9ms | 4585 | 6.17ms | unless (-d $parent) { # spent 6.17ms making 4585 calls to File::Temp::CORE:ftdir, avg 1µs/call |
| 327 | ${$options{ErrStr}} = "Parent directory ($parent) is not a directory"; | ||||
| 328 | return (); | ||||
| 329 | } | ||||
| 330 | |||||
| 331 | # Check the stickiness of the directory and chown giveaway if required | ||||
| 332 | # If the directory is world writable the sticky bit | ||||
| 333 | # must be set | ||||
| 334 | |||||
| 335 | 4585 | 29.5ms | 9170 | 16.7ms | if (File::Temp->safe_level == MEDIUM) { # spent 16.7ms making 9170 calls to File::Temp::safe_level, avg 2µs/call |
| 336 | my $safeerr; | ||||
| 337 | unless (_is_safe($parent,\$safeerr)) { | ||||
| 338 | ${$options{ErrStr}} = "Parent directory ($parent) is not safe ($safeerr)"; | ||||
| 339 | return (); | ||||
| 340 | } | ||||
| 341 | } elsif (File::Temp->safe_level == HIGH) { | ||||
| 342 | my $safeerr; | ||||
| 343 | unless (_is_verysafe($parent, \$safeerr)) { | ||||
| 344 | ${$options{ErrStr}} = "Parent directory ($parent) is not safe ($safeerr)"; | ||||
| 345 | return (); | ||||
| 346 | } | ||||
| 347 | } | ||||
| 348 | |||||
| 349 | # Now try MAX_TRIES time to open the file | ||||
| 350 | 4585 | 6.36ms | for (my $i = 0; $i < MAX_TRIES; $i++) { | ||
| 351 | |||||
| 352 | # Try to open the file if requested | ||||
| 353 | 4585 | 1.73ms | if ($options{"open"}) { | ||
| 354 | 4583 | 836µs | my $fh; | ||
| 355 | |||||
| 356 | # If we are running before perl5.6.0 we can not auto-vivify | ||||
| 357 | 4583 | 1.75ms | if ($] < 5.006) { | ||
| 358 | $fh = &Symbol::gensym; | ||||
| 359 | } | ||||
| 360 | |||||
| 361 | # Try to make sure this will be marked close-on-exec | ||||
| 362 | # XXX: Win32 doesn't respect this, nor the proper fcntl, | ||||
| 363 | # but may have O_NOINHERIT. This may or may not be in Fcntl. | ||||
| 364 | 4583 | 8.01ms | local $^F = 2; | ||
| 365 | |||||
| 366 | # Attempt to open the file | ||||
| 367 | 4583 | 1.63ms | my $open_success = undef; | ||
| 368 | 4583 | 6.71ms | if ( $^O eq 'VMS' and $options{"unlink_on_close"} && !$KEEP_ALL) { | ||
| 369 | # make it auto delete on close by setting FAB$V_DLT bit | ||||
| 370 | $fh = VMS::Stdio::vmssysopen($path, $OPENFLAGS, 0600, 'fop=dlt'); | ||||
| 371 | $open_success = $fh; | ||||
| 372 | } else { | ||||
| 373 | 4583 | 4.23ms | my $flags = ( ($options{"unlink_on_close"} && !$KEEP_ALL) ? | ||
| 374 | $OPENTEMPFLAGS : | ||||
| 375 | $OPENFLAGS ); | ||||
| 376 | 4583 | 2.21ms | $flags |= $LOCKFLAG if (defined $LOCKFLAG && $options{use_exlock}); | ||
| 377 | 4583 | 265ms | 4583 | 222ms | $open_success = sysopen($fh, $path, $flags, 0600); # spent 222ms making 4583 calls to File::Temp::CORE:sysopen, avg 48µs/call |
| 378 | } | ||||
| 379 | 4583 | 1.83ms | if ( $open_success ) { | ||
| 380 | |||||
| 381 | # in case of odd umask force rw | ||||
| 382 | 4583 | 77.0ms | 4583 | 32.9ms | chmod(0600, $path); # spent 32.9ms making 4583 calls to File::Temp::CORE:chmod, avg 7µs/call |
| 383 | |||||
| 384 | # Opened successfully - return file handle and name | ||||
| 385 | 4583 | 22.6ms | return ($fh, $path); | ||
| 386 | |||||
| 387 | } else { | ||||
| 388 | |||||
| 389 | # Error opening file - abort with error | ||||
| 390 | # if the reason was anything but EEXIST | ||||
| 391 | 1 | 4.29ms | 1 | 11µs | unless ($!{EEXIST}) { # spent 11µs making 1 call to Errno::_tie_it |
| 392 | ${$options{ErrStr}} = "Could not create temp file $path: $!"; | ||||
| 393 | return (); | ||||
| 394 | } | ||||
| 395 | |||||
| 396 | # Loop round for another try | ||||
| 397 | |||||
| 398 | } | ||||
| 399 | } elsif ($options{"mkdir"}) { | ||||
| 400 | |||||
| 401 | # Open the temp directory | ||||
| 402 | 2 | 245µs | 2 | 232µs | if (mkdir( $path, 0700)) { # spent 232µs making 2 calls to File::Temp::CORE:mkdir, avg 116µs/call |
| 403 | # in case of odd umask | ||||
| 404 | 2 | 25µs | 2 | 17µs | chmod(0700, $path); # spent 17µs making 2 calls to File::Temp::CORE:chmod, avg 9µs/call |
| 405 | |||||
| 406 | 2 | 10µs | return undef, $path; | ||
| 407 | } else { | ||||
| 408 | |||||
| 409 | # Abort with error if the reason for failure was anything | ||||
| 410 | # except EEXIST | ||||
| 411 | unless ($!{EEXIST}) { | ||||
| 412 | ${$options{ErrStr}} = "Could not create directory $path: $!"; | ||||
| 413 | return (); | ||||
| 414 | } | ||||
| 415 | |||||
| 416 | # Loop round for another try | ||||
| 417 | |||||
| 418 | } | ||||
| 419 | |||||
| 420 | } else { | ||||
| 421 | |||||
| 422 | # Return true if the file can not be found | ||||
| 423 | # Directory has been checked previously | ||||
| 424 | |||||
| 425 | return (undef, $path) unless -e $path; | ||||
| 426 | |||||
| 427 | # Try again until MAX_TRIES | ||||
| 428 | |||||
| 429 | } | ||||
| 430 | |||||
| 431 | # Did not successfully open the tempfile/dir | ||||
| 432 | # so try again with a different set of random letters | ||||
| 433 | # No point in trying to increment unless we have only | ||||
| 434 | # 1 X say and the randomness could come up with the same | ||||
| 435 | # file MAX_TRIES in a row. | ||||
| 436 | |||||
| 437 | # Store current attempt - in principal this implies that the | ||||
| 438 | # 3rd time around the open attempt that the first temp file | ||||
| 439 | # name could be generated again. Probably should store each | ||||
| 440 | # attempt and make sure that none are repeated | ||||
| 441 | |||||
| 442 | my $original = $path; | ||||
| 443 | my $counter = 0; # Stop infinite loop | ||||
| 444 | my $MAX_GUESS = 50; | ||||
| 445 | |||||
| 446 | do { | ||||
| 447 | |||||
| 448 | # Generate new name from original template | ||||
| 449 | $path = _replace_XX($template, $options{"suffixlen"}); | ||||
| 450 | |||||
| 451 | $counter++; | ||||
| 452 | |||||
| 453 | } until ($path ne $original || $counter > $MAX_GUESS); | ||||
| 454 | |||||
| 455 | # Check for out of control looping | ||||
| 456 | if ($counter > $MAX_GUESS) { | ||||
| 457 | ${$options{ErrStr}} = "Tried to get a new temp name different to the previous value $MAX_GUESS times.\nSomething wrong with template?? ($template)"; | ||||
| 458 | return (); | ||||
| 459 | } | ||||
| 460 | |||||
| 461 | } | ||||
| 462 | |||||
| 463 | # If we get here, we have run out of tries | ||||
| 464 | ${ $options{ErrStr} } = "Have exceeded the maximum number of attempts (" | ||||
| 465 | . MAX_TRIES . ") to open temp file/dir"; | ||||
| 466 | |||||
| 467 | return (); | ||||
| 468 | |||||
| 469 | } | ||||
| 470 | |||||
| 471 | # Internal routine to replace the XXXX... with random characters | ||||
| 472 | # This has to be done by _gettemp() every time it fails to | ||||
| 473 | # open a temp file/dir | ||||
| 474 | |||||
| 475 | # Arguments: $template (the template with XXX), | ||||
| 476 | # $ignore (number of characters at end to ignore) | ||||
| 477 | |||||
| 478 | # Returns: modified template | ||||
| 479 | |||||
| 480 | # spent 343ms (256+87.3) within File::Temp::_replace_XX which was called 4585 times, avg 75µs/call:
# 4585 times (256ms+87.3ms) by File::Temp::_gettemp at line 266, avg 75µs/call | ||||
| 481 | |||||
| 482 | 4585 | 3.49ms | croak 'Usage: _replace_XX($template, $ignore)' | ||
| 483 | unless scalar(@_) == 2; | ||||
| 484 | |||||
| 485 | 4585 | 2.47ms | my ($path, $ignore) = @_; | ||
| 486 | |||||
| 487 | # Do it as an if, since the suffix adjusts which section to replace | ||||
| 488 | # and suffixlen=0 returns nothing if used in the substr directly | ||||
| 489 | # Alternatively, could simply set $ignore to length($path)-1 | ||||
| 490 | # Don't want to always use substr when not required though. | ||||
| 491 | 4585 | 5.10ms | my $end = ( $] >= 5.006 ? "\\z" : "\\Z" ); | ||
| 492 | |||||
| 493 | 4585 | 3.91ms | if ($ignore) { | ||
| 494 | substr($path, 0, - $ignore) =~ s/X(?=X*$end)/$CHARS[ int( rand( @CHARS ) ) ]/ge; | ||||
| 495 | } else { | ||||
| 496 | 4585 | 318ms | 59596 | 87.3ms | $path =~ s/X(?=X*$end)/$CHARS[ int( rand( @CHARS ) ) ]/ge; # spent 54.4ms making 50426 calls to File::Temp::CORE:substcont, avg 1µs/call
# spent 22.8ms making 4585 calls to File::Temp::CORE:subst, avg 5µs/call
# spent 10.1ms making 4585 calls to File::Temp::CORE:regcomp, avg 2µs/call |
| 497 | } | ||||
| 498 | 4585 | 29.5ms | return $path; | ||
| 499 | } | ||||
| 500 | |||||
| 501 | # Internal routine to force a temp file to be writable after | ||||
| 502 | # it is created so that we can unlink it. Windows seems to occasionally | ||||
| 503 | # force a file to be readonly when written to certain temp locations | ||||
| 504 | sub _force_writable { | ||||
| 505 | my $file = shift; | ||||
| 506 | chmod 0600, $file; | ||||
| 507 | } | ||||
| 508 | |||||
| 509 | # internal routine to check to see if the directory is safe | ||||
| 510 | # First checks to see if the directory is not owned by the | ||||
| 511 | # current user or root. Then checks to see if anyone else | ||||
| 512 | # can write to the directory and if so, checks to see if | ||||
| 513 | # it has the sticky bit set | ||||
| 514 | |||||
| 515 | # Will not work on systems that do not support sticky bit | ||||
| 516 | |||||
| 517 | #Args: directory path to check | ||||
| 518 | # Optionally: reference to scalar to contain error message | ||||
| 519 | # Returns true if the path is safe and false otherwise. | ||||
| 520 | # Returns undef if can not even run stat() on the path | ||||
| 521 | |||||
| 522 | # This routine based on version written by Tom Christiansen | ||||
| 523 | |||||
| 524 | # Presumably, by the time we actually attempt to create the | ||||
| 525 | # file or directory in this directory, it may not be safe | ||||
| 526 | # anymore... Have to run _is_safe directly after the open. | ||||
| 527 | |||||
| 528 | sub _is_safe { | ||||
| 529 | |||||
| 530 | my $path = shift; | ||||
| 531 | my $err_ref = shift; | ||||
| 532 | |||||
| 533 | # Stat path | ||||
| 534 | my @info = stat($path); | ||||
| 535 | unless (scalar(@info)) { | ||||
| 536 | $$err_ref = "stat(path) returned no values"; | ||||
| 537 | return 0; | ||||
| 538 | } | ||||
| 539 | ; | ||||
| 540 | return 1 if $^O eq 'VMS'; # owner delete control at file level | ||||
| 541 | |||||
| 542 | # Check to see whether owner is neither superuser (or a system uid) nor me | ||||
| 543 | # Use the effective uid from the $> variable | ||||
| 544 | # UID is in [4] | ||||
| 545 | if ($info[4] > File::Temp->top_system_uid() && $info[4] != $>) { | ||||
| 546 | |||||
| 547 | Carp::cluck(sprintf "uid=$info[4] topuid=%s euid=$> path='$path'", | ||||
| 548 | File::Temp->top_system_uid()); | ||||
| 549 | |||||
| 550 | $$err_ref = "Directory owned neither by root nor the current user" | ||||
| 551 | if ref($err_ref); | ||||
| 552 | return 0; | ||||
| 553 | } | ||||
| 554 | |||||
| 555 | # check whether group or other can write file | ||||
| 556 | # use 066 to detect either reading or writing | ||||
| 557 | # use 022 to check writability | ||||
| 558 | # Do it with S_IWOTH and S_IWGRP for portability (maybe) | ||||
| 559 | # mode is in info[2] | ||||
| 560 | if (($info[2] & &Fcntl::S_IWGRP) || # Is group writable? | ||||
| 561 | ($info[2] & &Fcntl::S_IWOTH) ) { # Is world writable? | ||||
| 562 | # Must be a directory | ||||
| 563 | unless (-d $path) { | ||||
| 564 | $$err_ref = "Path ($path) is not a directory" | ||||
| 565 | if ref($err_ref); | ||||
| 566 | return 0; | ||||
| 567 | } | ||||
| 568 | # Must have sticky bit set | ||||
| 569 | unless (-k $path) { | ||||
| 570 | $$err_ref = "Sticky bit not set on $path when dir is group|world writable" | ||||
| 571 | if ref($err_ref); | ||||
| 572 | return 0; | ||||
| 573 | } | ||||
| 574 | } | ||||
| 575 | |||||
| 576 | return 1; | ||||
| 577 | } | ||||
| 578 | |||||
| 579 | # Internal routine to check whether a directory is safe | ||||
| 580 | # for temp files. Safer than _is_safe since it checks for | ||||
| 581 | # the possibility of chown giveaway and if that is a possibility | ||||
| 582 | # checks each directory in the path to see if it is safe (with _is_safe) | ||||
| 583 | |||||
| 584 | # If _PC_CHOWN_RESTRICTED is not set, does the full test of each | ||||
| 585 | # directory anyway. | ||||
| 586 | |||||
| 587 | # Takes optional second arg as scalar ref to error reason | ||||
| 588 | |||||
| 589 | sub _is_verysafe { | ||||
| 590 | |||||
| 591 | # Need POSIX - but only want to bother if really necessary due to overhead | ||||
| 592 | require POSIX; | ||||
| 593 | |||||
| 594 | my $path = shift; | ||||
| 595 | print "_is_verysafe testing $path\n" if $DEBUG; | ||||
| 596 | return 1 if $^O eq 'VMS'; # owner delete control at file level | ||||
| 597 | |||||
| 598 | my $err_ref = shift; | ||||
| 599 | |||||
| 600 | # Should Get the value of _PC_CHOWN_RESTRICTED if it is defined | ||||
| 601 | # and If it is not there do the extensive test | ||||
| 602 | local($@); | ||||
| 603 | my $chown_restricted; | ||||
| 604 | $chown_restricted = &POSIX::_PC_CHOWN_RESTRICTED() | ||||
| 605 | if eval { &POSIX::_PC_CHOWN_RESTRICTED(); 1}; | ||||
| 606 | |||||
| 607 | # If chown_resticted is set to some value we should test it | ||||
| 608 | if (defined $chown_restricted) { | ||||
| 609 | |||||
| 610 | # Return if the current directory is safe | ||||
| 611 | return _is_safe($path,$err_ref) if POSIX::sysconf( $chown_restricted ); | ||||
| 612 | |||||
| 613 | } | ||||
| 614 | |||||
| 615 | # To reach this point either, the _PC_CHOWN_RESTRICTED symbol | ||||
| 616 | # was not available or the symbol was there but chown giveaway | ||||
| 617 | # is allowed. Either way, we now have to test the entire tree for | ||||
| 618 | # safety. | ||||
| 619 | |||||
| 620 | # Convert path to an absolute directory if required | ||||
| 621 | unless (File::Spec->file_name_is_absolute($path)) { | ||||
| 622 | $path = File::Spec->rel2abs($path); | ||||
| 623 | } | ||||
| 624 | |||||
| 625 | # Split directory into components - assume no file | ||||
| 626 | my ($volume, $directories, undef) = File::Spec->splitpath( $path, 1); | ||||
| 627 | |||||
| 628 | # Slightly less efficient than having a function in File::Spec | ||||
| 629 | # to chop off the end of a directory or even a function that | ||||
| 630 | # can handle ../ in a directory tree | ||||
| 631 | # Sometimes splitdir() returns a blank at the end | ||||
| 632 | # so we will probably check the bottom directory twice in some cases | ||||
| 633 | my @dirs = File::Spec->splitdir($directories); | ||||
| 634 | |||||
| 635 | # Concatenate one less directory each time around | ||||
| 636 | foreach my $pos (0.. $#dirs) { | ||||
| 637 | # Get a directory name | ||||
| 638 | my $dir = File::Spec->catpath($volume, | ||||
| 639 | File::Spec->catdir(@dirs[0.. $#dirs - $pos]), | ||||
| 640 | '' | ||||
| 641 | ); | ||||
| 642 | |||||
| 643 | print "TESTING DIR $dir\n" if $DEBUG; | ||||
| 644 | |||||
| 645 | # Check the directory | ||||
| 646 | return 0 unless _is_safe($dir,$err_ref); | ||||
| 647 | |||||
| 648 | } | ||||
| 649 | |||||
| 650 | return 1; | ||||
| 651 | } | ||||
| 652 | |||||
| 653 | # internal routine to determine whether unlink works on this | ||||
| 654 | # platform for files that are currently open. | ||||
| 655 | # Returns true if we can, false otherwise. | ||||
| 656 | |||||
| 657 | # Currently WinNT, OS/2 and VMS can not unlink an opened file | ||||
| 658 | # On VMS this is because the O_EXCL flag is used to open the | ||||
| 659 | # temporary file. Currently I do not know enough about the issues | ||||
| 660 | # on VMS to decide whether O_EXCL is a requirement. | ||||
| 661 | |||||
| 662 | sub _can_unlink_opened_file { | ||||
| 663 | |||||
| 664 | if (grep { $^O eq $_ } qw/MSWin32 os2 VMS dos MacOS haiku/) { | ||||
| 665 | return 0; | ||||
| 666 | } else { | ||||
| 667 | return 1; | ||||
| 668 | } | ||||
| 669 | |||||
| 670 | } | ||||
| 671 | |||||
| 672 | # internal routine to decide which security levels are allowed | ||||
| 673 | # see safe_level() for more information on this | ||||
| 674 | |||||
| 675 | # Controls whether the supplied security level is allowed | ||||
| 676 | |||||
| 677 | # $cando = _can_do_level( $level ) | ||||
| 678 | |||||
| 679 | sub _can_do_level { | ||||
| 680 | |||||
| 681 | # Get security level | ||||
| 682 | my $level = shift; | ||||
| 683 | |||||
| 684 | # Always have to be able to do STANDARD | ||||
| 685 | return 1 if $level == STANDARD; | ||||
| 686 | |||||
| 687 | # Currently, the systems that can do HIGH or MEDIUM are identical | ||||
| 688 | if ( $^O eq 'MSWin32' || $^O eq 'os2' || $^O eq 'cygwin' || $^O eq 'dos' || $^O eq 'MacOS' || $^O eq 'mpeix') { | ||||
| 689 | return 0; | ||||
| 690 | } else { | ||||
| 691 | return 1; | ||||
| 692 | } | ||||
| 693 | |||||
| 694 | } | ||||
| 695 | |||||
| 696 | # This routine sets up a deferred unlinking of a specified | ||||
| 697 | # filename and filehandle. It is used in the following cases: | ||||
| 698 | # - Called by unlink0 if an opened file can not be unlinked | ||||
| 699 | # - Called by tempfile() if files are to be removed on shutdown | ||||
| 700 | # - Called by tempdir() if directories are to be removed on shutdown | ||||
| 701 | |||||
| 702 | # Arguments: | ||||
| 703 | # _deferred_unlink( $fh, $fname, $isdir ); | ||||
| 704 | # | ||||
| 705 | # - filehandle (so that it can be explicitly closed if open | ||||
| 706 | # - filename (the thing we want to remove) | ||||
| 707 | # - isdir (flag to indicate that we are being given a directory) | ||||
| 708 | # [and hence no filehandle] | ||||
| 709 | |||||
| 710 | # Status is not referred to since all the magic is done with an END block | ||||
| 711 | |||||
| 712 | { | ||||
| 713 | # Will set up two lexical variables to contain all the files to be | ||||
| 714 | # removed. One array for files, another for directories They will | ||||
| 715 | # only exist in this block. | ||||
| 716 | |||||
| 717 | # This means we only have to set up a single END block to remove | ||||
| 718 | # all files. | ||||
| 719 | |||||
| 720 | # in order to prevent child processes inadvertently deleting the parent | ||||
| 721 | # temp files we use a hash to store the temp files and directories | ||||
| 722 | # created by a particular process id. | ||||
| 723 | |||||
| 724 | # %files_to_unlink contains values that are references to an array of | ||||
| 725 | # array references containing the filehandle and filename associated with | ||||
| 726 | # the temp file. | ||||
| 727 | 1 | 500ns | my (%files_to_unlink, %dirs_to_unlink); | ||
| 728 | |||||
| 729 | # Set up an end block to use these arrays | ||||
| 730 | # spent 69µs (30+38) within File::Temp::END which was called:
# once (30µs+38µs) by main::RUNTIME at line 0 of /root/tor-browser-build/rbm/rbm | ||||
| 731 | 1 | 16µs | local($., $@, $!, $^E, $?); | ||
| 732 | 1 | 12µs | 1 | 38µs | cleanup(at_exit => 1); # spent 38µs making 1 call to File::Temp::cleanup |
| 733 | } | ||||
| 734 | |||||
| 735 | # Cleanup function. Always triggered on END (with at_exit => 1) but | ||||
| 736 | # can be invoked manually. | ||||
| 737 | # spent 38µs (36+2) within File::Temp::cleanup which was called:
# once (36µs+2µs) by File::Temp::END at line 732 | ||||
| 738 | 1 | 4µs | my %h = @_; | ||
| 739 | 1 | 2µs | my $at_exit = delete $h{at_exit}; | ||
| 740 | 1 | 1µs | $at_exit = 0 if not defined $at_exit; | ||
| 741 | 3 | 18µs | 1 | 2µs | { my @k = sort keys %h; die "unrecognized parameters: @k" if @k } # spent 2µs making 1 call to File::Temp::CORE:sort |
| 742 | |||||
| 743 | 1 | 10µs | if (!$KEEP_ALL) { | ||
| 744 | # Files | ||||
| 745 | my @files = (exists $files_to_unlink{$$} ? | ||||
| 746 | 1 | 2µs | @{ $files_to_unlink{$$} } : () ); | ||
| 747 | 1 | 1µs | foreach my $file (@files) { | ||
| 748 | # close the filehandle without checking its state | ||||
| 749 | # in order to make real sure that this is closed | ||||
| 750 | # if its already closed then I don't care about the answer | ||||
| 751 | # probably a better way to do this | ||||
| 752 | close($file->[0]); # file handle is [0] | ||||
| 753 | |||||
| 754 | if (-f $file->[1]) { # file name is [1] | ||||
| 755 | _force_writable( $file->[1] ); # for windows | ||||
| 756 | unlink $file->[1] or warn "Error removing ".$file->[1]; | ||||
| 757 | } | ||||
| 758 | } | ||||
| 759 | # Dirs | ||||
| 760 | my @dirs = (exists $dirs_to_unlink{$$} ? | ||||
| 761 | 1 | 2µs | @{ $dirs_to_unlink{$$} } : () ); | ||
| 762 | 1 | 400ns | my ($cwd, $cwd_to_remove); | ||
| 763 | 1 | 500ns | foreach my $dir (@dirs) { | ||
| 764 | if (-d $dir) { | ||||
| 765 | # Some versions of rmtree will abort if you attempt to remove | ||||
| 766 | # the directory you are sitting in. For automatic cleanup | ||||
| 767 | # at program exit, we avoid this by chdir()ing out of the way | ||||
| 768 | # first. If not at program exit, it's best not to mess with the | ||||
| 769 | # current directory, so just let it fail with a warning. | ||||
| 770 | if ($at_exit) { | ||||
| 771 | $cwd = Cwd::abs_path(File::Spec->curdir) if not defined $cwd; | ||||
| 772 | my $abs = Cwd::abs_path($dir); | ||||
| 773 | if ($abs eq $cwd) { | ||||
| 774 | $cwd_to_remove = $dir; | ||||
| 775 | next; | ||||
| 776 | } | ||||
| 777 | } | ||||
| 778 | eval { rmtree($dir, $DEBUG, 0); }; | ||||
| 779 | warn $@ if ($@ && $^W); | ||||
| 780 | } | ||||
| 781 | } | ||||
| 782 | |||||
| 783 | 1 | 1µs | if (defined $cwd_to_remove) { | ||
| 784 | # We do need to clean up the current directory, and everything | ||||
| 785 | # else is done, so get out of there and remove it. | ||||
| 786 | chdir $cwd_to_remove or die "cannot chdir to $cwd_to_remove: $!"; | ||||
| 787 | my $updir = File::Spec->updir; | ||||
| 788 | chdir $updir or die "cannot chdir to $updir: $!"; | ||||
| 789 | eval { rmtree($cwd_to_remove, $DEBUG, 0); }; | ||||
| 790 | warn $@ if ($@ && $^W); | ||||
| 791 | } | ||||
| 792 | |||||
| 793 | # clear the arrays | ||||
| 794 | @{ $files_to_unlink{$$} } = () | ||||
| 795 | 1 | 400ns | if exists $files_to_unlink{$$}; | ||
| 796 | @{ $dirs_to_unlink{$$} } = () | ||||
| 797 | 1 | 700ns | if exists $dirs_to_unlink{$$}; | ||
| 798 | } | ||||
| 799 | } | ||||
| 800 | |||||
| 801 | # This is the sub called to register a file for deferred unlinking | ||||
| 802 | # This could simply store the input parameters and defer everything | ||||
| 803 | # until the END block. For now we do a bit of checking at this | ||||
| 804 | # point in order to make sure that (1) we have a file/dir to delete | ||||
| 805 | # and (2) we have been called with the correct arguments. | ||||
| 806 | sub _deferred_unlink { | ||||
| 807 | |||||
| 808 | croak 'Usage: _deferred_unlink($fh, $fname, $isdir)' | ||||
| 809 | unless scalar(@_) == 3; | ||||
| 810 | |||||
| 811 | my ($fh, $fname, $isdir) = @_; | ||||
| 812 | |||||
| 813 | warn "Setting up deferred removal of $fname\n" | ||||
| 814 | if $DEBUG; | ||||
| 815 | |||||
| 816 | # make sure we save the absolute path for later cleanup | ||||
| 817 | # OK to untaint because we only ever use this internally | ||||
| 818 | # as a file path, never interpolating into the shell | ||||
| 819 | $fname = Cwd::abs_path($fname); | ||||
| 820 | ($fname) = $fname =~ /^(.*)$/; | ||||
| 821 | |||||
| 822 | # If we have a directory, check that it is a directory | ||||
| 823 | if ($isdir) { | ||||
| 824 | |||||
| 825 | if (-d $fname) { | ||||
| 826 | |||||
| 827 | # Directory exists so store it | ||||
| 828 | # first on VMS turn []foo into [.foo] for rmtree | ||||
| 829 | $fname = VMS::Filespec::vmspath($fname) if $^O eq 'VMS'; | ||||
| 830 | $dirs_to_unlink{$$} = [] | ||||
| 831 | unless exists $dirs_to_unlink{$$}; | ||||
| 832 | push (@{ $dirs_to_unlink{$$} }, $fname); | ||||
| 833 | |||||
| 834 | } else { | ||||
| 835 | carp "Request to remove directory $fname could not be completed since it does not exist!\n" if $^W; | ||||
| 836 | } | ||||
| 837 | |||||
| 838 | } else { | ||||
| 839 | |||||
| 840 | if (-f $fname) { | ||||
| 841 | |||||
| 842 | # file exists so store handle and name for later removal | ||||
| 843 | $files_to_unlink{$$} = [] | ||||
| 844 | unless exists $files_to_unlink{$$}; | ||||
| 845 | push(@{ $files_to_unlink{$$} }, [$fh, $fname]); | ||||
| 846 | |||||
| 847 | } else { | ||||
| 848 | carp "Request to remove file $fname could not be completed since it is not there!\n" if $^W; | ||||
| 849 | } | ||||
| 850 | |||||
| 851 | } | ||||
| 852 | |||||
| 853 | } | ||||
| 854 | |||||
| 855 | } | ||||
| 856 | |||||
| 857 | # normalize argument keys to upper case and do consistent handling | ||||
| 858 | # of leading template vs TEMPLATE | ||||
| 859 | 1 | 100ns | # spent 38.0ms within File::Temp::_parse_args which was called 4587 times, avg 8µs/call:
# 4583 times (37.9ms+0s) by File::Temp::tempfile at line 1009, avg 8µs/call
# 2 times (29µs+0s) by File::Temp::newdir at line 914, avg 15µs/call
# 2 times (7µs+0s) by File::Temp::tempdir at line 1137, avg 4µs/call | ||
| 860 | 4587 | 7.30ms | my $leading_template = (scalar(@_) % 2 == 1 ? shift(@_) : '' ); | ||
| 861 | 4587 | 2.72ms | my %args = @_; | ||
| 862 | 4587 | 7.11ms | %args = map { uc($_), $args{$_} } keys %args; | ||
| 863 | |||||
| 864 | # template (store it in an array so that it will | ||||
| 865 | # disappear from the arg list of tempfile) | ||||
| 866 | my @template = ( | ||||
| 867 | exists $args{TEMPLATE} ? $args{TEMPLATE} : | ||||
| 868 | 4587 | 7.51ms | $leading_template ? $leading_template : () | ||
| 869 | ); | ||||
| 870 | 4587 | 1.63ms | delete $args{TEMPLATE}; | ||
| 871 | |||||
| 872 | 4587 | 36.8ms | return( \@template, \%args ); | ||
| 873 | } | ||||
| 874 | |||||
| 875 | sub new { | ||||
| 876 | my $proto = shift; | ||||
| 877 | my $class = ref($proto) || $proto; | ||||
| 878 | |||||
| 879 | my ($maybe_template, $args) = _parse_args(@_); | ||||
| 880 | |||||
| 881 | # see if they are unlinking (defaulting to yes) | ||||
| 882 | my $unlink = (exists $args->{UNLINK} ? $args->{UNLINK} : 1 ); | ||||
| 883 | delete $args->{UNLINK}; | ||||
| 884 | |||||
| 885 | # Protect OPEN | ||||
| 886 | delete $args->{OPEN}; | ||||
| 887 | |||||
| 888 | # Open the file and retain file handle and file name | ||||
| 889 | my ($fh, $path) = tempfile( @$maybe_template, %$args ); | ||||
| 890 | |||||
| 891 | print "Tmp: $fh - $path\n" if $DEBUG; | ||||
| 892 | |||||
| 893 | # Store the filename in the scalar slot | ||||
| 894 | ${*$fh} = $path; | ||||
| 895 | |||||
| 896 | # Cache the filename by pid so that the destructor can decide whether to remove it | ||||
| 897 | $FILES_CREATED_BY_OBJECT{$$}{$path} = 1; | ||||
| 898 | |||||
| 899 | # Store unlink information in hash slot (plus other constructor info) | ||||
| 900 | %{*$fh} = %$args; | ||||
| 901 | |||||
| 902 | # create the object | ||||
| 903 | bless $fh, $class; | ||||
| 904 | |||||
| 905 | # final method-based configuration | ||||
| 906 | $fh->unlink_on_destroy( $unlink ); | ||||
| 907 | |||||
| 908 | return $fh; | ||||
| 909 | } | ||||
| 910 | |||||
| 911 | # spent 981µs (168+814) within File::Temp::newdir which was called 2 times, avg 491µs/call:
# once (140µs+461µs) by RBM::build_run at line 1008 of /root/tor-browser-build/rbm/lib/RBM.pm
# once (28µs+352µs) by RBM::DefaultConfig::rbm_tmp_dir at line 111 of /root/tor-browser-build/rbm/lib/RBM/DefaultConfig.pm | ||||
| 912 | 2 | 1µs | my $self = shift; | ||
| 913 | |||||
| 914 | 2 | 7µs | 2 | 29µs | my ($maybe_template, $args) = _parse_args(@_); # spent 29µs making 2 calls to File::Temp::_parse_args, avg 15µs/call |
| 915 | |||||
| 916 | # handle CLEANUP without passing CLEANUP to tempdir | ||||
| 917 | 2 | 2µs | my $cleanup = (exists $args->{CLEANUP} ? $args->{CLEANUP} : 1 ); | ||
| 918 | 2 | 900ns | delete $args->{CLEANUP}; | ||
| 919 | |||||
| 920 | 2 | 8µs | 2 | 750µs | my $tempdir = tempdir( @$maybe_template, %$args); # spent 750µs making 2 calls to File::Temp::tempdir, avg 375µs/call |
| 921 | |||||
| 922 | # get a safe absolute path for cleanup, just like | ||||
| 923 | # happens in _deferred_unlink | ||||
| 924 | 2 | 36µs | 2 | 28µs | my $real_dir = Cwd::abs_path( $tempdir ); # spent 28µs making 2 calls to Cwd::abs_path, avg 14µs/call |
| 925 | 2 | 122µs | 2 | 7µs | ($real_dir) = $real_dir =~ /^(.*)$/; # spent 7µs making 2 calls to File::Temp::CORE:match, avg 3µs/call |
| 926 | |||||
| 927 | 2 | 24µs | return bless { DIRNAME => $tempdir, | ||
| 928 | REALNAME => $real_dir, | ||||
| 929 | CLEANUP => $cleanup, | ||||
| 930 | LAUNCHPID => $$, | ||||
| 931 | }, "File::Temp::Dir"; | ||||
| 932 | } | ||||
| 933 | |||||
| 934 | sub filename { | ||||
| 935 | my $self = shift; | ||||
| 936 | return ${*$self}; | ||||
| 937 | } | ||||
| 938 | |||||
| 939 | sub STRINGIFY { | ||||
| 940 | my $self = shift; | ||||
| 941 | return $self->filename; | ||||
| 942 | } | ||||
| 943 | |||||
| 944 | # For reference, can't use '0+'=>\&Scalar::Util::refaddr directly because | ||||
| 945 | # refaddr() demands one parameter only, whereas overload.pm calls with three | ||||
| 946 | # even for unary operations like '0+'. | ||||
| 947 | # spent 361µs (258+103) within File::Temp::NUMIFY which was called 25 times, avg 14µs/call:
# 25 times (258µs+103µs) by RBM::DefaultConfig::rbm_tmp_dir at line 108 of /root/tor-browser-build/rbm/lib/RBM/DefaultConfig.pm, avg 14µs/call | ||||
| 948 | 25 | 373µs | 25 | 103µs | return refaddr($_[0]); # spent 103µs making 25 calls to Scalar::Util::refaddr, avg 4µs/call |
| 949 | } | ||||
| 950 | |||||
| 951 | sub unlink_on_destroy { | ||||
| 952 | my $self = shift; | ||||
| 953 | if (@_) { | ||||
| 954 | ${*$self}{UNLINK} = shift; | ||||
| 955 | } | ||||
| 956 | return ${*$self}{UNLINK}; | ||||
| 957 | } | ||||
| 958 | |||||
| 959 | sub DESTROY { | ||||
| 960 | local($., $@, $!, $^E, $?); | ||||
| 961 | my $self = shift; | ||||
| 962 | |||||
| 963 | # Make sure we always remove the file from the global hash | ||||
| 964 | # on destruction. This prevents the hash from growing uncontrollably | ||||
| 965 | # and post-destruction there is no reason to know about the file. | ||||
| 966 | my $file = $self->filename; | ||||
| 967 | my $was_created_by_proc; | ||||
| 968 | if (exists $FILES_CREATED_BY_OBJECT{$$}{$file}) { | ||||
| 969 | $was_created_by_proc = 1; | ||||
| 970 | delete $FILES_CREATED_BY_OBJECT{$$}{$file}; | ||||
| 971 | } | ||||
| 972 | |||||
| 973 | if (${*$self}{UNLINK} && !$KEEP_ALL) { | ||||
| 974 | print "# ---------> Unlinking $self\n" if $DEBUG; | ||||
| 975 | |||||
| 976 | # only delete if this process created it | ||||
| 977 | return unless $was_created_by_proc; | ||||
| 978 | |||||
| 979 | # The unlink1 may fail if the file has been closed | ||||
| 980 | # by the caller. This leaves us with the decision | ||||
| 981 | # of whether to refuse to remove the file or simply | ||||
| 982 | # do an unlink without test. Seems to be silly | ||||
| 983 | # to do this when we are trying to be careful | ||||
| 984 | # about security | ||||
| 985 | _force_writable( $file ); # for windows | ||||
| 986 | unlink1( $self, $file ) | ||||
| 987 | or unlink($file); | ||||
| 988 | } | ||||
| 989 | } | ||||
| 990 | |||||
| 991 | # spent 1.64s (252ms+1.39) within File::Temp::tempfile which was called 4583 times, avg 358µs/call:
# 4568 times (251ms+1.37s) by IO::CaptureOutput::_proxy::new at line 168 of IO/CaptureOutput.pm, avg 355µs/call
# 15 times (903µs+14.4ms) by RBM::run_script at line 465 of /root/tor-browser-build/rbm/lib/RBM.pm, avg 1.02ms/call | ||||
| 992 | 4583 | 2.57ms | if ( @_ && $_[0] eq 'File::Temp' ) { | ||
| 993 | croak "'tempfile' can't be called as a method"; | ||||
| 994 | } | ||||
| 995 | # Can not check for argument count since we can have any | ||||
| 996 | # number of args | ||||
| 997 | |||||
| 998 | # Default options | ||||
| 999 | 4583 | 35.8ms | my %options = ( | ||
| 1000 | "DIR" => undef, # Directory prefix | ||||
| 1001 | "SUFFIX" => '', # Template suffix | ||||
| 1002 | "UNLINK" => 0, # Do not unlink file on exit | ||||
| 1003 | "OPEN" => 1, # Open file | ||||
| 1004 | "TMPDIR" => 0, # Place tempfile in tempdir if template specified | ||||
| 1005 | "EXLOCK" => 1, # Open file with O_EXLOCK | ||||
| 1006 | ); | ||||
| 1007 | |||||
| 1008 | # Check to see whether we have an odd or even number of arguments | ||||
| 1009 | 4583 | 13.6ms | 4583 | 37.9ms | my ($maybe_template, $args) = _parse_args(@_); # spent 37.9ms making 4583 calls to File::Temp::_parse_args, avg 8µs/call |
| 1010 | 4583 | 3.22ms | my $template = @$maybe_template ? $maybe_template->[0] : undef; | ||
| 1011 | |||||
| 1012 | # Read the options and merge with defaults | ||||
| 1013 | 4583 | 15.3ms | %options = (%options, %$args); | ||
| 1014 | |||||
| 1015 | # First decision is whether or not to open the file | ||||
| 1016 | 4583 | 3.69ms | if (! $options{"OPEN"}) { | ||
| 1017 | |||||
| 1018 | warn "tempfile(): temporary filename requested but not opened.\nPossibly unsafe, consider using tempfile() with OPEN set to true\n" | ||||
| 1019 | if $^W; | ||||
| 1020 | |||||
| 1021 | } | ||||
| 1022 | |||||
| 1023 | 4583 | 1.93ms | if ($options{"DIR"} and $^O eq 'VMS') { | ||
| 1024 | |||||
| 1025 | # on VMS turn []foo into [.foo] for concatenation | ||||
| 1026 | $options{"DIR"} = VMS::Filespec::vmspath($options{"DIR"}); | ||||
| 1027 | } | ||||
| 1028 | |||||
| 1029 | # Construct the template | ||||
| 1030 | |||||
| 1031 | # Have a choice of trying to work around the mkstemp/mktemp/tmpnam etc | ||||
| 1032 | # functions or simply constructing a template and using _gettemp() | ||||
| 1033 | # explicitly. Go for the latter | ||||
| 1034 | |||||
| 1035 | # First generate a template if not defined and prefix the directory | ||||
| 1036 | # If no template must prefix the temp directory | ||||
| 1037 | 4583 | 5.04ms | if (defined $template) { | ||
| 1038 | # End up with current directory if neither DIR not TMPDIR are set | ||||
| 1039 | if ($options{"DIR"}) { | ||||
| 1040 | |||||
| 1041 | $template = File::Spec->catfile($options{"DIR"}, $template); | ||||
| 1042 | |||||
| 1043 | } elsif ($options{TMPDIR}) { | ||||
| 1044 | |||||
| 1045 | $template = File::Spec->catfile(File::Spec->tmpdir, $template ); | ||||
| 1046 | |||||
| 1047 | } | ||||
| 1048 | |||||
| 1049 | } else { | ||||
| 1050 | |||||
| 1051 | 4583 | 3.13ms | 60 | 792µs | if ($options{"DIR"}) { # spent 650µs making 15 calls to File::Spec::Unix::catfile, avg 43µs/call
# spent 102µs making 15 calls to File::Spec::Unix::catdir, avg 7µs/call
# spent 40µs making 30 calls to File::Spec::Unix::canonpath, avg 1µs/call |
| 1052 | |||||
| 1053 | $template = File::Spec->catfile($options{"DIR"}, TEMPXXX); | ||||
| 1054 | |||||
| 1055 | } else { | ||||
| 1056 | |||||
| 1057 | 4568 | 182ms | 22840 | 227ms | $template = File::Spec->catfile(File::Spec->tmpdir, TEMPXXX); # spent 103ms making 4568 calls to File::Spec::Unix::catfile, avg 23µs/call
# spent 85.9ms making 4568 calls to File::Spec::Unix::tmpdir, avg 19µs/call
# spent 28.9ms making 4568 calls to File::Spec::Unix::catdir, avg 6µs/call
# spent 9.36ms making 9136 calls to File::Spec::Unix::canonpath, avg 1µs/call |
| 1058 | |||||
| 1059 | } | ||||
| 1060 | |||||
| 1061 | } | ||||
| 1062 | |||||
| 1063 | # Now add a suffix | ||||
| 1064 | 4583 | 2.65ms | $template .= $options{"SUFFIX"}; | ||
| 1065 | |||||
| 1066 | # Determine whether we should tell _gettemp to unlink the file | ||||
| 1067 | # On unix this is irrelevant and can be worked out after the file is | ||||
| 1068 | # opened (simply by unlinking the open filehandle). On Windows or VMS | ||||
| 1069 | # we have to indicate temporary-ness when we open the file. In general | ||||
| 1070 | # we only want a true temporary file if we are returning just the | ||||
| 1071 | # filehandle - if the user wants the filename they probably do not | ||||
| 1072 | # want the file to disappear as soon as they close it (which may be | ||||
| 1073 | # important if they want a child process to use the file) | ||||
| 1074 | # For this reason, tie unlink_on_close to the return context regardless | ||||
| 1075 | # of OS. | ||||
| 1076 | 4583 | 3.28ms | my $unlink_on_close = ( wantarray ? 0 : 1); | ||
| 1077 | |||||
| 1078 | # Create the file | ||||
| 1079 | 4583 | 1.35ms | my ($fh, $path, $errstr); | ||
| 1080 | croak "Error in tempfile() using template $template: $errstr" | ||||
| 1081 | unless (($fh, $path) = _gettemp($template, | ||||
| 1082 | "open" => $options{'OPEN'}, | ||||
| 1083 | "mkdir"=> 0 , | ||||
| 1084 | "unlink_on_close" => $unlink_on_close, | ||||
| 1085 | "suffixlen" => length($options{'SUFFIX'}), | ||||
| 1086 | "ErrStr" => \$errstr, | ||||
| 1087 | "use_exlock" => $options{EXLOCK}, | ||||
| 1088 | 4583 | 20.7ms | 4583 | 1.16s | ) ); # spent 1.16s making 4583 calls to File::Temp::_gettemp, avg 253µs/call |
| 1089 | |||||
| 1090 | # Set up an exit handler that can do whatever is right for the | ||||
| 1091 | # system. This removes files at exit when requested explicitly or when | ||||
| 1092 | # system is asked to unlink_on_close but is unable to do so because | ||||
| 1093 | # of OS limitations. | ||||
| 1094 | # The latter should be achieved by using a tied filehandle. | ||||
| 1095 | # Do not check return status since this is all done with END blocks. | ||||
| 1096 | 4583 | 2.37ms | _deferred_unlink($fh, $path, 0) if $options{"UNLINK"}; | ||
| 1097 | |||||
| 1098 | # Return | ||||
| 1099 | 4583 | 23.6ms | if (wantarray()) { | ||
| 1100 | |||||
| 1101 | if ($options{'OPEN'}) { | ||||
| 1102 | return ($fh, $path); | ||||
| 1103 | } else { | ||||
| 1104 | return (undef, $path); | ||||
| 1105 | } | ||||
| 1106 | |||||
| 1107 | } else { | ||||
| 1108 | |||||
| 1109 | # Unlink the file. It is up to unlink0 to decide what to do with | ||||
| 1110 | # this (whether to unlink now or to defer until later) | ||||
| 1111 | unlink0($fh, $path) or croak "Error unlinking file $path using unlink0"; | ||||
| 1112 | |||||
| 1113 | # Return just the filehandle. | ||||
| 1114 | return $fh; | ||||
| 1115 | } | ||||
| 1116 | |||||
| 1117 | } | ||||
| 1118 | |||||
| 1119 | # ' | ||||
| 1120 | |||||
| 1121 | # spent 750µs (77+672) within File::Temp::tempdir which was called 2 times, avg 375µs/call:
# 2 times (77µs+672µs) by File::Temp::newdir at line 920, avg 375µs/call | ||||
| 1122 | 2 | 2µs | if ( @_ && $_[0] eq 'File::Temp' ) { | ||
| 1123 | croak "'tempdir' can't be called as a method"; | ||||
| 1124 | } | ||||
| 1125 | |||||
| 1126 | # Can not check for argument count since we can have any | ||||
| 1127 | # number of args | ||||
| 1128 | |||||
| 1129 | # Default options | ||||
| 1130 | 2 | 7µs | my %options = ( | ||
| 1131 | "CLEANUP" => 0, # Remove directory on exit | ||||
| 1132 | "DIR" => '', # Root directory | ||||
| 1133 | "TMPDIR" => 0, # Use tempdir with template | ||||
| 1134 | ); | ||||
| 1135 | |||||
| 1136 | # Check to see whether we have an odd or even number of arguments | ||||
| 1137 | 2 | 4µs | 2 | 7µs | my ($maybe_template, $args) = _parse_args(@_); # spent 7µs making 2 calls to File::Temp::_parse_args, avg 4µs/call |
| 1138 | 2 | 3µs | my $template = @$maybe_template ? $maybe_template->[0] : undef; | ||
| 1139 | |||||
| 1140 | # Read the options and merge with defaults | ||||
| 1141 | 2 | 6µs | %options = (%options, %$args); | ||
| 1142 | |||||
| 1143 | # Modify or generate the template | ||||
| 1144 | |||||
| 1145 | # Deal with the DIR and TMPDIR options | ||||
| 1146 | 2 | 2µs | if (defined $template) { | ||
| 1147 | |||||
| 1148 | # Need to strip directory path if using DIR or TMPDIR | ||||
| 1149 | 2 | 3µs | if ($options{'TMPDIR'} || $options{'DIR'}) { | ||
| 1150 | |||||
| 1151 | # Strip parent directory from the filename | ||||
| 1152 | # | ||||
| 1153 | # There is no filename at the end | ||||
| 1154 | 1 | 1µs | $template = VMS::Filespec::vmspath($template) if $^O eq 'VMS'; | ||
| 1155 | 1 | 6µs | 1 | 6µs | my ($volume, $directories, undef) = File::Spec->splitpath( $template, 1); # spent 6µs making 1 call to File::Spec::Unix::splitpath |
| 1156 | |||||
| 1157 | # Last directory is then our template | ||||
| 1158 | 1 | 2µs | 1 | 2µs | $template = (File::Spec->splitdir($directories))[-1]; # spent 2µs making 1 call to File::Spec::Unix::splitdir |
| 1159 | |||||
| 1160 | # Prepend the supplied directory or temp dir | ||||
| 1161 | 1 | 22µs | 2 | 16µs | if ($options{"DIR"}) { # spent 15µs making 1 call to File::Spec::Unix::catdir
# spent 2µs making 1 call to File::Spec::Unix::canonpath |
| 1162 | |||||
| 1163 | $template = File::Spec->catdir($options{"DIR"}, $template); | ||||
| 1164 | |||||
| 1165 | } elsif ($options{TMPDIR}) { | ||||
| 1166 | |||||
| 1167 | # Prepend tmpdir | ||||
| 1168 | $template = File::Spec->catdir(File::Spec->tmpdir, $template); | ||||
| 1169 | |||||
| 1170 | } | ||||
| 1171 | |||||
| 1172 | } | ||||
| 1173 | |||||
| 1174 | } else { | ||||
| 1175 | |||||
| 1176 | if ($options{"DIR"}) { | ||||
| 1177 | |||||
| 1178 | $template = File::Spec->catdir($options{"DIR"}, TEMPXXX); | ||||
| 1179 | |||||
| 1180 | } else { | ||||
| 1181 | |||||
| 1182 | $template = File::Spec->catdir(File::Spec->tmpdir, TEMPXXX); | ||||
| 1183 | |||||
| 1184 | } | ||||
| 1185 | |||||
| 1186 | } | ||||
| 1187 | |||||
| 1188 | # Create the directory | ||||
| 1189 | 2 | 900ns | my $tempdir; | ||
| 1190 | 2 | 600ns | my $suffixlen = 0; | ||
| 1191 | 2 | 2µs | if ($^O eq 'VMS') { # dir names can end in delimiters | ||
| 1192 | $template =~ m/([\.\]:>]+)$/; | ||||
| 1193 | $suffixlen = length($1); | ||||
| 1194 | } | ||||
| 1195 | 2 | 2µs | if ( ($^O eq 'MacOS') && (substr($template, -1) eq ':') ) { | ||
| 1196 | # dir name has a trailing ':' | ||||
| 1197 | ++$suffixlen; | ||||
| 1198 | } | ||||
| 1199 | |||||
| 1200 | 2 | 500ns | my $errstr; | ||
| 1201 | 2 | 10µs | 2 | 642µs | croak "Error in tempdir() using $template: $errstr" # spent 642µs making 2 calls to File::Temp::_gettemp, avg 321µs/call |
| 1202 | unless ((undef, $tempdir) = _gettemp($template, | ||||
| 1203 | "open" => 0, | ||||
| 1204 | "mkdir"=> 1 , | ||||
| 1205 | "suffixlen" => $suffixlen, | ||||
| 1206 | "ErrStr" => \$errstr, | ||||
| 1207 | ) ); | ||||
| 1208 | |||||
| 1209 | # Install exit handler; must be dynamic to get lexical | ||||
| 1210 | 2 | 800ns | if ( $options{'CLEANUP'} && -d $tempdir) { | ||
| 1211 | _deferred_unlink(undef, $tempdir, 1); | ||||
| 1212 | } | ||||
| 1213 | |||||
| 1214 | # Return the dir name | ||||
| 1215 | 2 | 8µs | return $tempdir; | ||
| 1216 | |||||
| 1217 | } | ||||
| 1218 | |||||
| 1219 | sub mkstemp { | ||||
| 1220 | |||||
| 1221 | croak "Usage: mkstemp(template)" | ||||
| 1222 | if scalar(@_) != 1; | ||||
| 1223 | |||||
| 1224 | my $template = shift; | ||||
| 1225 | |||||
| 1226 | my ($fh, $path, $errstr); | ||||
| 1227 | croak "Error in mkstemp using $template: $errstr" | ||||
| 1228 | unless (($fh, $path) = _gettemp($template, | ||||
| 1229 | "open" => 1, | ||||
| 1230 | "mkdir"=> 0 , | ||||
| 1231 | "suffixlen" => 0, | ||||
| 1232 | "ErrStr" => \$errstr, | ||||
| 1233 | ) ); | ||||
| 1234 | |||||
| 1235 | if (wantarray()) { | ||||
| 1236 | return ($fh, $path); | ||||
| 1237 | } else { | ||||
| 1238 | return $fh; | ||||
| 1239 | } | ||||
| 1240 | |||||
| 1241 | } | ||||
| 1242 | |||||
| 1243 | sub mkstemps { | ||||
| 1244 | |||||
| 1245 | croak "Usage: mkstemps(template, suffix)" | ||||
| 1246 | if scalar(@_) != 2; | ||||
| 1247 | |||||
| 1248 | my $template = shift; | ||||
| 1249 | my $suffix = shift; | ||||
| 1250 | |||||
| 1251 | $template .= $suffix; | ||||
| 1252 | |||||
| 1253 | my ($fh, $path, $errstr); | ||||
| 1254 | croak "Error in mkstemps using $template: $errstr" | ||||
| 1255 | unless (($fh, $path) = _gettemp($template, | ||||
| 1256 | "open" => 1, | ||||
| 1257 | "mkdir"=> 0 , | ||||
| 1258 | "suffixlen" => length($suffix), | ||||
| 1259 | "ErrStr" => \$errstr, | ||||
| 1260 | ) ); | ||||
| 1261 | |||||
| 1262 | if (wantarray()) { | ||||
| 1263 | return ($fh, $path); | ||||
| 1264 | } else { | ||||
| 1265 | return $fh; | ||||
| 1266 | } | ||||
| 1267 | |||||
| 1268 | } | ||||
| 1269 | |||||
| 1270 | #' # for emacs | ||||
| 1271 | |||||
| 1272 | sub mkdtemp { | ||||
| 1273 | |||||
| 1274 | croak "Usage: mkdtemp(template)" | ||||
| 1275 | if scalar(@_) != 1; | ||||
| 1276 | |||||
| 1277 | my $template = shift; | ||||
| 1278 | my $suffixlen = 0; | ||||
| 1279 | if ($^O eq 'VMS') { # dir names can end in delimiters | ||||
| 1280 | $template =~ m/([\.\]:>]+)$/; | ||||
| 1281 | $suffixlen = length($1); | ||||
| 1282 | } | ||||
| 1283 | if ( ($^O eq 'MacOS') && (substr($template, -1) eq ':') ) { | ||||
| 1284 | # dir name has a trailing ':' | ||||
| 1285 | ++$suffixlen; | ||||
| 1286 | } | ||||
| 1287 | my ($junk, $tmpdir, $errstr); | ||||
| 1288 | croak "Error creating temp directory from template $template\: $errstr" | ||||
| 1289 | unless (($junk, $tmpdir) = _gettemp($template, | ||||
| 1290 | "open" => 0, | ||||
| 1291 | "mkdir"=> 1 , | ||||
| 1292 | "suffixlen" => $suffixlen, | ||||
| 1293 | "ErrStr" => \$errstr, | ||||
| 1294 | ) ); | ||||
| 1295 | |||||
| 1296 | return $tmpdir; | ||||
| 1297 | |||||
| 1298 | } | ||||
| 1299 | |||||
| 1300 | sub mktemp { | ||||
| 1301 | |||||
| 1302 | croak "Usage: mktemp(template)" | ||||
| 1303 | if scalar(@_) != 1; | ||||
| 1304 | |||||
| 1305 | my $template = shift; | ||||
| 1306 | |||||
| 1307 | my ($tmpname, $junk, $errstr); | ||||
| 1308 | croak "Error getting name to temp file from template $template: $errstr" | ||||
| 1309 | unless (($junk, $tmpname) = _gettemp($template, | ||||
| 1310 | "open" => 0, | ||||
| 1311 | "mkdir"=> 0 , | ||||
| 1312 | "suffixlen" => 0, | ||||
| 1313 | "ErrStr" => \$errstr, | ||||
| 1314 | ) ); | ||||
| 1315 | |||||
| 1316 | return $tmpname; | ||||
| 1317 | } | ||||
| 1318 | |||||
| 1319 | sub tmpnam { | ||||
| 1320 | |||||
| 1321 | # Retrieve the temporary directory name | ||||
| 1322 | my $tmpdir = File::Spec->tmpdir; | ||||
| 1323 | |||||
| 1324 | croak "Error temporary directory is not writable" | ||||
| 1325 | if $tmpdir eq ''; | ||||
| 1326 | |||||
| 1327 | # Use a ten character template and append to tmpdir | ||||
| 1328 | my $template = File::Spec->catfile($tmpdir, TEMPXXX); | ||||
| 1329 | |||||
| 1330 | if (wantarray() ) { | ||||
| 1331 | return mkstemp($template); | ||||
| 1332 | } else { | ||||
| 1333 | return mktemp($template); | ||||
| 1334 | } | ||||
| 1335 | |||||
| 1336 | } | ||||
| 1337 | |||||
| 1338 | sub tmpfile { | ||||
| 1339 | |||||
| 1340 | # Simply call tmpnam() in a list context | ||||
| 1341 | my ($fh, $file) = tmpnam(); | ||||
| 1342 | |||||
| 1343 | # Make sure file is removed when filehandle is closed | ||||
| 1344 | # This will fail on NFS | ||||
| 1345 | unlink0($fh, $file) | ||||
| 1346 | or return undef; | ||||
| 1347 | |||||
| 1348 | return $fh; | ||||
| 1349 | |||||
| 1350 | } | ||||
| 1351 | |||||
| 1352 | sub tempnam { | ||||
| 1353 | |||||
| 1354 | croak 'Usage tempnam($dir, $prefix)' unless scalar(@_) == 2; | ||||
| 1355 | |||||
| 1356 | my ($dir, $prefix) = @_; | ||||
| 1357 | |||||
| 1358 | # Add a string to the prefix | ||||
| 1359 | $prefix .= 'XXXXXXXX'; | ||||
| 1360 | |||||
| 1361 | # Concatenate the directory to the file | ||||
| 1362 | my $template = File::Spec->catfile($dir, $prefix); | ||||
| 1363 | |||||
| 1364 | return mktemp($template); | ||||
| 1365 | |||||
| 1366 | } | ||||
| 1367 | |||||
| 1368 | sub unlink0 { | ||||
| 1369 | |||||
| 1370 | croak 'Usage: unlink0(filehandle, filename)' | ||||
| 1371 | unless scalar(@_) == 2; | ||||
| 1372 | |||||
| 1373 | # Read args | ||||
| 1374 | my ($fh, $path) = @_; | ||||
| 1375 | |||||
| 1376 | cmpstat($fh, $path) or return 0; | ||||
| 1377 | |||||
| 1378 | # attempt remove the file (does not work on some platforms) | ||||
| 1379 | if (_can_unlink_opened_file()) { | ||||
| 1380 | |||||
| 1381 | # return early (Without unlink) if we have been instructed to retain files. | ||||
| 1382 | return 1 if $KEEP_ALL; | ||||
| 1383 | |||||
| 1384 | # XXX: do *not* call this on a directory; possible race | ||||
| 1385 | # resulting in recursive removal | ||||
| 1386 | croak "unlink0: $path has become a directory!" if -d $path; | ||||
| 1387 | unlink($path) or return 0; | ||||
| 1388 | |||||
| 1389 | # Stat the filehandle | ||||
| 1390 | my @fh = stat $fh; | ||||
| 1391 | |||||
| 1392 | print "Link count = $fh[3] \n" if $DEBUG; | ||||
| 1393 | |||||
| 1394 | # Make sure that the link count is zero | ||||
| 1395 | # - Cygwin provides deferred unlinking, however, | ||||
| 1396 | # on Win9x the link count remains 1 | ||||
| 1397 | # On NFS the link count may still be 1 but we can't know that | ||||
| 1398 | # we are on NFS. Since we can't be sure, we'll defer it | ||||
| 1399 | |||||
| 1400 | return 1 if $fh[3] == 0 || $^O eq 'cygwin'; | ||||
| 1401 | } | ||||
| 1402 | # fall-through if we can't unlink now | ||||
| 1403 | _deferred_unlink($fh, $path, 0); | ||||
| 1404 | return 1; | ||||
| 1405 | } | ||||
| 1406 | |||||
| 1407 | sub cmpstat { | ||||
| 1408 | |||||
| 1409 | croak 'Usage: cmpstat(filehandle, filename)' | ||||
| 1410 | unless scalar(@_) == 2; | ||||
| 1411 | |||||
| 1412 | # Read args | ||||
| 1413 | my ($fh, $path) = @_; | ||||
| 1414 | |||||
| 1415 | warn "Comparing stat\n" | ||||
| 1416 | if $DEBUG; | ||||
| 1417 | |||||
| 1418 | # Stat the filehandle - which may be closed if someone has manually | ||||
| 1419 | # closed the file. Can not turn off warnings without using $^W | ||||
| 1420 | # unless we upgrade to 5.006 minimum requirement | ||||
| 1421 | my @fh; | ||||
| 1422 | { | ||||
| 1423 | local ($^W) = 0; | ||||
| 1424 | @fh = stat $fh; | ||||
| 1425 | } | ||||
| 1426 | return unless @fh; | ||||
| 1427 | |||||
| 1428 | if ($fh[3] > 1 && $^W) { | ||||
| 1429 | carp "unlink0: fstat found too many links; SB=@fh" if $^W; | ||||
| 1430 | } | ||||
| 1431 | |||||
| 1432 | # Stat the path | ||||
| 1433 | my @path = stat $path; | ||||
| 1434 | |||||
| 1435 | unless (@path) { | ||||
| 1436 | carp "unlink0: $path is gone already" if $^W; | ||||
| 1437 | return; | ||||
| 1438 | } | ||||
| 1439 | |||||
| 1440 | # this is no longer a file, but may be a directory, or worse | ||||
| 1441 | unless (-f $path) { | ||||
| 1442 | confess "panic: $path is no longer a file: SB=@fh"; | ||||
| 1443 | } | ||||
| 1444 | |||||
| 1445 | # Do comparison of each member of the array | ||||
| 1446 | # On WinNT dev and rdev seem to be different | ||||
| 1447 | # depending on whether it is a file or a handle. | ||||
| 1448 | # Cannot simply compare all members of the stat return | ||||
| 1449 | # Select the ones we can use | ||||
| 1450 | my @okstat = (0..$#fh); # Use all by default | ||||
| 1451 | if ($^O eq 'MSWin32') { | ||||
| 1452 | @okstat = (1,2,3,4,5,7,8,9,10); | ||||
| 1453 | } elsif ($^O eq 'os2') { | ||||
| 1454 | @okstat = (0, 2..$#fh); | ||||
| 1455 | } elsif ($^O eq 'VMS') { # device and file ID are sufficient | ||||
| 1456 | @okstat = (0, 1); | ||||
| 1457 | } elsif ($^O eq 'dos') { | ||||
| 1458 | @okstat = (0,2..7,11..$#fh); | ||||
| 1459 | } elsif ($^O eq 'mpeix') { | ||||
| 1460 | @okstat = (0..4,8..10); | ||||
| 1461 | } | ||||
| 1462 | |||||
| 1463 | # Now compare each entry explicitly by number | ||||
| 1464 | for (@okstat) { | ||||
| 1465 | print "Comparing: $_ : $fh[$_] and $path[$_]\n" if $DEBUG; | ||||
| 1466 | # Use eq rather than == since rdev, blksize, and blocks (6, 11, | ||||
| 1467 | # and 12) will be '' on platforms that do not support them. This | ||||
| 1468 | # is fine since we are only comparing integers. | ||||
| 1469 | unless ($fh[$_] eq $path[$_]) { | ||||
| 1470 | warn "Did not match $_ element of stat\n" if $DEBUG; | ||||
| 1471 | return 0; | ||||
| 1472 | } | ||||
| 1473 | } | ||||
| 1474 | |||||
| 1475 | return 1; | ||||
| 1476 | } | ||||
| 1477 | |||||
| 1478 | sub unlink1 { | ||||
| 1479 | croak 'Usage: unlink1(filehandle, filename)' | ||||
| 1480 | unless scalar(@_) == 2; | ||||
| 1481 | |||||
| 1482 | # Read args | ||||
| 1483 | my ($fh, $path) = @_; | ||||
| 1484 | |||||
| 1485 | cmpstat($fh, $path) or return 0; | ||||
| 1486 | |||||
| 1487 | # Close the file | ||||
| 1488 | close( $fh ) or return 0; | ||||
| 1489 | |||||
| 1490 | # Make sure the file is writable (for windows) | ||||
| 1491 | _force_writable( $path ); | ||||
| 1492 | |||||
| 1493 | # return early (without unlink) if we have been instructed to retain files. | ||||
| 1494 | return 1 if $KEEP_ALL; | ||||
| 1495 | |||||
| 1496 | # remove the file | ||||
| 1497 | return unlink($path); | ||||
| 1498 | } | ||||
| 1499 | |||||
| 1500 | { | ||||
| 1501 | # protect from using the variable itself | ||||
| 1502 | 1 | 200ns | my $LEVEL = STANDARD; | ||
| 1503 | # spent 16.7ms within File::Temp::safe_level which was called 9170 times, avg 2µs/call:
# 9170 times (16.7ms+0s) by File::Temp::_gettemp at line 335, avg 2µs/call | ||||
| 1504 | 9170 | 2.92ms | my $self = shift; | ||
| 1505 | 9170 | 2.60ms | if (@_) { | ||
| 1506 | my $level = shift; | ||||
| 1507 | if (($level != STANDARD) && ($level != MEDIUM) && ($level != HIGH)) { | ||||
| 1508 | carp "safe_level: Specified level ($level) not STANDARD, MEDIUM or HIGH - ignoring\n" if $^W; | ||||
| 1509 | } else { | ||||
| 1510 | # Don't allow this on perl 5.005 or earlier | ||||
| 1511 | if ($] < 5.006 && $level != STANDARD) { | ||||
| 1512 | # Cant do MEDIUM or HIGH checks | ||||
| 1513 | croak "Currently requires perl 5.006 or newer to do the safe checks"; | ||||
| 1514 | } | ||||
| 1515 | # Check that we are allowed to change level | ||||
| 1516 | # Silently ignore if we can not. | ||||
| 1517 | $LEVEL = $level if _can_do_level($level); | ||||
| 1518 | } | ||||
| 1519 | } | ||||
| 1520 | 9170 | 46.1ms | return $LEVEL; | ||
| 1521 | } | ||||
| 1522 | } | ||||
| 1523 | |||||
| 1524 | { | ||||
| 1525 | 2 | 600ns | my $TopSystemUID = 10; | ||
| 1526 | 1 | 500ns | $TopSystemUID = 197108 if $^O eq 'interix'; # "Administrator" | ||
| 1527 | sub top_system_uid { | ||||
| 1528 | my $self = shift; | ||||
| 1529 | if (@_) { | ||||
| 1530 | my $newuid = shift; | ||||
| 1531 | croak "top_system_uid: UIDs should be numeric" | ||||
| 1532 | unless $newuid =~ /^\d+$/s; | ||||
| 1533 | $TopSystemUID = $newuid; | ||||
| 1534 | } | ||||
| 1535 | return $TopSystemUID; | ||||
| 1536 | } | ||||
| 1537 | } | ||||
| 1538 | |||||
| 1539 | 1 | 200ns | package File::Temp::Dir; | ||
| 1540 | |||||
| 1541 | 2 | 34µs | 2 | 144µs | # spent 79µs (14+65) within File::Temp::Dir::BEGIN@1541 which was called:
# once (14µs+65µs) by IO::CaptureOutput::_proxy::BEGIN@138 at line 1541 # spent 79µs making 1 call to File::Temp::Dir::BEGIN@1541
# spent 65µs making 1 call to Exporter::import |
| 1542 | 2 | 37µs | 2 | 19µs | # spent 14µs (9+5) within File::Temp::Dir::BEGIN@1542 which was called:
# once (9µs+5µs) by IO::CaptureOutput::_proxy::BEGIN@138 at line 1542 # spent 14µs making 1 call to File::Temp::Dir::BEGIN@1542
# spent 5µs making 1 call to strict::import |
| 1543 | 1 | 7µs | 1 | 44µs | # spent 54µs (10+44) within File::Temp::Dir::BEGIN@1543 which was called:
# once (10µs+44µs) by IO::CaptureOutput::_proxy::BEGIN@138 at line 1545 # spent 44µs making 1 call to overload::import |
| 1544 | '0+' => \&File::Temp::NUMIFY, | ||||
| 1545 | 1 | 352µs | 1 | 54µs | fallback => 1; # spent 54µs making 1 call to File::Temp::Dir::BEGIN@1543 |
| 1546 | |||||
| 1547 | # private class specifically to support tempdir objects | ||||
| 1548 | # created by File::Temp->newdir | ||||
| 1549 | |||||
| 1550 | # ostensibly the same method interface as File::Temp but without | ||||
| 1551 | # inheriting all the IO::Seekable methods and other cruft | ||||
| 1552 | |||||
| 1553 | # Read-only - returns the name of the temp directory | ||||
| 1554 | |||||
| 1555 | # spent 205µs within File::Temp::Dir::dirname which was called 27 times, avg 8µs/call:
# 25 times (178µs+0s) by RBM::DefaultConfig::rbm_tmp_dir at line 108 of /root/tor-browser-build/rbm/lib/RBM/DefaultConfig.pm, avg 7µs/call
# once (23µs+0s) by RBM::build_run at line 1014 of /root/tor-browser-build/rbm/lib/RBM.pm
# once (3µs+0s) by RBM::DefaultConfig::rbm_tmp_dir at line 113 of /root/tor-browser-build/rbm/lib/RBM/DefaultConfig.pm | ||||
| 1556 | 27 | 39µs | my $self = shift; | ||
| 1557 | 27 | 206µs | return $self->{DIRNAME}; | ||
| 1558 | } | ||||
| 1559 | |||||
| 1560 | sub STRINGIFY { | ||||
| 1561 | my $self = shift; | ||||
| 1562 | return $self->dirname; | ||||
| 1563 | } | ||||
| 1564 | |||||
| 1565 | # spent 7µs within File::Temp::Dir::unlink_on_destroy which was called:
# once (7µs+0s) by File::Temp::Dir::DESTROY at line 1576 | ||||
| 1566 | 1 | 900ns | my $self = shift; | ||
| 1567 | 1 | 1µs | if (@_) { | ||
| 1568 | $self->{CLEANUP} = shift; | ||||
| 1569 | } | ||||
| 1570 | 1 | 20µs | return $self->{CLEANUP}; | ||
| 1571 | } | ||||
| 1572 | |||||
| 1573 | # spent 105ms (133µs+104) within File::Temp::Dir::DESTROY which was called:
# once (133µs+104ms) by RBM::build_run at line 1163 of /root/tor-browser-build/rbm/lib/RBM.pm | ||||
| 1574 | 1 | 1µs | my $self = shift; | ||
| 1575 | 1 | 28µs | local($., $@, $!, $^E, $?); | ||
| 1576 | 1 | 43µs | 1 | 7µs | if ($self->unlink_on_destroy && # spent 7µs making 1 call to File::Temp::Dir::unlink_on_destroy |
| 1577 | $$ == $self->{LAUNCHPID} && !$File::Temp::KEEP_ALL) { | ||||
| 1578 | 1 | 63µs | 1 | 13µs | if (-d $self->{REALNAME}) { # spent 13µs making 1 call to File::Temp::Dir::CORE:ftdir |
| 1579 | # Some versions of rmtree will abort if you attempt to remove | ||||
| 1580 | # the directory you are sitting in. We protect that and turn it | ||||
| 1581 | # into a warning. We do this because this occurs during object | ||||
| 1582 | # destruction and so can not be caught by the user. | ||||
| 1583 | 2 | 6µs | 1 | 104ms | eval { rmtree($self->{REALNAME}, $File::Temp::DEBUG, 0); }; # spent 104ms making 1 call to File::Path::rmtree |
| 1584 | 1 | 600ns | warn $@ if ($@ && $^W); | ||
| 1585 | } | ||||
| 1586 | } | ||||
| 1587 | } | ||||
| 1588 | |||||
| 1589 | 1 | 26µs | 1; | ||
| 1590 | |||||
| 1591 | __END__ | ||||
sub File::Temp::CORE:chmod; # opcode | |||||
# spent 6.17ms within File::Temp::CORE:ftdir which was called 4585 times, avg 1µs/call:
# 4585 times (6.17ms+0s) by File::Temp::_gettemp at line 326, avg 1µs/call | |||||
# spent 29.1ms within File::Temp::CORE:ftis which was called 4585 times, avg 6µs/call:
# 4585 times (29.1ms+0s) by File::Temp::_gettemp at line 322, avg 6µs/call | |||||
# spent 7µs within File::Temp::CORE:match which was called 2 times, avg 3µs/call:
# 2 times (7µs+0s) by File::Temp::newdir at line 925, avg 3µs/call | |||||
# spent 232µs within File::Temp::CORE:mkdir which was called 2 times, avg 116µs/call:
# 2 times (232µs+0s) by File::Temp::_gettemp at line 402, avg 116µs/call | |||||
# spent 10.1ms within File::Temp::CORE:regcomp which was called 4585 times, avg 2µs/call:
# 4585 times (10.1ms+0s) by File::Temp::_replace_XX at line 496, avg 2µs/call | |||||
# spent 2µs within File::Temp::CORE:sort which was called:
# once (2µs+0s) by File::Temp::cleanup at line 741 | |||||
# spent 22.8ms within File::Temp::CORE:subst which was called 4585 times, avg 5µs/call:
# 4585 times (22.8ms+0s) by File::Temp::_replace_XX at line 496, avg 5µs/call | |||||
# spent 54.4ms within File::Temp::CORE:substcont which was called 50426 times, avg 1µs/call:
# 50426 times (54.4ms+0s) by File::Temp::_replace_XX at line 496, avg 1µs/call | |||||
# spent 222ms within File::Temp::CORE:sysopen which was called 4583 times, avg 48µs/call:
# 4583 times (222ms+0s) by File::Temp::_gettemp at line 377, avg 48µs/call | |||||
# spent 13µs within File::Temp::Dir::CORE:ftdir which was called:
# once (13µs+0s) by File::Temp::Dir::DESTROY at line 1578 | |||||
# spent 600ns within File::Temp::__ANON__ which was called:
# once (600ns+0s) by File::Temp::BEGIN@11 at line 11 |