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
rename
functionSynopsis
#include <stdio.h> int rename(const char *old, const char *new);
Description
The
rename
function causes the file whose name is the string pointed to byold
to be henceforth known by the name given by the string pointed to bynew
. The file namedold
is no longer accessible by that name. If a file named by the string pointed to bynew
exists prior to the call to therename
function, the behavior is implementation-defined.Returns
The
rename
function 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)?