File Coverage

blib/lib/PAR/SetupTemp.pm
Criterion Covered Total %
statement 53 63 84.1
branch 17 30 56.6
condition 12 32 37.5
subroutine 8 9 88.8
pod 0 1 0.0
total 90 135 66.6


line stmt bran cond sub pod time code
1             package PAR::SetupTemp;
2             $PAR::SetupTemp::VERSION = '1.002';
3              
4 4     4   107 use 5.006;
  4         36  
5 4     4   21 use strict;
  4         8  
  4         116  
6 4     4   21 use warnings;
  4         6  
  4         160  
7              
8 4     4   25 use Fcntl ':mode';
  4         7  
  4         1416  
9              
10 4     4   30 use PAR::SetupProgname;
  4         20  
  4         3414  
11              
12             =head1 NAME
13              
14             PAR::SetupTemp - Setup $ENV{PAR_TEMP}
15              
16             =head1 SYNOPSIS
17              
18             PAR guts, beware. Check L
19              
20             =head1 DESCRIPTION
21              
22             Routines to setup the C environment variable.
23             The documentation of how the temporary directories are handled
24             is currently scattered across the C manual and the
25             C manual.
26              
27             The C subroutine sets up the C
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 50   7 0 24 PAR::SetupProgname::set_progname()
44             unless defined $PAR::SetupProgname::Progname;
45              
46 7 100 66     48 if (defined $ENV{PAR_TEMP} and $ENV{PAR_TEMP} =~ /(.+)/) {
47 3         11 $PARTemp = $1;
48 3         5 return;
49             }
50              
51 4         17 my $stmpdir = _get_par_user_tempdir();
52 4 50       17 die "unable to create cache directory" unless $stmpdir;
53              
54 4         26 require File::Spec;
55 4 100 66     55 if (!$ENV{PAR_CLEAN} and my $mtime = (stat($PAR::SetupProgname::Progname))[9]) {
56 2         1205 require Digest::SHA;
57 2         5349 my $ctx = Digest::SHA->new(1);
58              
59 2 50 33     143 if ($ctx and open(my $fh, "<$PAR::SetupProgname::Progname")) {
60 2         8 binmode($fh);
61 2         11 $ctx->addfile($fh);
62 2         127 close($fh);
63             }
64              
65 2 50       53 $stmpdir = File::Spec->catdir(
66             $stmpdir,
67             "cache-" . ( $ctx ? $ctx->hexdigest : $mtime )
68             );
69             }
70             else {
71 2         20 $ENV{PAR_CLEAN} = 1;
72 2         25 $stmpdir = File::Spec->catdir($stmpdir, "temp-$$");
73             }
74              
75 4         36 $ENV{PAR_TEMP} = $stmpdir;
76 4         283 mkdir $stmpdir, 0700;
77              
78 4 50 33     80 $PARTemp = $1 if defined $ENV{PAR_TEMP} and $ENV{PAR_TEMP} =~ /(.+)/;
79             }
80              
81             # Find any digester
82             # Used in PAR::Repository::Client!
83             sub _get_digester {
84             my $ctx = eval { require Digest::SHA; Digest::SHA->new(1) }
85             || eval { require Digest::SHA1; Digest::SHA1->new }
86 0   0 0   0 || eval { require Digest::MD5; Digest::MD5->new };
87 0         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     4   16 my $username = _find_username();
94 4         11 my $temp_path;
95 4         52 foreach my $path (
96             (map $ENV{$_}, qw( PAR_TMPDIR TMPDIR TEMPDIR TEMP TMP )),
97             qw( C:\\TEMP /tmp . )
98             ) {
99 4 50 33     146 next unless defined $path and -d $path and -w $path;
      33        
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         97 $temp_path = File::Spec->catdir($path, "par-".unpack("H*", $username));
105 4         39 ($temp_path) = $temp_path =~ /^(.*)$/s;
106 4 50 66     396 unless (mkdir($temp_path, 0700) || $!{EEXIST}) {
107 0         0 warn "creation of private subdirectory $temp_path failed (errno=$!)";
108 0         0 return;
109             }
110              
111 4 50       66 unless ($^O eq 'MSWin32') {
112 4         11 my @st;
113 4 50       86 unless (@st = lstat($temp_path)) {
114 0         0 warn "stat of private subdirectory $temp_path failed (errno=$!)";
115 0         0 return;
116             }
117 4 50 33     96 if (!S_ISDIR($st[2])
      33        
118             || $st[4] != $<
119             || ($st[2] & 0777) != 0700 ) {
120 0         0 warn "private subdirectory $temp_path is unsafe (please remove it and retry your operation)";
121 0         0 return;
122             }
123             }
124              
125 4         14 last;
126             }
127 4         16 return $temp_path;
128             }
129              
130             # tries hard to find out the name of the current user
131             sub _find_username {
132 4     4   10 my $username;
133             my $pwuid;
134             # does not work everywhere:
135 4 50       8 eval {($pwuid) = getpwuid($>) if defined $>;};
  4         3424  
136              
137 4 50       38 if ( defined(&Win32::LoginName) ) {
    50          
138 0         0 $username = &Win32::LoginName;
139             }
140             elsif (defined $pwuid) {
141 4         13 $username = $pwuid;
142             }
143             else {
144 0   0     0 $username = $ENV{USERNAME} || $ENV{USER} || 'SYSTEM';
145             }
146              
147 4         13 return $username;
148             }
149              
150             1;
151              
152             __END__