Commit | Line | Data |
1fc4cb55 |
1 | package File::Path; |
fed7345c |
2 | |
cac619e8 |
3 | use 5.005_04; |
4 | use strict; |
5 | |
6 | use Cwd 'getcwd'; |
7 | use File::Basename (); |
8 | use File::Spec (); |
9 | |
10 | BEGIN { |
11 | if ($] < 5.006) { |
12 | # can't say 'opendir my $dh, $dirname' |
13 | # need to initialise $dh |
14 | eval "use Symbol"; |
15 | } |
16 | } |
17 | |
18 | use Exporter (); |
19 | use vars qw($VERSION @ISA @EXPORT); |
8b0a9f85 |
20 | $VERSION = '2.02_01'; |
cac619e8 |
21 | @ISA = qw(Exporter); |
22 | @EXPORT = qw(mkpath rmtree); |
23 | |
24 | my $Is_VMS = $^O eq 'VMS'; |
25 | my $Is_MacOS = $^O eq 'MacOS'; |
26 | |
27 | # These OSes complain if you want to remove a file that you have no |
28 | # write permission to: |
29 | my $Force_Writeable = ($^O eq 'os2' || $^O eq 'dos' || $^O eq 'MSWin32' || |
30 | $^O eq 'amigaos' || $^O eq 'MacOS' || $^O eq 'epoc'); |
31 | |
32 | sub _carp { |
33 | require Carp; |
34 | goto &Carp::carp; |
35 | } |
36 | |
37 | sub _croak { |
38 | require Carp; |
39 | goto &Carp::croak; |
40 | } |
41 | |
42 | sub _error { |
43 | my $arg = shift; |
44 | my $message = shift; |
45 | my $object = shift; |
46 | |
47 | if ($arg->{error}) { |
48 | $object = '' unless defined $object; |
49 | push @{${$arg->{error}}}, {$object => "$message: $!"}; |
50 | } |
51 | else { |
52 | _carp(defined($object) ? "$message for $object: $!" : "$message: $!"); |
53 | } |
54 | } |
55 | |
56 | sub mkpath { |
57 | my $old_style = ( |
58 | UNIVERSAL::isa($_[0],'ARRAY') |
59 | or (@_ == 2 and (defined $_[1] ? $_[1] =~ /\A\d+\z/ : 1)) |
60 | or (@_ == 3 |
61 | and (defined $_[1] ? $_[1] =~ /\A\d+\z/ : 1) |
62 | and (defined $_[2] ? $_[2] =~ /\A\d+\z/ : 1) |
63 | ) |
64 | ) ? 1 : 0; |
65 | |
66 | my $arg; |
67 | my $paths; |
68 | |
69 | if ($old_style) { |
70 | my ($verbose, $mode); |
71 | ($paths, $verbose, $mode) = @_; |
72 | $paths = [$paths] unless UNIVERSAL::isa($paths,'ARRAY'); |
73 | $arg->{verbose} = defined $verbose ? $verbose : 0; |
74 | $arg->{mode} = defined $mode ? $mode : 0777; |
75 | } |
76 | else { |
77 | if (@_ > 0 and UNIVERSAL::isa($_[-1], 'HASH')) { |
78 | $arg = pop @_; |
79 | exists $arg->{mask} and $arg->{mode} = delete $arg->{mask}; |
80 | $arg->{mode} = 0777 unless exists $arg->{mode}; |
81 | ${$arg->{error}} = [] if exists $arg->{error}; |
82 | } |
83 | else { |
84 | @{$arg}{qw(verbose mode)} = (0, 0777); |
85 | } |
86 | $paths = [@_]; |
87 | } |
88 | return _mkpath($arg, $paths); |
89 | } |
90 | |
91 | sub _mkpath { |
92 | my $arg = shift; |
93 | my $paths = shift; |
94 | |
95 | local($")=$Is_MacOS ? ":" : "/"; |
96 | my(@created,$path); |
97 | foreach $path (@$paths) { |
98 | next unless length($path); |
99 | $path .= '/' if $^O eq 'os2' and $path =~ /^\w:\z/s; # feature of CRT |
100 | # Logic wants Unix paths, so go with the flow. |
101 | if ($Is_VMS) { |
102 | next if $path eq '/'; |
103 | $path = VMS::Filespec::unixify($path); |
104 | } |
105 | next if -d $path; |
106 | my $parent = File::Basename::dirname($path); |
107 | unless (-d $parent or $path eq $parent) { |
108 | push(@created,_mkpath($arg, [$parent])); |
109 | } |
110 | print "mkdir $path\n" if $arg->{verbose}; |
111 | if (mkdir($path,$arg->{mode})) { |
112 | push(@created, $path); |
113 | } |
114 | else { |
115 | my $save_bang = $!; |
116 | my ($e, $e1) = ($save_bang, $^E); |
117 | $e .= "; $e1" if $e ne $e1; |
118 | # allow for another process to have created it meanwhile |
119 | if (!-d $path) { |
120 | $! = $save_bang; |
121 | if ($arg->{error}) { |
122 | push @{${$arg->{error}}}, {$path => $e}; |
123 | } |
124 | else { |
125 | _croak("mkdir $path: $e"); |
126 | } |
127 | } |
128 | } |
129 | } |
130 | return @created; |
131 | } |
132 | |
133 | sub rmtree { |
134 | my $old_style = ( |
135 | UNIVERSAL::isa($_[0],'ARRAY') |
136 | or (@_ == 2 and (defined $_[1] ? $_[1] =~ /\A\d+\z/ : 1)) |
137 | or (@_ == 3 |
138 | and (defined $_[1] ? $_[1] =~ /\A\d+\z/ : 1) |
139 | and (defined $_[2] ? $_[2] =~ /\A\d+\z/ : 1) |
140 | ) |
141 | ) ? 1 : 0; |
142 | |
143 | my $arg; |
144 | my $paths; |
145 | |
146 | if ($old_style) { |
147 | my ($verbose, $safe); |
148 | ($paths, $verbose, $safe) = @_; |
149 | $arg->{verbose} = defined $verbose ? $verbose : 0; |
150 | $arg->{safe} = defined $safe ? $safe : 0; |
151 | |
152 | if (defined($paths) and length($paths)) { |
153 | $paths = [$paths] unless UNIVERSAL::isa($paths,'ARRAY'); |
154 | } |
155 | else { |
156 | _carp ("No root path(s) specified\n"); |
157 | return 0; |
158 | } |
159 | } |
160 | else { |
161 | if (@_ > 0 and UNIVERSAL::isa($_[-1],'HASH')) { |
162 | $arg = pop @_; |
163 | ${$arg->{error}} = [] if exists $arg->{error}; |
164 | ${$arg->{result}} = [] if exists $arg->{result}; |
165 | } |
166 | else { |
167 | @{$arg}{qw(verbose safe)} = (0, 0); |
168 | } |
169 | $paths = [@_]; |
170 | } |
171 | |
172 | $arg->{prefix} = ''; |
173 | $arg->{depth} = 0; |
174 | |
175 | $arg->{cwd} = getcwd() or do { |
176 | _error($arg, "cannot fetch initial working directory"); |
177 | return 0; |
178 | }; |
179 | for ($arg->{cwd}) { /\A(.*)\Z/; $_ = $1 } # untaint |
180 | |
181 | @{$arg}{qw(device inode)} = (stat $arg->{cwd})[0,1] or do { |
182 | _error($arg, "cannot stat initial working directory", $arg->{cwd}); |
183 | return 0; |
184 | }; |
185 | |
186 | return _rmtree($arg, $paths); |
187 | } |
188 | |
189 | sub _rmtree { |
190 | my $arg = shift; |
191 | my $paths = shift; |
192 | |
193 | my $count = 0; |
194 | my $curdir = File::Spec->curdir(); |
195 | my $updir = File::Spec->updir(); |
196 | |
197 | my (@files, $root); |
198 | ROOT_DIR: |
199 | foreach $root (@$paths) { |
200 | if ($Is_MacOS) { |
201 | $root = ":$root" unless $root =~ /:/; |
202 | $root .= ":" unless $root =~ /:\z/; |
203 | } |
204 | else { |
205 | $root =~ s{/\z}{}; |
206 | } |
207 | |
208 | # since we chdir into each directory, it may not be obvious |
209 | # to figure out where we are if we generate a message about |
210 | # a file name. We therefore construct a semi-canonical |
211 | # filename, anchored from the directory being unlinked (as |
212 | # opposed to being truly canonical, anchored from the root (/). |
213 | |
214 | my $canon = $arg->{prefix} |
215 | ? File::Spec->catfile($arg->{prefix}, $root) |
216 | : $root |
217 | ; |
218 | |
219 | my ($ldev, $lino, $perm) = (lstat $root)[0,1,2] or next ROOT_DIR; |
220 | |
221 | if ( -d _ ) { |
222 | $root = VMS::Filespec::pathify($root) if $Is_VMS; |
223 | if (!chdir($root)) { |
224 | # see if we can escalate privileges to get in |
225 | # (e.g. funny protection mask such as -w- instead of rwx) |
226 | $perm &= 07777; |
227 | my $nperm = $perm | 0700; |
228 | if (!($arg->{safe} or $nperm == $perm or chmod($nperm, $root))) { |
229 | _error($arg, "cannot make child directory read-write-exec", $canon); |
230 | next ROOT_DIR; |
231 | } |
232 | elsif (!chdir($root)) { |
233 | _error($arg, "cannot chdir to child", $canon); |
234 | next ROOT_DIR; |
235 | } |
236 | } |
237 | |
238 | my ($device, $inode, $perm) = (stat $curdir)[0,1,2] or do { |
239 | _error($arg, "cannot stat current working directory", $canon); |
240 | next ROOT_DIR; |
241 | }; |
242 | |
243 | ($ldev eq $device and $lino eq $inode) |
244 | or _croak("directory $canon changed before chdir, expected dev=$ldev inode=$lino, actual dev=$device ino=$inode, aborting."); |
245 | |
246 | $perm &= 07777; # don't forget setuid, setgid, sticky bits |
247 | my $nperm = $perm | 0700; |
248 | |
249 | # notabene: 0700 is for making readable in the first place, |
250 | # it's also intended to change it to writable in case we have |
251 | # to recurse in which case we are better than rm -rf for |
252 | # subtrees with strange permissions |
253 | |
254 | if (!($arg->{safe} or $nperm == $perm or chmod($nperm, $curdir))) { |
255 | _error($arg, "cannot make directory read+writeable", $canon); |
256 | $nperm = $perm; |
257 | } |
258 | |
259 | my $d; |
260 | $d = gensym() if $] < 5.006; |
261 | if (!opendir $d, $curdir) { |
262 | _error($arg, "cannot opendir", $canon); |
263 | @files = (); |
264 | } |
265 | else { |
266 | no strict 'refs'; |
267 | if (!defined ${"\cTAINT"} or ${"\cTAINT"}) { |
268 | # Blindly untaint dir names if taint mode is |
269 | # active, or any perl < 5.006 |
270 | @files = map { /\A(.*)\z/s; $1 } readdir $d; |
271 | } |
272 | else { |
273 | @files = readdir $d; |
274 | } |
275 | closedir $d; |
276 | } |
277 | |
278 | if ($Is_VMS) { |
279 | # Deleting large numbers of files from VMS Files-11 |
280 | # filesystems is faster if done in reverse ASCIIbetical order. |
281 | # include '.' to '.;' from blead patch #31775 |
282 | @files = map {$_ eq '.' ? '.;' : $_} reverse @files; |
283 | ($root = VMS::Filespec::unixify($root)) =~ s/\.dir\z//; |
284 | } |
285 | @files = grep {$_ ne $updir and $_ ne $curdir} @files; |
286 | |
287 | if (@files) { |
288 | # remove the contained files before the directory itself |
289 | my $narg = {%$arg}; |
290 | @{$narg}{qw(device inode cwd prefix depth)} |
291 | = ($device, $inode, $updir, $canon, $arg->{depth}+1); |
292 | $count += _rmtree($narg, \@files); |
293 | } |
294 | |
295 | # restore directory permissions of required now (in case the rmdir |
296 | # below fails), while we are still in the directory and may do so |
297 | # without a race via '.' |
298 | if ($nperm != $perm and not chmod($perm, $curdir)) { |
299 | _error($arg, "cannot reset chmod", $canon); |
300 | } |
301 | |
302 | # don't leave the client code in an unexpected directory |
303 | chdir($arg->{cwd}) |
304 | or _croak("cannot chdir to $arg->{cwd} from $canon: $!, aborting."); |
305 | |
306 | # ensure that a chdir upwards didn't take us somewhere other |
307 | # than we expected (see CVE-2002-0435) |
308 | ($device, $inode) = (stat $curdir)[0,1] |
309 | or _croak("cannot stat prior working directory $arg->{cwd}: $!, aborting."); |
310 | |
311 | ($arg->{device} eq $device and $arg->{inode} eq $inode) |
312 | or _croak("previous directory $arg->{cwd} changed before entering $canon, expected dev=$ldev inode=$lino, actual dev=$device ino=$inode, aborting."); |
313 | |
314 | if ($arg->{depth} or !$arg->{keep_root}) { |
315 | if ($arg->{safe} && |
316 | ($Is_VMS ? !&VMS::Filespec::candelete($root) : !-w $root)) { |
317 | print "skipped $root\n" if $arg->{verbose}; |
318 | next ROOT_DIR; |
319 | } |
320 | if (!chmod $perm | 0700, $root) { |
321 | if ($Force_Writeable) { |
322 | _error($arg, "cannot make directory writeable", $canon); |
323 | } |
324 | } |
325 | print "rmdir $root\n" if $arg->{verbose}; |
326 | if (rmdir $root) { |
327 | push @{${$arg->{result}}}, $root if $arg->{result}; |
328 | ++$count; |
329 | } |
330 | else { |
331 | _error($arg, "cannot remove directory", $canon); |
332 | if (!chmod($perm, ($Is_VMS ? VMS::Filespec::fileify($root) : $root)) |
333 | ) { |
334 | _error($arg, sprintf("cannot restore permissions to 0%o",$perm), $canon); |
335 | } |
336 | } |
337 | } |
338 | } |
339 | else { |
340 | # not a directory |
341 | |
342 | $root = VMS::Filespec::vmsify("./$root") |
8b0a9f85 |
343 | if $Is_VMS |
344 | && !File::Spec->file_name_is_absolute($root) |
345 | && ($root !~ m/(?<!\^)[\]>]+/); # not already in VMS syntax |
cac619e8 |
346 | |
347 | if ($arg->{safe} && |
348 | ($Is_VMS ? !&VMS::Filespec::candelete($root) |
349 | : !(-l $root || -w $root))) |
350 | { |
351 | print "skipped $root\n" if $arg->{verbose}; |
352 | next ROOT_DIR; |
353 | } |
354 | |
355 | my $nperm = $perm & 07777 | 0600; |
356 | if ($nperm != $perm and not chmod $nperm, $root) { |
357 | if ($Force_Writeable) { |
358 | _error($arg, "cannot make file writeable", $canon); |
359 | } |
360 | } |
361 | print "unlink $canon\n" if $arg->{verbose}; |
362 | # delete all versions under VMS |
363 | for (;;) { |
364 | if (unlink $root) { |
365 | push @{${$arg->{result}}}, $root if $arg->{result}; |
366 | } |
367 | else { |
368 | _error($arg, "cannot unlink file", $canon); |
369 | $Force_Writeable and chmod($perm, $root) or |
370 | _error($arg, sprintf("cannot restore permissions to 0%o",$perm), $canon); |
371 | last; |
372 | } |
373 | ++$count; |
374 | last unless $Is_VMS && lstat $root; |
375 | } |
376 | } |
377 | } |
378 | |
379 | return $count; |
380 | } |
381 | |
382 | 1; |
383 | __END__ |
384 | |
fed7345c |
385 | =head1 NAME |
386 | |
12c2e016 |
387 | File::Path - Create or remove directory trees |
388 | |
389 | =head1 VERSION |
390 | |
cac619e8 |
391 | This document describes version 2.02 of File::Path, released |
392 | 2007-10-24. |
fed7345c |
393 | |
394 | =head1 SYNOPSIS |
395 | |
8b87c192 |
396 | use File::Path; |
fed7345c |
397 | |
12c2e016 |
398 | # modern |
399 | mkpath( 'foo/bar/baz', '/zug/zwang', {verbose => 1} ); |
400 | |
401 | rmtree( |
402 | 'foo/bar/baz', '/zug/zwang', |
91c4f65e |
403 | { verbose => 1, error => \my $err_list } |
12c2e016 |
404 | ); |
405 | |
406 | # traditional |
8b87c192 |
407 | mkpath(['/foo/bar/baz', 'blurfl/quux'], 1, 0711); |
408 | rmtree(['foo/bar/baz', 'blurfl/quux'], 1, 1); |
fed7345c |
409 | |
410 | =head1 DESCRIPTION |
411 | |
0b3d36bd |
412 | The C<mkpath> function provides a convenient way to create directories |
413 | of arbitrary depth. Similarly, the C<rmtree> function provides a |
414 | convenient way to delete an entire directory subtree from the |
415 | filesystem, much like the Unix command C<rm -r>. |
12c2e016 |
416 | |
417 | Both functions may be called in one of two ways, the traditional, |
418 | compatible with code written since the dawn of time, and modern, |
419 | that offers a more flexible and readable idiom. New code should use |
420 | the modern interface. |
421 | |
422 | =head2 FUNCTIONS |
423 | |
0b3d36bd |
424 | The modern way of calling C<mkpath> and C<rmtree> is with a list |
425 | of directories to create, or remove, respectively, followed by an |
426 | optional hash reference containing keys to control the |
427 | function's behaviour. |
12c2e016 |
428 | |
429 | =head3 C<mkpath> |
430 | |
0b3d36bd |
431 | The following keys are recognised as parameters to C<mkpath>. |
432 | The function returns the list of files actually created during the |
433 | call. |
12c2e016 |
434 | |
435 | my @created = mkpath( |
436 | qw(/tmp /flub /home/nobody), |
437 | {verbose => 1, mode => 0750}, |
438 | ); |
439 | print "created $_\n" for @created; |
440 | |
441 | =over 4 |
442 | |
443 | =item mode |
444 | |
0b3d36bd |
445 | The numeric permissions mode to apply to each created directory |
446 | (defaults to 0777), to be modified by the current C<umask>. If the |
447 | directory already exists (and thus does not need to be created), |
448 | the permissions will not be modified. |
449 | |
450 | C<mask> is recognised as an alias for this parameter. |
12c2e016 |
451 | |
452 | =item verbose |
453 | |
454 | If present, will cause C<mkpath> to print the name of each directory |
455 | as it is created. By default nothing is printed. |
456 | |
457 | =item error |
458 | |
459 | If present, will be interpreted as a reference to a list, and will |
460 | be used to store any errors that are encountered. See the ERROR |
0b3d36bd |
461 | HANDLING section for more information. |
12c2e016 |
462 | |
0b3d36bd |
463 | If this parameter is not used, certain error conditions may raise |
464 | a fatal error that will cause the program will halt, unless trapped |
465 | in an C<eval> block. |
12c2e016 |
466 | |
467 | =back |
468 | |
469 | =head3 C<rmtree> |
470 | |
471 | =over 4 |
472 | |
473 | =item verbose |
474 | |
475 | If present, will cause C<rmtree> to print the name of each file as |
476 | it is unlinked. By default nothing is printed. |
477 | |
5808899a |
478 | =item safe |
12c2e016 |
479 | |
0b3d36bd |
480 | When set to a true value, will cause C<rmtree> to skip the files |
481 | for which the process lacks the required privileges needed to delete |
5808899a |
482 | files, such as delete privileges on VMS. In other words, the code |
483 | will make no attempt to alter file permissions. Thus, if the process |
484 | is interrupted, no filesystem object will be left in a more |
485 | permissive mode. |
12c2e016 |
486 | |
487 | =item keep_root |
488 | |
0b3d36bd |
489 | When set to a true value, will cause all files and subdirectories |
490 | to be removed, except the initially specified directories. This comes |
491 | in handy when cleaning out an application's scratch directory. |
12c2e016 |
492 | |
493 | rmtree( '/tmp', {keep_root => 1} ); |
494 | |
495 | =item result |
496 | |
497 | If present, will be interpreted as a reference to a list, and will |
498 | be used to store the list of all files and directories unlinked |
499 | during the call. If nothing is unlinked, a reference to an empty |
500 | list is returned (rather than C<undef>). |
501 | |
502 | rmtree( '/tmp', {result => \my $list} ); |
503 | print "unlinked $_\n" for @$list; |
504 | |
0b3d36bd |
505 | This is a useful alternative to the C<verbose> key. |
506 | |
12c2e016 |
507 | =item error |
508 | |
509 | If present, will be interpreted as a reference to a list, |
510 | and will be used to store any errors that are encountered. |
0b3d36bd |
511 | See the ERROR HANDLING section for more information. |
12c2e016 |
512 | |
0b3d36bd |
513 | Removing things is a much more dangerous proposition than |
514 | creating things. As such, there are certain conditions that |
515 | C<rmtree> may encounter that are so dangerous that the only |
516 | sane action left is to kill the program. |
517 | |
518 | Use C<error> to trap all that is reasonable (problems with |
519 | permissions and the like), and let it die if things get out |
520 | of hand. This is the safest course of action. |
12c2e016 |
521 | |
522 | =back |
523 | |
524 | =head2 TRADITIONAL INTERFACE |
525 | |
0b3d36bd |
526 | The old interfaces of C<mkpath> and C<rmtree> take a reference to |
527 | a list of directories (to create or remove), followed by a series |
528 | of positional, numeric, modal parameters that control their behaviour. |
529 | |
530 | This design made it difficult to add additional functionality, as |
531 | well as posed the problem of what to do when the calling code only |
532 | needs to set the last parameter. Even though the code doesn't care |
533 | how the initial positional parameters are set, the programmer is |
534 | forced to learn what the defaults are, and specify them. |
12c2e016 |
535 | |
0b3d36bd |
536 | Worse, if it turns out in the future that it would make more sense |
537 | to change the default behaviour of the first parameter (for example, |
538 | to avoid a security vulnerability), all existing code will remain |
539 | hard-wired to the wrong defaults. |
12c2e016 |
540 | |
0b3d36bd |
541 | Finally, a series of numeric parameters are much less self-documenting |
542 | in terms of communicating to the reader what the code is doing. Named |
543 | parameters do not have this problem. |
544 | |
545 | In the traditional API, C<mkpath> takes three arguments: |
fed7345c |
546 | |
547 | =over 4 |
548 | |
549 | =item * |
550 | |
0b3d36bd |
551 | The name of the path to create, or a reference to a list of paths |
552 | to create, |
fed7345c |
553 | |
554 | =item * |
555 | |
0b3d36bd |
556 | a boolean value, which if TRUE will cause C<mkpath> to print the |
557 | name of each directory as it is created (defaults to FALSE), and |
fed7345c |
558 | |
559 | =item * |
560 | |
0b3d36bd |
561 | the numeric mode to use when creating the directories (defaults to |
562 | 0777), to be modified by the current umask. |
fed7345c |
563 | |
564 | =back |
565 | |
037c8c09 |
566 | It returns a list of all directories (including intermediates, determined |
cc61921f |
567 | using the Unix '/' separator) created. In scalar context it returns |
568 | the number of directories created. |
fed7345c |
569 | |
070ed461 |
570 | If a system error prevents a directory from being created, then the |
99c4c5e8 |
571 | C<mkpath> function throws a fatal error with C<Carp::croak>. This error |
572 | can be trapped with an C<eval> block: |
070ed461 |
573 | |
574 | eval { mkpath($dir) }; |
575 | if ($@) { |
576 | print "Couldn't create $dir: $@"; |
577 | } |
578 | |
0b3d36bd |
579 | In the traditional API, C<rmtree> takes three arguments: |
fed7345c |
580 | |
581 | =over 4 |
582 | |
583 | =item * |
584 | |
0b3d36bd |
585 | the root of the subtree to delete, or a reference to a list of |
586 | roots. All of the files and directories below each root, as well |
587 | as the roots themselves, will be deleted. If you want to keep |
588 | the roots themselves, you must use the modern API. |
fed7345c |
589 | |
590 | =item * |
591 | |
0b3d36bd |
592 | a boolean value, which if TRUE will cause C<rmtree> to print a |
593 | message each time it examines a file, giving the name of the file, |
594 | and indicating whether it's using C<rmdir> or C<unlink> to remove |
595 | it, or that it's skipping it. (defaults to FALSE) |
fed7345c |
596 | |
597 | =item * |
598 | |
0b3d36bd |
599 | a boolean value, which if TRUE will cause C<rmtree> to skip any |
600 | files to which you do not have delete access (if running under VMS) |
601 | or write access (if running under another OS). This will change |
602 | in the future when a criterion for 'delete permission' under OSs |
603 | other than VMS is settled. (defaults to FALSE) |
fed7345c |
604 | |
605 | =back |
606 | |
cc61921f |
607 | It returns the number of files, directories and symlinks successfully |
608 | deleted. Symlinks are simply deleted and not followed. |
fed7345c |
609 | |
12c2e016 |
610 | Note also that the occurrence of errors in C<rmtree> using the |
611 | traditional interface can be determined I<only> by trapping diagnostic |
612 | messages using C<$SIG{__WARN__}>; it is not apparent from the return |
613 | value. (The modern interface may use the C<error> parameter to |
0b3d36bd |
614 | record any problems encountered). |
12c2e016 |
615 | |
616 | =head2 ERROR HANDLING |
617 | |
618 | If C<mkpath> or C<rmtree> encounter an error, a diagnostic message |
619 | will be printed to C<STDERR> via C<carp> (for non-fatal errors), |
620 | or via C<croak> (for fatal errors). |
621 | |
622 | If this behaviour is not desirable, the C<error> attribute may be |
623 | used to hold a reference to a variable, which will be used to store |
624 | the diagnostics. The result is a reference to a list of hash |
625 | references. For each hash reference, the key is the name of the |
626 | file, and the value is the error message (usually the contents of |
627 | C<$!>). An example usage looks like: |
628 | |
629 | rmpath( 'foo/bar', 'bar/rat', {error => \my $err} ); |
630 | for my $diag (@$err) { |
631 | my ($file, $message) = each %$diag; |
632 | print "problem unlinking $file: $message\n"; |
633 | } |
634 | |
635 | If no errors are encountered, C<$err> will point to an empty list |
636 | (thus there is no need to test for C<undef>). If a general error |
637 | is encountered (for instance, C<rmtree> attempts to remove a directory |
638 | tree that does not exist), the diagnostic key will be empty, only |
639 | the value will be set: |
640 | |
641 | rmpath( '/no/such/path', {error => \my $err} ); |
642 | for my $diag (@$err) { |
643 | my ($file, $message) = each %$diag; |
644 | if ($file eq '') { |
645 | print "general error: $message\n"; |
646 | } |
647 | } |
648 | |
649 | =head2 NOTES |
650 | |
0b3d36bd |
651 | C<File::Path> blindly exports C<mkpath> and C<rmtree> into the |
652 | current namespace. These days, this is considered bad style, but |
653 | to change it now would break too much code. Nonetheless, you are |
654 | invited to specify what it is you are expecting to use: |
655 | |
656 | use File::Path 'rmtree'; |
657 | |
12c2e016 |
658 | =head3 HEURISTICS |
659 | |
660 | The functions detect (as far as possible) which way they are being |
661 | called and will act appropriately. It is important to remember that |
662 | the heuristic for detecting the old style is either the presence |
663 | of an array reference, or two or three parameters total and second |
664 | and third parameters are numeric. Hence... |
665 | |
0b3d36bd |
666 | mkpath 486, 487, 488; |
12c2e016 |
667 | |
668 | ... will not assume the modern style and create three directories, rather |
669 | it will create one directory verbosely, setting the permission to |
670 | 0750 (488 being the decimal equivalent of octal 750). Here, old |
671 | style trumps new. It must, for backwards compatibility reasons. |
e2ba98a1 |
672 | |
12c2e016 |
673 | If you want to ensure there is absolutely no ambiguity about which |
674 | way the function will behave, make sure the first parameter is a |
675 | reference to a one-element list, to force the old style interpretation: |
e2ba98a1 |
676 | |
0b3d36bd |
677 | mkpath [486], 487, 488; |
12c2e016 |
678 | |
679 | and get only one directory created. Or add a reference to an empty |
680 | parameter hash, to force the new style: |
681 | |
0b3d36bd |
682 | mkpath 486, 487, 488, {}; |
12c2e016 |
683 | |
684 | ... and hence create the three directories. If the empty hash |
685 | reference seems a little strange to your eyes, or you suspect a |
686 | subsequent programmer might I<helpfully> optimise it away, you |
687 | can add a parameter set to a default value: |
688 | |
0b3d36bd |
689 | mkpath 486, 487, 488, {verbose => 0}; |
12c2e016 |
690 | |
0b3d36bd |
691 | =head3 SECURITY CONSIDERATIONS |
12c2e016 |
692 | |
0b3d36bd |
693 | There were race conditions 1.x implementations of File::Path's |
694 | C<rmtree> function (although sometimes patched depending on the OS |
695 | distribution or platform). The 2.0 version contains code to avoid the |
696 | problem mentioned in CVE-2002-0435. |
12c2e016 |
697 | |
0b3d36bd |
698 | See the following pages for more information: |
12c2e016 |
699 | |
0b3d36bd |
700 | http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=286905 |
701 | http://www.nntp.perl.org/group/perl.perl5.porters/2005/01/msg97623.html |
702 | http://www.debian.org/security/2005/dsa-696 |
12c2e016 |
703 | |
5808899a |
704 | Additionally, unless the C<safe> parameter is set (or the |
37b1cd44 |
705 | third parameter in the traditional interface is TRUE), should a |
0b3d36bd |
706 | C<rmtree> be interrupted, files that were originally in read-only |
707 | mode may now have their permissions set to a read-write (or "delete |
708 | OK") mode. |
96e4d5b1 |
709 | |
b8d5f521 |
710 | =head1 DIAGNOSTICS |
711 | |
0b3d36bd |
712 | FATAL errors will cause the program to halt (C<croak>), since the |
713 | problem is so severe that it would be dangerous to continue. (This |
714 | can always be trapped with C<eval>, but it's not a good idea. Under |
715 | the circumstances, dying is the best thing to do). |
716 | |
717 | SEVERE errors may be trapped using the modern interface. If the |
718 | they are not trapped, or the old interface is used, such an error |
719 | will cause the program will halt. |
720 | |
721 | All other errors may be trapped using the modern interface, otherwise |
722 | they will be C<carp>ed about. Program execution will not be halted. |
723 | |
b8d5f521 |
724 | =over 4 |
725 | |
37b1cd44 |
726 | =item mkdir [path]: [errmsg] (SEVERE) |
0b3d36bd |
727 | |
728 | C<mkpath> was unable to create the path. Probably some sort of |
729 | permissions error at the point of departure, or insufficient resources |
730 | (such as free inodes on Unix). |
731 | |
732 | =item No root path(s) specified |
733 | |
734 | C<mkpath> was not given any paths to create. This message is only |
735 | emitted if the routine is called with the traditional interface. |
736 | The modern interface will remain silent if given nothing to do. |
737 | |
738 | =item No such file or directory |
739 | |
740 | On Windows, if C<mkpath> gives you this warning, it may mean that |
741 | you have exceeded your filesystem's maximum path length. |
742 | |
743 | =item cannot fetch initial working directory: [errmsg] |
744 | |
745 | C<rmtree> attempted to determine the initial directory by calling |
746 | C<Cwd::getcwd>, but the call failed for some reason. No attempt |
747 | will be made to delete anything. |
748 | |
749 | =item cannot stat initial working directory: [errmsg] |
750 | |
751 | C<rmtree> attempted to stat the initial directory (after having |
752 | successfully obtained its name via C<getcwd>), however, the call |
753 | failed for some reason. No attempt will be made to delete anything. |
754 | |
755 | =item cannot chdir to [dir]: [errmsg] |
756 | |
757 | C<rmtree> attempted to set the working directory in order to |
758 | begin deleting the objects therein, but was unsuccessful. This is |
759 | usually a permissions issue. The routine will continue to delete |
760 | other things, but this directory will be left intact. |
761 | |
762 | =item directory [dir] changed before chdir, expected dev=[n] inode=[n], actual dev=[n] ino=[n], aborting. (FATAL) |
763 | |
764 | C<rmtree> recorded the device and inode of a directory, and then |
765 | moved into it. It then performed a C<stat> on the current directory |
766 | and detected that the device and inode were no longer the same. As |
767 | this is at the heart of the race condition problem, the program |
768 | will die at this point. |
769 | |
770 | =item cannot make directory [dir] read+writeable: [errmsg] |
771 | |
772 | C<rmtree> attempted to change the permissions on the current directory |
773 | to ensure that subsequent unlinkings would not run into problems, |
774 | but was unable to do so. The permissions remain as they were, and |
775 | the program will carry on, doing the best it can. |
776 | |
777 | =item cannot read [dir]: [errmsg] |
778 | |
779 | C<rmtree> tried to read the contents of the directory in order |
780 | to acquire the names of the directory entries to be unlinked, but |
781 | was unsuccessful. This is usually a permissions issue. The |
782 | program will continue, but the files in this directory will remain |
783 | after the call. |
784 | |
785 | =item cannot reset chmod [dir]: [errmsg] |
786 | |
cac619e8 |
787 | C<rmtree>, after having deleted everything in a directory, attempted |
788 | to restore its permissions to the original state but failed. The |
789 | directory may wind up being left behind. |
12c2e016 |
790 | |
cac619e8 |
791 | =item cannot chdir to [parent-dir] from [child-dir]: [errmsg], aborting. (FATAL) |
12c2e016 |
792 | |
cac619e8 |
793 | C<rmtree>, after having deleted everything and restored the permissions |
794 | of a directory, was unable to chdir back to the parent. This is usually |
795 | a sign that something evil this way comes. |
fed7345c |
796 | |
cac619e8 |
797 | =item cannot stat prior working directory [dir]: [errmsg], aborting. (FATAL) |
0b3d36bd |
798 | |
cac619e8 |
799 | C<rmtree> was unable to stat the parent directory after have returned |
800 | from the child. Since there is no way of knowing if we returned to |
801 | where we think we should be (by comparing device and inode) the only |
802 | way out is to C<croak>. |
0b3d36bd |
803 | |
cac619e8 |
804 | =item previous directory [parent-dir] changed before entering [child-dir], expected dev=[n] inode=[n], actual dev=[n] ino=[n], aborting. (FATAL) |
0b3d36bd |
805 | |
cac619e8 |
806 | When C<rmtree> returned from deleting files in a child directory, a |
807 | check revealed that the parent directory it returned to wasn't the one |
808 | it started out from. This is considered a sign of malicious activity. |
0b3d36bd |
809 | |
cac619e8 |
810 | =item cannot make directory [dir] writeable: [errmsg] |
ee79a11f |
811 | |
cac619e8 |
812 | Just before removing a directory (after having successfully removed |
813 | everything it contained), C<rmtree> attempted to set the permissions |
814 | on the directory to ensure it could be removed and failed. Program |
815 | execution continues, but the directory may possibly not be deleted. |
0b3d36bd |
816 | |
cac619e8 |
817 | =item cannot remove directory [dir]: [errmsg] |
0b3d36bd |
818 | |
cac619e8 |
819 | C<rmtree> attempted to remove a directory, but failed. This may because |
820 | some objects that were unable to be removed remain in the directory, or |
821 | a permissions issue. The directory will be left behind. |
0b3d36bd |
822 | |
cac619e8 |
823 | =item cannot restore permissions of [dir] to [0nnn]: [errmsg] |
0b3d36bd |
824 | |
cac619e8 |
825 | After having failed to remove a directory, C<rmtree> was unable to |
826 | restore its permissions from a permissive state back to a possibly |
827 | more restrictive setting. (Permissions given in octal). |
0b3d36bd |
828 | |
cac619e8 |
829 | =item cannot make file [file] writeable: [errmsg] |
b5400373 |
830 | |
cac619e8 |
831 | C<rmtree> attempted to force the permissions of a file to ensure it |
832 | could be deleted, but failed to do so. It will, however, still attempt |
833 | to unlink the file. |
0b3d36bd |
834 | |
cac619e8 |
835 | =item cannot unlink file [file]: [errmsg] |
0b3d36bd |
836 | |
cac619e8 |
837 | C<rmtree> failed to remove a file. Probably a permissions issue. |
0b3d36bd |
838 | |
cac619e8 |
839 | =item cannot restore permissions of [file] to [0nnn]: [errmsg] |
0b3d36bd |
840 | |
cac619e8 |
841 | After having failed to remove a file, C<rmtree> was also unable |
842 | to restore the permissions on the file to a possibly less permissive |
843 | setting. (Permissions given in octal). |
0b3d36bd |
844 | |
cac619e8 |
845 | =back |
12c2e016 |
846 | |
cac619e8 |
847 | =head1 SEE ALSO |
037c8c09 |
848 | |
cac619e8 |
849 | =over 4 |
0b3d36bd |
850 | |
cac619e8 |
851 | =item * |
0b3d36bd |
852 | |
cac619e8 |
853 | L<File::Find::Rule> |
0b3d36bd |
854 | |
cac619e8 |
855 | When removing directory trees, if you want to examine each file to |
856 | decide whether to delete it (and possibly leaving large swathes |
857 | alone), F<File::Find::Rule> offers a convenient and flexible approach |
858 | to examining directory trees. |
0b3d36bd |
859 | |
cac619e8 |
860 | =back |
0b3d36bd |
861 | |
cac619e8 |
862 | =head1 BUGS |
0b3d36bd |
863 | |
cac619e8 |
864 | Please report all bugs on the RT queue: |
b5400373 |
865 | |
cac619e8 |
866 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Path> |
b5400373 |
867 | |
cac619e8 |
868 | =head1 ACKNOWLEDGEMENTS |
0b3d36bd |
869 | |
cac619e8 |
870 | Paul Szabo identified the race condition originally, and Brendan |
871 | O'Dea wrote an implementation for Debian that addressed the problem. |
872 | That code was used as a basis for the current code. Their efforts |
873 | are greatly appreciated. |
fed7345c |
874 | |
cac619e8 |
875 | =head1 AUTHORS |
fed7345c |
876 | |
cac619e8 |
877 | Tim Bunce <F<Tim.Bunce@ig.co.uk>> and Charles Bailey |
878 | <F<bailey@newman.upenn.edu>>. Currently maintained by David Landgren |
879 | <F<david@landgren.net>>. |
880 | |
881 | =head1 COPYRIGHT |
882 | |
883 | This module is copyright (C) Charles Bailey, Tim Bunce and |
884 | David Landgren 1995-2007. All rights reserved. |
885 | |
886 | =head1 LICENSE |
887 | |
888 | This library is free software; you can redistribute it and/or modify |
889 | it under the same terms as Perl itself. |
890 | |
891 | =cut |