Atomic file write operations (cross platform)

How do I build up an atomic file write operation? The file is to be written by a Java service and read by python scripts. For the record, reads are far greater than writes. But the write happens in batches and tend to be long. The file size amounts to mega bytes.

Right now my approach is:

  • Write file contents to a temp file in same directory
  • Delete old file
  • Rename temp file to old filename.

Is this the right approach? How can avoid conditions where the old file is deleted but the new filename is yet to be renamed?

Do these programming languages (python and java) offer constructs to lock and avoid this situation?



And the reason is that for such an atomic operation to be possible, there has to be OS support in the form of a transactional file system. And none of the mainstream operating system offer a transactional file system.

EDIT - I'm wrong for POSIX-compliant systems at least. The POSIX rename syscall performs an atomic replace if a file with the target name already exists ... as pointed out by @janneb. That should be sufficient to do the OP's operation atomically.

However, the fact remains that the Java File.renameTo() method is explicitly not guaranteed to be atomic, so it does not provide a cross-platform solution to the OP's problem.

Need Your Help

Angular UI-Router doesn't match a defined state

javascript regex angularjs angular-ui-router

I'm using UI-Router module for routing. I have 2 states that router should match the urls with them :

How to pause a pre-implemented Runnable class in java?

java multithreading thread-safety jung2

I have got the following animator class that implements Runnable (mentioned in JUNG documentation).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.