I am not being able to check this via experiments and could not gather it from the man pages as well.
Say I have two processes, one moving(rename) file1 from directory1 to directory2. Say the other process running concurrently copies the contents of directory1 and directory2 to another location. Is it possible that the copy happens in such a way that both directory1 and directory2 will show file1 - i.e directory1 is copied before the move and directory2 after the move by the first process.
Basically is rename() is an atomic system call?
Thanks
Atomic renameThe rename function from the C library in Windows does not implement the POSIX atomic behaviour; instead it fails if the destination file already exists.
In other words, rename is not atomic on NFS.
The relevant passage from that MS research paper: "Under UNIX, rename() is guaranteed to atomically overwrite the old version of the file. Under Windows, the ReplaceFile() call is used to atomically replace one file with another."
Let's also identify that atomicity refers to file contents, not to the file name. For any individual file, the move or rename performed by mv is atomic provided that the file is moved within the same filesystem.
Yes and no.
rename() is atomic assuming the OS does not crash. It cannot be split by any other filesystem op.
If the system crashes you might see a ln() operation instead.
Also note, when operating on a network filesystem, you might get ENOENT when the operation succeeded successfully. Local filesystem can't do that to you.
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