13 # can't say 'opendir my $dh, $dirname'
14 # need to initialise $dh
20 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
22 $VERSION = eval $VERSION;
24 @EXPORT = qw(mkpath rmtree);
25 @EXPORT_OK = qw(make_path remove_tree);
28 for (qw(VMS MacOS MSWin32 os2)) {
30 *{"_IS_\U$_"} = $^O eq $_ ? sub () { 1 } : sub () { 0 };
33 # These OSes complain if you want to remove a file that you have no
34 # write permission to:
36 grep { $^O eq $_ } qw(amigaos dos epoc MSWin32 MacOS os2)
37 ) ? sub () { 1 } : sub () { 0 };
39 # Unix-like systems need to stat each directory in order to detect
40 # race condition. MS-Windows is immune to this particular attack.
41 *_NEED_STAT_CHECK = !(_IS_MSWIN32()) ? sub () { 1 } : sub () { 0 };
59 if ( $arg->{error} ) {
60 $object = '' unless defined $object;
61 $message .= ": $!" if $!;
62 push @{ ${ $arg->{error} } }, { $object => $message };
65 _carp( defined($object) ? "$message for $object: $!" : "$message: $!" );
72 # If client code blessed an array ref to HASH, this will not work
73 # properly. We could have done $arg->isa() wrapped in eval, but
74 # that would be expensive. This implementation should suffice.
75 # We could have also used Scalar::Util:blessed, but we choose not
76 # to add this dependency
77 return ( ref $arg eq 'HASH' );
81 push @_, {} unless @_ and __is_arg( $_[-1] );
86 my $old_style = !( @_ and __is_arg( $_[-1] ) );
92 my ( $verbose, $mode );
93 ( $paths, $verbose, $mode ) = @_;
94 $paths = [$paths] unless UNIVERSAL::isa( $paths, 'ARRAY' );
95 $arg->{verbose} = $verbose;
96 $arg->{mode} = defined $mode ? $mode : oct '777';
99 my %args_permitted = map { $_ => 1 } ( qw|
112 for my $k (sort keys %{$arg}) {
113 push @bad_args, $k unless $args_permitted{$k};
115 _carp("Unrecognized option(s) passed to make_path(): @bad_args")
117 $arg->{mode} = delete $arg->{mask} if exists $arg->{mask};
118 $arg->{mode} = oct '777' unless exists $arg->{mode};
119 ${ $arg->{error} } = [] if exists $arg->{error};
120 $arg->{owner} = delete $arg->{user} if exists $arg->{user};
121 $arg->{owner} = delete $arg->{uid} if exists $arg->{uid};
122 if ( exists $arg->{owner} and $arg->{owner} =~ /\D/ ) {
123 my $uid = ( getpwnam $arg->{owner} )[2];
124 if ( defined $uid ) {
125 $arg->{owner} = $uid;
129 "unable to map $arg->{owner} to a uid, ownership not changed"
131 delete $arg->{owner};
134 if ( exists $arg->{group} and $arg->{group} =~ /\D/ ) {
135 my $gid = ( getgrnam $arg->{group} )[2];
136 if ( defined $gid ) {
137 $arg->{group} = $gid;
141 "unable to map $arg->{group} to a gid, group ownership not changed"
143 delete $arg->{group};
146 if ( exists $arg->{owner} and not exists $arg->{group} ) {
147 $arg->{group} = -1; # chown will leave group unchanged
149 if ( exists $arg->{group} and not exists $arg->{owner} ) {
150 $arg->{owner} = -1; # chown will leave owner unchanged
154 return _mkpath( $arg, $paths );
162 foreach my $path ( @{$paths} ) {
163 next unless defined($path) and length($path);
164 $path .= '/' if _IS_OS2 and $path =~ /^\w:\z/s; # feature of CRT
166 # Logic wants Unix paths, so go with the flow.
168 next if $path eq '/';
169 $path = VMS::Filespec::unixify($path);
172 my $parent = File::Basename::dirname($path);
173 unless ( -d $parent or $path eq $parent ) {
174 push( @created, _mkpath( $arg, [$parent] ) );
176 print "mkdir $path\n" if $arg->{verbose};
177 if ( mkdir( $path, $arg->{mode} ) ) {
178 push( @created, $path );
179 if ( exists $arg->{owner} ) {
181 # NB: $arg->{group} guaranteed to be set during initialisation
182 if ( !chown $arg->{owner}, $arg->{group}, $path ) {
184 "Cannot change ownership of $path to $arg->{owner}:$arg->{group}"
188 if ( exists $arg->{chmod} ) {
189 if ( !chmod $arg->{chmod}, $path ) {
191 "Cannot change permissions of $path to $arg->{chmod}" );
197 my ( $e, $e1 ) = ( $save_bang, $^E );
198 $e .= "; $e1" if $e ne $e1;
200 # allow for another process to have created it meanwhile
203 if ( $arg->{error} ) {
204 push @{ ${ $arg->{error} } }, { $path => $e };
207 _croak("mkdir $path: $e");
216 push @_, {} unless @_ and __is_arg( $_[-1] );
221 my ( $dir, $test ) = @_;
223 my ( $dv, $dd ) = File::Spec->splitpath( $dir, 1 );
224 my ( $tv, $td ) = File::Spec->splitpath( $test, 1 );
227 return 0 if $dv ne $tv;
229 my @d = File::Spec->splitdir($dd);
230 my @t = File::Spec->splitdir($td);
232 # @t can't be a subdir if it's shorter than @d
235 return join( '/', @d ) eq join( '/', splice @t, 0, +@d );
239 my $old_style = !( @_ and __is_arg( $_[-1] ) );
245 my ( $verbose, $safe );
246 ( $paths, $verbose, $safe ) = @_;
247 $arg->{verbose} = $verbose;
248 $arg->{safe} = defined $safe ? $safe : 0;
250 if ( defined($paths) and length($paths) ) {
251 $paths = [$paths] unless UNIVERSAL::isa( $paths, 'ARRAY' );
254 _carp("No root path(s) specified\n");
259 my %args_permitted = map { $_ => 1 } ( qw|
268 for my $k (sort keys %{$arg}) {
269 push @bad_args, $k unless $args_permitted{$k};
271 _carp("Unrecognized option(s) passed to remove_tree(): @bad_args")
273 ${ $arg->{error} } = [] if exists $arg->{error};
274 ${ $arg->{result} } = [] if exists $arg->{result};
282 $arg->{cwd} = getcwd() or do {
283 _error( $arg, "cannot fetch initial working directory" );
286 for ( $arg->{cwd} ) { /\A(.*)\Z/s; $_ = $1 } # untaint
288 for my $p (@$paths) {
290 # need to fixup case and map \ to / on Windows
291 my $ortho_root = _IS_MSWIN32 ? _slash_lc($p) : $p;
293 _IS_MSWIN32 ? _slash_lc( $arg->{cwd} ) : $arg->{cwd};
294 my $ortho_root_length = length($ortho_root);
295 $ortho_root_length-- if _IS_VMS; # don't compare '.' with ']'
296 if ( $ortho_root_length && _is_subdir( $ortho_root, $ortho_cwd ) ) {
298 _error( $arg, "cannot remove path when cwd is $arg->{cwd}", $p );
303 $p = ":$p" unless $p =~ /:/;
304 $p .= ":" unless $p =~ /:\z/;
306 elsif ( _IS_MSWIN32 ) {
312 push @clean_path, $p;
315 @{$arg}{qw(device inode perm)} = ( lstat $arg->{cwd} )[ 0, 1 ] or do {
316 _error( $arg, "cannot stat initial working directory", $arg->{cwd} );
320 return _rmtree( $arg, \@clean_path );
328 my $curdir = File::Spec->curdir();
329 my $updir = File::Spec->updir();
331 my ( @files, $root );
333 foreach my $root (@$paths) {
335 # since we chdir into each directory, it may not be obvious
336 # to figure out where we are if we generate a message about
337 # a file name. We therefore construct a semi-canonical
338 # filename, anchored from the directory being unlinked (as
339 # opposed to being truly canonical, anchored from the root (/).
343 ? File::Spec->catfile( $arg->{prefix}, $root )
346 my ( $ldev, $lino, $perm ) = ( lstat $root )[ 0, 1, 2 ]
350 $root = VMS::Filespec::vmspath( VMS::Filespec::pathify($root) )
353 if ( !chdir($root) ) {
355 # see if we can escalate privileges to get in
356 # (e.g. funny protection mask such as -w- instead of rwx)
358 my $nperm = $perm | oct '700';
363 or chmod( $nperm, $root )
368 "cannot make child directory read-write-exec", $canon );
371 elsif ( !chdir($root) ) {
372 _error( $arg, "cannot chdir to child", $canon );
377 my ( $cur_dev, $cur_inode, $perm ) = ( stat $curdir )[ 0, 1, 2 ]
379 _error( $arg, "cannot stat current working directory", $canon );
383 if (_NEED_STAT_CHECK) {
384 ( $ldev eq $cur_dev and $lino eq $cur_inode )
386 "directory $canon changed before chdir, expected dev=$ldev ino=$lino, actual dev=$cur_dev ino=$cur_inode, aborting."
390 $perm &= oct '7777'; # don't forget setuid, setgid, sticky bits
391 my $nperm = $perm | oct '700';
393 # notabene: 0700 is for making readable in the first place,
394 # it's also intended to change it to writable in case we have
395 # to recurse in which case we are better than rm -rf for
396 # subtrees with strange permissions
402 or chmod( $nperm, $curdir )
406 _error( $arg, "cannot make directory read+writeable", $canon );
411 $d = gensym() if $] < 5.006;
412 if ( !opendir $d, $curdir ) {
413 _error( $arg, "cannot opendir", $canon );
417 if ( !defined ${^TAINT} or ${^TAINT} ) {
418 # Blindly untaint dir names if taint mode is active
419 @files = map { /\A(.*)\z/s; $1 } readdir $d;
429 # Deleting large numbers of files from VMS Files-11
430 # filesystems is faster if done in reverse ASCIIbetical order.
431 # include '.' to '.;' from blead patch #31775
432 @files = map { $_ eq '.' ? '.;' : $_ } reverse @files;
435 @files = grep { $_ ne $updir and $_ ne $curdir } @files;
439 # remove the contained files before the directory itself
441 @{$narg}{qw(device inode cwd prefix depth)} =
442 ( $cur_dev, $cur_inode, $updir, $canon, $arg->{depth} + 1 );
443 $count += _rmtree( $narg, \@files );
446 # restore directory permissions of required now (in case the rmdir
447 # below fails), while we are still in the directory and may do so
448 # without a race via '.'
449 if ( $nperm != $perm and not chmod( $perm, $curdir ) ) {
450 _error( $arg, "cannot reset chmod", $canon );
453 # don't leave the client code in an unexpected directory
456 _croak("cannot chdir to $arg->{cwd} from $canon: $!, aborting.");
458 # ensure that a chdir upwards didn't take us somewhere other
459 # than we expected (see CVE-2002-0435)
460 ( $cur_dev, $cur_inode ) = ( stat $curdir )[ 0, 1 ]
462 "cannot stat prior working directory $arg->{cwd}: $!, aborting."
465 if (_NEED_STAT_CHECK) {
466 ( $arg->{device} eq $cur_dev and $arg->{inode} eq $cur_inode )
467 or _croak( "previous directory $arg->{cwd} "
468 . "changed before entering $canon, "
469 . "expected dev=$ldev ino=$lino, "
470 . "actual dev=$cur_dev ino=$cur_inode, aborting."
474 if ( $arg->{depth} or !$arg->{keep_root} ) {
477 ? !&VMS::Filespec::candelete($root)
480 print "skipped $root\n" if $arg->{verbose};
483 if ( _FORCE_WRITABLE and !chmod $perm | oct '700', $root ) {
484 _error( $arg, "cannot make directory writeable", $canon );
486 print "rmdir $root\n" if $arg->{verbose};
488 push @{ ${ $arg->{result} } }, $root if $arg->{result};
492 _error( $arg, "cannot remove directory", $canon );
496 ( _IS_VMS ? VMS::Filespec::fileify($root) : $root )
502 sprintf( "cannot restore permissions to 0%o",
512 $root = VMS::Filespec::vmsify("./$root")
514 && !File::Spec->file_name_is_absolute($root)
515 && ( $root !~ m/(?<!\^)[\]>]+/ ); # not already in VMS syntax
521 ? !&VMS::Filespec::candelete($root)
522 : !( -l $root || -w $root )
526 print "skipped $root\n" if $arg->{verbose};
530 my $nperm = $perm & oct '7777' | oct '600';
533 and not chmod $nperm, $root )
535 _error( $arg, "cannot make file writeable", $canon );
537 print "unlink $canon\n" if $arg->{verbose};
539 # delete all versions under VMS
541 if ( unlink $root ) {
542 push @{ ${ $arg->{result} } }, $root if $arg->{result};
545 _error( $arg, "cannot unlink file", $canon );
546 _FORCE_WRITABLE and chmod( $perm, $root )
548 sprintf( "cannot restore permissions to 0%o", $perm ),
553 last unless _IS_VMS && lstat $root;
562 # fix up slashes and case on MSWin32 so that we can determine that
563 # c:\path\to\dir is underneath C:/Path/To
575 File::Path - Create or remove directory trees
579 This document describes version 2.12 of File::Path.
583 use File::Path qw(make_path remove_tree);
585 @created = make_path('foo/bar/baz', '/zug/zwang');
586 @created = make_path('foo/bar/baz', '/zug/zwang', {
590 make_path('foo/bar/baz', '/zug/zwang', {
594 $removed_count = remove_tree('foo/bar/baz', '/zug/zwang');
595 $removed_count = remove_tree('foo/bar/baz', '/zug/zwang', {
597 error => \my $err_list,
600 # legacy (interface promoted before v2.00)
601 @created = mkpath('/foo/bar/baz');
602 @created = mkpath('/foo/bar/baz', 1, 0711);
603 @created = mkpath(['/foo/bar/baz', 'blurfl/quux'], 1, 0711);
604 $removed_count = rmtree('foo/bar/baz', 1, 1);
605 $removed_count = rmtree(['foo/bar/baz', 'blurfl/quux'], 1, 1);
607 # legacy (interface promoted before v2.06)
608 @created = mkpath('foo/bar/baz', '/zug/zwang', { verbose => 1, mode => 0711 });
609 $removed_count = rmtree('foo/bar/baz', '/zug/zwang', { verbose => 1, mode => 0711 });
613 This module provide a convenient way to create directories of
614 arbitrary depth and to delete an entire directory subtree from the
617 The following functions are provided:
621 =item make_path( $dir1, $dir2, .... )
623 =item make_path( $dir1, $dir2, ...., \%opts )
625 The C<make_path> function creates the given directories if they don't
626 exists before, much like the Unix command C<mkdir -p>.
628 The function accepts a list of directories to be created. Its
629 behaviour may be tuned by an optional hashref appearing as the last
630 parameter on the call.
632 The function returns the list of directories actually created during
633 the call; in scalar context the number of directories created.
635 The following keys are recognised in the option hash:
641 The numeric permissions mode to apply to each created directory
642 (defaults to 0777), to be modified by the current C<umask>. If the
643 directory already exists (and thus does not need to be created),
644 the permissions will not be modified.
646 C<mask> is recognised as an alias for this parameter.
650 Takes a numeric mode to apply to each created directory (not
651 modified by the current C<umask>). If the directory already exists
652 (and thus does not need to be created), the permissions will
655 =item verbose => $bool
657 If present, will cause C<make_path> to print the name of each directory
658 as it is created. By default nothing is printed.
662 If present, it should be a reference to a scalar.
663 This scalar will be made to reference an array, which will
664 be used to store any errors that are encountered. See the L</"ERROR
665 HANDLING"> section for more information.
667 If this parameter is not used, certain error conditions may raise
668 a fatal error that will cause the program to halt, unless trapped
671 =item owner => $owner
677 If present, will cause any created directory to be owned by C<$owner>.
678 If the value is numeric, it will be interpreted as a uid, otherwise
679 as username is assumed. An error will be issued if the username cannot be
680 mapped to a uid, or the uid does not exist, or the process lacks the
681 privileges to change ownership.
683 Ownership of directories that already exist will not be changed.
685 C<user> and C<uid> are aliases of C<owner>.
687 =item group => $group
689 If present, will cause any created directory to be owned by the group C<$group>.
690 If the value is numeric, it will be interpreted as a gid, otherwise
691 as group name is assumed. An error will be issued if the group name cannot be
692 mapped to a gid, or the gid does not exist, or the process lacks the
693 privileges to change group ownership.
695 Group ownership of directories that already exist will not be changed.
697 make_path '/var/tmp/webcache', {owner=>'nobody', group=>'nogroup'};
703 =item mkpath( $dir, $verbose, $mode )
705 =item mkpath( [$dir1, $dir2,...], $verbose, $mode )
707 =item mkpath( $dir1, $dir2,..., \%opt )
709 The mkpath() function provide the legacy interface of make_path() with
710 a different interpretation of the arguments passed. The behaviour and
711 return value of the function is otherwise identical to make_path().
713 =item remove_tree( $dir1, $dir2, .... )
715 =item remove_tree( $dir1, $dir2, ...., \%opts )
717 The C<remove_tree> function deletes the given directories and any
718 files and subdirectories they might contain, much like the Unix
719 command C<rm -r> or the Windows commands C<rmdir /s> and C<rd /s>. The
720 only exception to the function similarity is C<remove_tree> accepts
721 only directories whereas C<rm -r> also accepts files.
723 The function accepts a list of directories to be
724 removed. Its behaviour may be tuned by an optional hashref
725 appearing as the last parameter on the call. If an empty string is
726 passed to C<remove_tree>, an error will occur.
728 The functions returns the number of files successfully deleted.
730 The following keys are recognised in the option hash:
734 =item verbose => $bool
736 If present, will cause C<remove_tree> to print the name of each file as
737 it is unlinked. By default nothing is printed.
741 When set to a true value, will cause C<remove_tree> to skip the files
742 for which the process lacks the required privileges needed to delete
743 files, such as delete privileges on VMS. In other words, the code
744 will make no attempt to alter file permissions. Thus, if the process
745 is interrupted, no filesystem object will be left in a more
748 =item keep_root => $bool
750 When set to a true value, will cause all files and subdirectories
751 to be removed, except the initially specified directories. This comes
752 in handy when cleaning out an application's scratch directory.
754 remove_tree( '/tmp', {keep_root => 1} );
756 =item result => \$res
758 If present, it should be a reference to a scalar.
759 This scalar will be made to reference an array, which will
760 be used to store all files and directories unlinked
761 during the call. If nothing is unlinked, the array will be empty.
763 remove_tree( '/tmp', {result => \my $list} );
764 print "unlinked $_\n" for @$list;
766 This is a useful alternative to the C<verbose> key.
770 If present, it should be a reference to a scalar.
771 This scalar will be made to reference an array, which will
772 be used to store any errors that are encountered. See the L</"ERROR
773 HANDLING"> section for more information.
775 Removing things is a much more dangerous proposition than
776 creating things. As such, there are certain conditions that
777 C<remove_tree> may encounter that are so dangerous that the only
778 sane action left is to kill the program.
780 Use C<error> to trap all that is reasonable (problems with
781 permissions and the like), and let it die if things get out
782 of hand. This is the safest course of action.
788 =item rmtree( $dir, $verbose, $safe )
790 =item rmtree( [$dir1, $dir2,...], $verbose, $safe )
792 =item rmtree( $dir1, $dir2,..., \%opt )
794 The rmtree() function provide the legacy interface of remove_tree()
795 with a different interpretation of the arguments passed. The behaviour
796 and return value of the function is otherwise identical to
801 =head2 ERROR HANDLING
807 The following error handling mechanism is consistent throughout all
808 code paths EXCEPT in cases where the ROOT node is nonexistent. In
809 version 2.11 the maintainers attempted to rectify this inconsistency
810 but too many downstream modules encountered problems. In such case,
811 if you require root node evaluation or error checking prior to calling
812 C<make_path> or C<remove_tree>, you should take additional precautions.
816 If C<make_path> or C<remove_tree> encounter an error, a diagnostic
817 message will be printed to C<STDERR> via C<carp> (for non-fatal
818 errors), or via C<croak> (for fatal errors).
820 If this behaviour is not desirable, the C<error> attribute may be
821 used to hold a reference to a variable, which will be used to store
822 the diagnostics. The variable is made a reference to an array of hash
823 references. Each hash contain a single key/value pair where the key
824 is the name of the file, and the value is the error message (including
825 the contents of C<$!> when appropriate). If a general error is
826 encountered the diagnostic key will be empty.
828 An example usage looks like:
830 remove_tree( 'foo/bar', 'bar/rat', {error => \my $err} );
832 for my $diag (@$err) {
833 my ($file, $message) = %$diag;
835 print "general error: $message\n";
838 print "problem unlinking $file: $message\n";
843 print "No error encountered\n";
846 Note that if no errors are encountered, C<$err> will reference an
847 empty array. This means that C<$err> will always end up TRUE; so you
848 need to test C<@$err> to determine if errors occurred.
852 C<File::Path> blindly exports C<mkpath> and C<rmtree> into the
853 current namespace. These days, this is considered bad style, but
854 to change it now would break too much code. Nonetheless, you are
855 invited to specify what it is you are expecting to use:
857 use File::Path 'rmtree';
859 The routines C<make_path> and C<remove_tree> are B<not> exported
860 by default. You must specify which ones you want to use.
862 use File::Path 'remove_tree';
864 Note that a side-effect of the above is that C<mkpath> and C<rmtree>
865 are no longer exported at all. This is due to the way the C<Exporter>
866 module works. If you are migrating a codebase to use the new
867 interface, you will have to list everything explicitly. But that's
868 just good practice anyway.
870 use File::Path qw(remove_tree rmtree);
874 The API was changed in the 2.0 branch. For a time, C<mkpath> and
875 C<rmtree> tried, unsuccessfully, to deal with the two different
876 calling mechanisms. This approach was considered a failure.
878 The new semantics are now only available with C<make_path> and
879 C<remove_tree>. The old semantics are only available through
880 C<mkpath> and C<rmtree>. Users are strongly encouraged to upgrade
881 to at least 2.08 in order to avoid surprises.
883 =head3 SECURITY CONSIDERATIONS
885 There were race conditions 1.x implementations of File::Path's
886 C<rmtree> function (although sometimes patched depending on the OS
887 distribution or platform). The 2.0 version contains code to avoid the
888 problem mentioned in CVE-2002-0435.
890 See the following pages for more information:
892 http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=286905
893 http://www.nntp.perl.org/group/perl.perl5.porters/2005/01/msg97623.html
894 http://www.debian.org/security/2005/dsa-696
896 Additionally, unless the C<safe> parameter is set (or the
897 third parameter in the traditional interface is TRUE), should a
898 C<remove_tree> be interrupted, files that were originally in read-only
899 mode may now have their permissions set to a read-write (or "delete
904 FATAL errors will cause the program to halt (C<croak>), since the
905 problem is so severe that it would be dangerous to continue. (This
906 can always be trapped with C<eval>, but it's not a good idea. Under
907 the circumstances, dying is the best thing to do).
909 SEVERE errors may be trapped using the modern interface. If the
910 they are not trapped, or the old interface is used, such an error
911 will cause the program will halt.
913 All other errors may be trapped using the modern interface, otherwise
914 they will be C<carp>ed about. Program execution will not be halted.
918 =item mkdir [path]: [errmsg] (SEVERE)
920 C<make_path> was unable to create the path. Probably some sort of
921 permissions error at the point of departure, or insufficient resources
922 (such as free inodes on Unix).
924 =item No root path(s) specified
926 C<make_path> was not given any paths to create. This message is only
927 emitted if the routine is called with the traditional interface.
928 The modern interface will remain silent if given nothing to do.
930 =item No such file or directory
932 On Windows, if C<make_path> gives you this warning, it may mean that
933 you have exceeded your filesystem's maximum path length.
935 =item cannot fetch initial working directory: [errmsg]
937 C<remove_tree> attempted to determine the initial directory by calling
938 C<Cwd::getcwd>, but the call failed for some reason. No attempt
939 will be made to delete anything.
941 =item cannot stat initial working directory: [errmsg]
943 C<remove_tree> attempted to stat the initial directory (after having
944 successfully obtained its name via C<getcwd>), however, the call
945 failed for some reason. No attempt will be made to delete anything.
947 =item cannot chdir to [dir]: [errmsg]
949 C<remove_tree> attempted to set the working directory in order to
950 begin deleting the objects therein, but was unsuccessful. This is
951 usually a permissions issue. The routine will continue to delete
952 other things, but this directory will be left intact.
954 =item directory [dir] changed before chdir, expected dev=[n] ino=[n], actual dev=[n] ino=[n], aborting. (FATAL)
956 C<remove_tree> recorded the device and inode of a directory, and then
957 moved into it. It then performed a C<stat> on the current directory
958 and detected that the device and inode were no longer the same. As
959 this is at the heart of the race condition problem, the program
960 will die at this point.
962 =item cannot make directory [dir] read+writeable: [errmsg]
964 C<remove_tree> attempted to change the permissions on the current directory
965 to ensure that subsequent unlinkings would not run into problems,
966 but was unable to do so. The permissions remain as they were, and
967 the program will carry on, doing the best it can.
969 =item cannot read [dir]: [errmsg]
971 C<remove_tree> tried to read the contents of the directory in order
972 to acquire the names of the directory entries to be unlinked, but
973 was unsuccessful. This is usually a permissions issue. The
974 program will continue, but the files in this directory will remain
977 =item cannot reset chmod [dir]: [errmsg]
979 C<remove_tree>, after having deleted everything in a directory, attempted
980 to restore its permissions to the original state but failed. The
981 directory may wind up being left behind.
983 =item cannot remove [dir] when cwd is [dir]
985 The current working directory of the program is F</some/path/to/here>
986 and you are attempting to remove an ancestor, such as F</some/path>.
987 The directory tree is left untouched.
989 The solution is to C<chdir> out of the child directory to a place
990 outside the directory tree to be removed.
992 =item cannot chdir to [parent-dir] from [child-dir]: [errmsg], aborting. (FATAL)
994 C<remove_tree>, after having deleted everything and restored the permissions
995 of a directory, was unable to chdir back to the parent. The program
996 halts to avoid a race condition from occurring.
998 =item cannot stat prior working directory [dir]: [errmsg], aborting. (FATAL)
1000 C<remove_tree> was unable to stat the parent directory after have returned
1001 from the child. Since there is no way of knowing if we returned to
1002 where we think we should be (by comparing device and inode) the only
1003 way out is to C<croak>.
1005 =item previous directory [parent-dir] changed before entering [child-dir], expected dev=[n] ino=[n], actual dev=[n] ino=[n], aborting. (FATAL)
1007 When C<remove_tree> returned from deleting files in a child directory, a
1008 check revealed that the parent directory it returned to wasn't the one
1009 it started out from. This is considered a sign of malicious activity.
1011 =item cannot make directory [dir] writeable: [errmsg]
1013 Just before removing a directory (after having successfully removed
1014 everything it contained), C<remove_tree> attempted to set the permissions
1015 on the directory to ensure it could be removed and failed. Program
1016 execution continues, but the directory may possibly not be deleted.
1018 =item cannot remove directory [dir]: [errmsg]
1020 C<remove_tree> attempted to remove a directory, but failed. This may because
1021 some objects that were unable to be removed remain in the directory, or
1022 a permissions issue. The directory will be left behind.
1024 =item cannot restore permissions of [dir] to [0nnn]: [errmsg]
1026 After having failed to remove a directory, C<remove_tree> was unable to
1027 restore its permissions from a permissive state back to a possibly
1028 more restrictive setting. (Permissions given in octal).
1030 =item cannot make file [file] writeable: [errmsg]
1032 C<remove_tree> attempted to force the permissions of a file to ensure it
1033 could be deleted, but failed to do so. It will, however, still attempt
1036 =item cannot unlink file [file]: [errmsg]
1038 C<remove_tree> failed to remove a file. Probably a permissions issue.
1040 =item cannot restore permissions of [file] to [0nnn]: [errmsg]
1042 After having failed to remove a file, C<remove_tree> was also unable
1043 to restore the permissions on the file to a possibly less permissive
1044 setting. (Permissions given in octal).
1046 =item unable to map [owner] to a uid, ownership not changed");
1048 C<make_path> was instructed to give the ownership of created
1049 directories to the symbolic name [owner], but C<getpwnam> did
1050 not return the corresponding numeric uid. The directory will
1051 be created, but ownership will not be changed.
1053 =item unable to map [group] to a gid, group ownership not changed
1055 C<make_path> was instructed to give the group ownership of created
1056 directories to the symbolic name [group], but C<getgrnam> did
1057 not return the corresponding numeric gid. The directory will
1058 be created, but group ownership will not be changed.
1070 Allows files and directories to be moved to the Trashcan/Recycle
1071 Bin (where they may later be restored if necessary) if the operating
1072 system supports such functionality. This feature may one day be
1073 made available directly in C<File::Path>.
1079 When removing directory trees, if you want to examine each file to
1080 decide whether to delete it (and possibly leaving large swathes
1081 alone), F<File::Find::Rule> offers a convenient and flexible approach
1082 to examining directory trees.
1086 =head1 BUGS AND LIMITATIONS
1088 The following describes F<File::Path> limitations and how to report bugs.
1090 =head2 MULTITHREAD APPLICATIONS
1092 F<File::Path> B<rmtree> and B<remove_tree> will not work with multithreaded
1093 applications due to its use of B<chdir>. At this time, no warning or error
1094 results and you will certainly encounter unexpected results.
1096 The implementation that surfaces this limitation may change in a future
1099 =head2 NFS Mount Points
1101 F<File::Path> is not responsible for triggering the automounts, mirror mounts,
1102 and the contents of network mounted filesystems. If your NFS implementation
1103 requires an action to be performed on the filesystem in order for
1104 F<File::Path> to perform operations, it is strongly suggested you assure
1105 filesystem availability by reading the root of the mounted filesystem.
1107 =head2 REPORTING BUGS
1109 Please report all bugs on the RT queue, either via the web interface:
1111 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Path>
1115 bug-File-Path@rt.cpan.org
1117 In either case, please B<attach> patches to the bug report rather than
1118 including them inline in the web post or the body of the email.
1120 You can also send pull requests to the Github repository:
1122 L<https://github.com/rpcme/File-Path>
1124 =head1 ACKNOWLEDGEMENTS
1126 Paul Szabo identified the race condition originally, and Brendan
1127 O'Dea wrote an implementation for Debian that addressed the problem.
1128 That code was used as a basis for the current code. Their efforts
1129 are greatly appreciated.
1131 Gisle Aas made a number of improvements to the documentation for
1132 2.07 and his advice and assistance is also greatly appreciated.
1136 Prior authors and maintainers: Tim Bunce, Charles Bailey, and
1137 David Landgren <F<david@landgren.net>>.
1139 Current maintainers are Richard Elberger <F<riche@cpan.org>> and
1140 James (Jim) Keenan <F<jkeenan@cpan.org>>.
1144 Contributors to File::Path, in alphabetical order.
1148 =item <F<bulkdd@cpan.org>>
1150 =item Craig A. Berry <F<craigberry@mac.com>>
1152 =item Richard Elberger <F<riche@cpan.org>>
1154 =item Ryan Yee <F<ryee@cpan.org>>
1156 =item Skye Shaw <F<shaw@cpan.org>>
1158 =item Tom Lutz <F<tommylutz@gmail.com>>
1164 This module is copyright (C) Charles Bailey, Tim Bunce, David Landgren,
1165 James Keenan, and Richard Elberger 1995-2015. All rights reserved.
1169 This library is free software; you can redistribute it and/or modify
1170 it under the same terms as Perl itself.