Re: Is the shell command "mv" atomic
From: Dragan Cvetkovic (d1r2a3g4a5n.NOSPAM@SPAM.t6h7t.net)
From: Dragan Cvetkovic <d1r2a3g4a5n.NOSPAM@SPAM.t6h7t.net> Date: 03 Apr 2003 10:54:30 -0500
Chris Lowth <email@example.com> writes:
> Andy Isaacson wrote:
> > mv(1) uses rename(2) to do its work. rename(2) is atomic. Therefore,
> > the race condition you describe cannot occur. Your script is safe.
> No: Not true for all Unixes - some have "rename" as a system, and for others
> "rename" is library call that uses link and unlink to do it's work.
> Under linux (for example) - "rename" is indeed a system call - but I
> wouldn't like to guarantee that this means that the action is atomic.
Well, POSIX requires it. Here is the quote from OpenGroup specification for
rename(2) (in Rationale):
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.
Note the last sentence there.
-- Dragan Cvetkovic, To be or not to be is true. G. Boole No it isn't. L. E. J. Brouwer