]> git.ipfire.org Git - thirdparty/make.git/commit
Add get_tmpfd() and allow anonymous temp files
authorPaul Smith <psmith@gnu.org>
Sat, 27 Aug 2022 23:03:40 +0000 (19:03 -0400)
committerPaul Smith <psmith@gnu.org>
Tue, 30 Aug 2022 19:39:02 +0000 (15:39 -0400)
commita2ba5ccbda4606dde29503f57caab33a974af5bf
tree09ae52b4c14dd5f363aa37e1e5f35387c1c82156
parent5eff618c8cbfbe1f386e3a55499cd8fe26cd35a1
Add get_tmpfd() and allow anonymous temp files

The output sync feature wants a file descriptor not a FILE*.  We were
using tmpfile() but this returns FILE* which means we needed to dup()
the descriptor then fclose() the original, which is just unnecessary
overhead for every command we run.

Create a get_tmpfd() method that returns a file descriptor directly
by using mkstemp() if available, else do the best we can.

Also allow anonymous temp files if the filename pointer is NULL.
This causes the file to be unlinked.  On Windows this requires a
special open so add an os_anontmp() method to handle this.

* src/makeint.h: Add prototype for get_tmpfd().
* src/misc.c (get_tmpfd): If we have mkstemp() use that, else just
open(2).  If we don't want to keep the filename, unlink the file.
(get_tmpfile): Use get_tmpfd() if we have fdopen(), else use fopen().
* src/output.c (output_tmpfd): Call get_tmpfd() with NULL.
* src/os.h (os_anontmp): On Windows make this a function, else fails.
* src/w32/compat/posixcfn.c (tmpfile): Move to w32os.c:os_anontmp().
* src/w32/w32os.c (os_anontmp): Create a temp file that will be deleted
when the process exits, and return a file descriptor to it.
src/makeint.h
src/misc.c
src/os.h
src/output.c
src/w32/compat/posixfcn.c
src/w32/w32os.c