replace file with hardlink to another file atomically

Posted by Ben Clifford on Stack Overflow See other posts from Stack Overflow or by Ben Clifford
Published on 2010-04-02T09:20:00Z Indexed on 2010/04/02 9:23 UTC
Read the original article Hit count: 557

Filed under:
|
|

I have two directory entries, a and b. Before, a and b point to different inodes. Afterwards, I want b to point to the same inode as a does. I want this to be safe - by which I mean if I fail somewhere, b either points to its original inode or the a inode. most especially I don't want to end up with b disappearing.

mv is atomic when overwriting.

ln appears to not work when the destination already exists.

so it looks like i can say:

ln a tmp mv tmp b

which in case of failure will leave a 'tmp' file around, which is undesirable but not a disaster.

Is there a better way to do this?

(what I'm actually trying to do is replace files that have identical content with a single inode containing that content, shared between all directory entries)

© Stack Overflow or respective owner

Related posts about posix

Related posts about filesystems