blib/lib/File/Copy/NoClobber.pm

filter: Statementstmt 96.8% 91 / 94 Branchbran 82.1% 23 / 28 Conditioncond 57.1% 12 / 21 MC/DCmcdc 21.4% 3 / 14 Subroutinesub 100% 16 / 16 Podpod 50.0% 2 / 4 totaltotal 83.0% 147 / 177 CC 22 SCAR 19.0
CC
22
Cov
88%
_declobber
14.4
copy
3.2
filename_with_sprintf_pattern
3.0
CRAP 6.7
↑ Summary
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.3';
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 12 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
23  
24 12 my $pkg = shift;
25  
26 12 my %args = @_;
27  
28 12 ▶ if ( exists $args{-warn} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"-warn"}) 1 11 12
29 1 $WarnNewFile = delete $args{-warn};
30 }
31  
32 12 ▶ $pattern = delete $args{-pattern} // $pattern;
Condition66%2/3
delete $args{"-pattern"} // $pattern
l!l&&r!l&&!r
1110
MC/DC0%0/2
delete $args{"-pattern"} // $pattern
delete $args{"-pattern"} $pattern
Truth table66%2/3
A // B
A= delete $args{"-pattern"}B= $pattern
ABresult
1X1
011
000
33 12 _check_pattern($pattern);
34  
35 12 @_ = %args;
36  
37 12 __PACKAGE__->export_to_level( 1, $pkg, @_ );
38  
39 }
40  
41 12 ▶sub _check_pattern {
Subroutine _check_pattern: called CC=2 SCAR=8.1
42  
43 12 my $ptn = shift;
44  
45 12 ▶ if ( sprintf($ptn,1) eq sprintf($ptn,2) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (sprintf($ptn, 1) eq sprintf($ptn, 2)) 0 12 12
46 0 croak "Invalid noclobber pattern '$pattern'";
47 }
48  
49 }
50  
51 35 ▶sub _declobber {
Subroutine _declobber: called CC=14 SCAR=26.7
52  
53 35 my($from,$to) = @_;
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
BranchTrueFalseTotal
-d $to ? : 32 3 35
57  
58 35 my $fh;
59  
60 35 my $write_test = 1;
61  
62 35 ▶ if ( -d $to ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $to) { } 32 3 35
elsif (-f $to) { } 1 2 3
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
BranchTrueFalseTotal
unless ($write_test) 1 34 35
70 1 croak "Destination is not writable";
71 }
72  
73 34 ▶ if ( -f $from and ref $to ne "GLOB" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $from and ref $to ne "GLOB") 34 0 34
Condition33%1/3
-f $from and ref $to ne "GLOB"
!ll&&!rl&&r
0034
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"
ABresult
0X0
100
111
74  
75 34 ▶ if ( !-d dirname $to ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d dirname($to)) 1 33 34
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
BranchTrueFalseTotal
if (not $opened and $!{"EEXIST"}) 18 33 51
Condition66%2/3
not $opened and $!{"EEXIST"}
!ll&&!rl&&r
33018
MC/DC50%1/2
not $opened and $!{"EEXIST"}
not $opened $!{"EEXIST"}
Truth table66%2/3
not(A) and B
A= not $openedB= $!{"EEXIST"}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($count > $MAX_COUNT) 0 18 18
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
BranchTrueFalseTotal
unless (fileno $fh) 0 33 33
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
BranchTrueFalseTotal
if ($dest_file ne $orig_dest_file and $WarnNewFile) 1 32 33
Condition100%3/3
$dest_file ne $orig_dest_file and $WarnNewFile
!ll&&!rl&&r
17151
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
ABresult
0X0
100
111
112 1 carp "Destination changed to " . basename $dest_file;
113 }
114  
115 33 return ($fh,$dest_file);
116  
117 }
118  
119 33 ▶sub copy {
Subroutine copy: called CC=3 SCAR=11.6
Pod documented
120  
121 33 my @args = @_;
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
l!l&&r!l&&!r
3100
MC/DC0%0/2
$fh // $dest_file
$fh $dest_file
Truth table33%1/3
A // B
A= $fhB= $dest_file
ABresult
1X1
011
000
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
!ll&&!rl&&r
0427
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
ABresult
0X0
100
111
131  
132 }
133  
134 2 ▶sub move {
Subroutine move: called CC=2 SCAR=7.1
Pod documented
135  
136 2 my @args = @_;
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;
Condition33%1/3
File::Copy::move(@args) && $dest_file
!ll&&!rl&&r
002
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
ABresult
0X0
100
111
147  
148 }
149  
150 35 ▶sub filename_with_sprintf_pattern {
Subroutine filename_with_sprintf_pattern: called CC=3 SCAR=11.0
Pod undocumented
151  
152 35 (my $path = shift) =~ s/%/%%/g;
153  
154 35 my($vol,$dir,$fn) = splitpath($path);
155  
156 35 ▶ if ( $fn =~ /\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fn =~ /\./) { } 13 22 35
157 13 ▶ $fn =~
Branch: 1 branch
BranchTrueFalseTotal
$fn =~ s/ (?= \. [^\.]+ $ ) /$pattern;/ex or die "Failed inserting noclobbering pattern into file" 13 0 13
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 33 ▶sub switch_off_buffering {
Subroutine switch_off_buffering: called CC=1 SCAR=0.0
Pod undocumented
173 33 my $h = select(shift);
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 destination 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 Supports 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 destination 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 unsuccessful.
222  
223 =head2 move( $from, $to )
224  
225 Supports 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 LICENSE 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>.
↑ Summary