Mojo/Asset/File.pm

filter: Statementstmt 100% 83 / 83 Branchbran 94.7% 36 / 38 Conditioncond 72.7% 16 / 22 MC/DCmcdc 46.1% 6 / 13 Subroutinesub 100% 18 / 18 Podpod 100% 10 / 10 totaltotal 91.8% 169 / 184 CC 28 SCAR 15.7
CC
28
Cov
94%
contains
9.0
DESTROY
7.4
__ANON__
5.0
CRAP 4.8
« Mojo/DOM.pm ↑ Summary Mojo/Date.pm »
1 package Mojo::Asset::File;
2 32 ▶use Mojo::Base 'Mojo::Asset';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 32 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 32 ▶use Fcntl qw(SEEK_SET);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 32 ▶use File::Spec::Functions ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 32 ▶use Mojo::File qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 32 has [qw(cleanup path)];
10 78 ▶has handle => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
11 78 my $self = shift;
12  
13 # Open existing file
14 78 my $path = $self->path;
15 78 ▶ return Mojo::File->new($path)->open('<') if defined $path && -e $path;
Branch: 1 branch
BranchTrueFalseTotal
if defined $path and -e $path 29 49 78
Condition100%3/3
defined $path and -e $path
!ll&&!rl&&r
47229
MC/DC100%2/2
defined $path and -e $path
defined $path -e $path
Truth table100%3/3
A and B
A= defined $pathB= -e $path
ABresult
0X0
100
111
16  
17 49 ▶ $self->cleanup(1) unless defined $self->cleanup;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->cleanup 47 2 49
18  
19 # Create a specific file
20 49 ▶ return Mojo::File->new($path)->open('+>>') if defined $path;
Branch: 1 branch
BranchTrueFalseTotal
if defined $path 2 47 49
21  
22 # Create a temporary file
23 47 my $file = tempfile DIR => $self->tmpdir, TEMPLATE => 'mojo.tmp.XXXXXXXXXXXXXXXX', UNLINK => 0;
24 46 $self->path($file->to_string);
25 46 return $file->open('+>>');
26 32 };
27 45 ▶has tmpdir => sub { $ENV{MOJO_TMPDIR} || File::Spec::Functions::tmpdir };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"MOJO_TMPDIR"} or File::Spec::Functions::tmpdir() 3 42 45
28  
29 73 ▶sub DESTROY {
Subroutine DESTROY: called CC=7 SCAR=20.0
30 73 my $self = shift;
31  
32 73 ▶ return unless $self->cleanup && defined(my $path = $self->path);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cleanup and defined(my $path = $self->path) 31 42 73
Condition100%3/3
$self->cleanup and defined(my $path = $self->path)
!ll&&!rl&&r
30142
MC/DC100%2/2
$self->cleanup and defined(my $path = $self->path)
$self->cleanup defined(my $path = $self->path)
Truth table100%3/3
A and B
A= $self->cleanupB= defined(my $path = $self->path)
ABresult
0X0
100
111
33 42 ▶ if (my $handle = $self->handle) { close $handle }
Branch: 1 branch
BranchTrueFalseTotal
if (my $handle = $self->handle) 42 0 42
34  
35 # Only the process that created the file is allowed to remove it
36 42 ▶ Mojo::File->new($path)->remove if -w $path && ($self->{pid} // $$) == $$;
Branch: 1 branch
BranchTrueFalseTotal
if -w $path and ($self->{"pid"} // $$) == $$ 41 1 42
Condition50%3/6
-w $path and ($self->{"pid"} // $$) == $$
!ll&&!rl&&r
0141
$self->{"pid"} // $$
l!l&&r!l&&!r
4200
MC/DC0%0/3
-w $path and ($self->{"pid"} // $$) == $$
-w $path $self->{"pid"} $$
Truth table25%1/4
A and B // C
A= -w $pathB= $self->{"pid"}C= $$
ABCresult
0XX0
1000
11X1
1011
37 }
38  
39 62 ▶sub add_chunk {
Subroutine add_chunk: called CC=3 SCAR=11.1
Pod documented
40 62 my ($self, $chunk) = @_;
41 62 ▶ ($self->handle->syswrite($chunk) // -1) == length $chunk or croak "Can't write to asset: $!";
Branch: 1 branch
BranchTrueFalseTotal
($self->handle->syswrite($chunk) // -1) == length $chunk or croak("Can't write to asset: $!") 59 2 61
Condition50%1/2
$self->handle->syswrite($chunk) // -1
l!l
610
MC/DC0%0/1
$self->handle->syswrite($chunk) // -1
$self->handle->syswrite($chunk)
Truth table50%1/2
A
A= $self->handle->syswrite($chunk)
Aresult
11
00
42 59 return $self;
43 }
44  
45 67 ▶sub contains {
Subroutine contains: called CC=9 SCAR=22.0
Pod documented
46 67 my ($self, $str) = @_;
47  
48 67 my $handle = $self->handle;
49 67 $handle->sysseek($self->start_range, SEEK_SET);
50  
51 # Calculate window size
52 67 ▶ my $end = $self->end_range // $self->size;
Condition66%2/3
$self->end_range // $self->size
l!l&&r!l&&!r
26410
MC/DC0%0/2
$self->end_range // $self->size
$self->end_range $self->size
Truth table66%2/3
A // B
A= $self->end_rangeB= $self->size
ABresult
1X1
011
000
53 67 my $len = length $str;
54 67 ▶ my $size = $len > 131072 ? $len : 131072;
Branch: 1 branch
BranchTrueFalseTotal
$len > 131072 ? : 4 63 67
55 67 ▶ $size = $end - $self->start_range if $size > $end - $self->start_range;
Branch: 1 branch
BranchTrueFalseTotal
if $size > $end - $self->start_range 29 38 67
56  
57 # Sliding window search
58 67 my $offset = 0;
59 67 my $start = $handle->sysread(my $window, $len);
60 67 ▶ while ($offset < $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $end) 102 2 104
61  
62 # Read as much as possible
63 102 my $diff = $end - ($start + $offset);
64 102 ▶ my $read = $handle->sysread(my $buffer, $diff < $size ? $diff : $size);
Branch: 1 branch
BranchTrueFalseTotal
$diff < $size ? : 50 52 102
65 102 $window .= $buffer;
66  
67 # Search window
68 102 my $pos = index $window, $str;
69 102 ▶ return $offset + $pos if $pos >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pos >= 0 52 50 102
70 50 ▶ return -1 if $read == 0 || ($offset += $read) == $end;
Branch: 1 branch
BranchTrueFalseTotal
if $read == 0 or ($offset += $read) == $end 13 37 50
Condition66%2/3
$read == 0 or ($offset += $read) == $end
l!l&&r!l&&!r
13037
MC/DC50%1/2
$read == 0 or ($offset += $read) == $end
$read == 0 ($offset += $read) == $end
Truth table66%2/3
A or B
A= $read == 0B= ($offset += $read) == $end
ABresult
1X1
011
000
71  
72 # Resize window
73 37 substr $window, 0, $read, '';
74 }
75  
76 2 return -1;
77 }
78  
79 35 ▶sub get_chunk {
Subroutine get_chunk: called CC=5 SCAR=16.1
Pod documented
80 35 my ($self, $offset, $max) = @_;
81 35 ▶ $max //= 131072;
Condition100%2/2
$max //= 131072
l!l
827
MC/DC100%1/1
$max //= 131072
$max
Truth table100%2/2
A
A= $max
Aresult
11
00
82  
83 35 $offset += $self->start_range;
84 35 my $handle = $self->handle;
85 35 $handle->sysseek($offset, SEEK_SET);
86  
87 35 my $buffer;
88 35 ▶ if (defined(my $end = $self->end_range)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $end = $self->end_range)) { } 18 17 35
89 18 ▶ return '' if (my $chunk = $end + 1 - $offset) <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if (my $chunk = $end + 1 - $offset) <= 0 2 16 18
90 16 ▶ $handle->sysread($buffer, $chunk > $max ? $max : $chunk);
Branch: 1 branch
BranchTrueFalseTotal
$chunk > $max ? : 6 10 16
91 }
92 17 else { $handle->sysread($buffer, $max) }
93  
94 33 return $buffer;
95 }
96  
97 34 ▶sub is_file {1}
Subroutine is_file: called CC=1 SCAR=0.0
Pod documented
98  
99 4 ▶sub move_to {
Subroutine move_to: called CC=1 SCAR=0.0
Pod documented
100 4 my ($self, $to) = @_;
101  
102 # Windows requires that the handle is closed
103 4 close $self->handle;
104 4 delete $self->{handle};
105  
106 # Move file and prevent clean up
107 4 Mojo::File->new($self->path)->move_to($to);
108 4 return $self->path($to)->cleanup(0);
109 }
110  
111 23 ▶sub mtime { (stat shift->handle)[9] }
Subroutine mtime: called CC=1 SCAR=0.0
Pod documented
112  
113 74 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
114 74 my $file = shift->SUPER::new(@_);
115 74 $file->{pid} = $$;
116 74 return $file;
117 }
118  
119 68 ▶sub size { -s shift->handle }
Subroutine size: called CC=1 SCAR=0.0
Pod documented
120  
121 33 ▶sub slurp {
Subroutine slurp: called CC=3 SCAR=11.0
Pod documented
122 33 my $handle = shift->handle;
123 33 $handle->sysseek(0, SEEK_SET);
124 33 my $ret = my $content = '';
125 33 ▶ while ($ret = $handle->sysread(my $buffer, 131072, 0)) { $content .= $buffer }
Branch: 1 branch
BranchTrueFalseTotal
if ($ret = $handle->sysread(my $buffer, 131072, 0)) 31 33 64
126 33 ▶ return defined $ret ? $content : croak "Can't read from asset: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined $ret ? : 33 0 33
127 }
128  
129 2 ▶sub to_file {shift}
Subroutine to_file: called CC=1 SCAR=0.0
Pod documented
130  
131 32 1;
132  
133 =encoding utf8
134  
135 =head1 NAME
136  
137 Mojo::Asset::File - File storage for HTTP content
138  
139 =head1 SYNOPSIS
140  
141 use Mojo::Asset::File;
142  
143 # Temporary file
144 my $file = Mojo::Asset::File->new;
145 $file->add_chunk('foo bar baz');
146 say 'File contains "bar"' if $file->contains('bar') >= 0;
147 say $file->slurp;
148  
149 # Existing file
150 my $file = Mojo::Asset::File->new(path => '/home/sri/foo.txt');
151 $file->move_to('/yada.txt');
152 say $file->slurp;
153  
154 =head1 DESCRIPTION
155  
156 L<Mojo::Asset::File> is a file storage backend for HTTP content.
157  
158 =head1 EVENTS
159  
160 L<Mojo::Asset::File> inherits all events from L<Mojo::Asset>.
161  
162 =head1 ATTRIBUTES
163  
164 L<Mojo::Asset::File> inherits all attributes from L<Mojo::Asset> and implements the following new ones.
165  
166 =head2 cleanup
167  
168 my $bool = $file->cleanup;
169 $file = $file->cleanup($bool);
170  
171 Delete L</"path"> automatically once the file is not used anymore.
172  
173 =head2 handle
174  
175 my $handle = $file->handle;
176 $file = $file->handle(IO::File->new);
177  
178 Filehandle, created on demand for L</"path">, which can be generated automatically and safely based on L</"tmpdir">.
179  
180 =head2 path
181  
182 my $path = $file->path;
183 $file = $file->path('/home/sri/foo.txt');
184  
185 File path used to create L</"handle">.
186  
187 =head2 tmpdir
188  
189 my $tmpdir = $file->tmpdir;
190 $file = $file->tmpdir('/tmp');
191  
192 Temporary directory used to generate L</"path">, defaults to the value of the C<MOJO_TMPDIR> environment variable or
193 auto-detection.
194  
195 =head1 METHODS
196  
197 L<Mojo::Asset::File> inherits all methods from L<Mojo::Asset> and implements the following new ones.
198  
199 =head2 add_chunk
200  
201 $file = $file->add_chunk('foo bar baz');
202  
203 Add chunk of data.
204  
205 =head2 contains
206  
207 my $position = $file->contains('bar');
208  
209 Check if asset contains a specific string.
210  
211 =head2 get_chunk
212  
213 my $bytes = $file->get_chunk($offset);
214 my $bytes = $file->get_chunk($offset, $max);
215  
216 Get chunk of data starting from a specific position, defaults to a maximum chunk size of C<131072> bytes (128KiB).
217  
218 =head2 is_file
219  
220 my $bool = $file->is_file;
221  
222 True, this is a L<Mojo::Asset::File> object.
223  
224 =head2 move_to
225  
226 $file = $file->move_to('/home/sri/bar.txt');
227  
228 Move asset data into a specific file and disable L</"cleanup">.
229  
230 =head2 mtime
231  
232 my $mtime = $file->mtime;
233  
234 Modification time of asset.
235  
236 =head2 new
237  
238 my $file = Mojo::Asset::File->new;
239 my $file = Mojo::Asset::File->new(path => '/home/sri/test.txt');
240 my $file = Mojo::Asset::File->new({path => '/home/sri/test.txt'});
241  
242 Construct a new L<Mojo::Asset::File> object.
243  
244 =head2 size
245  
246 my $size = $file->size;
247  
248 Size of asset data in bytes.
249  
250 =head2 slurp
251  
252 my $bytes = $file->slurp;
253  
254 Read all asset data at once.
255  
256 =head2 to_file
257  
258 $file = $file->to_file;
259  
260 Does nothing but return the invocant, since we already have a L<Mojo::Asset::File> object.
261  
262 =head1 SEE ALSO
263  
264 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
265  
266 =cut
« Mojo/DOM.pm ↑ Summary Mojo/Date.pm »