Solved [Solved] File system behavior: Rename file to itself

I think I have found some behavior in FreeBSD that is may be a bug; it seems to violate the POSIX standard. In a nutshell, the rename() system call silently fails if the new and old file name are both hard links (not soft links!) to the same file.

Here is how to try it. Create a file, and call it f1. Then create a second hard link to the same file, for example with ln f1 f2. Now you have one file, and it is visible under two names. In this really simple example, both names are in the same directory.

Now call the rename() system call, renaming f1 to f2 (this can be done by using the mv utility, or writing a tiny C program, or using Perl or Python). What should happen? According to the POSIX description (for example http://pubs.opengroup.org/onlinepubs/009695399/functions/rename.html), the name f1 should be changed to f2, and that should replace the old f2 directory entry. After that's done, you should only have one directory entry, and it should be f2. And the ctime of the parent directory should be updated.

Here is what happens on FreeBSD (I tried both UFS and ZFS), and interestingly also on it's relative MacOS (on HFS): In this situation, both file names f1 and f2 continue to exist; the name f1 is not removed. And the ctime of the parent directory is also not changed. Meaning the rename() system call did nothing at all. But it also didn't return an error indication, nor did it set errno. That seems broken to me.
 
Re: File system behavior: Rename file to itself

Seems it did exactly what it's supposed to, as per the specification you linked to:

If the old argument and the new argument resolve to the same existing file, rename() shall return successfully and perform no other action.

Do nothing and return success...
 
Re: File system behavior: Rename file to itself

Serious? I missed that in the POSIX spec.

So it works perfectly. Sorry about wasting everyone's time.
 
Re: File system behavior: Rename file to itself

It's never a waste of time to check things like that.
 
Back
Top