blib/lib/File/StatCache.pm

filter: Statementstmt 88.6% 47 / 53 Branchbran 65.0% 13 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.8% 8 / 9 Podpod 100% 2 / 2 totaltotal 83.3% 70 / 84 CC 11 SCAR 15.0
CC
11
Cov
82%
get_stat
7.2
get_item_mtime
6.0
_stat
4.1
CRAP 4.5
↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2006,2007,2009 -- leonerd@leonerd.org.uk
5  
6 package File::StatCache;
7  
8 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 our $VERSION = '0.06';
12  
13 4 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 our @ISA = qw( Exporter );
15 4 our @EXPORT_OK = qw(
16 get_stat
17 stat
18  
19 get_item_mtime
20 );
21  
22 4 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 =head1 NAME
25  
26 C<File::StatCache> - a caching wrapper around the C<stat()> function
27  
28 =head1 DESCRIPTION
29  
30 This module implements a cache of information returned by the C<stat()>
31 function. It stores the result of a C<stat()> syscall, to avoid putting excess
32 load on the host's filesystem in case many calls would be generated.
33  
34 By default the cache for any given filename will time out after 10 seconds; so
35 any request for information on the same name after this time will result in
36 another C<stat()> syscall, ensuring fresh information. This timeout is stored
37 in the package variable C<$File::StatCache::STATTIMEOUT>, and can be
38 modified by other modules if required.
39  
40 =cut
41  
42 4 my %laststattime;
43 my %stat_cache;
44  
45 # Make $STATTIMEOUT externally visible, so other modules change it
46 4 our $STATTIMEOUT = 10;
47  
48 =head1 FUNCTIONS
49  
50 =cut
51  
52 =head2 $stats = get_stat( $path [, $now ] )
53  
54 This function wraps a call to C<File::stat::stat()>, and caches the result. If
55 the requested file was C<stat()>ed within C<$STATTIMEOUT> seconds, it will not
56 be requested again, but the previous result (i.e. an object reference or
57 C<undef>) will be returned.
58  
59 The $now parameter allows some other time than the current time to be used,
60 rather than re-request it from the kernel using the C<time()> function. This
61 allows a succession of tests to be performed in a consistent way, to avoid a
62 race condition.
63  
64 =over 8
65  
66 =item $path
67  
68 The path to the filesystem item to C<stat()>
69  
70 =item $now
71  
72 Optional. The time to consider as the current time
73  
74 =back
75  
76 =cut
77  
78 sub get_stat($;$)
79  
80 # This stat always returns a File::stat object.
81 8 ▶{
Subroutine get_stat: called CC=7 SCAR=19.8
Pod documented
82 8 my ( $path, $now ) = @_;
83  
84 8 ▶ $now = time() if ( !defined $now );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $now 8 0 8
85  
86 8 ▶ if ( !exists $laststattime{$path} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $laststattime{$path}) 3 5 8
87  
88 # Definitely new
89 3 my $itemstat = File::stat::stat($path);
90 3 $laststattime{$path} = $now;
91 3 ▶ if ( !defined $itemstat ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $itemstat) 0 3 3
92 0 return undef;
93 }
94 3 return $stat_cache{$path} = $itemstat;
95 }
96  
97 5 ▶ if ( $now - $laststattime{$path} > $STATTIMEOUT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now - $laststattime{$path} > $File::StatCache::STATTIMEOUT) 2 3 5
98  
99 # Haven't checked it in a while - check again
100 2 my $itemstat = File::stat::stat($path);
101 2 $laststattime{$path} = $now;
102 2 ▶ if ( !defined $itemstat ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $itemstat) 1 1 2
103 1 delete $stat_cache{$path};
104 1 return undef;
105 }
106 1 return $stat_cache{$path} = $itemstat;
107 }
108  
109 3 ▶ if ( !exists $stat_cache{$path} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $stat_cache{$path}) 0 3 3
110  
111 # Recently checked, and it didn't exist
112 0 return undef;
113 }
114  
115 # Recently checked; exists
116 3 return $stat_cache{$path};
117 }
118  
119 sub _stat($)
120  
121 # The real call from outside - return an object or list as appropriate
122 6 ▶{
Subroutine _stat: called CC=4 SCAR=14.0
123 6 my ($path) = @_;
124  
125 6 my $stat = get_stat($path);
126  
127 6 ▶ if ( defined $stat ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stat) { } 5 1 6
128 5 ▶ return $stat unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 5 5
129  
130 # Need to construct the full annoying 13-element list
131 return (
132 5 $stat->dev, $stat->ino, $stat->mode, $stat->nlink,
133 $stat->uid, $stat->gid, $stat->rdev, $stat->size,
134 $stat->atime, $stat->mtime, $stat->ctime, $stat->blksize,
135 $stat->blocks,
136 );
137 }
138 else {
139 1 ▶ return wantarray ? () : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
140 }
141 }
142  
143 =head2 $stats = stat( $path )
144  
145 =head2 @stats = stat( $path )
146  
147 This is a drop-in replacement for either the perl core C<stat()> function or
148 the C<File::stat::stat> function, depending whether it is called in list or
149 scalar context. It behaves identically to either of these functions, except
150 that it returns cached results if the cached value is recent enough.
151  
152 Note that in the case of failure (i.e. C<undef> in scalar context, empty in
153 list context), the value of C<$!> is not reliable as the reason for error.
154 Error results are not currently cached.
155  
156 =over 8
157  
158 =item $path
159  
160 The path to the filesystem item to C<stat()>
161  
162 =back
163  
164 =cut
165  
166 # Need to work around perl's warning of "Subroutine stat redefined at..."
167  
168 4 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
169 4 *stat = \&_stat;
170 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
171  
172 =head2 get_item_mtime( $path [, $now ] )
173  
174 This function is equivalent to
175  
176 (scalar get_stat( $path, $now ))->mtime
177  
178 =over 8
179  
180 =item $path
181  
182 The path to the filesystem item to C<stat()>
183  
184 =item $now
185  
186 Optional. The time to consider as the current time
187  
188 =back
189  
190 =cut
191  
192 0 ▶sub get_item_mtime($;$) {
Subroutine get_item_mtime: not called CC=2 SCAR=17.9
Pod documented
193 0 my ( $path, $now ) = @_;
194  
195 0 my $itemstat = get_stat( $path, $now );
196 0 ▶ return $itemstat->mtime if defined $itemstat;
Branch: 1 branch
BranchTrueFalseTotal
if defined $itemstat 0 0 0
197 0 return undef;
198 }
199  
200 # Keep perl happy; keep Britain tidy
201 4 1;
202  
203 __END__
↑ Summary