hookenz
hookenz

Reputation: 38879

Atomic rename, works on Linux is there a way to emulate it on windows & OSX?

Under linux, you can rename a file with overwrite. For example, rename -f file_a file_b Will overwrite file_b with file_a and it'll be atomic.

Under OS/X it seems that this should have worked, but it's buggy. Under Windows, nope. On network filesystems - probably not.

Is there a way to simulate this behaviour among cooperating processes?

For example, if I'm only needing this in a single process then an atomic rename function could be created that creates a mutex, does a delete file then a rename file and unlocks. That should be able to work on all platforms.

However, across process it's a little more tricky. I could do a machine wide mutex lock but that's not going to solve it for cooperating processes on different machines.

Could I simulate this with lock files? If so, how do I do it reliably so if a process were to be prematurely ended it can cleanup, or I can recognize a lock file that is no longer valid and remove it?

Upvotes: 2

Views: 1530

Answers (2)

Andy
Andy

Reputation: 8562

EDIT: NTFS file system transaction support is deprecated. Leaving this answer for historical reasons, but alternatives are here: http://msdn.microsoft.com/en-us/library/windows/desktop/hh802690%28v=vs.85%29.aspx

NFTS supports transactions on the FS. So you can't do exactly what you want, but you can start a transaction, delete the file and then rename. http://msdn.microsoft.com/en-us/magazine/cc163388.aspx

I should point out this requires Vista or higher.

Upvotes: 2

Mel
Mel

Reputation: 6157

Passing on the windows, but mv -f is atomic rename on BSD (and probably OSX) if target and source share the same filesystem. I would imagine this restriction applies to linux as well as the operation of rename means the inode is moved to a different dir and inodes are unique per filesytem. I'm not sure what you're trying to prevent here: that two processes do the same rename, i.e. identical source and destination?

Upvotes: 0

Related Questions