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