blib/lib/Asset/File.pm

filter: Statementstmt 84.3% 135 / 160 Branchbran 65.1% 43 / 66 Conditioncond 60.7% 17 / 28 MC/DCmcdc 38.8% 7 / 18 Subroutinesub 92.8% 26 / 28 Podpod 76.9% 10 / 13 totaltotal 76.0% 238 / 313 CC 44 SCAR 18.9
CC
44
Cov
77%
crc32
20.0
__ANON__
14.5
contains
9.0
CRAP 6.6
↑ Summary
1 package Asset::File;
2 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Errno 'EEXIST';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Fcntl qw(O_APPEND O_CREAT O_EXCL O_RDONLY O_RDWR);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Copy 'move';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Spec::Functions 'catfile';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Digest::MD5 'md5_hex';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Digest::SHA1;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 our $VERSION = '1.03';
15  
16 1 has [qw/cleanup path end_range ro/] => (
17 is => 'rw',
18 );
19  
20 has start_range => (
21 is => 'rw',
22 15 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 1 );
24  
25 has handle => (
26 is => 'rw',
27 lazy => 1,
28 15 ▶ default => sub {
Subroutine __ANON__: called CC=11 SCAR=26.8
29 15 my $self = shift;
30  
31 # Open existing file
32 15 my $handle = IO::File->new;
33 15 my $path = $self->path;
34 15 ▶ if (defined $path && -f $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path and -f $path) 1 14 15
Condition100%3/3
defined $path and -f $path
!ll&&!rl&&r
1221
MC/DC100%2/2
defined $path and -f $path
defined $path -f $path
Truth table100%3/3
A and B
A= defined $pathB= -f $path
ABresult
0X0
100
111
35 1 ▶ $handle->open($path, -w $path ? ($self->ro ? O_RDONLY : O_RDWR) : O_RDONLY)
Branch: 3 branches
BranchTrueFalseTotal
$handle->open($path, -w $path ? $self->ro ? 0 : 2 : 0) or croak(qq[Can't open file "$path": $!]) 1 0 1
-w $path ? : 1 0 1
$self->ro ? : 0 1 1
36 or croak qq{Can't open file "$path": $!};
37 1 return $handle;
38 }
39  
40 # Open new or temporary file
41 14 my $out = File::Temp->new(UNLINK => $self->cleanup);
42 14 $out->autoflush(1);
43 14 my $base = $out->filename;
44 14 ▶ my $name = $path // $base;
Condition66%2/3
$path // $base
l!l&&r!l&&!r
2120
MC/DC0%0/2
$path // $base
$path $base
Truth table66%2/3
A // B
A= $pathB= $base
ABresult
1X1
011
000
45 14 ▶ until ($handle->open($name, O_CREAT | O_RDWR)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handle->open($name, 66)) 0 14 14
46 0 ▶ croak qq{Can't open file "$name": $!} if defined $path || $! != $!{EEXIST};
Branch: 1 branch
BranchTrueFalseTotal
if defined $path or $! != $!{"EEXIST"} 0 0 0
Condition0%0/3
defined $path or $! != $!{"EEXIST"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $path or $! != $!{"EEXIST"}
defined $path $! != $!{"EEXIST"}
Truth table0%0/3
A or B
A= defined $pathB= $! != $!{"EEXIST"}
ABresult
1X1
011
000
47 0 $name = "$base." . md5_hex(time . $$ . rand 999);
48 }
49 14 $self->path($name);
50  
51 # Enable automatic cleanup
52 14 ▶ $self->cleanup(1) unless defined $self->cleanup;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->cleanup 11 3 14
53  
54 14 return $handle;
55 }
56 1 );
57  
58  
59 15 ▶sub DESTROY {
Subroutine DESTROY: called CC=4 SCAR=14.1
60 15 my $self = shift;
61 15 ▶ return unless $self->cleanup && defined(my $path = $self->path);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cleanup and defined(my $path = $self->path) 6 9 15
Condition66%2/3
$self->cleanup and defined(my $path = $self->path)
!ll&&!rl&&r
609
MC/DC50%1/2
$self->cleanup and defined(my $path = $self->path)
$self->cleanup defined(my $path = $self->path)
Truth table66%2/3
A and B
A= $self->cleanupB= defined(my $path = $self->path)
ABresult
0X0
100
111
62 9 close $self->handle;
63 9 ▶ unlink $path if -w $path;
Branch: 1 branch
BranchTrueFalseTotal
if -w $path 9 0 9
64 }
65  
66 3 ▶sub is_range { !!($_[0]->end_range || $_[0]->start_range) };
Subroutine is_range: called CC=2 SCAR=6.9
Pod undocumented
Condition100%3/3
$_[0]->end_range || $_[0]->start_range
l!l&&r!l&&!r
111
MC/DC100%2/2
$_[0]->end_range || $_[0]->start_range
$_[0]->end_range $_[0]->start_range
Truth table100%3/3
A || B
A= $_[0]->end_rangeB= $_[0]->start_range
ABresult
1X1
011
000
67  
68 19 ▶sub add_chunk {
Subroutine add_chunk: called CC=4 SCAR=14.0
Pod documented
69 19 my ($self, $chunk) = @_;
70 19 ▶ $chunk //= '';
Condition50%1/2
$chunk //= ""
l!l
190
MC/DC0%0/1
$chunk //= ""
$chunk
Truth table50%1/2
A
A= $chunk
Aresult
11
00
71 19 my $handle = $self->handle;
72 19 ▶ if ($self->start_range) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->start_range) { } 1 18 19
73 1 $handle->sysseek($self->start_range, SEEK_SET);
74 }
75 else {
76 18 $handle->sysseek(0, SEEK_END);
77 }
78 19 ▶ croak "Can't write to asset: $!"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $handle->syswrite($chunk, length $chunk) 0 19 19
79 unless defined $handle->syswrite($chunk, length $chunk);
80 19 return $self;
81 }
82  
83 37 ▶sub contains {
Subroutine contains: called CC=9 SCAR=22.0
Pod documented
84 37 my ($self, $str) = @_;
85  
86 37 my $handle = $self->handle;
87 37 $handle->sysseek($self->start_range, SEEK_SET);
88  
89 # Calculate window size
90 37 ▶ my $end = $self->end_range // $self->size;
Condition66%2/3
$self->end_range // $self->size
l!l&&r!l&&!r
13240
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
91 37 my $len = length $str;
92 37 ▶ my $size = $len > 131072 ? $len : 131072;
Branch: 1 branch
BranchTrueFalseTotal
$len > 131072 ? : 2 35 37
93 37 ▶ $size = $end - $self->start_range if $size > $end - $self->start_range;
Branch: 1 branch
BranchTrueFalseTotal
if $size > $end - $self->start_range 18 19 37
94  
95 # Sliding window search
96 37 my $offset = 0;
97 37 my $start = $handle->sysread(my $window, $len);
98 37 ▶ while ($offset < $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $end) 55 1 56
99  
100 # Read as much as possible
101 55 my $diff = $end - ($start + $offset);
102 55 ▶ my $read = $handle->sysread(my $buffer, $diff < $size ? $diff : $size);
Branch: 1 branch
BranchTrueFalseTotal
$diff < $size ? : 29 26 55
103 55 $window .= $buffer;
104  
105 # Search window
106 55 my $pos = index $window, $str;
107 55 ▶ return $offset + $pos if $pos >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pos >= 0 29 26 55
108 26 ▶ return -1 if $read == 0 || ($offset += $read) == $end;
Branch: 1 branch
BranchTrueFalseTotal
if $read == 0 or ($offset += $read) == $end 7 19 26
Condition66%2/3
$read == 0 or ($offset += $read) == $end
l!l&&r!l&&!r
7019
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
109  
110 # Resize window
111 19 substr $window, 0, $read, '';
112 }
113  
114 1 return -1;
115 }
116  
117 8 ▶sub get_chunk {
Subroutine get_chunk: called CC=5 SCAR=16.1
Pod documented
118 8 my ($self, $offset, $max) = @_;
119 8 ▶ $max //= 131072;
Condition100%2/2
$max //= 131072
l!l
44
MC/DC100%1/1
$max //= 131072
$max
Truth table100%2/2
A
A= $max
Aresult
11
00
120  
121 8 $offset += $self->start_range;
122 8 my $handle = $self->handle;
123 8 $handle->sysseek($offset, SEEK_SET);
124  
125 8 my $buffer;
126 8 ▶ if (defined(my $end = $self->end_range)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $end = $self->end_range)) { } 7 1 8
127 7 ▶ return '' if (my $chunk = $end + 1 - $offset) <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if (my $chunk = $end + 1 - $offset) <= 0 0 7 7
128 7 ▶ $handle->sysread($buffer, $chunk > $max ? $max : $chunk);
Branch: 1 branch
BranchTrueFalseTotal
$chunk > $max ? : 3 4 7
129 }
130 1 else { $handle->sysread($buffer, $max) }
131  
132 8 return $buffer;
133 }
134  
135 0 ▶sub first_line_of {
Subroutine first_line_of: not called CC=1 SCAR=6.9
Pod undocumented
136 0 my $fh = shift->handle;
137 0 my $line = <$fh>;
138 0 chomp $line;
139 0 $line =~ s/^\s+|\s+$//g;
140  
141 0 return $line;
142 }
143  
144 1 ▶sub md5sum {
Subroutine md5sum: called CC=3 SCAR=11.2
Pod documented
145 1 my $self = shift;
146 1 my $content = shift;
147 1 my $md5 = Digest::MD5->new;
148 1 ▶ if ($content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content) 0 1 1
149 0 $md5->add($content);
150 0 return $md5->hexdigest,
151 }
152 1 my $handle = $self->handle;
153 1 $handle->sysseek(0, SEEK_SET);
154 1 ▶ while ($handle->sysread(my $buffer, 131072, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle->sysread(my $buffer, 131072, 0)) 1 1 2
155 1 $md5->add($buffer);
156 }
157 1 return $md5->hexdigest,
158 }
159  
160 1 ▶sub sha1sum {
Subroutine sha1sum: called CC=3 SCAR=11.2
Pod documented
161 1 my $self = shift;
162 1 my $content = shift;
163 1 my $sha1 = Digest::SHA1->new;
164 1 ▶ if ($content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content) 0 1 1
165 0 $sha1->add($content);
166 0 return $sha1->hexdigest,
167 }
168 1 my $handle = $self->handle;
169 1 $handle->sysseek(0, SEEK_SET);
170 1 ▶ while ($handle->sysread(my $buffer, 131072, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle->sysread(my $buffer, 131072, 0)) 1 1 2
171 1 $sha1->add($buffer);
172 }
173 1 return $sha1->hexdigest,
174 }
175  
176 0 ▶sub crc32 {
Subroutine crc32: not called CC=4 SCAR=30.0
Pod undocumented
177 0 my $self = shift;
178 0 my $content = shift;
179 0 ▶ eval q{ require Digest::CRC } or die 'Could not require Digest::CRC';
Branch: 1 branch
BranchTrueFalseTotal
eval " require Digest::CRC " or die "Could not require Digest::CRC" 0 0 0
180 0 my $crc = Digest::CRC->new( type => "crc32" );
181 0 my $handle = $self->handle;
182 0 $handle->sysseek(0, SEEK_SET);
183 0 ▶ if ($content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content) 0 0 0
184 0 $crc->add($content);
185 0 return $crc->hexdigest,
186 }
187 0 ▶ while ($handle->sysread(my $buffer, 131072, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle->sysread(my $buffer, 131072, 0)) 0 0 0
188 0 $crc->add($buffer);
189 }
190 0 return $crc->hexdigest,
191 }
192  
193 1 ▶sub is_file {1}
Subroutine is_file: called CC=1 SCAR=0.0
Pod documented
194  
195 2 ▶sub move_to {
Subroutine move_to: called CC=5 SCAR=19.4
Pod documented
196 2 my ($self, $to) = @_;
197  
198 # Windows requires that the handle is closed
199 2 close $self->handle;
200 2 delete $self->{handle};
201  
202 2 my $dir = File::Basename::dirname( $to );
203 2 ▶ if (! -e $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $dir) 0 2 2
204 0 ▶ if (! File::Path::make_path( $dir ) || ! -d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (File::Path::make_path($dir) and -d $dir) 0 0 0
Condition0%0/3
File::Path::make_path($dir) and -d $dir
!ll&&!rl&&r
000
MC/DC0%0/2
File::Path::make_path($dir) and -d $dir
File::Path::make_path($dir) -d $dir
Truth table0%0/3
A and B
A= File::Path::make_path($dir)B= -d $dir
ABresult
0X0
100
111
205 0 my $e = $!;
206 }
207 }
208  
209 # Move file and prevent clean up
210 2 my $from = $self->path;
211 2 ▶ move($from, $to) or croak qq{Can't move file "$from" to "$to": $!};
Branch: 1 branch
BranchTrueFalseTotal
move($from, $to) or croak(qq[Can't move file "$from" to "$to": $!]) 2 0 2
212 2 $self->cleanup(0);
213 2 $self->path($to);
214 2 return $self;
215 }
216  
217 3 ▶sub mtime { (stat shift->handle)[9] }
Subroutine mtime: called CC=1 SCAR=0.0
Pod documented
218  
219 27 ▶sub size { -s shift->handle }
Subroutine size: called CC=1 SCAR=0.0
Pod documented
220  
221 5 ▶sub slurp {
Subroutine slurp: called CC=4 SCAR=14.0
Pod documented
222 5 ▶ return '' unless defined (my $path = shift->path);
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $path = (shift())->path) 0 5 5
223 5 ▶ croak qq{Can't open file "$path": $!} unless open my $file, '<', $path;
Branch: 1 branch
BranchTrueFalseTotal
unless open my $file, "<", $path 0 5 5
224 5 my $content = '';
225 5 ▶ while ($file->sysread(my $buffer, 131072, 0)) { $content .= $buffer }
Branch: 1 branch
BranchTrueFalseTotal
if ($file->sysread(my $buffer, 131072, 0)) 3 5 8
226 5 return $content;
227 }
228  
229 1 1;
230  
231 =encoding utf8
232  
233 =head1 NAME
234  
235 Asset::File - File Operation interface
236  
237 =head1 SYNOPSIS
238  
239 use Asset::File;
240
241 # Temporary file
242 my $file = Asset::File->new;
243 $file->add_chunk('foo bar baz');
244 say 'File contains "bar"' if $file->contains('bar') >= 0;
245 say $file->slurp;
246
247 # Existing file
248 my $file = Asset::File->new(path => '/home/sri/foo.txt');
249 $file->move_to('/yada.txt');
250 say $file->slurp;
251  
252 =head1 DESCRIPTION
253  
254 L<Asset::File> is a file content interface.
255  
256 =head1 ATTRIBUTES
257  
258 =head2 cleanup
259  
260 my $bool = $file->cleanup;
261 $file = $file->cleanup($bool);
262  
263 Delete L</"path"> automatically once the file is not used anymore.
264  
265 =head2 handle
266  
267 my $handle = $file->handle;
268 $file = $file->handle(IO::File->new);
269  
270 Filehandle, created on demand.
271  
272 =head2 path
273  
274 my $path = $file->path;
275 $file = $file->path('/home/sri/foo.txt');
276  
277 File path used to create L</"handle">, can also be automatically generated if
278 necessary.
279  
280 =head1 METHODS
281  
282 =head2 add_chunk
283  
284 $file = $file->add_chunk('foo bar baz');
285  
286 Add chunk of data, if there is range from range position to start writing.
287  
288 =head2 contains
289  
290 my $position = $file->contains('bar');
291  
292 Check if asset contains a specific string, if there is range from range position to start checking.
293  
294 =head2 get_chunk
295  
296 my $bytes = $file->get_chunk($offset);
297 my $bytes = $file->get_chunk($offset, $max);
298  
299 Get chunk of data starting from a specific position, defaults to a maximum
300 chunk size of C<131072> bytes (128KB).
301  
302 =head2 is_file
303  
304 my $true = $file->is_file;
305  
306 True.
307  
308 =head2 move_to
309  
310 $file = $file->move_to('/home/sri/bar.txt');
311  
312 Move asset data into a specific file and disable L</"cleanup">.
313  
314 =head2 mtime
315  
316 my $mtime = $file->mtime;
317  
318 Modification time of asset.
319  
320 =head2 size
321  
322 my $size = $file->size;
323  
324 Size of asset data in bytes.
325  
326 =head2 md5sum
327  
328 my $md5 = $file->md5sum;
329  
330 return the md5 digest in hexadecimal form file;
331  
332 =head2 sha1sum
333  
334 my $sha1 = $file->sha1sum;
335  
336 return the sha1 digest in hexadecimal form file;
337  
338 =head2 slurp
339  
340 my $bytes = $file->slurp;
341  
342 Read all asset data at once.
343  
344 =head1 SEE ALSO
345  
346 L<Mojo::Asset::File>
347  
348 =cut
↑ Summary