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 int remove_template_directory(struct tempfile
*tempfile
,
54 int in_signal_handler
)
56 if (tempfile
->directory
) {
57 if (in_signal_handler
)
58 return rmdir(tempfile
->directory
);
60 return rmdir_or_warn(tempfile
->directory
);
66 static void remove_tempfiles(int in_signal_handler
)
69 volatile struct volatile_list_head
*pos
;
71 list_for_each(pos
, &tempfile_list
) {
72 struct tempfile
*p
= list_entry(pos
, struct tempfile
, list
);
74 if (!is_tempfile_active(p
) || p
->owner
!= me
)
80 if (in_signal_handler
)
81 unlink(p
->filename
.buf
);
83 unlink_or_warn(p
->filename
.buf
);
84 remove_template_directory(p
, in_signal_handler
);
88 static void remove_tempfiles_on_exit(void)
93 static void remove_tempfiles_on_signal(int signo
)
100 static struct tempfile
*new_tempfile(void)
102 struct tempfile
*tempfile
= xmalloc(sizeof(*tempfile
));
106 INIT_LIST_HEAD(&tempfile
->list
);
107 strbuf_init(&tempfile
->filename
, 0);
108 tempfile
->directory
= NULL
;
112 static void activate_tempfile(struct tempfile
*tempfile
)
114 static int initialized
;
117 sigchain_push_common(remove_tempfiles_on_signal
);
118 atexit(remove_tempfiles_on_exit
);
122 volatile_list_add(&tempfile
->list
, &tempfile_list
);
123 tempfile
->owner
= getpid();
126 static void deactivate_tempfile(struct tempfile
*tempfile
)
128 volatile_list_del(&tempfile
->list
);
129 strbuf_release(&tempfile
->filename
);
130 free(tempfile
->directory
);
134 /* Make sure errno contains a meaningful value on error */
135 struct tempfile
*create_tempfile_mode(const char *path
, int mode
)
137 struct tempfile
*tempfile
= new_tempfile();
139 strbuf_add_absolute_path(&tempfile
->filename
, path
);
140 tempfile
->fd
= open(tempfile
->filename
.buf
,
141 O_RDWR
| O_CREAT
| O_EXCL
| O_CLOEXEC
, mode
);
142 if (O_CLOEXEC
&& tempfile
->fd
< 0 && errno
== EINVAL
)
143 /* Try again w/o O_CLOEXEC: the kernel might not support it */
144 tempfile
->fd
= open(tempfile
->filename
.buf
,
145 O_RDWR
| O_CREAT
| O_EXCL
, mode
);
146 if (tempfile
->fd
< 0) {
147 deactivate_tempfile(tempfile
);
150 activate_tempfile(tempfile
);
151 if (adjust_shared_perm(tempfile
->filename
.buf
)) {
152 int save_errno
= errno
;
153 error("cannot fix permission bits on %s", tempfile
->filename
.buf
);
154 delete_tempfile(&tempfile
);
162 struct tempfile
*register_tempfile(const char *path
)
164 struct tempfile
*tempfile
= new_tempfile();
165 strbuf_add_absolute_path(&tempfile
->filename
, path
);
166 activate_tempfile(tempfile
);
170 struct tempfile
*mks_tempfile_sm(const char *filename_template
, int suffixlen
, int mode
)
172 struct tempfile
*tempfile
= new_tempfile();
174 strbuf_add_absolute_path(&tempfile
->filename
, filename_template
);
175 tempfile
->fd
= git_mkstemps_mode(tempfile
->filename
.buf
, suffixlen
, mode
);
176 if (tempfile
->fd
< 0) {
177 deactivate_tempfile(tempfile
);
180 activate_tempfile(tempfile
);
184 struct tempfile
*mks_tempfile_tsm(const char *filename_template
, int suffixlen
, int mode
)
186 struct tempfile
*tempfile
= new_tempfile();
189 tmpdir
= getenv("TMPDIR");
193 strbuf_addf(&tempfile
->filename
, "%s/%s", tmpdir
, filename_template
);
194 tempfile
->fd
= git_mkstemps_mode(tempfile
->filename
.buf
, suffixlen
, mode
);
195 if (tempfile
->fd
< 0) {
196 deactivate_tempfile(tempfile
);
199 activate_tempfile(tempfile
);
203 struct tempfile
*mks_tempfile_dt(const char *directory_template
,
204 const char *filename
)
206 struct tempfile
*tempfile
;
208 struct strbuf sb
= STRBUF_INIT
;
212 if (!ends_with(directory_template
, "XXXXXX")) {
217 tmpdir
= getenv("TMPDIR");
221 strbuf_addf(&sb
, "%s/%s", tmpdir
, directory_template
);
222 directorylen
= sb
.len
;
223 if (!mkdtemp(sb
.buf
)) {
224 int orig_errno
= errno
;
230 strbuf_addf(&sb
, "/%s", filename
);
231 fd
= open(sb
.buf
, O_CREAT
| O_EXCL
| O_RDWR
, 0600);
233 int orig_errno
= errno
;
234 strbuf_setlen(&sb
, directorylen
);
241 tempfile
= new_tempfile();
242 strbuf_swap(&tempfile
->filename
, &sb
);
243 tempfile
->directory
= xmemdupz(tempfile
->filename
.buf
, directorylen
);
245 activate_tempfile(tempfile
);
249 struct tempfile
*xmks_tempfile_m(const char *filename_template
, int mode
)
251 struct tempfile
*tempfile
;
252 struct strbuf full_template
= STRBUF_INIT
;
254 strbuf_add_absolute_path(&full_template
, filename_template
);
255 tempfile
= mks_tempfile_m(full_template
.buf
, mode
);
257 die_errno("Unable to create temporary file '%s'",
260 strbuf_release(&full_template
);
264 FILE *fdopen_tempfile(struct tempfile
*tempfile
, const char *mode
)
266 if (!is_tempfile_active(tempfile
))
267 BUG("fdopen_tempfile() called for inactive object");
269 BUG("fdopen_tempfile() called for open object");
271 tempfile
->fp
= fdopen(tempfile
->fd
, mode
);
275 const char *get_tempfile_path(struct tempfile
*tempfile
)
277 if (!is_tempfile_active(tempfile
))
278 BUG("get_tempfile_path() called for inactive object");
279 return tempfile
->filename
.buf
;
282 int get_tempfile_fd(struct tempfile
*tempfile
)
284 if (!is_tempfile_active(tempfile
))
285 BUG("get_tempfile_fd() called for inactive object");
289 FILE *get_tempfile_fp(struct tempfile
*tempfile
)
291 if (!is_tempfile_active(tempfile
))
292 BUG("get_tempfile_fp() called for inactive object");
296 int close_tempfile_gently(struct tempfile
*tempfile
)
302 if (!is_tempfile_active(tempfile
) || tempfile
->fd
< 0)
324 int reopen_tempfile(struct tempfile
*tempfile
)
326 if (!is_tempfile_active(tempfile
))
327 BUG("reopen_tempfile called for an inactive object");
328 if (0 <= tempfile
->fd
)
329 BUG("reopen_tempfile called for an open object");
330 tempfile
->fd
= open(tempfile
->filename
.buf
, O_WRONLY
|O_TRUNC
);
334 int rename_tempfile(struct tempfile
**tempfile_p
, const char *path
)
336 struct tempfile
*tempfile
= *tempfile_p
;
338 if (!is_tempfile_active(tempfile
))
339 BUG("rename_tempfile called for inactive object");
341 if (close_tempfile_gently(tempfile
)) {
342 delete_tempfile(tempfile_p
);
346 if (rename(tempfile
->filename
.buf
, path
)) {
347 int save_errno
= errno
;
348 delete_tempfile(tempfile_p
);
353 deactivate_tempfile(tempfile
);
358 int delete_tempfile(struct tempfile
**tempfile_p
)
360 struct tempfile
*tempfile
= *tempfile_p
;
363 if (!is_tempfile_active(tempfile
))
366 err
|= close_tempfile_gently(tempfile
);
367 err
|= unlink_or_warn(tempfile
->filename
.buf
);
368 err
|= remove_template_directory(tempfile
, 0);
369 deactivate_tempfile(tempfile
);