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