lib/File/Copy/Vigilant.pm

filter: Statementstmt 85.5% 95 / 111 Branchbran 75.0% 54 / 72 Conditioncond 62.9% 17 / 27 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 76.8% 183 / 238 SCAR 41.8
CC
46
Cov
79%
_check_files
27.1
_try_copy
15.1
copy_vigilant
13.8
CRAP 65.5
« blib/lib/File/Copy/Vigilant.pm ↑ Summary Build »
1 package File::Copy::Vigilant;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 our @EXPORT = qw(copy_vigilant move_vigilant);
8 2 our @EXPORT_OK = qw(copy move cp mv);
9  
10 2 ▶use File::Copy qw(); # Don't import copy / move into the local namespace
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::Compare;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Digest::MD5::File qw(file_md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 File::Copy::Vigilant - Copy and move files with verification and retries
17  
18 =cut
19  
20 2 our $VERSION = '1.2';
21  
22 =head1 SYNOPSIS
23  
24 use File::Copy::Vigilant;
25  
26 copy_vigilant( $source, $destination );
27 move_vigilant( $source, $destination );
28  
29 =head1 DESCRIPTION
30  
31 A module for when your files absolutely, positively have to get there.
32  
33 By default the copy and move functions will perform MD5 sums on the source
34 and destination file to ensure that the destination file is exactly the same
35 as the source file upon completion. If the copy or move fails for any reason
36 it will attempt 2 retries by default.
37  
38 =head1 FUNCTIONS
39  
40 =head2 copy_vigilant, copy, cp
41  
42 Copies a file, with post-copy verification and optional retries.
43  
44 copy_vigilant(
45 $source, $destination,
46 [ check => (checktype), ]
47 [ retires => (number of retries), ]
48 );
49  
50 "checktype" is one of the following:
51  
52 md5 - Get the MD5 sum of the source before copying, and compare it to
53 the MD5 of the destination after copying (default)
54 size - Get the size of the source before copying, and compare it to the
55 size of the destination after copying
56 compare - Perform a byte-for-byte comparison of the source and destination
57 after copying
58 none - Do not perform a check of the source to the destination
59  
60 "number of retries" is one of the following:
61  
62 0 - No retries are performed
63 integer - The number of retries to perform (if a 1 is passed, then the
64 copy will be attempted a maximum of 2 times). Must be a
65 positive, whole number.
66 'infinite' - The string 'infinite' (and all other strings for that matter)
67 will cause it to retry continuously until success
68  
69 The default is 2 retries (or three attempts total).
70  
71 If called in a scalar context, it returns 0 for failure or 1 for success. If
72 called in a list context, the return value is a 1 or 0, followed by a list of
73 error messages.
74  
75 The 'cp' and 'copy' named versions the function are not exported by default,
76 and you must specify them explicitly to use them:
77  
78 use File::Copy::Vigilant qw(cp mv);
79  
80 cp( $source, $destination );
81  
82 Or
83  
84 use File::Copy::Vigilant qw(copy move);
85  
86 copy( $source, $destination );
87  
88 =cut
89  
90 sub copy_vigilant
91 {
92  
93 21 ▶ my ( $source, $dest, %params ) = @_;
Subroutine copy_vigilant: called CC=13 SCAR=26.3
Pod documented
94  
95 21 my @errors = ();
96 21 my $success = eval {
97  
98 21 ▶ my $retries = defined( $params{'retries'} ) ? $params{'retries'} : 2;
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'retries'} ? : 6 15 21
99 21 ▶ if ( $retries !~ m/^\d+$/x )
Branch: 1 branch
BranchTrueFalseTotal
unless ($retries =~ /^\d+$/x) 0 21 21
100 {
101 0 $retries = 'infinite';
102 } # Blank = continuous
103  
104 my $check
105 21 ▶ = defined( $params{'check'} ) ? lc( $params{'check'} ) : 'md5';
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'check'} ? : 8 13 21
106 21 ▶ if ( $check !~ m/^md5|size|compare|none$/x ) { $check = 'md5'; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($check =~ /^md5|size|compare|none$/x) 0 21 21
107  
108 # This hook allows us to do some whitebox testing by modifying the
109 # results of the copy. You probably don't want this unless you're
110 # testing this module.
111 21 my $postcopy = $params{'_postcopy'};
112 21 ▶ if ( defined($postcopy) && ref($postcopy) ne 'CODE' )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $postcopy and ref $postcopy ne "CODE") 0 21 21
Condition66%2/3
defined $postcopy and ref $postcopy ne "CODE"
!ll&&!rl&&r
1560
MC/DC0%0/2
defined $postcopy and ref $postcopy ne "CODE"
defined $postcopy ref $postcopy ne "CODE"
Truth table66%2/3
A and B
A= defined $postcopyB= ref $postcopy ne "CODE"
ABresult
0X0
100
111
113 {
114 0 $postcopy = undef;
115 }
116  
117 21 my $check_error = _check_files( $source, $dest );
118 21 ▶ if ($check_error)
Branch: 1 branch
BranchTrueFalseTotal
if ($check_error) 5 16 21
119 {
120 5 push @errors, "Pre-copy check failed: $check_error\n";
121 5 return 0;
122 }
123  
124 16 my $attempt = 0;
125 16 ▶ while ( ( $retries eq 'infinite' ) || ( $attempt++ <= $retries ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($retries eq "infinite" or $attempt++ <= $retries) 19 4 23
Condition66%2/3
$retries eq "infinite" or $attempt++ <= $retries
l!l&&r!l&&!r
0194
MC/DC50%1/2
$retries eq "infinite" or $attempt++ <= $retries
$retries eq "infinite" $attempt++ <= $retries
Truth table66%2/3
A or B
A= $retries eq "infinite"B= $attempt++ <= $retries
ABresult
1X1
011
000
126 {
127  
128 19 my $copy_error = _try_copy( $source, $dest, $check, $postcopy );
129  
130 18 ▶ if ($copy_error)
Branch: 1 branch
BranchTrueFalseTotal
if ($copy_error) { } 7 11 18
131 {
132 7 push @errors, "Copy attempt $attempt failed: $copy_error\n";
133 }
134 else
135 {
136 11 return 1;
137 }
138  
139 }
140  
141 # If we got here, then we looped as many times as
142 # we were allowed without a success
143 4 return 0;
144  
145 };
146  
147 21 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 20 21
148 {
149 1 $success = 0;
150 1 push @errors, "Internal error in copy_vigilant: $@\n";
151 }
152  
153 21 ▶ return wantarray ? ( $success, @errors ) : $success;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 21 0 21
154  
155 } ## end sub copy_vigilant
156  
157 # Syntax borrowed from core module File::Copy
158 sub cp;
159 2 *cp = \&copy_vigilant;
160  
161 sub copy;
162 2 *copy = \&copy_vigilant;
163  
164 sub _check_files
165 {
166  
167 21 ▶ my ( $source, $dest ) = @_;
Subroutine _check_files: called CC=15 SCAR=33.0
168  
169 21 ▶ if ( ref $source )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $source) { } 2 19 21
elsif (ref \$source eq 'GLOB') { } 0 19 19
170 {
171 2 ▶ if ( ref($source) eq 'GLOB' ||
Branch: 1 branch
BranchTrueFalseTotal
if (ref $source eq "GLOB" or eval { do { $source->isa("GLOB") } } or eval { do { $source->isa("IO::Handle") } }) 1 0 1
Condition50%3/6
ref $source eq "GLOB" or eval { do { $source->isa("GLOB") } } or eval { do { $source->isa("IO::Handle") } }
l!l&&r!l&&!r
200
ref $source eq "GLOB" or eval { do { $source->isa("GLOB") } }
l!l&&r!l&&!r
110
MC/DC0%0/3
ref $source eq "GLOB" or eval { do { $source->isa("GLOB") } } or eval { do { $source->isa("IO::Handle") } }
ref $source eq "GLOB" eval { do { $source->isa("GLOB") } } eval { do { $source->isa("IO::Handle") } }
Truth table50%2/4
A or B or C
A= ref $source eq "GLOB"B= eval { do { $source->isa("GLOB") } }C= eval { do { $source->isa("IO::Handle") } }
ABCresult
1XX1
01X1
0011
0000
172 1 eval { $source->isa('GLOB') } ||
173 0 eval { $source->isa('IO::Handle') } )
174 {
175 2 return "can't use filehandle for source";
176 }
177 }
178 elsif ( ref( \$source ) eq 'GLOB' )
179 {
180 0 return "can't use filehandle for source";
181 }
182  
183 19 ▶ if ( ref $dest )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $dest) { } 2 17 19
elsif (ref \$dest eq 'GLOB') { } 0 17 17
184 {
185 2 ▶ if ( ref($dest) eq 'GLOB' ||
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dest eq "GLOB" or eval { do { $dest->isa("GLOB") } } or eval { do { $dest->isa("IO::Handle") } }) 1 0 1
Condition50%3/6
ref $dest eq "GLOB" or eval { do { $dest->isa("GLOB") } } or eval { do { $dest->isa("IO::Handle") } }
l!l&&r!l&&!r
200
ref $dest eq "GLOB" or eval { do { $dest->isa("GLOB") } }
l!l&&r!l&&!r
110
MC/DC0%0/3
ref $dest eq "GLOB" or eval { do { $dest->isa("GLOB") } } or eval { do { $dest->isa("IO::Handle") } }
ref $dest eq "GLOB" eval { do { $dest->isa("GLOB") } } eval { do { $dest->isa("IO::Handle") } }
Truth table50%2/4
A or B or C
A= ref $dest eq "GLOB"B= eval { do { $dest->isa("GLOB") } }C= eval { do { $dest->isa("IO::Handle") } }
ABCresult
1XX1
01X1
0011
0000
186 1 eval { $dest->isa('GLOB') } ||
187 0 eval { $dest->isa('IO::Handle') } )
188 {
189 2 return "Can't use filehandle for desination";
190 }
191 }
192 elsif ( ref( \$dest ) eq 'GLOB' )
193 {
194 0 return "can't use filehandle for destination";
195 }
196  
197 17 ▶ unless ( stat $source )
Branch: 1 branch
BranchTrueFalseTotal
unless (stat $source) 1 16 17
198 {
199 1 return "unable to stat source file $source";
200 }
201  
202 16 ▶ if ( -d $source )
Branch: 1 branch
BranchTrueFalseTotal
if (-d $source) 0 16 16
203 {
204 0 return "unable to copy directory source $source";
205 }
206  
207 16 ▶ unless ( -f $source || -l $source )
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $source or -l $source) 0 16 16
Condition33%1/3
-f $source or -l $source
l!l&&r!l&&!r
1600
MC/DC0%0/2
-f $source or -l $source
-f $source -l $source
Truth table33%1/3
A or B
A= -f $sourceB= -l $source
ABresult
1X1
011
000
208 {
209 0 return "unable to copy non-file source $source";
210 }
211  
212 # If we got this far then both the source and dest look OK
213 16 return '';
214 } ## end sub _check_files
215  
216 sub _try_copy
217 {
218  
219 19 ▶ my ( $source, $dest, $check, $postcopy ) = @_;
Subroutine _try_copy: called CC=15 SCAR=27.1
220  
221 19 my $source_md5 = undef;
222 19 my $source_size = undef;
223 19 ▶ if ( $check eq 'md5' )
Branch: 1 branch
BranchTrueFalseTotal
if ($check eq "md5") 14 5 19
224 {
225 14 $source_md5 = file_md5_hex($source);
226 }
227 19 ▶ if ( ( $check eq 'md5' ) || ( $check eq 'size' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($check eq "md5" or $check eq "size") 16 3 19
Condition100%3/3
$check eq "md5" or $check eq "size"
l!l&&r!l&&!r
1423
MC/DC100%2/2
$check eq "md5" or $check eq "size"
$check eq "md5" $check eq "size"
Truth table100%3/3
A or B
A= $check eq "md5"B= $check eq "size"
ABresult
1X1
011
000
228 {
229 16 $source_size = ( stat $source )[7];
230 }
231  
232 19 ▶ unless ( File::Copy::copy( $source, $dest ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (File::Copy::copy($source, $dest)) 0 19 19
233 {
234 0 return "copy failed: $!";
235 }
236  
237 19 ▶ defined($postcopy) && $postcopy->(@_);
Branch: 1 branch
BranchTrueFalseTotal
defined $postcopy and &$postcopy(@_) 9 10 19
238  
239 18 my $dest_size = undef;
240 18 ▶ if ( $check eq 'md5' || $check eq 'size' )
Branch: 1 branch
BranchTrueFalseTotal
if ($check eq "md5" or $check eq "size") 15 3 18
Condition100%3/3
$check eq "md5" or $check eq "size"
l!l&&r!l&&!r
1323
MC/DC100%2/2
$check eq "md5" or $check eq "size"
$check eq "md5" $check eq "size"
Truth table100%3/3
A or B
A= $check eq "md5"B= $check eq "size"
ABresult
1X1
011
000
241 {
242 15 $dest_size = ( stat $dest )[7];
243 }
244  
245 18 ▶ if ( $check eq 'md5' )
Branch: 3 branches
BranchTrueFalseTotal
if ($check eq 'md5') { } 13 5 18
elsif ($check eq 'size') { } 2 3 5
elsif ($check eq 'compare') { } 2 1 3
246 {
247 13 ▶ if ( $source_size != $dest_size )
Branch: 1 branch
BranchTrueFalseTotal
if ($source_size != $dest_size) 5 8 13
248 {
249 5 return "pre-md5 size check failed";
250 }
251 8 my $dest_md5 = file_md5_hex($dest);
252 8 ▶ if ( $source_md5 ne $dest_md5 )
Branch: 1 branch
BranchTrueFalseTotal
if ($source_md5 ne $dest_md5) 0 8 8
253 {
254 0 return "md5 check failed";
255 }
256 }
257 elsif ( $check eq 'size' )
258 {
259 2 ▶ if ( $source_size != $dest_size )
Branch: 1 branch
BranchTrueFalseTotal
if ($source_size != $dest_size) 1 1 2
260 {
261 1 return "size check failed";
262 }
263 }
264 elsif ( $check eq 'compare' )
265 {
266 2 ▶ if ( File::Compare::compare( $source, $dest ) )
Branch: 1 branch
BranchTrueFalseTotal
if (File::Compare::compare($source, $dest)) 1 1 2
267 {
268 1 return "file compare failed";
269 }
270 }
271  
272 # If we got this far then the copy was a success!
273 11 return '';
274 } ## end sub _try_copy
275  
276 =head2 move_vigilant, move, mv
277  
278 The syntax and behavior is exactly the same as copy_vigilant, except it
279 perfoms an unlink as the last step.
280  
281 This is terribly inefficient compared to File::Copy's move, which in most
282 cases is a simple filesystem rename.
283  
284 'move' and 'mv' are not imported by default, you'll have to add them in
285 the use syntax (see copy_vigilant for details).
286  
287 =cut
288  
289 sub move_vigilant
290 {
291  
292 3 ▶ my ( $source, $dest, %params ) = @_;
Subroutine move_vigilant: called CC=6 SCAR=22.5
Pod documented
293  
294 3 my ( $copy_success, @copy_errors )
295 = copy_vigilant( $source, $dest, %params );
296  
297 3 ▶ unless ($copy_success)
Branch: 1 branch
BranchTrueFalseTotal
unless ($copy_success) 0 3 3
298 {
299 0 ▶ return wantarray ? ( 0, @copy_errors ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
300 }
301  
302 3 my @errors = ();
303 3 my $success = eval {
304 3 ▶ if ( unlink $source )
Branch: 1 branch
BranchTrueFalseTotal
if (unlink $source) { } 3 0 3
305 {
306 3 return 1;
307 }
308 else
309 {
310 0 push @errors, "Unable to remove source $source "
311 . "(destination file $dest has been left in place)\n";
312 0 return 0;
313 }
314 };
315  
316 3 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
317 {
318 0 $success = 0;
319 0 push @errors, "Internal error in move_vigilant: $@\n";
320 }
321  
322 3 ▶ return wantarray ? ( $success, @errors ) : $success;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
323  
324 }
325  
326 # Syntax borrowed from core module File::Copy
327 sub mv;
328 2 *mv = \&move_vigilant;
329  
330 sub move;
331 2 *move = \&move_vigilant;
332  
333 =head1 AUTHOR
334  
335 Anthony Kilna, C<< <anthony at kilna.com> >> - L<http://anthony.kilna.com>
336  
337 =head1 BUGS
338  
339 Please report any bugs or feature requests to
340 C<bug-file-copy-vigilant at rt.cpan.org>,
341 or through the web interface at
342 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-Copy-Vigilant>.
343 I will be notified, and then you'll automatically be notified
344 of progress on your bug as I make changes.
345  
346  
347 =head1 SUPPORT
348  
349 You can find documentation for this module with the perldoc command.
350  
351 perldoc File::Copy::Vigilant
352  
353 You can also look for information at:
354  
355 =over 4
356  
357 =item * RT: CPAN's request tracker
358  
359 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Copy-Vigilant>
360  
361 =item * AnnoCPAN: Annotated CPAN documentation
362  
363 L<http://annocpan.org/dist/File-Copy-Vigilant>
364  
365 =item * CPAN Ratings
366  
367 L<http://cpanratings.perl.org/d/File-Copy-Vigilant>
368  
369 =item * Search CPAN
370  
371 L<http://search.cpan.org/dist/File-Copy-Vigilant>
372  
373 =back
374  
375 =head1 SEE ALSO
376  
377 File::Copy - File::Copy::Reliable
378  
379 =head1 COPYRIGHT & LICENSE
380  
381 Copyright 2012 Kilna Companies.
382  
383 This program is free software; you can redistribute it and/or modify it
384 under the terms of either: the GNU General Public License as published
385 by the Free Software Foundation; or the Artistic License.
386  
387 See http://dev.perl.org/licenses/ for more information.
388  
389 =cut
390  
391 2 1; # End of File::Copy::Vigilant
« blib/lib/File/Copy/Vigilant.pm ↑ Summary Build »