c - Is rename required by standard to be atomic? -


an interesting question arose while trying answer this:

is mv atomic on fs?

is rename() function required atomic standard?

the 'rationale' section of posix standard rename states:

this rename() function equivalent regular files defined iso c standard. inclusion here expands definition include actions on directories , specifies behavior when new parameter names file exists. that specification requires action of function atomic.

but, latest publicly-available iso c standard section on rename, in entirety, states:

7.21.4.2 rename function

synopsis

#include <stdio.h> int rename(const char *old, const char *new); 

description

the rename function causes file name string pointed old henceforth known name given string pointed new. file named old no longer accessible name. if file named string pointed new exists prior call rename function, behavior implementation-defined.

returns

the rename function returns 0 if operation succeeds, nonzero if fails, in case if file existed still known original name.

there's no explicit requirement of kind type of atomicity in rename() section of iso c standard.

having written many programs relied upon apparently implementation-specific atomicity of rename(), had assumed atomicity requirement , surprised lack in c standard.

but posix standard says iso c standard requires rename() atomic.

explanation(s)?

your quote posix standard rename() comes (non-normative) 'rationale' section. main entry — actual normative material — begins:

for rename(): [cx] [option start] functionality described on reference page aligned iso c standard. conflict between requirements described here , iso c standard unintentional. volume of posix.1-2008 defers iso c standard. [option end]

the rename() function shall change name of file. old argument points pathname of file renamed. new argument points new pathname of file. [cx] [option start] if new argument not resolve existing directory entry file of type directory , new argument contains @ least 1 non-<slash> character , ends 1 or more trailing <slash> characters after symbolic links have been processed, rename() shall fail.

[option end]

all rest of entry within [cx] (c extension) tag , discusses other special behaviours.

the rationale quote says:

this rename() function equivalent regular files defined iso c standard. inclusion here expands definition include actions on directories , specifies behavior when new parameter names file exists. specification requires action of function atomic.

the 'that specification' referred in last sentence expanded definition includes specification of actions on directories , 'when new parameter names file exists', not specification in c standard which, observe, not atomicity (very reasonably; there systems can support c , rename() without being able support posix's more stringent atomicity requirement).

and see argument made t.c in comment — i agree t.c.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -