| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 21 | ▶ | if ( $retries !~ m/^\d+$/x ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | { | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | $retries = 'infinite'; | |||||||||||||||||||||||||||||||||||||||||
| 102 | } # Blank = continuous | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | my $check | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 21 | ▶ | = defined( $params{'check'} ) ? lc( $params{'check'} ) : 'md5'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 21 | ▶ | if ( $check !~ m/^md5|size|compare|none$/x ) { $check = 'md5'; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $postcopy and ref $postcopy ne "CODE"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | { | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | $postcopy = undef; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 21 | my $check_error = _check_files( $source, $dest ); | |||||||||||||||||||||||||||||||||||||||||
| 118 | 21 | ▶ | if ($check_error) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$retries eq "infinite" or $attempt++ <= $retries
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
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | { | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 19 | my $copy_error = _try_copy( $source, $dest, $check, $postcopy ); | |||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 18 | ▶ | if ($copy_error) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | } ## end sub copy_vigilant | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | # Syntax borrowed from core module File::Copy | ||||||||||||||||||||||||||||||||||||||||||
| 158 | sub cp; | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 2 | *cp = \©_vigilant; | |||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | sub copy; | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 2 | *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
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | { | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 2 | ▶ | if ( ref($source) eq 'GLOB' || | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
ref $source eq "GLOB" or eval {
do {
$source->isa("GLOB")
}
} or eval {
do {
$source->isa("IO::Handle")
}
}
ref $source eq "GLOB" or eval {
do {
$source->isa("GLOB")
}
}
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")
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | { | ||||||||||||||||||||||||||||||||||||||||||
| 185 | 2 | ▶ | if ( ref($dest) eq 'GLOB' || | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
ref $dest eq "GLOB" or eval {
do {
$dest->isa("GLOB")
}
} or eval {
do {
$dest->isa("IO::Handle")
}
}
ref $dest eq "GLOB" or eval {
do {
$dest->isa("GLOB")
}
}
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")
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | { | ||||||||||||||||||||||||||||||||||||||||||
| 199 | 1 | return "unable to stat source file $source"; | |||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | 16 | ▶ | if ( -d $source ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | { | ||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | return "unable to copy directory source $source"; | |||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | 16 | ▶ | unless ( -f $source || -l $source ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-f $source or -l $source
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | { | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 14 | $source_md5 = file_md5_hex($source); | |||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | 19 | ▶ | if ( ( $check eq 'md5' ) || ( $check eq 'size' ) ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$check eq "md5" or $check eq "size"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | { | ||||||||||||||||||||||||||||||||||||||||||
| 229 | 16 | $source_size = ( stat $source )[7]; | |||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | 19 | ▶ | unless ( File::Copy::copy( $source, $dest ) ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | { | ||||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | return "copy failed: $!"; | |||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | 19 | ▶ | defined($postcopy) && $postcopy->(@_); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | 18 | my $dest_size = undef; | |||||||||||||||||||||||||||||||||||||||||
| 240 | 18 | ▶ | if ( $check eq 'md5' || $check eq 'size' ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$check eq "md5" or $check eq "size"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | { | ||||||||||||||||||||||||||||||||||||||||||
| 242 | 15 | $dest_size = ( stat $dest )[7]; | |||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | 18 | ▶ | if ( $check eq 'md5' ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | { | ||||||||||||||||||||||||||||||||||||||||||
| 247 | 13 | ▶ | if ( $source_size != $dest_size ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 298 | { | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | ▶ | return wantarray ? ( 0, @copy_errors ) : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | 3 | my @errors = (); | |||||||||||||||||||||||||||||||||||||||||
| 303 | 3 | my $success = eval { | |||||||||||||||||||||||||||||||||||||||||
| 304 | 3 | ▶ | if ( unlink $source ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||||||||||||||