Box/Mbox.pm

filter: Statementstmt 98.2% 111 / 113 Branchbran 81.0% 47 / 58 Conditioncond 62.9% 34 / 54 MC/DCmcdc 22.8% 8 / 35 Subroutinesub 100% 16 / 16 Podpod 88.8% 8 / 9 totaltotal 78.6% 224 / 285 CC 50 SCAR 26.8
CC
50
Cov
85%
listSubFolders
23.0
foundIn
19.9
folderToFilename
8.0
CRAP 14.6
« Box/Test.pm ↑ Summary Box/Search/Grep.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::Mbox;{
13 25 our $VERSION = '4.02';
14 }
15  
16 25 ▶use parent 'Mail::Box::File';
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// ];
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 25 ▶use Mail::Box::Mbox::Message ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 25 ▶use File::Spec::Functions qw/catdir catfile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 #--------------------
29  
30 25 ▶our $default_folder_dir = exists $ENV{HOME} ? $ENV{HOME} . '/Mail' : '.';
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'HOME'} ? : 25 0 25
31 25 our $default_sub_extension = '.d';
32  
33 sub init($)
34 49 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=2 SCAR=7.0
Pod undocumented
35 49 ▶ $self->{MBM_sub_ext} = $args->{subfolder_extension} || $default_sub_extension;
Condition66%2/3
$args->{'subfolder_extension'} || $Mail::Box::Mbox::default_sub_extension
l!l&&r!l&&!r
11380
MC/DC0%0/2
$args->{'subfolder_extension'} || $Mail::Box::Mbox::default_sub_extension
$args->{'subfolder_extension'} $Mail::Box::Mbox::default_sub_extension
Truth table66%2/3
A || B
A= $args->{'subfolder_extension'}B= $Mail::Box::Mbox::default_sub_extension
ABresult
1X1
011
000
36 49 $self->SUPER::init($args);
37 }
38  
39  
40 sub create($@)
41 11 ▶{ my ($thingy, $name, %args) = @_;
Subroutine create: called CC=4 SCAR=15.5
Pod documented
42 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
43 11 ▶ $args{folderdir} ||= $default_folder_dir;
Condition33%1/3
$args{"folderdir"} ||= $Mail::Box::Mbox::default_folder_dir
l!l&&r!l&&!r
1100
MC/DC0%0/2
$args{"folderdir"} ||= $Mail::Box::Mbox::default_folder_dir
$args{"folderdir"} $Mail::Box::Mbox::default_folder_dir
Truth table33%1/3
A ||= B
A= $args{"folderdir"}B= $Mail::Box::Mbox::default_folder_dir
ABresult
1X1
011
000
44 11 ▶ $args{subfolder_extension} ||= $default_sub_extension;
Condition66%2/3
$args{"subfolder_extension"} ||= $Mail::Box::Mbox::default_sub_extension
l!l&&r!l&&!r
380
MC/DC0%0/2
$args{"subfolder_extension"} ||= $Mail::Box::Mbox::default_sub_extension
$args{"subfolder_extension"} $Mail::Box::Mbox::default_sub_extension
Truth table66%2/3
A ||= B
A= $args{"subfolder_extension"}B= $Mail::Box::Mbox::default_sub_extension
ABresult
1X1
011
000
45  
46 11 $class->SUPER::create($name, %args);
47 }
48  
49 #--------------------
50  
51 87 ▶sub subfolderExtension() { $_[0]->{MBM_sub_ext} }
Subroutine subfolderExtension: called CC=1 SCAR=0.0
Pod documented
52  
53 sub delete(@)
54 12 ▶{ my $self = shift;
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
55 12 $self->SUPER::delete(@_);
56  
57 12 my $subfdir = $self->filename . $default_sub_extension;
58 12 rmdir $subfdir; # may fail, when there are still subfolders (no recurse)
59 }
60  
61 sub writeMessages($)
62 14 ▶{ my ($self, $args) = @_;
Subroutine writeMessages: called CC=2 SCAR=7.0
Pod documented
63 14 $self->SUPER::writeMessages($args);
64  
65 14 ▶ if($self->removeEmpty)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->removeEmpty) 14 0 14
66 { # Can the sub-folder directory be removed? Don't mind if this
67 # doesn't work: probably no subdir or still something in it. This
68 # is a rather blunt approach...
69 14 rmdir $self->filename . $self->subfolderExtension;
70 }
71  
72 14 $self;
73 }
74  
75 sub type() {'mbox'}
76  
77 #--------------------
78  
79 sub listSubFolders(@)
80 20 ▶{ my ($thingy, %args) = @_;
Subroutine listSubFolders: called CC=22 SCAR=31.3
Pod documented
81 20 ▶ my $class = ref $thingy || $thingy;
Condition66%2/3
ref $thingy || $thingy
l!l&&r!l&&!r
1370
MC/DC0%0/2
ref $thingy || $thingy
ref $thingy $thingy
Truth table66%2/3
A || B
A= ref $thingyB= $thingy
ABresult
1X1
011
000
82  
83 20 ▶ my $skip_empty = $args{skip_empty} || 0;
Condition100%2/2
$args{'skip_empty'} || 0
l!l
119
MC/DC100%1/1
$args{'skip_empty'} || 0
$args{'skip_empty'}
Truth table100%2/2
A
A= $args{'skip_empty'}
Aresult
11
00
84 20 ▶ my $check = $args{check} || 0;
Condition100%2/2
$args{'check'} || 0
l!l
119
MC/DC100%1/1
$args{'check'} || 0
$args{'check'}
Truth table100%2/2
A
A= $args{'check'}
Aresult
11
00
85 20 ▶ my $folder = $args{folder} // '=';
Condition100%2/2
$args{"folder"} // "="
l!l
218
MC/DC100%1/1
$args{"folder"} // "="
$args{"folder"}
Truth table100%2/2
A
A= $args{"folder"}
Aresult
11
00
86 20 ▶ my $folderdir = $args{folderdir} // $default_folder_dir;
Condition66%2/3
$args{"folderdir"} // $Mail::Box::Mbox::default_folder_dir
l!l&&r!l&&!r
6140
MC/DC0%0/2
$args{"folderdir"} // $Mail::Box::Mbox::default_folder_dir
$args{"folderdir"} $Mail::Box::Mbox::default_folder_dir
Truth table66%2/3
A // B
A= $args{"folderdir"}B= $Mail::Box::Mbox::default_folder_dir
ABresult
1X1
011
000
87 20 my $extension = $args{subfolder_extension};
88  
89 20 my $dir;
90 20 ▶ if(ref $thingy) # Mail::Box::Mbox
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thingy) { } 13 7 20
91 13 ▶ { $extension ||= $thingy->subfolderExtension;
Condition33%1/3
$extension ||= $thingy->subfolderExtension
l!l&&r!l&&!r
0130
MC/DC0%0/2
$extension ||= $thingy->subfolderExtension
$extension $thingy->subfolderExtension
Truth table33%1/3
A ||= B
A= $extensionB= $thingy->subfolderExtension
ABresult
1X1
011
000
92 13 $dir = $thingy->filename;
93 }
94 else
95 7 ▶ { $extension ||= $default_sub_extension;
Condition33%1/3
$extension ||= $Mail::Box::Mbox::default_sub_extension
l!l&&r!l&&!r
070
MC/DC0%0/2
$extension ||= $Mail::Box::Mbox::default_sub_extension
$extension $Mail::Box::Mbox::default_sub_extension
Truth table33%1/3
A ||= B
A= $extensionB= $Mail::Box::Mbox::default_sub_extension
ABresult
1X1
011
000
96 7 $dir = $class->folderToFilename($folder, $folderdir, $extension);
97 }
98  
99 20 ▶ my $real = -d $dir ? $dir : "$dir$extension";
Branch: 1 branch
BranchTrueFalseTotal
-d $dir ? : 6 14 20
100 20 ▶ opendir my $dh, $real or return ();
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $real or return () 11 9 20
101  
102 # Some files have to be removed because they are created by all
103 # kinds of programs, but are no folders.
104  
105 11 my @entries = grep !m/\.lo?ck$|^\./, readdir $dh;
106 11 closedir $dh;
107  
108 # Look for files in the folderdir. They should be readable to
109 # avoid warnings for usage later. Furthermore, if we check on
110 # the size too, we avoid a syscall especially to get the size
111 # of the file by performing that check immediately.
112  
113 11 my %folders; # hash to immediately un-double names.
114  
115 11 foreach my $b (@entries)
116 48 { my $entry = catfile $real, $b;
117 48 ▶ if( -f $entry )
Branch: 2 branches
BranchTrueFalseTotal
if (-f $entry) { } 34 14 48
elsif (-d _) { } 14 0 14
118 34 ▶ { next if $args{skip_empty} && ! -s _;
Branch: 1 branch
BranchTrueFalseTotal
if $args{"skip_empty"} and not -s _ 1 33 34
Condition100%3/3
$args{"skip_empty"} and not -s _
!ll&&!rl&&r
3031
MC/DC100%2/2
$args{"skip_empty"} and not -s _
$args{"skip_empty"} not -s _
Truth table100%3/3
A and not(B)
A= $args{"skip_empty"}B= not -s _
ABresult
0X0
100
111
119 33 ▶ next if $args{check} && !$class->foundIn($entry);
Branch: 1 branch
BranchTrueFalseTotal
if $args{"check"} and not $class->foundIn($entry) 1 32 33
Condition100%3/3
$args{"check"} and not $class->foundIn($entry)
!ll&&!rl&&r
2931
MC/DC100%2/2
$args{"check"} and not $class->foundIn($entry)
$args{"check"} not $class->foundIn($entry)
Truth table100%3/3
A and not(B)
A= $args{"check"}B= not $class->foundIn($entry)
ABresult
0X0
100
111
120 32 $folders{$b}++;
121 }
122 elsif( -d _ )
123 { # Directories may create fake folders.
124 14 ▶ if($args{skip_empty})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"skip_empty"}) 3 11 14
125 3 ▶ { opendir my $dh, $entry or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $entry or next 3 0 3
126 3 my @sub = grep !/^\./, readdir $dh;
127 3 closedir $dh;
128 3 ▶ @sub or next;
Branch: 1 branch
BranchTrueFalseTotal
@sub or next 2 1 3
129 }
130  
131 13 my $folder = $b =~ s/$extension$//r;
132 13 $folders{$folder}++;
133 }
134 }
135  
136 11 ▶ map +(m/(.*)/ && $1), keys %folders; # untained names
Condition33%1/3
/(.*)/ && $1
!ll&&!rl&&r
0039
MC/DC0%0/2
/(.*)/ && $1
/(.*)/ $1
Truth table33%1/3
A && B
A= /(.*)/B= $1
ABresult
0X0
100
111
137 }
138  
139 sub openRelatedFolder(@)
140 11 ▶{ my $self = shift;
Subroutine openRelatedFolder: called CC=1 SCAR=0.0
Pod documented
141 11 $self->SUPER::openRelatedFolder(subfolder_extension => $self->subfolderExtension, @_);
142 }
143  
144 #--------------------
145  
146 sub folderToFilename($$;$)
147 88 ▶{ my ($thingy, $name, $folderdir, $extension) = @_;
Subroutine folderToFilename: called CC=8 SCAR=20.8
Pod documented
148 88 ▶ $extension ||= ref $thingy ? $thingy->subfolderExtension : $default_sub_extension;
Branch: 1 branch
BranchTrueFalseTotal
ref $thingy ? : 49 0 49
Condition66%2/3
$extension ||= ref $thingy ? $thingy->subfolderExtension : $Mail::Box::Mbox::default_sub_extension
l!l&&r!l&&!r
39490
MC/DC0%0/2
$extension ||= ref $thingy ? $thingy->subfolderExtension : $Mail::Box::Mbox::default_sub_extension
$extension ref $thingy ? $thingy->subfolderExtension : $Mail::Box::Mbox::default_sub_extension
Truth table66%2/3
A ||= B
A= $extensionB= ref $thingy ? $thingy->subfolderExtension : $Mail::Box::Mbox::default_sub_extension
ABresult
1X1
011
000
149  
150 88 $name =~ s#^=#$folderdir/#;
151 88 my @parts = split m!/!, $name;
152  
153 88 my $real = shift @parts;
154 88 ▶ $real = '/' if $real eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $real eq "" 54 34 88
155  
156 88 ▶ if(@parts)
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) 79 9 88
157 79 { my $file = pop @parts;
158 79 ▶ $real = catdir $real.(-d $real ? '' : $extension), $_ for @parts;
Branch: 1 branch
BranchTrueFalseTotal
-d $real ? : 147 3 150
159 79 ▶ $real = catfile $real.(-d $real ? '' : $extension), $file;
Branch: 1 branch
BranchTrueFalseTotal
-d $real ? : 62 17 79
160 }
161  
162 88 $real;
163 }
164  
165  
166 sub foundIn($@)
167 21 ▶{ my $class = shift;
Subroutine foundIn: called CC=17 SCAR=29.9
Pod documented
168 21 ▶ my $name = @_ % 2 ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 20 1 21
169 21 my %args = @_;
170 21 ▶ $name ||= $args{folder} or return;
Branch: 1 branch
BranchTrueFalseTotal
$name ||= $args{"folder"} or return 21 0 21
Condition66%2/3
$name ||= $args{"folder"}
l!l&&r!l&&!r
2010
MC/DC0%0/2
$name ||= $args{"folder"}
$name $args{"folder"}
Truth table66%2/3
A ||= B
A= $nameB= $args{"folder"}
ABresult
1X1
011
000
171  
172 21 ▶ my $folderdir = $args{folderdir} || $default_folder_dir;
Condition66%2/3
$args{'folderdir'} || $Mail::Box::Mbox::default_folder_dir
l!l&&r!l&&!r
1470
MC/DC0%0/2
$args{'folderdir'} || $Mail::Box::Mbox::default_folder_dir
$args{'folderdir'} $Mail::Box::Mbox::default_folder_dir
Truth table66%2/3
A || B
A= $args{'folderdir'}B= $Mail::Box::Mbox::default_folder_dir
ABresult
1X1
011
000
173 21 ▶ my $extension = $args{subfolder_extension} || $default_sub_extension;
Condition33%1/3
$args{'subfolder_extension'} || $Mail::Box::Mbox::default_sub_extension
l!l&&r!l&&!r
0210
MC/DC0%0/2
$args{'subfolder_extension'} || $Mail::Box::Mbox::default_sub_extension
$args{'subfolder_extension'} $Mail::Box::Mbox::default_sub_extension
Truth table33%1/3
A || B
A= $args{'subfolder_extension'}B= $Mail::Box::Mbox::default_sub_extension
ABresult
1X1
011
000
174 21 my $filename = $class->folderToFilename($name, $folderdir, $extension);
175  
176 21 ▶ if(-d $filename)
Branch: 1 branch
BranchTrueFalseTotal
if (-d $filename) 7 14 21
177 { # Maildir and MH Sylpheed have a 'new' sub-directory
178 7 ▶ return 0 if -d catdir $filename, 'new';
Branch: 1 branch
BranchTrueFalseTotal
if -d catdir($filename, "new") 2 5 7
179 5 ▶ if(opendir my $dir, $filename)
Branch: 1 branch
BranchTrueFalseTotal
if (opendir my $dir, $filename) 5 0 5
180 5 { my @f = grep !/^\./, readdir $dir; # skip . .. and hidden
181 5 ▶ return 0 if @f && ! grep /\D/, @f; # MH
Branch: 1 branch
BranchTrueFalseTotal
if @f and not grep(/\D/, @f) 4 1 5
Condition66%2/3
@f and not grep(/\D/, @f)
!ll&&!rl&&r
014
MC/DC50%1/2
@f and not grep(/\D/, @f)
@f not grep(/\D/, @f)
Truth table66%2/3
A and not(B)
A= @fB= not grep(/\D/, @f)
ABresult
0X0
100
111
182 1 closedir $dir;
183 }
184  
185 1 ▶ return 0 # Other MH
Branch: 1 branch
BranchTrueFalseTotal
if -f "$filename/.mh_sequences" 1 0 1
186 if -f "$filename/.mh_sequences";
187  
188 0 return 1; # faked empty Mbox sub-folder (with subsub-folders?)
189 }
190  
191 14 ▶ return 0 unless -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $filename 1 13 14
192 13 ▶ return 1 if -z $filename; # empty folder is ok
Branch: 1 branch
BranchTrueFalseTotal
if -z $filename 1 12 13
193  
194 12 ▶ open my $file, '<:raw', $filename or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open my $file, "<:raw", $filename or return 0 12 0 12
195 12 local $_;
196 12 ▶ while(<$file>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $file)) 12 0 12
197 12 ▶ { next if /^\s*$/; # skip empty lines
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 0 12 12
198 12 $file->close;
199 12 return substr($_, 0, 5) eq 'From '; # found Mbox separator?
200 }
201  
202 0 return 1;
203 }
204  
205 #--------------------
206  
207 25 1;
« Box/Test.pm ↑ Summary Box/Search/Grep.pm »