lib/File/Value.pm

filter: Statementstmt 89.2% 141 / 158 Branchbran 65.5% 80 / 122 Conditioncond 72.1% 44 / 61 MC/DCmcdc 46.8% 15 / 32 Subroutinesub 88.8% 16 / 18 Podpod 60.0% 6 / 10 totaltotal 75.3% 302 / 401 CC 83 SCAR 30.4
CC
83
Cov
78%
file_value
32.6
snag_version
32.2
flvl
30.0
CRAP 20.9
↑ Summary script/snag »
1 # To do: change default behavior and add options:
2 # snag foo -> defaults to:
3 # --mknextcopy for file if it exists, else snag_file
4 # --mknext for dir if it exists, else snag_dir
5 # add options: --noversion, --dir (default is 'file'), --nocopy
6 # Add list of regexs and substitutions for versions, esp. so that
7 # after foo.txt comes foo_1.txt instead of foo.txt_1; order is preference:
8 # qr/[-_]?(\d+)(\.[a-z]*)?$/, etc.
9 # also check for case of foo_\d{4,6} which is a date be prepared to use
10 # today's date for next version
11 #
12 package File::Value;
13  
14 20 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 20 our $VERSION;
19 20 $VERSION = sprintf "%d.%02d", q$Name: Release-1-02 $ =~ /Release-(\d+)-(\d+)/;
20  
21 20 require Exporter;
22 20 our @ISA = qw(Exporter);
23  
24 20 our @EXPORT = qw();
25 20 our @EXPORT_OK = qw(
26 file_value
27 flvl
28 list_high_version
29 list_low_version
30 snag_dir
31 snag_file
32 snag_version
33 fiso_dname
34 fiso_uname
35 );
36 20 our %EXPORT_TAGS = (all => [ @EXPORT_OK ]);
37  
38 # if length is 0, go for it.
39 #
40 20 my $ridiculous = 4294967296; # max length is 2**32 yyy a better way?
41  
42 # returns empty string on success, else error message
43 19 ▶sub file_value { my( $file, $value, $how, $length )=@_;
Subroutine file_value: called CC=30 SCAR=34.9
Pod documented
44  
45 19 my $ret_value = \$_[1];
46  
47 19 ▶ $file or return "must specify a file name";
Branch: 1 branch
BranchTrueFalseTotal
$file or return "must specify a file name" 19 0 19
48  
49 # Force caller to be explicit about whether doing read/write/append.
50 #
51 19 ▶ $file !~ /^\s*(<|>|>>)\s*(\S.*)/ and
Branch: 1 branch
BranchTrueFalseTotal
$file =~ /^\s*(<|>|>>)\s*(\S.*)/ or return "file ($file) must begin with '<', '>', or '>>'" 18 1 19
52 return "file ($file) must begin with '<', '>', or '>>'";
53 18 my ($mode, $statfname) = ($1, $2);
54  
55 18 ▶ $how ||= "trim"; # trim (def), raw, untaint
Condition100%2/2
$how ||= "trim"
l!l
108
MC/DC100%1/1
$how ||= "trim"
$how
Truth table100%2/2
A
A= $how
Aresult
11
00
56 18 $how = lc($how);
57 18 ▶ $how ne "trim" && $how ne "raw" && $how ne "untaint" and
Branch: 1 branch
BranchTrueFalseTotal
$how ne "trim" and $how ne "raw" and $how ne "untaint" and return "third arg ($how) must be one of: trim, raw, or untaint" 1 17 18
Condition100%6/6
$how ne "trim" and $how ne "raw" and $how ne "untaint"
!ll&&!rl&&r
1611
$how ne "trim" and $how ne "raw"
!ll&&!rl&&r
1242
MC/DC100%3/3
$how ne "trim" and $how ne "raw" and $how ne "untaint"
$how ne "trim" $how ne "raw" $how ne "untaint"
Truth table100%4/4
A and B and C
A= $how ne "trim"B= $how ne "raw"C= $how ne "untaint"
ABCresult
0XX0
10X0
1100
1111
58 return "third arg ($how) must be one of: trim, raw, or untaint";
59  
60 17 ▶ if ($mode =~ />>?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode =~ />>?/) 5 12 17
61 # If we get here, we're doing value-to-file case (> or >>).
62  
63 # ignore $how and $length, but we need specified $value
64 5 ▶ defined($value) or
Branch: 1 branch
BranchTrueFalseTotal
defined $value or return "needs a value to put in '${file}'" 5 0 5
65 return "needs a value to put in '$file'";
66 # for $how, here we only understand "trim" or "raw", but
67 # "trim" removes initial and final \n, adding one final \n
68 5 ▶ if ($how eq "trim") {
Branch: 1 branch
BranchTrueFalseTotal
if ($how eq "trim") 4 1 5
69 4 $value =~ s/^\s+//s;
70 4 $value =~ s/\s+$/\n/s;
71 }
72 5 ▶ open(OUT, $file) or
Branch: 1 branch
BranchTrueFalseTotal
open OUT, $file or return "${statfname}: $!" 5 0 5
73 return "$statfname: $!";
74 5 my $r = print OUT $value;
75 5 close(OUT);
76 5 ▶ return ($r ? "" : "write failed: $!");
Branch: 1 branch
BranchTrueFalseTotal
$r ? : 5 0 5
77 }
78  
79 # If we get here, we're doing file-to-value case.
80  
81 12 ▶ my $go_for_it = (defined($length) && $length eq "0" ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $length && $length eq '0' ? : 1 11 12
Condition100%3/3
defined $length and $length eq 0
!ll&&!rl&&r
921
MC/DC100%2/2
defined $length and $length eq 0
defined $length $length eq 0
Truth table100%3/3
A and B
A= defined $lengthB= $length eq 0
ABresult
0X0
100
111
82 12 my $statlength = undef;
83  
84 12 ▶ if (defined($length)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $length) { } 3 9 12
elsif ($statfname ne '-') { } 9 0 9
85 3 ▶ $length !~ /^\d+$/ and
Branch: 1 branch
BranchTrueFalseTotal
$length =~ /^\d+$/ or return "length unspecified or not an integer" 3 0 3
86 return "length unspecified or not an integer";
87 }
88 elsif ($statfname ne "-") {
89 # yyy tested with "-" (stdin or stdout)?
90 # no length means read whole file, but be reasonable
91 9 $statlength = (-s $statfname);
92 9 ▶ ! defined($statlength) and
Branch: 1 branch
BranchTrueFalseTotal
defined $statlength or return "${statfname}: $!" 9 0 9
93 return "$statfname: $!";
94 9 ▶ $length = ($statlength > $ridiculous
Branch: 1 branch
BranchTrueFalseTotal
$statlength > $ridiculous ? : 0 9 9
95 ? $ridiculous : $statlength);
96 }
97 else {
98 0 $length = $ridiculous;
99 }
100  
101 12 ▶ open(IN, $file) or
Branch: 1 branch
BranchTrueFalseTotal
open IN, $file or return "${statfname}: $!" 12 0 12
102 return "$statfname: $!";
103 12 ▶ if ($go_for_it) { # don't be reasonable about length
Branch: 1 branch
BranchTrueFalseTotal
if ($go_for_it) { } 1 11 12
104 1 local $/;
105 1 $$ret_value = <IN>;
106 1 close(IN);
107 }
108 else {
109 11 my ($offset, $n) = (0, 0);
110 # Ask for the whole $length but be prepared not to get it.
111 11 ▶ 1 while ($n = read(IN, $$ret_value, $length, $offset))
Branch: 1 branch
BranchTrueFalseTotal
$n = read(IN, $$ret_value, $length, $offset) and $n > 0 and ($offset += $n) < $length and do { '???' } 1 11 12
Condition83%5/6
$n = read(IN, $$ret_value, $length, $offset) and $n > 0 and ($offset += $n) < $length
!ll&&!rl&&r
291
$n = read(IN, $$ret_value, $length, $offset) and $n > 0
!ll&&!rl&&r
2010
MC/DC66%2/3
$n = read(IN, $$ret_value, $length, $offset) and $n > 0 and ($offset += $n) < $length
$n = read(IN, $$ret_value, $length, $offset) $n > 0 ($offset += $n) < $length
Truth table75%3/4
A and B and C
A= $n = read(IN, $$ret_value, $length, $offset)B= $n > 0C= ($offset += $n) < $length
ABCresult
0XX0
10X0
1100
1111
112 # yyy and print("offset=$offset\n")
113 and $n > 0 and ($offset += $n) < $length;
114 11 close(IN);
115 11 ▶ ! defined($n) and
Branch: 1 branch
BranchTrueFalseTotal
defined $n or return "${statfname}: failed to read $length bytes: $!" 11 0 11
116 return "$statfname: failed to read $length bytes: $!";
117 11 ▶ return "$statfname: read fewer bytes than expected"
Branch: 1 branch
BranchTrueFalseTotal
if defined $statlength and $offset < $statlength 0 11 11
Condition66%2/3
defined $statlength and $offset < $statlength
!ll&&!rl&&r
290
MC/DC0%0/2
defined $statlength and $offset < $statlength
defined $statlength $offset < $statlength
Truth table66%2/3
A and B
A= defined $statlengthB= $offset < $statlength
ABresult
0X0
100
111
118 if (defined($statlength) && $offset < $statlength);
119 #if (defined($statlength) && $n < $statlength);
120 }
121  
122 12 ▶ if ($how eq "trim") {
Branch: 2 branches
BranchTrueFalseTotal
if ($how eq 'trim') { } 8 4 12
elsif ($how eq 'untaint') { } 1 3 4
123 8 $$ret_value =~ s/^\s+//;
124 8 $$ret_value =~ s/\s+$//;
125 }
126 elsif ($how eq "untaint") {
127 1 ▶ if ($$ret_value =~ /([-\@\w.]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$ret_value =~ /([-\@\w.]+)/) 1 0 1
128 1 $$ret_value = $1;
129 }
130 }
131 # elsif ($how eq "raw") { then no further processing }
132  
133 12 return "";
134 }
135  
136 # Faster simpler cruder version of file_value(), just in case.
137 #
138 0 ▶sub flvl { my( $file, $value )=@_; # $file must begin with >, <, or >>
Subroutine flvl: not called CC=5 SCAR=34.0
Pod undocumented
139  
140 0 ▶ if ($file =~ /^\s*>>?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /^\s*>>?/) 0 0 0
141 0 ▶ open(OUT, $file) or return "$file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open OUT, $file or return "${file}: $!" 0 0 0
142 0 my $r = print OUT $value;
143 0 ▶ close(OUT); return ($r ? '' : "write failed: $!");
Branch: 1 branch
BranchTrueFalseTotal
$r ? : 0 0 0
144 }
145  
146 # If we get here, we're doing file-to-value case.
147 0 ▶ open(IN, $file) or return "$file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open IN, $file or return "${file}: $!" 0 0 0
148 0 local $/; $_[1] = <IN>; # slurp mode (entire file)
149 0 close(IN); return '';
150 }
151  
152 20 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
153  
154 20 our $Win; # whether we're running on Windows
155 20 ▶defined($Win) or # if we're on a Windows platform avoid -l
Branch: 1 branch
BranchTrueFalseTotal
defined $File::Value::Win or $File::Value::Win = grep(/Win32|OS2/i, @File::Spec::ISA) 0 20 20
156 $Win = grep(/Win32|OS2/i, @File::Spec::ISA);
157 20 our ($B, $C); # short names for boundary char for output (B) and regexes (C)
158 #our $B = $Win ? '\\\\' : '/'; # short name for boundary (\) in regexes
159 20 ▶$Win and
Branch: 1 branch
BranchTrueFalseTotal
$File::Value::Win and $File::Value::B = "\\", $File::Value::C = $File::Value::B . $File::Value::B, 1 or $File::Value::B = $File::Value::C = "/" 0 20 20
Condition33%1/3
$File::Value::Win and $File::Value::B = "\\", $File::Value::C = $File::Value::B . $File::Value::B, 1
!ll&&!rl&&r
2000
MC/DC0%0/2
$File::Value::Win and $File::Value::B = "\\", $File::Value::C = $File::Value::B . $File::Value::B, 1
$File::Value::Win $File::Value::B = "\\", $File::Value::C = $File::Value::B . $File::Value::B, 1
Truth table33%1/3
A and B
A= $File::Value::WinB= $File::Value::B = "\\", $File::Value::C = $File::Value::B . $File::Value::B, 1
ABresult
0X0
100
111
160 $B = '\\',
161 $C = $B.$B,
162 1
163 or
164 $B = $C = '/';
165 ;
166 #$B = '\\';
167 #$C = $B.$B; #
168  
169 # Return the full normalized name of a filesystem-based object that can
170 # legitimately be specified either by its enclosing directory name or
171 # by the directory name plus something in that directory. Useful when
172 # a user could correctly say 'myobj' or 'myobj/minter.bdb' but we will
173 # eventually need the longer form to test existence or create the object.
174 #
175 # It's important (eg, find_minder) that existence of enclosing directory
176 # not be mistaken for existence of the object, since creating that dir
177 # by itself (eg, with 'snag') could be a good way of reserving the minder
178 # that you are _about_ to create.
179 #
180 # Need this when the user will be specifying the location of an object
181 # that's defined by both an enclosing directory $base and a well-known
182 # file or directory $last under $base. It is valid for the user to say
183 # either $base/$last or $base, but an existence test against $base/$last
184 # is what we need to perform to verify whether it's a legitimate object,
185 # with some tedious special cases when $base is '/' or '.'. Typical use
186 # cases: user specifies "pairtree_root", "ptname", "ptname/pairtree_root",
187 # "mintername/", or "mintername/minter.bdb".
188 #
189 # Lengthen base path with last component according to the table, normalizing
190 # multiple slashes between $base and $last. Useful to get tedious details
191 # right and when path may already have the last component on it (in which
192 # case we don't want it there twice). Removes final slashes from $last.
193 #
194 # $base $last Returns
195 # 1. / bar /bar
196 # 2. . bar bar
197 # 3. foo bar foo/bar
198 # 4. foo/ bar foo/bar
199 # 5. foo/bar bar foo/bar
200 # 6. bar bar bar
201 # 7. "" bar bar
202 # 8. foo "" foo
203 #
204 # yyy might not port to Windows for the root directory cases
205 # yyy was called prep_file in pt script
206 # FISO = FIle System Object
207 # dname = "down" name (full name with descender, suitable for -X tests)
208 # uname = "up" name (upper name without descender, still unique, suitable
209 # for communicating with users)
210 14 ▶sub fiso_dname { my( $base, $last )=@_;
Subroutine fiso_dname: called CC=9 SCAR=22.0
Pod undocumented
211  
212 14 ▶ return (($base || "") . ($last || ""))
Branch: 1 branch
BranchTrueFalseTotal
unless $base and $last 2 12 14
Condition100%7/7
$base and $last
!ll&&!rl&&r
1112
$base || ''
l!l
11
$last || ''
l!l
11
MC/DC100%2/2
$base and $last
$base $last
MC/DC100%1/1
$base || ''
$base
MC/DC100%1/1
$last || ''
$last
Truth table100%3/3
A and B
A= $baseB= $last
ABresult
0X0
100
111
Truth table100%2/2
A
A= $base
Aresult
11
00
Truth table100%2/2
A
A= $last
Aresult
11
00
213 unless ($base and $last); # cases 7-8 done
214 12 $last =~ s{${C}*(.*)${C}*$}{$1}o; # remove bounding slashes
215 12 ▶ return "${B}$last" if $base =~ m{^${C}+$}o; # case 1 done
Branch: 1 branch
BranchTrueFalseTotal
if $base =~ /^$File::Value::C+$/o 3 9 12
216 9 $base =~ s{${C}+$}{}o; # remove trailing slashes
217 9 ▶ return "$last" if $base =~ m{^\.${C}*$}o; # case 2 done
Branch: 1 branch
BranchTrueFalseTotal
if $base =~ /^\.$File::Value::C*$/o 2 7 9
218 7 ▶ return "$base${B}$last" if $base !~ m{$last$}; # cases 3-4 gone
Branch: 1 branch
BranchTrueFalseTotal
unless $base =~ /$last$/ 3 4 7
219 4 ▶ return "$last" if $base =~ m{^$last$}; # case 6 eliminated
Branch: 1 branch
BranchTrueFalseTotal
if $base =~ /^$last$/ 1 3 4
220 3 $base =~ s{${C}*$last$}{}; # remove $last and preceding slashes
221 3 return "$base${B}$last"; # case 5 done
222 }
223  
224 # Return parent of $oname (object name) with trailing slash intact.
225 # xxx This needs to work closely with fiso_dname
226 #
227 8 ▶sub fiso_uname { my( $oname )=@_;
Subroutine fiso_uname: called CC=5 SCAR=16.1
Pod undocumented
228  
229 8 ▶ return "" unless $oname;
Branch: 1 branch
BranchTrueFalseTotal
unless $oname 2 6 8
230 6 ▶ return "${B}" if $oname =~ m,^${C}+$,o;
Branch: 1 branch
BranchTrueFalseTotal
if $oname =~ /^$File::Value::C+$/o 2 4 6
231 4 ▶ return ".${B}" if $oname =~ m,^\.${C}*$,o;
Branch: 1 branch
BranchTrueFalseTotal
if $oname =~ /^\.$File::Value::C*$/o 0 4 4
232 # next assumes $oname was a directory?
233 # final / means this fiso_dname was a dir? yyy
234 4 $oname =~ s,[^${C}]+${C}*$,,o;
235 4 ▶ return ".${B}" if $oname =~ m,^$,;
Branch: 1 branch
BranchTrueFalseTotal
if $oname =~ /^$/ 2 2 4
236 2 return $oname;
237 }
238  
239 20 ▶use File::Glob ':glob'; # standard use of module, which we need
Subroutine BEGIN: called CC=1 SCAR=0.0
240 # as vanilla glob won't match whitespace
241  
242 9 ▶sub list_high_version { my( $template )=@_;
Subroutine list_high_version: called CC=4 SCAR=14.1
Pod documented
243  
244 9 ▶ $template ||= "";
Condition50%1/2
$template ||= ""
l!l
90
MC/DC0%0/1
$template ||= ""
$template
Truth table50%1/2
A
A= $template
Aresult
11
00
245 9 my ($max, $n, $fname) = (-1, -1, undef);
246 9 for (grep( /$template\d+$/, bsd_glob("$template*") )) {
247 6 ($n) = m/(\d+)$/; # extract number for comparisons
248 6 ▶ $max < $n and
Branch: 1 branch
BranchTrueFalseTotal
$max < $n and ($max, $fname) = ($n, $_) 6 0 6
249 ($max, $fname) = ($n, $_);
250 }
251 9 return ($max, $fname);
252 }
253  
254 # Tempting for program maintenance to combine computation of low and high,
255 # but that would make computation of the high (by far the most common call)
256 # pay a performance penalty for a seldom used value. So we separate.
257 #
258 1 ▶sub list_low_version { my( $template )=@_;
Subroutine list_low_version: called CC=5 SCAR=16.2
Pod documented
259  
260 1 ▶ $template ||= "";
Condition50%1/2
$template ||= ""
l!l
10
MC/DC0%0/1
$template ||= ""
$template
Truth table50%1/2
A
A= $template
Aresult
11
00
261 1 my ($min, $n, $fname) = (-1, -1, undef);
262 1 for (grep( /$template\d+$/, bsd_glob("$template*") )) {
263 2 ($n) = m/(\d+)$/; # extract number for comparisons
264 2 ▶ $min > $n || $min < 0 and
Branch: 1 branch
BranchTrueFalseTotal
$min > $n || $min < 0 and ($min, $fname) = ($n, $_) 1 1 2
Condition66%2/3
$min > $n or $min < 0
l!l&&r!l&&!r
011
MC/DC50%1/2
$min > $n or $min < 0
$min > $n $min < 0
Truth table66%2/3
A or B
A= $min > $nB= $min < 0
ABresult
1X1
011
000
265 ($min, $fname) = ($n, $_);
266 }
267 1 return ($min, $fname);
268 }
269  
270 0 ▶sub snag_opt_defaults { return {
Subroutine snag_opt_defaults: not called CC=1 SCAR=6.9
Pod undocumented
271  
272 0 as_dir => undef, # create node as dir (default is to
273 # infer it from argument if possible)
274 no_type_mismatch => 0, # complain if mismatch (default don't)
275 mknextcopy => 0, # copy unnumbered file (default don't)
276 };
277 }
278  
279 10 ▶sub snag_version { my( $template, $opt ) = (shift, shift);
Subroutine snag_version: called CC=23 SCAR=34.7
Pod documented
280  
281 10 ▶ return (-1, "no base name given") if ! defined $template;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $template 0 10 10
282 10 ▶ $opt ||= snag_opt_defaults();
Condition33%1/3
$opt ||= snag_opt_defaults()
l!l&&r!l&&!r
1000
MC/DC0%0/2
$opt ||= snag_opt_defaults()
$opt snag_opt_defaults()
Truth table33%1/3
A ||= B
A= $optB= snag_opt_defaults()
ABresult
1X1
011
000
283 10 my $as_dir = $opt->{as_dir}; # might be undefined (ok)
284  
285 # Carve off terminal digit string $digits ("1" default).
286 10 ▶ my $digits = ($template =~ s/(\d+)$//)
Branch: 1 branch
BranchTrueFalseTotal
$template =~ s/(\d+)$// ? : 4 6 10
287 ? $1 : "1";
288 10 my $vfmt = '%0'. length($digits) . 'd'; # version number format
289  
290 10 ▶ $opt->{mknextcopy} and -f $template || # pre-condition of mknextcopy
Branch: 1 branch
BranchTrueFalseTotal
$opt->{"mknextcopy"} and -f $template || (return -1, "mknextcopy only works if '${template}' is a " . 'pre-existing file') 3 7 10
Condition80%4/5
$opt->{'mknextcopy'} && (-f $template || (return -1, "mknextcopy only works if '${template}' is a " . 'pre-existing file'))
!ll&&!rl&&r
730
-f $template or return -1, "mknextcopy only works if '${template}' is a " . 'pre-existing file'
l!l
12
MC/DC0%0/2
$opt->{'mknextcopy'} && (-f $template || (return -1, "mknextcopy only works if '${template}' is a " . 'pre-existing file'))
$opt->{'mknextcopy'} -f $template
Truth table66%2/3
A && B
A= $opt->{'mknextcopy'}B= -f $template
ABresult
0X0
100
111
291 return (-1, "mknextcopy only works if '$template' is a "
292 . "pre-existing file");
293  
294 8 ▶ $opt->{no_type_mismatch} ||= 0; # default this option to false
Condition50%1/2
$opt->{"no_type_mismatch"} ||= 0
l!l
80
MC/DC0%0/1
$opt->{"no_type_mismatch"} ||= 0
$opt->{"no_type_mismatch"}
Truth table50%1/2
A
A= $opt->{"no_type_mismatch"}
Aresult
11
00
295 8 my ($hnum, $hnode) = list_high_version($template);
296 8 ▶ if ($hnum == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hnum == -1) { } 4 4 8
297 4 ▶ $as_dir ||= 0; # default type here is "file"
Condition50%1/2
$as_dir ||= 0
l!l
04
MC/DC0%0/1
$as_dir ||= 0
$as_dir
Truth table50%1/2
A
A= $as_dir
Aresult
11
00
298 4 $hnum = $digits - 1; # number we'll start trying for
299 }
300 else { # current high is in $hnode
301 4 my $isa_dir = -d $hnode;
302  
303 # Note that ||= can't be used to set default if 0 (or "")
304 # is a valid specified value, as for $as_dir below.
305 #
306 4 ▶ if (defined $as_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $as_dir) { } 4 0 4
307 $opt->{no_type_mismatch} and # if intolerant and
308 4 ▶ ($isa_dir xor $as_dir) and # a mismatch
Branch: 3 branches
BranchTrueFalseTotal
$opt->{"no_type_mismatch"} and $isa_dir ^^ $as_dir and return -1, "high version ($hnode" . ($isa_dir ? "/" : "") . ') is a ' . ($isa_dir ? "directory" : "file") . ', different from type requested' 1 3 4
$isa_dir ? : 0 1 1
$isa_dir ? : 0 1 1
Condition57%4/7
$opt->{"no_type_mismatch"} and $isa_dir ^^ $as_dir
!ll&&!rl&&r
031
$isa_dir xor $as_dir
l&&rl&&!r!l&&r!l&&!r
0013
MC/DC33%1/3
$opt->{"no_type_mismatch"} and $isa_dir ^^ $as_dir
$opt->{"no_type_mismatch"} $isa_dir $as_dir
Truth table40%2/5
A and B xor C
A= $opt->{"no_type_mismatch"}B= $isa_dirC= $as_dir
ABCresult
0XX0
1110
1000
1101
1011
309 return (-1, "high version ($hnode" .
310 ($isa_dir ? "/" : "") . ") is a " .
311 ($isa_dir ? "directory" : "file") .
312 ", different from type requested");
313 }
314 else {
315 0 $as_dir = $isa_dir; # infer type from $hnode
316 }
317 }
318  
319 20 ▶ use constant MAX_TRIES => 3; # number of races we might run
Subroutine BEGIN: called CC=1 SCAR=0.0
320 7 my $hmax = $hnum + MAX_TRIES;
321 7 my ($try, $msg);
322 7 ▶ while (++$hnum <= $hmax) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$hnum <= $hmax) 7 0 7
323 7 $try = $template . sprintf($vfmt, $hnum);
324 7 ▶ $msg = $as_dir ?
Branch: 1 branch
BranchTrueFalseTotal
$as_dir ? : 0 7 7
325 snag_dir($try) : snag_file($try);
326 7 ▶ last if ! $msg; # success
Branch: 1 branch
BranchTrueFalseTotal
unless $msg 7 0 7
327 #return ($hnum, $try) if ! $msg; # success
328 0 ▶ return (-1, $msg) if $msg ne "1"; # error, not race
Branch: 1 branch
BranchTrueFalseTotal
if $msg ne 1 0 0 0
329 }
330 7 ▶ $msg and return (-1, "gave up after " . MAX_TRIES . " races lost");
Branch: 1 branch
BranchTrueFalseTotal
$msg and return -1, "gave up after 3 races lost" 0 7 7
331  
332 # If we get here, $try contains the node that we snagged.
333 7 ▶ if ($opt->{mknextcopy}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"mknextcopy"}) 1 6 7
334 1 ▶ return (-1, "mknextcopy only works if '$try' is a file")
Branch: 1 branch
BranchTrueFalseTotal
unless -f $try 0 1 1
335 unless -f $try; # re-check pre-condition
336 20 ▶ use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
337 1 ▶ copy($template, $try) or
Branch: 1 branch
BranchTrueFalseTotal
copy($template, $try) or return -1, "mknextcopy failed: $!" 1 0 1
338 return (-1, "mknextcopy failed: $!");
339 }
340 7 return ($hnum, $try);
341 }
342  
343 2 ▶sub snag_dir { my( $node )=@_;
Subroutine snag_dir: called CC=4 SCAR=18.9
Pod documented
344  
345 2 ▶ return "snag_dir: no directory given" if ! defined $node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 2 2
346 2 ▶ return "" if mkdir($node);
Branch: 1 branch
BranchTrueFalseTotal
if mkdir $node 2 0 2
347 0 ▶ return "1" if -e $node; # potential race condition detector
Branch: 1 branch
BranchTrueFalseTotal
if -e $node 0 0 0
348 0 return $!; # else return system error message
349 }
350  
351 20 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
352 10 ▶sub snag_file { my( $node )=@_;
Subroutine snag_file: called CC=6 SCAR=18.8
Pod documented
353  
354 10 ▶ return "snag_file: no file given" if ! defined $node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 10 10
355 10 ▶ return ""
Branch: 1 branch
BranchTrueFalseTotal
if sysopen FH, $node, 192 and close FH || 1 9 1 10
Condition60%3/5
sysopen FH, $node, 192 and close FH || 1
!ll&&!rl&&r
109
close FH or 1
l!l
90
MC/DC50%1/2
sysopen FH, $node, 192 and close FH || 1
sysopen FH, $node, 192 close FH
Truth table66%2/3
A and B
A= sysopen FH, $node, 192B= close FH
ABresult
0X0
100
111
356 # Create, but don't succeed if it doesn't exist (O_EXCL).
357 # Return success even if for some reason the close fails.
358 if sysopen(FH, $node, O_CREAT | O_EXCL)
359 && (close(FH) || 1);
360 1 ▶ return "1" if -e $node; # potential race condition detector
Branch: 1 branch
BranchTrueFalseTotal
if -e $node 0 1 1
361 1 return $!; # else return system error message
362 }
363  
364 20 1; # End of File::Value
365  
366 __END__
↑ Summary script/snag »