lib/File/Copy/Vigilant.pm

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