Basically, the same result as creating a temporary file in the desired file system, opening it, and then unlinking it.
Even better, though unlikely, if this could be done without creating an inode that is visible to other processes.
The ability to do so is OS-specific, since the relevant POSIX function calls all result in a link being generated. Linux in particular has allowed, since version 3.11, the use of O_TMPFILE
in the flags
argument of open(2)
in order to create an anonymous file in a given directory.
There are several POSIX APIs at your disposal:
mkstemp
- generates a unique temporary filename from
template, creates and opens the file, and returns an open file
descriptor for the file.tmpfile
- opens a unique temporary file in binary
read/write (w+b) mode. The file will be automatically deleted when
it is closed or the program terminates.Both of these functions do create files on the filesystem. Creating an inode is unavoidable, if you want to use a real file.
The first provides you a file descriptor for making low-level system calls, like read
and write
. The second gives you a FILE*
for all of the <stdio.h>
APIs.
If you don't need/desire an actual file on disk, you should consider the memory stream APIs provided by POSIX.1-2008.
open_memstream()
- opens a stream for writing to a buffer.
The buffer is dynamically allocated (as with malloc(3)
), and
automatically grows as required.libtmpfilefd : create a temporary unnamed file seem to fullfill your requirements
Looking at the source file this function create a temporary file with mkstemp
then unlink the file right after