tempfile: use list.h for linked list
[git/git.git] / tempfile.c
CommitLineData
1a9d15db
MH
1/*
2 * State diagram and cleanup
3 * -------------------------
4 *
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.
10 *
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.
14 *
15 * The possible states of a `tempfile` object are as follows:
16 *
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.
21 *
22 * - Active, file open (after `create_tempfile()` or
23 * `reopen_tempfile()`). In this state:
24 *
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
32 *
49bd0fc2 33 * - Active, file closed (after `close_tempfile_gently()`). Same
1a9d15db
MH
34 * as the previous state, except that the temporary file is closed,
35 * `fd` is -1, and `fp` is `NULL`.
36 *
49bd0fc2
JK
37 * - Inactive (after `delete_tempfile()`, `rename_tempfile()`, or a
38 * failed attempt to create a temporary file). In this state:
1a9d15db
MH
39 *
40 * - `active` is unset
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
46 * `on_list` is set.
47 *
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.
52 */
53
54#include "cache.h"
55#include "tempfile.h"
56#include "sigchain.h"
57
24d82185 58static VOLATILE_LIST_HEAD(tempfile_list);
1a9d15db 59
6b935066 60static void remove_tempfiles(int in_signal_handler)
1a9d15db
MH
61{
62 pid_t me = getpid();
24d82185
JK
63 volatile struct volatile_list_head *pos;
64
65 list_for_each(pos, &tempfile_list) {
66 struct tempfile *p = list_entry(pos, struct tempfile, list);
1a9d15db 67
6b935066
JK
68 if (!is_tempfile_active(p) || p->owner != me)
69 continue;
70
71 if (p->fd >= 0)
72 close(p->fd);
73
74 if (in_signal_handler)
75 unlink(p->filename.buf);
76 else
77 unlink_or_warn(p->filename.buf);
78
79 p->active = 0;
1a9d15db
MH
80 }
81}
82
83static void remove_tempfiles_on_exit(void)
84{
85 remove_tempfiles(0);
86}
87
88static void remove_tempfiles_on_signal(int signo)
89{
90 remove_tempfiles(1);
91 sigchain_pop(signo);
92 raise(signo);
93}
94
7eba6ce5
MH
95/*
96 * Initialize *tempfile if necessary and add it to tempfile_list.
97 */
98static void prepare_tempfile_object(struct tempfile *tempfile)
1a9d15db 99{
24d82185 100 if (volatile_list_empty(&tempfile_list)) {
1a9d15db
MH
101 /* One-time initialization */
102 sigchain_push_common(remove_tempfiles_on_signal);
103 atexit(remove_tempfiles_on_exit);
104 }
105
e6fc2673 106 if (is_tempfile_active(tempfile))
9b028aa4 107 BUG("prepare_tempfile_object called for active object");
1a9d15db
MH
108 if (!tempfile->on_list) {
109 /* Initialize *tempfile and add it to tempfile_list: */
110 tempfile->fd = -1;
111 tempfile->fp = NULL;
112 tempfile->active = 0;
113 tempfile->owner = 0;
7eba6ce5 114 strbuf_init(&tempfile->filename, 0);
24d82185 115 volatile_list_add(&tempfile->list, &tempfile_list);
1a9d15db
MH
116 tempfile->on_list = 1;
117 } else if (tempfile->filename.len) {
118 /* This shouldn't happen, but better safe than sorry. */
9b028aa4 119 BUG("prepare_tempfile_object called for improperly-reset object");
1a9d15db 120 }
7eba6ce5
MH
121}
122
2933ebba
JK
123static void activate_tempfile(struct tempfile *tempfile)
124{
125 tempfile->owner = getpid();
126 tempfile->active = 1;
127}
128
b5f4dcb5
JK
129static void deactivate_tempfile(struct tempfile *tempfile)
130{
131 tempfile->active = 0;
102cf7a6 132 strbuf_release(&tempfile->filename);
b5f4dcb5
JK
133}
134
7eba6ce5
MH
135/* Make sure errno contains a meaningful value on error */
136int create_tempfile(struct tempfile *tempfile, const char *path)
137{
138 prepare_tempfile_object(tempfile);
1a9d15db
MH
139
140 strbuf_add_absolute_path(&tempfile->filename, path);
05d1ed61
BW
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);
1a9d15db 147 if (tempfile->fd < 0) {
b5f4dcb5 148 deactivate_tempfile(tempfile);
1a9d15db
MH
149 return -1;
150 }
2933ebba 151 activate_tempfile(tempfile);
1a9d15db
MH
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);
156 errno = save_errno;
157 return -1;
158 }
159 return tempfile->fd;
160}
161
99397152
MH
162void register_tempfile(struct tempfile *tempfile, const char *path)
163{
164 prepare_tempfile_object(tempfile);
165 strbuf_add_absolute_path(&tempfile->filename, path);
2933ebba 166 activate_tempfile(tempfile);
99397152
MH
167}
168
354ab112
MH
169int mks_tempfile_sm(struct tempfile *tempfile,
170 const char *template, int suffixlen, int mode)
171{
172 prepare_tempfile_object(tempfile);
173
174 strbuf_add_absolute_path(&tempfile->filename, template);
175 tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
176 if (tempfile->fd < 0) {
b5f4dcb5 177 deactivate_tempfile(tempfile);
354ab112
MH
178 return -1;
179 }
2933ebba 180 activate_tempfile(tempfile);
354ab112
MH
181 return tempfile->fd;
182}
183
184int mks_tempfile_tsm(struct tempfile *tempfile,
185 const char *template, int suffixlen, int mode)
186{
187 const char *tmpdir;
188
189 prepare_tempfile_object(tempfile);
190
191 tmpdir = getenv("TMPDIR");
192 if (!tmpdir)
193 tmpdir = "/tmp";
194
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) {
b5f4dcb5 198 deactivate_tempfile(tempfile);
354ab112
MH
199 return -1;
200 }
2933ebba 201 activate_tempfile(tempfile);
354ab112
MH
202 return tempfile->fd;
203}
204
205int xmks_tempfile_m(struct tempfile *tempfile, const char *template, int mode)
206{
207 int fd;
208 struct strbuf full_template = STRBUF_INIT;
209
210 strbuf_add_absolute_path(&full_template, template);
211 fd = mks_tempfile_m(tempfile, full_template.buf, mode);
212 if (fd < 0)
213 die_errno("Unable to create temporary file '%s'",
214 full_template.buf);
215
216 strbuf_release(&full_template);
217 return fd;
218}
219
1a9d15db
MH
220FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode)
221{
e6fc2673 222 if (!is_tempfile_active(tempfile))
9b028aa4 223 BUG("fdopen_tempfile() called for inactive object");
1a9d15db 224 if (tempfile->fp)
9b028aa4 225 BUG("fdopen_tempfile() called for open object");
1a9d15db
MH
226
227 tempfile->fp = fdopen(tempfile->fd, mode);
228 return tempfile->fp;
229}
230
231const char *get_tempfile_path(struct tempfile *tempfile)
232{
e6fc2673 233 if (!is_tempfile_active(tempfile))
9b028aa4 234 BUG("get_tempfile_path() called for inactive object");
1a9d15db
MH
235 return tempfile->filename.buf;
236}
237
238int get_tempfile_fd(struct tempfile *tempfile)
239{
e6fc2673 240 if (!is_tempfile_active(tempfile))
9b028aa4 241 BUG("get_tempfile_fd() called for inactive object");
1a9d15db
MH
242 return tempfile->fd;
243}
244
245FILE *get_tempfile_fp(struct tempfile *tempfile)
246{
e6fc2673 247 if (!is_tempfile_active(tempfile))
9b028aa4 248 BUG("get_tempfile_fp() called for inactive object");
1a9d15db
MH
249 return tempfile->fp;
250}
251
49bd0fc2 252int close_tempfile_gently(struct tempfile *tempfile)
1a9d15db 253{
f5b4dc76
JK
254 int fd;
255 FILE *fp;
1a9d15db
MH
256 int err;
257
f5b4dc76 258 if (!is_tempfile_active(tempfile) || tempfile->fd < 0)
1a9d15db
MH
259 return 0;
260
f5b4dc76
JK
261 fd = tempfile->fd;
262 fp = tempfile->fp;
1a9d15db
MH
263 tempfile->fd = -1;
264 if (fp) {
265 tempfile->fp = NULL;
7e8c9355
JK
266 if (ferror(fp)) {
267 err = -1;
268 if (!fclose(fp))
269 errno = EIO;
270 } else {
271 err = fclose(fp);
272 }
1a9d15db
MH
273 } else {
274 err = close(fd);
275 }
276
49bd0fc2 277 return err ? -1 : 0;
1a9d15db
MH
278}
279
280int reopen_tempfile(struct tempfile *tempfile)
281{
e6fc2673 282 if (!is_tempfile_active(tempfile))
9b028aa4 283 BUG("reopen_tempfile called for an inactive object");
f5b4dc76 284 if (0 <= tempfile->fd)
9b028aa4 285 BUG("reopen_tempfile called for an open object");
1a9d15db
MH
286 tempfile->fd = open(tempfile->filename.buf, O_WRONLY);
287 return tempfile->fd;
288}
289
290int rename_tempfile(struct tempfile *tempfile, const char *path)
291{
e6fc2673 292 if (!is_tempfile_active(tempfile))
9b028aa4 293 BUG("rename_tempfile called for inactive object");
1a9d15db 294
49bd0fc2
JK
295 if (close_tempfile_gently(tempfile)) {
296 delete_tempfile(tempfile);
1a9d15db 297 return -1;
49bd0fc2 298 }
1a9d15db
MH
299
300 if (rename(tempfile->filename.buf, path)) {
301 int save_errno = errno;
302 delete_tempfile(tempfile);
303 errno = save_errno;
304 return -1;
305 }
306
b5f4dcb5 307 deactivate_tempfile(tempfile);
1a9d15db
MH
308 return 0;
309}
310
311void delete_tempfile(struct tempfile *tempfile)
312{
e6fc2673 313 if (!is_tempfile_active(tempfile))
1a9d15db
MH
314 return;
315
49bd0fc2
JK
316 close_tempfile_gently(tempfile);
317 unlink_or_warn(tempfile->filename.buf);
b5f4dcb5 318 deactivate_tempfile(tempfile);
1a9d15db 319}