Leaked source code of windows server 2003
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.

251 lines
7.0 KiB

  1. package File::Path;
  2. =head1 NAME
  3. File::Path - create or remove directory trees
  4. =head1 SYNOPSIS
  5. use File::Path;
  6. mkpath(['/foo/bar/baz', 'blurfl/quux'], 1, 0711);
  7. 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. simply deleted and not followed.
  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. =cut
  63. use 5.005_64;
  64. use Carp;
  65. use File::Basename ();
  66. use Exporter ();
  67. use strict;
  68. our $VERSION = "1.0404";
  69. our @ISA = qw( Exporter );
  70. our @EXPORT = qw( mkpath rmtree );
  71. my $Is_VMS = $^O eq 'VMS';
  72. my $Is_MacOS = $^O eq 'MacOS';
  73. # These OSes complain if you want to remove a file that you have no
  74. # write permission to:
  75. my $force_writeable = ($^O eq 'os2' || $^O eq 'dos' || $^O eq 'MSWin32' ||
  76. $^O eq 'amigaos' || $^O eq 'MacOS' || $^O eq 'epoc');
  77. sub mkpath {
  78. my($paths, $verbose, $mode) = @_;
  79. # $paths -- either a path string or ref to list of paths
  80. # $verbose -- optional print "mkdir $path" for each directory created
  81. # $mode -- optional permissions, defaults to 0777
  82. local($")=$Is_MacOS ? ":" : "/";
  83. $mode = 0777 unless defined($mode);
  84. $paths = [$paths] unless ref $paths;
  85. my(@created,$path);
  86. foreach $path (@$paths) {
  87. $path .= '/' if $^O eq 'os2' and $path =~ /^\w:\z/s; # feature of CRT
  88. # Logic wants Unix paths, so go with the flow.
  89. if ($Is_VMS) {
  90. next if $path eq '/';
  91. $path = VMS::Filespec::unixify($path);
  92. if ($path =~ m:^(/[^/]+)/?\z:) {
  93. $path = $1.'/000000';
  94. }
  95. }
  96. next if -d $path;
  97. my $parent = File::Basename::dirname($path);
  98. unless (-d $parent or $path eq $parent) {
  99. push(@created,mkpath($parent, $verbose, $mode));
  100. }
  101. print "mkdir $path\n" if $verbose;
  102. unless (mkdir($path,$mode)) {
  103. my $e = $!;
  104. # allow for another process to have created it meanwhile
  105. croak "mkdir $path: $e" unless -d $path;
  106. }
  107. push(@created, $path);
  108. }
  109. @created;
  110. }
  111. sub rmtree {
  112. my($roots, $verbose, $safe) = @_;
  113. my(@files);
  114. my($count) = 0;
  115. $verbose ||= 0;
  116. $safe ||= 0;
  117. if ( defined($roots) && length($roots) ) {
  118. $roots = [$roots] unless ref $roots;
  119. }
  120. else {
  121. carp "No root path(s) specified\n";
  122. return 0;
  123. }
  124. my($root);
  125. foreach $root (@{$roots}) {
  126. if ($Is_MacOS) {
  127. $root = ":$root" if $root !~ /:/;
  128. $root =~ s#([^:])\z#$1:#;
  129. } else {
  130. $root =~ s#/\z##;
  131. }
  132. (undef, undef, my $rp) = lstat $root or next;
  133. $rp &= 07777; # don't forget setuid, setgid, sticky bits
  134. if ( -d _ ) {
  135. # notabene: 0777 is for making readable in the first place,
  136. # it's also intended to change it to writable in case we have
  137. # to recurse in which case we are better than rm -rf for
  138. # subtrees with strange permissions
  139. chmod(0777, ($Is_VMS ? VMS::Filespec::fileify($root) : $root))
  140. or carp "Can't make directory $root read+writeable: $!"
  141. unless $safe;
  142. if (opendir my $d, $root) {
  143. @files = readdir $d;
  144. closedir $d;
  145. }
  146. else {
  147. carp "Can't read $root: $!";
  148. @files = ();
  149. }
  150. # Deleting large numbers of files from VMS Files-11 filesystems
  151. # is faster if done in reverse ASCIIbetical order
  152. @files = reverse @files if $Is_VMS;
  153. ($root = VMS::Filespec::unixify($root)) =~ s#\.dir\z## if $Is_VMS;
  154. if ($Is_MacOS) {
  155. @files = map("$root$_", @files);
  156. } else {
  157. @files = map("$root/$_", grep $_!~/^\.{1,2}\z/s,@files);
  158. }
  159. $count += rmtree(\@files,$verbose,$safe);
  160. if ($safe &&
  161. ($Is_VMS ? !&VMS::Filespec::candelete($root) : !-w $root)) {
  162. print "skipped $root\n" if $verbose;
  163. next;
  164. }
  165. chmod 0777, $root
  166. or carp "Can't make directory $root writeable: $!"
  167. if $force_writeable;
  168. print "rmdir $root\n" if $verbose;
  169. if (rmdir $root) {
  170. ++$count;
  171. }
  172. else {
  173. carp "Can't remove directory $root: $!";
  174. chmod($rp, ($Is_VMS ? VMS::Filespec::fileify($root) : $root))
  175. or carp("and can't restore permissions to "
  176. . sprintf("0%o",$rp) . "\n");
  177. }
  178. }
  179. else {
  180. if ($safe &&
  181. ($Is_VMS ? !&VMS::Filespec::candelete($root)
  182. : !(-l $root || -w $root)))
  183. {
  184. print "skipped $root\n" if $verbose;
  185. next;
  186. }
  187. chmod 0666, $root
  188. or carp "Can't make file $root writeable: $!"
  189. if $force_writeable;
  190. print "unlink $root\n" if $verbose;
  191. # delete all versions under VMS
  192. for (;;) {
  193. unless (unlink $root) {
  194. carp "Can't unlink file $root: $!";
  195. if ($force_writeable) {
  196. chmod $rp, $root
  197. or carp("and can't restore permissions to "
  198. . sprintf("0%o",$rp) . "\n");
  199. }
  200. last;
  201. }
  202. ++$count;
  203. last unless $Is_VMS && lstat $root;
  204. }
  205. }
  206. }
  207. $count;
  208. }
  209. 1;