Git 2.14.4
[git.git] / tempfile.c
blob68437106703470f39a6079aaad7d3bb407ac3389
1 /*
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
9 * temporary files.
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
26 * - `active` is set
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 successful `close_tempfile()`). 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()`, a
38 * failed attempt to create a temporary file, or a failed
39 * `close_tempfile()`). In this state:
41 * - `active` is unset
42 * - `filename` is empty (usually, though there are transitory
43 * states in which this condition doesn't hold). Client code should
44 * *not* rely on the filename being empty in this state.
45 * - `fd` is -1 and `fp` is `NULL`
46 * - the object is left registered in the `tempfile_list`, and
47 * `on_list` is set.
49 * A temporary file is owned by the process that created it. The
50 * `tempfile` has an `owner` field that records the owner's PID. This
51 * field is used to prevent a forked process from deleting a temporary
52 * file created by its parent.
55 #include "cache.h"
56 #include "tempfile.h"
57 #include "sigchain.h"
59 static struct tempfile *volatile tempfile_list;
61 static void remove_tempfiles(int skip_fclose)
63 pid_t me = getpid();
65 while (tempfile_list) {
66 if (tempfile_list->owner == me) {
67 /* fclose() is not safe to call in a signal handler */
68 if (skip_fclose)
69 tempfile_list->fp = NULL;
70 delete_tempfile(tempfile_list);
72 tempfile_list = tempfile_list->next;
76 static void remove_tempfiles_on_exit(void)
78 remove_tempfiles(0);
81 static void remove_tempfiles_on_signal(int signo)
83 remove_tempfiles(1);
84 sigchain_pop(signo);
85 raise(signo);
89 * Initialize *tempfile if necessary and add it to tempfile_list.
91 static void prepare_tempfile_object(struct tempfile *tempfile)
93 if (!tempfile_list) {
94 /* One-time initialization */
95 sigchain_push_common(remove_tempfiles_on_signal);
96 atexit(remove_tempfiles_on_exit);
99 if (tempfile->active)
100 die("BUG: prepare_tempfile_object called for active object");
101 if (!tempfile->on_list) {
102 /* Initialize *tempfile and add it to tempfile_list: */
103 tempfile->fd = -1;
104 tempfile->fp = NULL;
105 tempfile->active = 0;
106 tempfile->owner = 0;
107 strbuf_init(&tempfile->filename, 0);
108 tempfile->next = tempfile_list;
109 tempfile_list = tempfile;
110 tempfile->on_list = 1;
111 } else if (tempfile->filename.len) {
112 /* This shouldn't happen, but better safe than sorry. */
113 die("BUG: prepare_tempfile_object called for improperly-reset object");
117 /* Make sure errno contains a meaningful value on error */
118 int create_tempfile(struct tempfile *tempfile, const char *path)
120 prepare_tempfile_object(tempfile);
122 strbuf_add_absolute_path(&tempfile->filename, path);
123 tempfile->fd = open(tempfile->filename.buf,
124 O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0666);
125 if (O_CLOEXEC && tempfile->fd < 0 && errno == EINVAL)
126 /* Try again w/o O_CLOEXEC: the kernel might not support it */
127 tempfile->fd = open(tempfile->filename.buf,
128 O_RDWR | O_CREAT | O_EXCL, 0666);
129 if (tempfile->fd < 0) {
130 strbuf_reset(&tempfile->filename);
131 return -1;
133 tempfile->owner = getpid();
134 tempfile->active = 1;
135 if (adjust_shared_perm(tempfile->filename.buf)) {
136 int save_errno = errno;
137 error("cannot fix permission bits on %s", tempfile->filename.buf);
138 delete_tempfile(tempfile);
139 errno = save_errno;
140 return -1;
142 return tempfile->fd;
145 void register_tempfile(struct tempfile *tempfile, const char *path)
147 prepare_tempfile_object(tempfile);
148 strbuf_add_absolute_path(&tempfile->filename, path);
149 tempfile->owner = getpid();
150 tempfile->active = 1;
153 int mks_tempfile_sm(struct tempfile *tempfile,
154 const char *template, int suffixlen, int mode)
156 prepare_tempfile_object(tempfile);
158 strbuf_add_absolute_path(&tempfile->filename, template);
159 tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
160 if (tempfile->fd < 0) {
161 strbuf_reset(&tempfile->filename);
162 return -1;
164 tempfile->owner = getpid();
165 tempfile->active = 1;
166 return tempfile->fd;
169 int mks_tempfile_tsm(struct tempfile *tempfile,
170 const char *template, int suffixlen, int mode)
172 const char *tmpdir;
174 prepare_tempfile_object(tempfile);
176 tmpdir = getenv("TMPDIR");
177 if (!tmpdir)
178 tmpdir = "/tmp";
180 strbuf_addf(&tempfile->filename, "%s/%s", tmpdir, template);
181 tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
182 if (tempfile->fd < 0) {
183 strbuf_reset(&tempfile->filename);
184 return -1;
186 tempfile->owner = getpid();
187 tempfile->active = 1;
188 return tempfile->fd;
191 int xmks_tempfile_m(struct tempfile *tempfile, const char *template, int mode)
193 int fd;
194 struct strbuf full_template = STRBUF_INIT;
196 strbuf_add_absolute_path(&full_template, template);
197 fd = mks_tempfile_m(tempfile, full_template.buf, mode);
198 if (fd < 0)
199 die_errno("Unable to create temporary file '%s'",
200 full_template.buf);
202 strbuf_release(&full_template);
203 return fd;
206 FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode)
208 if (!tempfile->active)
209 die("BUG: fdopen_tempfile() called for inactive object");
210 if (tempfile->fp)
211 die("BUG: fdopen_tempfile() called for open object");
213 tempfile->fp = fdopen(tempfile->fd, mode);
214 return tempfile->fp;
217 const char *get_tempfile_path(struct tempfile *tempfile)
219 if (!tempfile->active)
220 die("BUG: get_tempfile_path() called for inactive object");
221 return tempfile->filename.buf;
224 int get_tempfile_fd(struct tempfile *tempfile)
226 if (!tempfile->active)
227 die("BUG: get_tempfile_fd() called for inactive object");
228 return tempfile->fd;
231 FILE *get_tempfile_fp(struct tempfile *tempfile)
233 if (!tempfile->active)
234 die("BUG: get_tempfile_fp() called for inactive object");
235 return tempfile->fp;
238 int close_tempfile(struct tempfile *tempfile)
240 int fd = tempfile->fd;
241 FILE *fp = tempfile->fp;
242 int err;
244 if (fd < 0)
245 return 0;
247 tempfile->fd = -1;
248 if (fp) {
249 tempfile->fp = NULL;
250 if (ferror(fp)) {
251 err = -1;
252 if (!fclose(fp))
253 errno = EIO;
254 } else {
255 err = fclose(fp);
257 } else {
258 err = close(fd);
261 if (err) {
262 int save_errno = errno;
263 delete_tempfile(tempfile);
264 errno = save_errno;
265 return -1;
268 return 0;
271 int reopen_tempfile(struct tempfile *tempfile)
273 if (0 <= tempfile->fd)
274 die("BUG: reopen_tempfile called for an open object");
275 if (!tempfile->active)
276 die("BUG: reopen_tempfile called for an inactive object");
277 tempfile->fd = open(tempfile->filename.buf, O_WRONLY);
278 return tempfile->fd;
281 int rename_tempfile(struct tempfile *tempfile, const char *path)
283 if (!tempfile->active)
284 die("BUG: rename_tempfile called for inactive object");
286 if (close_tempfile(tempfile))
287 return -1;
289 if (rename(tempfile->filename.buf, path)) {
290 int save_errno = errno;
291 delete_tempfile(tempfile);
292 errno = save_errno;
293 return -1;
296 tempfile->active = 0;
297 strbuf_reset(&tempfile->filename);
298 return 0;
301 void delete_tempfile(struct tempfile *tempfile)
303 if (!tempfile->active)
304 return;
306 if (!close_tempfile(tempfile)) {
307 unlink_or_warn(tempfile->filename.buf);
308 tempfile->active = 0;
309 strbuf_reset(&tempfile->filename);