Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Files.deleteDirectoryContents() deprecated in Guava?

In Guava 10+, Google deprecated Files.deleteDirectoryContents(). JavaDoc says

Deprecated. This method suffers from poor symlink detection and race conditions. This functionality can be supported suitably only by shelling out to an operating system command such as rm -rf or del /s. This method is scheduled to be removed from Guava in Guava release 11.0

I am confused on why there is a race condition. I think having this method is actually useful and find shelling out to operating system a poor solution. Can the authors share why the made this decision?

like image 969
Amir Raminfar Avatar asked Feb 02 '23 11:02

Amir Raminfar


1 Answers

I am confused on why there is a race condition.

For example, suppose that one thread calls Files.deleteDirectoryContents() and a second thread (or an external process) simultaneously creates a new file in the directory.

When you return from the call, can you rely on the directory being empty? Nope!

Anyway, if you find the functionality of this method to be useful ... despite its flaws ... you are free to take a copy of the code, tweak it, and embed it in your application. (Just check the Guava source code license and make sure that you conform to it.)

Can the authors share why the made this decision?

I think that they already have; see the deprecation notice. If you want more, try searching the issue tracker and the Guava discussion group. You could even try asking politely on the discussion group, though if your agenda is to change their minds, I doubt that you will succeed.

like image 179
Stephen C Avatar answered Feb 19 '23 20:02

Stephen C