1 /* temp-stream.c -- provide a stream to a per process temp file
3 Copyright (C) 2023 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
23 #include "stdlib--.h" /* For mkstemp that returns safer FDs. */
27 #include "temp-stream.h"
30 #if defined __MSDOS__ || defined _WIN32
31 /* Define this to non-zero on systems for which the regular mechanism
32 (of unlinking an open file and expecting to be able to write, seek
33 back to the beginning, then reread it) doesn't work. E.g., on Windows
35 # define DONT_UNLINK_WHILE_OPEN 1
38 #if DONT_UNLINK_WHILE_OPEN
40 /* FIXME-someday: remove all of this DONT_UNLINK_WHILE_OPEN junk.
41 Using atexit like this is wrong, since it can fail
42 when called e.g. 32 or more times.
43 But this isn't a big deal, since the code is used only on WOE/DOS
44 systems, and few people invoke tac on that many nonseekable files. */
46 static char const *file_to_remove
;
47 static FILE *fp_to_close
;
50 unlink_tempfile (void)
53 unlink (file_to_remove
);
57 record_or_unlink_tempfile (char const *fn
, FILE *fp
)
63 atexit (unlink_tempfile
);
70 record_or_unlink_tempfile (char const *fn
, MAYBE_UNUSED
FILE *fp
)
77 /* A wrapper around mkstemp that gives us both an open stream pointer,
78 FP, and the corresponding FILE_NAME. Always return the same FP/name
79 pair, rewinding/truncating it upon each reuse.
81 Note this honors $TMPDIR, unlike the standard defined tmpfile().
83 Returns TRUE on success. */
85 temp_stream (FILE **fp
, char **file_name
)
87 static char *tempfile
= nullptr;
89 if (tempfile
== nullptr)
91 char *tempbuf
= nullptr;
92 size_t tempbuf_len
= 128;
96 if (! (tempbuf
= realloc (tempbuf
, tempbuf_len
)))
98 error (0, errno
, _("failed to make temporary file name"));
102 if (path_search (tempbuf
, tempbuf_len
, nullptr, "cutmp", true) == 0)
105 if (errno
!= EINVAL
|| PATH_MAX
/ 2 < tempbuf_len
)
107 error (0, errno
== EINVAL
? ENAMETOOLONG
: errno
,
108 _("failed to make temporary file name"));
117 /* FIXME: there's a small window between a successful mkstemp call
118 and the unlink that's performed by record_or_unlink_tempfile.
119 If we're interrupted in that interval, this code fails to remove
120 the temporary file. On systems that define DONT_UNLINK_WHILE_OPEN,
121 the window is much larger -- it extends to the atexit-called
123 FIXME: clean up upon fatal signal. Don't block them, in case
124 $TMPDIR is a remote file system. */
126 int fd
= mkstemp (tempfile
);
129 error (0, errno
, _("failed to create temporary file %s"),
134 tmp_fp
= fdopen (fd
, (O_BINARY
? "w+b" : "w+"));
137 error (0, errno
, _("failed to open %s for writing"),
147 record_or_unlink_tempfile (tempfile
, tmp_fp
);
152 if (fseeko (tmp_fp
, 0, SEEK_SET
) < 0
153 || ftruncate (fileno (tmp_fp
), 0) < 0)
155 error (0, errno
, _("failed to rewind stream for %s"),
163 *file_name
= tempfile
;