2 * State diagram and cleanup
3 * -------------------------
5 * If the program exits while a temporary file is active, we want to
6 * make sure that we remove it. This is done by remembering the active
7 * temporary files in a linked list, `tempfile_list`. An `atexit(3)`
8 * handler and a signal handler are registered, to clean up any active
11 * Because the signal handler can run at any time, `tempfile_list` and
12 * the `tempfile` objects that comprise it must be kept in
13 * self-consistent states at all times.
15 * The possible states of a `tempfile` object are as follows:
17 * - Inactive/unallocated. The only way to get a tempfile is via a creation
18 * function like create_tempfile(). Once allocated, the tempfile is on the
19 * global tempfile_list and considered active.
21 * - Active, file open (after `create_tempfile()` or
22 * `reopen_tempfile()`). In this state:
24 * - the temporary file exists
25 * - `filename` holds the filename of the temporary file
26 * - `fd` holds a file descriptor open for writing to it
27 * - `fp` holds a pointer to an open `FILE` object if and only if
28 * `fdopen_tempfile()` has been called on the object
29 * - `owner` holds the PID of the process that created the file
31 * - Active, file closed (after `close_tempfile_gently()`). Same
32 * as the previous state, except that the temporary file is closed,
33 * `fd` is -1, and `fp` is `NULL`.
35 * - Inactive (after `delete_tempfile()`, `rename_tempfile()`, or a
36 * failed attempt to create a temporary file). The struct is removed from
37 * the global tempfile_list and deallocated.
39 * A temporary file is owned by the process that created it. The
40 * `tempfile` has an `owner` field that records the owner's PID. This
41 * field is used to prevent a forked process from deleting a temporary
42 * file created by its parent.
45 #include "git-compat-util.h"
51 static VOLATILE_LIST_HEAD(tempfile_list
);
53 static void remove_template_directory(struct tempfile
*tempfile
,
54 int in_signal_handler
)
56 if (tempfile
->directory
) {
57 if (in_signal_handler
)
58 rmdir(tempfile
->directory
);
60 rmdir_or_warn(tempfile
->directory
);
64 static void remove_tempfiles(int in_signal_handler
)
67 volatile struct volatile_list_head
*pos
;
69 list_for_each(pos
, &tempfile_list
) {
70 struct tempfile
*p
= list_entry(pos
, struct tempfile
, list
);
72 if (!is_tempfile_active(p
) || p
->owner
!= me
)
78 if (in_signal_handler
)
79 unlink(p
->filename
.buf
);
81 unlink_or_warn(p
->filename
.buf
);
82 remove_template_directory(p
, in_signal_handler
);
86 static void remove_tempfiles_on_exit(void)
91 static void remove_tempfiles_on_signal(int signo
)
98 static struct tempfile
*new_tempfile(void)
100 struct tempfile
*tempfile
= xmalloc(sizeof(*tempfile
));
104 INIT_LIST_HEAD(&tempfile
->list
);
105 strbuf_init(&tempfile
->filename
, 0);
106 tempfile
->directory
= NULL
;
110 static void activate_tempfile(struct tempfile
*tempfile
)
112 static int initialized
;
115 sigchain_push_common(remove_tempfiles_on_signal
);
116 atexit(remove_tempfiles_on_exit
);
120 volatile_list_add(&tempfile
->list
, &tempfile_list
);
121 tempfile
->owner
= getpid();
124 static void deactivate_tempfile(struct tempfile
*tempfile
)
126 volatile_list_del(&tempfile
->list
);
127 strbuf_release(&tempfile
->filename
);
128 free(tempfile
->directory
);
132 /* Make sure errno contains a meaningful value on error */
133 struct tempfile
*create_tempfile_mode(const char *path
, int mode
)
135 struct tempfile
*tempfile
= new_tempfile();
137 strbuf_add_absolute_path(&tempfile
->filename
, path
);
138 tempfile
->fd
= open(tempfile
->filename
.buf
,
139 O_RDWR
| O_CREAT
| O_EXCL
| O_CLOEXEC
, mode
);
140 if (O_CLOEXEC
&& tempfile
->fd
< 0 && errno
== EINVAL
)
141 /* Try again w/o O_CLOEXEC: the kernel might not support it */
142 tempfile
->fd
= open(tempfile
->filename
.buf
,
143 O_RDWR
| O_CREAT
| O_EXCL
, mode
);
144 if (tempfile
->fd
< 0) {
145 deactivate_tempfile(tempfile
);
148 activate_tempfile(tempfile
);
149 if (adjust_shared_perm(tempfile
->filename
.buf
)) {
150 int save_errno
= errno
;
151 error("cannot fix permission bits on %s", tempfile
->filename
.buf
);
152 delete_tempfile(&tempfile
);
160 struct tempfile
*register_tempfile(const char *path
)
162 struct tempfile
*tempfile
= new_tempfile();
163 strbuf_add_absolute_path(&tempfile
->filename
, path
);
164 activate_tempfile(tempfile
);
168 struct tempfile
*mks_tempfile_sm(const char *filename_template
, int suffixlen
, int mode
)
170 struct tempfile
*tempfile
= new_tempfile();
172 strbuf_add_absolute_path(&tempfile
->filename
, filename_template
);
173 tempfile
->fd
= git_mkstemps_mode(tempfile
->filename
.buf
, suffixlen
, mode
);
174 if (tempfile
->fd
< 0) {
175 deactivate_tempfile(tempfile
);
178 activate_tempfile(tempfile
);
182 struct tempfile
*mks_tempfile_tsm(const char *filename_template
, int suffixlen
, int mode
)
184 struct tempfile
*tempfile
= new_tempfile();
187 tmpdir
= getenv("TMPDIR");
191 strbuf_addf(&tempfile
->filename
, "%s/%s", tmpdir
, filename_template
);
192 tempfile
->fd
= git_mkstemps_mode(tempfile
->filename
.buf
, suffixlen
, mode
);
193 if (tempfile
->fd
< 0) {
194 deactivate_tempfile(tempfile
);
197 activate_tempfile(tempfile
);
201 struct tempfile
*mks_tempfile_dt(const char *directory_template
,
202 const char *filename
)
204 struct tempfile
*tempfile
;
206 struct strbuf sb
= STRBUF_INIT
;
210 if (!ends_with(directory_template
, "XXXXXX")) {
215 tmpdir
= getenv("TMPDIR");
219 strbuf_addf(&sb
, "%s/%s", tmpdir
, directory_template
);
220 directorylen
= sb
.len
;
221 if (!mkdtemp(sb
.buf
)) {
222 int orig_errno
= errno
;
228 strbuf_addf(&sb
, "/%s", filename
);
229 fd
= open(sb
.buf
, O_CREAT
| O_EXCL
| O_RDWR
, 0600);
231 int orig_errno
= errno
;
232 strbuf_setlen(&sb
, directorylen
);
239 tempfile
= new_tempfile();
240 strbuf_swap(&tempfile
->filename
, &sb
);
241 tempfile
->directory
= xmemdupz(tempfile
->filename
.buf
, directorylen
);
243 activate_tempfile(tempfile
);
247 struct tempfile
*xmks_tempfile_m(const char *filename_template
, int mode
)
249 struct tempfile
*tempfile
;
250 struct strbuf full_template
= STRBUF_INIT
;
252 strbuf_add_absolute_path(&full_template
, filename_template
);
253 tempfile
= mks_tempfile_m(full_template
.buf
, mode
);
255 die_errno("Unable to create temporary file '%s'",
258 strbuf_release(&full_template
);
262 FILE *fdopen_tempfile(struct tempfile
*tempfile
, const char *mode
)
264 if (!is_tempfile_active(tempfile
))
265 BUG("fdopen_tempfile() called for inactive object");
267 BUG("fdopen_tempfile() called for open object");
269 tempfile
->fp
= fdopen(tempfile
->fd
, mode
);
273 const char *get_tempfile_path(struct tempfile
*tempfile
)
275 if (!is_tempfile_active(tempfile
))
276 BUG("get_tempfile_path() called for inactive object");
277 return tempfile
->filename
.buf
;
280 int get_tempfile_fd(struct tempfile
*tempfile
)
282 if (!is_tempfile_active(tempfile
))
283 BUG("get_tempfile_fd() called for inactive object");
287 FILE *get_tempfile_fp(struct tempfile
*tempfile
)
289 if (!is_tempfile_active(tempfile
))
290 BUG("get_tempfile_fp() called for inactive object");
294 int close_tempfile_gently(struct tempfile
*tempfile
)
300 if (!is_tempfile_active(tempfile
) || tempfile
->fd
< 0)
322 int reopen_tempfile(struct tempfile
*tempfile
)
324 if (!is_tempfile_active(tempfile
))
325 BUG("reopen_tempfile called for an inactive object");
326 if (0 <= tempfile
->fd
)
327 BUG("reopen_tempfile called for an open object");
328 tempfile
->fd
= open(tempfile
->filename
.buf
, O_WRONLY
|O_TRUNC
);
332 int rename_tempfile(struct tempfile
**tempfile_p
, const char *path
)
334 struct tempfile
*tempfile
= *tempfile_p
;
336 if (!is_tempfile_active(tempfile
))
337 BUG("rename_tempfile called for inactive object");
339 if (close_tempfile_gently(tempfile
)) {
340 delete_tempfile(tempfile_p
);
344 if (rename(tempfile
->filename
.buf
, path
)) {
345 int save_errno
= errno
;
346 delete_tempfile(tempfile_p
);
351 deactivate_tempfile(tempfile
);
356 void delete_tempfile(struct tempfile
**tempfile_p
)
358 struct tempfile
*tempfile
= *tempfile_p
;
360 if (!is_tempfile_active(tempfile
))
363 close_tempfile_gently(tempfile
);
364 unlink_or_warn(tempfile
->filename
.buf
);
365 remove_template_directory(tempfile
, 0);
366 deactivate_tempfile(tempfile
);