| 1 | package PAR::SetupTemp; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | $PAR::SetupTemp::VERSION = '1.002'; | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Fcntl ':mode'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use PAR::SetupProgname; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | PAR::SetupTemp - Setup $ENV{PAR_TEMP} | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | PAR guts, beware. Check L<PAR> | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | Routines to setup the C<PAR_TEMP> environment variable. | ||||||||||||||||||||||||||||||||||||||||||
| 23 | The documentation of how the temporary directories are handled | ||||||||||||||||||||||||||||||||||||||||||
| 24 | is currently scattered across the C<PAR> manual and the | ||||||||||||||||||||||||||||||||||||||||||
| 25 | C<PAR::Environment> manual. | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | The C<set_par_temp_env()> subroutine sets up the C<PAR_TEMP> | ||||||||||||||||||||||||||||||||||||||||||
| 28 | environment variable. | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | # for PAR internal use only! | ||||||||||||||||||||||||||||||||||||||||||
| 33 | our $PARTemp; | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | # name of the canary file | ||||||||||||||||||||||||||||||||||||||||||
| 36 | our $Canary = "_CANARY_.txt"; | ||||||||||||||||||||||||||||||||||||||||||
| 37 | # how much to "date back" the canary file (in seconds) | ||||||||||||||||||||||||||||||||||||||||||
| 38 | our $CanaryDateBack = 24 * 3600; # 1 day | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | # The C version of this code appears in myldr/mktmpdir.c | ||||||||||||||||||||||||||||||||||||||||||
| 41 | # This code also lives in PAR::Packer's par.pl as _set_par_temp! | ||||||||||||||||||||||||||||||||||||||||||
| 42 | sub set_par_temp_env { | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 7 | ▶ | PAR::SetupProgname::set_progname() | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_par_temp_env: called CC=12 SCAR=26.4
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | unless defined $PAR::SetupProgname::Progname; | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 7 | ▶ | if (defined $ENV{PAR_TEMP} and $ENV{PAR_TEMP} =~ /(.+)/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $ENV{"PAR_TEMP"} and $ENV{"PAR_TEMP"} =~ /(.+)/
MC/DC50%1/2
defined $ENV{"PAR_TEMP"} and $ENV{"PAR_TEMP"} =~ /(.+)/
defined $ENV{"PAR_TEMP"}
$ENV{"PAR_TEMP"} =~ /(.+)/
Truth table66%2/3
A and B
A= defined $ENV{"PAR_TEMP"}B= $ENV{"PAR_TEMP"} =~ /(.+)/
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 3 | $PARTemp = $1; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 4 | my $stmpdir = _get_par_user_tempdir(); | |||||||||||||||||||||||||||||||||||||||||
| 52 | 4 | ▶ | die "unable to create cache directory" unless $stmpdir; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 4 | require File::Spec; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 4 | ▶ | if (!$ENV{PAR_CLEAN} and my $mtime = (stat($PAR::SetupProgname::Progname))[9]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $ENV{"PAR_CLEAN"} and my $mtime = (stat $PAR::SetupProgname::Progname)[9]
MC/DC50%1/2
not $ENV{"PAR_CLEAN"} and my $mtime = (stat $PAR::SetupProgname::Progname)[9]
not $ENV{"PAR_CLEAN"}
my $mtime = (stat $PAR::SetupProgname::Progname)[9]
Truth table66%2/3
not(A) and B
A= not $ENV{"PAR_CLEAN"}B= my $mtime = (stat $PAR::SetupProgname::Progname)[9]
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 2 | require Digest::SHA; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 2 | my $ctx = Digest::SHA->new(1); | |||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 2 | ▶ | if ($ctx and open(my $fh, "<$PAR::SetupProgname::Progname")) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ctx and open my $fh, "<$PAR::SetupProgname::Progname"
MC/DC0%0/2
$ctx and open my $fh, "<$PAR::SetupProgname::Progname"
$ctx
open my $fh, "<$PAR::SetupProgname::Progname"
Truth table33%1/3
A and B
A= $ctxB= open my $fh, "<$PAR::SetupProgname::Progname"
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | binmode($fh); | |||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | $ctx->addfile($fh); | |||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | close($fh); | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | ▶ | $stmpdir = File::Spec->catdir( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | $stmpdir, | ||||||||||||||||||||||||||||||||||||||||||
| 67 | "cache-" . ( $ctx ? $ctx->hexdigest : $mtime ) | ||||||||||||||||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 2 | $ENV{PAR_CLEAN} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 2 | $stmpdir = File::Spec->catdir($stmpdir, "temp-$$"); | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 4 | $ENV{PAR_TEMP} = $stmpdir; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 4 | mkdir $stmpdir, 0700; | |||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 4 | ▶ | $PARTemp = $1 if defined $ENV{PAR_TEMP} and $ENV{PAR_TEMP} =~ /(.+)/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $ENV{"PAR_TEMP"} and $ENV{"PAR_TEMP"} =~ /(.+)/
MC/DC0%0/2
defined $ENV{"PAR_TEMP"} and $ENV{"PAR_TEMP"} =~ /(.+)/
defined $ENV{"PAR_TEMP"}
$ENV{"PAR_TEMP"} =~ /(.+)/
Truth table33%1/3
A and B
A= defined $ENV{"PAR_TEMP"}B= $ENV{"PAR_TEMP"} =~ /(.+)/
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | # Find any digester | ||||||||||||||||||||||||||||||||||||||||||
| 82 | # Used in PAR::Repository::Client! | ||||||||||||||||||||||||||||||||||||||||||
| 83 | sub _get_digester { | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my $ctx = eval { require Digest::SHA; Digest::SHA->new(1) } | |||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | || eval { require Digest::SHA1; Digest::SHA1->new } | |||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | || eval { require Digest::MD5; Digest::MD5->new }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_digester: not called CC=3 SCAR=24.8
Condition0%0/6
eval {
do {
require Digest::SHA;
'Digest::SHA'->new(1)
}
} || eval {
do {
require Digest::SHA1;
'Digest::SHA1'->new
}
} || eval {
do {
require Digest::MD5;
'Digest::MD5'->new
}
}
eval {
do {
require Digest::SHA;
"Digest::SHA"->new(1)
}
} or eval {
do {
require Digest::SHA1;
"Digest::SHA1"->new
}
}
MC/DC0%0/3
eval {
do {
require Digest::SHA;
'Digest::SHA'->new(1)
}
} || eval {
do {
require Digest::SHA1;
'Digest::SHA1'->new
}
} || eval {
do {
require Digest::MD5;
'Digest::MD5'->new
}
}
eval {
do {
require Digest::SHA;
"Digest::SHA"->new(1)
}
}
eval {
do {
require Digest::SHA1;
"Digest::SHA1"->new
}
}
eval {
do {
require Digest::MD5;
'Digest::MD5'->new
}
}
Truth table0%0/4
A or B || C
A= eval {
do {
require Digest::SHA;
"Digest::SHA"->new(1)
}
}B= eval {
do {
require Digest::SHA1;
"Digest::SHA1"->new
}
}C= eval {
do {
require Digest::MD5;
'Digest::MD5'->new
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | return $ctx; | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | # find the per-user temporary directory (eg /tmp/par-$USER) | ||||||||||||||||||||||||||||||||||||||||||
| 91 | # Used in PAR::Repository::Client! | ||||||||||||||||||||||||||||||||||||||||||
| 92 | sub _get_par_user_tempdir { | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 4 | ▶ | my $username = _find_username(); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_par_user_tempdir: called CC=12 SCAR=32.4
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 4 | my $temp_path; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 4 | foreach my $path ( | |||||||||||||||||||||||||||||||||||||||||
| 96 | (map $ENV{$_}, qw( PAR_TMPDIR TMPDIR TEMPDIR TEMP TMP )), | ||||||||||||||||||||||||||||||||||||||||||
| 97 | qw( C:\\TEMP /tmp . ) | ||||||||||||||||||||||||||||||||||||||||||
| 98 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 4 | ▶ | next unless defined $path and -d $path and -w $path; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $path and -d $path and -w $path
defined $path and -d $path
MC/DC0%0/3
defined $path and -d $path and -w $path
defined $path
-d $path
-w $path
Truth table25%1/4
A and B and C
A= defined $pathB= -d $pathC= -w $path
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | # create a temp directory that is unique per user | ||||||||||||||||||||||||||||||||||||||||||
| 101 | # NOTE: $username may be in an unspecified charset/encoding; | ||||||||||||||||||||||||||||||||||||||||||
| 102 | # use a name that hopefully works for all of them; | ||||||||||||||||||||||||||||||||||||||||||
| 103 | # also avoid problems with platform-specific meta characters in the name | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 4 | $temp_path = File::Spec->catdir($path, "par-".unpack("H*", $username)); | |||||||||||||||||||||||||||||||||||||||||
| 105 | 4 | ($temp_path) = $temp_path =~ /^(.*)$/s; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 4 | ▶ | unless (mkdir($temp_path, 0700) || $!{EEXIST}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
mkdir $temp_path, 448 or $!{"EEXIST"}
MC/DC0%0/2
mkdir $temp_path, 448 or $!{"EEXIST"}
mkdir $temp_path, 448
$!{"EEXIST"}
Truth table66%2/3
A or B
A= mkdir $temp_path, 448B= $!{"EEXIST"}
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | warn "creation of private subdirectory $temp_path failed (errno=$!)"; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 4 | ▶ | unless ($^O eq 'MSWin32') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 4 | my @st; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 4 | ▶ | unless (@st = lstat($temp_path)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | warn "stat of private subdirectory $temp_path failed (errno=$!)"; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 4 | ▶ | if (!S_ISDIR($st[2]) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
not S_ISDIR($st[2]) or $st[4] != $< or ($st[2] & 511) != 448
not S_ISDIR($st[2]) or $st[4] != $<
MC/DC0%0/3
not S_ISDIR($st[2]) or $st[4] != $< or ($st[2] & 511) != 448
not S_ISDIR($st[2])
$st[4] != $<
($st[2] & 511) != 448
Truth table25%1/4
not(A) or B or C
A= not S_ISDIR($st[2])B= $st[4] != $<C= ($st[2] & 511) != 448
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | || $st[4] != $< | ||||||||||||||||||||||||||||||||||||||||||
| 119 | || ($st[2] & 0777) != 0700 ) { | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | warn "private subdirectory $temp_path is unsafe (please remove it and retry your operation)"; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 4 | last; | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 4 | return $temp_path; | |||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | # tries hard to find out the name of the current user | ||||||||||||||||||||||||||||||||||||||||||
| 131 | sub _find_username { | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 4 | ▶ | my $username; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_username: called CC=6 SCAR=24.2
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | my $pwuid; | ||||||||||||||||||||||||||||||||||||||||||
| 134 | # does not work everywhere: | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 4 | ▶ | eval {($pwuid) = getpwuid($>) if defined $>;}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 4 | ▶ | if ( defined(&Win32::LoginName) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | $username = &Win32::LoginName; | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | elsif (defined $pwuid) { | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 4 | $username = $pwuid; | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | $username = $ENV{USERNAME} || $ENV{USER} || 'SYSTEM'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/5
$ENV{'USERNAME'} || $ENV{'USER'} || 'SYSTEM'
$ENV{"USERNAME"} or $ENV{"USER"}
MC/DC0%0/2
$ENV{'USERNAME'} || $ENV{'USER'} || 'SYSTEM'
$ENV{"USERNAME"}
$ENV{"USER"}
Truth table0%0/3
A or B
A= $ENV{"USERNAME"}B= $ENV{"USER"}
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | 4 | return $username; | |||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | 1; | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | __END__ | ||||||||||||||||||||||||||||||||||||||||||