Relinking an anonymous (unlinked but open) file

前端 未结 5 1235
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 08:39

In Unix, it\'s possible to create a handle to an anonymous file by, e.g., creating and opening it with creat() and then removing the directory link with unlink() - leaving y

5条回答
  •  一个人的身影
    2020-11-30 09:12

    My question: is there any way to re-attach a file like this back into the directory structure? If you could do this it means that you could e.g. implement file writes so that the file appears atomically and fully formed. This appeals to the my compulsive neatness. ;)

    If this is your only goal, you can achieve this in a much simpler and more widely used manner. If you are outputting to a.dat:

    1. Open a.dat.part for write.
    2. Write your data.
    3. Rename a.dat.part to a.dat.

    I can understand wanting to be neat, but unlinking a file and relinking it just to be "neat" is kind of silly.

    This question on serverfault seems to indicate that this kind of re-linking is unsafe and not supported.

提交回复
热议问题