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