blib/lib/Mail/Mbox/MessageParser/MetaInfo.pm

filter: Statementstmt 80.2% 77 / 96 Branchbran 52.7% 19 / 36 Conditioncond 10.0% 3 / 30 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 84.6% 11 / 13 Podpod 100% 3 / 3 totaltotal 59.0% 114 / 193 CC 30 SCAR 28.7
CC
30
Cov
61%
_ENTRY_STILL_VALID
56.0
_INITIALIZE_ENTRY
25.2
CLEAR_CACHE
12.0
CRAP 17.6
« blib/lib/Mail/Mbox/MessageParser/Grep.pm ↑ Summary blib/lib/Mail/Mbox/MessageParser/Cache.pm »
1 package Mail::Mbox::MessageParser::MetaInfo;
2  
3 51 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 51 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 51 ▶use Mail::Mbox::MessageParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 51 ▶use vars qw( $VERSION $_DEBUG @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 51 ▶use vars qw( $_CACHE %_CACHE_OPTIONS $UPDATING_CACHE );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 51 @ISA = qw( Exporter );
12  
13 51 $VERSION = sprintf "%d.%02d%02d", q/0.2.0/ =~ /(\d+)/g;
14  
15 51 *_DEBUG = \$Mail::Mbox::MessageParser::_DEBUG;
16 51 *_dprint = \&Mail::Mbox::MessageParser::_dprint;
17 sub _dprint;
18  
19 # The class-wide cache, which will be read and written when necessary. i.e.
20 # read when an folder reader object is created which uses caching, and
21 # written when a different cache is specified, or when the program exits,
22 51 $_CACHE = {};
23  
24 51 %_CACHE_OPTIONS = ();
25  
26 51 $UPDATING_CACHE = 0;
27  
28 #-------------------------------------------------------------------------------
29  
30 sub _LOAD_STORABLE
31 266 ▶{
Subroutine _LOAD_STORABLE: called CC=2 SCAR=7.4
32 266 ▶ if (eval {require Storable})
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Storable } }) { } 266 0 266
33 {
34 266 import Storable;
35 266 return 1;
36 }
37 else
38 {
39 0 return 0;
40 }
41 }
42  
43 #-------------------------------------------------------------------------------
44  
45 sub SETUP_CACHE
46 266 ▶{
Subroutine SETUP_CACHE: called CC=5 SCAR=16.3
Pod documented
47 266 my $cache_options = shift;
48  
49 266 ▶ carp "Need file_name option" unless defined $cache_options->{'file_name'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cache_options->{"file_name"} 0 266 266
50  
51 266 ▶ return "Can not load " . __PACKAGE__ . ": Storable is not installed.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless _LOAD_STORABLE() 0 266 266
52 unless _LOAD_STORABLE();
53
54 # Load Storable if we need to
55 # See if the client is setting up a different cache
56 266 ▶ if (exists $_CACHE_OPTIONS{'file_name'} &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Mail::Mbox::MessageParser::MetaInfo::_CACHE_OPTIONS{"file_name"} and $cache_options->{"file_name"} ne $Mail::Mbox::MessageParser::MetaInfo::_CACHE_OPTIONS{"file_name"}) 243 23 266
Condition66%2/3
exists $Mail::Mbox::MessageParser::MetaInfo::_CACHE_OPTIONS{"file_name"} and $cache_options->{"file_name"} ne $Mail::Mbox::MessageParser::MetaInfo::_CACHE_OPTIONS{"file_name"}
!ll&&!rl&&r
230243
MC/DC50%1/2
exists $Mail::Mbox::MessageParser::MetaInfo::_CACHE_OPTIONS{"file_name"} and $cache_options->{"file_name"} ne $Mail::Mbox::MessageParser::MetaInfo::_CACHE_OPTIONS{"file_name"}
exists $Mail::Mbox::MessageParser::MetaInfo::_CACHE_OPTIONS{"file_name"} $cache_options->{"file_name"} ne $Mail::Mbox::MessageParser::MetaInfo::_CACHE_OPTIONS{"file_name"}
Truth table66%2/3
A and B
A= exists $Mail::Mbox::MessageParser::MetaInfo::_CACHE_OPTIONS{"file_name"}B= $cache_options->{"file_name"} ne $Mail::Mbox::MessageParser::MetaInfo::_CACHE_OPTIONS{"file_name"}
ABresult
0X0
100
111
57 $cache_options->{'file_name'} ne $_CACHE_OPTIONS{'file_name'})
58 {
59 243 _dprint "New cache file specified--writing old cache if necessary.";
60 243 WRITE_CACHE();
61 243 $_CACHE = {};
62 }
63  
64 266 %_CACHE_OPTIONS = %$cache_options;
65  
66 266 _READ_CACHE();
67  
68 266 return 'ok';
69 }
70  
71 #-------------------------------------------------------------------------------
72  
73 sub CLEAR_CACHE
74 0 ▶{
Subroutine CLEAR_CACHE: not called CC=3 SCAR=24.8
Pod documented
75 unlink $_CACHE_OPTIONS{'file_name'}
76 0 ▶ if defined $_CACHE_OPTIONS{'file_name'} && -f $_CACHE_OPTIONS{'file_name'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE_OPTIONS{"file_name"} and -f $Mail::Mbox::MessageParser::MetaInfo::_CACHE_OPTIONS{"file_name"} 0 0 0
Condition0%0/3
defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE_OPTIONS{"file_name"} and -f $Mail::Mbox::MessageParser::MetaInfo::_CACHE_OPTIONS{"file_name"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE_OPTIONS{"file_name"} and -f $Mail::Mbox::MessageParser::MetaInfo::_CACHE_OPTIONS{"file_name"}
defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE_OPTIONS{"file_name"} -f $Mail::Mbox::MessageParser::MetaInfo::_CACHE_OPTIONS{"file_name"}
Truth table0%0/3
A and B
A= defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE_OPTIONS{"file_name"}B= -f $Mail::Mbox::MessageParser::MetaInfo::_CACHE_OPTIONS{"file_name"}
ABresult
0X0
100
111
77  
78 0 $_CACHE = {};
79 0 $UPDATING_CACHE = 1;
80 }
81  
82 #-------------------------------------------------------------------------------
83  
84 sub _INITIALIZE_ENTRY
85 257 ▶{
Subroutine _INITIALIZE_ENTRY: called CC=9 SCAR=32.3
86 257 my $file_name = shift;
87  
88 257 my @stat = stat $file_name;
89  
90 257 ▶ return 0 unless @stat;
Branch: 1 branch
BranchTrueFalseTotal
unless @stat 0 257 257
91  
92 257 my $size = $stat[7];
93 257 my $time_stamp = $stat[9];
94  
95  
96 257 ▶ if (exists $_CACHE->{$file_name} &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name} and (defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{'size'} and defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{'time_stamp'} and $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{'size'} == $size and $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{'time_stamp'} == $time_stamp)) { } 0 257 257
Condition8%1/12
exists $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name} and (defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"size"} and defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"time_stamp"} and $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"size"} == $size and $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"time_stamp"} == $time_stamp)
!ll&&!rl&&r
25700
defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"size"} and defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"time_stamp"} and $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"size"} == $size and $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"time_stamp"} == $time_stamp
!ll&&!rl&&r
000
defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"size"} and defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"time_stamp"} and $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"size"} == $size
!ll&&!rl&&r
000
defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"size"} and defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"time_stamp"}
!ll&&!rl&&r
000
MC/DC0%0/5
exists $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name} and (defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"size"} and defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"time_stamp"} and $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"size"} == $size and $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"time_stamp"} == $time_stamp)
exists $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name} defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"size"} defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"time_stamp"} $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"size"} == $size $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"time_stamp"} == $time_stamp
Truth table16%1/6
A and B and C and D and E
A= exists $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}B= defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"size"}C= defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"time_stamp"}D= $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"size"} == $sizeE= $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"time_stamp"} == $time_stamp
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
97 (defined $_CACHE->{$file_name}{'size'} &&
98 defined $_CACHE->{$file_name}{'time_stamp'} &&
99 $_CACHE->{$file_name}{'size'} == $size &&
100 $_CACHE->{$file_name}{'time_stamp'} == $time_stamp))
101 {
102 0 _dprint "Cache is valid";
103  
104 # TODO: For now, if we re-initialize, we start over. Fix this so that we
105 # can use partial cache information.
106 0 ▶ if ($UPDATING_CACHE)
Branch: 1 branch
BranchTrueFalseTotal
if ($Mail::Mbox::MessageParser::MetaInfo::UPDATING_CACHE) 0 0 0
107 {
108 0 _dprint "Resetting cache entry for \"$file_name\"\n";
109  
110 # Reset the cache entry for this file
111 0 $_CACHE->{$file_name}{'size'} = $size;
112 0 $_CACHE->{$file_name}{'time_stamp'} = $time_stamp;
113 0 $_CACHE->{$file_name}{'emails'} = [];
114 0 $_CACHE->{$file_name}{'modified'} = 0;
115 }
116 }
117 else
118 {
119 257 ▶ if (exists $_CACHE->{$file_name})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}) { } 0 257 257
120 {
121 0 _dprint "Size or time stamp has changed for file \"" .
122 $file_name . "\". Invalidating cache entry";
123 }
124 else
125 {
126 257 _dprint "Cache is invalid: \"$file_name\" has not yet been parsed";
127 }
128  
129 257 $_CACHE->{$file_name}{'size'} = $size;
130 257 $_CACHE->{$file_name}{'time_stamp'} = $time_stamp;
131 257 $_CACHE->{$file_name}{'emails'} = [];
132 257 $_CACHE->{$file_name}{'modified'} = 0;
133  
134 257 $UPDATING_CACHE = 1;
135 }
136 }
137  
138 #-------------------------------------------------------------------------------
139  
140 sub _ENTRY_STILL_VALID
141 0 ▶{
Subroutine _ENTRY_STILL_VALID: not called CC=7 SCAR=40.3
142 0 my $file_name = shift;
143  
144 return 0 unless exists $_CACHE->{$file_name} &&
145 defined $_CACHE->{$file_name}{'size'} &&
146 defined $_CACHE->{$file_name}{'time_stamp'} &&
147 # Sanity check the cache to ensure we can at least determine the prologue
148 # length.
149 0 ▶ defined $_CACHE->{$file_name}{'emails'}[0]{'offset'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name} and defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"size"} and defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"time_stamp"} and defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"emails"}[0]{"offset"} 0 0 0
Condition0%0/9
exists $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name} and defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"size"} and defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"time_stamp"} and defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"emails"}[0]{"offset"}
!ll&&!rl&&r
000
exists $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name} and defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"size"} and defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"time_stamp"}
!ll&&!rl&&r
000
exists $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name} and defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"size"}
!ll&&!rl&&r
000
MC/DC0%0/4
exists $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name} and defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"size"} and defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"time_stamp"} and defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"emails"}[0]{"offset"}
exists $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name} defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"size"} defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"time_stamp"} defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"emails"}[0]{"offset"}
Truth table0%0/5
A and B and C and D
A= exists $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}B= defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"size"}C= defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"time_stamp"}D= defined $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"emails"}[0]{"offset"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
150  
151 0 my @stat = stat $file_name;
152  
153 0 ▶ return 0 unless @stat;
Branch: 1 branch
BranchTrueFalseTotal
unless @stat 0 0 0
154  
155 0 my $size = $stat[7];
156 0 my $time_stamp = $stat[9];
157  
158 return ($_CACHE->{$file_name}{'size'} == $size &&
159 0 ▶ $_CACHE->{$file_name}{'time_stamp'} == $time_stamp);
Condition0%0/3
$Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{'size'} == $size && $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{'time_stamp'} == $time_stamp
!ll&&!rl&&r
000
MC/DC0%0/2
$Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{'size'} == $size && $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{'time_stamp'} == $time_stamp
$Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{'size'} == $size $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{'time_stamp'} == $time_stamp
Truth table0%0/3
A && B
A= $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{'size'} == $sizeB= $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{'time_stamp'} == $time_stamp
ABresult
0X0
100
111
160 }
161  
162 #-------------------------------------------------------------------------------
163  
164 sub _READ_CACHE
165 266 ▶{
Subroutine _READ_CACHE: called CC=3 SCAR=11.0
166 266 my $self = shift;
167  
168 266 ▶ return unless -f $_CACHE_OPTIONS{'file_name'};
Branch: 1 branch
BranchTrueFalseTotal
unless -f $Mail::Mbox::MessageParser::MetaInfo::_CACHE_OPTIONS{"file_name"} 113 153 266
169  
170 153 _dprint "Reading cache";
171  
172 # Unserialize using Storable
173 153 local $@;
174  
175 153 eval { $_CACHE = retrieve($_CACHE_OPTIONS{'file_name'}) };
176  
177 153 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 153 0 153
178 {
179 153 $_CACHE = {};
180 153 _dprint "Invalid cache detected, and will be ignored.";
181 153 _dprint "Message from Storable module: \"$@\"";
182 }
183 }
184  
185 #-------------------------------------------------------------------------------
186  
187 sub WRITE_CACHE
188 661 ▶{
Subroutine WRITE_CACHE: called CC=6 SCAR=17.9
Pod documented
189 # In case this is called during cleanup following an error loading
190 # Storable
191 661 ▶ return unless defined $Storable::VERSION;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Storable::VERSION 0 661 661
192  
193 661 ▶ return if $UPDATING_CACHE;
Branch: 1 branch
BranchTrueFalseTotal
if $Mail::Mbox::MessageParser::MetaInfo::UPDATING_CACHE 7 654 661
194  
195 # TODO: Make this cache separate files instead of one big file, to improve
196 # performance.
197 654 my $cache_modified = 0;
198  
199 654 foreach my $file_name (keys %$_CACHE)
200 {
201 473 ▶ if ($_CACHE->{$file_name}{'modified'})
Branch: 1 branch
BranchTrueFalseTotal
if ($Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"modified"}) 253 220 473
202 {
203 253 $cache_modified = 1;
204 253 $_CACHE->{$file_name}{'modified'} = 0;
205 }
206 }
207  
208 654 ▶ unless ($cache_modified)
Branch: 1 branch
BranchTrueFalseTotal
unless ($cache_modified) 401 253 654
209 {
210 401 _dprint "Cache not modified, so no writing is necessary";
211 401 return;
212 }
213  
214 253 _dprint "Cache was modified, so writing is necessary";
215  
216 # The mail box cache may contain sensitive information, so protect it
217 # from prying eyes.
218 253 my $oldmask = umask(077);
219  
220 # Serialize using Storable
221 253 store($_CACHE, $_CACHE_OPTIONS{'file_name'});
222  
223 253 umask($oldmask);
224  
225 253 $_CACHE->{$_CACHE_OPTIONS{'file_name'}}{'modified'} = 0;
226 }
227  
228 #-------------------------------------------------------------------------------
229  
230 # Write the cache when the program exits
231 sub END
232 51 ▶{
Subroutine END: called CC=2 SCAR=7.2
233 51 ▶ _dprint "Exiting and writing cache if necessary"
Branch: 1 branch
BranchTrueFalseTotal
if defined &Mail::Mbox::MessageParser::MetaInfo::_dprint 51 0 51
234 if defined(&_dprint);
235  
236 51 WRITE_CACHE();
237 }
238  
239 51 1;
240  
241 __END__
« blib/lib/Mail/Mbox/MessageParser/Grep.pm ↑ Summary blib/lib/Mail/Mbox/MessageParser/Cache.pm »