BlockSync.pm

filter: Statementstmt 37.3% 53 / 142 Branchbran 2.1% 1 / 46 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 54.1% 13 / 24 Podpod 100% 2 / 2 totaltotal 27.0% 69 / 255 CC 33 SCAR 59.1
CC
33
Cov
25%
__ANON__
756.0
__ANON__
20.0
_getFh
20.0
CRAP 367.9
↑ Summary BlockSync/App.pm »
1 package IO::BlockSync;
2  
3 # Basic
4 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # Build in
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Fcntl qw(:DEFAULT :seek);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use POSIX qw(ceil);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # CPAN
15 1 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Log::Log4perl::Level;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # These two come last - in that order
21 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 ################################################################
25  
26 # Moo roles to implement
27 1 with('MooseX::Log::Log4perl');
28  
29 # Make sure log4perl doesn't come with errors
30 1 ▶if ( not Log::Log4perl->initialized() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Log::Log4perl"->initialized) 1 0 1
31 1 Log::Log4perl->easy_init( Log::Log4perl::Level::to_priority('OFF') );
32 }
33  
34 ################################################################
35  
36 =head1 NAME
37  
38 IO::BlockSync - Syncronize changed blocks
39  
40 =head1 VERSION
41  
42 Version 0.001
43  
44 =cut
45  
46 1 our $VERSION = '0.001';
47  
48 ################################################################
49  
50 =head1 SYNOPSIS
51  
52 BlockSync can some of the same stuff that bigsync (by Egor Egorov) can
53 - it's just written in perl.
54  
55 BlockSync copies data from source file to destination file (can be a block
56 device) and calculates checksum on each block it copies.
57 On all runs after the first only the changed blocks will be copied.
58  
59 use IO::BlockSync;
60  
61 # OOP way
62 my $bs = IO::BlockSync->new(
63 src => '/path/to/source/file',
64 dst => '/path/to/destination/file',
65 chk => '/path/to/chk/file',
66 );
67 $bs->run;
68  
69 # Non OOP way
70 BlockSync(
71 src => '/path/to/source/file',
72 dst => '/path/to/destination/file',
73 chk => '/path/to/chk/file',
74 );
75  
76 =cut
77  
78 ################################################################
79  
80 =head1 EXPORT
81  
82 =cut
83  
84 1 our @EXPORT = qw(BlockSync);
85  
86 ################################################################
87  
88 =head2 BlockSync
89  
90 Run BlockSync non-object-oriented
91  
92 =cut
93  
94 sub BlockSync {
95 0 ▶ return __PACKAGE__->new(@_)->run;
Subroutine BlockSync: not called CC=1 SCAR=6.9
Pod documented
96 }
97  
98 ################################################################
99  
100 =head1 ATTRIBUTES
101  
102 =cut
103  
104 ################################################################
105  
106 =head2 src
107  
108 Path to source file.
109  
110 mandatory - string (containing path) or filehandle
111  
112 =cut
113  
114 1 has 'src' => (
115 is => 'ro',
116 required => 1,
117 );
118  
119 ################################################################
120  
121 =head2 dst
122  
123 Destination file. If not set, then only checksum file will be updated.
124  
125 optional - string (containing path) or filehandle
126  
127 =cut
128  
129 1 has 'dst' => ( is => 'ro', );
130  
131 ################################################################
132  
133 =head2 chk
134  
135 Path to checksum file.
136  
137 mandatory - string (containing path) or filehandle
138  
139 =cut
140  
141 1 has 'chk' => (
142 is => 'ro',
143 required => 1,
144 );
145  
146 ################################################################
147  
148 =head2 bs
149  
150 Block size to use in bytes.
151  
152 optional - integer - defaults to 1_048_576 B (1 MB)
153  
154 =cut
155  
156 1 has 'bs' => (
157 is => 'ro',
158 default => 1_048_576,
159 );
160  
161 ################################################################
162  
163 =head2 hash
164  
165 Sub that retrurn hashed data.
166  
167 optional - sub - defaults to sub that return MD5 hash followed by newline
168  
169 =cut
170  
171 has 'hash' => (
172 is => 'ro',
173 default => sub {
174 0 ▶ require Digest::MD5;
Subroutine __ANON__: not called CC=1 SCAR=6.9
175 sub {
176 0 ▶ Digest::MD5::md5_hex(shift) . "\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
177 }
178 0 },
179 1 );
180  
181 ################################################################
182  
183 =head2 sparse
184  
185 Seek in dst file, instead of writing blocks only containing \0
186  
187 optional - boolean - defaults to 0 (false)
188  
189 =cut
190  
191 1 has 'sparse' => (
192 is => 'ro',
193 default => 0,
194 );
195  
196 ################################################################
197  
198 =head2 truncate
199  
200 Truncate the destination file to same size as source file. Does not work on block devices. Will only be tried if C<data> has default value (whole file is copied).
201  
202 optional - boolean - defaults to 0 (false)
203  
204 =cut
205  
206 1 has 'truncate' => (
207 is => 'ro',
208 default => 0,
209 );
210  
211 ################################################################
212  
213 =head2 data
214  
215 List of areas (in bytes) inside the source file that should be looked at.
216 Usefull if you know excactly which blocks in src that could have changed.
217  
218 data => [
219 {start => 0, end => 9999},
220 {start => 88888, end => 777777},
221 ]
222  
223 optional - array of hashes - defaults to "whole file"
224  
225 =cut
226  
227 has 'data' => (
228 is => 'ro',
229 default => sub {
230 [
231 {
232 0 ▶ start => 0,
Subroutine __ANON__: not called CC=1 SCAR=6.9
233 end => 0
234 }
235 ]
236 },
237 1 );
238  
239 ################################################################
240  
241 =head2 status
242  
243 Sub that will be run everytime a block has been read (and written).
244  
245 optional - sub - default to sub doing nothing
246  
247 =cut
248  
249 has 'status' => (
250 is => 'ro',
251 default => sub {
252 ▶ sub { }
Subroutine __ANON__: not called CC=1 SCAR=0.0
253 0 ▶ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
254 1 );
255  
256 ################################################################
257  
258 =head1 METHODS
259  
260 =cut
261  
262 ################################################################
263  
264 =head2 run
265  
266 This is the method that starts copying data.
267  
268 =cut
269  
270 sub run { ## no critic (Subroutines::ProhibitExcessComplexity)
271 0 ▶ my $self = shift;
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
272  
273 0 my ( $srcFh, $srcClose, $dstFh, $dstClose, $chkFh, $chkClose );
274  
275 try {
276 # Get file handles for source, destination and checksum files
277 0 ▶ $srcFh = $self->_getFh( 'src', $self->src, \$srcClose, O_RDONLY );
Subroutine __ANON__: not called CC=27 SCAR=66.3
278 0 $chkFh =
279 $self->_getFh( 'chk', $self->chk, \$chkClose, O_RDWR | O_CREAT );
280 0 ▶ if ( $self->dst ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dst) { } 0 0 0
281 0 $dstFh = $self->_getFh( 'dst', $self->dst, \$dstClose,
282 O_WRONLY | O_CREAT );
283 }
284 else {
285 0 $self->logger->debug('No dst file, only calculating checksums');
286 }
287  
288 # Calculate hash for a block only containing ASCII 0
289 0 my $nullHash = &{ $self->hash }( "\0" x $self->bs );
290  
291 # Get number of bytes that a hash takes up
292 0 my $hashSize = length($nullHash);
293  
294 0 my $srcSeek;
295  
296 # Loop through "areas" that should be copied
297 # Default i one area containing the whole source file
298 0 foreach my $dataBlocks ( @{ $self->data } ) {
299  
300 # Start and end of this "area" (default is $start=0, $end=0)
301 0 my $start = $dataBlocks->{start};
302 0 my $end = $dataBlocks->{end};
303  
304 0 $self->logger->debug(
305 "Going to process data from <$start> to <$end>");
306  
307 # Seek to $start
308 # (or the beginning of the block that $start is in,
309 # if $start is not aligned with bs)
310 0 $srcSeek = int( $start / $self->bs ) * $self->bs;
311 0 ▶ sysseek( $srcFh, $srcSeek, SEEK_SET )
Branch: 1 branch
BranchTrueFalseTotal
sysseek $srcFh, $srcSeek, 0 or $self->logger->logcroak("Cannot seek to block <$srcSeek> in src file") 0 0 0
312 || $self->logger->logcroak(
313 "Cannot seek to block <$srcSeek> in src file");
314  
315 # Just die! Muhahaha. Or not
316 0 my $die = 0;
317  
318 # Can be either sparse, new, unchanged or changed
319 0 my $status;
320  
321 # Read block from source
322 0 ▶ while ( my $srcReadSize = sysread( $srcFh, my $data, $self->bs ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $srcReadSize = sysread($srcFh, my $data, $self->bs)) 0 0 0
323  
324 # It's ok to read a block smaller than bs if it's the last
325 # block. But it's not ok if it's not the last.
326 0 ▶ if ($die) {
Branch: 1 branch
BranchTrueFalseTotal
if ($die) 0 0 0
327 0 croak 'not reading full block';
328 }
329 0 $die = $srcReadSize != $self->bs;
330  
331 # $block = block number in source with the specified block size
332 0 my $block = $srcSeek / $self->bs;
333  
334 # We start by assuming that we should write to dst
335 # - if dst is set (= we are not just calculating checksum)
336 0 my $writeData = 1 && $dstFh;
337  
338 # We start be assuming that we should write checksum to chk
339 0 my $writeHash = 1;
340  
341 # Calculate hash for data read from src
342 0 my $newHash = &{ $self->hash }($data);
343  
344 # Get old hash for the same block
345 0 ▶ sysseek( $chkFh, $block * $hashSize, SEEK_SET )
Branch: 1 branch
BranchTrueFalseTotal
sysseek $chkFh, $block * $hashSize, 0 or $self->logger->logcroak("Cannot seek in chk file") 0 0 0
346 || $self->logger->logcroak('Cannot seek in chk file');
347 0 my $oldHashSize = sysread( $chkFh, my $oldHash, $hashSize );
348  
349 # Test source against checksum
350 0 ▶ if ( $oldHashSize != $hashSize || $oldHash eq "\0" x $hashSize )
Branch: 2 branches
BranchTrueFalseTotal
if ($oldHashSize != $hashSize or $oldHash eq "\000" x $hashSize) { } 0 0 0
elsif ($newHash eq $oldHash) { } 0 0 0
Condition0%0/3
$oldHashSize != $hashSize or $oldHash eq "\0" x $hashSize
l!l&&r!l&&!r
000
MC/DC0%0/2
$oldHashSize != $hashSize or $oldHash eq "\0" x $hashSize
$oldHashSize != $hashSize $oldHash eq "\0" x $hashSize
Truth table0%0/3
A or B
A= $oldHashSize != $hashSizeB= $oldHash eq "\0" x $hashSize
ABresult
1X1
011
000
351 {
352 0 ▶ if ( $self->sparse && $newHash eq $nullHash ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sparse and $newHash eq $nullHash) { } 0 0 0
Condition0%0/3
$self->sparse and $newHash eq $nullHash
!ll&&!rl&&r
000
MC/DC0%0/2
$self->sparse and $newHash eq $nullHash
$self->sparse $newHash eq $nullHash
Truth table0%0/3
A and B
A= $self->sparseB= $newHash eq $nullHash
ABresult
0X0
100
111
353  
354 # Sparse is only for new blocks
355 # Blocks that have been nulled out in source will
356 # also get nulled out in destination
357 0 $status = 'sparse';
358 0 $writeData = 0;
359 }
360 else {
361 0 $status = 'new';
362 }
363 }
364 elsif ( $newHash eq $oldHash ) {
365 0 $status = 'unchanged';
366 0 $writeData = 0;
367 0 $writeHash = 0;
368 }
369 else {
370 0 $status = 'changed';
371 }
372  
373 # Write data to destination
374 0 ▶ if ($writeData) {
Branch: 1 branch
BranchTrueFalseTotal
if ($writeData) 0 0 0
375 0 ▶ sysseek( $dstFh, $srcSeek, SEEK_SET )
Branch: 1 branch
BranchTrueFalseTotal
sysseek $dstFh, $srcSeek, 0 or $self->logger->logcroak("Cannot seek in dst file") 0 0 0
376 || $self->logger->logcroak('Cannot seek in dst file');
377 0 syswrite( $dstFh, $data );
378 }
379  
380 # Update hash in checksum
381 0 ▶ if ($writeHash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($writeHash) 0 0 0
382 0 ▶ sysseek( $chkFh, $block * $hashSize, SEEK_SET )
Branch: 1 branch
BranchTrueFalseTotal
sysseek $chkFh, $block * $hashSize, 0 or $self->logger->logcroak("Cannot seek in chk file") 0 0 0
383 || $self->logger->logcroak('Cannot seek in chk file');
384 0 syswrite( $chkFh, $newHash );
385 }
386  
387 $self->logger->debug(
388 0 sprintf 'Block <%u> was <%s> (<%u> to <%u>)',
389 $block, $status, $srcSeek, $srcSeek + $srcReadSize - 1 );
390 0 &{ $self->status }
391 ( $block, $status, $srcSeek, $srcSeek + $srcReadSize - 1 );
392  
393 # Next block will start here
394 0 $srcSeek += $srcReadSize;
395  
396 # Was this the last block in this batch
397 0 ▶ if ( $end && $srcSeek > $end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end and $srcSeek > $end) 0 0 0
Condition0%0/3
$end and $srcSeek > $end
!ll&&!rl&&r
000
MC/DC0%0/2
$end and $srcSeek > $end
$end $srcSeek > $end
Truth table0%0/3
A and B
A= $endB= $srcSeek > $end
ABresult
0X0
100
111
398 0 last;
399 }
400 }
401  
402 # If last block is sparse, it is not enough to seek to where the
403 # EOF should be. We need to at least write a single \0
404 0 ▶ if ( $dstFh && $status eq 'sparse' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dstFh and $status eq "sparse") 0 0 0
Condition0%0/3
$dstFh and $status eq "sparse"
!ll&&!rl&&r
000
MC/DC0%0/2
$dstFh and $status eq "sparse"
$dstFh $status eq "sparse"
Truth table0%0/3
A and B
A= $dstFhB= $status eq "sparse"
ABresult
0X0
100
111
405 0 ▶ sysseek( $dstFh, $srcSeek - 1, SEEK_SET )
Branch: 1 branch
BranchTrueFalseTotal
sysseek $dstFh, $srcSeek - 1, 0 or $self->logger->logcroak("Cannot seek in dst file") 0 0 0
406 || $self->logger->logcroak('Cannot seek in dst file');
407 0 syswrite( $dstFh, "\0" );
408 }
409 }
410  
411 0 ▶ if ( $self->truncate
Branch: 1 branch
BranchTrueFalseTotal
if ($self->truncate and $dstFh and $srcSeek and @{$self->data;} == 1 and $self->data->[0]{"start"} == 0 and $self->data->[0]{"end"} == 0) 0 0 0
Condition0%0/15
$self->truncate and $dstFh and $srcSeek and @{$self->data;} == 1 and $self->data->[0]{"start"} == 0 and $self->data->[0]{"end"} == 0
!ll&&!rl&&r
000
$self->truncate and $dstFh and $srcSeek and @{$self->data;} == 1 and $self->data->[0]{"start"} == 0
!ll&&!rl&&r
000
$self->truncate and $dstFh and $srcSeek and @{$self->data;} == 1
!ll&&!rl&&r
000
$self->truncate and $dstFh and $srcSeek
!ll&&!rl&&r
000
$self->truncate and $dstFh
!ll&&!rl&&r
000
MC/DC0%0/6
$self->truncate and $dstFh and $srcSeek and @{$self->data;} == 1 and $self->data->[0]{"start"} == 0 and $self->data->[0]{"end"} == 0
$self->truncate $dstFh $srcSeek @{$self->data;} == 1 $self->data->[0]{"start"} == 0 $self->data->[0]{"end"} == 0
Truth table0%0/7
A and B and C and D and E and F
A= $self->truncateB= $dstFhC= $srcSeekD= @{$self->data;} == 1E= $self->data->[0]{"start"} == 0F= $self->data->[0]{"end"} == 0
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
412 && $dstFh
413 && $srcSeek
414 0 && @{ $self->data } == 1
415 && $self->data->[0]->{start} == 0
416 && $self->data->[0]->{end} == 0 )
417 {
418 0 $self->logger->debug("Truncating dst file at <$srcSeek>");
419 0 truncate( $dstFh, $srcSeek );
420  
421 0 my $chkSeek = ceil( $srcSeek / $self->bs ) * $hashSize;
422 0 $self->logger->debug("Truncating chk file at <$chkSeek>");
423 0 truncate( $chkFh, $chkSeek );
424 }
425  
426 }
427 catch {
428 0 ▶ croak $_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
429 }
430 finally {
431 # If we opened the files (we got string with path), then we close them
432 # if we got a filehandle then we do nothing
433 0 ▶ if ($srcClose) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
if ($srcClose) 0 0 0
434 0 $self->logger->debug( sprintf 'closing src file <%s>', $self->src );
435 }
436 0 ▶ if ($dstClose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dstClose) 0 0 0
437 0 $self->logger->debug( sprintf 'closing dst file <%s>', $self->dst );
438 }
439 0 ▶ if ($chkClose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chkClose) 0 0 0
440 0 $self->logger->debug( sprintf 'closing chk file <%s>', $self->chk );
441 }
442 0 };
443  
444 # Make Perl::Critic happy
445 0 return;
446 }
447  
448 ################################################################
449  
450 =begin comment
451  
452 Private
453 Get file handle
454  
455 =end comment
456  
457 =cut
458  
459 sub _getFh {
460 0 ▶ my ( $self, $name, $file, $closeFile, $mode ) = @_;
Subroutine _getFh: not called CC=4 SCAR=30.0
461  
462 0 ▶ if ( my $t = reftype($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = builtin::reftype($file)) { } 0 0 0
463 0 ▶ if ( $t eq 'GLOB' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq 'GLOB') { } 0 0 0
464 0 $self->logger->debug(
465 sprintf '%s is a file handle, using that directly', $name );
466 0 return $file;
467 }
468 else {
469 0 $self->logger->logcroak(
470 sprintf '<%s> is not a supported type for %s',
471 $t, $name );
472 }
473 }
474 else {
475 0 $self->logger->debug( sprintf 'opening %s file <%s>', $name, $file );
476 0 ▶ sysopen( my $fh, $file, $mode )
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $file, $mode or $self->logger->logcroak(sprintf('error opening <%s>', $file)) 0 0 0
477 || $self->logger->logcroak( sprintf 'error opening <%s>', $file );
478 0 ${$closeFile} = 1;
479 0 return $fh;
480 }
481  
482 # Make Perl::Critic happy
483 0 croak 'We should never end here!';
484 }
485  
486 ################################################################
487  
488 =head1 LICENSE AND COPYRIGHT
489  
490 This software is copyright (c) 2019 by Thor Dreier-Hansen.
491  
492 This is free software; you can redistribute it and/or modify it under
493 the same terms as the Perl 5 programming language system itself.
494  
495 Terms of the Perl programming language system itself:
496  
497 =over
498  
499 =item * the
500 L<GNU General Public License|http://dev.perl.org/licenses/gpl1.html>
501 as published by the Free Software Foundation; either
502 L<version 1|http://dev.perl.org/licenses/gpl1.html>,
503 or (at your option) any later version, or
504  
505 =item * the L<"Artistic License"|http://dev.perl.org/licenses/artistic.html>
506  
507 =back
508  
509 See L<http://dev.perl.org/licenses/> for more information.
510  
511 =cut
512  
513 1 1; # End of IO::BlockSync
↑ Summary BlockSync/App.pm »