blib/lib/File/Cache.pm

filter: Statementstmt 89.5% 547 / 611 Branchbran 57.9% 205 / 354 Conditioncond 50.0% 37 / 74 MC/DCmcdc 27.0% 13 / 48 Subroutinesub 96.3% 80 / 83 Podpod 93.7% 30 / 32 totaltotal 75.8% 912 / 1202 CC 209 SCAR 19.9
CC
209
Cov
76%
REDUCE_SIZE
42.0
_UNSERIALIZE
17.9
_SUBDIRECTORIES_PRESENT
16.7
CRAP 7.3
↑ Summary
1 #!/usr/bin/perl -w
2  
3 package File::Cache;
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use File::Spec::Functions qw(tmpdir splitdir splitpath catdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use vars qw(@ISA @EXPORT_OK $VERSION $sSUCCESS $sFAILURE $sTRUE $sFALSE
Subroutine BEGIN: called CC=1 SCAR=0.0
15 $sEXPIRES_NOW $sEXPIRES_NEVER $sNO_MAX_SIZE $sGET_STALE_ONLY
16 3 $sGET_FRESH_ONLY $CACHE_OBJECT_VERSION);
17  
18 3 $VERSION = '0.16';
19  
20 # Describes the caches created by this version of File::Cache. (Should
21 # be incremented any time the cache file format changes in a way that
22 # breaks backward compatibility.)
23  
24 3 $CACHE_OBJECT_VERSION = '0.01';
25  
26 3 @ISA = qw(Exporter);
27  
28 3 @EXPORT_OK = qw($sSUCCESS $sFAILURE $sTRUE $sFALSE $sEXPIRES_NOW
29 $sEXPIRES_NEVER $sNO_MAX_SIZE );
30  
31 # -----------------------------------------------------------------------------
32  
33 # Code notes:
34 # Internal subroutines (helper routines not supposed to be called by
35 # external clients) are preceded with an underscore ("_"). Subroutines
36 # (both internal and external) that are called as functions, as
37 # opposed to methods, are in ALL CAPS. The PURGE and CLEAR routines
38 # are object-independent, which means that any subroutines they call
39 # must also be object-independent.
40  
41 # -----------------------------------------------------------------------------
42  
43 # Constants
44  
45 3 $sSUCCESS = 1;
46 3 $sFAILURE = 0;
47  
48 3 $sTRUE = 1;
49 3 $sFALSE = 0;
50  
51 3 $sEXPIRES_NOW = 0;
52 3 $sEXPIRES_NEVER = -1;
53  
54 3 $sNO_MAX_SIZE = -1;
55  
56 3 $sGET_STALE_ONLY = 1;
57 3 $sGET_FRESH_ONLY = 0;
58  
59 # The default cache key is used inside the tmp filesystem (as defined
60 # by File::Spec)
61  
62 3 my $sDEFAULT_CACHE_KEY;
63  
64 3 ▶$sDEFAULT_CACHE_KEY = ($^O eq 'dos' || $^O eq 'MSWin32') ?
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'dos' || $^O eq 'MSWin32' ? : 0 3 3
Condition33%1/3
$^O eq "dos" or $^O eq "MSWin32"
l!l&&r!l&&!r
003
MC/DC0%0/2
$^O eq "dos" or $^O eq "MSWin32"
$^O eq "dos" $^O eq "MSWin32"
Truth table33%1/3
A or B
A= $^O eq "dos"B= $^O eq "MSWin32"
ABresult
1X1
011
000
65 'FileCache' : 'File::Cache';
66  
67  
68 # if a namespace is not specified, use this as a default
69  
70 3 my $sDEFAULT_NAMESPACE = "_default";
71  
72  
73 # by default, remove objects that have expired when then are requested
74  
75 3 my $sDEFAULT_AUTO_REMOVE_STALE = $sTRUE;
76  
77  
78 # by default, the filemode is world read/writable
79  
80 3 my $sDEFAULT_FILEMODE = 0777;
81  
82  
83 # by default, there is no max size to the cache
84  
85 3 my $sDEFAULT_MAX_SIZE = $sNO_MAX_SIZE;
86  
87  
88 # if the OS does not support getpwuid, use this as a default username
89  
90 3 my $sDEFAULT_USERNAME = 'nobody';
91  
92  
93 # by default, the objects in the cache never expire
94  
95 3 my $sDEFAULT_GLOBAL_EXPIRES_IN = $sEXPIRES_NEVER;
96  
97  
98 # default cache depth
99  
100 3 my $sDEFAULT_CACHE_DEPTH = 0;
101  
102  
103 # File::Cache supports either Storable or Data::Dumper as the
104 # persistence mechanism. The default persistence mechanism uses Storable
105  
106 3 my $sDEFAULT_PERSISTENCE_MECHANISM = 'Storable';
107  
108  
109  
110 # cache description filename
111  
112 3 my $sCACHE_DESCRIPTION_FILENAME = '.description';
113  
114  
115 # Always use a global friendly umask for the .description files
116  
117 3 my $sCACHE_DESCRIPTION_UMASK = 022;
118  
119  
120 # valid filepath characters for tainting. Be sure to accept DOS/Windows style
121 # path specifiers (C:\path) also
122  
123 3 my $sUNTAINTED_FILE_PATH_REGEX = qr{^([-\@\w\\\\~./:]+|[\w]:[-\@\w\\\\~./]+)$};
124  
125  
126  
127 # -----------------------------------------------------------------------------
128  
129 # create a new Cache object that can be used to persist
130 # data across processes
131  
132 sub new
133 13 ▶{
Subroutine new: called CC=12 SCAR=25.2
Pod documented
134 13 my ($proto, $options) = @_;
135 13 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0130
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
136 13 my $self = {};
137 13 bless ($self, $class);
138  
139  
140 # remove objects from the cache that have expired on retrieval
141 # when this is set
142  
143 my $auto_remove_stale = defined $options->{auto_remove_stale} ?
144 13 ▶ $options->{auto_remove_stale} : $sDEFAULT_AUTO_REMOVE_STALE;
Branch: 1 branch
BranchTrueFalseTotal
defined $options->{'auto_remove_stale'} ? : 5 8 13
145  
146 13 $self->set_auto_remove_stale($auto_remove_stale);
147  
148  
149 # username is either specified or searched for in an OS
150 # independent way
151  
152 my $username = defined $options->{username} ?
153 13 ▶ $options->{username} : _FIND_USERNAME();
Branch: 1 branch
BranchTrueFalseTotal
defined $options->{'username'} ? : 7 6 13
154  
155 13 $self->set_username($username);
156  
157  
158 # the user can specify the filemode
159  
160 my $filemode = defined $options->{filemode} ?
161 13 ▶ $options->{filemode} : $sDEFAULT_FILEMODE;
Branch: 1 branch
BranchTrueFalseTotal
defined $options->{'filemode'} ? : 5 8 13
162  
163 13 $self->set_filemode($filemode);
164  
165  
166 # remember the expiration delta to be used for all objects if
167 # specified
168  
169 my $global_expires_in = defined $options->{expires_in} ?
170 13 ▶ $options->{expires_in} : $sDEFAULT_GLOBAL_EXPIRES_IN;
Branch: 1 branch
BranchTrueFalseTotal
defined $options->{'expires_in'} ? : 0 13 13
171  
172 13 $self->set_global_expires_in($global_expires_in);
173  
174  
175 # set the cache key to either the user's value or the default
176  
177 my $cache_key = defined $options->{cache_key} ?
178 13 ▶ $options->{cache_key} : _BUILD_DEFAULT_CACHE_KEY();
Branch: 1 branch
BranchTrueFalseTotal
defined $options->{'cache_key'} ? : 13 0 13
179  
180 13 $self->set_cache_key($cache_key);
181  
182  
183 # this instance will use the namespace specified or the default
184  
185 my $namespace = defined $options->{namespace} ?
186 13 ▶ $options->{namespace} : $sDEFAULT_NAMESPACE;
Branch: 1 branch
BranchTrueFalseTotal
defined $options->{'namespace'} ? : 13 0 13
187  
188 13 $self->set_namespace($namespace);
189  
190  
191 # the cache will automatically create subdirectories to this depth
192  
193 my $cache_depth = defined $options->{cache_depth} ?
194 13 ▶ $options->{cache_depth} : $sDEFAULT_CACHE_DEPTH;
Branch: 1 branch
BranchTrueFalseTotal
defined $options->{'cache_depth'} ? : 7 6 13
195  
196 13 $self->set_cache_depth($cache_depth);
197  
198  
199 # the max cache size is either specified by the user or by the
200 # default cache size. Be sure to do this after the cache key,
201 # user, and namespace are set up, because it invokes reduce_size.
202  
203 my $max_size = defined $options->{max_size} ?
204 13 ▶ $options->{max_size} : $sDEFAULT_MAX_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
defined $options->{'max_size'} ? : 5 8 13
205  
206 13 $self->set_max_size($max_size);
207  
208  
209 # verify that we can create the cache when necessary later
210  
211 13 ▶ _VERIFY_DIRECTORY( $self->_get_namespace_path() ) == $sSUCCESS or
Branch: 1 branch
BranchTrueFalseTotal
_VERIFY_DIRECTORY($self->_get_namespace_path) == $File::Cache::sSUCCESS or croak('Can not build cache at ' . $self->_get_namespace_path . '. Check directory permissions.') 13 0 13
212 croak("Can not build cache at " . $self->_get_namespace_path() .
213 ". Check directory permissions.");
214  
215 # set the persistence mechanism to the user specified one (or the
216 # default), then load the necessary modules that correspond to
217 # that persistence mechanism choice
218  
219 my $persistence_mechanism = defined $options->{persistence_mechanism} ?
220 13 ▶ $options->{persistence_mechanism} : $sDEFAULT_PERSISTENCE_MECHANISM;
Branch: 1 branch
BranchTrueFalseTotal
defined $options->{'persistence_mechanism'} ? : 0 13 13
221  
222 13 $self->set_persistence_mechanism($persistence_mechanism);
223 13 $self->_load_persistence_mechanism();
224  
225  
226 # could update a legacy cache here
227  
228  
229 # check that any existing cache is compatible
230  
231 13 $self->_check_cache_compatibility();
232  
233  
234 # write the cache description, in case there isn't already one
235  
236 13 my $cache_description = $self->_get_cache_description();
237  
238 13 _WRITE_CACHE_DESCRIPTION( $cache_key, $cache_description, $filemode );
239  
240  
241 13 return $self;
242 }
243  
244 # -----------------------------------------------------------------------------
245  
246 # Reads the cache description from the file system. Returns a reference to a
247 # hash, or undef if no cache appears to be in location specified by the cache
248 # key, or the cache has problems. A cache description is automatically
249 # generated for older style caches that do not have cache description files.
250 # (The presence of any directories in the cache key directory are taken to
251 # mean that such a legacy cache exists.)
252  
253 sub _READ_CACHE_DESCRIPTION
254 327 ▶{
Subroutine _READ_CACHE_DESCRIPTION: called CC=6 SCAR=19.9
255 327 my ($cache_key) = @_;
256  
257 327 ▶ defined($cache_key) or
Branch: 1 branch
BranchTrueFalseTotal
defined $cache_key or croak("cache key required") 327 0 327
258 croak("cache key required");
259  
260 327 my $cache_description_path =
261 _BUILD_PATH($cache_key, $sCACHE_DESCRIPTION_FILENAME);
262  
263 # This is the name of the variable stored using Data::Dumper in
264 # the cache description file.
265  
266 327 my $cache_description = {};
267  
268 327 ▶ if (-f $cache_description_path) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $cache_description_path) { } 316 11 327
elsif (_SUBDIRECTORIES_PRESENT($cache_key) eq $File::Cache::sTRUE) { } 0 11 11
269  
270 316 my $serialized_cache_description_ref =
271 _READ_FILE($cache_description_path);
272  
273 316 ▶ unless (defined $serialized_cache_description_ref and
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $serialized_cache_description_ref and defined $$serialized_cache_description_ref) 0 316 316
Condition33%1/3
defined $serialized_cache_description_ref and defined $$serialized_cache_description_ref
!ll&&!rl&&r
00316
MC/DC0%0/2
defined $serialized_cache_description_ref and defined $$serialized_cache_description_ref
defined $serialized_cache_description_ref defined $$serialized_cache_description_ref
Truth table33%1/3
A and B
A= defined $serialized_cache_description_refB= defined $$serialized_cache_description_ref
ABresult
0X0
100
111
274 defined $$serialized_cache_description_ref)
275 {
276  
277 0 warn "Could not read cache description file $cache_description_path";
278 0 return undef;
279  
280 }
281  
282 316 _UNSERIALIZE_HASH($$serialized_cache_description_ref,
283 $cache_description);
284  
285 } elsif (_SUBDIRECTORIES_PRESENT($cache_key) eq $sTRUE) {
286  
287 # Older caches used Storable as the persistence mechanism
288 0 $cache_description =
289 {
290 'File::Cache Version' => undef,
291 'Cache Object Version' => 0.01,
292 'Persistence Mechanism' => 'Storable',
293 };
294  
295 } else {
296  
297 11 return undef;
298  
299 }
300  
301 316 return $cache_description;
302 }
303  
304 # -----------------------------------------------------------------------------
305  
306 # Determines if there are subdirectories in a given directory
307  
308 sub _SUBDIRECTORIES_PRESENT
309 11 ▶{
Subroutine _SUBDIRECTORIES_PRESENT: called CC=6 SCAR=28.1
310 11 my ($directory) = @_;
311  
312 11 ▶ defined($directory) or
Branch: 1 branch
BranchTrueFalseTotal
defined $directory or croak("directory required") 11 0 11
313 croak("directory required");
314  
315 11 $directory = _UNTAINT_FILE_PATH($directory);
316  
317 11 ▶ return $sFALSE unless -d $directory;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $directory 11 0 11
318  
319 0 ▶ opendir(DIR, $directory) or
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $directory or croak("Couldn't open directory ${directory}: $!") 0 0 0
320 croak("Couldn't open directory $directory: $!");
321  
322 0 my @dirents = readdir(DIR);
323  
324 0 closedir DIR;
325  
326 0 foreach my $dirent (@dirents)
327 {
328 0 ▶ return $sTRUE if -d $dirent;
Branch: 1 branch
BranchTrueFalseTotal
if -d $dirent 0 0 0
329 }
330  
331 0 return $sFALSE;
332 }
333  
334 # -----------------------------------------------------------------------------
335  
336 # Writes a cache description to the file system. Takes a cache key, a
337 # reference to a hash, and a file mode
338  
339 sub _WRITE_CACHE_DESCRIPTION
340 13 ▶{
Subroutine _WRITE_CACHE_DESCRIPTION: called CC=4 SCAR=14.2
341 13 my ($cache_key, $cache_description, $filemode) = @_;
342  
343 13 ▶ defined($cache_key) or
Branch: 1 branch
BranchTrueFalseTotal
defined $cache_key or croak("cache_key required") 13 0 13
344 croak("cache_key required");
345  
346 13 ▶ defined($cache_description) or
Branch: 1 branch
BranchTrueFalseTotal
defined $cache_description or croak("cache description required") 13 0 13
347 croak("cache description required");
348  
349 13 ▶ defined($filemode) or
Branch: 1 branch
BranchTrueFalseTotal
defined $filemode or croak("filemode required") 13 0 13
350 croak("filemode required");
351  
352 13 my $cache_description_path =
353 _BUILD_PATH($cache_key, $sCACHE_DESCRIPTION_FILENAME);
354  
355 13 my $serialized_cache_description = _SERIALIZE_HASH($cache_description);
356  
357 13 _CREATE_DIRECTORY($cache_key,0);
358  
359 # mike@blakeley.com: specifying the filemode is bad for .description,
360 # since it's global for the whole cache.
361  
362 13 _WRITE_FILE($cache_description_path,
363 \$serialized_cache_description,
364 $filemode,
365 $sCACHE_DESCRIPTION_UMASK);
366  
367  
368 }
369  
370  
371 sub _SERIALIZE_HASH
372 13 ▶{
Subroutine _SERIALIZE_HASH: called CC=2 SCAR=6.9
373 13 my ($hash_ref) = @_;
374  
375 13 my $serialized_hash;
376  
377 13 foreach my $key (keys %{$hash_ref}) {
378  
379 39 $serialized_hash .= "$key => $hash_ref->{$key}\n";
380  
381 }
382  
383 13 return $serialized_hash;
384 }
385  
386  
387 sub _UNSERIALIZE_HASH
388 316 ▶{
Subroutine _UNSERIALIZE_HASH: called CC=4 SCAR=14.6
389 316 my ($string, $hash_ref) = @_;
390  
391 316 my @key_value_pair_list = split(/\n/, $string);
392  
393 316 foreach my $key_value_pair (@key_value_pair_list) {
394  
395 948 my ($key, $value) = $key_value_pair =~ m|(.*?) => (.*)|;
396  
397 948 ▶ next unless $key and $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $key and $value 0 948 948
Condition33%1/3
$key and $value
!ll&&!rl&&r
00948
MC/DC0%0/2
$key and $value
$key $value
Truth table33%1/3
A and B
A= $keyB= $value
ABresult
0X0
100
111
398  
399 948 $hash_ref->{$key} = $value;
400  
401 }
402 }
403  
404 # -----------------------------------------------------------------------------
405  
406 # Check that any existing cache is compatible. For example, a cache
407 # created using a later version of File::Cache with a new cached
408 # object format is incompatible.
409  
410 sub _check_cache_compatibility
411 13 ▶{
Subroutine _check_cache_compatibility: called CC=4 SCAR=17.3
412 13 my ($self) = @_;
413  
414 13 my $existing_cache_description =
415 _READ_CACHE_DESCRIPTION( $self->get_cache_key() );
416  
417 # Not defined means that there is no existing cache, or there is a problem
418 # with the cache.
419 13 ▶ return unless defined $existing_cache_description;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $existing_cache_description 11 2 13
420  
421 # Compare cache object versions.
422 2 ▶ if ( ($existing_cache_description->{'Cache Object Version'} >
Branch: 1 branch
BranchTrueFalseTotal
if ($existing_cache_description->{"Cache Object Version"} > $File::Cache::CACHE_OBJECT_VERSION) 0 2 2
423 $CACHE_OBJECT_VERSION) )
424 {
425 0 warn "Incompatible cache object versions detected. " .
426 "The cache will be cleared";
427 0 CLEAR( $self->get_cache_key() );
428 0 return;
429 }
430  
431 # Check that the persistence mechanisms match.
432 2 ▶ if ( $existing_cache_description->{'Persistence Mechanism'} ne
Branch: 1 branch
BranchTrueFalseTotal
if ($existing_cache_description->{"Persistence Mechanism"} ne $self->get_persistence_mechanism) 0 2 2
433 $self->get_persistence_mechanism() )
434 {
435 0 warn "Incompatible cache object persistence mechanisms detected. " .
436 "The cache will be cleared";
437 0 CLEAR( $self->get_cache_key() );
438 0 return;
439 }
440 }
441  
442 # -----------------------------------------------------------------------------
443  
444 # Gets the cache description for the cache, returning a reference to a
445 # hash. The keys are:
446 # - File::Cache Version: The version of File::Cache used to create the
447 # cache. (May be undef for cache descriptions that are auto-generated
448 # by _READ_CACHE_DESCRIPTION based on a legacy cache.)
449 # - Cache Object Version: The version number of the format used to store
450 # objects in the cache.
451 # - Persistence Mechanism: The persistence mechanism used to store
452 # objects in the cache.
453  
454 sub _get_cache_description
455 13 ▶{
Subroutine _get_cache_description: called CC=1 SCAR=0.0
456 13 my ($self) = @_;
457  
458 13 my $cache_description =
459 {
460 'File::Cache Version' => $VERSION,
461 'Cache Object Version' => $CACHE_OBJECT_VERSION,
462 'Persistence Mechanism' => $self->get_persistence_mechanism(),
463 };
464  
465 13 return $cache_description;
466 }
467  
468 # -----------------------------------------------------------------------------
469  
470 # store an object in the cache associated with the identifier
471  
472 sub set
473 73 ▶{
Subroutine set: called CC=7 SCAR=19.7
Pod documented
474 73 my ($self, $identifier, $object, $expires_in) = @_;
475  
476 73 ▶ defined($identifier) or
Branch: 1 branch
BranchTrueFalseTotal
defined $identifier or croak("identifier required") 73 0 73
477 croak("identifier required");
478  
479 73 my $unique_key = _BUILD_UNIQUE_KEY($identifier);
480  
481 # expiration time is based on a delta from the current time if
482 # expires_in is defined, the object will expire in that number of
483 # seconds from now else if expires_in is undefined, it will expire
484 # based on the global_expires_in
485  
486 73 my $global_expires_in = $self->get_global_expires_in();
487  
488 73 my $expires_at;
489  
490 73 my $created_at = time();
491  
492 73 ▶ if (defined $expires_in) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $expires_in) { } 9 64 73
elsif ($global_expires_in ne $File::Cache::sEXPIRES_NEVER) { } 0 64 64
493 9 ▶ $expires_at = ($expires_in eq $sEXPIRES_NEVER) ?
Branch: 1 branch
BranchTrueFalseTotal
$expires_in eq $File::Cache::sEXPIRES_NEVER ? : 0 9 9
494 $expires_in : ($created_at + $expires_in);
495 } elsif ($global_expires_in ne $sEXPIRES_NEVER) {
496 0 $expires_at = $created_at + $global_expires_in;
497 } else {
498 64 $expires_at = $sEXPIRES_NEVER;
499 }
500  
501  
502 # add the new object to the cache in this instance's namespace
503  
504 73 my %object_data = ( object => $object, expires_at => $expires_at,
505 created_at => $created_at );
506  
507 73 my $frozen_object_data =
508 _SERIALIZE( \%object_data, $self->get_persistence_mechanism() );
509  
510 # Figure out what the new size of the cache should be in order to
511 # accomodate the new data and still be below the max_size. Then
512 # reduce the size.
513  
514 73 my $max_size = $self->get_max_size();
515  
516 73 ▶ if ($max_size != $sNO_MAX_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_size != $File::Cache::sNO_MAX_SIZE) 70 3 73
517 70 my $new_size = $max_size - length $frozen_object_data;
518 70 ▶ $new_size = 0 if $new_size < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $new_size < 0 0 70 70
519 70 $self->reduce_size($new_size);
520 }
521  
522 73 my $filemode = $self->get_filemode();
523  
524 73 my $cached_file_path = $self->_build_cached_file_path($unique_key);
525  
526 73 _WRITE_FILE($cached_file_path, \$frozen_object_data, $filemode);
527  
528 73 return $sSUCCESS;
529 }
530  
531 # -----------------------------------------------------------------------------
532  
533 # loads the module for serializing data
534  
535 sub _load_persistence_mechanism
536 13 ▶{
Subroutine _load_persistence_mechanism: called CC=3 SCAR=15.0
537 13 my ($self) = @_;
538  
539 13 ▶ if ($self->get_persistence_mechanism() eq 'Storable')
Branch: 2 branches
BranchTrueFalseTotal
if ($self->get_persistence_mechanism eq 'Storable') { } 13 0 13
elsif ($self->get_persistence_mechanism eq 'Data::Dumper') { } 0 0 0
540 {
541 13 require Storable;
542 13 Storable->import( qw(nfreeze thaw dclone));
543 }
544 # Should be already loaded. No harm done in doing it again
545 elsif ($self->get_persistence_mechanism() eq 'Data::Dumper')
546 {
547 0 require Data::Dumper;
548 0 Data::Dumper->import();
549 }
550 # An invalid persistence mechanism choice by the user has already been
551 # checked. If we see an invalid choice here it must be a bug in
552 # the module. (die in this case instead of croaking)
553 else
554 {
555 0 croak("Argument must be either \"Storable\" or \"Data::Dumper\"");
556 }
557 }
558  
559 # ------------------------------------------------------------------------------
560  
561 # turns a hash reference into a serialized string using a method which
562 # depends on the persistence mechanism choice
563  
564 sub _SERIALIZE
565 73 ▶{
Subroutine _SERIALIZE: called CC=4 SCAR=15.2
566 73 my ($data_reference, $persistence_mechanism) = @_;
567  
568 73 ▶ defined($data_reference) or
Branch: 1 branch
BranchTrueFalseTotal
defined $data_reference or croak("object reference required") 73 0 73
569 croak("object reference required");
570  
571 73 ▶ defined($persistence_mechanism) or
Branch: 1 branch
BranchTrueFalseTotal
defined $persistence_mechanism or croak("persistence mechanism required") 73 0 73
572 croak("persistence mechanism required");
573  
574 73 ▶ if ($persistence_mechanism eq 'Storable')
Branch: 1 branch
BranchTrueFalseTotal
if ($persistence_mechanism eq 'Storable') { } 73 0 73
575 {
576 73 return nfreeze($data_reference);
577 }
578 else
579 {
580 0 return Data::Dumper->Dump([$data_reference], ['cache_object']);
581 }
582 }
583  
584 # ------------------------------------------------------------------------------
585  
586 # turns a reference to a serialized string into a reference to data using
587 # a method which depends on the persistence mechanism choice. Deletes the
588 # cache key if the unserialization fails.
589  
590 sub _UNSERIALIZE
591 314 ▶{
Subroutine _UNSERIALIZE: called CC=6 SCAR=28.9
592 314 my ($data_reference, $persistence_mechanism, $cache_key) = @_;
593  
594 314 ▶ defined($data_reference) or
Branch: 1 branch
BranchTrueFalseTotal
defined $data_reference or croak("object reference required") 314 0 314
595 croak("object reference required");
596  
597 314 ▶ defined($persistence_mechanism) or
Branch: 1 branch
BranchTrueFalseTotal
defined $persistence_mechanism or croak("persistence mechanism required") 314 0 314
598 croak("persistence mechanism required");
599  
600 314 ▶ if ($persistence_mechanism eq 'Storable')
Branch: 1 branch
BranchTrueFalseTotal
if ($persistence_mechanism eq 'Storable') { } 314 0 314
601 {
602 314 return thaw($$data_reference);
603 }
604 else
605 {
606 # This is what the serialize routine calls the cached object
607 0 my $cache_object;
608  
609 my $errors;
610 {
611 0 ▶ local $SIG{__WARN__} = sub { $errors .= $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
612  
613 0 eval $$data_reference;
614 }
615  
616 0 ▶ if ($errors || $@)
Branch: 1 branch
BranchTrueFalseTotal
if ($errors or $@) 0 0 0
Condition0%0/3
$errors or $@
l!l&&r!l&&!r
000
MC/DC0%0/2
$errors or $@
$errors $@
Truth table0%0/3
A or B
A= $errorsB= $@
ABresult
1X1
011
000
617 {
618 0 warn "Cache object is corrupted and will be deleted";
619 0 unlink $cache_key;
620 0 return undef;
621 }
622  
623 0 return $cache_object;
624 }
625 }
626  
627 # ------------------------------------------------------------------------------
628  
629 # return a copy of a serialized string (reference or non-reference)
630 # using a method which depends on the persistence mechanism choice
631  
632 sub _CLONE
633 29 ▶{
Subroutine _CLONE: called CC=6 SCAR=21.8
634 29 my ($data_reference, $persistence_mechanism) = @_;
635  
636 29 ▶ defined($data_reference) or
Branch: 1 branch
BranchTrueFalseTotal
defined $data_reference or croak("object reference required") 29 0 29
637 croak("object reference required");
638  
639 29 ▶ defined($persistence_mechanism) or
Branch: 1 branch
BranchTrueFalseTotal
defined $persistence_mechanism or croak("persistence mechanism required") 29 0 29
640 croak("persistence mechanism required");
641  
642 29 my $cloned_data;
643  
644 29 ▶ if ($persistence_mechanism eq 'Storable')
Branch: 1 branch
BranchTrueFalseTotal
if ($persistence_mechanism eq 'Storable') { } 29 0 29
645 {
646 29 ▶ $cloned_data =
Branch: 1 branch
BranchTrueFalseTotal
ref $data_reference ? : 5 24 29
647 (ref $data_reference) ? dclone($data_reference) : $data_reference;
648 }
649 else
650 {
651 0 ▶ if (ref $data_reference)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data_reference) { } 0 0 0
652 {
653 0 my $data = $$data_reference;
654 0 $cloned_data = \$data;
655 }
656 else
657 {
658 0 $cloned_data = $data_reference;
659 }
660 }
661  
662 29 return $cloned_data;
663 }
664  
665 # ------------------------------------------------------------------------------
666  
667 # retrieve an object from the cache associated with the identifier,
668 # and remove it from the cache if its expiration has elapsed and
669 # auto_remove_stale is 1.
670  
671 sub get
672 38 ▶{
Subroutine get: called CC=2 SCAR=7.0
Pod documented
673 38 my ($self, $identifier) = @_;
674  
675 38 ▶ defined($identifier) or
Branch: 1 branch
BranchTrueFalseTotal
defined $identifier or croak("identifier required") 38 0 38
676 croak("identifier required");
677  
678 38 my $object = $self->_get($identifier, $sGET_FRESH_ONLY);
679  
680 38 return $object;
681 }
682  
683 # ------------------------------------------------------------------------------
684  
685 # retrieve an object from the cache associated with the identifier,
686 # but only if it's stale
687  
688 sub get_stale
689 3 ▶{
Subroutine get_stale: called CC=2 SCAR=7.0
Pod documented
690 3 my ($self, $identifier) = @_;
691  
692 3 ▶ defined($identifier) or
Branch: 1 branch
BranchTrueFalseTotal
defined $identifier or croak("identifier required") 3 0 3
693 croak("identifier required");
694  
695 3 my $object = $self->_get($identifier, $sGET_STALE_ONLY);
696  
697 3 return $object;
698 }
699  
700 # ------------------------------------------------------------------------------
701  
702 # Gets the stale or non-stale data from the cache, depending on the
703 # second parameter ($sGET_STALE_ONLY or $sGET_FRESH_ONLY)
704  
705 sub _get
706 41 ▶{
Subroutine _get: called CC=9 SCAR=22.6
707 41 my ($self, $identifier, $freshness) = @_;
708  
709 41 ▶ defined($identifier) or
Branch: 1 branch
BranchTrueFalseTotal
defined $identifier or croak("identifier required") 41 0 41
710 croak("identifier required");
711  
712 41 ▶ defined($freshness) or
Branch: 1 branch
BranchTrueFalseTotal
defined $freshness or croak("freshness required") 41 0 41
713 croak("freshness required");
714  
715 41 my $unique_key = _BUILD_UNIQUE_KEY($identifier);
716  
717 41 my $cached_file_path = $self->_get_cached_file_path($unique_key);
718  
719 # check the cache for the specified object
720  
721 41 my $cloned_object = undef;
722  
723 41 my $object_data;
724  
725 41 $object_data =
726 _READ_OBJECT_DATA($cached_file_path);
727  
728 41 ▶ if ($object_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object_data) 35 6 41
729  
730 35 my $object = $object_data->{object};
731  
732 35 my $expires_at = $object_data->{expires_at};
733
734 # If we want non-stale data...
735  
736 35 ▶ if ($freshness eq $sGET_FRESH_ONLY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($freshness eq $File::Cache::sGET_FRESH_ONLY) { } 32 3 35
737  
738 # Check if the cache item has expired
739  
740 32 ▶ if (_S_SHOULD_EXPIRE($expires_at)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_S_SHOULD_EXPIRE($expires_at)) { } 6 26 32
741  
742 # Remove the item from the cache if auto_remove_stale
743 # is $sTRUE
744  
745 6 my $auto_remove_stale = $self->get_auto_remove_stale();
746
747 6 ▶ if ($auto_remove_stale eq $sTRUE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auto_remove_stale eq $File::Cache::sTRUE) 0 6 6
748 0 ▶ _REMOVE_CACHED_FILE($cached_file_path) or
Branch: 1 branch
BranchTrueFalseTotal
_REMOVE_CACHED_FILE($cached_file_path) or croak("Couldn't remove cached file $cached_file_path") 0 0 0
749 croak("Couldn't remove cached file $cached_file_path");
750 }
751  
752 # otherwise fetch the object and return a copy
753  
754 } else {
755 26 $cloned_object =
756 _CLONE( $object, $self->get_persistence_mechanism() );
757 }
758  
759 # If we want stale data...
760  
761 } else {
762
763 # and the cache item is indeed stale...
764  
765 3 ▶ if (_S_SHOULD_EXPIRE($expires_at)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_S_SHOULD_EXPIRE($expires_at)) 3 0 3
766
767 # fetch the object and return a copy
768 3 $cloned_object =
769 _CLONE( $object, $self->get_persistence_mechanism() );
770  
771 }
772 }
773 }
774  
775 41 return $cloned_object;
776 }
777  
778 # ------------------------------------------------------------------------------
779  
780 # removes a key and value from the cache, it always succeeds, even if
781 # the key or value doesn't exist
782  
783 sub remove
784 3 ▶{
Subroutine remove: called CC=3 SCAR=11.2
Pod documented
785 3 my ($self, $identifier) = @_;
786  
787 3 ▶ defined($identifier) or
Branch: 1 branch
BranchTrueFalseTotal
defined $identifier or croak("identifier required") 3 0 3
788 croak("identifier required");
789  
790 3 my $unique_key = _BUILD_UNIQUE_KEY($identifier);
791  
792 3 my $cached_file_path = $self->_get_cached_file_path($unique_key);
793  
794 3 ▶ _REMOVE_CACHED_FILE($cached_file_path) or
Branch: 1 branch
BranchTrueFalseTotal
_REMOVE_CACHED_FILE($cached_file_path) or croak("couldn't remove cached file $cached_file_path") 3 0 3
795 croak("couldn't remove cached file $cached_file_path");
796  
797 3 return $sSUCCESS;
798 }
799  
800 # ------------------------------------------------------------------------------
801  
802 # take an human readable identifier, and create a unique key from it
803  
804 sub _BUILD_UNIQUE_KEY
805 126 ▶{
Subroutine _BUILD_UNIQUE_KEY: called CC=3 SCAR=11.4
806 126 my ($identifier) = @_;
807  
808 126 ▶ defined($identifier) or
Branch: 1 branch
BranchTrueFalseTotal
defined $identifier or croak("identifier required") 126 0 126
809 croak("identifier required");
810  
811 126 ▶ my $unique_key = md5_hex($identifier) or
Branch: 1 branch
BranchTrueFalseTotal
my $unique_key = md5_hex($identifier) or croak("couldn't build unique key for identifier $identifier") 126 0 126
812 croak("couldn't build unique key for identifier $identifier");
813  
814 126 return $unique_key;
815 }
816  
817 # ------------------------------------------------------------------------------
818  
819 # Check to see if a directory exists and is writable, or if a prefix
820 # directory exists and we can write to it in order to create
821 # subdirectories. _VERIFY_DIRECTORY( $self->_get_namespace_path() )
822 # == $sSUCCESS should be checked every time the cache key, username,
823 # or namespace is changed.
824  
825 sub _VERIFY_DIRECTORY
826 13 ▶{
Subroutine _VERIFY_DIRECTORY: called CC=7 SCAR=21.8
827 13 my ($directory) = @_;
828  
829 13 ▶ defined($directory) or
Branch: 1 branch
BranchTrueFalseTotal
defined $directory or croak("directory required") 13 0 13
830 croak("directory required");
831  
832 # If the directory doesn't exist, crawl upwards until we find a file or
833 # directory that exists
834 13 ▶ while (defined $directory && !-e $directory)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $directory and not -e $directory) 33 13 46
Condition66%2/3
defined $directory and not -e $directory
!ll&&!rl&&r
01333
MC/DC50%1/2
defined $directory and not -e $directory
defined $directory not -e $directory
Truth table66%2/3
A and not(B)
A= defined $directoryB= not -e $directory
ABresult
0X0
100
111
835 {
836 33 $directory = _GET_PARENT_DIRECTORY($directory);
837 }
838  
839 13 ▶ return $sFAILURE unless defined $directory;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $directory 0 13 13
840  
841 13 ▶ return $sSUCCESS if -d $directory && -w $directory;
Branch: 1 branch
BranchTrueFalseTotal
if -d $directory and -w $directory 13 0 13
Condition33%1/3
-d $directory and -w $directory
!ll&&!rl&&r
0013
MC/DC0%0/2
-d $directory and -w $directory
-d $directory -w $directory
Truth table33%1/3
A and B
A= -d $directoryB= -w $directory
ABresult
0X0
100
111
842  
843 0 return $sFAILURE;
844 }
845  
846 # ------------------------------------------------------------------------------
847  
848 # find the parent directory of a directory. Returns undef if there is no
849 # parent
850  
851 sub _GET_PARENT_DIRECTORY
852 1981 ▶{
Subroutine _GET_PARENT_DIRECTORY: called CC=3 SCAR=11.2
853 1981 my ($directory) = @_;
854  
855 1981 ▶ defined($directory) or
Branch: 1 branch
BranchTrueFalseTotal
defined $directory or croak("directory required") 1981 0 1981
856 croak("directory required");
857  
858 1981 my @directories = splitdir($directory);
859 1981 pop @directories;
860  
861 1981 ▶ return undef unless @directories;
Branch: 1 branch
BranchTrueFalseTotal
unless @directories 0 1981 1981
862  
863 1981 return catdir(@directories);
864 }
865  
866 # -----------------------------------------------------------------------------
867  
868 # create a directory with optional mask, building subdirectories as needed. be
869 # sure to call _VERIFY_DIRECTORY before calling this function
870  
871 sub _CREATE_DIRECTORY
872 86 ▶{
Subroutine _CREATE_DIRECTORY: called CC=5 SCAR=16.6
873 86 my ($directory, $mask) = @_;
874  
875 86 ▶ defined($directory) or
Branch: 1 branch
BranchTrueFalseTotal
defined $directory or croak("directory required") 86 0 86
876 croak("directory required");
877  
878 86 my $old_mask;
879  
880 86 ▶ if (defined $mask)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mask) 86 0 86
881 {
882 86 $old_mask = umask;
883 86 umask($mask);
884 }
885  
886 86 $directory = _UNTAINT_FILE_PATH($directory);
887  
888 86 mkpath ($directory, 0, 0777);
889  
890 86 ▶ croak("Couldn't create directory: $directory: $!")
Branch: 1 branch
BranchTrueFalseTotal
unless -d $directory 0 86 86
891 unless -d $directory;
892  
893 86 ▶ umask($old_mask) if defined $mask;
Branch: 1 branch
BranchTrueFalseTotal
if defined $mask 86 0 86
894  
895 86 return $sSUCCESS;
896 }
897  
898 # -----------------------------------------------------------------------------
899  
900 # read in the object frozen in the specified file (absolute path).
901 # returns a reference to the object, or undef if the object can not be
902 # found or can not be unserialized
903  
904 sub _READ_OBJECT_DATA
905 320 ▶{
Subroutine _READ_OBJECT_DATA: called CC=6 SCAR=18.4
906 320 my ($cached_file_path) = @_;
907  
908 320 ▶ defined($cached_file_path) or
Branch: 1 branch
BranchTrueFalseTotal
defined $cached_file_path or croak("cached file path required") 320 0 320
909 croak("cached file path required");
910  
911 320 my $frozen_object_data = undef;
912  
913 320 ▶ if (-f $cached_file_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $cached_file_path) { } 314 6 320
914 314 $frozen_object_data = _READ_FILE($cached_file_path);
915 } else {
916 6 return;
917 }
918  
919 314 ▶ if (!$frozen_object_data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($frozen_object_data) 0 314 314
920 0 return;
921 }
922  
923  
924 # Get the cache persistence mechanism. Searching upwards for the cache
925 # description file is a bit of a hack, but it's much better than
926 # passing the persistence mechanism value through the call chain.
927  
928 314 my $cache_key = _SEARCH_FOR_CACHE_KEY($cached_file_path);
929  
930 314 ▶ die "Couldn't find cache key directory"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cache_key 0 314 314
931 unless defined $cache_key;
932  
933 314 my $cache_description = _READ_CACHE_DESCRIPTION( $cache_key );
934  
935 314 ▶ return undef unless defined $cache_description;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cache_description 0 314 314
936  
937 # if the $frozed_object_data is corrupted, thaw will return undef
938 my $thawed_data = _UNSERIALIZE( $frozen_object_data,
939 314 $cache_description->{'Persistence Mechanism'}, $cache_key );
940  
941  
942 314 return $thawed_data;
943 }
944  
945 # -----------------------------------------------------------------------------
946  
947 # Look up the directory hierarchy for the cache description file,
948 # which is in the cache key directory.
949  
950 sub _SEARCH_FOR_CACHE_KEY
951 314 ▶{
Subroutine _SEARCH_FOR_CACHE_KEY: called CC=5 SCAR=16.4
952 314 my ($directory) = @_;
953  
954 314 ▶ defined($directory) or
Branch: 1 branch
BranchTrueFalseTotal
defined $directory or croak("directory required") 314 0 314
955 croak("directory required");
956  
957 314 my $file = _BUILD_PATH($directory,$sCACHE_DESCRIPTION_FILENAME);
958  
959 # If the cache description file isn't in the current directory,
960 # crawl upwards
961 314 ▶ while (defined $directory && !-e $file)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $directory and not -e $file) 1875 314 2189
Condition66%2/3
defined $directory and not -e $file
!ll&&!rl&&r
03141875
MC/DC50%1/2
defined $directory and not -e $file
defined $directory not -e $file
Truth table66%2/3
A and not(B)
A= defined $directoryB= not -e $file
ABresult
0X0
100
111
962 {
963 1875 $directory = _GET_PARENT_DIRECTORY($directory);
964 1875 ▶ $file = _BUILD_PATH($directory,$sCACHE_DESCRIPTION_FILENAME)
Branch: 1 branch
BranchTrueFalseTotal
if defined $directory 1875 0 1875
965 if defined $directory;
966 }
967  
968 314 return $directory;
969 }
970  
971 # -----------------------------------------------------------------------------
972  
973 # remove an object from the cache
974  
975 sub _REMOVE_CACHED_FILE
976 18 ▶{
Subroutine _REMOVE_CACHED_FILE: called CC=3 SCAR=11.2
977 18 my ($cached_file_path) = @_;
978  
979 18 ▶ defined($cached_file_path) or
Branch: 1 branch
BranchTrueFalseTotal
defined $cached_file_path or croak("cached file path required") 18 0 18
980 croak("cached file path required");
981  
982  
983 # cached_file_path may be tainted
984  
985 18 $cached_file_path = _UNTAINT_FILE_PATH($cached_file_path);
986  
987  
988 # Is there any way to do this atomically?
989  
990 18 ▶ if (-f $cached_file_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $cached_file_path) 18 0 18
991  
992 # We don't catch the error, because this may fail if two
993 # processes are in a race and try to remove the object
994  
995 18 unlink($cached_file_path);
996  
997 }
998  
999 18 return $sSUCCESS;
1000 }
1001  
1002 # -----------------------------------------------------------------------------
1003  
1004 # clear all objects in this instance's namespace
1005  
1006 sub clear
1007 3 ▶{
Subroutine clear: called CC=3 SCAR=11.2
Pod documented
1008 3 my ($self) = @_;
1009  
1010 3 my $namespace_path = $self->_get_namespace_path();
1011  
1012 3 $namespace_path = _UNTAINT_FILE_PATH($namespace_path);
1013  
1014 3 ▶ return $sSUCCESS unless -e $namespace_path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $namespace_path 0 3 3
1015  
1016 3 ▶ _RECURSIVELY_REMOVE_DIRECTORY($namespace_path) or
Branch: 1 branch
BranchTrueFalseTotal
_RECURSIVELY_REMOVE_DIRECTORY($namespace_path) or croak("Couldn't clear namespace: $!") 3 0 3
1017 croak("Couldn't clear namespace: $!");
1018  
1019 3 return $sSUCCESS;
1020 }
1021  
1022  
1023 # -----------------------------------------------------------------------------
1024  
1025 # iterate over all the objects in this instance's namespace and delete
1026 # those that have expired
1027  
1028 sub purge
1029 3 ▶{
Subroutine purge: called CC=1 SCAR=0.0
Pod documented
1030 3 my ($self) = @_;
1031  
1032 3 my $namespace_path = $self->_get_namespace_path();
1033  
1034 3 finddepth(\&_PURGE_FILE_WRAPPER, $namespace_path);
1035  
1036 3 return $sSUCCESS;
1037 }
1038  
1039 # -----------------------------------------------------------------------------
1040  
1041 # used with the Find::Find::find routine, this calls _PURGE_FILE on
1042 # each file found
1043  
1044 sub _PURGE_FILE_WRAPPER
1045 390 ▶{
Subroutine _PURGE_FILE_WRAPPER: called CC=3 SCAR=11.0
1046 390 my $file_path = $File::Find::name;
1047  
1048 390 $file_path = _UNTAINT_FILE_PATH($file_path);
1049  
1050 390 my $file = (splitpath($file_path))[2];
1051  
1052 # Don't purge the cache description file
1053 390 ▶ if (-f $file && $file ne $sCACHE_DESCRIPTION_FILENAME) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file and $file ne $sCACHE_DESCRIPTION_FILENAME) { } 90 300 390
Condition100%3/3
-f $file and $file ne $sCACHE_DESCRIPTION_FILENAME
!ll&&!rl&&r
297390
MC/DC100%2/2
-f $file and $file ne $sCACHE_DESCRIPTION_FILENAME
-f $file $file ne $sCACHE_DESCRIPTION_FILENAME
Truth table100%3/3
A and B
A= -f $fileB= $file ne $sCACHE_DESCRIPTION_FILENAME
ABresult
0X0
100
111
1054 90 _PURGE_FILE($file_path);
1055 } else {
1056 300 return;
1057 }
1058 }
1059  
1060 # -----------------------------------------------------------------------------
1061  
1062 # if the file specified has expired, remove it from the cache. (path
1063 # is absolute)
1064  
1065 sub _PURGE_FILE
1066 90 ▶{
Subroutine _PURGE_FILE: called CC=5 SCAR=16.4
1067 90 my ($cached_file_path) = @_;
1068  
1069 90 ▶ defined($cached_file_path) or
Branch: 1 branch
BranchTrueFalseTotal
defined $cached_file_path or croak("cached file path required") 90 0 90
1070 croak("cached file path required");
1071  
1072 90 my $object_data = _READ_OBJECT_DATA($cached_file_path);
1073  
1074 90 ▶ if ($object_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object_data) 90 0 90
1075
1076 90 my $expires_at = $object_data->{expires_at};
1077  
1078 90 ▶ if (_S_SHOULD_EXPIRE($expires_at)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_S_SHOULD_EXPIRE($expires_at)) 3 87 90
1079 3 ▶ _REMOVE_CACHED_FILE($cached_file_path) or
Branch: 1 branch
BranchTrueFalseTotal
_REMOVE_CACHED_FILE($cached_file_path) or croak("Couldn't remove cached file $cached_file_path") 3 0 3
1080 croak("Couldn't remove cached file $cached_file_path");
1081 }
1082
1083 }
1084  
1085 90 return $sSUCCESS;
1086 }
1087  
1088 # -----------------------------------------------------------------------------
1089  
1090 # determine whether an object should expire
1091  
1092 sub _S_SHOULD_EXPIRE
1093 125 ▶{
Subroutine _S_SHOULD_EXPIRE: called CC=6 SCAR=18.6
1094 125 my ($expires_at, $time) = @_;
1095  
1096 125 ▶ defined($expires_at) or
Branch: 1 branch
BranchTrueFalseTotal
defined $expires_at or croak("expires_at required") 125 0 125
1097 croak("expires_at required");
1098  
1099 # time is optional
1100  
1101 125 ▶ $time = $time || time();
Condition50%1/2
$time || time
l!l
0125
MC/DC0%0/1
$time || time
$time
Truth table50%1/2
A
A= $time
Aresult
11
00
1102  
1103 125 ▶ if ($expires_at == $sEXPIRES_NOW) {
Branch: 3 branches
BranchTrueFalseTotal
if ($expires_at == $File::Cache::sEXPIRES_NOW) { } 0 125 125
elsif ($expires_at == $File::Cache::sEXPIRES_NEVER) { } 107 18 125
elsif ($time >= $expires_at) { } 12 6 18
1104 0 return $sTRUE;
1105 } elsif ($expires_at == $sEXPIRES_NEVER) {
1106 107 return $sFALSE;
1107 } elsif ($time >= $expires_at) {
1108 12 return $sTRUE;
1109 } else {
1110 6 return $sFALSE;
1111 }
1112 }
1113  
1114 # -----------------------------------------------------------------------------
1115  
1116 # reduce this namespace to a given size. (the size does not count the
1117 # space occupied by the cache description file.)
1118  
1119  
1120 sub reduce_size
1121 75 ▶{
Subroutine reduce_size: called CC=5 SCAR=17.1
Pod documented
1122 75 my ($self, $new_size) = @_;
1123  
1124 75 ▶ $new_size >= 0 or
Branch: 1 branch
BranchTrueFalseTotal
$new_size >= 0 or croak("size >= 0 required") 75 0 75
1125 croak("size >= 0 required");
1126  
1127 75 my $namespace_path = $self->_get_namespace_path();
1128  
1129 75 ▶ while ($self->size() > $new_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->size > $new_size) 12 75 87
1130  
1131 12 my $victim_file = _CHOOSE_VICTIM_FILE($namespace_path);
1132  
1133 12 ▶ if (!$victim_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($victim_file) 0 12 12
1134 0 warn("Couldn't reduce size to $new_size\n");
1135 0 return $sFAILURE;
1136 }
1137  
1138 12 ▶ _REMOVE_CACHED_FILE($victim_file) or
Branch: 1 branch
BranchTrueFalseTotal
_REMOVE_CACHED_FILE($victim_file) or croak("Couldn't remove cached file $victim_file") 12 0 12
1139 croak("Couldn't remove cached file $victim_file");
1140 }
1141  
1142 75 return $sSUCCESS;
1143 }
1144  
1145 # -----------------------------------------------------------------------------
1146  
1147 # reduce the entire cache size to a given size. (the size does not
1148 # count the space occupied by the cache description files.)
1149  
1150 sub REDUCE_SIZE
1151 0 ▶{
Subroutine REDUCE_SIZE: not called CC=6 SCAR=37.4
Pod documented
1152 0 my ($new_size, $cache_key) = @_;
1153  
1154 0 ▶ $new_size >= 0 or
Branch: 1 branch
BranchTrueFalseTotal
$new_size >= 0 or croak("size >= 0 required") 0 0 0
1155 croak("size >= 0 required");
1156  
1157 0 ▶ $cache_key = $cache_key || _BUILD_DEFAULT_CACHE_KEY();
Condition0%0/3
$cache_key || _BUILD_DEFAULT_CACHE_KEY()
l!l&&r!l&&!r
000
MC/DC0%0/2
$cache_key || _BUILD_DEFAULT_CACHE_KEY()
$cache_key _BUILD_DEFAULT_CACHE_KEY()
Truth table0%0/3
A || B
A= $cache_keyB= _BUILD_DEFAULT_CACHE_KEY()
ABresult
1X1
011
000
1158  
1159 0 ▶ while (SIZE() > $new_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (SIZE() > $new_size) 0 0 0
1160
1161 0 my $victim_file = _CHOOSE_VICTIM_FILE($cache_key);
1162
1163 0 ▶ if (!defined($victim_file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $victim_file) 0 0 0
1164 0 warn("Couldn't reduce size to $new_size\n");
1165 0 return $sFAILURE;
1166 }
1167  
1168 0 ▶ _REMOVE_CACHED_FILE($victim_file) or
Branch: 1 branch
BranchTrueFalseTotal
_REMOVE_CACHED_FILE($victim_file) or croak("Couldn't remove cached file $victim_file") 0 0 0
1169 croak("Couldn't remove cached file $victim_file");
1170 }
1171  
1172 0 return $sSUCCESS;
1173 }
1174  
1175 # -----------------------------------------------------------------------------
1176  
1177 # Choose a "victim" cache object to remove starting from the argument
1178 # directory. (This directory should be either the cache key path or
1179 # some subdirectory of it.) The returned file is determined in this
1180 # order: (1) the one with the closest expiration, (2) the least recently
1181 # accessed one, (3) undef if there are no cache files.
1182  
1183 sub _CHOOSE_VICTIM_FILE
1184 12 ▶{
Subroutine _CHOOSE_VICTIM_FILE: called CC=4 SCAR=14.3
1185 12 my ($root_directory) = @_;
1186  
1187 12 ▶ defined($root_directory) or
Branch: 1 branch
BranchTrueFalseTotal
defined $root_directory or croak("root directory required") 12 0 12
1188 croak("root directory required");
1189  
1190 # Look for the file to delete with the nearest expiration
1191  
1192 12 my ($nearest_expiration_path, $nearest_expiration_time) =
1193 _RECURSIVE_FIND_NEAREST_EXPIRATION($root_directory);
1194  
1195 12 ▶ return $nearest_expiration_path if defined $nearest_expiration_path;
Branch: 1 branch
BranchTrueFalseTotal
if defined $nearest_expiration_path 6 6 12
1196  
1197 # If there are no files with expirations, get the least recently
1198 # accessed one
1199  
1200 6 my ($latest_accessed_path, $latest_accessed_time) =
1201 _RECURSIVE_FIND_LATEST_ACCESSED($root_directory);
1202  
1203 6 ▶ return $latest_accessed_path if defined $latest_accessed_path;
Branch: 1 branch
BranchTrueFalseTotal
if defined $latest_accessed_path 6 0 6
1204  
1205 0 return undef;
1206 }
1207  
1208 # -----------------------------------------------------------------------------
1209  
1210 # Recursively searches a cache namespace for the cache object with the
1211 # nearest expiration. Returns undef if no cache object with an
1212 # expiration time could be found.
1213  
1214 sub _RECURSIVE_FIND_NEAREST_EXPIRATION
1215 552 ▶{
Subroutine _RECURSIVE_FIND_NEAREST_EXPIRATION: called CC=12 SCAR=25.1
1216 552 my ($directory) = @_;
1217  
1218 552 ▶ defined($directory) or
Branch: 1 branch
BranchTrueFalseTotal
defined $directory or croak("directory required") 552 0 552
1219 croak("directory required");
1220  
1221 552 my $best_nearest_expiration_path = undef;
1222  
1223 552 my $best_nearest_expiration_time = undef;
1224  
1225 552 $directory = _UNTAINT_FILE_PATH($directory);
1226  
1227 552 ▶ opendir(DIR, $directory) or
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $directory or croak("Couldn't open directory ${directory}: $!") 552 0 552
1228 croak("Couldn't open directory $directory: $!");
1229  
1230 552 my @dirents = readdir(DIR);
1231  
1232 552 foreach my $dirent (@dirents) {
1233  
1234 1824 ▶ next if $dirent eq '.' or $dirent eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $dirent eq "." or $dirent eq ".." 1104 720 1824
Condition100%3/3
$dirent eq "." or $dirent eq ".."
l!l&&r!l&&!r
552552720
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
1235  
1236 720 my $nearest_expiration_path_candidate = undef;
1237  
1238 720 my $nearest_expiration_time_candidate = undef;
1239  
1240 720 my $path = _BUILD_PATH($directory, $dirent);
1241  
1242 720 ▶ if (-d $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) { } 540 180 720
1243  
1244 540 ($nearest_expiration_path_candidate,
1245 $nearest_expiration_time_candidate) =
1246 _RECURSIVE_FIND_NEAREST_EXPIRATION($path);
1247  
1248 } else {
1249  
1250 180 my $object_data;
1251  
1252 180 $object_data = _READ_OBJECT_DATA_WITHOUT_MODIFICATION($path);
1253
1254 180 my $expires_at = $object_data->{expires_at};
1255  
1256 180 $nearest_expiration_path_candidate = $path;
1257  
1258 180 $nearest_expiration_time_candidate = $expires_at;
1259  
1260 }
1261  
1262
1263 720 ▶ next unless defined $nearest_expiration_path_candidate;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nearest_expiration_path_candidate 522 198 720
1264  
1265 198 ▶ next unless defined $nearest_expiration_time_candidate;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nearest_expiration_time_candidate 0 198 198
1266  
1267 # Skip this file if it doesn't have an expiration time.
1268  
1269 198 ▶ next if $nearest_expiration_time_candidate == $sEXPIRES_NEVER;
Branch: 1 branch
BranchTrueFalseTotal
if $nearest_expiration_time_candidate == $File::Cache::sEXPIRES_NEVER 174 24 198
1270  
1271 # if this is the first candidate, they're automatically the
1272 # best, otherwise they have to beat the best
1273  
1274 24 ▶ if ((!defined $best_nearest_expiration_time) or
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $best_nearest_expiration_time or $best_nearest_expiration_time > $nearest_expiration_time_candidate) 24 0 24
Condition33%1/3
not defined $best_nearest_expiration_time or $best_nearest_expiration_time > $nearest_expiration_time_candidate
l!l&&r!l&&!r
2400
MC/DC0%0/2
not defined $best_nearest_expiration_time or $best_nearest_expiration_time > $nearest_expiration_time_candidate
not defined $best_nearest_expiration_time $best_nearest_expiration_time > $nearest_expiration_time_candidate
Truth table33%1/3
not(A) or B
A= not defined $best_nearest_expiration_timeB= $best_nearest_expiration_time > $nearest_expiration_time_candidate
ABresult
1X1
011
000
1275 ($best_nearest_expiration_time >
1276 $nearest_expiration_time_candidate)) {
1277  
1278 24 $best_nearest_expiration_path =
1279 $nearest_expiration_path_candidate;
1280  
1281 24 $best_nearest_expiration_time =
1282 $nearest_expiration_time_candidate;
1283 }
1284  
1285 }
1286  
1287 552 closedir(DIR);
1288  
1289 552 return ($best_nearest_expiration_path, $best_nearest_expiration_time);
1290 }
1291  
1292 # -----------------------------------------------------------------------------
1293  
1294 # read in object data without modifying the access time. returns a
1295 # reference to the object, or undef if the object could not be read
1296  
1297 sub _READ_OBJECT_DATA_WITHOUT_MODIFICATION
1298 180 ▶{
Subroutine _READ_OBJECT_DATA_WITHOUT_MODIFICATION: called CC=2 SCAR=7.0
1299 180 my ($path) = @_;
1300  
1301 180 ▶ defined($path) or
Branch: 1 branch
BranchTrueFalseTotal
defined $path or croak("path required") 180 0 180
1302 croak("path required");
1303  
1304 180 $path = _UNTAINT_FILE_PATH($path);
1305  
1306 180 my ($file_access_time, $file_modified_time) = (stat($path))[8,9];
1307  
1308 180 my $object_data_ref = _READ_OBJECT_DATA($path);
1309
1310 180 utime($file_access_time, $file_modified_time, $path);
1311  
1312 180 return $object_data_ref;
1313 }
1314  
1315 # -----------------------------------------------------------------------------
1316  
1317 # Recursively searches a cache namespace for the cache object with the
1318 # latest access time. Recursively searches for the file with the
1319 # latest access time, starting at the directory supplied as an
1320 # argument. Returns the path to the last accessed file and the last
1321 # accessed time. Returns (undef,undef) if there is not at least one
1322 # file in the directory hierarchy below and including the argument
1323 # directory.
1324  
1325 sub _RECURSIVE_FIND_LATEST_ACCESSED
1326 261 ▶{
Subroutine _RECURSIVE_FIND_LATEST_ACCESSED: called CC=12 SCAR=25.0
1327 261 my ($directory) = @_;
1328  
1329 261 ▶ defined($directory) or
Branch: 1 branch
BranchTrueFalseTotal
defined $directory or croak("directory required") 261 0 261
1330 croak("directory required");
1331  
1332 261 my $best_latest_accessed_path = undef;
1333  
1334 261 my $best_latest_accessed_time = undef;
1335  
1336 261 $directory = _UNTAINT_FILE_PATH($directory);
1337  
1338 261 ▶ opendir(DIR, $directory) or
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $directory or croak("Couldn't open directory ${directory}: $!") 261 0 261
1339 croak("Couldn't open directory $directory: $!");
1340  
1341 261 my @dirents = readdir(DIR);
1342  
1343 261 foreach my $dirent (@dirents) {
1344  
1345 867 ▶ next if $dirent eq '.' or $dirent eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $dirent eq "." or $dirent eq ".." 522 345 867
Condition100%3/3
$dirent eq "." or $dirent eq ".."
l!l&&r!l&&!r
261261345
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
1346 345 ▶ next if $dirent eq $sCACHE_DESCRIPTION_FILENAME;
Branch: 1 branch
BranchTrueFalseTotal
if $dirent eq $sCACHE_DESCRIPTION_FILENAME 0 345 345
1347  
1348 345 my $latest_accessed_path_candidate = undef;
1349  
1350 345 my $latest_accessed_time_candidate = undef;
1351  
1352 345 my $path = _BUILD_PATH($directory, $dirent);
1353  
1354 345 ▶ if (-d $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) { } 255 90 345
1355  
1356 255 ($latest_accessed_path_candidate,
1357 $latest_accessed_time_candidate) =
1358 _RECURSIVE_FIND_LATEST_ACCESSED($path);
1359  
1360 } else {
1361  
1362 90 my $last_accessed_time = (stat($path))[8];
1363  
1364 90 $latest_accessed_path_candidate = $path;
1365  
1366 90 $latest_accessed_time_candidate = $last_accessed_time;
1367  
1368 }
1369  
1370 345 ▶ next unless defined $latest_accessed_path_candidate;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $latest_accessed_path_candidate 9 336 345
1371  
1372 336 ▶ next unless defined $latest_accessed_time_candidate;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $latest_accessed_time_candidate 0 336 336
1373  
1374 # if this is the first candidate, they're automatically the
1375 # best, otherwise they have to beat the best
1376  
1377 336 ▶ if ((!defined $best_latest_accessed_time) or
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $best_latest_accessed_time or $best_latest_accessed_time > $latest_accessed_time_candidate) 252 84 336
Condition66%2/3
not defined $best_latest_accessed_time or $best_latest_accessed_time > $latest_accessed_time_candidate
l!l&&r!l&&!r
252084
MC/DC50%1/2
not defined $best_latest_accessed_time or $best_latest_accessed_time > $latest_accessed_time_candidate
not defined $best_latest_accessed_time $best_latest_accessed_time > $latest_accessed_time_candidate
Truth table66%2/3
not(A) or B
A= not defined $best_latest_accessed_timeB= $best_latest_accessed_time > $latest_accessed_time_candidate
ABresult
1X1
011
000
1378 ($best_latest_accessed_time >
1379 $latest_accessed_time_candidate)) {
1380  
1381 252 $best_latest_accessed_path =
1382 $latest_accessed_path_candidate;
1383  
1384 252 $best_latest_accessed_time =
1385 $latest_accessed_time_candidate;
1386  
1387 }
1388 }
1389  
1390 261 closedir(DIR);
1391  
1392 261 return ($best_latest_accessed_path, $best_latest_accessed_time);
1393 }
1394  
1395 # -----------------------------------------------------------------------------
1396  
1397 # recursively descend to get an estimate of the memory consumption for
1398 # this namespace, ignoring space occupied by the cache description
1399 # file. returns 0 if the cache doesn't appear to exist
1400  
1401 sub size
1402 141 ▶{
Subroutine size: called CC=2 SCAR=6.9
Pod documented
1403 141 my ($self) = @_;
1404  
1405 141 my $namespace_path = $self->_get_namespace_path();
1406  
1407 141 ▶ return 0 unless -e $namespace_path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $namespace_path 16 125 141
1408  
1409 125 return _RECURSIVE_DIRECTORY_SIZE($namespace_path);
1410 }
1411  
1412 # -----------------------------------------------------------------------------
1413  
1414 # find the path to the cached file, taking into account the identifier and
1415 # namespace.
1416  
1417 sub _get_cached_file_path
1418 126 ▶{
Subroutine _get_cached_file_path: called CC=3 SCAR=11.0
1419 126 my ($self,$unique_key) = @_;
1420  
1421 126 ▶ defined($unique_key) or
Branch: 1 branch
BranchTrueFalseTotal
defined $unique_key or croak("unique key required") 126 0 126
1422 croak("unique key required");
1423  
1424 126 my $namespace_path = $self->_get_namespace_path();
1425  
1426 126 my $cache_depth = $self->get_cache_depth();
1427  
1428 126 my (@path_prefix) = _EXTRACT_PATH_PREFIX($unique_key, $cache_depth);
1429  
1430 126 my $cached_file_path = _BUILD_PATH($namespace_path);
1431  
1432 126 foreach my $path_element (@path_prefix) {
1433  
1434 360 $cached_file_path = _BUILD_PATH($cached_file_path, $path_element);
1435  
1436 }
1437  
1438 126 $cached_file_path = _BUILD_PATH($cached_file_path, $unique_key);
1439  
1440 126 return $cached_file_path;
1441 }
1442  
1443 # -----------------------------------------------------------------------------
1444  
1445 # build the path to the cached file in the file system, taking into account
1446 # the identifier, namespace, and cache depth.
1447  
1448 sub _build_cached_file_path
1449 73 ▶{
Subroutine _build_cached_file_path: called CC=2 SCAR=7.0
1450 73 my ($self,$unique_key) = @_;
1451  
1452 73 ▶ defined($unique_key) or
Branch: 1 branch
BranchTrueFalseTotal
defined $unique_key or croak("unique key required") 73 0 73
1453 croak("unique key required");
1454  
1455 73 my $cached_file_path = $self->_get_cached_file_path($unique_key);
1456  
1457 # $cached_file_path has the directory & file. remove the file.
1458 73 my $cached_file_directory = _GET_PARENT_DIRECTORY($cached_file_path);
1459  
1460 73 _CREATE_DIRECTORY($cached_file_directory,0);
1461  
1462 73 return $cached_file_path;
1463 }
1464  
1465 # -----------------------------------------------------------------------------
1466  
1467 # return a list of the first $cache_depth letters in the $identifier
1468  
1469 sub _EXTRACT_PATH_PREFIX
1470 126 ▶{
Subroutine _EXTRACT_PATH_PREFIX: called CC=4 SCAR=14.0
1471 126 my ($unique_key, $cache_depth) = @_;
1472  
1473 126 ▶ defined($unique_key) or
Branch: 1 branch
BranchTrueFalseTotal
defined $unique_key or croak("unique key required") 126 0 126
1474 croak("unique key required");
1475  
1476 126 ▶ defined($cache_depth) or
Branch: 1 branch
BranchTrueFalseTotal
defined $cache_depth or croak("cache depth required") 126 0 126
1477 croak("cache depth required");
1478  
1479 126 my @path_prefix;
1480  
1481 126 ▶ for (my $i = 0; $i < $cache_depth; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $cache_depth) 360 126 486
1482 360 push (@path_prefix, substr($unique_key, $i, 1));
1483 }
1484  
1485 126 return @path_prefix;
1486 }
1487  
1488 # -----------------------------------------------------------------------------
1489  
1490 # represent a path in canonical form, and check for illegal characters
1491  
1492 sub _BUILD_PATH
1493 9310 ▶{
Subroutine _BUILD_PATH: called CC=2 SCAR=7.4
1494 9310 my (@elements) = @_;
1495  
1496 9310 ▶ if (grep (/\.\./, @elements)) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /\.\./, @elements) 0 9310 9310
1497 0 croak("Illegal path characters ..");
1498 }
1499  
1500 9310 my $path = File::Spec->catfile(@elements);
1501  
1502 9310 return $path;
1503 }
1504  
1505 # -----------------------------------------------------------------------------
1506  
1507 # read in a file. returns a reference to the data read
1508  
1509 sub _READ_FILE
1510 630 ▶{
Subroutine _READ_FILE: called CC=3 SCAR=11.1
1511 630 my ($filename) = @_;
1512  
1513 630 my $data_ref;
1514  
1515 630 ▶ defined($filename) or
Branch: 1 branch
BranchTrueFalseTotal
defined $filename or croak("filename required") 630 0 630
1516 croak("filename required");
1517  
1518 630 $filename = _UNTAINT_FILE_PATH($filename);
1519  
1520 630 ▶ open(FILE, $filename) or
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $filename or croak("Couldn't open $filename for reading: $!") 630 0 630
1521 croak("Couldn't open $filename for reading: $!");
1522  
1523 # In case the user stores binary data
1524 630 binmode FILE;
1525  
1526 630 local $/ = undef;
1527  
1528 630 $$data_ref = <FILE>;
1529  
1530 630 close(FILE);
1531  
1532 630 return $data_ref;
1533 }
1534  
1535 # -----------------------------------------------------------------------------
1536  
1537 # write a file atomically
1538  
1539 sub _WRITE_FILE
1540 86 ▶{
Subroutine _WRITE_FILE: called CC=9 SCAR=22.3
1541 86 my ($filename, $data_ref, $mode, $new_umask) = @_;
1542  
1543 86 ▶ defined($filename) or
Branch: 1 branch
BranchTrueFalseTotal
defined $filename or croak("filename required") 86 0 86
1544 croak("filename required");
1545  
1546 86 ▶ defined($data_ref) or
Branch: 1 branch
BranchTrueFalseTotal
defined $data_ref or croak("data reference required") 86 0 86
1547 croak("data reference required");
1548  
1549 86 ▶ defined($mode) or
Branch: 1 branch
BranchTrueFalseTotal
defined $mode or croak("mode required") 86 0 86
1550 croak("mode required");
1551  
1552 # Prepare the name for taint checking
1553  
1554 86 $filename = _UNTAINT_FILE_PATH($filename);
1555  
1556 # Change the umask if necessary
1557  
1558 86 ▶ my $old_umask = umask if $new_umask;
Branch: 1 branch
BranchTrueFalseTotal
if $new_umask 13 73 86
1559  
1560 86 ▶ umask($new_umask) if $new_umask;
Branch: 1 branch
BranchTrueFalseTotal
if $new_umask 13 73 86
1561  
1562 # Create a temp filename
1563  
1564 86 my $temp_filename = "$filename.tmp$$";
1565  
1566 86 ▶ open(FILE, ">$temp_filename") or
Branch: 1 branch
BranchTrueFalseTotal
open FILE, ">$temp_filename" or croak("Couldn't open $temp_filename for writing: $!\n") 86 0 86
1567 croak("Couldn't open $temp_filename for writing: $!\n");
1568  
1569 # Use binmode in case the user stores binary data
1570  
1571 86 binmode(FILE);
1572  
1573 86 chmod($mode, $filename);
1574  
1575 86 print FILE $$data_ref;
1576  
1577 86 close(FILE);
1578  
1579 86 ▶ rename ($temp_filename, $filename) or
Branch: 1 branch
BranchTrueFalseTotal
rename $temp_filename, $filename or croak("Couldn't rename $temp_filename to $filename") 86 0 86
1580 croak("Couldn't rename $temp_filename to $filename");
1581  
1582 86 ▶ umask($old_umask) if $old_umask;
Branch: 1 branch
BranchTrueFalseTotal
if $old_umask 13 73 86
1583  
1584 86 return $sSUCCESS;
1585 }
1586  
1587 # -----------------------------------------------------------------------------
1588  
1589 # clear all objects in all namespaces
1590  
1591 sub CLEAR
1592 12 ▶{
Subroutine CLEAR: called CC=4 SCAR=14.2
1593 12 my ($cache_key) = @_;
1594  
1595 12 ▶ $cache_key = $cache_key || _BUILD_DEFAULT_CACHE_KEY();
Condition33%1/3
$cache_key || _BUILD_DEFAULT_CACHE_KEY()
l!l&&r!l&&!r
1200
MC/DC0%0/2
$cache_key || _BUILD_DEFAULT_CACHE_KEY()
$cache_key _BUILD_DEFAULT_CACHE_KEY()
Truth table33%1/3
A || B
A= $cache_keyB= _BUILD_DEFAULT_CACHE_KEY()
ABresult
1X1
011
000
1596  
1597 12 ▶ if (!-d $cache_key) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $cache_key) 3 9 12
1598 3 return $sSUCCESS;
1599 }
1600  
1601 # [Should this use the _UNTAINT_FILE_PATH routine?]
1602 9 $cache_key = _UNTAINT_FILE_PATH($cache_key);
1603  
1604 9 ▶ _RECURSIVELY_REMOVE_DIRECTORY($cache_key) or
Branch: 1 branch
BranchTrueFalseTotal
_RECURSIVELY_REMOVE_DIRECTORY($cache_key) or croak("Couldn't clear cache") 9 0 9
1605 croak("Couldn't clear cache");
1606  
1607 9 return $sSUCCESS;
1608 }
1609  
1610 # -----------------------------------------------------------------------------
1611  
1612 # purge all objects in all namespaces that have expired
1613  
1614 sub PURGE
1615 3 ▶{
Subroutine PURGE: called CC=3 SCAR=12.0
Pod documented
1616 3 my ($cache_key) = @_;
1617  
1618 # [Should this use the _UNTAINT_FILE_PATH routine?]
1619 3 $cache_key = _UNTAINT_FILE_PATH($cache_key);
1620  
1621 3 ▶ $cache_key = $cache_key || _BUILD_DEFAULT_CACHE_KEY();
Condition33%1/3
$cache_key || _BUILD_DEFAULT_CACHE_KEY()
l!l&&r!l&&!r
300
MC/DC0%0/2
$cache_key || _BUILD_DEFAULT_CACHE_KEY()
$cache_key _BUILD_DEFAULT_CACHE_KEY()
Truth table33%1/3
A || B
A= $cache_keyB= _BUILD_DEFAULT_CACHE_KEY()
ABresult
1X1
011
000
1622  
1623 3 ▶ if (!-d $cache_key) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $cache_key) 0 3 3
1624 0 return $sSUCCESS;
1625 }
1626  
1627 3 finddepth(\&_PURGE_FILE_WRAPPER, $cache_key);
1628  
1629 3 return $sSUCCESS;
1630 }
1631  
1632 # -----------------------------------------------------------------------------
1633  
1634 # get an estimate of the total memory consumption of the cache,
1635 # ignoring space occupied by cache description files. returns 0 if the
1636 # cache doesn't appear to exist
1637  
1638 sub SIZE
1639 3 ▶{
Subroutine SIZE: called CC=2 SCAR=7.1
Pod documented
1640 3 my ($cache_key) = @_;
1641  
1642 3 ▶ return 0 unless -e $cache_key;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $cache_key 0 3 3
1643  
1644 3 return _RECURSIVE_DIRECTORY_SIZE($cache_key);
1645 }
1646  
1647 # -----------------------------------------------------------------------------
1648  
1649 # walk down a directory structure and total the size of the files
1650 # contained therein. Doesn't count the size of the cache description
1651 # file
1652  
1653 sub _RECURSIVE_DIRECTORY_SIZE
1654 3383 ▶{
Subroutine _RECURSIVE_DIRECTORY_SIZE: called CC=8 SCAR=20.8
1655 3383 my ($directory) = @_;
1656  
1657 3383 ▶ defined($directory) or
Branch: 1 branch
BranchTrueFalseTotal
defined $directory or croak("directory required") 3383 0 3383
1658 croak("directory required");
1659  
1660 3383 my $size = 0;
1661  
1662 3383 $directory = _UNTAINT_FILE_PATH($directory);
1663  
1664 3383 ▶ opendir(DIR, $directory) or
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $directory or croak("Couldn't open directory ${directory}: $!") 3383 0 3383
1665 croak("Couldn't open directory $directory: $!");
1666  
1667 3383 my @dirents = readdir(DIR);
1668  
1669 3383 foreach my $dirent (@dirents) {
1670  
1671 11148 ▶ next if $dirent eq '.' or $dirent eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $dirent eq "." or $dirent eq ".." 6766 4382 11148
Condition100%3/3
$dirent eq "." or $dirent eq ".."
l!l&&r!l&&!r
338333834382
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
1672  
1673 4382 my $path = _BUILD_PATH($directory, $dirent);
1674  
1675 4382 ▶ if (-d $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) { } 3255 1127 4382
1676 3255 $size += _RECURSIVE_DIRECTORY_SIZE($path);
1677 } else {
1678 # Don't count the cache description file
1679 1127 ▶ $size += -s $path if $dirent ne $sCACHE_DESCRIPTION_FILENAME;
Branch: 1 branch
BranchTrueFalseTotal
if $dirent ne $sCACHE_DESCRIPTION_FILENAME 1124 3 1127
1680 }
1681  
1682 }
1683  
1684 3383 closedir(DIR);
1685  
1686 3383 return $size;
1687 }
1688  
1689 # -----------------------------------------------------------------------------
1690  
1691 # Find the username of the person running the process in an OS
1692 # independent way
1693  
1694 sub _FIND_USERNAME
1695 6 ▶{
Subroutine _FIND_USERNAME: called CC=3 SCAR=11.8
1696 6 my ($self) = @_;
1697  
1698 6 my $username;
1699  
1700 6 my $success = eval {
1701 6 my $effective_uid = $>;
1702 6 $username = getpwuid($effective_uid);
1703 };
1704  
1705 6 ▶ if ($success and $username) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success and $username) { } 6 0 6
Condition33%1/3
$success and $username
!ll&&!rl&&r
006
MC/DC0%0/2
$success and $username
$success $username
Truth table33%1/3
A and B
A= $successB= $username
ABresult
0X0
100
111
1706 6 return $username;
1707 } else {
1708 0 return $sDEFAULT_USERNAME;
1709 }
1710 }
1711  
1712 # -----------------------------------------------------------------------------
1713  
1714  
1715 # Untaint a path to a file
1716  
1717 sub _UNTAINT_FILE_PATH
1718 5855 ▶{
Subroutine _UNTAINT_FILE_PATH: called CC=1 SCAR=0.0
1719 5855 my ($file_path) = @_;
1720  
1721 5855 return _UNTAINT_STRING($file_path, $sUNTAINTED_FILE_PATH_REGEX);
1722 }
1723  
1724  
1725  
1726 # Untaint a string
1727  
1728 sub _UNTAINT_STRING
1729 5855 ▶{
Subroutine _UNTAINT_STRING: called CC=5 SCAR=18.4
1730 5855 my ($string, $untainted_regex) = @_;
1731  
1732 5855 ▶ defined($untainted_regex) or
Branch: 1 branch
BranchTrueFalseTotal
defined $untainted_regex or croak("untainted regex required") 5855 0 5855
1733 croak("untainted regex required");
1734  
1735 5855 ▶ defined($string) or
Branch: 1 branch
BranchTrueFalseTotal
defined $string or croak("string required") 5855 0 5855
1736 croak("string required");
1737  
1738 5855 my ($untainted_string) = $string =~ /$untainted_regex/;
1739  
1740 5855 ▶ if (!defined $untainted_string || $untainted_string ne $string) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $untainted_string or $untainted_string ne $string) 0 5855 5855
Condition33%1/3
not defined $untainted_string or $untainted_string ne $string
l!l&&r!l&&!r
005855
MC/DC0%0/2
not defined $untainted_string or $untainted_string ne $string
not defined $untainted_string $untainted_string ne $string
Truth table33%1/3
not(A) or B
A= not defined $untainted_stringB= $untainted_string ne $string
ABresult
1X1
011
000
1741 0 warn("String $string contains possible taint");
1742 }
1743  
1744 5855 return $untainted_string;
1745 }
1746  
1747  
1748 # -----------------------------------------------------------------------------
1749  
1750 # Returns the default root of the cache under the OS dependent temp dir
1751  
1752 sub _BUILD_DEFAULT_CACHE_KEY
1753 0 ▶{
Subroutine _BUILD_DEFAULT_CACHE_KEY: not called CC=2 SCAR=17.9
1754 0 ▶ my $tmpdir = tmpdir() or
Branch: 1 branch
BranchTrueFalseTotal
my $tmpdir = tmpdir() or croak("No tmpdir on this system. Bugs to the authors of File::Spec") 0 0 0
1755 croak("No tmpdir on this system. Bugs to the authors of File::Spec");
1756  
1757 0 my $default_cache_key = _BUILD_PATH($tmpdir, $sDEFAULT_CACHE_KEY);
1758  
1759 0 return $default_cache_key;
1760 }
1761  
1762  
1763 # -----------------------------------------------------------------------------
1764  
1765 # Remove a directory starting at the root
1766  
1767  
1768 sub _RECURSIVELY_REMOVE_DIRECTORY
1769 195 ▶{
Subroutine _RECURSIVELY_REMOVE_DIRECTORY: called CC=10 SCAR=23.5
1770 195 my ($root) = @_;
1771  
1772 195 ▶ -d $root or
Branch: 1 branch
BranchTrueFalseTotal
-d $root or croak("$root is not a directory") 195 0 195
1773 croak("$root is not a directory");
1774  
1775 195 ▶ opendir(DIR, $root) or
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $root or croak("Couldn't open directory ${root}: $!") 195 0 195
1776 croak("Couldn't open directory $root: $!");
1777  
1778 195 my @dirents = readdir(DIR);
1779  
1780 195 ▶ closedir(DIR) or
Branch: 1 branch
BranchTrueFalseTotal
closedir DIR or croak("Couldn't close directory ${root}: $!") 195 0 195
1781 croak("Couldn't close directory $root: $!");
1782  
1783 195 foreach my $dirent (@dirents) {
1784  
1785 633 ▶ next if $dirent eq '.' or $dirent eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $dirent eq "." or $dirent eq ".." 390 243 633
Condition100%3/3
$dirent eq "." or $dirent eq ".."
l!l&&r!l&&!r
195195243
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
1786  
1787 243 my $path_to_dirent = "$root/$dirent";
1788  
1789 243 $path_to_dirent = _UNTAINT_FILE_PATH($path_to_dirent);
1790  
1791 243 ▶ if (-d $path_to_dirent) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path_to_dirent) { } 183 60 243
1792 183 _RECURSIVELY_REMOVE_DIRECTORY($path_to_dirent);
1793 } else {
1794 60 ▶ unlink($path_to_dirent) or
Branch: 1 branch
BranchTrueFalseTotal
unlink $path_to_dirent or croak("Couldn't unlink($path_to_dirent): $!\n") 60 0 60
1795 croak("Couldn't unlink($path_to_dirent): $!\n");
1796 }
1797  
1798 }
1799  
1800 195 ▶ rmdir($root) or
Branch: 1 branch
BranchTrueFalseTotal
rmdir $root or croak("Couldn't rmdir ${root}: $!") 195 0 195
1801 croak("Couldn't rmdir $root: $!");
1802 }
1803  
1804  
1805 # -----------------------------------------------------------------------------
1806  
1807 # Get whether or not we automatically remove stale data from the cache
1808 # on retrieval
1809  
1810 sub get_auto_remove_stale
1811 6 ▶{
Subroutine get_auto_remove_stale: called CC=1 SCAR=0.0
Pod documented
1812 6 my ($self) = @_;
1813  
1814 6 return $self->{_auto_remove_stale};
1815 }
1816  
1817 # -----------------------------------------------------------------------------
1818  
1819 # Set whether or not we automatically remove stale data from the cache
1820 # on retrieval
1821  
1822 sub set_auto_remove_stale
1823 13 ▶{
Subroutine set_auto_remove_stale: called CC=2 SCAR=7.1
Pod documented
1824 13 my ($self, $auto_remove_stale) = @_;
1825  
1826 13 ▶ defined($auto_remove_stale) or
Branch: 1 branch
BranchTrueFalseTotal
defined $auto_remove_stale or croak("\$File::Cache::sTRUE (i.e. 1) or \$File::Cache::sFALSE (i.e. 0) required") 13 0 13
1827 croak("\$File::Cache::sTRUE (i.e. 1) or " .
1828 "\$File::Cache::sFALSE (i.e. 0) required");
1829  
1830 13 $self->{_auto_remove_stale} = $auto_remove_stale;
1831 }
1832  
1833 # -----------------------------------------------------------------------------
1834  
1835 # Get the root of this cache on the filesystem
1836  
1837 sub get_cache_key
1838 374 ▶{
Subroutine get_cache_key: called CC=1 SCAR=0.0
Pod undocumented
1839 374 my ($self) = @_;
1840  
1841 374 my $cache_key = $self->{_cache_key};
1842  
1843 374 return $cache_key;
1844 }
1845  
1846 # -----------------------------------------------------------------------------
1847  
1848 # Set the root of this cache on the filesystem
1849  
1850 sub set_cache_key
1851 13 ▶{
Subroutine set_cache_key: called CC=4 SCAR=17.1
Pod undocumented
1852 13 my ($self, $cache_key) = @_;
1853  
1854 13 ▶ defined($cache_key) or
Branch: 1 branch
BranchTrueFalseTotal
defined $cache_key or croak("cache key required") 13 0 13
1855 croak("cache key required");
1856  
1857 13 $self->{_cache_key} = $cache_key;
1858  
1859 # We don't verify the new directory if this function is called
1860 # during cache creation
1861 13 ▶ if ( (caller(1))[3] ne 'File::Cache::new')
Branch: 1 branch
BranchTrueFalseTotal
if ((caller 1)[3] ne "File::Cache::new") 0 13 13
1862 {
1863 0 ▶ _VERIFY_DIRECTORY( $self->_get_namespace_path() ) == $sSUCCESS or
Branch: 1 branch
BranchTrueFalseTotal
_VERIFY_DIRECTORY($self->_get_namespace_path) == $File::Cache::sSUCCESS or croak('Can not build cache at ' . $self->_get_namespace_path . '. Check directory permissions.') 0 0 0
1864 croak("Can not build cache at " . $self->_get_namespace_path() .
1865 ". Check directory permissions.");
1866 }
1867 }
1868  
1869 # -----------------------------------------------------------------------------
1870  
1871 # Get the root of this user's path
1872  
1873 sub _get_user_path
1874 361 ▶{
Subroutine _get_user_path: called CC=1 SCAR=0.0
1875 361 my ($self) = @_;
1876  
1877 361 my $cache_key = $self->get_cache_key();
1878 361 my $username = $self->get_username();
1879  
1880 361 my $user_path = _BUILD_PATH($cache_key, $username);
1881  
1882 361 return $user_path;
1883 }
1884  
1885 # -----------------------------------------------------------------------------
1886  
1887 # Get the root of this namespace's path
1888  
1889 sub _get_namespace_path
1890 361 ▶{
Subroutine _get_namespace_path: called CC=1 SCAR=0.0
1891 361 my ($self) = @_;
1892  
1893 361 my $user_path = $self->_get_user_path();
1894 361 my $namespace = $self->get_namespace();
1895  
1896 361 my $namespace_path = _BUILD_PATH($user_path, $namespace);
1897  
1898 361 return $namespace_path;
1899 }
1900  
1901 # -----------------------------------------------------------------------------
1902  
1903 # Get the namespace for this cache instance (within the entire cache)
1904  
1905 sub get_namespace
1906 361 ▶{
Subroutine get_namespace: called CC=1 SCAR=0.0
Pod documented
1907 361 my ($self) = @_;
1908  
1909 361 return $self->{_namespace};
1910 }
1911  
1912 # -----------------------------------------------------------------------------
1913  
1914 # Set the namespace for this cache instance (within the entire cache)
1915  
1916 sub set_namespace
1917 13 ▶{
Subroutine set_namespace: called CC=4 SCAR=17.1
Pod documented
1918 13 my ($self, $namespace) = @_;
1919  
1920 13 ▶ defined($namespace) or
Branch: 1 branch
BranchTrueFalseTotal
defined $namespace or croak("namespace required") 13 0 13
1921 croak("namespace required");
1922  
1923 13 $self->{_namespace} = $namespace;
1924  
1925 # We don't verify the new directory if this function is called
1926 # during cache creation
1927 13 ▶ if ( (caller(1))[3] ne 'File::Cache::new')
Branch: 1 branch
BranchTrueFalseTotal
if ((caller 1)[3] ne "File::Cache::new") 0 13 13
1928 {
1929 0 ▶ _VERIFY_DIRECTORY( $self->_get_namespace_path() ) == $sSUCCESS or
Branch: 1 branch
BranchTrueFalseTotal
_VERIFY_DIRECTORY($self->_get_namespace_path) == $File::Cache::sSUCCESS or croak('Can not build cache at ' . $self->_get_namespace_path . '. Check directory permissions.') 0 0 0
1930 croak("Can not build cache at " . $self->_get_namespace_path() .
1931 ". Check directory permissions.");
1932 }
1933 }
1934  
1935 # -----------------------------------------------------------------------------
1936  
1937 # Get the global expiration value for the cache
1938  
1939 sub get_global_expires_in
1940 73 ▶{
Subroutine get_global_expires_in: called CC=1 SCAR=0.0
Pod documented
1941 73 my ($self) = @_;
1942  
1943 73 return $self->{_global_expires_in};
1944 }
1945  
1946 # -----------------------------------------------------------------------------
1947  
1948 # Set the global expiration value for the cache
1949  
1950 sub set_global_expires_in
1951 13 ▶{
Subroutine set_global_expires_in: called CC=4 SCAR=17.1
Pod documented
1952 13 my ($self, $global_expires_in) = @_;
1953  
1954 13 ▶ ($global_expires_in > 0) ||
Branch: 1 branch
BranchTrueFalseTotal
$global_expires_in > 0 or $global_expires_in == $File::Cache::sEXPIRES_NEVER or $global_expires_in == $File::Cache::sEXPIRES_NOW or croak("\$global_expires_in must be > 0,\$sEXPIRES_NOW, or \$sEXPIRES_NEVER") 13 0 13
Condition33%2/6
$global_expires_in > 0 or $global_expires_in == $File::Cache::sEXPIRES_NEVER or $global_expires_in == $File::Cache::sEXPIRES_NOW
l!l&&r!l&&!r
1300
$global_expires_in > 0 or $global_expires_in == $File::Cache::sEXPIRES_NEVER
l!l&&r!l&&!r
0130
MC/DC0%0/3
$global_expires_in > 0 or $global_expires_in == $File::Cache::sEXPIRES_NEVER or $global_expires_in == $File::Cache::sEXPIRES_NOW
$global_expires_in > 0 $global_expires_in == $File::Cache::sEXPIRES_NEVER $global_expires_in == $File::Cache::sEXPIRES_NOW
Truth table25%1/4
A or B or C
A= $global_expires_in > 0B= $global_expires_in == $File::Cache::sEXPIRES_NEVERC= $global_expires_in == $File::Cache::sEXPIRES_NOW
ABCresult
1XX1
01X1
0011
0000
1955 ($global_expires_in == $sEXPIRES_NEVER) ||
1956 ($global_expires_in == $sEXPIRES_NOW) or
1957 croak("\$global_expires_in must be > 0," .
1958 "\$sEXPIRES_NOW, or \$sEXPIRES_NEVER");
1959  
1960 13 $self->{_global_expires_in} = $global_expires_in;
1961 }
1962  
1963 # -----------------------------------------------------------------------------
1964  
1965 # Get the creation time for a cache object. Returns undef if the value
1966 # is not in the cache
1967  
1968 sub get_creation_time
1969 6 ▶{
Subroutine get_creation_time: called CC=2 SCAR=7.1
Pod documented
1970 6 my ($self, $identifier) = @_;
1971  
1972 6 my $unique_key = _BUILD_UNIQUE_KEY($identifier);
1973  
1974 6 my $cached_file_path = $self->_get_cached_file_path($unique_key);
1975  
1976 6 my $object_data;
1977  
1978 6 $object_data =
1979 _READ_OBJECT_DATA($cached_file_path);
1980  
1981 6 ▶ if ($object_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object_data) { } 6 0 6
1982  
1983 6 return $object_data->{created_at};
1984
1985 } else {
1986
1987 0 return undef;
1988
1989 }
1990 }
1991  
1992 # -----------------------------------------------------------------------------
1993  
1994 # Get the expiration time for a cache object. Returns undef if the
1995 # value is not in the cache
1996  
1997 sub get_expiration_time
1998 3 ▶{
Subroutine get_expiration_time: called CC=2 SCAR=7.1
Pod documented
1999 3 my ($self, $identifier) = @_;
2000  
2001 3 my $unique_key = _BUILD_UNIQUE_KEY($identifier);
2002  
2003 3 my $cached_file_path = $self->_get_cached_file_path($unique_key);
2004  
2005 3 my $object_data;
2006  
2007 3 $object_data =
2008 _READ_OBJECT_DATA($cached_file_path);
2009  
2010 3 ▶ if ($object_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object_data) { } 3 0 3
2011
2012 3 return $object_data->{expires_at};
2013
2014 } else {
2015
2016 0 return undef;
2017
2018 }
2019 }
2020  
2021 # -----------------------------------------------------------------------------
2022  
2023 # Get the username associated with this cache
2024  
2025 sub get_username
2026 361 ▶{
Subroutine get_username: called CC=1 SCAR=0.0
Pod documented
2027 361 my ($self) = @_;
2028  
2029 361 return $self->{_username};
2030 }
2031  
2032 # -----------------------------------------------------------------------------
2033  
2034 # Set the username associated with this cache
2035  
2036 sub set_username
2037 13 ▶{
Subroutine set_username: called CC=4 SCAR=17.1
Pod documented
2038 13 my ($self, $username) = @_;
2039  
2040 13 ▶ defined($username) or
Branch: 1 branch
BranchTrueFalseTotal
defined $username or croak("username required") 13 0 13
2041 croak("username required");
2042  
2043 13 $self->{_username} = $username;
2044  
2045 # We don't verify the new directory if this function is called
2046 # during cache creation
2047 13 ▶ if ( (caller(1))[3] ne 'File::Cache::new')
Branch: 1 branch
BranchTrueFalseTotal
if ((caller 1)[3] ne "File::Cache::new") 0 13 13
2048 {
2049 0 ▶ _VERIFY_DIRECTORY( $self->_get_namespace_path() ) == $sSUCCESS or
Branch: 1 branch
BranchTrueFalseTotal
_VERIFY_DIRECTORY($self->_get_namespace_path) == $File::Cache::sSUCCESS or croak('Can not build cache at ' . $self->_get_namespace_path . '. Check directory permissions.') 0 0 0
2050 croak("Can not build cache at " . $self->_get_namespace_path() .
2051 ". Check directory permissions.");
2052 }
2053 }
2054  
2055 # -----------------------------------------------------------------------------
2056  
2057 # Gets the filemode for files created within the cache
2058  
2059 sub get_filemode
2060 73 ▶{
Subroutine get_filemode: called CC=1 SCAR=0.0
Pod documented
2061 73 my ($self) = @_;
2062  
2063 73 return $self->{_filemode};
2064 }
2065  
2066 # -----------------------------------------------------------------------------
2067  
2068 # Sets the filemode for files created within the cache
2069  
2070 sub set_filemode
2071 13 ▶{
Subroutine set_filemode: called CC=2 SCAR=7.1
Pod documented
2072 13 my ($self, $filemode) = @_;
2073  
2074 13 ▶ defined ($filemode) or
Branch: 1 branch
BranchTrueFalseTotal
defined $filemode or croak("filemode required") 13 0 13
2075 croak("filemode required");
2076  
2077 13 $self->{_filemode} = $filemode;
2078 }
2079  
2080 # -----------------------------------------------------------------------------
2081  
2082 # Gets the max cache size.
2083  
2084 sub get_max_size
2085 73 ▶{
Subroutine get_max_size: called CC=1 SCAR=0.0
Pod documented
2086 73 my ($self) = @_;
2087  
2088 73 return $self->{_max_size};
2089 }
2090  
2091 # -----------------------------------------------------------------------------
2092  
2093 # Sets the max cache size.
2094  
2095 sub set_max_size
2096 13 ▶{
Subroutine set_max_size: called CC=4 SCAR=14.0
Pod documented
2097 13 my ($self, $max_size) = @_;
2098  
2099 13 ▶ ($max_size > 0) || ($max_size == $sNO_MAX_SIZE) or
Branch: 1 branch
BranchTrueFalseTotal
$max_size > 0 or $max_size == $File::Cache::sNO_MAX_SIZE or croak("Invalid cache size. Must be either \$sNO_MAX_SIZE or greater than zero") 13 0 13
Condition66%2/3
$max_size > 0 or $max_size == $File::Cache::sNO_MAX_SIZE
l!l&&r!l&&!r
580
MC/DC0%0/2
$max_size > 0 or $max_size == $File::Cache::sNO_MAX_SIZE
$max_size > 0 $max_size == $File::Cache::sNO_MAX_SIZE
Truth table66%2/3
A or B
A= $max_size > 0B= $max_size == $File::Cache::sNO_MAX_SIZE
ABresult
1X1
011
000
2100 croak("Invalid cache size. " .
2101 "Must be either \$sNO_MAX_SIZE or greater than zero");
2102  
2103 13 $self->{_max_size} = $max_size;
2104  
2105 # Reduce the size if necessary.
2106 13 ▶ if ($max_size != $sNO_MAX_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_size != $File::Cache::sNO_MAX_SIZE) 5 8 13
2107 5 $self->reduce_size($max_size);
2108 }
2109 }
2110  
2111 # -----------------------------------------------------------------------------
2112  
2113 # Gets the cache depth
2114  
2115 sub get_cache_depth
2116 126 ▶{
Subroutine get_cache_depth: called CC=1 SCAR=0.0
Pod documented
2117 126 my ($self) = @_;
2118  
2119 126 return $self->{_cache_depth};
2120 }
2121  
2122 # -----------------------------------------------------------------------------
2123  
2124 # Sets the cache depth
2125  
2126 sub set_cache_depth
2127 13 ▶{
Subroutine set_cache_depth: called CC=2 SCAR=7.1
Pod documented
2128 13 my ($self, $cache_depth) = @_;
2129  
2130 13 ▶ ($cache_depth >= 0) or
Branch: 1 branch
BranchTrueFalseTotal
$cache_depth >= 0 or croak("Invalid cache depth. Must be greater than zero") 13 0 13
2131 croak("Invalid cache depth. Must be greater than zero");
2132  
2133 13 $self->{_cache_depth} = $cache_depth;
2134 }
2135  
2136 # -----------------------------------------------------------------------------
2137  
2138 # Gets the persistence mechanism
2139  
2140 sub get_persistence_mechanism
2141 130 ▶{
Subroutine get_persistence_mechanism: called CC=1 SCAR=0.0
Pod documented
2142 130 my ($self) = @_;
2143  
2144 130 return $self->{_persistence_mechanism};
2145 }
2146  
2147 # -----------------------------------------------------------------------------
2148  
2149 # Sets the persistence mechanism.
2150  
2151 sub set_persistence_mechanism
2152 13 ▶{
Subroutine set_persistence_mechanism: called CC=5 SCAR=18.9
Pod documented
2153 13 my ($self, $persistence_mechanism) = @_;
2154  
2155 13 ▶ defined ($persistence_mechanism) or
Branch: 1 branch
BranchTrueFalseTotal
defined $persistence_mechanism or croak("persistence mechanism required") 13 0 13
2156 croak("persistence mechanism required");
2157  
2158 # We don't clear the cache if this function is called during cache
2159 # creation
2160 13 ▶ if ( (caller(1))[3] ne 'File::Cache::new')
Branch: 1 branch
BranchTrueFalseTotal
if ((caller 1)[3] ne "File::Cache::new") 0 13 13
2161 {
2162 0 $self->clear();
2163 }
2164  
2165 13 ▶ ($persistence_mechanism eq 'Storable') ||
Branch: 1 branch
BranchTrueFalseTotal
$persistence_mechanism eq "Storable" or $persistence_mechanism eq "Data::Dumper" or croak("Peristence mechanism must be either SCALAR(0x60839aa1e040)") 13 0 13
Condition33%1/3
$persistence_mechanism eq "Storable" or $persistence_mechanism eq "Data::Dumper"
l!l&&r!l&&!r
1300
MC/DC0%0/2
$persistence_mechanism eq "Storable" or $persistence_mechanism eq "Data::Dumper"
$persistence_mechanism eq "Storable" $persistence_mechanism eq "Data::Dumper"
Truth table33%1/3
A or B
A= $persistence_mechanism eq "Storable"B= $persistence_mechanism eq "Data::Dumper"
ABresult
1X1
011
000
2166 ($persistence_mechanism eq 'Data::Dumper') or
2167 croak("Peristence mechanism must be either " .
2168 \"Storable\" or \"Data::Dumper\"");
2169  
2170 13 $self->{_persistence_mechanism} = $persistence_mechanism;
2171 }
2172  
2173  
2174 3 1;
2175  
2176  
2177 __END__
↑ Summary