blib/lib/File/Remove.pm

filter: Statementstmt 81.8% 104 / 127 Branchbran 61.2% 38 / 62 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 81.8% 18 / 22 Podpod 66.6% 4 / 6 totaltotal 74.3% 165 / 222 CC 37 SCAR 29.5
CC
37
Cov
74%
remove
39.4
trash
6.0
_moveto
5.0
CRAP 19.1
↑ Summary
1 package File::Remove;
2  
3 8 ▶use 5.00503;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use vars qw{ $VERSION @ISA @EXPORT_OK };
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use vars qw{ $DEBUG $unlink $rmdir };
Subroutine BEGIN: called CC=1 SCAR=0.0
8 BEGIN {
9 8 ▶ $VERSION = '1.47_01';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 $VERSION = eval $VERSION;
11 8 @ISA = qw{ Exporter };
12 8 @EXPORT_OK = qw{ remove rm clear trash };
13 }
14  
15 # If we ever need a Mac::Glue object we will want to cache it.
16 8 my $glue;
17  
18 8 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use File::Glob ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use File::Spec 3.2701 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 8 ▶use Cwd 3.2701 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 sub expand (@) {
24 26 ▶ map { -e $_ ? $_ : File::Glob::bsd_glob($_) } @_;
Subroutine expand: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
-e $_ ? : 24 3 27
25 }
26  
27 # $debug variable must be set before loading File::Remove.
28 # Convert to a constant to allow debugging code to be pruned out.
29 8 ▶use constant DEBUG => !! $DEBUG;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # Are we on VMS?
32 # If so copy File::Path and assume VMS::Filespec is loaded
33 8 ▶use constant IS_VMS => !! ( $^O eq 'VMS' );
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 # Are we on Mac?
36 # If so we'll need to do some special trash work
37 8 ▶use constant IS_MAC => !! ( $^O eq 'darwin' );
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 # Are we on Win32?
40 # If so write permissions does not imply deletion permissions
41 8 ▶use constant IS_WIN32 => !! ( $^O =~ /^MSWin/ or $^O eq 'cygwin' );
Subroutine BEGIN: called CC=2 SCAR=7.6
Condition33%1/3
$^O =~ /^MSWin/ || $^O eq 'cygwin'
l!l&&r!l&&!r
008
MC/DC0%0/2
$^O =~ /^MSWin/ || $^O eq 'cygwin'
$^O =~ /^MSWin/ $^O eq 'cygwin'
Truth table33%1/3
A || B
A= $^O =~ /^MSWin/B= $^O eq 'cygwin'
ABresult
1X1
011
000
42  
43  
44  
45  
46  
47 #####################################################################
48 # Main Functions
49  
50 8 my @END_DELETE = ();
51  
52 sub clear (@) {
53 1 ▶ my @files = expand( @_ );
Subroutine clear: called CC=3 SCAR=11.7
Pod documented
54  
55 # Do the initial deletion
56 1 foreach my $file ( @files ) {
57 1 ▶ next unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 1 0 1
58 0 remove( \1, $file );
59 }
60  
61 # Delete again at END-time
62 1 push @END_DELETE, @files;
63 }
64  
65 END {
66 7 ▶ foreach my $file ( @END_DELETE ) {
Subroutine END: called CC=3 SCAR=11.2
67 1 ▶ next unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 1 1
68 1 remove( \1, $file );
69 }
70 }
71  
72 # acts like unlink would until given a directory as an argument, then
73 # it acts like rm -rf ;) unless the recursive arg is zero which it is by
74 # default
75 sub remove (@) {
76 24 ▶ my $recursive = (ref $_[0] eq 'SCALAR') ? shift : \0;
Subroutine remove: called CC=26 SCAR=36.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'SCALAR' ? : 15 9 24
77 24 my @files = expand(@_);
78  
79 # Iterate over the files
80 24 my @removes;
81 24 foreach my $path ( @files ) {
82 # need to check for symlink first
83 # could be pointing to nonexisting/non-readable destination
84 25 ▶ if ( -l $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-l $path) 2 23 25
85 2 print "link: $path\n" if DEBUG;
86 2 ▶ if ( $unlink ? $unlink->($path) : unlink($path) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($File::Remove::unlink ? &$File::Remove::unlink($path) : unlink($path)) 2 0 2
$File::Remove::unlink ? : 0 2 2
87 2 push @removes, $path;
88 }
89 2 next;
90 }
91 23 ▶ unless ( -e $path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $path) 0 23 23
92 0 print "missing: $path\n" if DEBUG;
93 0 push @removes, $path; # Say we deleted it
94 0 next;
95 }
96 23 my $can_delete;
97 23 ▶ if ( IS_VMS ) {
Branch: 3 branches
BranchTrueFalseTotal
if (do { -w $path }) { } 22 1 23
elsif ($< == 0) { } 0 1 1
elsif ((lstat $path)[4] == $<) { } 1 0 1
98 $can_delete = VMS::Filespec::candelete($path);
99 } elsif ( IS_WIN32 ) {
100 # Assume we can delete it for the moment
101 $can_delete = 1;
102 } elsif ( -w $path ) {
103 # We have write permissions already
104 22 $can_delete = 1;
105 } elsif ( $< == 0 ) {
106 # Unixy and root
107 0 $can_delete = 1;
108 } elsif ( (lstat($path))[4] == $< ) {
109 # I own the file
110 1 $can_delete = 1;
111 } else {
112 # I don't think we can delete it
113 0 $can_delete = 0;
114 }
115 23 ▶ unless ( $can_delete ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($can_delete) 0 23 23
116 0 print "nowrite: $path\n" if DEBUG;
117 0 next;
118 }
119  
120 23 ▶ if ( -f $path ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $path) { } 1 22 23
elsif (-d $path) { } 22 0 22
121 1 print "file: $path\n" if DEBUG;
122 1 ▶ unless ( -w $path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-w $path) 1 0 1
123 # Make the file writable (implementation from File::Path)
124 1 ▶ (undef, undef, my $rp) = lstat $path or next;
Branch: 1 branch
BranchTrueFalseTotal
my(undef, undef, $rp) = lstat $path or next 0 1 1
125 1 $rp &= 07777; # Don't forget setuid, setgid, sticky bits
126 1 $rp |= 0600; # Turn on user read/write
127 1 chmod $rp, $path;
128 }
129 1 ▶ if ( $unlink ? $unlink->($path) : unlink($path) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($File::Remove::unlink ? &$File::Remove::unlink($path) : unlink($path)) 1 0 1
$File::Remove::unlink ? : 0 1 1
130 # Failed to delete the file
131 1 ▶ next if -e $path;
Branch: 1 branch
BranchTrueFalseTotal
if -e $path 0 1 1
132 1 push @removes, $path;
133 }
134  
135 } elsif ( -d $path ) {
136 22 print "dir: $path\n" if DEBUG;
137 22 my $dir = File::Spec->canonpath($path);
138  
139 # Do we need to move our cwd out of the location
140 # we are planning to delete?
141 22 my $chdir = _moveto($dir);
142 22 ▶ if ( length $chdir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $chdir) 1 21 22
143 1 ▶ chdir($chdir) or next;
Branch: 1 branch
BranchTrueFalseTotal
chdir $chdir or next 0 1 1
144 }
145  
146 22 ▶ if ( $$recursive ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$recursive) { } 14 8 22
147 14 ▶ if ( File::Path::rmtree( [ $dir ], DEBUG, 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (File::Path::rmtree([$dir], !!0, 0)) 14 0 14
148 # Failed to delete the directory
149 14 ▶ next if -e $path;
Branch: 1 branch
BranchTrueFalseTotal
if -e $path 0 14 14
150 14 push @removes, $path;
151 }
152  
153 } else {
154 8 my ($save_mode) = (stat $dir)[2];
155 8 chmod $save_mode & 0777, $dir; # just in case we cannot remove it.
156 8 ▶ if ( $rmdir ? $rmdir->($dir) : rmdir($dir) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($File::Remove::rmdir ? &$File::Remove::rmdir($dir) : rmdir $dir) 8 0 8
$File::Remove::rmdir ? : 0 8 8
157 # Failed to delete the directory
158 8 ▶ next if -e $path;
Branch: 1 branch
BranchTrueFalseTotal
if -e $path 0 8 8
159 8 push @removes, $path;
160 }
161 }
162  
163 } else {
164 0 print "???: $path\n" if DEBUG;
165 }
166 }
167  
168 24 return @removes;
169 }
170  
171 sub rm (@) {
172 0 ▶ goto &remove;
Subroutine rm: not called CC=1 SCAR=6.9
Pod documented
173 }
174  
175 sub trash (@) {
176 0 ▶ local $unlink = $unlink;
Subroutine trash: not called CC=2 SCAR=17.9
Pod documented
177 0 local $rmdir = $rmdir;
178  
179 0 ▶ if ( ref $_[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 0 0
180 0 my %options = %{+shift @_};
181 0 $unlink = $options{unlink};
182 0 $rmdir = $options{rmdir};
183  
184 } elsif ( IS_WIN32 ) {
185 local $@;
186 eval 'use Win32::FileOp ();';
187 die "Can't load Win32::FileOp to support the Recycle Bin: \$@ = $@" if length $@;
188 $unlink = \&Win32::FileOp::Recycle;
189 $rmdir = \&Win32::FileOp::Recycle;
190  
191 } elsif ( IS_MAC ) {
192 unless ( $glue ) {
193 local $@;
194 eval 'use Mac::Glue ();';
195 die "Can't load Mac::Glue::Finder to support the Trash Can: \$@ = $@" if length $@;
196 $glue = Mac::Glue->new('Finder');
197 }
198 my $code = sub {
199 my @files = map {
200 0 ▶ Mac::Glue::param_type(
Subroutine __ANON__: not called CC=1 SCAR=6.9
201 Mac::Glue::typeAlias() => $_
202 )
203 } @_;
204 0 $glue->delete(\@files);
205 };
206 $unlink = $code;
207 $rmdir = $code;
208 } else {
209 0 die "Support for trash() on platform '$^O' not available at this time.\n";
210 }
211  
212 0 remove(@_);
213 }
214  
215 sub undelete (@) {
216 0 ▶ goto &trash;
Subroutine undelete: not called CC=1 SCAR=6.9
Pod undocumented
217 }
218  
219  
220  
221  
222  
223 ######################################################################
224 # Support Functions
225  
226 # Do we need to move to a different directory to delete a directory,
227 # and if so which.
228 sub _moveto {
229 # Do everything in absolute terms
230 22 ▶ my $cwd = Cwd::abs_path( Cwd::cwd() );
Subroutine _moveto: called CC=5 SCAR=16.2
231 22 my $remove = Cwd::abs_path( File::Spec->rel2abs(shift) );
232  
233 # If we are on a different volume we don't need to move
234 22 my ( $cv, $cd ) = File::Spec->splitpath( $cwd, 1 );
235 22 my ( $rv, $rd ) = File::Spec->splitpath( $remove, 1 );
236 22 ▶ return '' unless $cv eq $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless $cv eq $rv 0 22 22
237  
238 # If we have to move, it's to one level above the deletion
239 22 my @cd = File::Spec->splitdir($cd);
240 22 my @rd = File::Spec->splitdir($rd);
241 22 pop @rd;
242  
243 # Is the current directory inside of the moveto directory?
244 22 ▶ unless ( @cd > @rd ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@cd > @rd) 21 1 22
245 21 return '';
246 }
247 1 foreach ( 0 .. $#rd ) {
248 8 ▶ $cd[$_] eq $rd[$_] or return '';
Branch: 1 branch
BranchTrueFalseTotal
$cd[$_] eq $rd[$_] or return "" 0 8 8
249 }
250  
251 # Confirmed, the current working dir is in the removal dir
252 1 return File::Spec->catpath(
253 $rv,
254 File::Spec->catdir(@rd),
255 ''
256 );
257 }
258  
259 8 1;
260  
261 __END__
↑ Summary