blib/lib/File/Remove.pm

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