An interesting question arose while I was trying to answer this:
Is the rename() function required to be atomic by standard?
The 'rationale' section of the POSIX standard for rename states:
This
rename()function is equivalent for regular files to that defined by the ISO C standard. Its inclusion here expands that definition to include actions on directories and specifies behavior when the new parameter names a file that already exists. That specification requires that the action of the function be atomic.
But, the latest publicly-available ISO C Standard section on rename, in its entirety, states:
7.21.4.2 The
renamefunctionSynopsis
#include <stdio.h> int rename(const char *old, const char *new);Description
The
renamefunction causes the file whose name is the string pointed to byoldto be henceforth known by the name given by the string pointed to bynew. The file namedoldis no longer accessible by that name. If a file named by the string pointed to bynewexists prior to the call to therenamefunction, the behavior is implementation-defined.Returns
The
renamefunction returns zero if the operation succeeds, nonzero if it fails, in which case if the file existed previously it is still known by its original name.
There's no explicit requirement of any kind for any type of atomicity in the rename() section of the ISO C Standard.
Having written many programs that relied upon the apparently implementation-specific atomicity of rename(), I had assumed that atomicity was a requirement and was surprised by the lack in the C Standard.
But the POSIX standard says that the ISO C standard requires rename() to be atomic.
Explanation(s)?
The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of POSIX.1-2008 defers to the ISO C standard. ![[Option End]](../../images/3813674083.webp)