Using rmtree from File::Path to remove a directory tree on a unix platfrom with an automount directory. The directory is then re-created for clean use.
However the rmtree command can sometime be to quick for the NFS system and fail to delete the toplevel directory as it contains '.NFS****' files when it attempt 'rmdir'
This does not really effect the use of the directory, as the contents has been removed. However I don't want the error message to be displayed, as it will confuse the users into thinking that there is a problem...
File::Path Module rmtree sub
sub rmtree {
my($roots, $verbose, $safe) = @_;
my(@files);
my($count) = 0;
$verbose ||= 0;
$safe ||= 0;
if ( defined($roots) && length($roots) ) {
$roots = [$roots] unless ref $roots;
}
else {
carp "No root path(s) specified\n";
return 0;
}
my($root);
foreach $root (@{$roots}) {
if ($Is_MacOS) {
$root = ":$root" if $root !~ /:/;
$root =~ s#([^:])\z#$1:#;
} else {
$root =~ s#/\z##;
}
(undef, undef, my $rp) = lstat $root or next;
$rp &= 07777; # don't forget setuid, setgid, sticky bits
if ( -d _ ) {
# notabene: 0777 is for making readable in the first place,
# it's also intended to change it to writable in case we have
# to recurse in which case we are better than rm -rf for
# subtrees with strange permissions
chmod(0777, ($Is_VMS ? VMS::Filespec::fileify($root) : $root))
or carp "Can't make directory $root read+writeable: $!"
unless $safe;
if (opendir my $d, $root) {
@files = readdir $d;
closedir $d;
}
else {
carp "Can't read $root: $!";
@files = ();
}
# Deleting large numbers of files from VMS Files-11 filesystem
+s
# is faster if done in reverse ASCIIbetical order
@files = reverse @files if $Is_VMS;
($root = VMS::Filespec::unixify($root)) =~ s#\.dir\z## if $Is_
+VMS;
if ($Is_MacOS) {
@files = map("$root$_", @files);
} else {
@files = map("$root/$_", grep $_!~/^\.{1,2}\z/s,@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;
if (rmdir $root) {
++$count;
}
else {
carp "Can't remove directory $root: $!";
chmod($rp, ($Is_VMS ? VMS::Filespec::fileify($root) : $root))
or carp("and can't restore permissions to "
. sprintf("0%o",$rp) . "\n");
}
}
else {
if ($safe &&
($Is_VMS ? !&VMS::Filespec::candelete($root)
: !(-l $root || -w $root)))
{
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;
# delete all versions under VMS
for (;;) {
unless (unlink $root) {
carp "Can't unlink file $root: $!";
if ($force_writeable) {
chmod $rp, $root
or carp("and can't restore permissions to "
. sprintf("0%o",$rp) . "\n");
}
last;
}
++$count;
last unless $Is_VMS && lstat $root;
}
}
}
$count;
}
So how can I modify the rmtree function to allow it check and wait for the .nfs**** files to be removed by the filesystem?
--
The Snowman
snowman@notreally.co.uk