]> git.ipfire.org Git - thirdparty/git.git/blame - tempfile.c
tempfile: factor out deactivation
[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`
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
58static struct tempfile *volatile tempfile_list;
59
60static void remove_tempfiles(int skip_fclose)
61{
62 pid_t me = getpid();
63
64 while (tempfile_list) {
65 if (tempfile_list->owner == me) {
66 /* fclose() is not safe to call in a signal handler */
67 if (skip_fclose)
68 tempfile_list->fp = NULL;
69 delete_tempfile(tempfile_list);
70 }
71 tempfile_list = tempfile_list->next;
72 }
73}
74
75static void remove_tempfiles_on_exit(void)
76{
77 remove_tempfiles(0);
78}
79
80static void remove_tempfiles_on_signal(int signo)
81{
82 remove_tempfiles(1);
83 sigchain_pop(signo);
84 raise(signo);
85}
86
7eba6ce5
MH
87/*
88 * Initialize *tempfile if necessary and add it to tempfile_list.
89 */
90static void prepare_tempfile_object(struct tempfile *tempfile)
1a9d15db 91{
1a9d15db
MH
92 if (!tempfile_list) {
93 /* One-time initialization */
94 sigchain_push_common(remove_tempfiles_on_signal);
95 atexit(remove_tempfiles_on_exit);
96 }
97
e6fc2673 98 if (is_tempfile_active(tempfile))
9b028aa4 99 BUG("prepare_tempfile_object called for active object");
1a9d15db
MH
100 if (!tempfile->on_list) {
101 /* Initialize *tempfile and add it to tempfile_list: */
102 tempfile->fd = -1;
103 tempfile->fp = NULL;
104 tempfile->active = 0;
105 tempfile->owner = 0;
7eba6ce5 106 strbuf_init(&tempfile->filename, 0);
1a9d15db
MH
107 tempfile->next = tempfile_list;
108 tempfile_list = tempfile;
109 tempfile->on_list = 1;
110 } else if (tempfile->filename.len) {
111 /* This shouldn't happen, but better safe than sorry. */
9b028aa4 112 BUG("prepare_tempfile_object called for improperly-reset object");
1a9d15db 113 }
7eba6ce5
MH
114}
115
2933ebba
JK
116static void activate_tempfile(struct tempfile *tempfile)
117{
118 tempfile->owner = getpid();
119 tempfile->active = 1;
120}
121
b5f4dcb5
JK
122static void deactivate_tempfile(struct tempfile *tempfile)
123{
124 tempfile->active = 0;
125 strbuf_reset(&tempfile->filename);
126}
127
7eba6ce5
MH
128/* Make sure errno contains a meaningful value on error */
129int create_tempfile(struct tempfile *tempfile, const char *path)
130{
131 prepare_tempfile_object(tempfile);
1a9d15db
MH
132
133 strbuf_add_absolute_path(&tempfile->filename, path);
05d1ed61
BW
134 tempfile->fd = open(tempfile->filename.buf,
135 O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0666);
136 if (O_CLOEXEC && tempfile->fd < 0 && errno == EINVAL)
137 /* Try again w/o O_CLOEXEC: the kernel might not support it */
138 tempfile->fd = open(tempfile->filename.buf,
139 O_RDWR | O_CREAT | O_EXCL, 0666);
1a9d15db 140 if (tempfile->fd < 0) {
b5f4dcb5 141 deactivate_tempfile(tempfile);
1a9d15db
MH
142 return -1;
143 }
2933ebba 144 activate_tempfile(tempfile);
1a9d15db
MH
145 if (adjust_shared_perm(tempfile->filename.buf)) {
146 int save_errno = errno;
147 error("cannot fix permission bits on %s", tempfile->filename.buf);
148 delete_tempfile(tempfile);
149 errno = save_errno;
150 return -1;
151 }
152 return tempfile->fd;
153}
154
99397152
MH
155void register_tempfile(struct tempfile *tempfile, const char *path)
156{
157 prepare_tempfile_object(tempfile);
158 strbuf_add_absolute_path(&tempfile->filename, path);
2933ebba 159 activate_tempfile(tempfile);
99397152
MH
160}
161
354ab112
MH
162int mks_tempfile_sm(struct tempfile *tempfile,
163 const char *template, int suffixlen, int mode)
164{
165 prepare_tempfile_object(tempfile);
166
167 strbuf_add_absolute_path(&tempfile->filename, template);
168 tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
169 if (tempfile->fd < 0) {
b5f4dcb5 170 deactivate_tempfile(tempfile);
354ab112
MH
171 return -1;
172 }
2933ebba 173 activate_tempfile(tempfile);
354ab112
MH
174 return tempfile->fd;
175}
176
177int mks_tempfile_tsm(struct tempfile *tempfile,
178 const char *template, int suffixlen, int mode)
179{
180 const char *tmpdir;
181
182 prepare_tempfile_object(tempfile);
183
184 tmpdir = getenv("TMPDIR");
185 if (!tmpdir)
186 tmpdir = "/tmp";
187
188 strbuf_addf(&tempfile->filename, "%s/%s", tmpdir, template);
189 tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
190 if (tempfile->fd < 0) {
b5f4dcb5 191 deactivate_tempfile(tempfile);
354ab112
MH
192 return -1;
193 }
2933ebba 194 activate_tempfile(tempfile);
354ab112
MH
195 return tempfile->fd;
196}
197
198int xmks_tempfile_m(struct tempfile *tempfile, const char *template, int mode)
199{
200 int fd;
201 struct strbuf full_template = STRBUF_INIT;
202
203 strbuf_add_absolute_path(&full_template, template);
204 fd = mks_tempfile_m(tempfile, full_template.buf, mode);
205 if (fd < 0)
206 die_errno("Unable to create temporary file '%s'",
207 full_template.buf);
208
209 strbuf_release(&full_template);
210 return fd;
211}
212
1a9d15db
MH
213FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode)
214{
e6fc2673 215 if (!is_tempfile_active(tempfile))
9b028aa4 216 BUG("fdopen_tempfile() called for inactive object");
1a9d15db 217 if (tempfile->fp)
9b028aa4 218 BUG("fdopen_tempfile() called for open object");
1a9d15db
MH
219
220 tempfile->fp = fdopen(tempfile->fd, mode);
221 return tempfile->fp;
222}
223
224const char *get_tempfile_path(struct tempfile *tempfile)
225{
e6fc2673 226 if (!is_tempfile_active(tempfile))
9b028aa4 227 BUG("get_tempfile_path() called for inactive object");
1a9d15db
MH
228 return tempfile->filename.buf;
229}
230
231int get_tempfile_fd(struct tempfile *tempfile)
232{
e6fc2673 233 if (!is_tempfile_active(tempfile))
9b028aa4 234 BUG("get_tempfile_fd() called for inactive object");
1a9d15db
MH
235 return tempfile->fd;
236}
237
238FILE *get_tempfile_fp(struct tempfile *tempfile)
239{
e6fc2673 240 if (!is_tempfile_active(tempfile))
9b028aa4 241 BUG("get_tempfile_fp() called for inactive object");
1a9d15db
MH
242 return tempfile->fp;
243}
244
49bd0fc2 245int close_tempfile_gently(struct tempfile *tempfile)
1a9d15db 246{
f5b4dc76
JK
247 int fd;
248 FILE *fp;
1a9d15db
MH
249 int err;
250
f5b4dc76 251 if (!is_tempfile_active(tempfile) || tempfile->fd < 0)
1a9d15db
MH
252 return 0;
253
f5b4dc76
JK
254 fd = tempfile->fd;
255 fp = tempfile->fp;
1a9d15db
MH
256 tempfile->fd = -1;
257 if (fp) {
258 tempfile->fp = NULL;
7e8c9355
JK
259 if (ferror(fp)) {
260 err = -1;
261 if (!fclose(fp))
262 errno = EIO;
263 } else {
264 err = fclose(fp);
265 }
1a9d15db
MH
266 } else {
267 err = close(fd);
268 }
269
49bd0fc2 270 return err ? -1 : 0;
1a9d15db
MH
271}
272
273int reopen_tempfile(struct tempfile *tempfile)
274{
e6fc2673 275 if (!is_tempfile_active(tempfile))
9b028aa4 276 BUG("reopen_tempfile called for an inactive object");
f5b4dc76 277 if (0 <= tempfile->fd)
9b028aa4 278 BUG("reopen_tempfile called for an open object");
1a9d15db
MH
279 tempfile->fd = open(tempfile->filename.buf, O_WRONLY);
280 return tempfile->fd;
281}
282
283int rename_tempfile(struct tempfile *tempfile, const char *path)
284{
e6fc2673 285 if (!is_tempfile_active(tempfile))
9b028aa4 286 BUG("rename_tempfile called for inactive object");
1a9d15db 287
49bd0fc2
JK
288 if (close_tempfile_gently(tempfile)) {
289 delete_tempfile(tempfile);
1a9d15db 290 return -1;
49bd0fc2 291 }
1a9d15db
MH
292
293 if (rename(tempfile->filename.buf, path)) {
294 int save_errno = errno;
295 delete_tempfile(tempfile);
296 errno = save_errno;
297 return -1;
298 }
299
b5f4dcb5 300 deactivate_tempfile(tempfile);
1a9d15db
MH
301 return 0;
302}
303
304void delete_tempfile(struct tempfile *tempfile)
305{
e6fc2673 306 if (!is_tempfile_active(tempfile))
1a9d15db
MH
307 return;
308
49bd0fc2
JK
309 close_tempfile_gently(tempfile);
310 unlink_or_warn(tempfile->filename.buf);
b5f4dcb5 311 deactivate_tempfile(tempfile);
1a9d15db 312}