]>
Commit | Line | Data |
---|---|---|
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 | * | |
320fa579 JK |
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. | |
1a9d15db MH |
20 | * |
21 | * - Active, file open (after `create_tempfile()` or | |
22 | * `reopen_tempfile()`). In this state: | |
23 | * | |
24 | * - the temporary file exists | |
1a9d15db MH |
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 | |
30 | * | |
49bd0fc2 | 31 | * - Active, file closed (after `close_tempfile_gently()`). Same |
1a9d15db MH |
32 | * as the previous state, except that the temporary file is closed, |
33 | * `fd` is -1, and `fp` is `NULL`. | |
34 | * | |
49bd0fc2 | 35 | * - Inactive (after `delete_tempfile()`, `rename_tempfile()`, or a |
320fa579 JK |
36 | * failed attempt to create a temporary file). The struct is removed from |
37 | * the global tempfile_list and deallocated. | |
1a9d15db MH |
38 | * |
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. | |
43 | */ | |
44 | ||
a64acf72 | 45 | #include "git-compat-util.h" |
5d1344b4 | 46 | #include "abspath.h" |
a64acf72 | 47 | #include "path.h" |
1a9d15db MH |
48 | #include "tempfile.h" |
49 | #include "sigchain.h" | |
50 | ||
24d82185 | 51 | static VOLATILE_LIST_HEAD(tempfile_list); |
1a9d15db | 52 | |
4ae540d4 | 53 | static int remove_template_directory(struct tempfile *tempfile, |
2c2db194 RS |
54 | int in_signal_handler) |
55 | { | |
babe2e05 | 56 | if (tempfile->directory) { |
2c2db194 | 57 | if (in_signal_handler) |
4ae540d4 | 58 | return rmdir(tempfile->directory); |
2c2db194 | 59 | else |
4ae540d4 | 60 | return rmdir_or_warn(tempfile->directory); |
2c2db194 | 61 | } |
4ae540d4 PS |
62 | |
63 | return 0; | |
2c2db194 RS |
64 | } |
65 | ||
6b935066 | 66 | static void remove_tempfiles(int in_signal_handler) |
1a9d15db MH |
67 | { |
68 | pid_t me = getpid(); | |
24d82185 JK |
69 | volatile struct volatile_list_head *pos; |
70 | ||
71 | list_for_each(pos, &tempfile_list) { | |
72 | struct tempfile *p = list_entry(pos, struct tempfile, list); | |
1a9d15db | 73 | |
6b935066 JK |
74 | if (!is_tempfile_active(p) || p->owner != me) |
75 | continue; | |
76 | ||
77 | if (p->fd >= 0) | |
78 | close(p->fd); | |
79 | ||
80 | if (in_signal_handler) | |
81 | unlink(p->filename.buf); | |
82 | else | |
83 | unlink_or_warn(p->filename.buf); | |
2c2db194 | 84 | remove_template_directory(p, in_signal_handler); |
1a9d15db MH |
85 | } |
86 | } | |
87 | ||
88 | static void remove_tempfiles_on_exit(void) | |
89 | { | |
90 | remove_tempfiles(0); | |
91 | } | |
92 | ||
93 | static void remove_tempfiles_on_signal(int signo) | |
94 | { | |
95 | remove_tempfiles(1); | |
96 | sigchain_pop(signo); | |
97 | raise(signo); | |
98 | } | |
99 | ||
076aa2cb | 100 | static struct tempfile *new_tempfile(void) |
1a9d15db | 101 | { |
076aa2cb | 102 | struct tempfile *tempfile = xmalloc(sizeof(*tempfile)); |
422a21c6 JK |
103 | tempfile->fd = -1; |
104 | tempfile->fp = NULL; | |
422a21c6 JK |
105 | tempfile->owner = 0; |
106 | INIT_LIST_HEAD(&tempfile->list); | |
107 | strbuf_init(&tempfile->filename, 0); | |
babe2e05 | 108 | tempfile->directory = NULL; |
076aa2cb | 109 | return tempfile; |
7eba6ce5 MH |
110 | } |
111 | ||
2933ebba JK |
112 | static void activate_tempfile(struct tempfile *tempfile) |
113 | { | |
422a21c6 JK |
114 | static int initialized; |
115 | ||
422a21c6 JK |
116 | if (!initialized) { |
117 | sigchain_push_common(remove_tempfiles_on_signal); | |
118 | atexit(remove_tempfiles_on_exit); | |
119 | initialized = 1; | |
120 | } | |
121 | ||
122 | volatile_list_add(&tempfile->list, &tempfile_list); | |
2933ebba | 123 | tempfile->owner = getpid(); |
2933ebba JK |
124 | } |
125 | ||
b5f4dcb5 JK |
126 | static void deactivate_tempfile(struct tempfile *tempfile) |
127 | { | |
77a42b3b | 128 | volatile_list_del(&tempfile->list); |
102cf7a6 | 129 | strbuf_release(&tempfile->filename); |
babe2e05 | 130 | free(tempfile->directory); |
076aa2cb | 131 | free(tempfile); |
b5f4dcb5 JK |
132 | } |
133 | ||
7eba6ce5 | 134 | /* Make sure errno contains a meaningful value on error */ |
bef0413c | 135 | struct tempfile *create_tempfile_mode(const char *path, int mode) |
7eba6ce5 | 136 | { |
076aa2cb | 137 | struct tempfile *tempfile = new_tempfile(); |
1a9d15db MH |
138 | |
139 | strbuf_add_absolute_path(&tempfile->filename, path); | |
05d1ed61 | 140 | tempfile->fd = open(tempfile->filename.buf, |
bef0413c | 141 | O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, mode); |
05d1ed61 BW |
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, | |
bef0413c | 145 | O_RDWR | O_CREAT | O_EXCL, mode); |
1a9d15db | 146 | if (tempfile->fd < 0) { |
b5f4dcb5 | 147 | deactivate_tempfile(tempfile); |
076aa2cb | 148 | return NULL; |
1a9d15db | 149 | } |
2933ebba | 150 | activate_tempfile(tempfile); |
1a9d15db MH |
151 | if (adjust_shared_perm(tempfile->filename.buf)) { |
152 | int save_errno = errno; | |
153 | error("cannot fix permission bits on %s", tempfile->filename.buf); | |
076aa2cb | 154 | delete_tempfile(&tempfile); |
1a9d15db | 155 | errno = save_errno; |
076aa2cb | 156 | return NULL; |
1a9d15db | 157 | } |
076aa2cb JK |
158 | |
159 | return tempfile; | |
1a9d15db MH |
160 | } |
161 | ||
076aa2cb | 162 | struct tempfile *register_tempfile(const char *path) |
99397152 | 163 | { |
076aa2cb | 164 | struct tempfile *tempfile = new_tempfile(); |
99397152 | 165 | strbuf_add_absolute_path(&tempfile->filename, path); |
2933ebba | 166 | activate_tempfile(tempfile); |
076aa2cb | 167 | return tempfile; |
99397152 MH |
168 | } |
169 | ||
ea8ace4a | 170 | struct tempfile *mks_tempfile_sm(const char *filename_template, int suffixlen, int mode) |
354ab112 | 171 | { |
076aa2cb | 172 | struct tempfile *tempfile = new_tempfile(); |
354ab112 | 173 | |
ea8ace4a | 174 | strbuf_add_absolute_path(&tempfile->filename, filename_template); |
354ab112 MH |
175 | tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode); |
176 | if (tempfile->fd < 0) { | |
b5f4dcb5 | 177 | deactivate_tempfile(tempfile); |
076aa2cb | 178 | return NULL; |
354ab112 | 179 | } |
2933ebba | 180 | activate_tempfile(tempfile); |
076aa2cb | 181 | return tempfile; |
354ab112 MH |
182 | } |
183 | ||
ea8ace4a | 184 | struct tempfile *mks_tempfile_tsm(const char *filename_template, int suffixlen, int mode) |
354ab112 | 185 | { |
076aa2cb | 186 | struct tempfile *tempfile = new_tempfile(); |
354ab112 MH |
187 | const char *tmpdir; |
188 | ||
354ab112 MH |
189 | tmpdir = getenv("TMPDIR"); |
190 | if (!tmpdir) | |
191 | tmpdir = "/tmp"; | |
192 | ||
ea8ace4a | 193 | strbuf_addf(&tempfile->filename, "%s/%s", tmpdir, filename_template); |
354ab112 MH |
194 | tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode); |
195 | if (tempfile->fd < 0) { | |
b5f4dcb5 | 196 | deactivate_tempfile(tempfile); |
076aa2cb | 197 | return NULL; |
354ab112 | 198 | } |
2933ebba | 199 | activate_tempfile(tempfile); |
076aa2cb | 200 | return tempfile; |
354ab112 MH |
201 | } |
202 | ||
2c2db194 RS |
203 | struct tempfile *mks_tempfile_dt(const char *directory_template, |
204 | const char *filename) | |
205 | { | |
206 | struct tempfile *tempfile; | |
207 | const char *tmpdir; | |
208 | struct strbuf sb = STRBUF_INIT; | |
209 | int fd; | |
210 | size_t directorylen; | |
211 | ||
212 | if (!ends_with(directory_template, "XXXXXX")) { | |
213 | errno = EINVAL; | |
214 | return NULL; | |
215 | } | |
216 | ||
217 | tmpdir = getenv("TMPDIR"); | |
218 | if (!tmpdir) | |
219 | tmpdir = "/tmp"; | |
220 | ||
221 | strbuf_addf(&sb, "%s/%s", tmpdir, directory_template); | |
222 | directorylen = sb.len; | |
223 | if (!mkdtemp(sb.buf)) { | |
224 | int orig_errno = errno; | |
225 | strbuf_release(&sb); | |
226 | errno = orig_errno; | |
227 | return NULL; | |
228 | } | |
229 | ||
230 | strbuf_addf(&sb, "/%s", filename); | |
231 | fd = open(sb.buf, O_CREAT | O_EXCL | O_RDWR, 0600); | |
232 | if (fd < 0) { | |
233 | int orig_errno = errno; | |
234 | strbuf_setlen(&sb, directorylen); | |
235 | rmdir(sb.buf); | |
236 | strbuf_release(&sb); | |
237 | errno = orig_errno; | |
238 | return NULL; | |
239 | } | |
240 | ||
241 | tempfile = new_tempfile(); | |
242 | strbuf_swap(&tempfile->filename, &sb); | |
babe2e05 | 243 | tempfile->directory = xmemdupz(tempfile->filename.buf, directorylen); |
2c2db194 RS |
244 | tempfile->fd = fd; |
245 | activate_tempfile(tempfile); | |
246 | return tempfile; | |
247 | } | |
248 | ||
ea8ace4a | 249 | struct tempfile *xmks_tempfile_m(const char *filename_template, int mode) |
354ab112 | 250 | { |
076aa2cb | 251 | struct tempfile *tempfile; |
354ab112 MH |
252 | struct strbuf full_template = STRBUF_INIT; |
253 | ||
ea8ace4a | 254 | strbuf_add_absolute_path(&full_template, filename_template); |
076aa2cb JK |
255 | tempfile = mks_tempfile_m(full_template.buf, mode); |
256 | if (!tempfile) | |
354ab112 MH |
257 | die_errno("Unable to create temporary file '%s'", |
258 | full_template.buf); | |
259 | ||
260 | strbuf_release(&full_template); | |
076aa2cb | 261 | return tempfile; |
354ab112 MH |
262 | } |
263 | ||
1a9d15db MH |
264 | FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode) |
265 | { | |
e6fc2673 | 266 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 267 | BUG("fdopen_tempfile() called for inactive object"); |
1a9d15db | 268 | if (tempfile->fp) |
9b028aa4 | 269 | BUG("fdopen_tempfile() called for open object"); |
1a9d15db MH |
270 | |
271 | tempfile->fp = fdopen(tempfile->fd, mode); | |
272 | return tempfile->fp; | |
273 | } | |
274 | ||
275 | const char *get_tempfile_path(struct tempfile *tempfile) | |
276 | { | |
e6fc2673 | 277 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 278 | BUG("get_tempfile_path() called for inactive object"); |
1a9d15db MH |
279 | return tempfile->filename.buf; |
280 | } | |
281 | ||
282 | int get_tempfile_fd(struct tempfile *tempfile) | |
283 | { | |
e6fc2673 | 284 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 285 | BUG("get_tempfile_fd() called for inactive object"); |
1a9d15db MH |
286 | return tempfile->fd; |
287 | } | |
288 | ||
289 | FILE *get_tempfile_fp(struct tempfile *tempfile) | |
290 | { | |
e6fc2673 | 291 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 292 | BUG("get_tempfile_fp() called for inactive object"); |
1a9d15db MH |
293 | return tempfile->fp; |
294 | } | |
295 | ||
49bd0fc2 | 296 | int close_tempfile_gently(struct tempfile *tempfile) |
1a9d15db | 297 | { |
f5b4dc76 JK |
298 | int fd; |
299 | FILE *fp; | |
1a9d15db MH |
300 | int err; |
301 | ||
f5b4dc76 | 302 | if (!is_tempfile_active(tempfile) || tempfile->fd < 0) |
1a9d15db MH |
303 | return 0; |
304 | ||
f5b4dc76 JK |
305 | fd = tempfile->fd; |
306 | fp = tempfile->fp; | |
1a9d15db MH |
307 | tempfile->fd = -1; |
308 | if (fp) { | |
309 | tempfile->fp = NULL; | |
7e8c9355 JK |
310 | if (ferror(fp)) { |
311 | err = -1; | |
312 | if (!fclose(fp)) | |
313 | errno = EIO; | |
314 | } else { | |
315 | err = fclose(fp); | |
316 | } | |
1a9d15db MH |
317 | } else { |
318 | err = close(fd); | |
319 | } | |
320 | ||
49bd0fc2 | 321 | return err ? -1 : 0; |
1a9d15db MH |
322 | } |
323 | ||
324 | int reopen_tempfile(struct tempfile *tempfile) | |
325 | { | |
e6fc2673 | 326 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 327 | BUG("reopen_tempfile called for an inactive object"); |
f5b4dc76 | 328 | if (0 <= tempfile->fd) |
9b028aa4 | 329 | BUG("reopen_tempfile called for an open object"); |
6c003d6f | 330 | tempfile->fd = open(tempfile->filename.buf, O_WRONLY|O_TRUNC); |
1a9d15db MH |
331 | return tempfile->fd; |
332 | } | |
333 | ||
076aa2cb | 334 | int rename_tempfile(struct tempfile **tempfile_p, const char *path) |
1a9d15db | 335 | { |
076aa2cb JK |
336 | struct tempfile *tempfile = *tempfile_p; |
337 | ||
e6fc2673 | 338 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 339 | BUG("rename_tempfile called for inactive object"); |
1a9d15db | 340 | |
49bd0fc2 | 341 | if (close_tempfile_gently(tempfile)) { |
076aa2cb | 342 | delete_tempfile(tempfile_p); |
1a9d15db | 343 | return -1; |
49bd0fc2 | 344 | } |
1a9d15db MH |
345 | |
346 | if (rename(tempfile->filename.buf, path)) { | |
347 | int save_errno = errno; | |
076aa2cb | 348 | delete_tempfile(tempfile_p); |
1a9d15db MH |
349 | errno = save_errno; |
350 | return -1; | |
351 | } | |
352 | ||
b5f4dcb5 | 353 | deactivate_tempfile(tempfile); |
076aa2cb | 354 | *tempfile_p = NULL; |
1a9d15db MH |
355 | return 0; |
356 | } | |
357 | ||
4ae540d4 | 358 | int delete_tempfile(struct tempfile **tempfile_p) |
1a9d15db | 359 | { |
076aa2cb | 360 | struct tempfile *tempfile = *tempfile_p; |
4ae540d4 | 361 | int err = 0; |
076aa2cb | 362 | |
e6fc2673 | 363 | if (!is_tempfile_active(tempfile)) |
4ae540d4 | 364 | return 0; |
1a9d15db | 365 | |
4ae540d4 PS |
366 | err |= close_tempfile_gently(tempfile); |
367 | err |= unlink_or_warn(tempfile->filename.buf); | |
368 | err |= remove_template_directory(tempfile, 0); | |
b5f4dcb5 | 369 | deactivate_tempfile(tempfile); |
076aa2cb | 370 | *tempfile_p = NULL; |
4ae540d4 PS |
371 | |
372 | return err ? -1 : 0; | |
1a9d15db | 373 | } |