blib/lib/File/Properties/Compressed.pm

filter: Statementstmt 75.2% 85 / 113 Branchbran 43.1% 19 / 44 Conditioncond 29.6% 8 / 27 MC/DCmcdc 5.8% 1 / 17 Subroutinesub 85.0% 17 / 20 Podpod 100% 5 / 5 totaltotal 59.7% 135 / 226 CC 32 SCAR 24.7
CC
32
Cov
61%
_tmpgunzip
56.0
string
12.0
_tmpbunzip
8.0
CRAP 11.9
« blib/lib/File/Properties/Regular.pm ↑ Summary lib/File/Properties/Database.pm »
1 # ----------------------------------------------------------------------------
2 #
3 # This module supports computing and caching of file SHA-2 digests and
4 # mime types. Files compressed using bzip2 and gzip are uncompressed,
5 # and their content SHA digests and mime types are also accessible.
6 #
7 # Copyright © 2010,2011 Brendt Wohlberg <wohl@cpan.org>
8 # See distribution LICENSE file for license details.
9 #
10 # Most recent modification: 18 December 2011
11 #
12 # ----------------------------------------------------------------------------
13  
14 package File::Properties::Compressed;
15 3 our $VERSION = 0.02;
16  
17 3 ▶use File::Properties::Regular;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use base qw(File::Properties::Regular);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 require 5.005;
21 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use Compress::Bzip2 qw(bzopen BZ_OK BZ_STREAM_END);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use Compress::Zlib qw(gzopen Z_OK Z_STREAM_END);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 3 our $UncompressBufferSize = 1048576;
30 3 our $CacheTableName = 'CompFileCache';
31 3 our $CacheTableCols = ['FileDigest TEXT','ContentMimeType TEXT',
32 'ContentDigest TEXT'];
33  
34  
35 # ----------------------------------------------------------------------------
36 # Initialiser
37 # ----------------------------------------------------------------------------
38 2 ▶sub _init {
Subroutine _init: called CC=6 SCAR=18.2
39 2 my $self = shift;
40 2 my $path = shift; # File path
41 2 my $fpcr = shift; # File::Properties::Cache reference
42  
43 # Initialisation for base
44 2 $self->SUPER::_init($path, $fpcr);
45 # Remainder of initialisation only required if file is a regular
46 # file that is compressed
47 2 ▶ if ($self->isreg and $self->iscompressed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isreg and $self->iscompressed) 2 0 2
Condition33%1/3
$self->isreg and $self->iscompressed
!ll&&!rl&&r
002
MC/DC0%0/2
$self->isreg and $self->iscompressed
$self->isreg $self->iscompressed
Truth table33%1/3
A and B
A= $self->isregB= $self->iscompressed
ABresult
0X0
100
111
48 2 my $cent;
49 ## If File::Properties::Cache reference has been specified, try to
50 ## retrieve compressed file cache entry for this file. If
51 ## retrieval fails, compute the relevant properties for this file
52 ## and store them in the cache.
53 2 ▶ if (defined $fpcr and ($cent = $fpcr->cretrieve($CacheTableName,
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fpcr and $cent = $fpcr->cretrieve($File::Properties::Compressed::CacheTableName, {'FileDigest', $self->SUPER::digest})) { } 1 1 2
Condition66%2/3
defined $fpcr and $cent = $fpcr->cretrieve($File::Properties::Compressed::CacheTableName, {"FileDigest", $self->SUPER::digest})
!ll&&!rl&&r
011
MC/DC50%1/2
defined $fpcr and $cent = $fpcr->cretrieve($File::Properties::Compressed::CacheTableName, {"FileDigest", $self->SUPER::digest})
defined $fpcr $cent = $fpcr->cretrieve($File::Properties::Compressed::CacheTableName, {"FileDigest", $self->SUPER::digest})
Truth table66%2/3
A and B
A= defined $fpcrB= $cent = $fpcr->cretrieve($File::Properties::Compressed::CacheTableName, {"FileDigest", $self->SUPER::digest})
ABresult
0X0
100
111
54 {'FileDigest' => $self->SUPER::digest}))) {
55 1 $self->cmimetype($cent->{'ContentMimeType'});
56 1 $self->cdigest($cent->{'ContentDigest'});
57 # Set flag indicating that this entry was obtained from the cache
58 1 $self->_fromcache($CacheTableName, 1);
59 } else {
60 1 my $fhnd = $self->cfilehandle;
61 1 $self->cmimetype(File::Properties::Regular::_mimetype($fhnd->filename));
62 1 $self->cdigest(File::Properties::Regular::_digest($fhnd->filename));
63 # Set flag indicating that this entry was not obtained from the cache
64 1 $self->_fromcache($CacheTableName, 0);
65 1 ▶ if (defined $fpcr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fpcr) 1 0 1
66 1 my $row = {'FileDigest' => $self->SUPER::digest,
67 'ContentMimeType' => $self->cmimetype,
68 'ContentDigest' => $self->cdigest};
69 1 $fpcr->cinsert($CacheTableName, $row);
70 }
71 }
72 }
73 }
74  
75  
76 # ----------------------------------------------------------------------------
77 # Get (or set) mime type of compressed file content
78 # ----------------------------------------------------------------------------
79 3 ▶sub cmimetype {
Subroutine cmimetype: called CC=3 SCAR=11.1
Pod documented
80 3 my $self = shift;
81  
82 3 ▶ $self->{'cmtp'} = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 1 3
83 3 ▶ return (defined $self->{'cmtp'})?$self->{'cmtp'}:$self->mimetype;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'cmtp'} ? : 3 0 3
84 }
85  
86  
87 # ----------------------------------------------------------------------------
88 # Get (or set) digest string of compressed file content
89 # ----------------------------------------------------------------------------
90 3 ▶sub cdigest {
Subroutine cdigest: called CC=3 SCAR=11.1
Pod documented
91 3 my $self = shift;
92  
93 3 ▶ $self->{'cdgs'} = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 1 3
94 3 ▶ return (defined $self->{'cdgs'})?$self->{'cdgs'}:$self->digest;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'cdgs'} ? : 3 0 3
95 }
96  
97  
98 # ----------------------------------------------------------------------------
99 # Get file handle to file, or to file containing uncompressed content
100 # if file is compressed.
101 # ----------------------------------------------------------------------------
102 1 ▶sub cfilehandle {
Subroutine cfilehandle: called CC=5 SCAR=18.1
Pod documented
103 1 my $self = shift;
104  
105 ## If a file handle for the file (or temporary file containing
106 ## uncompressed file data, if it is a compressed file) is not
107 ## stored, create one. The assumption is that this file handle will
108 ## only need to be created and accessed on the initial pass through
109 ## the class hierarchy for a specific file, before it has been
110 ## cached. Any class derived from File::Properties::Compressed
111 ## should cache all information that has to be computed from the
112 ## file so that subsequent object constructions for the file can be
113 ## done purely from the cache, without need for additional
114 ## uncompressing of the data.
115 1 ▶ if (not defined $self->{'cfhn'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"cfhn"}) 1 0 1
116 # Open and store a file handle for the file, to the file itself if
117 # it is not compressed, or to a temporary file containing
118 # uncompressed file data if it is
119 1 ▶ $self->{'cfhn'} = ($self->iscompressed)?
Branch: 1 branch
BranchTrueFalseTotal
$self->iscompressed ? : 1 0 1
120 $self->_tmpunzip:
121 IO::File->new($self->path, 'r');
122 throw File::Properties::Error("Error opening file handle")
123 1 ▶ if (not (defined $self->{'cfhn'} and $self->{'cfhn'}->opened));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"cfhn"} and $self->{"cfhn"}->opened 0 1 1
Condition33%1/3
defined $self->{"cfhn"} and $self->{"cfhn"}->opened
!ll&&!rl&&r
001
MC/DC0%0/2
defined $self->{"cfhn"} and $self->{"cfhn"}->opened
defined $self->{"cfhn"} $self->{"cfhn"}->opened
Truth table33%1/3
A and B
A= defined $self->{"cfhn"}B= $self->{"cfhn"}->opened
ABresult
0X0
100
111
124 }
125 1 return $self->{'cfhn'};
126 }
127  
128  
129 # ----------------------------------------------------------------------------
130 # Determine whether file properties represent a compressed file
131 # ----------------------------------------------------------------------------
132 3 ▶sub iscompressed {
Subroutine iscompressed: called CC=3 SCAR=14.2
Pod documented
133 3 my $self = shift;
134  
135 3 ▶ return ((defined $self->SUPER::mimetype) and
Condition33%2/6
defined $self->SUPER::mimetype && ($self->SUPER::mimetype eq 'application/x-bzip2' || $self->SUPER::mimetype eq 'application/x-gzip')
!ll&&!rl&&r
003
$self->SUPER::mimetype eq "application/x-bzip2" or $self->SUPER::mimetype eq "application/x-gzip"
l!l&&r!l&&!r
300
MC/DC0%0/3
defined $self->SUPER::mimetype && ($self->SUPER::mimetype eq 'application/x-bzip2' || $self->SUPER::mimetype eq 'application/x-gzip')
defined $self->SUPER::mimetype $self->SUPER::mimetype eq "application/x-bzip2" $self->SUPER::mimetype eq "application/x-gzip"
Truth table25%1/4
A && B or C
A= defined $self->SUPER::mimetypeB= $self->SUPER::mimetype eq "application/x-bzip2"C= $self->SUPER::mimetype eq "application/x-gzip"
ABCresult
0XX0
1000
11X1
1011
136 ($self->SUPER::mimetype eq 'application/x-bzip2' or
137 $self->SUPER::mimetype eq 'application/x-gzip'));
138 }
139  
140  
141 # ----------------------------------------------------------------------------
142 # Construct string representing properties hash
143 # ----------------------------------------------------------------------------
144 0 ▶sub string {
Subroutine string: not called CC=3 SCAR=24.8
Pod documented
145 0 my $self = shift;
146 0 my $levl = shift;
147  
148 0 ▶ $levl = 0 if (!defined $levl);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $levl 0 0 0
149 0 my $lpfx = ' ' x (2*$levl);
150 0 my $s = $self->SUPER::string($levl);
151 0 ▶ if ($self->iscompressed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->iscompressed) 0 0 0
152 0 $s .= $lpfx . " Content Mime Type: ".$self->cmimetype."\n";
153 0 $s .= $lpfx . " Content Digest: ".substr($self->cdigest,0,40)."...\n";
154 }
155 0 return $s;
156 }
157  
158  
159 # ----------------------------------------------------------------------------
160 # Initialise cache table for File::Properties::Compressed objects
161 # ----------------------------------------------------------------------------
162 1 ▶sub _cacheinit {
Subroutine _cacheinit: called CC=1 SCAR=0.0
163 1 my $self = shift;
164 1 my $fpcr = shift; # File::Properties::Cache reference
165 1 my $opts = shift; # Options hash
166  
167 1 $self->SUPER::_cacheinit($fpcr, $opts);
168 1 $fpcr->define($CacheTableName, $CacheTableCols,
169 {'TableVersion' => [__PACKAGE__.'::Version', $VERSION]});
170 }
171  
172  
173 # ----------------------------------------------------------------------------
174 # Clear invalid entries in cache table for File::Properties::Compressed data
175 # ----------------------------------------------------------------------------
176 0 ▶sub _cacheclean {
Subroutine _cacheclean: not called CC=1 SCAR=6.9
177 0 my $self = shift;
178 0 my $fpcr = shift; # File::Properties::Cache reference
179  
180 0 my $ctbl = $CacheTableName;
181 0 my $rtbl = $File::Properties::Regular::CacheTableName;
182 # Remove any entries in the File::Properties::Compressed cache table
183 # for which there is not a corresponding entry with the same file
184 # digest in the File::Properties::Regular cache table
185 0 $fpcr->remove($ctbl, {'Where' => "NOT EXISTS (SELECT * FROM $rtbl " .
186 "WHERE Digest = $ctbl.FileDigest)"});
187 }
188  
189  
190 # ----------------------------------------------------------------------------
191 # Return file handle and path to temporary file containing unzipped data
192 # ----------------------------------------------------------------------------
193 1 ▶sub _tmpunzip {
Subroutine _tmpunzip: called CC=3 SCAR=13.5
194 1 my $self = shift;
195  
196 1 my $fmtp = $self->SUPER::mimetype;
197 # Path is specified in constructor argument, so it should be
198 # available independent of whether base object was retrieved from
199 # the cache
200 1 my $path = $self->path;
201 1 ▶ if ($fmtp eq 'application/x-bzip2') {
Branch: 2 branches
BranchTrueFalseTotal
if ($fmtp eq 'application/x-bzip2') { } 1 0 1
elsif ($fmtp eq 'application/x-gzip') { } 0 0 0
202 1 return _tmpbunzip($path);
203 } elsif ($fmtp eq 'application/x-gzip') {
204 0 return _tmpgunzip($path);
205 } else {
206 0 return undef;
207 }
208 }
209  
210  
211 # ----------------------------------------------------------------------------
212 # Return file handle to temporary file containing bunzipped data
213 # ----------------------------------------------------------------------------
214 1 ▶sub _tmpbunzip {
Subroutine _tmpbunzip: called CC=7 SCAR=20.7
215 1 my $path = shift; # File path
216  
217 1 my $buf;
218 1 my $bsz = $UncompressBufferSize;
219 1 my $tpfh = File::Temp->new();
220 1 ▶ throw File::Properties::Error("Error creating temporary file")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tpfh and $tpfh->opened 0 1 1
Condition33%1/3
defined $tpfh and $tpfh->opened
!ll&&!rl&&r
001
MC/DC0%0/2
defined $tpfh and $tpfh->opened
defined $tpfh $tpfh->opened
Truth table33%1/3
A and B
A= defined $tpfhB= $tpfh->opened
ABresult
0X0
100
111
221 if (not (defined $tpfh and $tpfh->opened));
222 1 ▶ my $bz = bzopen($path, 'r') or
Branch: 1 branch
BranchTrueFalseTotal
my $bz = &bzopen($path, "r") or "File::Properties::Error"->throw("Error opening bzip2 file $path") 1 0 1
223 throw File::Properties::Error("Error opening bzip2 file $path");
224 1 ▶ while ($bz->bzread($buf, $bsz)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bz->bzread($buf, $bsz)) 1 1 2
225 1 print $tpfh $buf;
226 }
227 1 ▶ throw File::Properties::Error("Error reading bzip2 file $path: ".$bz->bzerror)
Branch: 1 branch
BranchTrueFalseTotal
if $bz->bzerror != BZ_OK() and $bz->bzerror != BZ_STREAM_END() 0 1 1
Condition33%1/3
$bz->bzerror != BZ_OK() and $bz->bzerror != BZ_STREAM_END()
!ll&&!rl&&r
100
MC/DC0%0/2
$bz->bzerror != BZ_OK() and $bz->bzerror != BZ_STREAM_END()
$bz->bzerror != BZ_OK() $bz->bzerror != BZ_STREAM_END()
Truth table33%1/3
A and B
A= $bz->bzerror != BZ_OK()B= $bz->bzerror != BZ_STREAM_END()
ABresult
0X0
100
111
228 if $bz->bzerror != BZ_OK and $bz->bzerror != BZ_STREAM_END;
229 1 $bz->bzclose();
230 1 $tpfh->seek(0,0);
231  
232 1 return $tpfh;
233 }
234  
235  
236 # ----------------------------------------------------------------------------
237 # Return file handle to temporary file containing gunzipped data
238 # ----------------------------------------------------------------------------
239 0 ▶sub _tmpgunzip {
Subroutine _tmpgunzip: not called CC=7 SCAR=40.3
240 0 my $path = shift; # File path
241  
242 0 my $buf;
243 0 my $bsz = $UncompressBufferSize;
244 0 my $tpfh = File::Temp->new();
245 0 ▶ throw File::Properties::Error("Error creating temporary file")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tpfh and $tpfh->opened 0 0 0
Condition0%0/3
defined $tpfh and $tpfh->opened
!ll&&!rl&&r
000
MC/DC0%0/2
defined $tpfh and $tpfh->opened
defined $tpfh $tpfh->opened
Truth table0%0/3
A and B
A= defined $tpfhB= $tpfh->opened
ABresult
0X0
100
111
246 if (not (defined $tpfh and $tpfh->opened));
247 0 ▶ my $gz = gzopen($path, 'r') or
Branch: 1 branch
BranchTrueFalseTotal
my $gz = &gzopen($path, "r") or "File::Properties::Error"->throw("Error opening gzip file $path") 0 0 0
248 throw File::Properties::Error("Error opening gzip file $path");
249 0 ▶ while ($gz->gzread($buf, $bsz)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gz->gzread($buf, $bsz)) 0 0 0
250 0 print $tpfh $buf;
251 }
252 0 ▶ throw File::Properties::Error("Error reading gzip file $path: ".$gz->gzerror)
Branch: 1 branch
BranchTrueFalseTotal
if $gz->gzerror != Z_OK() and $gz->gzerror != Z_STREAM_END() 0 0 0
Condition0%0/3
$gz->gzerror != Z_OK() and $gz->gzerror != Z_STREAM_END()
!ll&&!rl&&r
000
MC/DC0%0/2
$gz->gzerror != Z_OK() and $gz->gzerror != Z_STREAM_END()
$gz->gzerror != Z_OK() $gz->gzerror != Z_STREAM_END()
Truth table0%0/3
A and B
A= $gz->gzerror != Z_OK()B= $gz->gzerror != Z_STREAM_END()
ABresult
0X0
100
111
253 if $gz->gzerror != Z_OK and $gz->gzerror != Z_STREAM_END;
254 0 $gz->gzclose();
255 0 $tpfh->seek(0,0);
256  
257 0 return $tpfh;
258 }
259  
260  
261 # ----------------------------------------------------------------------------
262 # End of method definitions
263 # ----------------------------------------------------------------------------
264  
265  
266 3 1;
267 __END__
« blib/lib/File/Properties/Regular.pm ↑ Summary lib/File/Properties/Database.pm »