line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package File::Remove; |
2
|
|
|
|
|
|
|
$File::Remove::VERSION = '1.61'; |
3
|
11
|
|
|
11
|
|
595829
|
use 5.008; |
|
11
|
|
|
|
|
118
|
|
4
|
11
|
|
|
11
|
|
48
|
use strict; |
|
11
|
|
|
|
|
20
|
|
|
11
|
|
|
|
|
224
|
|
5
|
11
|
|
|
11
|
|
48
|
use warnings; |
|
11
|
|
|
|
|
17
|
|
|
11
|
|
|
|
|
334
|
|
6
|
|
|
|
|
|
|
|
7
|
11
|
|
|
11
|
|
64
|
use vars qw{ @ISA @EXPORT_OK }; |
|
11
|
|
|
|
|
16
|
|
|
11
|
|
|
|
|
570
|
|
8
|
11
|
|
|
11
|
|
51
|
use vars qw{ $DEBUG $unlink $rmdir }; |
|
11
|
|
|
|
|
17
|
|
|
11
|
|
|
|
|
644
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
BEGIN |
11
|
|
|
|
|
|
|
{ |
12
|
11
|
|
|
11
|
|
164
|
@ISA = qw{ Exporter }; |
13
|
11
|
|
|
|
|
246
|
@EXPORT_OK = qw{ remove rm clear trash }; |
14
|
|
|
|
|
|
|
} |
15
|
|
|
|
|
|
|
|
16
|
11
|
|
|
11
|
|
71
|
use File::Path (); |
|
11
|
|
|
|
|
20
|
|
|
11
|
|
|
|
|
168
|
|
17
|
11
|
|
|
11
|
|
53
|
use File::Glob (); |
|
11
|
|
|
|
|
24
|
|
|
11
|
|
|
|
|
258
|
|
18
|
11
|
|
|
11
|
|
50
|
use File::Spec 3.29 (); |
|
11
|
|
|
|
|
215
|
|
|
11
|
|
|
|
|
256
|
|
19
|
11
|
|
|
11
|
|
52
|
use Cwd 3.29 (); |
|
11
|
|
|
|
|
180
|
|
|
11
|
|
|
|
|
313
|
|
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
|
11
|
|
|
11
|
|
48
|
use constant DEBUG => !!$DEBUG; |
|
11
|
|
|
|
|
16
|
|
|
11
|
|
|
|
|
1062
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# Are we on VMS? |
26
|
|
|
|
|
|
|
# If so copy File::Path and assume VMS::Filespec is loaded |
27
|
11
|
|
|
11
|
|
60
|
use constant IS_VMS => !!( $^O eq 'VMS' ); |
|
11
|
|
|
|
|
17
|
|
|
11
|
|
|
|
|
568
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# Are we on Mac? |
30
|
|
|
|
|
|
|
# If so we'll need to do some special trash work |
31
|
11
|
|
|
11
|
|
51
|
use constant IS_MAC => !!( $^O eq 'darwin' ); |
|
11
|
|
|
|
|
16
|
|
|
11
|
|
|
|
|
882
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# Are we on Win32? |
34
|
|
|
|
|
|
|
# If so write permissions does not imply deletion permissions |
35
|
11
|
|
33
|
11
|
|
66
|
use constant IS_WIN32 => !!( $^O =~ /^MSWin/ or $^O eq 'cygwin' ); |
|
11
|
|
|
|
|
33
|
|
|
11
|
|
|
|
|
13360
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# If we ever need a Mac::Glue object we will want to cache it. |
38
|
|
|
|
|
|
|
my $glue; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
##################################################################### |
41
|
|
|
|
|
|
|
# Main Functions |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
my @CLEANUP = (); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
## no critic |
46
|
|
|
|
|
|
|
sub clear (@) |
47
|
|
|
|
|
|
|
{ |
48
|
4
|
|
|
4
|
1
|
5128
|
my @files = expand(@_); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# Do the initial deletion |
51
|
4
|
|
|
|
|
39
|
foreach my $file (@files) |
52
|
|
|
|
|
|
|
{ |
53
|
4
|
50
|
|
|
|
47
|
next unless -e $file; |
54
|
0
|
|
|
|
|
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
|
|
|
|
|
13
|
push @CLEANUP, map { [ $$, $_ ] } @files; |
|
4
|
|
|
|
|
105
|
|
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
## use critic |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
END |
66
|
|
|
|
|
|
|
{ |
67
|
10
|
|
|
10
|
|
5218709
|
foreach my $file (@CLEANUP) |
68
|
|
|
|
|
|
|
{ |
69
|
4
|
100
|
|
|
|
61
|
next unless $file->[0] == $$; |
70
|
3
|
50
|
|
|
|
111
|
next unless -e $file->[1]; |
71
|
3
|
|
|
|
|
45
|
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
|
|
|
|
|
|
|
{ |
81
|
|
|
|
|
|
|
## use critic |
82
|
29
|
100
|
|
29
|
1
|
53563
|
my $recursive = ( ref $_[0] eq 'SCALAR' ) ? shift : \0; |
83
|
29
|
100
|
|
|
|
149
|
my $opts = ( ref $_[0] eq 'HASH' ) ? shift : { glob => 1 }; |
84
|
29
|
|
|
|
|
187
|
my @files = _expand_with_opts( $opts, @_ ); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# Iterate over the files |
87
|
29
|
|
|
|
|
89
|
my @removes; |
88
|
29
|
|
|
|
|
127
|
foreach my $path (@files) |
89
|
|
|
|
|
|
|
{ |
90
|
|
|
|
|
|
|
# need to check for symlink first |
91
|
|
|
|
|
|
|
# could be pointing to nonexisting/non-readable destination |
92
|
30
|
100
|
|
|
|
333
|
if ( -l $path ) |
93
|
|
|
|
|
|
|
{ |
94
|
2
|
|
|
|
|
4
|
print "link: $path\n" if DEBUG; |
95
|
2
|
50
|
|
|
|
64
|
if ( $unlink ? $unlink->($path) : unlink($path) ) |
|
|
50
|
|
|
|
|
|
96
|
|
|
|
|
|
|
{ |
97
|
2
|
|
|
|
|
6
|
push @removes, $path; |
98
|
|
|
|
|
|
|
} |
99
|
2
|
|
|
|
|
5
|
next; |
100
|
|
|
|
|
|
|
} |
101
|
28
|
100
|
|
|
|
291
|
unless ( -e $path ) |
102
|
|
|
|
|
|
|
{ |
103
|
3
|
|
|
|
|
6
|
print "missing: $path\n" if DEBUG; |
104
|
3
|
|
|
|
|
9
|
push @removes, $path; # Say we deleted it |
105
|
3
|
|
|
|
|
10
|
next; |
106
|
|
|
|
|
|
|
} |
107
|
25
|
|
|
|
|
73
|
my $can_delete; |
108
|
25
|
50
|
|
|
|
290
|
if (IS_VMS) |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
0
|
elsif ( -w $path ) |
118
|
|
|
|
|
|
|
{ |
119
|
|
|
|
|
|
|
# We have write permissions already |
120
|
25
|
|
|
|
|
70
|
$can_delete = 1; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
elsif ( $< == 0 ) |
123
|
|
|
|
|
|
|
{ |
124
|
|
|
|
|
|
|
# Unixy and root |
125
|
0
|
|
|
|
|
0
|
$can_delete = 1; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
elsif ( ( lstat($path) )[4] == $< ) |
128
|
|
|
|
|
|
|
{ |
129
|
|
|
|
|
|
|
# I own the file |
130
|
0
|
|
|
|
|
0
|
$can_delete = 1; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
else |
133
|
|
|
|
|
|
|
{ |
134
|
|
|
|
|
|
|
# I don't think we can delete it |
135
|
0
|
|
|
|
|
0
|
$can_delete = 0; |
136
|
|
|
|
|
|
|
} |
137
|
25
|
50
|
|
|
|
102
|
unless ($can_delete) |
138
|
|
|
|
|
|
|
{ |
139
|
0
|
|
|
|
|
0
|
print "nowrite: $path\n" if DEBUG; |
140
|
0
|
|
|
|
|
0
|
next; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
25
|
100
|
|
|
|
423
|
if ( -f $path ) |
|
|
50
|
|
|
|
|
|
144
|
|
|
|
|
|
|
{ |
145
|
1
|
|
|
|
|
3
|
print "file: $path\n" if DEBUG; |
146
|
1
|
50
|
|
|
|
11
|
unless ( -w $path ) |
147
|
|
|
|
|
|
|
{ |
148
|
|
|
|
|
|
|
# Make the file writable (implementation from File::Path) |
149
|
0
|
0
|
|
|
|
0
|
( undef, undef, my $rp ) = lstat $path or next; |
150
|
0
|
|
|
|
|
0
|
$rp &= 07777; # Don't forget setuid, setgid, sticky bits |
151
|
0
|
|
|
|
|
0
|
$rp |= 0600; # Turn on user read/write |
152
|
0
|
|
|
|
|
0
|
chmod $rp, $path; |
153
|
|
|
|
|
|
|
} |
154
|
1
|
50
|
|
|
|
67
|
if ( $unlink ? $unlink->($path) : unlink($path) ) |
|
|
50
|
|
|
|
|
|
155
|
|
|
|
|
|
|
{ |
156
|
|
|
|
|
|
|
# Failed to delete the file |
157
|
1
|
50
|
|
|
|
19
|
next if -e $path; |
158
|
1
|
|
|
|
|
5
|
push @removes, $path; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
elsif ( -d $path ) |
163
|
|
|
|
|
|
|
{ |
164
|
24
|
|
|
|
|
57
|
print "dir: $path\n" if DEBUG; |
165
|
24
|
|
|
|
|
324
|
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
|
|
|
|
|
140
|
my $chdir = _moveto($dir); |
170
|
24
|
100
|
|
|
|
154
|
if ( length $chdir ) |
171
|
|
|
|
|
|
|
{ |
172
|
1
|
50
|
|
|
|
39
|
chdir($chdir) or next; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
24
|
100
|
|
|
|
126
|
if ($$recursive) |
176
|
|
|
|
|
|
|
{ |
177
|
16
|
50
|
|
|
|
8182
|
if ( File::Path::rmtree( [$dir], DEBUG, 0 ) ) |
178
|
|
|
|
|
|
|
{ |
179
|
|
|
|
|
|
|
# Failed to delete the directory |
180
|
16
|
50
|
|
|
|
376
|
next if -e $path; |
181
|
16
|
|
|
|
|
238
|
push @removes, $path; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
else |
186
|
|
|
|
|
|
|
{ |
187
|
8
|
|
|
|
|
167
|
my ($save_mode) = ( stat $dir )[2]; |
188
|
8
|
|
|
|
|
176
|
chmod $save_mode & 0777, |
189
|
|
|
|
|
|
|
$dir; # just in case we cannot remove it. |
190
|
8
|
50
|
|
|
|
545
|
if ( $rmdir ? $rmdir->($dir) : rmdir($dir) ) |
|
|
50
|
|
|
|
|
|
191
|
|
|
|
|
|
|
{ |
192
|
|
|
|
|
|
|
# Failed to delete the directory |
193
|
8
|
50
|
|
|
|
155
|
next if -e $path; |
194
|
8
|
|
|
|
|
88
|
push @removes, $path; |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
else |
200
|
|
|
|
|
|
|
{ |
201
|
0
|
|
|
|
|
0
|
print "???: $path\n" if DEBUG; |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
29
|
|
|
|
|
1038
|
return @removes; |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
sub rm (@) |
209
|
|
|
|
|
|
|
{ |
210
|
0
|
|
|
0
|
1
|
0
|
goto &remove; |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
sub trash (@) |
214
|
|
|
|
|
|
|
{ |
215
|
0
|
|
|
0
|
1
|
0
|
local $unlink = $unlink; |
216
|
0
|
|
|
|
|
0
|
local $rmdir = $rmdir; |
217
|
|
|
|
|
|
|
|
218
|
0
|
0
|
|
|
|
0
|
if ( ref $_[0] eq 'HASH' ) |
219
|
|
|
|
|
|
|
{ |
220
|
0
|
|
|
|
|
0
|
my %options = %{ +shift @_ }; |
|
0
|
|
|
|
|
0
|
|
221
|
0
|
|
|
|
|
0
|
$unlink = $options{unlink}; |
222
|
0
|
|
|
|
|
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
|
|
|
|
|
|
|
my $code = sub { |
247
|
|
|
|
|
|
|
my @files = |
248
|
0
|
|
|
0
|
|
0
|
map { Mac::Glue::param_type( Mac::Glue::typeAlias() => $_ ) } |
|
0
|
|
|
|
|
0
|
|
249
|
|
|
|
|
|
|
@_; |
250
|
0
|
|
|
|
|
0
|
$glue->delete( \@files ); |
251
|
|
|
|
|
|
|
}; |
252
|
|
|
|
|
|
|
$unlink = $code; |
253
|
|
|
|
|
|
|
$rmdir = $code; |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
else |
256
|
|
|
|
|
|
|
{ |
257
|
0
|
|
|
|
|
0
|
die |
258
|
|
|
|
|
|
|
"Support for trash() on platform '$^O' not available at this time.\n"; |
259
|
|
|
|
|
|
|
} |
260
|
|
|
|
|
|
|
|
261
|
0
|
|
|
|
|
0
|
remove(@_); |
262
|
|
|
|
|
|
|
} |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
sub undelete (@) |
265
|
|
|
|
|
|
|
{ |
266
|
0
|
|
|
0
|
1
|
0
|
goto &trash; |
267
|
|
|
|
|
|
|
} |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
###################################################################### |
270
|
|
|
|
|
|
|
# Support Functions |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
sub _expand_with_opts |
273
|
|
|
|
|
|
|
{ |
274
|
29
|
|
|
29
|
|
60
|
my $opts = shift; |
275
|
29
|
100
|
|
|
|
446
|
return ( $opts->{glob} ? expand(@_) : @_ ); |
276
|
|
|
|
|
|
|
} |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
sub expand (@) |
279
|
|
|
|
|
|
|
{ |
280
|
33
|
100
|
|
33
|
1
|
1518
|
map { -e $_ ? $_ : File::Glob::bsd_glob($_) } @_; |
|
34
|
|
|
|
|
1046
|
|
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
|
|
|
|
|
|
|
{ |
287
|
28
|
|
|
28
|
|
3652
|
my $remove = File::Spec->rel2abs(shift); |
288
|
28
|
100
|
|
|
|
70735
|
my $cwd = @_ ? shift : Cwd::cwd(); |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
# Do everything in absolute terms |
291
|
28
|
|
|
|
|
2049
|
$remove = Cwd::abs_path($remove); |
292
|
28
|
|
|
|
|
853
|
$cwd = Cwd::abs_path($cwd); |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
# If we are on a different volume we don't need to move |
295
|
28
|
|
|
|
|
990
|
my ( $cv, $cd ) = File::Spec->splitpath( $cwd, 1 ); |
296
|
28
|
|
|
|
|
190
|
my ( $rv, $rd ) = File::Spec->splitpath( $remove, 1 ); |
297
|
28
|
50
|
|
|
|
173
|
return '' unless $cv eq $rv; |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
# If we have to move, it's to one level above the deletion |
300
|
28
|
|
|
|
|
397
|
my @cd = File::Spec->splitdir($cd); |
301
|
28
|
|
|
|
|
327
|
my @rd = File::Spec->splitdir($rd); |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
# Is the current directory the same as or inside the remove directory? |
304
|
28
|
100
|
|
|
|
133
|
unless ( @cd >= @rd ) |
305
|
|
|
|
|
|
|
{ |
306
|
24
|
|
|
|
|
384
|
return ''; |
307
|
|
|
|
|
|
|
} |
308
|
4
|
|
|
|
|
20
|
foreach ( 0 .. $#rd ) |
309
|
|
|
|
|
|
|
{ |
310
|
26
|
100
|
|
|
|
55
|
$cd[$_] eq $rd[$_] or return ''; |
311
|
|
|
|
|
|
|
} |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
# Confirmed, the current working dir is in the removal dir |
314
|
3
|
|
|
|
|
5
|
pop @rd; |
315
|
3
|
|
|
|
|
89
|
return File::Spec->catpath( $rv, File::Spec->catdir(@rd), '' ); |
316
|
|
|
|
|
|
|
} |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
1; |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
__END__ |