1

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.

Jesch
  • 45
  • 5

3 Answers3

4

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.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • It looks like `O_TMPFILE` is actually used under-the-hood, when available, for better implementing `tmpfile(3)` (mentioned in my answer). – Jonathon Reinhart Mar 12 '15 at 06:03
1

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.
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
1

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

dvhh
  • 4,724
  • 27
  • 33
  • `"This library function is part of Netpbm(1)."` ...which I've never even heard of. It seems like a pretty *heavy* solution to have to link against this "toolkit for manipulation of graphic images" just to create temporary files. Consider this a -1 without the actual downvote. – Jonathon Reinhart Mar 12 '15 at 05:50
  • @JonathonReinhart: It's one of those ancient tools that no one has really needed for several decades. – Ignacio Vazquez-Abrams Mar 12 '15 at 05:51