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 * - Uninitialized. In this state the object's `on_list` field must be
18 * zero but the rest of its contents need not be initialized. As
19 * soon as the object is used in any way, it is irrevocably
20 * registered in `tempfile_list`, and `on_list` is set.
22 * - Active, file open (after `create_tempfile()` or
23 * `reopen_tempfile()`). In this state:
25 * - the temporary file exists
27 * - `filename` holds the filename of the temporary file
28 * - `fd` holds a file descriptor open for writing to it
29 * - `fp` holds a pointer to an open `FILE` object if and only if
30 * `fdopen_tempfile()` has been called on the object
31 * - `owner` holds the PID of the process that created the file
33 * - Active, file closed (after `close_tempfile_gently()`). Same
34 * as the previous state, except that the temporary file is closed,
35 * `fd` is -1, and `fp` is `NULL`.
37 * - Inactive (after `delete_tempfile()`, `rename_tempfile()`, or a
38 * failed attempt to create a temporary file). In this state:
41 * - `filename` is empty (usually, though there are transitory
42 * states in which this condition doesn't hold). Client code should
43 * *not* rely on the filename being empty in this state.
44 * - `fd` is -1 and `fp` is `NULL`
45 * - the object is left registered in the `tempfile_list`, and
48 * A temporary file is owned by the process that created it. The
49 * `tempfile` has an `owner` field that records the owner's PID. This
50 * field is used to prevent a forked process from deleting a temporary
51 * file created by its parent.
58 static VOLATILE_LIST_HEAD(tempfile_list);
60 static void remove_tempfiles(int in_signal_handler)
63 volatile struct volatile_list_head *pos;
65 list_for_each(pos, &tempfile_list) {
66 struct tempfile *p = list_entry(pos, struct tempfile, list);
68 if (!is_tempfile_active(p) || p->owner != me)
74 if (in_signal_handler)
75 unlink(p->filename.buf);
77 unlink_or_warn(p->filename.buf);
83 static void remove_tempfiles_on_exit(void)
88 static void remove_tempfiles_on_signal(int signo)
96 * Initialize *tempfile if necessary and add it to tempfile_list.
98 static void prepare_tempfile_object(struct tempfile *tempfile)
100 if (volatile_list_empty(&tempfile_list)) {
101 /* One-time initialization */
102 sigchain_push_common(remove_tempfiles_on_signal);
103 atexit(remove_tempfiles_on_exit);
106 if (is_tempfile_active(tempfile))
107 BUG("prepare_tempfile_object called for active object");
108 if (!tempfile->on_list) {
109 /* Initialize *tempfile and add it to tempfile_list: */
112 tempfile->active = 0;
114 strbuf_init(&tempfile->filename, 0);
115 volatile_list_add(&tempfile->list, &tempfile_list);
116 tempfile->on_list = 1;
117 } else if (tempfile->filename.len) {
118 /* This shouldn't happen, but better safe than sorry. */
119 BUG("prepare_tempfile_object called for improperly-reset object");
123 static void activate_tempfile(struct tempfile *tempfile)
125 tempfile->owner = getpid();
126 tempfile->active = 1;
129 static void deactivate_tempfile(struct tempfile *tempfile)
131 tempfile->active = 0;
132 strbuf_release(&tempfile->filename);
135 /* Make sure errno contains a meaningful value on error */
136 int create_tempfile(struct tempfile *tempfile, const char *path)
138 prepare_tempfile_object(tempfile);
140 strbuf_add_absolute_path(&tempfile->filename, path);
141 tempfile->fd = open(tempfile->filename.buf,
142 O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0666);
143 if (O_CLOEXEC && tempfile->fd < 0 && errno == EINVAL)
144 /* Try again w/o O_CLOEXEC: the kernel might not support it */
145 tempfile->fd = open(tempfile->filename.buf,
146 O_RDWR | O_CREAT | O_EXCL, 0666);
147 if (tempfile->fd < 0) {
148 deactivate_tempfile(tempfile);
151 activate_tempfile(tempfile);
152 if (adjust_shared_perm(tempfile->filename.buf)) {
153 int save_errno = errno;
154 error("cannot fix permission bits on %s", tempfile->filename.buf);
155 delete_tempfile(tempfile);
162 void register_tempfile(struct tempfile *tempfile, const char *path)
164 prepare_tempfile_object(tempfile);
165 strbuf_add_absolute_path(&tempfile->filename, path);
166 activate_tempfile(tempfile);
169 int mks_tempfile_sm(struct tempfile *tempfile,
170 const char *template, int suffixlen, int mode)
172 prepare_tempfile_object(tempfile);
174 strbuf_add_absolute_path(&tempfile->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 int mks_tempfile_tsm(struct tempfile *tempfile,
185 const char *template, int suffixlen, int mode)
189 prepare_tempfile_object(tempfile);
191 tmpdir = getenv("TMPDIR");
195 strbuf_addf(&tempfile->filename, "%s/%s", tmpdir, template);
196 tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
197 if (tempfile->fd < 0) {
198 deactivate_tempfile(tempfile);
201 activate_tempfile(tempfile);
205 int xmks_tempfile_m(struct tempfile *tempfile, const char *template, int mode)
208 struct strbuf full_template = STRBUF_INIT;
210 strbuf_add_absolute_path(&full_template, template);
211 fd = mks_tempfile_m(tempfile, full_template.buf, mode);
213 die_errno("Unable to create temporary file '%s'",
216 strbuf_release(&full_template);
220 FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode)
222 if (!is_tempfile_active(tempfile))
223 BUG("fdopen_tempfile() called for inactive object");
225 BUG("fdopen_tempfile() called for open object");
227 tempfile->fp = fdopen(tempfile->fd, mode);
231 const char *get_tempfile_path(struct tempfile *tempfile)
233 if (!is_tempfile_active(tempfile))
234 BUG("get_tempfile_path() called for inactive object");
235 return tempfile->filename.buf;
238 int get_tempfile_fd(struct tempfile *tempfile)
240 if (!is_tempfile_active(tempfile))
241 BUG("get_tempfile_fd() called for inactive object");
245 FILE *get_tempfile_fp(struct tempfile *tempfile)
247 if (!is_tempfile_active(tempfile))
248 BUG("get_tempfile_fp() called for inactive object");
252 int close_tempfile_gently(struct tempfile *tempfile)
258 if (!is_tempfile_active(tempfile) || tempfile->fd < 0)
280 int reopen_tempfile(struct tempfile *tempfile)
282 if (!is_tempfile_active(tempfile))
283 BUG("reopen_tempfile called for an inactive object");
284 if (0 <= tempfile->fd)
285 BUG("reopen_tempfile called for an open object");
286 tempfile->fd = open(tempfile->filename.buf, O_WRONLY);
290 int rename_tempfile(struct tempfile *tempfile, const char *path)
292 if (!is_tempfile_active(tempfile))
293 BUG("rename_tempfile called for inactive object");
295 if (close_tempfile_gently(tempfile)) {
296 delete_tempfile(tempfile);
300 if (rename(tempfile->filename.buf, path)) {
301 int save_errno = errno;
302 delete_tempfile(tempfile);
307 deactivate_tempfile(tempfile);
311 void delete_tempfile(struct tempfile *tempfile)
313 if (!is_tempfile_active(tempfile))
316 close_tempfile_gently(tempfile);
317 unlink_or_warn(tempfile->filename.buf);
318 deactivate_tempfile(tempfile);