Red Hat Bugzilla – Bug 273921
rename(a,b) does not replace b if a and b are hard linked
Last modified: 2007-11-30 17:12:14 EST
Description of problem:
Normally, rename(a,b) replaces b with a. However, if a and b are hard linked,
rename(a,b) exits successfully, but performs no action.
Version-Release number of selected component (if applicable):
ln file1 file2
rename file1 file2 file1
ls -l file1 file2
strace on the 'rename' step shows the problem:
rename("file1", "file2") = 0
file1 should replace file2
Created attachment 184541 [details]
Rename syscall problem demonstration
I see this bug has been reassigned to util-linux. Note that I'm filing a bug
against the rename syscall and not the rename command line tool. I only used
to rename command line tool to demonstrate the syscall.
Here is a demonstration program that does not use the rename command line tool.
Note that the rename() succeeds, but leaves both "foo" and "bar" behind.
Posix spec says this is correct:
"If the old argument and the new argument resolve to the same existing file,
rename() shall return successfully and perform no other action."
I disagree (as do the BSD people, it seems, as rename() there works the other
way). The Posix language does indeed say what you quoted, however, it also adds
some clarifying language to the bit you quoted in the Rationale section:
The specification that if old and new refer to the same file is intended to
does not remove the file.
The clarification makes sense (renaming something to the same name doesn't make
sense). Renaming something to another name does make sense, and it's very odd
that it wouldn't work just because the files happened to be linked to each
other. Names have nothing to do with the links.