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 you with a file with an inode and storage but no possible way to re-open it. Such files are often used as temp files (and typically this is what tmpfile() returns to you).
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 my compulsive neatness. 😉
When poking through the relevant system call functions I expected to find a version of link() called flink() (compare with chmod()/fchmod()) but, at least on Linux this doesn’t exist.
Bonus points for telling me how to create the anonymous file without briefly exposing a filename in the disk’s directory structure.
A patch for a proposed Linux
flink()system call was submitted several years ago, but when Linus stated “there is no way in HELL we can do this securely without major other incursions”, that pretty much ended the debate on whether to add this.Update: As of Linux 3.11, it is now possible to create a file with no directory entry using
open()with the newO_TMPFILEflag, and link it into the filesystem once it is fully formed usinglinkat()on/proc/self/fd/fd with theAT_SYMLINK_FOLLOWflag.The following example is provided on the
open()manual page:Note that
linkat()will not allow open files to be re-attached after the last link is removed withunlink().