Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is rename() atomic?

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

like image 463
Lipika Deka Avatar asked Aug 14 '11 03:08

Lipika Deka


People also ask

Is rename file Atomic?

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.

Is rename Atomic on NFS?

In other words, rename is not atomic on NFS.

Is Windows file rename Atomic?

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."

Is file move an atomic?

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.


1 Answers

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.

like image 81
Joshua Avatar answered Sep 22 '22 15:09

Joshua