Box/File.pm

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