Backup/Copy.pm

filter: Statementstmt 9.7% 17 / 175 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 38 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 20.0% 4 / 20 Podpod 0.0% 0 / 7 totaltotal 6.3% 21 / 329 CC 48 SCAR 39.7
CC
48
Cov
6%
_copyOneFile
156.0
remove
90.0
_copyDirectoryRecursive
42.0
CRAP 52.8
↑ Summary Backup.pm »
1 # -*-perl-*-
2 # Creation date: 2003-04-12 22:43:55
3 # Authors: Don
4 # Change log:
5 # $Id: Copy.pm,v 1.10 2004/03/21 04:56:19 don Exp $
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 { package File::Rotate::Backup::Copy;
10  
11 1 ▶ use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 $VERSION = do { my @r=(q$Revision: 1.10 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r };
13  
14 1 ▶ use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶ use Fcntl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 0 ▶ sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod undocumented
18 0 my ($proto, $params) = @_;
19 0 ▶ $params = {} unless ref($params) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $params eq "HASH" 0 0 0
20 0 ▶ my $self = bless { _params => $params }, ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
21 0 return $self;
22 }
23  
24 0 ▶ sub copy {
Subroutine copy: not called CC=4 SCAR=30.0
Pod undocumented
25 0 my ($self, $src, $dst) = @_;
26
27 0 ▶ if (-l $src or -f $src) {
Branch: 2 branches
BranchTrueFalseTotal
if (-l $src or -f $src) { } 0 0 0
elsif (-d $src) { } 0 0 0
Condition0%0/3
-l $src or -f $src
l!l&&r!l&&!r
000
MC/DC0%0/2
-l $src or -f $src
-l $src -f $src
Truth table0%0/3
A or B
A= -l $srcB= -f $src
ABresult
1X1
011
000
28 0 return $self->_copySymlinkOrFile($src, $dst);
29 } elsif (-d $src) {
30 0 return $self->_copyDirectoryRecursive($src, $dst);
31 }
32 }
33  
34 0 ▶ sub _copyDirectoryRecursive {
Subroutine _copyDirectoryRecursive: not called CC=6 SCAR=37.4
35 0 my ($self, $src, $dst) = @_;
36  
37 0 my ($src_vol, $src_dirs, $src_file) = File::Spec->splitpath($src);
38 0 my ($dst_vol, $dst_dirs, $dst_file) = File::Spec->splitpath($dst);
39  
40 0 ▶ if (-e $dst and -d $dst) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $dst and -d $dst) 0 0 0
Condition0%0/3
-e $dst and -d $dst
!ll&&!rl&&r
000
MC/DC0%0/2
-e $dst and -d $dst
-e $dst -d $dst
Truth table0%0/3
A and B
A= -e $dstB= -d $dst
ABresult
0X0
100
111
41 # if dst is a directory, add file name to end of path
42 0 my $dir = File::Spec->catdir($dst_dirs, $dst_file);
43 0 $dst = File::Spec->catpath($dst_vol, $dir, $src_file);
44 }
45  
46 0 $self->_copyOneFile($src, $dst);
47  
48 0 my $cur_dir = File::Spec->curdir;
49 0 my $parent_dir = File::Spec->updir;
50 0 local(*DIR);
51  
52 0 ▶ opendir(DIR, $src) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $src or return undef 0 0 0
53 0 ▶ my @files = grep { $_ ne $cur_dir and $_ ne $parent_dir } readdir DIR;
Condition0%0/3
$_ ne $cur_dir && $_ ne $parent_dir
!ll&&!rl&&r
000
MC/DC0%0/2
$_ ne $cur_dir && $_ ne $parent_dir
$_ ne $cur_dir $_ ne $parent_dir
Truth table0%0/3
A && B
A= $_ ne $cur_dirB= $_ ne $parent_dir
ABresult
0X0
100
111
54 0 closedir DIR;
55  
56 0 foreach my $file (@files) {
57 0 my $new_src_dir = File::Spec->catdir($src_dirs, $src_file);
58 0 my $src_path = File::Spec->catpath($src_vol, $new_src_dir, $file);
59 0 $self->copy($src_path, $dst);
60 }
61 }
62  
63 0 ▶ sub _copySymlinkOrFile {
Subroutine _copySymlinkOrFile: not called CC=3 SCAR=24.8
64 0 my ($self, $src, $dst) = @_;
65 0 my ($src_vol, $src_dirs, $src_file) = File::Spec->splitpath($src);
66 0 my ($dst_vol, $dst_dirs, $dst_file) = File::Spec->splitpath($dst);
67  
68 0 ▶ if (-e $dst and -d $dst) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $dst and -d $dst) 0 0 0
Condition0%0/3
-e $dst and -d $dst
!ll&&!rl&&r
000
MC/DC0%0/2
-e $dst and -d $dst
-e $dst -d $dst
Truth table0%0/3
A and B
A= -e $dstB= -d $dst
ABresult
0X0
100
111
69 # if dst is a directory, add file name to end of path
70 0 my $dir = File::Spec->catdir($dst_dirs, $dst_file);
71 0 $dst = File::Spec->catpath($dst_vol, $dir, $src_file);
72 }
73
74 # FIXME: should handle $dst being a symlink
75  
76 0 $self->debugPrint(5, "src_path is $src_dirs, $src_file => $src\n");
77 0 $self->debugPrint(5, "dst_path is $dst_dirs, $dst_file => $dst\n");
78  
79 0 return $self->_copyOneFile($src, $dst);
80 }
81  
82 0 ▶ sub _copyOneFile {
Subroutine _copyOneFile: not called CC=12 SCAR=50.5
83 0 my ($self, $src_path, $dst_path) = @_;
84  
85 0 ▶ if ($self->_isSameFile($src_path, $dst_path)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isSameFile($src_path, $dst_path)) 0 0 0
86 0 return 0;
87 }
88  
89 # find out what kind of file it is
90 0 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
91 $atime,$mtime,$ctime,$blksize,$blocks)
92 = stat($src_path);
93  
94 0 my $permissions = $mode & 07777;
95  
96 0 $self->debugPrint(1, "$src_path ==> $dst_path\n");
97 0 ▶ if (-l $src_path) {
Branch: 7 branches
BranchTrueFalseTotal
if (-l $src_path) { } 0 0 0
elsif (-f $src_path) { } 0 0 0
elsif (-d _) { } 0 0 0
elsif (-p _) { } 0 0 0
elsif (-S _) { } 0 0 0
elsif (-b _) { } 0 0 0
elsif (-c _) { } 0 0 0
98 # symlink
99 0 $self->debugPrint(9, "$src_path is a symbolic link\n");
100 0 my $link_content = readlink $src_path;
101 0 ▶ return undef unless symlink $link_content, $dst_path;
Branch: 1 branch
BranchTrueFalseTotal
unless symlink $link_content, $dst_path 0 0 0
102 # FIXME: set up owner and group of symlink
103 } elsif (-f $src_path) {
104 # need the full path here instead of the _ filehandle
105 # because the -l does an lstat
106  
107 # plain file
108 0 my $size = -s _;
109 0 $self->debugPrint(9, "$src_path is a plain file - $size bytes\n");
110 0 ▶ $self->_copyPlainFile($src_path, $dst_path) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_copyPlainFile($src_path, $dst_path) or return undef 0 0 0
111 0 $self->_fixOwnerPermissionsTimestamp($dst_path);
112 } elsif (-d _) {
113 # directory
114 0 $self->debugPrint(9, "$src_path is a directory\n");
115 0 ▶ return undef unless mkdir $dst_path, 0777;
Branch: 1 branch
BranchTrueFalseTotal
unless mkdir $dst_path, 511 0 0 0
116 0 $self->_fixOwnerPermissionsTimestamp($dst_path);
117 } elsif (-p _) {
118 # don't copy pipes, sockets, and other special files for now
119
120 # named pipe
121 0 $self->debugPrint(9, "$src_path is a named pipe\n");
122 } elsif (-S _) {
123 # socket
124 0 $self->debugPrint(9, "$src_path is a socket\n");
125 } elsif (-b _) {
126 # block special file
127 0 $self->debugPrint(9, "$src_path is a block special file\n");
128 } elsif (-c _) {
129 # character special file
130 0 $self->debugPrint(9,"$src_path is a character special file\n");
131 }
132  
133 0 $self->debugPrint(9, sprintf("$src_path has permissions %o\n", $permissions));
134  
135 0 return 1;
136 }
137  
138 0 ▶ sub _isSameFile {
Subroutine _isSameFile: not called CC=5 SCAR=34.0
139 0 my ($self, $src_file, $dst_file) = @_;
140 0 my ($src_dev, $src_ino);
141 0 my ($dst_dev, $dst_ino);
142  
143 0 ▶ if (-l $src_file or -l $dst_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-l $src_file or -l $dst_file) { } 0 0 0
Condition0%0/3
-l $src_file or -l $dst_file
l!l&&r!l&&!r
000
MC/DC0%0/2
-l $src_file or -l $dst_file
-l $src_file -l $dst_file
Truth table0%0/3
A or B
A= -l $src_fileB= -l $dst_file
ABresult
1X1
011
000
144 0 ($src_dev, $src_ino) = (lstat($src_file))[0,1];
145 0 ($dst_dev, $dst_ino) = (lstat($dst_file))[0,1];
146 } else {
147 0 ($src_dev, $src_ino) = (stat($src_file))[0,1];
148 0 ($dst_dev, $dst_ino) = (stat($dst_file))[0,1];
149 }
150  
151 0 ▶ if ($src_dev == $dst_dev and $src_ino == $dst_ino) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src_dev == $dst_dev and $src_ino == $dst_ino) 0 0 0
Condition0%0/3
$src_dev == $dst_dev and $src_ino == $dst_ino
!ll&&!rl&&r
000
MC/DC0%0/2
$src_dev == $dst_dev and $src_ino == $dst_ino
$src_dev == $dst_dev $src_ino == $dst_ino
Truth table0%0/3
A and B
A= $src_dev == $dst_devB= $src_ino == $dst_ino
ABresult
0X0
100
111
152 0 return 1;
153 }
154  
155 0 return 0;
156 }
157
158 0 ▶ sub _fixOwnerPermissionsTimestamp {
Subroutine _fixOwnerPermissionsTimestamp: not called CC=1 SCAR=6.9
159 0 my ($self, $dst_file) = @_;
160 0 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
161 $atime,$mtime,$ctime,$blksize,$blocks)
162 = stat(_);
163  
164 0 my $permissions = $mode & 07777;
165  
166 0 chown $uid, $gid, $dst_file;
167 0 chmod $permissions, $dst_file;
168 0 utime $atime, $mtime, $dst_file;
169 }
170  
171 0 ▶ sub _copyPlainFile {
Subroutine _copyPlainFile: not called CC=4 SCAR=30.0
172 0 my ($self, $src_path, $dst_path) = @_;
173
174 0 local(*IN);
175 0 local(*OUT);
176 0 ▶ open(IN, '<' . $src_path) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open IN, '<' . $src_path or return undef 0 0 0
177 0 ▶ unless (open(OUT, '>' . $dst_path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open OUT, '>' . $dst_path) 0 0 0
178 0 close IN;
179 0 return undef;
180 }
181  
182 # just in case this ever runs on windoze
183 0 binmode IN, ':raw';
184 0 binmode OUT, ':raw';
185
186 0 my $buf;
187 0 ▶ while (read(IN, $buf, 1024)) {
Branch: 1 branch
BranchTrueFalseTotal
if (read IN, $buf, 1024) 0 0 0
188 0 print OUT $buf;
189 }
190 0 close IN;
191 0 close OUT;
192  
193 0 return 1;
194 }
195  
196 0 ▶ sub remove {
Subroutine remove: not called CC=9 SCAR=45.0
Pod undocumented
197 0 my ($self, $victim) = @_;
198  
199 0 $self->debugPrint(9, "remove() - passed $victim\n");
200  
201 0 ▶ if (not -l $victim and -d $victim) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -l $victim and -d $victim) { } 0 0 0
Condition0%0/3
not -l $victim and -d $victim
!ll&&!rl&&r
000
MC/DC0%0/2
not -l $victim and -d $victim
not -l $victim -d $victim
Truth table0%0/3
not(A) and B
A= not -l $victimB= -d $victim
ABresult
0X0
100
111
202 0 return $self->_removeDirectoryRecursive($victim);
203 } else {
204 0 $self->debugPrint(1, "Removing $victim\n");
205 0 my $params = $self->_getParams;
206 0 ▶ if ($$params{use_flock}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{'use_flock'}) { } 0 0 0
207 0 local(*FILE);
208 0 open(FILE, '+<' . $victim);
209 0 ▶ unless (CORE::flock(FILE, &Fcntl::LOCK_EX() | &Fcntl::LOCK_NB)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock FILE, &Fcntl::LOCK_EX() | &Fcntl::LOCK_NB) 0 0 0
210 # can't get lock
211 0 close FILE;
212 0 $self->debugPrint(1, "Could not get lock on $victim -- not removing\n");
213 0 return undef;
214 }
215 0 my $rv = unlink $victim;
216 0 CORE::flock(FILE, &Fcntl::LOCK_UN);
217 0 close FILE;
218 0 ▶ if (not $rv and $$params{use_rm}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rv and $params->{"use_rm"}) 0 0 0
Condition0%0/3
not $rv and $params->{"use_rm"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $rv and $params->{"use_rm"}
not $rv $params->{"use_rm"}
Truth table0%0/3
not(A) and B
A= not $rvB= $params->{"use_rm"}
ABresult
0X0
100
111
219 # added for v0.08
220 0 $self->debugPrint(1, "unlink() failed -- using /bin/rm\n");
221 0 $rv = not system("/bin/rm", "-f", $victim);
222 }
223 0 return $rv;
224 } else {
225 0 my $rv = unlink $victim;
226 0 ▶ if (not $rv and $$params{use_rm}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rv and $params->{"use_rm"}) 0 0 0
Condition0%0/3
not $rv and $params->{"use_rm"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $rv and $params->{"use_rm"}
not $rv $params->{"use_rm"}
Truth table0%0/3
not(A) and B
A= not $rvB= $params->{"use_rm"}
ABresult
0X0
100
111
227 # added for v0.08
228 0 $self->debugPrint(1, "unlink() failed -- using /bin/rm\n");
229 0 $rv = not system("/bin/rm", "-f", $victim);
230 }
231 0 return $rv;
232 }
233 }
234 }
235  
236 0 ▶ sub _removeDirectoryRecursive {
Subroutine _removeDirectoryRecursive: not called CC=4 SCAR=30.0
237 0 my ($self, $dir) = @_;
238  
239 0 $self->debugPrint(9, "_removeDirectoryRecursive() - passed $dir\n");
240
241 0 local(*DIR);
242 0 my $cur_dir = File::Spec->curdir;
243 0 my $parent_dir = File::Spec->updir;
244  
245 0 ▶ opendir(DIR, $dir) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $dir or return undef 0 0 0
246 0 ▶ my @files = grep { $_ ne $cur_dir and $_ ne $parent_dir } readdir DIR;
Condition0%0/3
$_ ne $cur_dir && $_ ne $parent_dir
!ll&&!rl&&r
000
MC/DC0%0/2
$_ ne $cur_dir && $_ ne $parent_dir
$_ ne $cur_dir $_ ne $parent_dir
Truth table0%0/3
A && B
A= $_ ne $cur_dirB= $_ ne $parent_dir
ABresult
0X0
100
111
247 0 closedir DIR;
248  
249 0 my ($vol, $dirs, $dir_file) = File::Spec->splitpath($dir);
250 0 foreach my $file (@files) {
251 0 my $victim_dir = File::Spec->catdir($dirs, $dir_file);
252 0 my $victim_path = File::Spec->catpath($vol, $victim_dir, $file);
253 0 $self->debugPrint(9, "Trying to remove $victim_path\n");
254 0 $self->remove($victim_path);
255 }
256  
257 0 $self->debugPrint(1, "Removing directory $dir\n");
258 0 rmdir $dir;
259  
260 0 return 1;
261 }
262  
263 0 ▶ sub move {
Subroutine move: not called CC=1 SCAR=6.9
Pod undocumented
264 0 my ($self, $src, $dst) = @_;
265  
266 # FIXME: implement
267 }
268  
269 # expects full path for $src and $dst
270 0 ▶ sub _move {
Subroutine _move: not called CC=4 SCAR=30.0
271 0 my ($self, $src, $dst) = @_;
272 # HERE
273  
274 0 my ($src_dev, $src_ino);
275 0 my ($dst_dev, $dst_ino);
276  
277 0 ▶ if (-l $src or -l $dst) {
Branch: 1 branch
BranchTrueFalseTotal
if (-l $src or -l $dst) { } 0 0 0
Condition0%0/3
-l $src or -l $dst
l!l&&r!l&&!r
000
MC/DC0%0/2
-l $src or -l $dst
-l $src -l $dst
Truth table0%0/3
A or B
A= -l $srcB= -l $dst
ABresult
1X1
011
000
278 0 ($src_dev, $src_ino) = (lstat($src))[0,1];
279 0 ($dst_dev, $dst_ino) = (lstat($dst))[0,1];
280 } else {
281 0 ($src_dev, $src_ino) = (stat($src))[0,1];
282 0 ($dst_dev, $dst_ino) = (stat($dst))[0,1];
283 }
284  
285 0 ▶ if ($src_dev == $dst_dev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src_dev == $dst_dev) { } 0 0 0
286 # same filesystem, so we can just do a rename
287 0 rename $src, $dst;
288 } else {
289 # HERE
290 }
291 }
292  
293 0 ▶ sub debugOn {
Subroutine debugOn: not called CC=1 SCAR=6.9
Pod undocumented
294 0 my ($self, $fh, $level) = @_;
295 0 $$self{_debug} = 1;
296 0 $$self{_debug_level} = $level;
297 0 $$self{_debug_fh} = $fh;
298 }
299  
300 0 ▶ sub debugOff {
Subroutine debugOff: not called CC=1 SCAR=6.9
Pod undocumented
301 0 my ($self) = @_;
302 0 undef $$self{_debug};
303 0 undef $$self{_debug_fh};
304 }
305  
306 0 ▶ sub debugPrint {
Subroutine debugPrint: not called CC=3 SCAR=24.8
Pod undocumented
307 0 my ($self, $level, $str) = @_;
308 0 ▶ return undef unless $$self{_debug};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_debug"} 0 0 0
309 0 ▶ return undef unless $$self{_debug_level} >= $level;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_debug_level"} >= $level 0 0 0
310
311 0 my $fh = $$self{_debug_fh};
312 0 print $fh $str;
313 }
314  
315 0 ▶ sub _getParams {
Subroutine _getParams: not called CC=2 SCAR=17.9
316 0 my ($self) = @_;
317 0 ▶ return $$self{_params} || {};
Condition0%0/2
$self->{'_params'} || {}
l!l
00
MC/DC0%0/1
$self->{'_params'} || {}
$self->{'_params'}
Truth table0%0/2
A
A= $self->{'_params'}
Aresult
11
00
318 }
319 }
320  
321 1 1;
322  
323 __END__
↑ Summary Backup.pm »