FileBackend.pm

filter: Statementstmt 89.6% 207 / 231 Branchbran 58.9% 46 / 78 Conditioncond 47.2% 17 / 36 MC/DCmcdc 29.1% 7 / 24 Subroutinesub 95.3% 41 / 43 Podpod 7.1% 1 / 14 totaltotal 74.8% 319 / 426 SCAR 45.3
CC
58
Cov
78%
_Recursive_Directory_Size
42.0
_Recursively_List_Files_With_Paths
30.0
_Write_File
13.4
CRAP 92.6
↑ Summary CacheTester.pm »
1 ######################################################################
2 # $Id: FileBackend.pm,v 1.27 2005/03/17 19:31:27 dclinton Exp $
3 # Copyright (C) 2001-2003 DeWitt Clinton All Rights Reserved
4 #
5 # Software distributed under the License is distributed on an "AS
6 # IS" basis, WITHOUT WARRANTY OF ANY KIND, either expressed or
7 # implied. See the License for the specific language governing
8 # rights and limitations under the License.
9 ######################################################################
10  
11 package Cache::FileBackend;
12  
13 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Cache::CacheUtils qw( Assert_Defined Build_Path Freeze_Data Thaw_Data );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Digest::SHA1 qw( sha1_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use File::Path qw( mkpath );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use File::Temp qw( tempfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21 # the file mode for new directories, which will be modified by the
22 # current umask
23  
24 2 my $DIRECTORY_MODE = 0777;
25  
26  
27 # regex for untainting directory and file paths. since all paths are
28 # generated by us or come from user via API, a tautological regex
29 # suffices.
30  
31 2 my $UNTAINTED_PATH_REGEX = '^(.*)$';
32  
33  
34 sub new
35 {
36 68 ▶ my ( $proto, $p_root, $p_depth, $p_directory_umask ) = @_;
Subroutine new: called CC=2 SCAR=7.1
Pod documented
37 68 ▶ my $class = ref( $proto ) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0680
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
38 68 my $self = {};
39 68 $self = bless( $self, $class );
40 68 $self->set_root( $p_root );
41 68 $self->set_depth( $p_depth );
42 68 $self->set_directory_umask( $p_directory_umask );
43 68 return $self;
44 }
45  
46  
47 sub delete_key
48 {
49 27 ▶ my ( $self, $p_namespace, $p_key ) = @_;
Subroutine delete_key: called CC=1 SCAR=0.0
Pod undocumented
50  
51 27 Assert_Defined( $p_namespace );
52 27 Assert_Defined( $p_key );
53  
54 27 _Remove_File( $self->_path_to_key( $p_namespace, $p_key ) );
55 }
56  
57  
58 sub delete_namespace
59 {
60 25 ▶ my ( $self, $p_namespace ) = @_;
Subroutine delete_namespace: called CC=1 SCAR=0.0
Pod undocumented
61  
62 25 Assert_Defined( $p_namespace );
63  
64 25 _Recursively_Remove_Directory( Build_Path( $self->get_root( ),
65 $p_namespace ) );
66 }
67  
68  
69 sub get_keys
70 {
71 47 ▶ my ( $self, $p_namespace ) = @_;
Subroutine get_keys: called CC=3 SCAR=11.0
Pod undocumented
72  
73 47 Assert_Defined( $p_namespace );
74  
75 47 my @keys;
76  
77 47 foreach my $unique_key ( $self->_get_unique_keys( $p_namespace ) )
78 {
79 60 ▶ my $key = $self->_get_key_for_unique_key( $p_namespace, $unique_key ) or
Branch: 1 branch
BranchTrueFalseTotal
my $key = $self->_get_key_for_unique_key($p_namespace, $unique_key) or next 0 60 60
80 next;
81  
82 60 push( @keys, $key );
83 }
84  
85 47 return @keys;
86  
87 }
88  
89  
90 sub get_namespaces
91 {
92 36 ▶ my ( $self ) = @_;
Subroutine get_namespaces: called CC=1 SCAR=0.0
Pod undocumented
93  
94 36 my @namespaces;
95  
96 36 _List_Subdirectories( $self->get_root( ), \@namespaces );
97  
98 36 return @namespaces;
99 }
100  
101  
102 sub get_size
103 {
104 119 ▶ my ( $self, $p_namespace, $p_key ) = @_;
Subroutine get_size: called CC=2 SCAR=7.2
Pod undocumented
105  
106 119 Assert_Defined( $p_namespace );
107 119 Assert_Defined( $p_key );
108  
109 119 ▶ if ( -e $self->_path_to_key( $p_namespace, $p_key ) )
Branch: 1 branch
BranchTrueFalseTotal
if (-e $self->_path_to_key($p_namespace, $p_key)) { } 119 0 119
110 {
111 119 return -s $self->_path_to_key( $p_namespace, $p_key );
112  
113 }
114 else
115 {
116 0 return 0;
117 }
118 }
119  
120  
121 sub restore
122 {
123 122 ▶ my ( $self, $p_namespace, $p_key ) = @_;
Subroutine restore: called CC=1 SCAR=0.0
Pod undocumented
124  
125 122 Assert_Defined( $p_namespace );
126 122 Assert_Defined( $p_key );
127  
128 122 return $self->_read_data( $self->_path_to_key($p_namespace, $p_key) )->[1];
129 }
130  
131  
132 sub store
133 {
134 90 ▶ my ( $self, $p_namespace, $p_key, $p_data ) = @_;
Subroutine store: called CC=1 SCAR=0.0
Pod undocumented
135  
136 90 Assert_Defined( $p_namespace );
137 90 Assert_Defined( $p_key );
138  
139 90 $self->_write_data( $self->_path_to_key( $p_namespace, $p_key ),
140 [ $p_key, $p_data ] );
141  
142 }
143  
144  
145 sub get_depth
146 {
147 537 ▶ my ( $self ) = @_;
Subroutine get_depth: called CC=1 SCAR=0.0
Pod undocumented
148  
149 537 return $self->{_Depth};
150 }
151  
152  
153 sub set_depth
154 {
155 68 ▶ my ( $self, $depth ) = @_;
Subroutine set_depth: called CC=1 SCAR=0.0
Pod undocumented
156  
157 68 $self->{_Depth} = $depth;
158 }
159  
160  
161 sub get_root
162 {
163 645 ▶ my ( $self ) = @_;
Subroutine get_root: called CC=1 SCAR=0.0
Pod undocumented
164  
165 645 return $self->{_Root};
166 }
167  
168  
169 sub set_root
170 {
171 68 ▶ my ( $self, $root ) = @_;
Subroutine set_root: called CC=1 SCAR=0.0
Pod undocumented
172  
173 68 $self->{_Root} = $root;
174 }
175  
176  
177 sub get_directory_umask
178 {
179 90 ▶ my ( $self ) = @_;
Subroutine get_directory_umask: called CC=1 SCAR=0.0
Pod undocumented
180  
181 90 return $self->{_Directory_Umask};
182 }
183  
184  
185 sub set_directory_umask
186 {
187 68 ▶ my ( $self, $directory_umask ) = @_;
Subroutine set_directory_umask: called CC=1 SCAR=0.0
Pod undocumented
188  
189 68 $self->{_Directory_Umask} = $directory_umask;
190 }
191  
192  
193 # Take an human readable key, and create a unique key from it
194  
195 sub _Build_Unique_Key
196 {
197 477 ▶ my ( $p_key ) = @_;
Subroutine _Build_Unique_Key: called CC=1 SCAR=0.0
198  
199 477 Assert_Defined( $p_key );
200  
201 477 return sha1_hex( $p_key );
202 }
203  
204  
205 # create a directory with optional mask, building subdirectories as
206 # needed.
207  
208 sub _Create_Directory
209 {
210 47 ▶ my ( $p_directory, $p_optional_new_umask ) = @_;
Subroutine _Create_Directory: called CC=5 SCAR=16.7
211  
212 47 Assert_Defined( $p_directory );
213  
214 47 ▶ my $old_umask = umask( ) if defined $p_optional_new_umask;
Branch: 1 branch
BranchTrueFalseTotal
if defined $p_optional_new_umask 47 0 47
215  
216 47 ▶ umask( $p_optional_new_umask ) if defined $p_optional_new_umask;
Branch: 1 branch
BranchTrueFalseTotal
if defined $p_optional_new_umask 47 0 47
217  
218 47 my $directory = _Untaint_Path( $p_directory );
219  
220 47 $directory =~ s|/$||;
221  
222 47 mkpath( $directory, 0, $DIRECTORY_MODE );
223  
224 47 ▶ -d $directory or
Branch: 1 branch
BranchTrueFalseTotal
-d $directory or "Error::Simple"->throw("Couldn't create directory: ${directory}: $!") 0 47 47
225 throw Error::Simple( "Couldn't create directory: $directory: $!" );
226  
227 47 ▶ umask( $old_umask ) if defined $old_umask;
Branch: 1 branch
BranchTrueFalseTotal
if defined $old_umask 47 0 47
228 }
229  
230  
231  
232 # list the names of the subdirectories in a given directory, without the
233 # full path
234  
235 sub _List_Subdirectories
236 {
237 36 ▶ my ( $p_directory, $p_subdirectories_ref ) = @_;
Subroutine _List_Subdirectories: called CC=5 SCAR=16.1
238  
239 36 foreach my $dirent ( _Read_Dirents( $p_directory ) )
240 {
241 104 ▶ next if $dirent eq '.' or $dirent eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $dirent eq "." or $dirent eq ".." 70 34 104
Condition100%3/3
$dirent eq "." or $dirent eq ".."
l!l&&r!l&&!r
353534
MC/DC100%2/2
$dirent eq "." or $dirent eq ".."
$dirent eq "." $dirent eq ".."
Truth table100%3/3
A or B
A= $dirent eq "."B= $dirent eq ".."
ABresult
1X1
011
000
242  
243 34 my $path = Build_Path( $p_directory, $dirent );
244  
245 34 ▶ next unless -d $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $path 0 34 34
246  
247 34 push( @$p_subdirectories_ref, $dirent );
248 }
249 }
250  
251  
252 # read the dirents from a directory
253  
254 sub _Read_Dirents
255 {
256 432 ▶ my ( $p_directory ) = @_;
Subroutine _Read_Dirents: called CC=4 SCAR=14.0
257  
258 432 Assert_Defined( $p_directory );
259  
260 432 ▶ -d $p_directory or
Branch: 1 branch
BranchTrueFalseTotal
-d $p_directory or return () 1 431 432
261 return ( );
262  
263 431 local *Dir;
264  
265 431 ▶ opendir( Dir, _Untaint_Path( $p_directory ) ) or
Branch: 1 branch
BranchTrueFalseTotal
opendir Dir, _Untaint_Path($p_directory) or "Error::Simple"->throw("Couldn't open directory ${p_directory}: $!") 0 431 431
266 throw Error::Simple( "Couldn't open directory $p_directory: $!" );
267  
268 431 my @dirents = readdir( Dir );
269  
270 431 ▶ closedir( Dir ) or
Branch: 1 branch
BranchTrueFalseTotal
closedir Dir or "Error::Simple"->throw("Couldn't close directory ${p_directory}: $!") 0 431 431
271 throw Error::Simple( "Couldn't close directory $p_directory: $!" );
272  
273 431 return @dirents;
274 }
275  
276  
277 # read in a file. returns a reference to the data read
278  
279 sub _Read_File
280 {
281 151 ▶ my ( $p_path ) = @_;
Subroutine _Read_File: called CC=2 SCAR=6.9
282  
283 151 Assert_Defined( $p_path );
284  
285 151 local *File;
286  
287 151 ▶ open( File, _Untaint_Path( $p_path ) ) or
Branch: 1 branch
BranchTrueFalseTotal
open File, _Untaint_Path($p_path) or return undef 0 151 151
288 return undef;
289  
290 151 binmode( File );
291  
292 151 local $/ = undef;
293  
294 151 my $data_ref;
295  
296 151 $$data_ref = <File>;
297  
298 151 close( File );
299  
300 151 return $data_ref;
301 }
302  
303  
304 # read in a file. returns a reference to the data read, without
305 # modifying the last accessed time
306  
307 sub _Read_File_Without_Time_Modification
308 {
309 182 ▶ my ( $p_path ) = @_;
Subroutine _Read_File_Without_Time_Modification: called CC=2 SCAR=6.9
310  
311 182 Assert_Defined( $p_path );
312  
313 182 ▶ -e $p_path or
Branch: 1 branch
BranchTrueFalseTotal
-e $p_path or return undef 31 151 182
314 return undef;
315  
316 151 my ( $file_access_time, $file_modified_time ) =
317 ( stat( _Untaint_Path( $p_path ) ) )[8,9];
318  
319 151 my $data_ref = _Read_File( $p_path );
320  
321 151 utime( $file_access_time, $file_modified_time, _Untaint_Path( $p_path ) );
322  
323 151 return $data_ref;
324 }
325  
326  
327 # remove a file
328  
329 sub _Remove_File
330 {
331 59 ▶ my ( $p_path ) = @_;
Subroutine _Remove_File: called CC=2 SCAR=7.0
332  
333 59 Assert_Defined( $p_path );
334  
335 59 ▶ if ( -f _Untaint_Path( $p_path ) )
Branch: 1 branch
BranchTrueFalseTotal
if (-f _Untaint_Path($p_path)) 59 0 59
336 {
337 # We don't catch the error, because this may fail if two
338 # processes are in a race and try to remove the object
339  
340 59 unlink( _Untaint_Path( $p_path ) );
341 }
342 }
343  
344  
345 # remove a directory
346  
347 sub _Remove_Directory
348 {
349 155 ▶ my ( $p_directory ) = @_;
Subroutine _Remove_Directory: called CC=2 SCAR=7.0
350  
351 155 Assert_Defined( $p_directory );
352  
353 155 ▶ if ( -d _Untaint_Path( $p_directory ) )
Branch: 1 branch
BranchTrueFalseTotal
if (-d _Untaint_Path($p_directory)) 155 0 155
354 {
355 # We don't catch the error, because this may fail if two
356 # processes are in a race and try to remove the object
357  
358 155 rmdir( _Untaint_Path( $p_directory ) );
359 }
360 }
361  
362  
363 # recursively list the files of the subdirectories, without the full paths
364  
365 sub _Recursively_List_Files
366 {
367 251 ▶ my ( $p_directory, $p_files_ref ) = @_;
Subroutine _Recursively_List_Files: called CC=6 SCAR=17.9
368  
369 251 ▶ return unless -d $p_directory;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $p_directory 10 241 251
370  
371 241 foreach my $dirent ( _Read_Dirents( $p_directory ) )
372 {
373 746 ▶ next if $dirent eq '.' or $dirent eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $dirent eq "." or $dirent eq ".." 482 264 746
Condition100%3/3
$dirent eq "." or $dirent eq ".."
l!l&&r!l&&!r
241241264
MC/DC100%2/2
$dirent eq "." or $dirent eq ".."
$dirent eq "." $dirent eq ".."
Truth table100%3/3
A or B
A= $dirent eq "."B= $dirent eq ".."
ABresult
1X1
011
000
374  
375 264 my $path = Build_Path( $p_directory, $dirent );
376  
377 264 ▶ if ( -d $path )
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) { } 204 60 264
378 {
379 204 _Recursively_List_Files( $path, $p_files_ref );
380 }
381 else
382 {
383 60 push( @$p_files_ref, $dirent );
384 }
385 }
386 }
387  
388  
389 # recursively list the files of the subdirectories, with the full paths
390  
391 sub _Recursively_List_Files_With_Paths
392 {
393 0 ▶ my ( $p_directory, $p_files_ref ) = @_;
Subroutine _Recursively_List_Files_With_Paths: not called CC=5 SCAR=34.0
394  
395 0 foreach my $dirent ( _Read_Dirents( $p_directory ) )
396 {
397 0 ▶ next if $dirent eq '.' or $dirent eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $dirent eq "." or $dirent eq ".." 0 0 0
Condition0%0/3
$dirent eq "." or $dirent eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/2
$dirent eq "." or $dirent eq ".."
$dirent eq "." $dirent eq ".."
Truth table0%0/3
A or B
A= $dirent eq "."B= $dirent eq ".."
ABresult
1X1
011
000
398  
399 0 my $path = Build_Path( $p_directory, $dirent );
400  
401 0 ▶ if ( -d $path )
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) { } 0 0 0
402 {
403 0 _Recursively_List_Files_With_Paths( $path, $p_files_ref );
404 }
405 else
406 {
407 0 push( @$p_files_ref, $path );
408 }
409 }
410 }
411  
412  
413  
414 # remove a directory and all subdirectories and files
415  
416 sub _Recursively_Remove_Directory
417 {
418 156 ▶ my ( $p_root ) = @_;
Subroutine _Recursively_Remove_Directory: called CC=6 SCAR=17.9
419  
420 156 ▶ return unless -d $p_root;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $p_root 1 155 156
421  
422 155 foreach my $dirent ( _Read_Dirents( $p_root ) )
423 {
424 473 ▶ next if $dirent eq '.' or $dirent eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $dirent eq "." or $dirent eq ".." 310 163 473
Condition100%3/3
$dirent eq "." or $dirent eq ".."
l!l&&r!l&&!r
155155163
MC/DC100%2/2
$dirent eq "." or $dirent eq ".."
$dirent eq "." $dirent eq ".."
Truth table100%3/3
A or B
A= $dirent eq "."B= $dirent eq ".."
ABresult
1X1
011
000
425  
426 163 my $path = Build_Path( $p_root, $dirent );
427  
428 163 ▶ if ( -d $path )
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) { } 131 32 163
429 {
430 131 _Recursively_Remove_Directory( $path );
431 }
432 else
433 {
434 32 _Remove_File( _Untaint_Path( $path ) );
435 }
436 }
437  
438 155 _Remove_Directory( _Untaint_Path( $p_root ) );
439 }
440  
441  
442  
443 # walk down a directory structure and total the size of the files
444 # contained therein.
445  
446 sub _Recursive_Directory_Size
447 {
448 0 ▶ my ( $p_directory ) = @_;
Subroutine _Recursive_Directory_Size: not called CC=6 SCAR=37.4
449  
450 0 Assert_Defined( $p_directory );
451  
452 0 ▶ return 0 unless -d $p_directory;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $p_directory 0 0 0
453  
454 0 my $size = 0;
455  
456 0 foreach my $dirent ( _Read_Dirents( $p_directory ) )
457 {
458 0 ▶ next if $dirent eq '.' or $dirent eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $dirent eq "." or $dirent eq ".." 0 0 0
Condition0%0/3
$dirent eq "." or $dirent eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/2
$dirent eq "." or $dirent eq ".."
$dirent eq "." $dirent eq ".."
Truth table0%0/3
A or B
A= $dirent eq "."B= $dirent eq ".."
ABresult
1X1
011
000
459  
460 0 my $path = Build_Path( $p_directory, $dirent );
461  
462 0 ▶ if ( -d $path )
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) { } 0 0 0
463 {
464 0 $size += _Recursive_Directory_Size( $path );
465 }
466 else
467 {
468 0 $size += -s $path;
469 }
470 }
471  
472 0 return $size;
473 }
474  
475  
476 # Untaint a file path
477  
478 sub _Untaint_Path
479 {
480 1726 ▶ my ( $p_path ) = @_;
Subroutine _Untaint_Path: called CC=1 SCAR=0.0
481  
482 1726 return _Untaint_String( $p_path, $UNTAINTED_PATH_REGEX );
483 }
484  
485  
486 # Untaint a string
487  
488 sub _Untaint_String
489 {
490 1726 ▶ my ( $p_string, $p_untainted_regex ) = @_;
Subroutine _Untaint_String: called CC=3 SCAR=12.0
491  
492 1726 Assert_Defined( $p_string );
493 1726 Assert_Defined( $p_untainted_regex );
494  
495 1726 my ( $untainted_string ) = $p_string =~ /$p_untainted_regex/;
496  
497 1726 ▶ if ( not defined $untainted_string || $untainted_string ne $p_string )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $untainted_string or $untainted_string ne $p_string) 0 1726 1726
Condition33%1/3
defined $untainted_string or $untainted_string ne $p_string
l!l&&r!l&&!r
172600
MC/DC0%0/2
defined $untainted_string or $untainted_string ne $p_string
defined $untainted_string $untainted_string ne $p_string
Truth table33%1/3
A or B
A= defined $untainted_stringB= $untainted_string ne $p_string
ABresult
1X1
011
000
498 {
499 0 throw Error::Simple( "String $p_string contains possible taint" );
500 }
501  
502 1726 return $untainted_string;
503 }
504  
505  
506 # create a directory with the optional umask if it doesn't already
507 # exist
508  
509 sub _Make_Path
510 {
511 90 ▶ my ( $p_path, $p_optional_new_umask ) = @_;
Subroutine _Make_Path: called CC=5 SCAR=16.8
512  
513 90 my ( $volume, $directory, $filename ) = File::Spec->splitpath( $p_path );
514  
515 90 ▶ if ( defined $directory and defined $volume )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $directory and defined $volume) 90 0 90
Condition33%1/3
defined $directory and defined $volume
!ll&&!rl&&r
0090
MC/DC0%0/2
defined $directory and defined $volume
defined $directory defined $volume
Truth table33%1/3
A and B
A= defined $directoryB= defined $volume
ABresult
0X0
100
111
516 {
517 90 $directory = File::Spec->catpath( $volume, $directory, "" );
518 }
519  
520 90 ▶ if ( defined $directory and not -d $directory )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $directory and not -d $directory) 47 43 90
Condition66%2/3
defined $directory and not -d $directory
!ll&&!rl&&r
04347
MC/DC50%1/2
defined $directory and not -d $directory
defined $directory not -d $directory
Truth table66%2/3
A and not(B)
A= defined $directoryB= not -d $directory
ABresult
0X0
100
111
521 {
522 47 _Create_Directory( $directory, $p_optional_new_umask );
523 }
524 }
525  
526  
527 # return a list of the first $depth letters in the $word
528  
529 sub _Split_Word
530 {
531 537 ▶ my ( $p_word, $p_depth ) = @_;
Subroutine _Split_Word: called CC=2 SCAR=6.9
532  
533 537 Assert_Defined( $p_word );
534 537 Assert_Defined( $p_depth );
535  
536 537 my @split_word_list;
537  
538 537 ▶ for ( my $i = 0; $i < $p_depth; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $p_depth) 1611 537 2148
539 {
540 1611 push ( @split_word_list, substr( $p_word, $i, 1 ) );
541 }
542  
543 537 return @split_word_list;
544 }
545  
546  
547 # write a file atomically
548  
549 sub _Write_File
550 {
551 90 ▶ my ( $p_path, $p_data_ref, $p_optional_mode, $p_optional_umask ) = @_;
Subroutine _Write_File: called CC=10 SCAR=26.0
552  
553 90 Assert_Defined( $p_path );
554 90 Assert_Defined( $p_data_ref );
555  
556 90 ▶ my $old_umask = umask if $p_optional_umask;
Branch: 1 branch
BranchTrueFalseTotal
if $p_optional_umask 0 90 90
557  
558 90 ▶ umask( $p_optional_umask ) if $p_optional_umask;
Branch: 1 branch
BranchTrueFalseTotal
if $p_optional_umask 0 90 90
559  
560 90 my ( $volume, $directory, $filename ) = File::Spec->splitpath( $p_path );
561
562 90 ▶ if ( defined $directory and defined $volume )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $directory and defined $volume) 90 0 90
Condition33%1/3
defined $directory and defined $volume
!ll&&!rl&&r
0090
MC/DC0%0/2
defined $directory and defined $volume
defined $directory defined $volume
Truth table33%1/3
A and B
A= defined $directoryB= defined $volume
ABresult
0X0
100
111
563 {
564 90 $directory = File::Spec->catpath( $volume, $directory, "" );
565 }
566  
567 90 my ( $temp_fh, $temp_filename ) = tempfile( DIR => $directory );
568  
569 90 binmode( $temp_fh );
570  
571 90 print $temp_fh $$p_data_ref;
572  
573 90 close( $temp_fh );
574  
575 90 ▶ -e $temp_filename or
Branch: 1 branch
BranchTrueFalseTotal
-e $temp_filename or "Error::Simple"->throw("Temp file '${temp_filename}' does not exist: $!") 0 90 90
576 throw Error::Simple( "Temp file '$temp_filename' does not exist: $!" );
577
578 90 ▶ rename( $temp_filename, _Untaint_Path( $p_path ) ) or
Branch: 1 branch
BranchTrueFalseTotal
rename $temp_filename, _Untaint_Path($p_path) or "Error::Simple"->throw("Couldn't rename $temp_filename to ${p_path}: $!") 0 90 90
579 throw Error::Simple( "Couldn't rename $temp_filename to $p_path: $!" );
580  
581 90 ▶ if ( -e $temp_filename )
Branch: 1 branch
BranchTrueFalseTotal
if (-e $temp_filename) 0 90 90
582 {
583 0 _Remove_File( $temp_filename );
584 0 warn( "Temp file '$temp_filename' shouldn't still exist" );
585 }
586  
587 90 ▶ $p_optional_mode ||= 0666 - umask( );
Condition33%1/3
$p_optional_mode ||= 438 - (umask())
l!l&&r!l&&!r
0900
MC/DC0%0/2
$p_optional_mode ||= 438 - (umask())
$p_optional_mode 438 - (umask())
Truth table33%1/3
A ||= B
A= $p_optional_modeB= 438 - (umask())
ABresult
1X1
011
000
588  
589 90 chmod( $p_optional_mode, _Untaint_Path($p_path) );
590  
591 90 ▶ umask( $old_umask ) if $old_umask;
Branch: 1 branch
BranchTrueFalseTotal
if $old_umask 0 90 90
592 }
593  
594  
595 sub _get_key_for_unique_key
596 {
597 60 ▶ my ( $self, $p_namespace, $p_unique_key ) = @_;
Subroutine _get_key_for_unique_key: called CC=1 SCAR=0.0
598  
599 60 return $self->_read_data( $self->_path_to_unique_key( $p_namespace,
600 $p_unique_key ) )->[0];
601 }
602  
603  
604 sub _get_unique_keys
605 {
606 47 ▶ my ( $self, $p_namespace ) = @_;
Subroutine _get_unique_keys: called CC=1 SCAR=0.0
607  
608 47 Assert_Defined( $p_namespace );
609  
610 47 my @unique_keys;
611  
612 47 _Recursively_List_Files( Build_Path( $self->get_root( ), $p_namespace ),
613 \@unique_keys );
614  
615 47 return @unique_keys;
616 }
617  
618  
619 sub _path_to_key
620 {
621 477 ▶ my ( $self, $p_namespace, $p_key ) = @_;
Subroutine _path_to_key: called CC=1 SCAR=0.0
622  
623 477 Assert_Defined( $p_namespace );
624 477 Assert_Defined( $p_key );
625  
626 477 return $self->_path_to_unique_key( $p_namespace,
627 _Build_Unique_Key( $p_key ) );
628 }
629  
630  
631 sub _path_to_unique_key
632 {
633 537 ▶ my ( $self, $p_namespace, $p_unique_key ) = @_;
Subroutine _path_to_unique_key: called CC=1 SCAR=0.0
634  
635 537 Assert_Defined( $p_unique_key );
636 537 Assert_Defined( $p_namespace );
637  
638 537 return Build_Path( $self->get_root( ),
639 $p_namespace,
640 _Split_Word( $p_unique_key, $self->get_depth( ) ),
641 $p_unique_key );
642 }
643  
644 # the data is returned as reference to an array ( key, data )
645  
646 sub _read_data
647 {
648 182 ▶ my ( $self, $p_path ) = @_;
Subroutine _read_data: called CC=4 SCAR=15.0
649  
650 182 Assert_Defined( $p_path );
651  
652 182 ▶ my $frozen_data_ref = _Read_File_Without_Time_Modification( $p_path ) or
Branch: 1 branch
BranchTrueFalseTotal
my $frozen_data_ref = _Read_File_Without_Time_Modification($p_path) or return [undef, undef] 31 151 182
653 return [ undef, undef ];
654  
655 151 my $data_ref = eval{ Thaw_Data( $$frozen_data_ref ) };
656
657 151 ▶ if ( $@ || ( ref( $data_ref ) ne 'ARRAY' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or ref $data_ref ne 'ARRAY') { } 0 151 151
Condition33%1/3
$@ or ref $data_ref ne "ARRAY"
l!l&&r!l&&!r
00151
MC/DC0%0/2
$@ or ref $data_ref ne "ARRAY"
$@ ref $data_ref ne "ARRAY"
Truth table33%1/3
A or B
A= $@B= ref $data_ref ne "ARRAY"
ABresult
1X1
011
000
658 {
659 0 unlink _Untaint_Path( $p_path );
660 0 return [ undef, undef ];
661 }
662 else
663 {
664 151 return $data_ref;
665 }
666 }
667  
668  
669 # the data is passed as reference to an array ( key, data )
670  
671 sub _write_data
672 {
673 90 ▶ my ( $self, $p_path, $p_data ) = @_;
Subroutine _write_data: called CC=1 SCAR=0.0
674  
675 90 Assert_Defined( $p_path );
676 90 Assert_Defined( $p_data );
677  
678 90 _Make_Path( $p_path, $self->get_directory_umask( ) );
679  
680 90 my $frozen_file = Freeze_Data( $p_data );
681  
682 90 _Write_File( $p_path, \$frozen_file );
683 }
684  
685  
686 2 1;
687  
688  
689 __END__
↑ Summary CacheTester.pm »