summaryrefslogtreecommitdiff
path: root/src/output.c
diff options
context:
space:
mode:
authorPaul Smith <psmith@gnu.org>2022-08-27 19:03:40 -0400
committerPaul Smith <psmith@gnu.org>2022-08-30 15:39:02 -0400
commita2ba5ccbda4606dde29503f57caab33a974af5bf (patch)
tree09ae52b4c14dd5f363aa37e1e5f35387c1c82156 /src/output.c
parent5eff618c8cbfbe1f386e3a55499cd8fe26cd35a1 (diff)
downloadmake-git-a2ba5ccbda4606dde29503f57caab33a974af5bf.tar.gz
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.
Diffstat (limited to 'src/output.c')
-rw-r--r--src/output.c18
1 files changed, 1 insertions, 17 deletions
diff --git a/src/output.c b/src/output.c
index dccbff26..fda783bb 100644
--- a/src/output.c
+++ b/src/output.c
@@ -285,24 +285,8 @@ release_semaphore (void *sem)
int
output_tmpfd (void)
{
- mode_t mask = umask (0077);
- int fd = -1;
- FILE *tfile = tmpfile ();
-
- if (! tfile)
- pfatal_with_name ("tmpfile");
-
- /* Create a duplicate so we can close the stream. */
- fd = dup (fileno (tfile));
- if (fd < 0)
- pfatal_with_name ("dup");
-
- fclose (tfile);
-
+ int fd = get_tmpfd (NULL);
set_append_mode (fd);
-
- umask (mask);
-
return fd;
}