blib/lib/File/Properties/Regular.pm

filter: Statementstmt 75.3% 101 / 134 Branchbran 38.4% 20 / 52 Conditioncond 33.3% 2 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 89.4% 17 / 19 Podpod 100% 5 / 5 totaltotal 66.3% 146 / 220 CC 30 SCAR 27.6
CC
30
Cov
64%
_cacheclean
56.0
_cacheinit
23.4
string
12.0
CRAP 15.8
« blib/lib/File/Properties/Media.pm ↑ Summary blib/lib/File/Properties/Compressed.pm »
1 # ----------------------------------------------------------------------------
2 #
3 # This module supports computing and caching of file SHA-2 digests and
4 # mime types.
5 #
6 # Copyright © 2010,2011 Brendt Wohlberg <wohl@cpan.org>
7 # See distribution LICENSE file for license details.
8 #
9 # Most recent modification: 18 December 2011
10 #
11 # ----------------------------------------------------------------------------
12  
13 package File::Properties::Regular;
14 4 our $VERSION = 0.02;
15  
16 4 ▶use File::Properties::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use File::Properties::Generic;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use base qw(File::Properties::Generic);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 require 5.005;
21 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use File::Type; # Perhaps use File::MMagic::XS, File::MimeInfo
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 4 our $SHADigestType = 512;
28 4 our $CacheTableName = 'RegFileCache';
29  
30  
31 # ----------------------------------------------------------------------------
32 # Initialiser
33 # ----------------------------------------------------------------------------
34 6 ▶sub _init {
Subroutine _init: called CC=6 SCAR=18.0
35 6 my $self = shift;
36 6 my $path = shift; # File path
37 6 my $fpcr = shift; # File::Properties::Cache reference
38  
39 # Call base class _init method
40 6 $self->SUPER::_init($path, $fpcr);
41 ## Remainder of initialisation only necessary for regular files (in
42 ## particular, it should not be performed for directories)
43 6 ▶ if ($self->isreg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isreg) 6 0 6
44 # Set flag indicating whether file path is cached
45 6 ▶ my $cptf = defined $fpcr?
Branch: 1 branch
BranchTrueFalseTotal
defined $fpcr ? : 6 0 6
46 $fpcr->cproperties($CacheTableName, 'CachedPath'):1;
47 # Initialisation for _fromcache method
48 6 $self->{'rfcf'} = {};
49 6 my $cent;
50 ## If File::Properties::Cache reference specified and the cache
51 ## contains an entry matching the base Generic object, set the mime
52 ## type and file digest from the cache entry, otherwise compute
53 ## these values.
54 6 ▶ if (defined $fpcr and ($cent = $fpcr->cretrieve($CacheTableName,
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fpcr and $cent = $fpcr->cretrieve($File::Properties::Regular::CacheTableName, $self->cachekey($cptf))) { } 3 3 6
Condition66%2/3
defined $fpcr and $cent = $fpcr->cretrieve($File::Properties::Regular::CacheTableName, $self->cachekey($cptf))
!ll&&!rl&&r
033
MC/DC50%1/2
defined $fpcr and $cent = $fpcr->cretrieve($File::Properties::Regular::CacheTableName, $self->cachekey($cptf))
defined $fpcr $cent = $fpcr->cretrieve($File::Properties::Regular::CacheTableName, $self->cachekey($cptf))
Truth table66%2/3
A and B
A= defined $fpcrB= $cent = $fpcr->cretrieve($File::Properties::Regular::CacheTableName, $self->cachekey($cptf))
ABresult
0X0
100
111
55 $self->cachekey($cptf)))) {
56 3 $self->mimetype($cent->{'MimeType'});
57 3 $self->digest($cent->{'Digest'});
58 # Set flag indicating that this entry was obtained from the cache
59 3 $self->_fromcache($CacheTableName, 1);
60 } else {
61 3 $self->mimetype(_mimetype($self->path));
62 3 $self->digest(_digest($self->path));
63 # Set flag indicating that this entry was not obtained from the cache
64 3 $self->_fromcache($CacheTableName, 0);
65 ## If a File::Properties::Cache reference is specified, record the
66 ## mime type and digest in the cache
67 3 ▶ if (defined $fpcr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fpcr) 3 0 3
68 3 my $row = $self->cachekey($cptf);
69 3 $row->{'MimeType'} = $self->mimetype;
70 3 $row->{'Digest'} = $self->digest;
71 3 $fpcr->cinsert($CacheTableName, $row);
72 }
73 }
74 }
75 }
76  
77  
78 # ----------------------------------------------------------------------------
79 # Get (or set) file mime type
80 # ----------------------------------------------------------------------------
81 16 ▶sub mimetype {
Subroutine mimetype: called CC=2 SCAR=6.9
Pod documented
82 16 my $self = shift;
83  
84 16 ▶ $self->{'fmtp'} = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 6 10 16
85 16 return $self->{'fmtp'};
86 }
87  
88  
89 # ----------------------------------------------------------------------------
90 # Get (or set) file digest
91 # ----------------------------------------------------------------------------
92 16 ▶sub digest {
Subroutine digest: called CC=2 SCAR=6.9
Pod documented
93 16 my $self = shift;
94  
95 16 ▶ $self->{'fdgs'} = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 6 10 16
96 16 return $self->{'fdgs'};
97 }
98  
99  
100 # ----------------------------------------------------------------------------
101 # Construct string representing properties hash
102 # ----------------------------------------------------------------------------
103 0 ▶sub string {
Subroutine string: not called CC=3 SCAR=24.8
Pod documented
104 0 my $self = shift;
105 0 my $levl = shift;
106  
107 0 ▶ $levl = 0 if (!defined $levl);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $levl 0 0 0
108 0 my $lpfx = ' ' x (2*$levl);
109 0 my $s = $self->SUPER::string($levl);
110  
111 0 ▶ if (not $self->isdir) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->isdir) 0 0 0
112 0 $s .= $lpfx . " File Mime Type: ".$self->mimetype."\n";
113 0 $s .= $lpfx . " File Digest: ".substr($self->digest,0,40)."...\n";
114 }
115  
116 0 return $s;
117 }
118  
119  
120 # ----------------------------------------------------------------------------
121 # Compute cache key array for this object
122 # ----------------------------------------------------------------------------
123 9 ▶sub cachekey {
Subroutine cachekey: called CC=3 SCAR=11.1
Pod documented
124 9 my $self = shift;
125 9 my $cptf = shift; # Cache path flag
126  
127 9 my $key = {'Size' => $self->size, 'ModTime' => $self->mtime};
128 # Insert path into key data if path is cached
129 9 ▶ $key->{'Path'} = $self->path if ($cptf);
Branch: 1 branch
BranchTrueFalseTotal
if $cptf 9 0 9
130 # Insert device and inode numbers into key data if stat is fully supported
131 9 ▶ if ($File::Properties::Generic::FullStatSupport) {
Branch: 1 branch
BranchTrueFalseTotal
if ($File::Properties::Generic::FullStatSupport) 9 0 9
132 9 $key->{'Device'} = $self->device;
133 9 $key->{'Inode'} = $self->inode;
134 }
135 9 return $key;
136 }
137  
138  
139 # ----------------------------------------------------------------------------
140 # Create initialised cache object
141 # ----------------------------------------------------------------------------
142 2 ▶sub cache {
Subroutine cache: called CC=1 SCAR=0.0
Pod documented
143 2 my $self = shift;
144 2 my $dbfp = shift; # Database file path
145 2 my $opts = shift; # Options hash
146  
147 # Construct a File::Properties::Cache object attached to the
148 # specified database file
149 2 my $fpcr = File::Properties::Cache->new($dbfp, $opts);
150 # Initialise the cache table for this class
151 2 $self->_cacheinit($fpcr, $opts);
152 2 return $fpcr;
153 }
154  
155  
156 # ----------------------------------------------------------------------------
157 # Initialise cache table for File::Properties::Regular data
158 # ----------------------------------------------------------------------------
159 2 ▶sub _cacheinit {
Subroutine _cacheinit: called CC=11 SCAR=31.5
160 2 my $self = shift;
161 2 my $fpcr = shift; # File::Properties::Cache reference
162 2 my $opts = shift; # Options hash
163  
164 ## Flag determining caching of file path
165 2 my $cptf = 1;
166 2 ▶ $cptf = $opts->{'CachedPath'} if (defined $opts->{'CachedPath'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"CachedPath"} 0 2 2
167 2 ▶ $cptf = 1 if (not $File::Properties::Generic::FullStatSupport);
Branch: 1 branch
BranchTrueFalseTotal
unless $File::Properties::Generic::FullStatSupport 0 2 2
168 ## If cache table exists, check that it is compatible with current
169 ## options, otherwise initialise it according to options
170 2 ▶ if ($fpcr->tableexists($CacheTableName)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fpcr->tableexists($File::Properties::Regular::CacheTableName)) 0 2 2
171 # Get array of cache table column names
172 0 my $cnam = $fpcr->columns($CacheTableName);
173 # Determine whether cache table column names include 'Path'
174 0 my $ecpt = File::Properties::Cache::_inarray($cnam, 'Path');
175 # Throw error if file path caching is requested in options but not
176 # configured in cache database
177 0 ▶ throw File::Properties::Error("Path caching requested but is not " .
Branch: 1 branch
BranchTrueFalseTotal
if $cptf and not $ecpt 0 0 0
Condition0%0/3
$cptf and not $ecpt
!ll&&!rl&&r
000
MC/DC0%0/2
$cptf and not $ecpt
$cptf not $ecpt
Truth table0%0/3
A and not(B)
A= $cptfB= not $ecpt
ABresult
0X0
100
111
178 "configured in existing cache database")
179 if ($cptf and not $ecpt);
180 # Set file path caching flag if configured in cache database
181 0 ▶ $cptf = 1 if ($ecpt);
Branch: 1 branch
BranchTrueFalseTotal
if $ecpt 0 0 0
182 # Determine whether cache table column names include 'Device' and 'Inode'
183 0 ▶ my $ecsf = File::Properties::Cache::_inarray($cnam, 'Device') and
Branch: 1 branch
BranchTrueFalseTotal
my $ecsf = File::Properties::Cache::_inarray($cnam, "Device") and File::Properties::Cache::_inarray($cnam, "Inode") 0 0 0
184 File::Properties::Cache::_inarray($cnam, 'Inode');
185 # Throw error if File::Properties::Generic::FullStatSupport flag
186 # value does not match flag indicating inclusion of 'Device' and
187 # 'Inode' entries in cache table
188 0 ▶ throw File::Properties::Error("Mismatch between stat support in current ".
Branch: 1 branch
BranchTrueFalseTotal
if !!$File::Properties::Generic::FullStatSupport != !!$ecsf 0 0 0
189 "architecture and existing database")
190 if (!!$File::Properties::Generic::FullStatSupport != !!$ecsf);
191 }
192 2 my $ctbk = []; # Cache table keys
193 2 my $ctbc = []; # Cache table columns
194 ## Construct array describing cache table definition
195 2 my $cols = [];
196 2 ▶ push @$cols, 'Path TEXT' if ($cptf);
Branch: 1 branch
BranchTrueFalseTotal
if $cptf 2 0 2
197 2 ▶ push @$cols, ('Device INTEGER','Inode INTEGER')
Branch: 1 branch
BranchTrueFalseTotal
if $File::Properties::Generic::FullStatSupport 2 0 2
198 if ($File::Properties::Generic::FullStatSupport);
199 2 push @$cols,('Size INTEGER','ModTime INTEGER');
200 # Construct cache table keys array by removing type specifications
201 # from a copy of $cols array
202 2 $ctbk = [map { /^[^\s]+/; $& } @$cols];
203 ## Construct final cache table specification array by appending
204 ## column definitions for MimeType and Digest to copy of $cols array
205 2 $ctbc = [@$cols];
206 2 push @$ctbc, ('MimeType TEXT','Digest TEXT');
207 # Define cache table
208 2 $fpcr->define($CacheTableName, $ctbc, {'IncludeInsertDate' => 1,
209 'TableVersion' => [__PACKAGE__.'::Version', $VERSION]});
210 # Make temporary record of path caching flag
211 2 $fpcr->cproperties($CacheTableName, 'CachedPath', $cptf);
212 }
213  
214  
215 # ----------------------------------------------------------------------------
216 # Clear invalid entries in cache table for File::Properties::Regular data
217 # ----------------------------------------------------------------------------
218 0 ▶sub _cacheclean {
Subroutine _cacheclean: not called CC=7 SCAR=40.3
219 0 my $self = shift;
220 0 my $fpcr = shift; # File::Properties::Cache reference
221  
222 # This operation is only possible if file paths are included in the
223 # cache
224 0 ▶ if ($fpcr->cproperties($CacheTableName, 'CachedPath')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fpcr->cproperties($File::Properties::Regular::CacheTableName, 'CachedPath')) { } 0 0 0
225 # Initialise array of paths found not to exist
226 0 my $clst = [];
227 # Set the number of database rows retrieved in each database access
228 0 my $nblk = 100;
229 # Get the total number of rows in the database
230 0 my $nrow = $fpcr->numrows($CacheTableName);
231 0 my ($k, $l);
232 ## Iterate over all database rows, accessing them in blocks of
233 ## $nblk rows at a time. Within each block of rows, push any paths
234 ## that no longer exist onto the array of paths to be cleared from
235 ## the cache.
236 0 ▶ for ($k = 0; $k < $nrow / $nblk; $k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $nrow / $nblk) 0 0 0
237 # Compute the number of rows in the current block
238 0 ▶ my $lmax = (int($nrow / $nblk) == $k)?($nrow % $nblk):$nblk;
Branch: 1 branch
BranchTrueFalseTotal
int $nrow / $nblk == $k ? : 0 0 0
239 # Compute the row index of the first row in the current block
240 0 my $ridx = $k * $nblk;
241 # Retrieve the current block of rows from the database
242 0 my $rdat = $fpcr->retrieve($CacheTableName,
243 {'ReturnType' => 'Array',
244 'Suffix' => "LIMIT $ridx, $lmax"});
245 ## Push any paths that no longer exist onto the clean list
246 0 ▶ for ($l = 0; $l < $lmax; $l++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l < $lmax) 0 0 0
247 0 ▶ push @$clst, $rdat->[$l]->[0] if (! -f $rdat->[$l]->[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $rdat->[$l][0] 0 0 0
248 }
249 }
250 0 my $pth;
251 ## Iterate over array of non-existent paths and remove the
252 ## corresponding cache entry
253 0 foreach $pth (@$clst) {
254 0 $fpcr->remove($CacheTableName, {'Where' => {'Path' => $pth}});
255 }
256 0 return 1;
257 } else {
258 0 return 0;
259 }
260 }
261  
262  
263 # ----------------------------------------------------------------------------
264 # Get or set flag indicating whether data was retrieved from the cache
265 # ----------------------------------------------------------------------------
266 14 ▶sub _fromcache {
Subroutine _fromcache: called CC=3 SCAR=11.0
267 14 my $self = shift;
268  
269 14 my $mkey = shift;
270 14 ▶ $mkey = eval('$' . ref($self) . "::CacheTableName") if not defined $mkey;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mkey 6 8 14
271 14 ▶ $self->{'rfcf'}->{$mkey} = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 8 6 14
272 14 return $self->{'rfcf'}->{$mkey};
273 }
274  
275  
276 # ----------------------------------------------------------------------------
277 # Compute mime type of file specified by path
278 # ----------------------------------------------------------------------------
279 4 ▶sub _mimetype {
Subroutine _mimetype: called CC=1 SCAR=0.0
280 4 my $path = shift; # File path
281  
282 4 my $ft = File::Type->new();
283 4 return $ft->checktype_filename($path);
284 }
285  
286  
287 # ----------------------------------------------------------------------------
288 # Compute digest of file specified by path
289 # ----------------------------------------------------------------------------
290 6 ▶sub _digest {
Subroutine _digest: called CC=1 SCAR=0.0
291 6 my $path = shift; # File path
292  
293 6 my $sha = Digest::SHA->new($SHADigestType);
294 6 $sha->addfile($path, 'b');
295 6 return $sha->hexdigest;
296 }
297  
298  
299 # ----------------------------------------------------------------------------
300 # End of method definitions
301 # ----------------------------------------------------------------------------
302  
303  
304 4 1;
305 __END__
« blib/lib/File/Properties/Media.pm ↑ Summary blib/lib/File/Properties/Compressed.pm »