File Coverage

blib/lib/Mail/Message/Body/File.pm
Criterion Covered Total %
statement 70 109 64.2
branch 11 34 32.3
condition 3 6 50.0
subroutine 18 23 78.2
pod 10 10 100.0
total 112 182 61.5


line stmt bran cond sub pod time code
1             # This code is part of Perl distribution Mail-Message version 4.04.
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::Message::Body::File;{
13             our $VERSION = '4.04';
14             }
15              
16 37     37   1033 use parent 'Mail::Message::Body';
  37         79  
  37         317  
17              
18 37     37   3251 use strict;
  37         109  
  37         1237  
19 37     37   283 use warnings;
  37         149  
  37         2506  
20              
21 37     37   217 use Log::Report 'mail-message', import => [ qw/__x fault/ ];
  37         154  
  37         364  
22              
23 37     37   47200 use File::Temp qw/tempfile/;
  37         619362  
  37         3969  
24 37     37   9718 use File::Copy qw/copy/;
  37         108452  
  37         2830  
25 37     37   268 use Fcntl qw/SEEK_END/;
  37         85  
  37         2045  
26              
27 37     37   5067 use Mail::Box::Parser ();
  37         148  
  37         997  
28 37     37   10148 use Mail::Message ();
  37         131  
  37         61160  
29              
30             #--------------------
31              
32             sub _data_from_filename(@)
33 0     0   0 { my ($self, $filename) = @_;
34              
35 0 0       0 open my $in, '<:raw', $filename
36             or fault __x"unable to read file {name} for message body file", name => $filename;
37              
38 0         0 my $file = $self->tempFilename;
39 0 0       0 open my $out, '>:raw', $file
40             or fault __x"cannot write to temporary body file {name}", name => $file;
41              
42 0         0 my $nrlines = 0;
43 0         0 local $_;
44 0         0 while(<$in>) { $out->print($_); $nrlines++ }
  0         0  
  0         0  
45 0         0 $self->{MMBF_nrlines} = $nrlines;
46 0         0 $self;
47             }
48              
49             sub _data_from_filehandle(@)
50 1     1   3 { my ($self, $fh) = @_;
51 1         7 my $file = $self->tempFilename;
52 1         2192 my $nrlines = 0;
53              
54 1 50       88 open my $out, '>:raw', $file
55             or fault __x"cannot write to temporary body file {name}", name => $file;
56              
57 1         3 local $_;
58 1         8 while(<$fh>)
59 5         223 { $out->print($_);
60 5         35 $nrlines++;
61             }
62              
63 1         27 $self->{MMBF_nrlines} = $nrlines;
64 1         170 $self;
65             }
66              
67             sub _data_from_lines(@)
68 1     1   3 { my ($self, $lines) = @_;
69 1         5 my $file = $self->tempFilename;
70              
71 1 50       765 open my $out, '>:raw', $file
72             or fault __x"cannot write body to file {name}", name => $file;
73              
74 1         12 $out->print(@$lines);
75              
76 1         31 $self->{MMBF_nrlines} = @$lines;
77 1         179 $self;
78             }
79              
80             sub clone()
81 0     0 1 0 { my $self = shift;
82 0         0 my $clone = ref($self)->new(based_on => $self);
83              
84 0 0       0 copy $self->tempFilename, $clone->tempFilename
85             or return;
86              
87 0         0 $clone->{MMBF_nrlines} = $self->{MMBF_nrlines};
88 0         0 $clone->{MMBF_size} = $self->{MMBF_size};
89 0         0 $self;
90             }
91              
92              
93             sub nrLines()
94 2     2 1 7 { my $self = shift;
95 2 50       21 return $self->{MMBF_nrlines} if defined $self->{MMBF_nrlines};
96              
97 0         0 my $file = $self->tempFilename;
98 0         0 my $nrlines = 0;
99              
100 0 0       0 open my $in, '<:raw', $file
101             or fault __x"cannot read from file {name}", name => $file;
102              
103 0         0 local $_;
104 0         0 $nrlines++ while <$in>;
105              
106 0         0 $self->{MMBF_nrlines} = $nrlines;
107             }
108              
109             sub size()
110 2     2 1 7 { my $self = shift;
111              
112             return $self->{MMBF_size}
113 2 50       9 if exists $self->{MMBF_size};
114              
115 2         5 my $size = eval { -s $self->tempFilename };
  2         7  
116              
117 2 50       9 $size -= $self->nrLines
118             if $Mail::Message::crlf_platform; # remove count for extra CR's
119              
120 2         16 $self->{MMBF_size} = $size;
121             }
122              
123              
124             sub string()
125 3     3 1 697 { my $self = shift;
126 3         13 my $file = $self->tempFilename;
127              
128 3 50       186 open my $in, '<:raw', $file
129             or fault __x"cannot read from file {name}", name => $file;
130              
131 3         211 join '', $in->getlines;
132             }
133              
134              
135             sub lines()
136 3     3 1 955 { my $self = shift;
137 3         11 my $file = $self->tempFilename;
138              
139 3 50       189 open my $in, '<:raw', $file
140             or fault __x"cannot read from file {name}", name => $file;
141              
142 3         116 my $r = $self->{MMBF_nrlines} = [ $in->getlines ];
143 3 100       66 wantarray ? @$r: $r;
144             }
145              
146             sub file()
147 0     0 1 0 { my $self = shift;
148 0         0 open my($tmp), '<:raw', $self->tempFilename;
149 0         0 $tmp;
150             }
151              
152              
153             sub print(;$)
154 2     2 1 50 { my $self = shift;
155 2   33     24 my $fh = shift || select;
156              
157 2         18 my $file = $self->tempFilename;
158 2 50       94 open my $in, '<:raw', $file
159             or fault __x"cannot read from file {name}", name => $file;
160              
161 2         66 $fh->print($_) while <$in>;
162 2         158 $in->close;
163              
164 2         51 $self;
165             }
166              
167              
168             sub endsOnNewline()
169 0     0 1 0 { my $self = shift;
170              
171 0         0 my $file = $self->tempFilename;
172 0 0       0 open my $in, '<:raw', $file
173             or fault __x"cannot read from file {name}", name => $file;
174              
175 0         0 $in->seek(-1, SEEK_END);
176 0         0 $in->read(my $char, 1);
177 0 0       0 $char eq "\n" || $char eq "\r";
178             }
179              
180              
181             sub read($$;$@)
182 0     0 1 0 { my ($self, $parser, $head, $bodytype) = splice @_, 0, 4;
183 0         0 my $file = $self->tempFilename;
184              
185 0 0       0 open my $out, '>:raw', $file
186             or fault __x"cannot write to file {name}", name => $file;
187              
188 0         0 (my $begin, my $end, $self->{MMBF_nrlines}) = $parser->bodyAsFile($out, @_);
189 0         0 $out->close;
190              
191 0         0 $self->fileLocation($begin, $end);
192 0         0 $self;
193             }
194              
195             #--------------------
196              
197             sub tempFilename(;$)
198 15     15 1 5842 { my $self = shift;
199 15 50 66     503 @_ ? ($self->{MMBF_filename} = shift) : ($self->{MMBF_filename} //= (tempfile)[1]);
200             }
201              
202             #--------------------
203              
204 2     2   461 sub DESTROY { unlink $_[0]->tempFilename }
205              
206             1;