Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
6.3 KiB

  1. package File::Path;
  2. =head1 NAME
  3. File::Path - create or remove a series of directories
  4. =head1 SYNOPSIS
  5. C<use File::Path>
  6. C<mkpath(['/foo/bar/baz', 'blurfl/quux'], 1, 0711);>
  7. C<rmtree(['foo/bar/baz', 'blurfl/quux'], 1, 1);>
  8. =head1 DESCRIPTION
  9. The C<mkpath> function provides a convenient way to create directories, even
  10. if your C<mkdir> kernel call won't create more than one level of directory at
  11. a time. C<mkpath> takes three arguments:
  12. =over 4
  13. =item *
  14. the name of the path to create, or a reference
  15. to a list of paths to create,
  16. =item *
  17. a boolean value, which if TRUE will cause C<mkpath>
  18. to print the name of each directory as it is created
  19. (defaults to FALSE), and
  20. =item *
  21. the numeric mode to use when creating the directories
  22. (defaults to 0777)
  23. =back
  24. It returns a list of all directories (including intermediates, determined
  25. using the Unix '/' separator) created.
  26. Similarly, the C<rmtree> function provides a convenient way to delete a
  27. subtree from the directory structure, much like the Unix command C<rm -r>.
  28. C<rmtree> takes three arguments:
  29. =over 4
  30. =item *
  31. the root of the subtree to delete, or a reference to
  32. a list of roots. All of the files and directories
  33. below each root, as well as the roots themselves,
  34. will be deleted.
  35. =item *
  36. a boolean value, which if TRUE will cause C<rmtree> to
  37. print a message each time it examines a file, giving the
  38. name of the file, and indicating whether it's using C<rmdir>
  39. or C<unlink> to remove it, or that it's skipping it.
  40. (defaults to FALSE)
  41. =item *
  42. a boolean value, which if TRUE will cause C<rmtree> to
  43. skip any files to which you do not have delete access
  44. (if running under VMS) or write access (if running
  45. under another OS). This will change in the future when
  46. a criterion for 'delete permission' under OSs other
  47. than VMS is settled. (defaults to FALSE)
  48. =back
  49. It returns the number of files successfully deleted. Symlinks are
  50. treated as ordinary files.
  51. B<NOTE:> If the third parameter is not TRUE, C<rmtree> is B<unsecure>
  52. in the face of failure or interruption. Files and directories which
  53. were not deleted may be left with permissions reset to allow world
  54. read and write access. Note also that the occurrence of errors in
  55. rmtree can be determined I<only> by trapping diagnostic messages
  56. using C<$SIG{__WARN__}>; it is not apparent from the return value.
  57. Therefore, you must be extremely careful about using C<rmtree($foo,$bar,0>
  58. in situations where security is an issue.
  59. =head1 AUTHORS
  60. Tim Bunce <F<[email protected]>> and
  61. Charles Bailey <F<[email protected]>>
  62. =head1 REVISION
  63. Current $VERSION is 1.0401.
  64. =cut
  65. use Carp;
  66. use File::Basename ();
  67. use DirHandle ();
  68. use Exporter ();
  69. use strict;
  70. use vars qw( $VERSION @ISA @EXPORT );
  71. $VERSION = "1.0401";
  72. @ISA = qw( Exporter );
  73. @EXPORT = qw( mkpath rmtree );
  74. my $Is_VMS = $^O eq 'VMS';
  75. # These OSes complain if you want to remove a file that you have no
  76. # write permission to:
  77. my $force_writeable = ($^O eq 'os2' || $^O eq 'dos' || $^O eq 'MSWin32'
  78. || $^O eq 'amigaos');
  79. sub mkpath {
  80. my($paths, $verbose, $mode) = @_;
  81. # $paths -- either a path string or ref to list of paths
  82. # $verbose -- optional print "mkdir $path" for each directory created
  83. # $mode -- optional permissions, defaults to 0777
  84. local($")="/";
  85. $mode = 0777 unless defined($mode);
  86. $paths = [$paths] unless ref $paths;
  87. my(@created,$path);
  88. foreach $path (@$paths) {
  89. $path .= '/' if $^O eq 'os2' and $path =~ /^\w:$/; # feature of CRT
  90. next if -d $path;
  91. # Logic wants Unix paths, so go with the flow.
  92. $path = VMS::Filespec::unixify($path) if $Is_VMS;
  93. my $parent = File::Basename::dirname($path);
  94. # Allow for creation of new logical filesystems under VMS
  95. if (not $Is_VMS or $parent !~ m:/[^/]+/000000/?:) {
  96. push(@created,mkpath($parent, $verbose, $mode)) unless (-d $parent);
  97. }
  98. print "mkdir $path\n" if $verbose;
  99. unless (mkdir($path,$mode)) {
  100. # allow for another process to have created it meanwhile
  101. croak "mkdir $path: $!" unless -d $path;
  102. }
  103. push(@created, $path);
  104. }
  105. @created;
  106. }
  107. sub rmtree {
  108. my($roots, $verbose, $safe) = @_;
  109. my(@files);
  110. my($count) = 0;
  111. $roots = [$roots] unless ref $roots;
  112. $verbose ||= 0;
  113. $safe ||= 0;
  114. my($root);
  115. foreach $root (@{$roots}) {
  116. $root =~ s#/$##;
  117. (undef, undef, my $rp) = lstat $root or next;
  118. $rp &= 07777; # don't forget setuid, setgid, sticky bits
  119. if ( -d _ ) {
  120. # notabene: 0777 is for making readable in the first place,
  121. # it's also intended to change it to writable in case we have
  122. # to recurse in which case we are better than rm -rf for
  123. # subtrees with strange permissions
  124. chmod(0777, ($Is_VMS ? VMS::Filespec::fileify($root) : $root))
  125. or carp "Can't make directory $root read+writeable: $!"
  126. unless $safe;
  127. my $d = DirHandle->new($root)
  128. or carp "Can't read $root: $!";
  129. @files = $d->read;
  130. $d->close;
  131. # Deleting large numbers of files from VMS Files-11 filesystems
  132. # is faster if done in reverse ASCIIbetical order
  133. @files = reverse @files if $Is_VMS;
  134. ($root = VMS::Filespec::unixify($root)) =~ s#\.dir$## if $Is_VMS;
  135. @files = map("$root/$_", grep $_!~/^\.{1,2}$/,@files);
  136. $count += rmtree(\@files,$verbose,$safe);
  137. if ($safe &&
  138. ($Is_VMS ? !&VMS::Filespec::candelete($root) : !-w $root)) {
  139. print "skipped $root\n" if $verbose;
  140. next;
  141. }
  142. chmod 0777, $root
  143. or carp "Can't make directory $root writeable: $!"
  144. if $force_writeable;
  145. print "rmdir $root\n" if $verbose;
  146. if (rmdir $root) {
  147. ++$count;
  148. }
  149. else {
  150. carp "Can't remove directory $root: $!";
  151. chmod($rp, ($Is_VMS ? VMS::Filespec::fileify($root) : $root))
  152. or carp("and can't restore permissions to "
  153. . sprintf("0%o",$rp) . "\n");
  154. }
  155. }
  156. else {
  157. if ($safe &&
  158. ($Is_VMS ? !&VMS::Filespec::candelete($root) : !-w $root)) {
  159. print "skipped $root\n" if $verbose;
  160. next;
  161. }
  162. chmod 0666, $root
  163. or carp "Can't make file $root writeable: $!"
  164. if $force_writeable;
  165. print "unlink $root\n" if $verbose;
  166. # delete all versions under VMS
  167. for (;;) {
  168. unless (unlink $root) {
  169. carp "Can't unlink file $root: $!";
  170. if ($force_writeable) {
  171. chmod $rp, $root
  172. or carp("and can't restore permissions to "
  173. . sprintf("0%o",$rp) . "\n");
  174. }
  175. last;
  176. }
  177. ++$count;
  178. last unless $Is_VMS && lstat $root;
  179. }
  180. }
  181. }
  182. $count;
  183. }
  184. 1;