]>
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 | * | |
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 | * | |
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`. | |
36 | * | |
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: | |
40 | * | |
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. | |
48 | * | |
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. | |
53 | */ | |
54 | ||
55 | #include "cache.h" | |
56 | #include "tempfile.h" | |
57 | #include "sigchain.h" | |
58 | ||
59 | static struct tempfile *volatile tempfile_list; | |
60 | ||
61 | static void remove_tempfiles(int skip_fclose) | |
62 | { | |
63 | pid_t me = getpid(); | |
64 | ||
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); | |
71 | } | |
72 | tempfile_list = tempfile_list->next; | |
73 | } | |
74 | } | |
75 | ||
76 | static void remove_tempfiles_on_exit(void) | |
77 | { | |
78 | remove_tempfiles(0); | |
79 | } | |
80 | ||
81 | static void remove_tempfiles_on_signal(int signo) | |
82 | { | |
83 | remove_tempfiles(1); | |
84 | sigchain_pop(signo); | |
85 | raise(signo); | |
86 | } | |
87 | ||
7eba6ce5 MH |
88 | /* |
89 | * Initialize *tempfile if necessary and add it to tempfile_list. | |
90 | */ | |
91 | static void prepare_tempfile_object(struct tempfile *tempfile) | |
1a9d15db | 92 | { |
1a9d15db MH |
93 | if (!tempfile_list) { |
94 | /* One-time initialization */ | |
95 | sigchain_push_common(remove_tempfiles_on_signal); | |
96 | atexit(remove_tempfiles_on_exit); | |
97 | } | |
98 | ||
99 | if (tempfile->active) | |
7eba6ce5 | 100 | die("BUG: prepare_tempfile_object called for active object"); |
1a9d15db MH |
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; | |
7eba6ce5 | 107 | strbuf_init(&tempfile->filename, 0); |
1a9d15db MH |
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. */ | |
7eba6ce5 | 113 | die("BUG: prepare_tempfile_object called for improperly-reset object"); |
1a9d15db | 114 | } |
7eba6ce5 MH |
115 | } |
116 | ||
117 | /* Make sure errno contains a meaningful value on error */ | |
118 | int create_tempfile(struct tempfile *tempfile, const char *path) | |
119 | { | |
120 | prepare_tempfile_object(tempfile); | |
1a9d15db MH |
121 | |
122 | strbuf_add_absolute_path(&tempfile->filename, path); | |
123 | tempfile->fd = open(tempfile->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666); | |
124 | if (tempfile->fd < 0) { | |
125 | strbuf_reset(&tempfile->filename); | |
126 | return -1; | |
127 | } | |
128 | tempfile->owner = getpid(); | |
129 | tempfile->active = 1; | |
130 | if (adjust_shared_perm(tempfile->filename.buf)) { | |
131 | int save_errno = errno; | |
132 | error("cannot fix permission bits on %s", tempfile->filename.buf); | |
133 | delete_tempfile(tempfile); | |
134 | errno = save_errno; | |
135 | return -1; | |
136 | } | |
137 | return tempfile->fd; | |
138 | } | |
139 | ||
354ab112 MH |
140 | int mks_tempfile_sm(struct tempfile *tempfile, |
141 | const char *template, int suffixlen, int mode) | |
142 | { | |
143 | prepare_tempfile_object(tempfile); | |
144 | ||
145 | strbuf_add_absolute_path(&tempfile->filename, template); | |
146 | tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode); | |
147 | if (tempfile->fd < 0) { | |
148 | strbuf_reset(&tempfile->filename); | |
149 | return -1; | |
150 | } | |
151 | tempfile->owner = getpid(); | |
152 | tempfile->active = 1; | |
153 | return tempfile->fd; | |
154 | } | |
155 | ||
156 | int mks_tempfile_tsm(struct tempfile *tempfile, | |
157 | const char *template, int suffixlen, int mode) | |
158 | { | |
159 | const char *tmpdir; | |
160 | ||
161 | prepare_tempfile_object(tempfile); | |
162 | ||
163 | tmpdir = getenv("TMPDIR"); | |
164 | if (!tmpdir) | |
165 | tmpdir = "/tmp"; | |
166 | ||
167 | strbuf_addf(&tempfile->filename, "%s/%s", tmpdir, template); | |
168 | tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode); | |
169 | if (tempfile->fd < 0) { | |
170 | strbuf_reset(&tempfile->filename); | |
171 | return -1; | |
172 | } | |
173 | tempfile->owner = getpid(); | |
174 | tempfile->active = 1; | |
175 | return tempfile->fd; | |
176 | } | |
177 | ||
178 | int xmks_tempfile_m(struct tempfile *tempfile, const char *template, int mode) | |
179 | { | |
180 | int fd; | |
181 | struct strbuf full_template = STRBUF_INIT; | |
182 | ||
183 | strbuf_add_absolute_path(&full_template, template); | |
184 | fd = mks_tempfile_m(tempfile, full_template.buf, mode); | |
185 | if (fd < 0) | |
186 | die_errno("Unable to create temporary file '%s'", | |
187 | full_template.buf); | |
188 | ||
189 | strbuf_release(&full_template); | |
190 | return fd; | |
191 | } | |
192 | ||
1a9d15db MH |
193 | FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode) |
194 | { | |
195 | if (!tempfile->active) | |
196 | die("BUG: fdopen_tempfile() called for inactive object"); | |
197 | if (tempfile->fp) | |
198 | die("BUG: fdopen_tempfile() called for open object"); | |
199 | ||
200 | tempfile->fp = fdopen(tempfile->fd, mode); | |
201 | return tempfile->fp; | |
202 | } | |
203 | ||
204 | const char *get_tempfile_path(struct tempfile *tempfile) | |
205 | { | |
206 | if (!tempfile->active) | |
207 | die("BUG: get_tempfile_path() called for inactive object"); | |
208 | return tempfile->filename.buf; | |
209 | } | |
210 | ||
211 | int get_tempfile_fd(struct tempfile *tempfile) | |
212 | { | |
213 | if (!tempfile->active) | |
214 | die("BUG: get_tempfile_fd() called for inactive object"); | |
215 | return tempfile->fd; | |
216 | } | |
217 | ||
218 | FILE *get_tempfile_fp(struct tempfile *tempfile) | |
219 | { | |
220 | if (!tempfile->active) | |
221 | die("BUG: get_tempfile_fp() called for inactive object"); | |
222 | return tempfile->fp; | |
223 | } | |
224 | ||
225 | int close_tempfile(struct tempfile *tempfile) | |
226 | { | |
227 | int fd = tempfile->fd; | |
228 | FILE *fp = tempfile->fp; | |
229 | int err; | |
230 | ||
231 | if (fd < 0) | |
232 | return 0; | |
233 | ||
234 | tempfile->fd = -1; | |
235 | if (fp) { | |
236 | tempfile->fp = NULL; | |
237 | ||
238 | /* | |
239 | * Note: no short-circuiting here; we want to fclose() | |
240 | * in any case! | |
241 | */ | |
242 | err = ferror(fp) | fclose(fp); | |
243 | } else { | |
244 | err = close(fd); | |
245 | } | |
246 | ||
247 | if (err) { | |
248 | int save_errno = errno; | |
249 | delete_tempfile(tempfile); | |
250 | errno = save_errno; | |
251 | return -1; | |
252 | } | |
253 | ||
254 | return 0; | |
255 | } | |
256 | ||
257 | int reopen_tempfile(struct tempfile *tempfile) | |
258 | { | |
259 | if (0 <= tempfile->fd) | |
260 | die("BUG: reopen_tempfile called for an open object"); | |
261 | if (!tempfile->active) | |
262 | die("BUG: reopen_tempfile called for an inactive object"); | |
263 | tempfile->fd = open(tempfile->filename.buf, O_WRONLY); | |
264 | return tempfile->fd; | |
265 | } | |
266 | ||
267 | int rename_tempfile(struct tempfile *tempfile, const char *path) | |
268 | { | |
269 | if (!tempfile->active) | |
270 | die("BUG: rename_tempfile called for inactive object"); | |
271 | ||
272 | if (close_tempfile(tempfile)) | |
273 | return -1; | |
274 | ||
275 | if (rename(tempfile->filename.buf, path)) { | |
276 | int save_errno = errno; | |
277 | delete_tempfile(tempfile); | |
278 | errno = save_errno; | |
279 | return -1; | |
280 | } | |
281 | ||
282 | tempfile->active = 0; | |
283 | strbuf_reset(&tempfile->filename); | |
284 | return 0; | |
285 | } | |
286 | ||
287 | void delete_tempfile(struct tempfile *tempfile) | |
288 | { | |
289 | if (!tempfile->active) | |
290 | return; | |
291 | ||
292 | if (!close_tempfile(tempfile)) { | |
293 | unlink_or_warn(tempfile->filename.buf); | |
294 | tempfile->active = 0; | |
295 | strbuf_reset(&tempfile->filename); | |
296 | } | |
297 | } |