Fix MM doc's use of "SUPER::"
[p5sagit/p5-mst-13.2.git] / lib / File / Path.pm
index ec117b8..137e7bb 100644 (file)
@@ -52,8 +52,7 @@ C<rmtree> takes three arguments:
 the root of the subtree to delete, or a reference to
 a list of roots.  All of the files and directories
 below each root, as well as the roots themselves,
-will be deleted.  For the moment, C<rmtree> expects
-Unix file specification syntax.
+will be deleted.
 
 =item *
 
@@ -74,29 +73,36 @@ than VMS is settled. (defaults to FALSE)
 
 =back
 
-It returns the number of files successfully deleted.
+It returns the number of files successfully deleted. Symlinks are
+treated as ordinary files.
 
 =head1 AUTHORS
 
-Tim Bunce <Tim.Bunce@ig.co.uk>
-Charles Bailey <bailey@genetics.upenn.edu>
+Tim Bunce E<lt>F<Tim.Bunce@ig.co.uk>E<gt>
+Charles Bailey E<lt>F<bailey@genetics.upenn.edu>E<gt>
 
 =head1 REVISION
 
-This document was last revised 08-Mar-1995, for perl 5.001
+This module was last revised 14-Feb-1996, for perl 5.002.
+$VERSION is 1.0101.
 
 =cut
 
 require 5.000;
-use Config;
 use Carp;
+use File::Basename;
 require Exporter;
+
+use vars qw( $VERSION @ISA @EXPORT );
+$VERSION = "1.0101";
 @ISA = qw( Exporter );
 @EXPORT = qw( mkpath rmtree );
 
-$Is_VMS = $Config{'osname'} eq 'VMS';
+my $Is_VMS = $^O eq 'VMS';
+my $force_writeable = ($^O eq 'os2' || $^O eq 'msdos' || $^O eq 'MSWin32'
+                      || $^O eq 'amigaos');
 
-sub mkpath{
+sub mkpath {
     my($paths, $verbose, $mode) = @_;
     # $paths   -- either a path string or ref to list of paths
     # $verbose -- optional print "mkdir $path" for each directory created
@@ -105,38 +111,42 @@ sub mkpath{
     $mode = 0777 unless defined($mode);
     $paths = [$paths] unless ref $paths;
     my(@created);
-    foreach $path (@$paths){
+    foreach $path (@$paths) {
         next if -d $path;
-        my(@p);
-        foreach(split(/\//, $path)){
-            push(@p, $_);
-            next if -d "@p/";
-            print "mkdir @p\n" if $verbose;
-           mkdir("@p",$mode) || croak "mkdir @p: $!";
-            push(@created, "@p");
-        }
+        my $parent = dirname($path);
+        push(@created,mkpath($parent, $verbose, $mode)) unless (-d $parent);
+        print "mkdir $path\n" if $verbose;
+        mkdir($path,$mode) || croak "mkdir $path: $!";
+        push(@created, $path);
     }
     @created;
 }
 
 sub rmtree {
     my($roots, $verbose, $safe) = @_;
-    my(@files,$count);
+    my(@files);
+    my($count) = 0;
     $roots = [$roots] unless ref $roots;
 
     foreach $root (@{$roots}) {
        $root =~ s#/$##;
-       if (-d $root) { 
+       if (not -l $root and -d _) { 
            opendir(D,$root);
-           $root =~ s#\.dir$## if $Is_VMS;
-           @files = map("$root/$_", grep $_!~/^\.{1,2}$/, readdir(D));
+           @files = readdir(D);
            closedir(D);
+           # Deleting large numbers of files from VMS Files-11 filesystems
+           # is faster if done in reverse ASCIIbetical order 
+           @files = reverse @files if $Is_VMS;
+           ($root = VMS::Filespec::unixify($root)) =~ s#\.dir$## if $Is_VMS;
+           @files = map("$root/$_", grep $_!~/^\.{1,2}$/,@files);
            $count += rmtree(\@files,$verbose,$safe);
            if ($safe &&
                ($Is_VMS ? !&VMS::Filespec::candelete($root) : !-w $root)) {
                print "skipped $root\n" if $verbose;
                next;
            }
+          chmod 0777, $root or carp "Can't make directory $root writeable: $!"
+              if $force_writeable;
            print "rmdir $root\n" if $verbose;
            (rmdir $root && ++$count) or carp "Can't remove directory $root: $!";
         }
@@ -146,8 +156,13 @@ sub rmtree {
                print "skipped $root\n" if $verbose;
                next;
            }
+          chmod 0666, $root or carp "Can't make file $root writeable: $!"
+              if $force_writeable;
            print "unlink $root\n" if $verbose;
-           (unlink($root) && ++$count) or carp "Can't unlink file $root: $!";
+           while (-e $root || -l $root) { # delete all versions under VMS
+               (unlink($root) && ++$count)
+                   or croak "Can't unlink file $root: $!";
+           }
         }
     }