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 52 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 52 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 52 ▶use Mail::Mbox::MessageParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 52 ▶use vars qw( $VERSION $_DEBUG @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 52 ▶use vars qw( $_CACHE %_CACHE_OPTIONS $UPDATING_CACHE );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 52 @ISA = qw( Exporter );
12  
13 52 $VERSION = sprintf "%d.%02d%02d", q/0.2.0/ =~ /(\d+)/g;
14  
15 52 *_DEBUG = \$Mail::Mbox::MessageParser::_DEBUG;
16 52 *_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 52 $_CACHE = {};
23  
24 52 %_CACHE_OPTIONS = ();
25  
26 52 $UPDATING_CACHE = 0;
27  
28 #-------------------------------------------------------------------------------
29  
30 sub _LOAD_STORABLE
31 {
32 284 ▶ if (eval {require Storable})
Subroutine _LOAD_STORABLE: called CC=2 SCAR=7.4
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Storable } }) { } 284 0 284
33 {
34 284 import Storable;
35 284 return 1;
36 }
37 else
38 {
39 0 return 0;
40 }
41 }
42  
43 #-------------------------------------------------------------------------------
44  
45 sub SETUP_CACHE
46 {
47 284 ▶ my $cache_options = shift;
Subroutine SETUP_CACHE: called CC=5 SCAR=16.3
Pod documented
48  
49 284 ▶ carp "Need file_name option" unless defined $cache_options->{'file_name'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cache_options->{"file_name"} 0 284 284
50  
51 284 ▶ return "Can not load " . __PACKAGE__ . ": Storable is not installed.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless _LOAD_STORABLE() 0 284 284
52 unless _LOAD_STORABLE();
53
54 # Load Storable if we need to
55 # See if the client is setting up a different cache
56 284 ▶ 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"}) 261 23 284
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
230261
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 261 _dprint "New cache file specified--writing old cache if necessary.";
60 261 WRITE_CACHE();
61 261 $_CACHE = {};
62 }
63  
64 284 %_CACHE_OPTIONS = %$cache_options;
65  
66 284 _READ_CACHE();
67  
68 284 return 'ok';
69 }
70  
71 #-------------------------------------------------------------------------------
72  
73 sub CLEAR_CACHE
74 {
75 unlink $_CACHE_OPTIONS{'file_name'}
76 0 ▶ if defined $_CACHE_OPTIONS{'file_name'} && -f $_CACHE_OPTIONS{'file_name'};
Subroutine CLEAR_CACHE: not called CC=3 SCAR=24.8
Pod documented
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 {
86 275 ▶ my $file_name = shift;
Subroutine _INITIALIZE_ENTRY: called CC=9 SCAR=32.3
87  
88 275 my @stat = stat $file_name;
89  
90 275 ▶ return 0 unless @stat;
Branch: 1 branch
BranchTrueFalseTotal
unless @stat 0 275 275
91  
92 275 my $size = $stat[7];
93 275 my $time_stamp = $stat[9];
94  
95  
96 275 ▶ 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 275 275
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
27500
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 275 ▶ if (exists $_CACHE->{$file_name})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}) { } 0 275 275
120 {
121 0 _dprint "Size or time stamp has changed for file \"" .
122 $file_name . "\". Invalidating cache entry";
123 }
124 else
125 {
126 275 _dprint "Cache is invalid: \"$file_name\" has not yet been parsed";
127 }
128  
129 275 $_CACHE->{$file_name}{'size'} = $size;
130 275 $_CACHE->{$file_name}{'time_stamp'} = $time_stamp;
131 275 $_CACHE->{$file_name}{'emails'} = [];
132 275 $_CACHE->{$file_name}{'modified'} = 0;
133  
134 275 $UPDATING_CACHE = 1;
135 }
136 }
137  
138 #-------------------------------------------------------------------------------
139  
140 sub _ENTRY_STILL_VALID
141 {
142 0 ▶ my $file_name = shift;
Subroutine _ENTRY_STILL_VALID: not called CC=7 SCAR=40.3
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 {
166 284 ▶ my $self = shift;
Subroutine _READ_CACHE: called CC=3 SCAR=11.0
167  
168 284 ▶ return unless -f $_CACHE_OPTIONS{'file_name'};
Branch: 1 branch
BranchTrueFalseTotal
unless -f $Mail::Mbox::MessageParser::MetaInfo::_CACHE_OPTIONS{"file_name"} 121 163 284
169  
170 163 _dprint "Reading cache";
171  
172 # Unserialize using Storable
173 163 local $@;
174  
175 163 eval { $_CACHE = retrieve($_CACHE_OPTIONS{'file_name'}) };
176  
177 163 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 163 0 163
178 {
179 163 $_CACHE = {};
180 163 _dprint "Invalid cache detected, and will be ignored.";
181 163 _dprint "Message from Storable module: \"$@\"";
182 }
183 }
184  
185 #-------------------------------------------------------------------------------
186  
187 sub WRITE_CACHE
188 {
189 # In case this is called during cleanup following an error loading
190 # Storable
191 706 ▶ return unless defined $Storable::VERSION;
Subroutine WRITE_CACHE: called CC=6 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Storable::VERSION 0 706 706
192  
193 706 ▶ return if $UPDATING_CACHE;
Branch: 1 branch
BranchTrueFalseTotal
if $Mail::Mbox::MessageParser::MetaInfo::UPDATING_CACHE 7 699 706
194  
195 # TODO: Make this cache separate files instead of one big file, to improve
196 # performance.
197 699 my $cache_modified = 0;
198  
199 699 foreach my $file_name (keys %$_CACHE)
200 {
201 507 ▶ if ($_CACHE->{$file_name}{'modified'})
Branch: 1 branch
BranchTrueFalseTotal
if ($Mail::Mbox::MessageParser::MetaInfo::_CACHE->{$file_name}{"modified"}) 271 236 507
202 {
203 271 $cache_modified = 1;
204 271 $_CACHE->{$file_name}{'modified'} = 0;
205 }
206 }
207  
208 699 ▶ unless ($cache_modified)
Branch: 1 branch
BranchTrueFalseTotal
unless ($cache_modified) 428 271 699
209 {
210 428 _dprint "Cache not modified, so no writing is necessary";
211 428 return;
212 }
213  
214 271 _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 271 my $oldmask = umask(077);
219  
220 # Serialize using Storable
221 271 store($_CACHE, $_CACHE_OPTIONS{'file_name'});
222  
223 271 umask($oldmask);
224  
225 271 $_CACHE->{$_CACHE_OPTIONS{'file_name'}}{'modified'} = 0;
226 }
227  
228 #-------------------------------------------------------------------------------
229  
230 # Write the cache when the program exits
231 sub END
232 {
233 52 ▶ _dprint "Exiting and writing cache if necessary"
Subroutine END: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if defined &Mail::Mbox::MessageParser::MetaInfo::_dprint 52 0 52
234 if defined(&_dprint);
235  
236 52 WRITE_CACHE();
237 }
238  
239 52 1;
240  
241 __END__
« blib/lib/Mail/Mbox/MessageParser/Grep.pm ↑ Summary blib/lib/Mail/Mbox/MessageParser/Cache.pm »