BlockSync/App.pm

filter: Statementstmt 61.9% 39 / 63 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 76.9% 10 / 13 Podpod 100% 1 / 1 totaltotal 49.5% 50 / 101 CC 10 SCAR 39.2
CC
10
Cov
47%
run
110.0
__ANON__
2.0
__ANON__
2.0
CRAP 50.6
« BlockSync.pm ↑ Summary
1 package IO::BlockSync::App;
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 English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # CPAN
13 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use MooX::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
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  
18 # This bundle
19 1 ▶use IO::BlockSync;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 ################################################################
22  
23 =head1 NAME
24  
25 IO::BlockSync::App - Perl module
26  
27 =head1 VERSION
28  
29 Version 0.002
30  
31 =cut
32  
33 1 our $VERSION = '0.002';
34  
35 ################################################################
36  
37 =head1 SYNOPSIS
38  
39 BlockSync can some of the same stuff that bigsync (by Egor Egorov) can
40 - it's just written in perl.
41  
42 BlockSync copies data from source file to destination file (can be a block
43 device) and calculates checksum on each block it copies.
44 On all runs after the first only the changed blocks will be copied.
45  
46 blocksync -S -s /source/path -d /destination/path
47  
48 =cut
49  
50 ################################################################
51  
52 =head1 INSTALLATION
53  
54 Look in C<README.pod>
55  
56 Can also be found on
57 L<GitHub|https://github.com/thordreier/perl-IO-BlockSync/blob/master/README.pod>
58 or L<meta::cpan|https://metacpan.org/pod/distribution/IO-BlockSync/README.pod>
59  
60 =cut
61  
62 ################################################################
63  
64 =head1 COMMAND LINE OPTIONS
65  
66 =cut
67  
68 ################################################################
69  
70 =head2 -V --version
71  
72 Print version and exit
73  
74 =cut
75  
76 1 option 'version' => (
77 is => 'ro',
78 short => 'V',
79 doc => 'print version and exit',
80 );
81  
82 ################################################################
83  
84 =head2 -v --verbose
85  
86 Print version and exit
87  
88 =cut
89  
90 1 option 'verbose' => (
91 is => 'ro',
92 short => 'v',
93 repeatable => 1,
94 doc => 'be verbose',
95 );
96  
97 ################################################################
98  
99 =head2 -s --src
100  
101 Path to source file.
102  
103 mandatory - string (containing path)
104  
105 =cut
106  
107 1 option 'src' => (
108 is => 'ro',
109 format => 's',
110 short => 's',
111 doc => 'source file path',
112 );
113  
114 ################################################################
115  
116 =head2 -d --dst
117  
118 Destination file. If not set, then only checksum file will be updated.
119  
120 optional - string (containing path)
121  
122 =cut
123  
124 1 option 'dst' => (
125 is => 'ro',
126 format => 's',
127 short => 'd',
128 doc => 'destination file path',
129 );
130  
131 ################################################################
132  
133 =head2 -c --chk
134  
135 Path to checksum file.
136  
137 mandatory - string (containing path)
138  
139 =cut
140  
141 1 option 'chk' => (
142 is => 'rwp',
143 format => 's',
144 short => 'c',
145 doc => 'checksum file path',
146 );
147  
148 ################################################################
149  
150 =head2 -b --bs
151  
152 Block size to use in bytes.
153  
154 optional - integer - defaults to 1_048_576 B (1 MB)
155  
156 =cut
157  
158 1 option 'bs' => (
159 is => 'ro',
160 format => 'i',
161 short => 'b',
162 default => 1_048_576,
163 doc => 'block size',
164 );
165  
166 ################################################################
167  
168 =head2 -S --sparse
169  
170 Seek in dst file, instead of writing blocks only containing \0
171  
172 optional - boolean - defaults to 0 (false)
173  
174 =cut
175  
176 1 option 'sparse' => (
177 is => 'ro',
178 short => 'S',
179 default => 0,
180 ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars)
181 doc => 'seek in dst file, instead of writing blocks only containing \0',
182 ## use critic
183 );
184  
185 ################################################################
186  
187 =head2 -t --truncate
188  
189 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).
190  
191 optional - boolean - defaults to 0 (false)
192  
193 =cut
194  
195 1 option 'truncate' => (
196 is => 'ro',
197 short => 't',
198 default => 0,
199 doc => 'truncate destination file',
200 );
201  
202 ################################################################
203  
204 =head1 METHODS
205  
206 =cut
207  
208 ################################################################
209  
210 =head2 run
211  
212 C<bin/blocksync> calls C<IO::BlockSync::App::run> to start the program.
213  
214 If you just run C<blocksync> command then ignore this method.
215  
216 =cut
217  
218 0 ▶sub run {
Subroutine run: not called CC=10 SCAR=47.0
Pod documented
219 0 my ($self) = @_;
220  
221 # Create object with data from command line and run this function again
222 0 ▶ if ( !blessed $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($self)) 0 0 0
223 0 return $self->new_with_options->run;
224 }
225  
226 0 ▶ if ( $self->version ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->version) 0 0 0
227 0 say "blocksync version $VERSION";
228 0 exit;
229 }
230  
231 0 ▶ if ( !$self->src ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->src) 0 0 0
232 0 $self->options_usage( 1, '<src> must be set' );
233 }
234  
235 0 ▶ if ( $self->dst && !$self->chk ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->dst and not $self->chk) { } 0 0 0
elsif (not $self->dst || $self->chk) { } 0 0 0
Condition0%0/3
$self->dst and not $self->chk
!ll&&!rl&&r
000
MC/DC0%0/2
$self->dst and not $self->chk
$self->dst not $self->chk
Truth table0%0/3
A and not(B)
A= $self->dstB= not $self->chk
ABresult
0X0
100
111
236 0 $self->_set_chk( $self->dst . '.chk' );
237 }
238 ▶ elsif ( !$self->dst && !$self->chk ) {
Condition0%0/3
$self->dst || $self->chk
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->dst || $self->chk
$self->dst $self->chk
Truth table0%0/3
A || B
A= $self->dstB= $self->chk
ABresult
1X1
011
000
239 0 $self->options_usage( 1, 'at least one of <chk> or <dst> must be set' );
240 }
241  
242 0 my $status;
243 0 ▶ if ( $self->verbose ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) { } 0 0 0
244 0 ▶ $status = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
245 0 printf
246 "Copying block=<%s> type=<%s> start=<%s> end=<%s> size=<%s>\n",
247 @_, $_[3] - $_[2];
248 0 };
249 0 ▶ if ( $self->verbose > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose > 1) 0 0 0
250 0 Log::Log4perl->init( \<<"EOF");
251 log4perl.logger = DEBUG, Screen
252 log4perl.appender.Screen = Log::Log4perl::Appender::Screen
253 log4perl.appender.Screen.layout = Log::Log4perl::Layout::PatternLayout
254 log4perl.appender.Screen.layout.ConversionPattern = %d %p %M %m%n
255 EOF
256 }
257 }
258 else {
259 0 ▶ $status = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
260 0 $OUTPUT_AUTOFLUSH++;
261 0 print( "\rCopying block " . shift );
262 }
263 0 }
264  
265 0 BlockSync( %{$self}, 'status' => $status, );
266  
267 0 say '';
268  
269 # Make Perl::Critic happy
270 0 return;
271 }
272  
273 ################################################################
274  
275 =head1 LICENSE AND COPYRIGHT
276  
277 This software is copyright (c) 2019 by Thor Dreier-Hansen.
278  
279 This is free software; you can redistribute it and/or modify it under
280 the same terms as the Perl 5 programming language system itself.
281  
282 Terms of the Perl programming language system itself:
283  
284 =over
285  
286 =item * the
287 L<GNU General Public License|http://dev.perl.org/licenses/gpl1.html>
288 as published by the Free Software Foundation; either
289 L<version 1|http://dev.perl.org/licenses/gpl1.html>,
290 or (at your option) any later version, or
291  
292 =item * the L<"Artistic License"|http://dev.perl.org/licenses/artistic.html>
293  
294 =back
295  
296 See L<http://dev.perl.org/licenses/> for more information.
297  
298 =cut
299  
300 1 1; # End of IO::BlockSync::App
« BlockSync.pm ↑ Summary