| 1 | package File::Copy::NoClobber; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 13 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 13 | our $VERSION = '0.2.1'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 13 | ▶ | use parent 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 13 | ▶ | use File::Copy (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 13 | ▶ | use File::Spec::Functions qw(splitpath catpath catfile); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 13 | ▶ | use File::Basename qw(basename dirname); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 13 | ▶ | use Fcntl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 13 | our @EXPORT = qw(copy move); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 13 | my $pattern = " (%02d)"; | |||||||||||||||||||||||||||
| 18 | 13 | my $MAX_COUNT = 1e4; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 13 | my $WarnNewFile = 0; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | sub import { | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 12 | ▶ | my $pkg = shift; | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 12 | my %args = @_; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 12 | ▶ | if ( exists $args{-warn} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 1 | $WarnNewFile = delete $args{-warn}; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 12 | ▶ | $pattern = delete $args{-pattern} // $pattern; | ||||||||||||||||||||||||||
|
Condition66%2/3
delete $args{"-pattern"} // $pattern
MC/DC0%0/2
delete $args{"-pattern"} // $pattern
delete $args{"-pattern"}
$pattern
Truth table66%2/3
A // B
A= delete $args{"-pattern"}B= $pattern
| |||||||||||||||||||||||||||||
| 33 | 12 | _check_pattern($pattern); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 12 | @_ = %args; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 12 | __PACKAGE__->export_to_level( 1, $pkg, @_ ); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | sub _check_pattern { | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 12 | ▶ | my $ptn = shift; | ||||||||||||||||||||||||||
|
Subroutine
_check_pattern: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 12 | ▶ | if ( sprintf($ptn,1) eq sprintf($ptn,2) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | croak "Invalid noclobber pattern '$pattern'"; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub _declobber { | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 35 | ▶ | my($from,$to) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_declobber: called CC=14 SCAR=26.7
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 35 | my $from_bn = basename $from; | |||||||||||||||||||||||||||
| 56 | 35 | ▶ | my $orig_dest_file = my $dest_file = -d $to ? catfile( $to, $from_bn ) : $to; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 35 | my $fh; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 35 | my $write_test = 1; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 35 | ▶ | if ( -d $to ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 63 | 32 | $write_test = -w $to; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | elsif ( -f $to ) { | ||||||||||||||||||||||||||||
| 66 | 1 | $write_test = -w dirname $to; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 35 | ▶ | if ( not $write_test ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 1 | croak "Destination is not writable"; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 34 | ▶ | if ( -f $from and ref $to ne "GLOB" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-f $from and ref $to ne "GLOB"
MC/DC0%0/2
-f $from and ref $to ne "GLOB"
-f $from
ref $to ne "GLOB"
Truth table33%1/3
A and B
A= -f $fromB= ref $to ne "GLOB"
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 34 | ▶ | if ( !-d dirname $to ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 1 | croak "Invalid destination, should be in an existing directory"; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # use eval in case autodie or friends get in here | ||||||||||||||||||||||||||||
| 80 | 33 | my $opened = eval { | |||||||||||||||||||||||||||
| 81 | 33 | sysopen $fh, $dest_file, O_EXCL|O_CREAT|O_WRONLY; | |||||||||||||||||||||||||||
| 82 | }; | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 33 | my $count = 0; | |||||||||||||||||||||||||||
| 85 | 33 | my $fp = filename_with_sprintf_pattern( $dest_file ); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 33 | ▶ | while (not $opened and $!{EEXIST} ) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
Condition66%2/3
not $opened and $!{"EEXIST"}
MC/DC50%1/2
not $opened and $!{"EEXIST"}
not $opened
$!{"EEXIST"}
Truth table66%2/3
not(A) and B
A= not $openedB= $!{"EEXIST"}
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 18 | $opened = eval { | |||||||||||||||||||||||||||
| 90 | 18 | sysopen | |||||||||||||||||||||||||||
| 91 | $fh, | ||||||||||||||||||||||||||||
| 92 | ($dest_file = sprintf( $fp, ++$count )), | ||||||||||||||||||||||||||||
| 93 | O_CREAT|O_EXCL|O_WRONLY; | ||||||||||||||||||||||||||||
| 94 | }; | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 18 | ▶ | if ($count > $MAX_COUNT) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | croak "Failed to find a nonclobbering filename, tried to increment counter $MAX_COUNT times"; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 33 | ▶ | if (not fileno $fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | croak $!; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 33 | binmode $fh; | |||||||||||||||||||||||||||
| 107 | 33 | switch_off_buffering($fh); | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 33 | ▶ | if ($dest_file ne $orig_dest_file and $WarnNewFile) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$dest_file ne $orig_dest_file and $WarnNewFile
MC/DC100%2/2
$dest_file ne $orig_dest_file and $WarnNewFile
$dest_file ne $orig_dest_file
$WarnNewFile
Truth table100%3/3
A and B
A= $dest_file ne $orig_dest_fileB= $WarnNewFile
| |||||||||||||||||||||||||||||
| 112 | 1 | carp "Destionation changed to " . basename $dest_file; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 33 | return ($fh,$dest_file); | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | sub copy { | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 33 | ▶ | my @args = @_; | ||||||||||||||||||||||||||
|
Subroutine
copy: called CC=3 SCAR=11.6
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 33 | my($from,$to,$buffersize) = @args; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 33 | my($fh,$dest_file) = _declobber($from,$to); | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 31 | ▶ | $args[1] = $fh // $dest_file; | ||||||||||||||||||||||||||
|
Condition33%1/3
$fh // $dest_file
MC/DC0%0/2
$fh // $dest_file
$fh
$dest_file
Truth table33%1/3
A // B
A= $fhB= $dest_file
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # return destination filename, as it may be altered | ||||||||||||||||||||||||||||
| 130 | 31 | ▶ | return File::Copy::copy(@args) && $dest_file; | ||||||||||||||||||||||||||
|
Condition66%2/3
File::Copy::copy(@args) && $dest_file
MC/DC0%0/2
File::Copy::copy(@args) && $dest_file
File::Copy::copy(@args)
$dest_file
Truth table33%1/3
A && B
A= File::Copy::copy(@args)B= $dest_file
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | sub move { | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 2 | ▶ | my @args = @_; | ||||||||||||||||||||||||||
|
Subroutine
move: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 2 | my($from,$to,$buffersize) = @args; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 2 | my($fh,$dest_file) = _declobber($from,$to); | |||||||||||||||||||||||||||
| 141 | 2 | close $fh; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 2 | $args[1] = $dest_file; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # return destination filename, as it may be altered | ||||||||||||||||||||||||||||
| 146 | 2 | ▶ | return File::Copy::move(@args) && $dest_file; | ||||||||||||||||||||||||||
|
Condition66%2/3
File::Copy::move(@args) && $dest_file
MC/DC0%0/2
File::Copy::move(@args) && $dest_file
File::Copy::move(@args)
$dest_file
Truth table33%1/3
A && B
A= File::Copy::move(@args)B= $dest_file
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | sub filename_with_sprintf_pattern { | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 35 | ▶ | (my $path = shift) =~ s/%/%%/g; | ||||||||||||||||||||||||||
|
Subroutine
filename_with_sprintf_pattern: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 35 | my($vol,$dir,$fn) = splitpath($path); | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 35 | ▶ | if ( $fn =~ /\./ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 13 | ▶ | $fn =~ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | s{ (?= \. [^\.]+ $ ) } | ||||||||||||||||||||||||||||
| 160 | 13 | { $pattern }ex | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | or die "Failed inserting noclobbering pattern into file"; | ||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | else { | ||||||||||||||||||||||||||||
| 165 | 22 | $fn .= $pattern; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 35 | return catpath($vol,$dir,$fn); | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | sub switch_off_buffering { | ||||||||||||||||||||||||||||
| 173 | 33 | ▶ | my $h = select(shift); | ||||||||||||||||||||||||||
|
Subroutine
switch_off_buffering: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 174 | 33 | $|=1; | |||||||||||||||||||||||||||
| 175 | 33 | select($h); | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 13 | 1; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =encoding utf8 | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head1 NAME | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | File::Copy::NoClobber - Rename copied files safely if destionation exists | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | use File::Copy::NoClobber; | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | copy( "file.txt", "elsewhere/" ); # elsewhere/file.txt | ||||||||||||||||||||||||||||
| 191 | copy( "file.txt", "elsewhere/" ); # elsewhere/file (01).txt | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | # similar with move | ||||||||||||||||||||||||||||
| 194 | move( "file.txt", "elsewhere/" ); # elsewhere/file (02).txt | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | use File::Copy::NoClobber -warn => 1; # warns when name is changed | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | use File::Copy::NoClobber -pattern => "[%04d]" # custom noclobber | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | The module exports copy() and move(). They are wrappers around C<copy> | ||||||||||||||||||||||||||||
| 203 | and C<move> in L<File::Copy>. | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =head1 INTERFACE | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =head2 copy( $from, $to [, $buffersize] ) | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | Supportes the same arguments as L<File::Copy>. | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | Checks if the operation would overwrite an existing file, if so adds a | ||||||||||||||||||||||||||||
| 212 | counter to the destionation filename as shown in the SYNOPSIS. | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | The module uses sysopen with O_EXCL and an increasing counter to | ||||||||||||||||||||||||||||
| 215 | determine a working filename. The second argument is then replaced | ||||||||||||||||||||||||||||
| 216 | with this filehandle and passed to C<File::Copy::copy>. | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | The counter inserted to filenames is C<" (%02d)"> by default, but can | ||||||||||||||||||||||||||||
| 219 | be changed on import. | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | It returns the filename written to or undef if unsuccesful. | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =head2 move( $from, $to ) | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | Supportes the same arguments as L<File::Copy>. | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | Determines destination filename in the same way as C<copy>, but the | ||||||||||||||||||||||||||||
| 228 | move operation is used on the filename rather than the filehandle, to | ||||||||||||||||||||||||||||
| 229 | allow rename to be used. | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =head1 DEPENDENCIES | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | This module does not introduce dependencies. It does not use modules | ||||||||||||||||||||||||||||
| 234 | not already in use in File::Copy. | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | Torbjørn Lindahl C<< torbjorn.lindahl@gmail.com >> | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | =head1 CONTRIBUTORS | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | Core ideas from I<Botje>, I<huf> and I<tm604> in #perl@freenode | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | =head1 LICENCE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | Copyright (c) 2016, Torbjørn Lindahl C<< torbjorn.lindahl@gmail.com >>. | ||||||||||||||||||||||||||||
| 247 | All rights reserved. | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | This module is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 250 | under the same terms as Perl itself. See L<perlartistic>. | ||||||||||||||||||||||||||||