I have a directory that contains data files served to clients, say, /srv/data
. While making a series of updates, I am working on /srv/data_tmp
, and at the end of the operation, I would like to atomically replace data
with data_tmp
. File.renameTo()
always returns false for me when the destination is an existing directory. How can I do this?
I am afraid you can't. Not at the SO level at least. So even if you manage "atomicity" in the context of your java application, you have no guarantee about some other "rogue" process interfering at the actual filesystem level.
If I were you, I'd read this article (quite old, but should give you some ideas) and then see if you can port the suggested approach to a more modern version .
Oh, wait, someone did this already!
And apparently your aren't the first one to ask here, either
Best of luck...
You could replace the /srv/data
directory with a symbolic link (or a junction in Windows XP), and change the link's target when appropriate.
You won't be able to do that with a Java 6 API though - You'd have to rely on a library or write the command line commands yourself.
NB: I don't guarantee anything about the atomicity of that operation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With