| 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'
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'
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 70 | 3 | ▶ | next unless -e $file->[1]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 80 | 29 | ▶ | my $opts = (ref $_[0] eq 'HASH') ? shift : { glob => 1 }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 89 | 2 | print "link: $path\n" if DEBUG; | |||||||||||||||||||
| 90 | 2 | ▶ | if ( $unlink ? $unlink->($path) : unlink($path) ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 91 | 2 | push @removes, $path; | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | 2 | next; | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | 28 | ▶ | unless ( -e $path ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 120 | 0 | print "nowrite: $path\n" if DEBUG; | |||||||||||||||||||
| 121 | 0 | next; | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 25 | ▶ | if ( -f $path ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 125 | 1 | print "file: $path\n" if DEBUG; | |||||||||||||||||||
| 126 | 1 | ▶ | unless ( -w $path ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 127 | # Make the file writable (implementation from File::Path) | ||||||||||||||||||||
| 128 | 1 | ▶ | (undef, undef, my $rp) = lstat $path or next; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 134 | # Failed to delete the file | ||||||||||||||||||||
| 135 | 1 | ▶ | next if -e $path; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 147 | 1 | ▶ | chdir($chdir) or next; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 24 | ▶ | if ( $$recursive ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 151 | 16 | ▶ | if ( File::Path::rmtree( [ $dir ], DEBUG, 0 ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 152 | # Failed to delete the directory | ||||||||||||||||||||
| 153 | 16 | ▶ | next if -e $path; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 161 | # Failed to delete the directory | ||||||||||||||||||||
| 162 | 8 | ▶ | next if -e $path; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 260 | 24 | return ''; | |||||||||||||||||||
| 261 | } | ||||||||||||||||||||
| 262 | 4 | foreach ( 0 .. $#rd ) { | |||||||||||||||||||
| 263 | 30 | ▶ | $cd[$_] eq $rd[$_] or return ''; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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__ | ||||||||||||||||||||