
Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
> It seems to me that a sane OS should do this in the background.
It depends on your definition of a sane OS. A slight mistake could kill
a whole file system - if you accidentally try to delete the root directory.
> Are there sane OS's?
Well, I know that in some versions of Unix you can unlink a directory,
leaving a corrupted file system behind. Not what you really want.
> File.deleteDirectory ( File dir, boolean onlyIfEmpty )
/* hasta la vista baby */
File.deleteDirectory(new File("/"), false);
> On the other paw, a file might be in use in the tree and can't be
> deleted.
On Unix you can delete files while they are in use. The OS removes the
file directory entry, but the file contents remains until the last
process using that file has closed it. Then the contents is removed,
too. Very nice for temporary files. You create them, you delete them,
and afterwards you use them :-) If the application exits, crashes, etc.
the temporary file is nicely cleaned up by the OS.
On the other hand, directories in use as mount points can't be removed.
You first have to unmount the file system. A file system driver trying
to clean up such a tree would be in trouble.
> You don't know that until you traverse the tree. Any thoughts
> on speeding this up?
A file system might anyhow have to recursively traverse a directory tree
to find all disk space claimed by the data in the tree. And for each
file and subdirectory found in a directory tree it would have to do some
housekeeping to reclaim the used disk space. I would guess it can be
done (much?) faster if done inside the OS file system driver, but still
there would be some time consuming disk I/O be involved.
To speed this up, exec a separate process, or a separate thread and do
the deletion in the background. This is basically what you would like to
see from the OS: The directory tree is removed in the background.
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
Roedy Green - 19 Jan 2006 11:57 GMT
On Thu, 19 Jan 2006 12:31:47 +0100, Thomas Weidenfeller
<nobody@ericsson.invalid> wrote, quoted or indirectly quoted someone
who said :
>/* hasta la vista baby */
>File.deleteDirectory(new File("/"), false);
You can do the same thing recursively, carrying on if any individual
delete fails.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 19 Jan 2006 17:25 GMT
On Thu, 19 Jan 2006 11:57:06 GMT, Roedy Green
<my_email_is_posted_on_my_website@munged.invalid> wrote, quoted or
indirectly quoted someone who said :
>>/* hasta la vista baby */
>>File.deleteDirectory(new File("/"), false);
>
>You can do the same thing recursively, carrying on if any individual
>delete fails.
Yours is an argument for making the garden variety delete insist on
emptiness. If you are trying to delete the tree, you are more likely
to do undesired damage with a home grown method, especially during
debugging. Perhaps a safety check would be a provided limit on how
many files or total space you are prepared to delete.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Rogan Dawes - 20 Jan 2006 07:15 GMT
> On Thu, 19 Jan 2006 11:57:06 GMT, Roedy Green
> <my_email_is_posted_on_my_website@munged.invalid> wrote, quoted or
[quoted text clipped - 11 lines]
>
>
How difficult is this really?
/**
* Recursive delete files.
*/
public static boolean recursiveDelete(File dir) {
String[] ls = dir.list();
for (int i = 0; i < ls.length; i++) {
File file = new File(dir, ls[i]);
if (file.isDirectory()) {
if (!recursiveDelete(file)) return false;
} else {
if (!file.delete()) return false;
}
}
return dir.delete();
}
> File.delete has a rather onerous condition:
>
[quoted text clipped - 16 lines]
> deleted. You don't know that until you traverse the tree. Any thoughts
> on speeding this up?
No - and you provided your own reason in the last paragraph.
As for a utility to do it in the background - this would be easy, just a
simple EXE file and call it from another thread. Or even make the EXE file
spawn it's own thread and run in the background.
--
LTP
:)
> It seems to me that a sane OS should do this in the background. It
> could nip the root of the tree and at its leisure reclaim the space of
> the occupied files.
>
> Are there sane OS's? Are there ones that will chase the tree for
> you?
I use a sane OS. The system call will only delete a leaf node in the
file system (i.e. a file or an empty directory), but the OS provides a
command line utility that will do the recursion if I ask it to.
The system call will either do the whole job or nothing at all in case
of failure, while the utility may fail after removing part of the
tree. I wouldn't want a system call that might return after finishing
only part of the job.
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
> File.delete has a rather onerous condition:
>
[quoted text clipped - 16 lines]
> deleted. You don't know that until you traverse the tree. Any thoughts
> on speeding this up?
I guess you need to visit every file to check for permissions
as well as usage etc. And I'd suspect there might be notification
mechanisms at a low level that sends update events in aid of the GUI
and these will have to be synchronous.
But as far as your API suggestion goes I fully agree. If you want to
delete a sub tree you'd have to write the method yourself. And accidently
deleting the entire filesystem will be as easy with this one as with a JDK
method that does the same. (The only difference is that Sun can't be blamed
for it. Perhaps thats the reason they didn't include it :-)
Btw, Python has exactly this feature: shutil.rmtree() which do the
recursion for you.