in reply to [OT] rename(2) -- renaming file to itself produces no error.

SUSv3 says (under rename Rationale):
The specification that if old and new refer to the same file is intended to guarantee that:

rename("x", "x");

does not remove the file.

Seems a little lame to me (the given rationale, not necessarily the behaviour).

All of the special handling for when the new file already exists is a POSIX extension of the ISO C standard.

  • Comment on Re: [OT] rename(2) -- renaming file to itself produces no error.

Replies are listed 'Best First'.
Re^2: [OT] rename(2) -- renaming file to itself produces no error.
by Coruscate (Sexton) on Jan 08, 2004 at 05:30 UTC

    I don't know, the rationale seems quite reasonable to me. I don't know the technical aspects of rename, but I assume that the file is copied and then the one that was copied is removed, making it so that a copy of this file is guaranteed to exist in full somewhere on the filesystem. If this is indeed the method used, how would you go about moving a file to the same path? You'd have to hold the entire file contents in memory, delete the original file, then create the "new" file and place the in-memory contents back in. (Or you could use a temp file rather than memory). This would leave somewhat of an undesirable race condition in which a file we are renaming doesn't exist on the filesystem at all.

    This doesn't answer the "why is there no error" question, but I don't see why there should be.