Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

File::Path rmtree too fast for NFS....

by Snowman (Acolyte)
on Aug 09, 2001 at 18:50 UTC ( [id://103421]=perlquestion: print w/replies, xml ) Need Help??

Snowman has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: File::Path rmtree too fast for NFS....
by nardo (Friar) on Aug 09, 2001 at 20:01 UTC
    Easy way is to just put the rmdir in a loop

    $tries = 0; while($tries < 10 && !rmdir($root)) { select(undef, undef, undef, 0.05); $tries++; } if($tries < 10) { ++$count; } else { #unable to rmdir }
    Of course the sleep time and number of tries can be tweaked to your liking. This does not wait for just the .nfs files to be removed, if another process put files in the directory it waits for those to be removed too. Since you say it doesn't matter whether the directory is actually deleted, just that it doesn't print any error messages you may simply want to comment out all the carp lines.
Re: File::Path rmtree too fast for NFS....
by stefan k (Curate) on Jan 21, 2003 at 07:39 UTC
    Hi,
    just yesterday I came across the same problem and issuing a second rmtree command solved the problem by then removing the .nfs* files, too. Actually this feels rather nasty but since in my case it was just a quick'n'dirty script for a very small task that solution is fine for me. In a larger and more important project there should be a clean way to achieve this!

    Regards... Stefan
    you begin bashing the string with a +42 regexp of confusion

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://103421]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-23 22:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found