Box/File.pm

filter: Statementstmt 23.9% 67 / 280 Branchbran 1.4% 2 / 140 Conditioncond 6.3% 4 / 63 MC/DCmcdc 0.0% 0 / 40 Subroutinesub 52.6% 20 / 38 Podpod 87.5% 14 / 16 totaltotal 18.5% 107 / 577 CC 99 SCAR 46.5
CC
99
Cov
15%
_write_inplace
240.0
init
194.5
create
156.0
CRAP 104.3
« Box/Maildir/Message.pm ↑ Summary Box/Search/Grep.pm »
1 # Copyrights 2001-2020 by [Mark Overmeer].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.02.
5 # This code is part of distribution Mail-Box. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package Mail::Box::File;
10 25 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 25 $VERSION = '3.009';
12  
13 25 ▶use base 'Mail::Box';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 25 ▶use filetest 'access';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 25 ▶use Mail::Box::File::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 25 ▶use Mail::Message::Body::Lines;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 25 ▶use Mail::Message::Body::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 25 ▶use Mail::Message::Body::Delayed;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 25 ▶use Mail::Message::Body::Multipart;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 25 ▶use Mail::Message::Head;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 25 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 25 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 25 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 25 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 25 ▶use POSIX ':unistd_h';
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 # tell() is not available for open(my $fh) on perl versions <= 5.10 So,
36 # we need to stick to IO::File syntax.
37 25 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 25 my $windows;
40 25 ▶BEGIN { $windows = $^O =~ m/mswin32/i }
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42  
43 25 ▶my $default_folder_dir = exists $ENV{HOME} ? $ENV{HOME} . '/Mail' : '.';
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'HOME'} ? : 25 0 25
44  
45 sub _default_body_type($$)
46 0 ▶{ my $size = shift->guessBodySize || 0;
Subroutine _default_body_type: not called CC=3 SCAR=24.8
Condition0%0/2
(shift())->guessBodySize || 0
l!l
00
MC/DC0%0/1
(shift())->guessBodySize || 0
(shift())->guessBodySize
Truth table0%0/2
A
A= (shift())->guessBodySize
Aresult
11
00
47 0 ▶ 'Mail::Message::Body::'.($size > 100000 ? 'File' : 'Lines');
Branch: 1 branch
BranchTrueFalseTotal
$size > 100000 ? : 0 0 0
48 }
49  
50 sub init($)
51 15 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=17 SCAR=52.7
Pod undocumented
52 15 ▶ $args->{folderdir} ||= $default_folder_dir;
Condition66%2/3
$args->{"folderdir"} ||= $default_folder_dir
l!l&&r!l&&!r
1410
MC/DC0%0/2
$args->{"folderdir"} ||= $default_folder_dir
$args->{"folderdir"} $default_folder_dir
Truth table66%2/3
A ||= B
A= $args->{"folderdir"}B= $default_folder_dir
ABresult
1X1
011
000
53 15 ▶ $args->{body_type} ||= \&_default_body_type;
Condition50%1/2
$args->{"body_type"} ||= \&Mail::Box::File::_default_body_type
l!l
015
MC/DC0%0/1
$args->{"body_type"} ||= \&Mail::Box::File::_default_body_type
$args->{"body_type"}
Truth table50%1/2
A
A= $args->{"body_type"}
Aresult
11
00
54 15 ▶ $args->{lock_file} ||= '--'; # to be resolved later
Condition50%1/2
$args->{"lock_file"} ||= "--"
l!l
015
MC/DC0%0/1
$args->{"lock_file"} ||= "--"
$args->{"lock_file"}
Truth table50%1/2
A
A= $args->{"lock_file"}
Aresult
11
00
55  
56 15 ▶ defined $self->SUPER::init($args)
Branch: 1 branch
BranchTrueFalseTotal
defined $self->SUPER::init($args) or return 0 0 0
57 or return;
58  
59 0 my $class = ref $self;
60  
61 my $filename = $self->{MBF_filename}
62 0 = $self->folderToFilename
63 ( $self->name
64 , $self->folderdir
65 );
66  
67 0 ▶ if(-e $filename) {;} # Folder already exists
Branch: 2 branches
BranchTrueFalseTotal
if (-e $filename) { } 0 0 0
elsif ($args->{'create'} and $class->create($args->{'folder'}, %$args)) { } 0 0 0
68 ▶ elsif($args->{create} && $class->create($args->{folder}, %$args)) {;}
Condition0%0/3
$args->{"create"} and $class->create($args->{"folder"}, %$args)
!ll&&!rl&&r
000
MC/DC0%0/2
$args->{"create"} and $class->create($args->{"folder"}, %$args)
$args->{"create"} $class->create($args->{"folder"}, %$args)
Truth table0%0/3
A and B
A= $args->{"create"}B= $class->create($args->{"folder"}, %$args)
ABresult
0X0
100
111
69 else
70 0 { $self->log(PROGRESS =>
71 "File $filename for folder $self does not exist.");
72 0 return;
73 }
74  
75 0 $self->{MBF_policy} = $args->{write_policy};
76  
77 # Lock the folder.
78  
79 0 my $locker = $self->locker;
80  
81 0 my $lockfile = $locker->filename;
82 0 ▶ if($lockfile eq '--') # filename to be used not resolved yet
Branch: 1 branch
BranchTrueFalseTotal
if ($lockfile eq "--") 0 0 0
83 0 { my $lockdir = $filename;
84 0 $lockdir =~ s!/([^/]*)$!!;
85 0 ▶ my $extension = $args->{lock_extension} || '.lock';
Condition0%0/2
$args->{'lock_extension'} || '.lock'
l!l
00
MC/DC0%0/1
$args->{'lock_extension'} || '.lock'
$args->{'lock_extension'}
Truth table0%0/2
A
A= $args->{'lock_extension'}
Aresult
11
00
86  
87 0 ▶ $locker->filename
Branch: 2 branches
BranchTrueFalseTotal
'File::Spec'->file_name_is_absolute($extension) ? : 0 0 0
$extension =~ /^\./ ? : 0 0 0
88 ( File::Spec->file_name_is_absolute($extension) ? $extension
89 : $extension =~ m!^\.! ? "$filename$extension"
90 : File::Spec->catfile($lockdir, $extension)
91 );
92 }
93  
94 0 ▶ unless($locker->lock)
Branch: 1 branch
BranchTrueFalseTotal
unless ($locker->lock) 0 0 0
95 0 { $self->log(ERROR => "Cannot get a lock on $class folder $self.");
96 0 return;
97 }
98  
99 # Check if we can write to the folder, if we need to.
100  
101 0 ▶ if($self->writable && ! -w $filename)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->writable and not -w $filename) 0 0 0
Condition0%0/3
$self->writable and not -w $filename
!ll&&!rl&&r
000
MC/DC0%0/2
$self->writable and not -w $filename
$self->writable not -w $filename
Truth table0%0/3
A and not(B)
A= $self->writableB= not -w $filename
ABresult
0X0
100
111
102 0 { $self->log(WARNING => "Folder $self file $filename is write-protected.");
103 0 $self->{MB_access} = 'r';
104 }
105  
106 # Start parser if reading is required.
107  
108 0 ▶ $self->{MB_access} !~ m/r/ ? $self
Branch: 2 branches
BranchTrueFalseTotal
!($self->{'MB_access'} =~ /r/) ? : 0 0 0
$self->parser ? : 0 0 0
109 : $self->parser ? $self
110 : undef;
111 }
112  
113  
114 sub create($@)
115 0 ▶{ my ($thingy, $name, %args) = @_;
Subroutine create: not called CC=12 SCAR=50.5
Pod documented
116 0 ▶ my $class = ref $thingy || $thingy;
Condition0%0/3
ref $thingy || $thingy
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $thingy || $thingy
ref $thingy $thingy
Truth table0%0/3
A || B
A= ref $thingyB= $thingy
ABresult
1X1
011
000
117 0 ▶ my $folderdir = $args{folderdir} || $default_folder_dir;
Condition0%0/3
$args{'folderdir'} || $default_folder_dir
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'folderdir'} || $default_folder_dir
$args{'folderdir'} $default_folder_dir
Truth table0%0/3
A || B
A= $args{'folderdir'}B= $default_folder_dir
ABresult
1X1
011
000
118 0 my $subext = $args{subfolder_extension}; # not always available
119 0 my $filename = $class->folderToFilename($name, $folderdir, $subext);
120  
121 0 ▶ return $class if -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $filename 0 0 0
122  
123 0 my $dir = dirname $filename;
124 0 ▶ if(-f $dir && defined $subext)
Branch: 1 branch
BranchTrueFalseTotal
if (-f $dir and defined $subext) 0 0 0
Condition0%0/3
-f $dir and defined $subext
!ll&&!rl&&r
000
MC/DC0%0/2
-f $dir and defined $subext
-f $dir defined $subext
Truth table0%0/3
A and B
A= -f $dirB= defined $subext
ABresult
0X0
100
111
125 0 { $dir .= $subext;
126 0 $filename = File::Spec->catfile($dir, basename $filename);
127 }
128  
129 0 ▶ $class->log(ERROR => "Cannot create directory $dir for folder $name: $!"),return
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir or mkdir $dir, 493 0 0 0
Condition0%0/3
-d $dir or mkdir $dir, 493
l!l&&r!l&&!r
000
MC/DC0%0/2
-d $dir or mkdir $dir, 493
-d $dir mkdir $dir, 493
Truth table0%0/3
A or B
A= -d $dirB= mkdir $dir, 493
ABresult
1X1
011
000
130 unless -d $dir || mkdir $dir, 0755;
131  
132 0 ▶ $class->moveAwaySubFolder($filename, $subext)
Branch: 1 branch
BranchTrueFalseTotal
if -d $filename and defined $subext 0 0 0
Condition0%0/3
-d $filename and defined $subext
!ll&&!rl&&r
000
MC/DC0%0/2
-d $filename and defined $subext
-d $filename defined $subext
Truth table0%0/3
A and B
A= -d $filenameB= defined $subext
ABresult
0X0
100
111
133 if -d $filename && defined $subext;
134  
135 0 my $create = IO::File->new($filename, 'w');
136 0 ▶ unless($create)
Branch: 1 branch
BranchTrueFalseTotal
unless ($create) 0 0 0
137 0 { $class->log(WARNING => "Cannot create folder file $name: $!");
138 0 return;
139 }
140  
141 0 $class->log(PROGRESS => "Created folder $name.");
142 0 ▶ $create->close or return;
Branch: 1 branch
BranchTrueFalseTotal
$create->close or return 0 0 0
143 0 $class;
144 }
145  
146 sub foundIn($@)
147 0 ▶{ my $class = shift;
Subroutine foundIn: not called CC=5 SCAR=34.0
Pod documented
148 0 ▶ my $name = @_ % 2 ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 0 0
149 0 my %args = @_;
150 0 ▶ $name ||= $args{folder} or return;
Branch: 1 branch
BranchTrueFalseTotal
$name ||= $args{"folder"} or return 0 0 0
Condition0%0/3
$name ||= $args{"folder"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$name ||= $args{"folder"}
$name $args{"folder"}
Truth table0%0/3
A ||= B
A= $nameB= $args{"folder"}
ABresult
1X1
011
000
151  
152 0 ▶ my $folderdir = $args{folderdir} || $default_folder_dir;
Condition0%0/3
$args{'folderdir'} || $default_folder_dir
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'folderdir'} || $default_folder_dir
$args{'folderdir'} $default_folder_dir
Truth table0%0/3
A || B
A= $args{'folderdir'}B= $default_folder_dir
ABresult
1X1
011
000
153 0 my $filename = $class->folderToFilename($name, $folderdir);
154  
155 0 -f $filename;
156 }
157  
158 sub organization() { 'FILE' }
159  
160 sub size()
161 0 ▶{ my $self = shift;
Subroutine size: not called CC=2 SCAR=17.9
Pod documented
162 0 ▶ $self->isModified ? $self->SUPER::size : -s $self->filename;
Branch: 1 branch
BranchTrueFalseTotal
$self->isModified ? : 0 0 0
163 }
164  
165 sub close(@)
166 15 ▶{ my $self = $_[0]; # be careful, we want to set the calling
Subroutine close: called CC=2 SCAR=7.1
Pod documented
167 15 undef $_[0]; # ref to undef, as the SUPER does.
168 15 shift;
169  
170 15 my $rc = $self->SUPER::close(@_);
171  
172 15 ▶ if(my $parser = delete $self->{MBF_parser}) { $parser->stop }
Branch: 1 branch
BranchTrueFalseTotal
if (my $parser = delete $self->{"MBF_parser"}) 0 15 15
173  
174 15 $rc;
175 }
176  
177  
178 sub appendMessages(@)
179 0 ▶{ my $class = shift;
Subroutine appendMessages: not called CC=10 SCAR=47.0
Pod documented
180 0 my %args = @_;
181  
182 my @messages
183 = exists $args{message} ? $args{message}
184 0 ▶ : exists $args{messages} ? @{$args{messages}}
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'message'} ? : 0 0 0
exists $args{'messages'} ? : 0 0 0
185 : return ();
186  
187 0 ▶ my $folder = $class->new(lock_type => 'NONE', @_, access => 'w+')
Branch: 1 branch
BranchTrueFalseTotal
my $folder = $class->new("lock_type", "NONE", @_, "access", "w+") or return () 0 0 0
188 or return ();
189
190 0 my $filename = $folder->filename;
191  
192 0 my $out = IO::File->new($filename, 'a');
193 0 ▶ unless($out)
Branch: 1 branch
BranchTrueFalseTotal
unless ($out) 0 0 0
194 0 { $class->log(ERROR => "Cannot append messages to folder file $filename: $!");
195 0 return ();
196 }
197  
198 0 my $msgtype = $class.'::Message';
199 0 my @coerced;
200  
201 0 foreach my $msg (@messages)
202 0 ▶ { my $coerced
Branch: 2 branches
BranchTrueFalseTotal
$msg->isa($msgtype) ? : 0 0 0
$msg->can('clone') ? : 0 0 0
203 = $msg->isa($msgtype) ? $msg
204 : $msg->can('clone') ? $msgtype->coerce($msg->clone)
205 : $msgtype->coerce($msg);
206  
207 0 $coerced->write($out);
208 0 push @coerced, $coerced;
209 }
210  
211 0 my $ok = $folder->close;
212 0 ▶ $out->close && $ok
Branch: 1 branch
BranchTrueFalseTotal
$out->close and $ok or return 0 0 0 0
Condition0%0/3
$out->close and $ok
!ll&&!rl&&r
000
MC/DC0%0/2
$out->close and $ok
$out->close $ok
Truth table0%0/3
A and B
A= $out->closeB= $ok
ABresult
0X0
100
111
213 or return 0;
214  
215 0 @coerced;
216 }
217  
218 #-------------------------------------------
219  
220  
221 0 ▶sub filename() { shift->{MBF_filename} }
Subroutine filename: not called CC=1 SCAR=6.9
Pod documented
222  
223 #-------------------------------------------
224  
225  
226 sub parser()
227 0 ▶{ my $self = shift;
Subroutine parser: not called CC=6 SCAR=37.4
Pod documented
228  
229 return $self->{MBF_parser}
230 0 ▶ if defined $self->{MBF_parser};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"MBF_parser"} 0 0 0
231  
232 0 my $source = $self->filename;
233  
234 0 ▶ my $mode = $self->{MB_access} || 'r';
Condition0%0/2
$self->{'MB_access'} || 'r'
l!l
00
MC/DC0%0/1
$self->{'MB_access'} || 'r'
$self->{'MB_access'}
Truth table0%0/2
A
A= $self->{'MB_access'}
Aresult
11
00
235 0 ▶ $mode = 'r+' if $mode eq 'rw' || $mode eq 'a';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "rw" or $mode eq "a" 0 0 0
Condition0%0/3
$mode eq "rw" or $mode eq "a"
l!l&&r!l&&!r
000
MC/DC0%0/2
$mode eq "rw" or $mode eq "a"
$mode eq "rw" $mode eq "a"
Truth table0%0/3
A or B
A= $mode eq "rw"B= $mode eq "a"
ABresult
1X1
011
000
236  
237 my $parser = $self->{MBF_parser}
238 = Mail::Box::Parser->new
239 ( filename => $source
240 , mode => $mode
241 , trusted => $self->{MB_trusted}
242 , fix_header_errors => $self->{MB_fix_headers}
243 0 ▶ , $self->logSettings
Branch: 1 branch
BranchTrueFalseTotal
my $parser = $self->{"MBF_parser"} = "Mail::Box::Parser"->new("filename", $source, "mode", $mode, "trusted", $self->{"MB_trusted"}, "fix_header_errors", $self->{"MB_fix_headers"}, $self->logSettings) or return 0 0 0
244 ) or return;
245  
246 0 $parser->pushSeparator('From ');
247 0 $parser;
248 }
249  
250 sub readMessages(@)
251 0 ▶{ my ($self, %args) = @_;
Subroutine readMessages: not called CC=1 SCAR=6.9
Pod documented
252  
253 $self->messageCreateOptions
254 ( $args{message_type}
255 , $self->logSettings
256 , folder => $self
257 , head_type => $args{head_type}
258 , field_type => $args{field_type}
259 , trusted => $args{trusted}
260 0 );
261  
262 0 $self->updateMessages;
263 }
264
265  
266 sub updateMessages(@)
267 0 ▶{ my ($self, %args) = @_;
Subroutine updateMessages: not called CC=6 SCAR=37.4
Pod documented
268 0 ▶ my $parser = $self->parser or return;
Branch: 1 branch
BranchTrueFalseTotal
my $parser = $self->parser or return 0 0 0
269  
270 # On a directory, simulate an empty folder with only subfolders.
271 0 my $filename = $self->filename;
272 0 ▶ return $self if -d $filename;
Branch: 1 branch
BranchTrueFalseTotal
if -d $filename 0 0 0
273  
274 0 ▶ if(my $last = $self->message(-1))
Branch: 1 branch
BranchTrueFalseTotal
if (my $last = $self->message(-1)) 0 0 0
275 0 { (undef, my $end) = $last->fileLocation;
276 0 $parser->filePosition($end);
277 }
278  
279 0 my ($type, @msgopts) = $self->messageCreateOptions;
280 0 my $count = 0;
281  
282 0 while(1)
283 0 { my $message = $type->new(@msgopts);
284 0 ▶ last unless $message->readFromParser($parser);
Branch: 1 branch
BranchTrueFalseTotal
unless $message->readFromParser($parser) 0 0 0
285 0 $self->storeMessage($message);
286 0 $count++;
287 }
288  
289 0 ▶ $self->log(PROGRESS => "Found $count new messages in $filename")
Branch: 1 branch
BranchTrueFalseTotal
if $count 0 0 0
290 if $count;
291  
292 0 $self;
293 }
294  
295  
296 sub messageCreateOptions(@)
297 0 ▶{ my ($self, @options) = @_;
Subroutine messageCreateOptions: not called CC=5 SCAR=34.0
Pod documented
298 0 ▶ if(@options)
Branch: 1 branch
BranchTrueFalseTotal
if (@options) 0 0 0
299 0 ▶ { ref($_) && ref($_) =~ m/^Mail::/ && weaken $_ for @options;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ and ref($_) =~ /^Mail::/ and $_->weaken 0 0 0
Condition0%0/3
ref $_ and ref($_) =~ /^Mail::/
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_ and ref($_) =~ /^Mail::/
ref $_ ref($_) =~ /^Mail::/
Truth table0%0/3
A and B
A= ref $_B= ref($_) =~ /^Mail::/
ABresult
0X0
100
111
300 0 $self->{MBF_create_options} = \@options;
301 }
302
303 0 @{$self->{MBF_create_options}};
304 }
305  
306  
307 sub moveAwaySubFolder($$)
308 0 ▶{ my ($self, $dir, $extension) = @_;
Subroutine moveAwaySubFolder: not called CC=2 SCAR=17.9
Pod documented
309 0 ▶ $self->log(ERROR => "Cannot move away sub-folder $dir")
Branch: 1 branch
BranchTrueFalseTotal
unless move($dir, $dir . $extension) 0 0 0
310 unless move $dir, $dir.$extension;
311 0 $self;
312 }
313  
314 sub delete(@)
315 0 ▶{ my $self = shift;
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
316 0 $self->SUPER::delete(@_);
317 0 unlink $self->filename;
318 }
319  
320  
321 sub writeMessages($)
322 0 ▶{ my ($self, $args) = @_;
Subroutine writeMessages: not called CC=11 SCAR=48.8
Pod documented
323  
324 0 my $filename = $self->filename;
325 0 ▶ if( ! @{$args->{messages}} && $self->{MB_remove_empty})
Branch: 1 branch
BranchTrueFalseTotal
if (not @{$args->{"messages"};} and $self->{"MB_remove_empty"}) 0 0 0
Condition0%0/3
not @{$args->{"messages"};} and $self->{"MB_remove_empty"}
!ll&&!rl&&r
000
MC/DC0%0/2
not @{$args->{"messages"};} and $self->{"MB_remove_empty"}
not @{$args->{"messages"};} $self->{"MB_remove_empty"}
Truth table0%0/3
not(A) and B
A= not @{$args->{"messages"};}B= $self->{"MB_remove_empty"}
ABresult
0X0
100
111
326 0 ▶ { $self->log(WARNING => "Cannot remove folder $self file $filename: $!")
Branch: 1 branch
BranchTrueFalseTotal
unless unlink $filename 0 0 0
327 unless unlink $filename;
328 0 return $self;
329 }
330  
331 0 ▶ my $policy = exists $args->{policy} ? $args->{policy} : $self->{MBF_policy};
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'policy'} ? : 0 0 0
332 0 ▶ $policy ||= '';
Condition0%0/2
$policy ||= ""
l!l
00
MC/DC0%0/1
$policy ||= ""
$policy
Truth table0%0/2
A
A= $policy
Aresult
11
00
333  
334 0 ▶ my $success
Branch: 4 branches
BranchTrueFalseTotal
!(-e $filename) ? : 0 0 0
$policy eq 'INPLACE' ? : 0 0 0
$policy eq 'REPLACE' ? : 0 0 0
$self->_write_replace($args) ? : 0 0 0
335 = ! -e $filename ? $self->_write_new($args)
336 : $policy eq 'INPLACE' ? $self->_write_inplace($args)
337 : $policy eq 'REPLACE' ? $self->_write_replace($args)
338 : $self->_write_replace($args) ? 1
339 : $self->_write_inplace($args);
340  
341 0 ▶ unless($success)
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
342 0 { $self->log(ERROR => "Unable to update folder $self.");
343 0 return;
344 }
345  
346 # $self->parser->restart;
347 0 $self;
348 }
349  
350 sub _write_new($)
351 0 ▶{ my ($self, $args) = @_;
Subroutine _write_new: not called CC=4 SCAR=30.0
352  
353 0 my $filename = $self->filename;
354 0 my $new = IO::File->new($filename, 'w');
355 0 ▶ return 0 unless defined $new;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new 0 0 0
356  
357 0 $new->binmode;
358 0 $_->write($new) foreach @{$args->{messages}};
359  
360 0 ▶ $new->close or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$new->close or return 0 0 0 0
361  
362 $self->log(PROGRESS =>
363 0 "Wrote new folder $self with ".@{$args->{messages}}."msgs.");
364 0 1;
365 }
366  
367 # First write to a new file, then replace the source folder in one
368 # move. This is much slower than inplace update, but it is safer,
369 # The folder is always in the right shape, even if the program is
370 # interrupted.
371  
372 sub _write_replace($)
373 0 ▶{ my ($self, $args) = @_;
Subroutine _write_replace: not called CC=12 SCAR=50.5
374  
375 0 my $filename = $self->filename;
376 0 my $tmpnew = $self->tmpNewFolder($filename);
377  
378 0 ▶ my $new = IO::File->new($tmpnew, 'w') or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $new = "IO::File"->new($tmpnew, "w") or return 0 0 0 0
379 0 $new->binmode;
380  
381 0 ▶ my $old = IO::File->new($filename, 'r') or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $old = "IO::File"->new($filename, "r") or return 0 0 0 0
382 0 $old->binmode;
383  
384 0 my ($reprint, $kept) = (0,0);
385  
386 0 foreach my $message ( @{$args->{messages}} )
387 {
388 0 my $newbegin = $new->tell;
389 0 my $oldbegin = $message->fileLocation;
390  
391 0 ▶ if($message->isModified)
Branch: 1 branch
BranchTrueFalseTotal
if ($message->isModified) 0 0 0
392 0 { $message->write($new);
393 0 ▶ $message->moveLocation($newbegin - $oldbegin)
Branch: 1 branch
BranchTrueFalseTotal
if defined $oldbegin 0 0 0
394 if defined $oldbegin;
395 0 $reprint++;
396 0 next;
397 }
398  
399 0 my ($begin, $end) = $message->fileLocation;
400 0 my $need = $end-$begin;
401  
402 0 $old->seek($begin, 0);
403 0 my $whole;
404 0 my $size = $old->read($whole, $need);
405  
406 0 ▶ $self->log(ERROR => "File too short to get write message "
Branch: 1 branch
BranchTrueFalseTotal
unless $size == $need 0 0 0
407 . $message->seqnr. " ($size, $need)")
408 unless $size == $need;
409  
410 0 $new->print($whole);
411 0 ▶ $new->print($Mail::Message::crlf_platform ? "\r\n" : "\n");
Branch: 1 branch
BranchTrueFalseTotal
$Mail::Message::crlf_platform ? : 0 0 0
412  
413 0 $message->moveLocation($newbegin - $oldbegin);
414 0 $kept++;
415 }
416  
417 0 my $ok = $new->close;
418 0 ▶ $old->close && $ok
Branch: 1 branch
BranchTrueFalseTotal
$old->close and $ok or return 0 0 0 0
Condition0%0/3
$old->close and $ok
!ll&&!rl&&r
000
MC/DC0%0/2
$old->close and $ok
$old->close $ok
Truth table0%0/3
A and B
A= $old->closeB= $ok
ABresult
0X0
100
111
419 or return 0;
420  
421 0 ▶ if($windows)
Branch: 1 branch
BranchTrueFalseTotal
if ($windows) 0 0 0
422 { # Windows does not like to move to existing filenames
423 0 unlink $filename;
424  
425 # Windows cannot move to files which are opened.
426 0 $self->parser->closeFile;
427 }
428  
429 0 ▶ unless(move $tmpnew, $filename)
Branch: 1 branch
BranchTrueFalseTotal
unless (move($tmpnew, $filename)) 0 0 0
430 0 { $self->log(WARNING =>
431 "Cannot replace $filename by $tmpnew, to update folder $self: $!");
432  
433 0 unlink $tmpnew;
434 0 return 0;
435 }
436  
437 0 $self->log(PROGRESS => "Folder $self replaced ($kept, $reprint)");
438 0 1;
439 }
440  
441 # Inplace is currently very poorly implemented. From the first
442 # location where changes appear, all messages are rewritten.
443  
444 sub _write_inplace($)
445 0 ▶{ my ($self, $args) = @_;
Subroutine _write_inplace: not called CC=15 SCAR=54.8
446  
447 0 my @messages = @{$args->{messages}};
448 0 my $last;
449  
450 0 my ($msgnr, $kept) = (0, 0);
451 0 ▶ while(@messages)
Branch: 1 branch
BranchTrueFalseTotal
if (@messages) 0 0 0
452 0 { my $next = $messages[0];
453 0 ▶ last if $next->isModified || $next->seqnr!=$msgnr++;
Branch: 1 branch
BranchTrueFalseTotal
if $next->isModified or $next->seqnr != $msgnr++ 0 0 0
Condition0%0/3
$next->isModified or $next->seqnr != $msgnr++
l!l&&r!l&&!r
000
MC/DC0%0/2
$next->isModified or $next->seqnr != $msgnr++
$next->isModified $next->seqnr != $msgnr++
Truth table0%0/3
A or B
A= $next->isModifiedB= $next->seqnr != $msgnr++
ABresult
1X1
011
000
454 0 $last = shift @messages;
455 0 $kept++;
456 }
457  
458 0 ▶ if(@messages==0 && $msgnr==$self->messages)
Branch: 1 branch
BranchTrueFalseTotal
if (@messages == 0 and $msgnr == $self->messages) 0 0 0
Condition0%0/3
@messages == 0 and $msgnr == $self->messages
!ll&&!rl&&r
000
MC/DC0%0/2
@messages == 0 and $msgnr == $self->messages
@messages == 0 $msgnr == $self->messages
Truth table0%0/3
A and B
A= @messages == 0B= $msgnr == $self->messages
ABresult
0X0
100
111
459 0 { $self->log(PROGRESS => "No changes to be written to $self.");
460 0 return 1;
461 }
462  
463 0 $_->body->load foreach @messages;
464  
465 0 ▶ my $mode = $^O eq 'MSWin32' ? 'a' : 'r+';
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 0 0
466 0 my $filename = $self->filename;
467 0 ▶ my $old = IO::File->new($filename, $mode) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $old = "IO::File"->new($filename, $mode) or return 0 0 0 0
468  
469 # Chop the folder after the messages which does not have to change.
470  
471 0 ▶ my $end = defined $last ? ($last->fileLocation)[1] : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $last ? : 0 0 0
472  
473 0 $end =~ m/(.*)/; # untaint, only required by perl5.6.1
474 0 $end = $1;
475  
476 0 ▶ unless($old->truncate($end))
Branch: 1 branch
BranchTrueFalseTotal
unless ($old->truncate($end)) 0 0 0
477 { # truncate impossible: try replace writing
478 0 $old->close;
479 0 return 0;
480 }
481  
482 0 ▶ unless(@messages)
Branch: 1 branch
BranchTrueFalseTotal
unless (@messages) 0 0 0
483 { # All further messages only are flagged to be deleted
484 0 ▶ $old->close or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$old->close or return 0 0 0 0
485 0 $self->log(PROGRESS => "Folder $self shortened in-place ($kept kept)");
486 0 return 1;
487 }
488  
489 # go to the end of the truncated output file.
490 0 $old->seek(0, 2);
491  
492 # Print the messages which have to move.
493 0 my $printed = @messages;
494 0 foreach my $message (@messages)
495 0 { my $oldbegin = $message->fileLocation;
496 0 my $newbegin = $old->tell;
497 0 $message->write($old);
498 0 $message->moveLocation($newbegin - $oldbegin);
499 }
500  
501 0 ▶ $old->close or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$old->close or return 0 0 0 0
502 0 $self->log(PROGRESS => "Folder $self updated in-place ($kept, $printed)");
503 0 1;
504 }
505  
506 #-------------------------------------------
507  
508  
509 sub folderToFilename($$;$)
510 0 ▶{ my ($thing, $name, $folderdir) = @_;
Subroutine folderToFilename: not called CC=2 SCAR=17.9
Pod documented
511  
512 0 ▶ substr $name, 0, 1, $folderdir
Branch: 1 branch
BranchTrueFalseTotal
if 0 0 0
513 if substr $name, 0, 1 eq '=';
514  
515 0 $name;
516 }
517  
518 0 ▶sub tmpNewFolder($) { shift->filename . '.tmp' }
Subroutine tmpNewFolder: not called CC=1 SCAR=6.9
Pod undocumented
519  
520 #-------------------------------------------
521  
522  
523 25 1;
« Box/Maildir/Message.pm ↑ Summary Box/Search/Grep.pm »