The normal way to safely, atomically write a file X on Unix is:
- Write the new file contents to a temporary file
Y. rename(2)YtoX
In two steps it appears that we have done nothing but change X "in-place".
It is protected against race conditions and unintentional data loss (where X is destroyed but Y is incomplete or destroyed).
The drawback (in this case) of this is that it doesn't write the inode referred to by X in-place; rename(2) makes X refer to a new inode number.
When X was a file with link count > 1 (an explicit hard link), now it doesn't refer to the same inode as before, the hard link is broken.
The obvious way to eliminate the drawback is to write the file in-place, but this is not atomic, can fail, might result in data loss etc.
Is there some way to do it atomically like rename(2) but preserve hard links?
Perhaps to change the inode number of Y (the temporary file) to the same as X, and give it X's name? An inode-level "rename."
This would effectively write the inode referred to by X with Y's new contents, but would not break its hard-link property, and would keep the old name.
If the hypothetical inode "rename" was atomic, then I think this would be atomic and protected against data loss / races.