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