Skip to content
Advertisement

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

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement