Merge branch 'pw/diff-no-index-from-named-pipes'
[alt-git.git] / tempfile.c
blobecdebf1afb0d8465717c18409877fe310f980e46
1 /*
2 * State diagram and cleanup
3 * -------------------------
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.
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.
15 * The possible states of a `tempfile` object are as follows:
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.
21 * - Active, file open (after `create_tempfile()` or
22 * `reopen_tempfile()`). In this state:
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
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`.
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.
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.
45 #include "git-compat-util.h"
46 #include "abspath.h"
47 #include "path.h"
48 #include "tempfile.h"
49 #include "sigchain.h"
51 static VOLATILE_LIST_HEAD(tempfile_list);
53 static void remove_template_directory(struct tempfile *tempfile,
54 int in_signal_handler)
56 if (tempfile->directory) {
57 if (in_signal_handler)
58 rmdir(tempfile->directory);
59 else
60 rmdir_or_warn(tempfile->directory);
64 static void remove_tempfiles(int in_signal_handler)
66 pid_t me = getpid();
67 volatile struct volatile_list_head *pos;
69 list_for_each(pos, &tempfile_list) {
70 struct tempfile *p = list_entry(pos, struct tempfile, list);
72 if (!is_tempfile_active(p) || p->owner != me)
73 continue;
75 if (p->fd >= 0)
76 close(p->fd);
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);
86 static void remove_tempfiles_on_exit(void)
88 remove_tempfiles(0);
91 static void remove_tempfiles_on_signal(int signo)
93 remove_tempfiles(1);
94 sigchain_pop(signo);
95 raise(signo);
98 static struct tempfile *new_tempfile(void)
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;
110 static void activate_tempfile(struct tempfile *tempfile)
112 static int initialized;
114 if (!initialized) {
115 sigchain_push_common(remove_tempfiles_on_signal);
116 atexit(remove_tempfiles_on_exit);
117 initialized = 1;
120 volatile_list_add(&tempfile->list, &tempfile_list);
121 tempfile->owner = getpid();
124 static void deactivate_tempfile(struct tempfile *tempfile)
126 volatile_list_del(&tempfile->list);
127 strbuf_release(&tempfile->filename);
128 free(tempfile->directory);
129 free(tempfile);
132 /* Make sure errno contains a meaningful value on error */
133 struct tempfile *create_tempfile_mode(const char *path, int mode)
135 struct tempfile *tempfile = new_tempfile();
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;
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;
157 return tempfile;
160 struct tempfile *register_tempfile(const char *path)
162 struct tempfile *tempfile = new_tempfile();
163 strbuf_add_absolute_path(&tempfile->filename, path);
164 activate_tempfile(tempfile);
165 return tempfile;
168 struct tempfile *mks_tempfile_sm(const char *filename_template, int suffixlen, int mode)
170 struct tempfile *tempfile = new_tempfile();
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;
178 activate_tempfile(tempfile);
179 return tempfile;
182 struct tempfile *mks_tempfile_tsm(const char *filename_template, int suffixlen, int mode)
184 struct tempfile *tempfile = new_tempfile();
185 const char *tmpdir;
187 tmpdir = getenv("TMPDIR");
188 if (!tmpdir)
189 tmpdir = "/tmp";
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;
197 activate_tempfile(tempfile);
198 return tempfile;
201 struct tempfile *mks_tempfile_dt(const char *directory_template,
202 const char *filename)
204 struct tempfile *tempfile;
205 const char *tmpdir;
206 struct strbuf sb = STRBUF_INIT;
207 int fd;
208 size_t directorylen;
210 if (!ends_with(directory_template, "XXXXXX")) {
211 errno = EINVAL;
212 return NULL;
215 tmpdir = getenv("TMPDIR");
216 if (!tmpdir)
217 tmpdir = "/tmp";
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;
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;
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;
247 struct tempfile *xmks_tempfile_m(const char *filename_template, int mode)
249 struct tempfile *tempfile;
250 struct strbuf full_template = STRBUF_INIT;
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);
258 strbuf_release(&full_template);
259 return tempfile;
262 FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode)
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");
269 tempfile->fp = fdopen(tempfile->fd, mode);
270 return tempfile->fp;
273 const char *get_tempfile_path(struct tempfile *tempfile)
275 if (!is_tempfile_active(tempfile))
276 BUG("get_tempfile_path() called for inactive object");
277 return tempfile->filename.buf;
280 int get_tempfile_fd(struct tempfile *tempfile)
282 if (!is_tempfile_active(tempfile))
283 BUG("get_tempfile_fd() called for inactive object");
284 return tempfile->fd;
287 FILE *get_tempfile_fp(struct tempfile *tempfile)
289 if (!is_tempfile_active(tempfile))
290 BUG("get_tempfile_fp() called for inactive object");
291 return tempfile->fp;
294 int close_tempfile_gently(struct tempfile *tempfile)
296 int fd;
297 FILE *fp;
298 int err;
300 if (!is_tempfile_active(tempfile) || tempfile->fd < 0)
301 return 0;
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);
315 } else {
316 err = close(fd);
319 return err ? -1 : 0;
322 int reopen_tempfile(struct tempfile *tempfile)
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;
332 int rename_tempfile(struct tempfile **tempfile_p, const char *path)
334 struct tempfile *tempfile = *tempfile_p;
336 if (!is_tempfile_active(tempfile))
337 BUG("rename_tempfile called for inactive object");
339 if (close_tempfile_gently(tempfile)) {
340 delete_tempfile(tempfile_p);
341 return -1;
344 if (rename(tempfile->filename.buf, path)) {
345 int save_errno = errno;
346 delete_tempfile(tempfile_p);
347 errno = save_errno;
348 return -1;
351 deactivate_tempfile(tempfile);
352 *tempfile_p = NULL;
353 return 0;
356 void delete_tempfile(struct tempfile **tempfile_p)
358 struct tempfile *tempfile = *tempfile_p;
360 if (!is_tempfile_active(tempfile))
361 return;
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;