Re: Recursive file remove
Re: Recursive file remove
- Subject: Re: Recursive file remove
- From: Don Quixote de la Mancha <email@hidden>
- Date: Sat, 14 Aug 2010 00:48:15 -0700
> What is the best way to recursively remove a directory and all of it's sub directories and files, BTW I need to support 10.4 systems.
Send an Apple Event to the finder to move the parent directory to the
Trash, then another Apple Event to actually empty the Trash.
**** RIM SHOT! ****
But seriously folks, look at "man opendir" "rewindir" and "readdir".
It's a portable way to read UNIX directories as if they were regular
files containing a linear array of filenames.
Make sure you look at "man 3 readdir" and not "man 2 readdir".
Section 3 of the man pages covers a portable standard library wrapper
around the non-portable and more-complex man section 2 system call.
The library call works the same on every POSIX system, but the system
call is, in general, different everywhere.
Start with the topmost directory and iterate through all of its
entries. Use the stat system call to distinguish regular files from
directories. As you come across regular files and symbolic links, use
the unlink system call to delete them.
As you find subdirectories, keep recursing into them. Your recursion
bottoms out at the point you reach a directory that only contains
regular files and symbolic links but no more subdirectories. When you
pop up from that directory, unlink it as well.
This algorithm SHOULD work on any POSIX system, not just Mac OS X.
While OS X uses the HFS+ filesystem, HFS+ can be accessed via POSIX
filesystem calls.
Extra credit if you preflight the entire process without deleting
anything at all, just to check whether any unlinks would fail because
you don't have permission. The permission bits are available in the
structure returned from the stat system call.
I'll send you my bill in the mail.
Don Quixote
--
Don Quixote de la Mancha
email@hidden
http://www.dulcineatech.com
Dulcinea Technologies Corporation: Software of Elegance and Beauty.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden