5 File::Path - Create or remove directory trees
9 This document describes version 2.00_02 of File::Path, released
17 mkpath( 'foo/bar/baz', '/zug/zwang', {verbose => 1} );
20 'foo/bar/baz', '/zug/zwang',
21 { verbose => 1, error => \my $err_list }
25 mkpath(['/foo/bar/baz', 'blurfl/quux'], 1, 0711);
26 rmtree(['foo/bar/baz', 'blurfl/quux'], 1, 1);
30 The C<mkpath> function provides a convenient way to create directories,
31 even if your C<mkdir> kernel call won't create more than one level
32 of directory at a time. Similarly, the C<rmtree> function provides
33 a convenient way to delete a subtree from the directory structure,
34 much like the Unix command C<rm -r>.
36 Both functions may be called in one of two ways, the traditional,
37 compatible with code written since the dawn of time, and modern,
38 that offers a more flexible and readable idiom. New code should use
43 The modern way of calling C<mkpath> and C<rmtree> is with an optional
44 hash reference at the end of the parameter list that holds various
45 keys that can be used to control the function's behaviour, following
46 a plain list of directories upon which to operate.
50 The following keys are recognised as as parameters to C<mkpath>.
51 It returns the list of files actually created during the call.
54 qw(/tmp /flub /home/nobody),
55 {verbose => 1, mode => 0750},
57 print "created $_\n" for @created;
63 The numeric mode to use when creating the directories (defaults
64 to 07777), to be modified by the current C<umask>. (C<mask> is
65 recognised as an alias for this parameter).
69 If present, will cause C<mkpath> to print the name of each directory
70 as it is created. By default nothing is printed.
74 If present, will be interpreted as a reference to a list, and will
75 be used to store any errors that are encountered. See the ERROR
76 HANDLING section below to find out more.
78 If this parameter is not used, any errors encountered will raise a
79 fatal error that need to be trapped in an C<eval> block, or the
90 If present, will cause C<rmtree> to print the name of each file as
91 it is unlinked. By default nothing is printed.
95 When set to a true value, will cause C<rmtree> to skip any files
96 to which you do not have delete access (if running under VMS) or
97 write access (if running under another OS). This will change in
98 the future when a criterion for 'delete permission' under OSs other
103 When set to a true value, will cause everything except the specified
104 base directories to be unlinked. This comes in handy when cleaning
105 out an application's scratch directory.
107 rmtree( '/tmp', {keep_root => 1} );
111 If present, will be interpreted as a reference to a list, and will
112 be used to store the list of all files and directories unlinked
113 during the call. If nothing is unlinked, a reference to an empty
114 list is returned (rather than C<undef>).
116 rmtree( '/tmp', {result => \my $list} );
117 print "unlinked $_\n" for @$list;
121 If present, will be interpreted as a reference to a list,
122 and will be used to store any errors that are encountered.
123 See the ERROR HANDLING section below to find out more.
125 If this parameter is not used, any errors encountered will
126 raise a fatal error that need to be trapped in an C<eval>
127 block, or the program will halt.
131 =head2 TRADITIONAL INTERFACE
133 The old interface for C<mkpath> and C<rmtree> take a
134 reference to a list of directories (to create or remove),
135 followed by a series of positional numeric modal parameters that
136 control their behaviour.
138 This design made it difficult to add
139 additional functionality, as well as posed the problem
140 of what to do when you don't care how the initial
141 positional parameters are specified but only the last
142 one needs to be specified. The calls themselves are also
143 less self-documenting.
145 C<mkpath> takes three arguments:
151 The name of the path to create, or a reference
152 to a list of paths to create,
156 a boolean value, which if TRUE will cause C<mkpath>
157 to print the name of each directory as it is created
158 (defaults to FALSE), and
162 the numeric mode to use when creating the directories
163 (defaults to 0777), to be modified by the current umask.
167 It returns a list of all directories (including intermediates, determined
168 using the Unix '/' separator) created. In scalar context it returns
169 the number of directories created.
171 If a system error prevents a directory from being created, then the
172 C<mkpath> function throws a fatal error with C<Carp::croak>. This error
173 can be trapped with an C<eval> block:
175 eval { mkpath($dir) };
177 print "Couldn't create $dir: $@";
180 In the traditional form, C<rmtree> takes three arguments:
186 the root of the subtree to delete, or a reference to
187 a list of roots. All of the files and directories
188 below each root, as well as the roots themselves,
193 a boolean value, which if TRUE will cause C<rmtree> to
194 print a message each time it examines a file, giving the
195 name of the file, and indicating whether it's using C<rmdir>
196 or C<unlink> to remove it, or that it's skipping it.
201 a boolean value, which if TRUE will cause C<rmtree> to
202 skip any files to which you do not have delete access
203 (if running under VMS) or write access (if running
204 under another OS). This will change in the future when
205 a criterion for 'delete permission' under OSs other
206 than VMS is settled. (defaults to FALSE)
210 It returns the number of files, directories and symlinks successfully
211 deleted. Symlinks are simply deleted and not followed.
213 Note also that the occurrence of errors in C<rmtree> using the
214 traditional interface can be determined I<only> by trapping diagnostic
215 messages using C<$SIG{__WARN__}>; it is not apparent from the return
216 value. (The modern interface may use the C<error> parameter to
217 record any problems encountered.
219 =head2 ERROR HANDLING
221 If C<mkpath> or C<rmtree> encounter an error, a diagnostic message
222 will be printed to C<STDERR> via C<carp> (for non-fatal errors),
223 or via C<croak> (for fatal errors).
225 If this behaviour is not desirable, the C<error> attribute may be
226 used to hold a reference to a variable, which will be used to store
227 the diagnostics. The result is a reference to a list of hash
228 references. For each hash reference, the key is the name of the
229 file, and the value is the error message (usually the contents of
230 C<$!>). An example usage looks like:
232 rmpath( 'foo/bar', 'bar/rat', {error => \my $err} );
233 for my $diag (@$err) {
234 my ($file, $message) = each %$diag;
235 print "problem unlinking $file: $message\n";
238 If no errors are encountered, C<$err> will point to an empty list
239 (thus there is no need to test for C<undef>). If a general error
240 is encountered (for instance, C<rmtree> attempts to remove a directory
241 tree that does not exist), the diagnostic key will be empty, only
242 the value will be set:
244 rmpath( '/no/such/path', {error => \my $err} );
245 for my $diag (@$err) {
246 my ($file, $message) = each %$diag;
248 print "general error: $message\n";
256 The functions detect (as far as possible) which way they are being
257 called and will act appropriately. It is important to remember that
258 the heuristic for detecting the old style is either the presence
259 of an array reference, or two or three parameters total and second
260 and third parameters are numeric. Hence...
262 mkpath '486', '487', '488';
264 ... will not assume the modern style and create three directories, rather
265 it will create one directory verbosely, setting the permission to
266 0750 (488 being the decimal equivalent of octal 750). Here, old
267 style trumps new. It must, for backwards compatibility reasons.
269 If you want to ensure there is absolutely no ambiguity about which
270 way the function will behave, make sure the first parameter is a
271 reference to a one-element list, to force the old style interpretation:
273 mkpath ['486'], '487', '488';
275 and get only one directory created. Or add a reference to an empty
276 parameter hash, to force the new style:
278 mkpath '486', '487', '488', {};
280 ... and hence create the three directories. If the empty hash
281 reference seems a little strange to your eyes, or you suspect a
282 subsequent programmer might I<helpfully> optimise it away, you
283 can add a parameter set to a default value:
285 mkpath '486', '487', '488', {verbose => 0};
287 =head3 RACE CONDITIONS
289 There are race conditions internal to the implementation of C<rmtree>
290 making it unsafe to use on directory trees which may be altered or
291 moved while C<rmtree> is running, and in particular on any directory
292 trees with any path components or subdirectories potentially writable
295 Additionally, if the C<skip_others> parameter is not set (or the
296 third parameter in the traditional inferface is not TRUE) and
297 C<rmtree> is interrupted, it may leave files and directories with
298 permissions altered to allow deletion.
300 C<File::Path> blindly exports C<mkpath> and C<rmtree> into the
301 current namespace. These days, this is considered bad style, but
302 to change it now would break too much code. Nonetheless, you are
303 invited to specify what it is you are expecting to use:
305 use File::Path 'rmtree';
313 On Windows, if C<mkpath> gives you the warning: B<No such file or
314 directory>, this may mean that you've exceeded your filesystem's
327 When removing directory trees, if you want to examine each file
328 before deciding whether to deleting it (and possibly leaving large
329 swathes alone), F<File::Find::Rule> offers a convenient and flexible
336 Please report all bugs on the RT queue:
338 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Path>
342 Tim Bunce <F<Tim.Bunce@ig.co.uk>> and
343 Charles Bailey <F<bailey@newman.upenn.edu>>.
345 Currently maintained by David Landgren <F<david@landgren.net>>.
349 This module is copyright (C) Charles Bailey, Tim Bunce and
350 David Landgren 1995-2007. All rights reserved.
354 This library is free software; you can redistribute it and/or modify
355 it under the same terms as Perl itself.
362 use File::Basename ();
366 # can't say 'opendir my $dh, $dirname'
367 # need to initialise $dh
373 use vars qw($VERSION @ISA @EXPORT);
374 $VERSION = '2.00_02';
376 @EXPORT = qw(mkpath rmtree);
378 my $Is_VMS = $^O eq 'VMS';
379 my $Is_MacOS = $^O eq 'MacOS';
381 # These OSes complain if you want to remove a file that you have no
382 # write permission to:
383 my $Force_Writeable = ($^O eq 'os2' || $^O eq 'dos' || $^O eq 'MSWin32' ||
384 $^O eq 'amigaos' || $^O eq 'MacOS' || $^O eq 'epoc');
398 UNIVERSAL::isa($_[0],'ARRAY')
399 or (@_ == 2 and $_[1] =~ /\A\d+\z/)
400 or (@_ == 3 and $_[1] =~ /\A\d+\z/ and $_[2] =~ /\A\d+\z/)
407 if (@_ > 0 and UNIVERSAL::isa($_[-1], 'HASH')) {
409 exists $arg->{mask} and $arg->{mode} = delete $arg->{mask};
410 $arg->{mode} = 0777 unless exists $arg->{mode};
411 ${$arg->{error}} = [] if exists $arg->{error};
414 @{$arg}{qw(verbose mode)} = (0, 0777);
419 my ($verbose, $mode);
420 ($paths, $verbose, $mode) = @_;
421 $paths = [$paths] unless UNIVERSAL::isa($paths,'ARRAY');
422 $arg->{verbose} = defined $verbose ? $verbose : 0;
423 $arg->{mode} = defined $mode ? $mode : 0777;
425 return _mkpath($arg, $paths);
432 local($")=$Is_MacOS ? ":" : "/";
434 foreach $path (@$paths) {
435 next unless length($path);
436 $path .= '/' if $^O eq 'os2' and $path =~ /^\w:\z/s; # feature of CRT
437 # Logic wants Unix paths, so go with the flow.
439 next if $path eq '/';
440 $path = VMS::Filespec::unixify($path);
443 my $parent = File::Basename::dirname($path);
444 unless (-d $parent or $path eq $parent) {
445 push(@created,_mkpath($arg, [$parent]));
447 print "mkdir $path\n" if $arg->{verbose};
448 if (mkdir($path,$arg->{mode})) {
449 push(@created, $path);
453 my ($e, $e1) = ($save_bang, $^E);
454 $e .= "; $e1" if $e ne $e1;
455 # allow for another process to have created it meanwhile
459 push @{${$arg->{error}}}, {$path => $e};
462 _croak("mkdir $path: $e");
472 UNIVERSAL::isa($_[0],'ARRAY')
473 or (@_ == 2 and $_[1] =~ /\A\d+\z/)
474 or (@_ == 3 and $_[1] =~ /\A\d+\z/ and $_[2] =~ /\A\d+\z/)
481 if (@_ > 0 and UNIVERSAL::isa($_[-1],'HASH')) {
483 ${$arg->{error}} = [] if exists $arg->{error};
484 ${$arg->{result}} = [] if exists $arg->{result};
487 @{$arg}{qw(verbose safe)} = (0, 0);
493 my ($verbose, $safe);
494 ($paths, $verbose, $safe) = @_;
495 $arg->{verbose} = defined $verbose ? $verbose : 0;
496 $arg->{safe} = defined $safe ? $safe : 0;
498 if (defined($paths) and length($paths)) {
499 $paths = [$paths] unless UNIVERSAL::isa($paths,'ARRAY');
503 push @{${$arg->{error}}}, {'' => "No root path(s) specified"};
506 _carp ("No root path(s) specified\n");
511 return _rmtree($arg, $paths);
519 foreach $root (@{$paths}) {
521 $root = ":$root" if $root !~ /:/;
522 $root =~ s/([^:])\z/$1:/;
527 my $rp = (lstat $root)[2] or next;
528 $rp &= 07777; # don't forget setuid, setgid, sticky bits
530 # notabene: 0700 is for making readable in the first place,
531 # it's also intended to change it to writable in case we have
532 # to recurse in which case we are better than rm -rf for
533 # subtrees with strange permissions
534 if (!chmod($rp | 0700,
535 ($Is_VMS ? VMS::Filespec::fileify($root) : $root))
539 push @{${$arg->{error}}},
540 {$root => "Can't make directory read+writeable: $!"};
543 _carp ("Can't make directory $root read+writeable: $!");
549 $d = gensym() if $] < 5.006;
550 if (!opendir $d, $root) {
552 push @{${$arg->{error}}}, {$root => "opendir: $!"};
555 _carp ("Can't read $root: $!");
561 if (!defined ${"\cTAINT"} or ${"\cTAINT"}) {
562 # Blindly untaint dir names if taint mode is
563 # active, or any perl < 5.006
564 @files = map { /\A(.*)\z/s; $1 } readdir $d;
572 # Deleting large numbers of files from VMS Files-11 filesystems
573 # is faster if done in reverse ASCIIbetical order
574 @files = reverse @files if $Is_VMS;
575 ($root = VMS::Filespec::unixify($root)) =~ s#\.dir\z## if $Is_VMS;
577 @files = map("$root$_", @files);
580 my $updir = File::Spec->updir();
581 my $curdir = File::Spec->curdir();
582 @files = map(File::Spec->catfile($root,$_),
583 grep {$_ ne $updir and $_ ne $curdir}
588 $count += _rmtree($arg, \@files);
590 if ($arg->{depth} or !$arg->{keep_root}) {
592 ($Is_VMS ? !&VMS::Filespec::candelete($root) : !-w $root)) {
593 print "skipped $root\n" if $arg->{verbose};
596 if (!chmod $rp | 0700, $root) {
597 if ($Force_Writeable) {
599 push @{${$arg->{error}}},
600 {$root => "Can't make directory writeable: $!"};
603 _carp ("Can't make directory $root writeable: $!")
607 print "rmdir $root\n" if $arg->{verbose};
609 push @{${$arg->{result}}}, $root if $arg->{result};
614 push @{${$arg->{error}}}, {$root => "rmdir: $!"};
617 _carp ("Can't remove directory $root: $!");
620 ($Is_VMS ? VMS::Filespec::fileify($root) : $root))
622 my $mask = sprintf("0%o",$rp);
624 push @{${$arg->{error}}}, {$root => "restore chmod: $!"};
627 _carp("and can't restore permissions to $mask\n");
635 ($Is_VMS ? !&VMS::Filespec::candelete($root)
636 : !(-l $root || -w $root)))
638 print "skipped $root\n" if $arg->{verbose};
641 if (!chmod $rp | 0600, $root) {
642 if ($Force_Writeable) {
644 push @{${$arg->{error}}},
645 {$root => "Can't make file writeable: $!"};
648 _carp ("Can't make file $root writeable: $!")
652 print "unlink $root\n" if $arg->{verbose};
653 # delete all versions under VMS
656 push @{${$arg->{result}}}, $root if $arg->{result};
660 push @{${$arg->{error}}},
661 {$root => "unlink: $!"};
664 _carp ("Can't unlink file $root: $!");
666 if ($Force_Writeable) {
667 if (!chmod $rp, $root) {
668 my $mask = sprintf("0%o",$rp);
670 push @{${$arg->{error}}}, {$root => "restore chmod: $!"};
673 _carp("and can't restore permissions to $mask\n");
680 last unless $Is_VMS && lstat $root;