Solved

file system saftey: unlink vs rmtree vs system calls for directory removal

Posted on 2007-04-02
3
394 Views
Last Modified: 2008-02-01
I keep hearing that I shouldn't use the unlink() command to remove directories because it could damage the file system and should use rmdir() instead.  

But rmdir only removes empty directories so a system call is sometimes easiest to remove directories with files still in them. (and I like to avoid system calls)

What I don't know is how safe is:
  use File::Path;
  rmtree(folder);

The use File::Path module seems to make use of both unlink and rmdir, but looks looks safe, but just want to ping you guys here first to see your experience .
0
Comment
Question by:Marketing_Insists
3 Comments
 
LVL 39

Accepted Solution

by:
Adam314 earned 100 total points
ID: 18839519
the rmtree will use unlink for files and rmdir for directories.
If you specify the second parameter as 1 to rmtree, it will print how it is removing each file/dir (eg: whether it is using unlink or rmdir)
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 100 total points
ID: 18839523
unlink on a directory will fail unless the euid is the superuser, but it is the super user, and it doed not know what it is doing, it can damage the file system
File::Path::rmtree could have a problem if the direcrories are being altered while rmtree is running, or if rmtree is interupted while running
0
 

Author Comment

by:Marketing_Insists
ID: 18892620
Thanks, both helpful
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question