blib/lib/File/NCopy.pm

filter: Statementstmt 27.9% 62 / 222 Branchbran 10.9% 20 / 182 Conditioncond 6.7% 5 / 74 MC/DCmcdc 0.0% 0 / 46 Subroutinesub 41.6% 10 / 24 Podpod 53.8% 7 / 13 totaltotal 18.5% 104 / 561 CC 118 SCAR 59.1
CC
118
Cov
18%
_docopy_file_file
1056.0
_recurse_from_dir
650.0
copy
40.5
CRAP 368.2
↑ Summary
1 package File::NCopy;
2 1 ▶use 5.004; # just because I think you should upgrade :)
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =head1 NAME
5  
6 File::NCopy - Deprecated module. Use File::Copy::Recursive instead. Copy file, file. Copy file[s] | dir[s], dir
7  
8 =head1 SYNOPSIS
9  
10 use File::NCopy qw(copy);
11  
12 copy "file","other_file";
13 copy "file1","file2","file3","directory";
14  
15 # we want to copy the directory recursively
16 copy \1,"directory1","directory2";
17 copy \1,"file1","file2","directory1","file3","directory2","file4",
18 "directory";
19  
20 # can also use references to file handles, this is for backward
21 # compatibility with File::Copy
22 copy \*FILE1,\*FILE2;
23 copy \*FILE1,"file";
24 copy "file1",\*FILE2;
25  
26  
27 # we don't specify \1 as the first argument because we don't want to
28 # copy directories recursively
29 copy "*.c","*.pl","programs";
30 copy "*", "backup";
31  
32 use File::NCopy;
33  
34 # the below are the default config values
35 $file = File::NCopy->new(
36 'recursive' => 0,
37 'preserve' => 0,
38 'follow_links' => 0,
39 'force_write' => 0,
40 'set_permission' => \&File::NCopy::u_chmod,
41 'file_check' => \&File::NCopy::f_check,
42 'set_times' => \&File::NCopy::s_times,
43 );
44  
45 set_permission will take two file names, the original to get the
46 file permissions from and the new file to set the file permissions
47 for.
48  
49 file_check takes two parameters, the file names to check the file to
50 copy from and the file to copy to. I am using flock for Unix
51 systems.
52 Default for this is \&File::NCopy::f_check. On Unix you can also use
53 \&File::NCopy::unix_check. This one compares the inode and device
54 numbers.
55  
56 set_times is used if the preserve attribute is true. It preserves
57 the access and modification time of the file and also attempts to
58 set the owner of the file to the original owner. This can be useful
59 in a script used by root, though enyone can preserve the access and
60 modification times. This also takes two arguments. The file to get
61 the stats from and apply the stats to.
62  
63 On Unix boxes you shouldn't need to worry. On other system you may
64 want to supply your own sub references.
65  
66 $file = File::NCopy->new(recursive => 1);
67 $file->copy "file","other_file";
68 $file->copy "directory1","directory2";
69  
70 $file = File::NCopy->new(u_chmod => \&my_chmod,f_check => \&my_fcheck);
71 $file->copy "directory1","directory2";
72  
73  
74 =head1 DESCRIPTION
75  
76 B<File::NCopy::copy> copies files to directories, or a single file to
77 another file. You can also use a reference to a file handle if you wish
78 whem doing a file to file copy. The functionality is very similar to
79 B<cp>. If the argument is a directory to directory copy and the
80 recursive flag is set then it is done recursively like B<cp -R>.
81 In fact it behaves like cp on Unix for the most part.
82 If called in array context, an array of successful copies is returned,
83 otherwise the number of succesful copies is returned. If passed a file
84 handle, it's difficult to make sure the file we are copying isn't the
85 same that we are copying to, since by opening the file in write mode it
86 gets pooched. To avoid this use file names instead, if at all possible,
87 especially for the to file. If passed a file handle, it is not closed
88 when copy returns, files opened by copy are closed.
89  
90 =over 4
91  
92 =item B<copy>
93  
94 Copies a file to another file. Or a file to a directory. Or multiple
95 files and directories to another directory. Or a directory to another
96 directory. Wildcard arguments are expanded, except for the last
97 argument which should not be expanded. The file and directory
98 permissions are set to the orginating file's permissions and if preserve
99 is set the access and modification times are also set. If preserve is
100 set then the uid and gid will also be attempted to be set, though this
101 may only for for the men in white hats.
102 In list context it returns all the names of the files/directories that
103 were successfully copied. In scalar context it returns the number of
104 successful copies made. A directory argument is considerd a single
105 successful copy if it manages to copy anything at all. To make a
106 directory to directory copy the recursive flag must be set.
107  
108 =item B<cp>
109  
110 Just calls copy. It's there to be compatible with File::Copy.
111  
112 =item B<new>
113  
114 If used then you can treat this as an object oriented module with some
115 configuration abilities.
116  
117 =item B<recursive>
118  
119 If used as an object then you can use this to set the recursive
120 attribute. It can also be set when instantiating with new. The other
121 attributes must all be set when instantiating the object. If it isn't
122 specified then directories are not followed.
123  
124 =item B<preserve>
125  
126 Attempt to preserve the last modification and access time as well as
127 user and group id's. This is a useful feature for sysadmins, though the
128 access and modification time should always be preservable, the uid and
129 gid may not.
130  
131 =item B<follow_links>
132  
133 If the link is to a directory and this attribute is true then the
134 directory is followed and recursively copied. Otherwise a link is made
135 to the root directory the link points to. eg.
136  
137 /sys/ is a link to /usr/src/sys/ is a link to /usr/src/i386/sys
138 then the link /sys/ is actually created in the source directory as a
139 link to /usr/src/i386/sys/ rather than /usr/src/sys/ since if the link
140 /usr/src/sys/ is removed then we lost the link even though the directory
141 we originally intended to link to still exists.
142  
143 =item B<force_write>
144  
145 Force the writing of a file even if the permissions are read only on it.
146  
147 =back
148  
149 =head1 EXAMPLE
150  
151 See SYNOPSIS.
152  
153 =head1 BUGS
154  
155 When following links the target directory might not exactly the same as
156 the source directory. The reason is that we have to make sure we don't
157 follow circular or dead links. This is really a feature though the
158 result may not quite resemble the source dir, the overall content will
159 be the same. :)
160  
161 From Ken Healy (Version 0.34)
162  
163 On Win32, The use of backslash for paths is required.
164  
165 =head1 AUTHOR
166  
167 Gabor Egressy B<gabor AT vmunix.com>
168  
169 Copyright (c) 1998 Gabor Egressy. All rights reserved. All wrongs
170 reversed. This program is free software; you can redistribute and/or
171 modify it under the same terms as Perl itself.
172  
173 Some ideas gleaned from File::Copy by Aaron Sherman & Charles Bailey,
174 but the code was written from scratch.
175  
176 Patch at versions 0.33, and 0.34 added by MZSANFORD.
177  
178 0.35, 0.36 - Alexandr Ciornii (alexchorny AT gmail.com)
179  
180 =cut
181  
182 1 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
183 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
184 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
185 1 ▶use vars qw(@EXPORT_OK @ISA $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
186 1 @ISA = qw(Exporter);
187 # we export nothing by default :)
188 1 @EXPORT_OK = qw(copy cp);
189  
190 1 $VERSION = '0.36';
191  
192 # this works on Unix
193 sub u_chmod($$)
194 0 ▶{
Subroutine u_chmod: not called CC=3 SCAR=24.8
Pod undocumented
195 0 my ($file_from,$file_to) = @_;
196  
197 0 my ($mode) = (stat $file_from)[2];
198 0 ▶ chmod $mode & 0777,$file_to
Branch: 1 branch
BranchTrueFalseTotal
unless ref $file_to eq "GLOB" or ref $file_to eq "FileHandle" 0 0 0
Condition0%0/3
ref $file_to eq "GLOB" or ref $file_to eq "FileHandle"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $file_to eq "GLOB" or ref $file_to eq "FileHandle"
ref $file_to eq "GLOB" ref $file_to eq "FileHandle"
Truth table0%0/3
A or B
A= ref $file_to eq "GLOB"B= ref $file_to eq "FileHandle"
ABresult
1X1
011
000
199 unless ref $file_to eq 'GLOB' || ref $file_to eq 'FileHandle';
200 0 1;
201 }
202  
203 # this also works on Unix
204 sub f_check($$)
205 0 ▶{
Subroutine f_check: not called CC=3 SCAR=24.8
Pod undocumented
206 0 my ($file_from,$file_to) = @_;
207  
208 # get a shared lock on file to copy from
209 0 ▶ flock $file_from,5
Branch: 1 branch
BranchTrueFalseTotal
flock $file_from, 5 or return 0 0 0 0
210 or return 0;
211 # try and get an exclusive lock on the file to copy to
212 flock $file_to,6
213 0 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
flock $file_to, 6 or do { flock $file_from, 8; return 0 } 0 0 0
214 0 flock $file_from,8;
215 0 return 0;
216 };
217 0 flock $file_from,8;
218 0 flock $file_to,8;
219  
220 0 1;
221 }
222  
223 # this also works on Unix, it's not the default but you can easily use
224 # it by using the module in an object oriented way
225 # $copy = File::NCopy->new('file_check' => \&File::NCopy::unix_check);
226 sub unix_check($$)
227 0 ▶{
Subroutine unix_check: not called CC=3 SCAR=24.8
Pod undocumented
228 0 my ($file_from,$file_to) = @_;
229  
230 0 my ($fdev,$fino) = (stat $file_from)[0,1];
231 0 my ($tdev,$tino) = (stat $file_to)[0,1];
232  
233 0 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
if $fdev == $tdev and $fino == $tino 0 0 0
Condition0%0/3
$fdev == $tdev and $fino == $tino
!ll&&!rl&&r
000
MC/DC0%0/2
$fdev == $tdev and $fino == $tino
$fdev == $tdev $fino == $tino
Truth table0%0/3
A and B
A= $fdev == $tdevB= $fino == $tino
ABresult
0X0
100
111
234 if $fdev == $tdev && $fino == $tino;
235 0 1;
236 }
237  
238 sub s_times($$)
239 0 ▶{
Subroutine s_times: not called CC=5 SCAR=34.0
Pod undocumented
240 0 my ($file_from,$file_to) = @_;
241  
242 0 my ($uid,$gid,$atime,$mtime) = (stat $file_from)[4,5,8,9];
243  
244 0 ▶ utime $atime,$mtime,$file_to
Branch: 1 branch
BranchTrueFalseTotal
unless ref $file_to eq "GLOB" or ref $file_to eq "FileHandle" 0 0 0
Condition0%0/3
ref $file_to eq "GLOB" or ref $file_to eq "FileHandle"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $file_to eq "GLOB" or ref $file_to eq "FileHandle"
ref $file_to eq "GLOB" ref $file_to eq "FileHandle"
Truth table0%0/3
A or B
A= ref $file_to eq "GLOB"B= ref $file_to eq "FileHandle"
ABresult
1X1
011
000
245 unless ref $file_to eq 'GLOB' || ref $file_to eq 'FileHandle';
246  
247 # this may only work for men in white hats; on Unix
248 0 ▶ chown $uid,$gid,$file_to
Branch: 1 branch
BranchTrueFalseTotal
unless ref $file_to eq "GLOB" or ref $file_to eq "FileHandle" 0 0 0
Condition0%0/3
ref $file_to eq "GLOB" or ref $file_to eq "FileHandle"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $file_to eq "GLOB" or ref $file_to eq "FileHandle"
ref $file_to eq "GLOB" ref $file_to eq "FileHandle"
Truth table0%0/3
A or B
A= ref $file_to eq "GLOB"B= ref $file_to eq "FileHandle"
ABresult
1X1
011
000
249 unless ref $file_to eq 'GLOB' || ref $file_to eq 'FileHandle';
250 0 1;
251 }
252  
253 # all the actual copying is done here, folks ;)
254 sub _docopy_file_file($$$)
255 0 ▶{
Subroutine _docopy_file_file: not called CC=32 SCAR=69.6
256 0 my $this = shift;
257 0 my ($file_from,$file_to) = @_;
258 0 local (*FILE_FROM,*FILE_TO);
259 0 my ($was_handle);
260  
261 # did we get a file handle ?
262 0 ▶ unless(ref $file_from eq 'GLOB' || ref $file_from eq 'FileHandle') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file_from eq 'GLOB' or ref $file_from eq 'FileHandle') { } 0 0 0
Condition0%0/3
ref $file_from eq "GLOB" or ref $file_from eq "FileHandle"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $file_from eq "GLOB" or ref $file_from eq "FileHandle"
ref $file_from eq "GLOB" ref $file_from eq "FileHandle"
Truth table0%0/3
A or B
A= ref $file_from eq "GLOB"B= ref $file_from eq "FileHandle"
ABresult
1X1
011
000
263 open FILE_FROM,"<$file_from"
264 0 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
open FILE_FROM, "<$file_from" or do { print "*** Couldn't open from file <$!> ==> $file_from\n" if $this->{"_debug"}; return 0 } 0 0 0
265 print "*** Couldn\'t open from file <$!> ==> $file_from\n"
266 0 ▶ if $this->{'_debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"_debug"} 0 0 0
267 0 return 0;
268 };
269 }
270 else {
271 0 *FILE_FROM = *$file_from;
272 }
273  
274 0 ▶ unless(ref $file_to eq 'GLOB' || ref $file_to eq 'FileHandle') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file_to eq 'GLOB' or ref $file_to eq 'FileHandle') { } 0 0 0
Condition0%0/3
ref $file_to eq "GLOB" or ref $file_to eq "FileHandle"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $file_to eq "GLOB" or ref $file_to eq "FileHandle"
ref $file_to eq "GLOB" ref $file_to eq "FileHandle"
Truth table0%0/3
A or B
A= ref $file_to eq "GLOB"B= ref $file_to eq "FileHandle"
ABresult
1X1
011
000
275 # we must open in update mode since on some systems exclusive
276 # locks are only granted to files that are going to be written;
277 0 ▶ open FILE_TO,"+<$file_to"
Branch: 1 branch
BranchTrueFalseTotal
open FILE_TO, "+<$file_to" or goto NO_FILE 0 0 0
278 or goto NO_FILE; # no file, so file can't be the same :)
279 }
280 else {
281 0 *FILE_TO = *$file_to;
282 0 $was_handle = 1;
283 }
284  
285 0 ▶ unless(-t FILE_FROM || -t FILE_TO) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-t FILE_FROM or -t FILE_TO) 0 0 0
Condition0%0/3
-t FILE_FROM or -t FILE_TO
l!l&&r!l&&!r
000
MC/DC0%0/2
-t FILE_FROM or -t FILE_TO
-t FILE_FROM -t FILE_TO
Truth table0%0/3
A or B
A= -t FILE_FROMB= -t FILE_TO
ABresult
1X1
011
000
286 0 ▶ $this->{'file_check'}->(\*FILE_FROM,\*FILE_TO)
Branch: 1 branch
BranchTrueFalseTotal
$this->{"file_check"}->(\*FILE_FROM, \*FILE_TO) or return 0 0 0 0
287 or return 0;
288 }
289  
290 NO_FILE:
291 # files aren't the same; now open for writing unless we got a
292 # filehandle
293 0 ▶ if(! $was_handle && ! $this->{test}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($was_handle or $this->{"test"}) 0 0 0
Condition0%0/3
$was_handle or $this->{"test"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$was_handle or $this->{"test"}
$was_handle $this->{"test"}
Truth table0%0/3
A or B
A= $was_handleB= $this->{"test"}
ABresult
1X1
011
000
294 open FILE_TO,">$file_to"
295 or chmod 0644, "$file_to"
296 0 ▶ if $this->{'force_write'};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"force_write"} 0 0 0
Condition0%0/6
$this->{'force_write'} && (open(FILE_TO, ">$file_to") || chmod(0644, "$file_to"))
!ll&&!rl&&r
000
open FILE_TO, ">$file_to" or chmod 0644, "$file_to"
l!l&&r!l&&!r
000
MC/DC0%0/3
$this->{'force_write'} && (open(FILE_TO, ">$file_to") || chmod(0644, "$file_to"))
$this->{'force_write'} open FILE_TO, ">$file_to" chmod 0644, "$file_to"
Truth table0%0/4
A && B or C
A= $this->{'force_write'}B= open FILE_TO, ">$file_to"C= chmod 0644, "$file_to"
ABCresult
0XX0
1000
11X1
1011
297 open FILE_TO,">$file_to"
298 0 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
open FILE_TO, ">$file_to" or do { print "*** Couldn't open to file <$!> ==> $file_to\n" if $this->{"_debug"}; return 0 } 0 0 0
299 print "*** Couldn\'t open to file <$!> ==> $file_to\n"
300 0 ▶ if $this->{'_debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"_debug"} 0 0 0
301 0 return 0;
302 };
303 }
304  
305 # and now for the braindead OS's
306 0 ▶ binmode FILE_FROM unless ($this->{test});
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"test"} 0 0 0
307 0 ▶ binmode FILE_TO unless ($this->{test});
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"test"} 0 0 0
308  
309 0 my $buf = '';
310 0 my ($len,$write_n);
311 # read file and write to new file, recover from write errors and
312 # read errors; we accept however much we read and try to write it
313 # 8K is a nice buffer size for most file systems
314 0 ▶ while(! $this->{test} && 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $this->{"test"} and 1) 0 0 0
Condition0%0/2
not $this->{"test"} and 1
!ll
00
MC/DC0%0/1
not $this->{"test"} and 1
not $this->{"test"}
Truth table0%0/2
not(A)
A= not $this->{"test"}
Aresult
00
11
315 0 $len = sysread(FILE_FROM,$buf,8192);
316 0 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 0 0 0
317 unless defined $len;
318 last
319 0 ▶ unless $len > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $len > 0 0 0 0
320 0 ▶ while($len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len) 0 0 0
321 0 $write_n = syswrite(FILE_TO,$buf,$len);
322 0 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $write_n 0 0 0
323 unless defined $write_n;
324 0 $len -= $write_n;
325 }
326 }
327  
328 0 $this->{'set_permission'}->($file_from,$file_to);
329  
330 # we only close files we opened
331 0 ▶ unless ($this->{test}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{"test"}) 0 0 0
332 0 ▶ close FILE_FROM
Branch: 1 branch
BranchTrueFalseTotal
unless ref $file_from eq "GLOB" or ref $file_from eq "FileHandle" 0 0 0
Condition0%0/3
ref $file_from eq "GLOB" or ref $file_from eq "FileHandle"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $file_from eq "GLOB" or ref $file_from eq "FileHandle"
ref $file_from eq "GLOB" ref $file_from eq "FileHandle"
Truth table0%0/3
A or B
A= ref $file_from eq "GLOB"B= ref $file_from eq "FileHandle"
ABresult
1X1
011
000
333 unless ref $file_from eq 'GLOB' || ref $file_from eq 'FileHandle';
334 0 ▶ close FILE_TO
Branch: 1 branch
BranchTrueFalseTotal
unless ref $file_to eq "GLOB" or ref $file_to eq "FileHandle" 0 0 0
Condition0%0/3
ref $file_to eq "GLOB" or ref $file_to eq "FileHandle"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $file_to eq "GLOB" or ref $file_to eq "FileHandle"
ref $file_to eq "GLOB" ref $file_to eq "FileHandle"
Truth table0%0/3
A or B
A= ref $file_to eq "GLOB"B= ref $file_to eq "FileHandle"
ABresult
1X1
011
000
335 unless ref $file_to eq 'GLOB' || ref $file_to eq 'FileHandle';
336 }
337  
338 # this was moved from above the unless statement per Ken Healy in version 0.34
339 $this->{'set_times'}->($file_from,$file_to)
340 0 ▶ if $this->{'preserve'};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"preserve"} 0 0 0
341  
342 print "$file_from ==> $file_to\n"
343 0 ▶ if $this->{'_debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"_debug"} 0 0 0
344  
345 0 1;
346 }
347  
348 sub get_path($)
349 0 ▶{
Subroutine get_path: not called CC=2 SCAR=17.9
Pod undocumented
350 0 my $dir = shift;
351  
352 0 my $save_dir = Cwd::cwd;
353 0 ▶ chdir $dir
Branch: 1 branch
BranchTrueFalseTotal
chdir $dir or return undef 0 0 0
354 or return undef;
355 0 $dir = Cwd::cwd;
356 0 chdir $save_dir;
357  
358 0 return $dir;
359 }
360  
361 sub _recurse_from_dir($$$);
362  
363 # we never actually change the directory :)
364 sub _recurse_from_dir($$$)
365 0 ▶{
Subroutine _recurse_from_dir: not called CC=25 SCAR=64.8
366 0 my $this = shift;
367 0 my ($from_dir,$to_dir) = @_;
368 0 local (*DIR);
369 # MZS - v0.39 - Changed from slash to File::Spec;
370 0 my $dir_sep = File::Spec->catfile('a','b');
371 0 $dir_sep =~ s/^a(.+)b$/$1/;
372  
373  
374 opendir DIR,$from_dir
375 0 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $from_dir or do { print "*** Couldn't opendir <$!> ==> $from_dir\n" if $this->{"_debug"}; return 0 } 0 0 0
376 print "*** Couldn\'t opendir <$!> ==> $from_dir\n"
377 0 ▶ if $this->{'_debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"_debug"} 0 0 0
378 0 return 0;
379 };
380 my @files = readdir DIR
381 0 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
my(@files) = readdir DIR or do { print "*** Couldn't read dir <$!> ==> $from_dir\n" if $this->{"_debug"}; return 0 } 0 0 0
382 print "*** Couldn\'t read dir <$!> ==> $from_dir\n"
383 0 ▶ if $this->{'_debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"_debug"} 0 0 0
384 0 return 0;
385 };
386 0 closedir DIR;
387  
388 0 my $made_dir;
389 0 ▶ unless(-e $to_dir && ! $this->{test}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $to_dir and not $this->{"test"}) 0 0 0
Condition0%0/3
-e $to_dir and not $this->{"test"}
!ll&&!rl&&r
000
MC/DC0%0/2
-e $to_dir and not $this->{"test"}
-e $to_dir not $this->{"test"}
Truth table0%0/3
A and not(B)
A= -e $to_dirB= not $this->{"test"}
ABresult
0X0
100
111
390 0 ▶ mkdir $to_dir,0777
Branch: 1 branch
BranchTrueFalseTotal
mkdir $to_dir, 511 or return 0 0 0 0
391 or return 0;
392 0 $made_dir = 1;
393 }
394  
395 0 my ($retval,$ret,$link,$save_link);
396  
397 # make sure we don't end up with a recursive, circular link
398 # this isn't totally foolproof, though it does prevent circular
399 # links
400 0 ▶ if($this->{'follow_links'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"follow_links"}) 0 0 0
401 0 ▶ if(defined($save_link = get_path $from_dir)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($save_link = &get_path($from_dir))) 0 0 0
402 0 $this->{'_links'}->{$save_link} = 1;
403 }
404 }
405  
406 0 for (@files) {
407 next
408 0 ▶ if /^\.\.?$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\.\.?$/ 0 0 0
409 0 ▶ if(-f $from_dir . $dir_sep . $_) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $from_dir . $dir_sep . $_) { } 0 0 0
elsif (-d "$from_dir$dir_sep$_") { } 0 0 0
410 0 $ret = _docopy_file_file $this, $from_dir . $dir_sep . $_ ,
411 $to_dir . $dir_sep . $_;
412 }
413 elsif(-d "$from_dir$dir_sep$_") {
414 0 ▶ if($this->{'follow_links'} && -l "$from_dir$dir_sep$_") {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"follow_links"} and -l "$from_dir$dir_sep$_") 0 0 0
Condition0%0/3
$this->{"follow_links"} and -l "$from_dir$dir_sep$_"
!ll&&!rl&&r
000
MC/DC0%0/2
$this->{"follow_links"} and -l "$from_dir$dir_sep$_"
$this->{"follow_links"} -l "$from_dir$dir_sep$_"
Truth table0%0/3
A and B
A= $this->{"follow_links"}B= -l "$from_dir$dir_sep$_"
ABresult
0X0
100
111
415 0 $link = get_path "$from_dir$dir_sep$_";
416 }
417 0 ▶ if(! -l "$from_dir$dir_sep$_" || $this->{'follow_links'}
Branch: 1 branch
BranchTrueFalseTotal
if (not -l "$from_dir$dir_sep$_" or $this->{'follow_links'} and defined $link and not exists $this->{'_links'}{$link}) { } 0 0 0
Condition0%0/9
not -l "$from_dir$dir_sep$_" or $this->{"follow_links"} and defined $link and not exists $this->{"_links"}{$link}
l!l&&r!l&&!r
000
$this->{"follow_links"} and defined $link and not exists $this->{"_links"}{$link}
!ll&&!rl&&r
000
$this->{"follow_links"} and defined $link
!ll&&!rl&&r
000
MC/DC0%0/4
not -l "$from_dir$dir_sep$_" or $this->{"follow_links"} and defined $link and not exists $this->{"_links"}{$link}
not -l "$from_dir$dir_sep$_" $this->{"follow_links"} defined $link not exists $this->{"_links"}{$link}
Truth table0%0/5
not(A) or B and C and not(D)
A= not -l "$from_dir$dir_sep$_"B= $this->{"follow_links"}C= defined $linkD= not exists $this->{"_links"}{$link}
ABCDresult
1XXX1
01111
00XX0
010X0
01100
418 && defined $link
419 && ! exists $this->{'_links'}->{$link}) {
420 0 $ret = _recurse_from_dir
421 $this,$from_dir . $dir_sep . $_ ,$to_dir . $dir_sep . $_;
422 }
423 else {
424 0 ▶ if(defined($link = get_path "$from_dir$dir_sep$_")) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($link = &get_path("$from_dir$dir_sep$_"))) 0 0 0
425 0 $ret = symlink $link, "$to_dir$dir_sep$_";
426 }
427 }
428 }
429 0 ▶ $retval = $retval || $ret;
Condition0%0/3
$retval || $ret
l!l&&r!l&&!r
000
MC/DC0%0/2
$retval || $ret
$retval $ret
Truth table0%0/3
A || B
A= $retvalB= $ret
ABresult
1X1
011
000
430 }
431  
432 0 ▶ if($made_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($made_dir) 0 0 0
433 0 $this->{'set_permission'}->($from_dir,$to_dir);
434 $this->{'set_times'}->($from_dir,$to_dir)
435 0 ▶ if $this->{'preserve'};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"preserve"} 0 0 0
436 }
437  
438 # remove the name so that there can be link to it from other dirs
439 # that are not subdirs of this one
440 0 ▶ if($this->{'follow_links'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"follow_links"}) 0 0 0
441 0 delete $this->{'_links'}->{$save_link};
442 }
443  
444 0 return $retval;
445 }
446  
447 sub _docopy_dir_dir($$$)
448 0 ▶{
Subroutine _docopy_dir_dir: not called CC=4 SCAR=30.0
449 0 my $this = shift;
450 0 my ($dir_from,$dir_to) = @_;
451 0 my ($from_name);
452 # MZS - v0.39 - Changed from slash to File::Spec;
453 0 my $dir_sep = File::Spec->catfile('a','b');
454 0 $dir_sep =~ s/^a(.+)b$/$1/;
455  
456 0 $dir_from =~ s/$dir_sep$//; # remove trailing slash, if any
457 0 ▶ if($dir_from =~ tr/$dir_sep//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir_from =~ tr/$_deiprs//) { } 0 0 0
458 0 $from_name = substr $dir_from,rindex($dir_from,$dir_sep) + 1;
459 }
460 else {
461 0 $from_name = $dir_from;
462 0 ▶ if($from_name =~ /^\.\.?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from_name =~ /^\.\.?$/) 0 0 0
463 0 $from_name = '';
464 }
465 }
466  
467 0 ▶ unless($dir_to =~ /$dir_sep$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir_to =~ /$dir_sep$/) 0 0 0
468 0 $dir_to .= $dir_sep;
469 }
470 0 $dir_to .= $from_name;
471  
472 0 $this->{'_links'} = {};
473  
474 0 _recurse_from_dir $this, $dir_from,$dir_to;
475 }
476  
477 sub _docopy_file_dir($$$)
478 0 ▶{
Subroutine _docopy_file_dir: not called CC=2 SCAR=17.9
479 0 my $this = shift;
480 0 my ($file,$dir) = @_;
481 0 my $file_to;
482 # MZS - v0.39 - Changed from slash to File::Spec;
483 0 my $dir_sep = File::Spec->catfile('a','b');
484 0 $dir_sep =~ s/^a(.+)b$/$1/;
485  
486 0 ▶ if($file =~ tr/$dir_sep//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ tr/$_deiprs//) { } 0 0 0
487 0 $file_to = substr $file,rindex($file,$dir_sep) + 1;
488 }
489 else {
490 0 $file_to = $file;
491 }
492
493 0 $dir =~ s/$dir_sep$//; # remove trailing slash
494  
495 0 _docopy_file_file $this, $file,$dir.$dir_sep.$file_to;
496 }
497  
498 # this just redirects calls, like copy ;)
499 sub _docopy_files_dir($$@)
500 1 ▶{
Subroutine _docopy_files_dir: called CC=9 SCAR=32.5
501 1 my $this = shift;
502 1 my $copies = shift;
503 1 my $dir = pop;
504  
505 1 for (@_) {
506 1 ▶ if(-d $_ && $this->{'recursive'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $_ and $this->{'recursive'}) { } 1 0 1
elsif (-f $_) { } 0 0 0
Condition33%1/3
-d $_ and $this->{"recursive"}
!ll&&!rl&&r
001
MC/DC0%0/2
-d $_ and $this->{"recursive"}
-d $_ $this->{"recursive"}
Truth table33%1/3
A and B
A= -d $_B= $this->{"recursive"}
ABresult
0X0
100
111
507 1 ▶ if ($this->{test}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'test'}) { } 1 0 1
508 1 push @$copies, $_;
509 } else {
510 0 ▶ _docopy_dir_dir $this, $_, $dir
Branch: 1 branch
BranchTrueFalseTotal
&_docopy_dir_dir($this, $_, $dir) and push @$copies, $_ 0 0 0
511 and push @$copies, $_;
512 }
513 }
514 elsif(-f $_) {
515 0 ▶ if ($this->{test}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'test'}) { } 0 0 0
516 0 push @$copies, $_;
517 } else {
518 0 ▶ _docopy_file_dir $this, $_, $dir
Branch: 1 branch
BranchTrueFalseTotal
&_docopy_file_dir($this, $_, $dir) and push @$copies, $_ 0 0 0
519 and push @$copies, $_;
520 }
521 }
522 }
523 1 1;
524 }
525  
526 # does glob work on all systems?
527 sub expand(@)
528 1 ▶{
Subroutine expand: called CC=3 SCAR=11.0
Pod undocumented
529 1 my @args;
530  
531 return
532 1 ▶ if @_ < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 0 1 1
533  
534 1 ▶ for (my $i = 0;$i < $#_;++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#_) 1 1 2
535 1 ▶ push @args,glob $_[$i];
Subroutine BEGIN: called CC=1 SCAR=0.0
536 }
537 1 push @args,$_[$#_];
538  
539 1 @args;
540 }
541  
542 sub new(@); #prototype
543  
544 # this just redirects calls
545 sub copy(@)
546 1 ▶{
Subroutine copy: called CC=11 SCAR=37.0
Pod documented
547 1 my $this;
548  
549 # were we called through an object reference?
550 1 ▶ if(ref $_[0] eq 'File::NCopy') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'File::NCopy') { } 1 0 1
551 1 $this = shift;
552 }
553 else {
554 # no, so let's make one
555 0 $this = new File::NCopy;
556 0 ▶ if(ref $_[0] eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "SCALAR") 0 0 0
557 0 my $rec = shift;
558 0 $this->recursive($$rec);
559 }
560 }
561  
562 1 my @copies;
563 1 my @args = expand @_;
564  
565 0 print "passed args ==> ".join(',',map {"'$_'"} @args)."\n"
566 1 ▶ if $this->{'_debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"_debug"} 0 1 1
567  
568 # one or more files/directories to a directory
569 1 ▶ if(@args >= 2 && -d $args[$#args]) {
Branch: 2 branches
BranchTrueFalseTotal
if (@args >= 2 and -d $args[$#args]) { } 1 0 1
elsif (@args == 2 and -f $args[0]) { } 0 0 0
Condition33%1/3
@args >= 2 and -d $args[$#args]
!ll&&!rl&&r
001
MC/DC0%0/2
@args >= 2 and -d $args[$#args]
@args >= 2 -d $args[$#args]
Truth table33%1/3
A and B
A= @args >= 2B= -d $args[$#args]
ABresult
0X0
100
111
570 1 ▶ print "Copy to dir started.\n" if ($this->{'_debug'});
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"_debug"} 0 1 1
571 1 _docopy_files_dir $this, \@copies, @args;
572 }
573 # file to file
574 ▶ elsif(@args == 2 && -f $args[0]) {
Condition0%0/3
@args == 2 and -f $args[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@args == 2 and -f $args[0]
@args == 2 -f $args[0]
Truth table0%0/3
A and B
A= @args == 2B= -f $args[0]
ABresult
0X0
100
111
575 0 ▶ if ($this->{test}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'test'}) { } 0 0 0
576 0 push @copies, $args[0];
577 } else {
578 0 ▶ _docopy_file_file $this, $args[0],$args[1]
Branch: 1 branch
BranchTrueFalseTotal
&_docopy_file_file($this, $args[0], $args[1]) and push @copies, $args[0] 0 0 0
579 and push @copies, $args[0];
580 }
581 }
582  
583 1 @copies;
584 }
585  
586 0 ▶sub cp(@) {
Subroutine cp: not called CC=1 SCAR=6.9
Pod documented
587 0 return copy @_;
588 }
589  
590 # instantiate our object
591 sub new(@)
592 1 ▶{
Subroutine new: called CC=16 SCAR=34.5
Pod documented
593 1 my $this = shift;
594
595 1 my $conf = {
596 'test' => 0,
597 'recursive' => 0,
598 'preserve' => 0,
599 'follow_links' => 0,
600 'force_write' => 0,
601 '_debug' => 0,
602 'set_permission' => \&File::NCopy::u_chmod,
603 'file_check' => \&File::NCopy::f_check,
604 'set_times' => \&File::NCopy::s_times,
605 '_links' => {},
606 };
607  
608 1 my $ref;
609 1 ▶ if(@_ % 2 == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ % 2 == 0) { } 1 0 1
elsif (ref $_[0] eq 'HASH') { } 0 0 0
610 1 my %ref = @_;
611 1 $ref = \%ref;
612 }
613 elsif(ref $_[0] eq 'HASH') {
614 0 $ref = shift;
615 }
616  
617 1 ▶ if(ref $ref eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ref eq "HASH") 1 0 1
618 $conf->{'test'} = abs int $ref->{'test'}
619 1 ▶ if defined $ref->{'test'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ref->{"test"} 1 0 1
620 $conf->{'recursive'} = abs int $ref->{'recursive'}
621 1 ▶ if defined $ref->{'recursive'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ref->{"recursive"} 0 1 1
622 $conf->{'preserve'} = abs int $ref->{'preserve'}
623 1 ▶ if defined $ref->{'preserve'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ref->{"preserve"} 0 1 1
624 $conf->{'follow_links'} = abs int $ref->{'follow_links'}
625 1 ▶ if defined $ref->{'follow_links'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ref->{"follow_links"} 0 1 1
626 $conf->{'force_write'} = abs int $ref->{'force_write'}
627 1 ▶ if defined $ref->{'force_write'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ref->{"force_write"} 0 1 1
628 $conf->{'_debug'} = abs int $ref->{'_debug'}
629 1 ▶ if defined $ref->{'_debug'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ref->{"_debug"} 0 1 1
630 $conf->{'set_permission'} = $ref->{'set_permission'}
631 if defined $ref->{'set_permission'}
632 1 ▶ && ref $ref->{'set_permission'} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if defined $ref->{"set_permission"} and ref $ref->{"set_permission"} eq "CODE" 0 1 1
Condition33%1/3
defined $ref->{"set_permission"} and ref $ref->{"set_permission"} eq "CODE"
!ll&&!rl&&r
100
MC/DC0%0/2
defined $ref->{"set_permission"} and ref $ref->{"set_permission"} eq "CODE"
defined $ref->{"set_permission"} ref $ref->{"set_permission"} eq "CODE"
Truth table33%1/3
A and B
A= defined $ref->{"set_permission"}B= ref $ref->{"set_permission"} eq "CODE"
ABresult
0X0
100
111
633 $conf->{'file_check'} = $ref->{'file_check'}
634 if defined $ref->{'file_check'}
635 1 ▶ && ref $ref->{'file_check'} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if defined $ref->{"file_check"} and ref $ref->{"file_check"} eq "CODE" 0 1 1
Condition33%1/3
defined $ref->{"file_check"} and ref $ref->{"file_check"} eq "CODE"
!ll&&!rl&&r
100
MC/DC0%0/2
defined $ref->{"file_check"} and ref $ref->{"file_check"} eq "CODE"
defined $ref->{"file_check"} ref $ref->{"file_check"} eq "CODE"
Truth table33%1/3
A and B
A= defined $ref->{"file_check"}B= ref $ref->{"file_check"} eq "CODE"
ABresult
0X0
100
111
636 $conf->{'set_times'} = $ref->{'set_times'}
637 if defined $ref->{'set_times'}
638 1 ▶ && ref $ref->{'set_times'} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if defined $ref->{"set_times"} and ref $ref->{"set_times"} eq "CODE" 0 1 1
Condition33%1/3
defined $ref->{"set_times"} and ref $ref->{"set_times"} eq "CODE"
!ll&&!rl&&r
100
MC/DC0%0/2
defined $ref->{"set_times"} and ref $ref->{"set_times"} eq "CODE"
defined $ref->{"set_times"} ref $ref->{"set_times"} eq "CODE"
Truth table33%1/3
A and B
A= defined $ref->{"set_times"}B= ref $ref->{"set_times"} eq "CODE"
ABresult
0X0
100
111
639 }
640  
641 1 bless $conf,$this;
642 }
643  
644 sub recursive($;$)
645 0 ▶{
Subroutine recursive: not called CC=4 SCAR=30.0
Pod documented
646 return
647 0 ▶ if @_ < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 1 0 0 0
648 0 my $this = shift;
649  
650 return
651 0 ▶ unless ref $this eq 'File::NCopy';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $this eq "File::NCopy" 0 0 0
652  
653 @_ ? $this->{'recursive'} = abs int shift
654 0 ▶ : $this->{'recursive'};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
655 }
656  
657 sub preserve($;$)
658 0 ▶{
Subroutine preserve: not called CC=4 SCAR=30.0
Pod documented
659 return
660 0 ▶ if @_ < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 1 0 0 0
661 0 my $this = shift;
662  
663 return
664 0 ▶ unless ref $this eq 'File::NCopy';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $this eq "File::NCopy" 0 0 0
665  
666 @_ ? $this->{'preserve'} = abs int shift
667 0 ▶ : $this->{'preserve'};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
668 }
669  
670 sub follow_links($;$)
671 0 ▶{
Subroutine follow_links: not called CC=4 SCAR=30.0
Pod documented
672 return
673 0 ▶ if @_ < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 1 0 0 0
674 0 my $this = shift;
675  
676 return
677 0 ▶ unless ref $this eq 'File::NCopy';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $this eq "File::NCopy" 0 0 0
678  
679 @_ ? $this->{'follow_links'} = abs int shift
680 0 ▶ : $this->{'follow_links'};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
681 }
682  
683 sub force_write($;$)
684 0 ▶{
Subroutine force_write: not called CC=4 SCAR=30.0
Pod documented
685 return
686 0 ▶ if @_ < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 1 0 0 0
687 0 my $this = shift;
688  
689 return
690 0 ▶ unless ref $this eq 'File::NCopy';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $this eq "File::NCopy" 0 0 0
691  
692 @_ ? $this->{'force_write'} = abs int shift
693 0 ▶ : $this->{'force_write'};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
694 }
695  
696 1 1;
↑ Summary