tempfile: a new module for handling temporary files
[git.git] / tempfile.c
blobd8358187f84fa1dc243df9ad57cbe8be93faf228
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 * - 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.
22 * - Active, file open (after `create_tempfile()` or
23 * `reopen_tempfile()`). In this state:
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
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`.
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:
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.
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.
55 #include "cache.h"
56 #include "tempfile.h"
57 #include "sigchain.h"
59 static struct tempfile *volatile tempfile_list;
61 static void remove_tempfiles(int skip_fclose)
63 pid_t me = getpid();
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);
72 tempfile_list = tempfile_list->next;
76 static void remove_tempfiles_on_exit(void)
78 remove_tempfiles(0);
81 static void remove_tempfiles_on_signal(int signo)
83 remove_tempfiles(1);
84 sigchain_pop(signo);
85 raise(signo);
88 /* Make sure errno contains a meaningful value on error */
89 int create_tempfile(struct tempfile *tempfile, const char *path)
91 size_t pathlen = strlen(path);
93 if (!tempfile_list) {
94 /* One-time initialization */
95 sigchain_push_common(remove_tempfiles_on_signal);
96 atexit(remove_tempfiles_on_exit);
99 if (tempfile->active)
100 die("BUG: create_tempfile called for active object");
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;
107 strbuf_init(&tempfile->filename, pathlen);
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. */
113 die("BUG: create_tempfile called for improperly-reset object");
116 strbuf_add_absolute_path(&tempfile->filename, path);
117 tempfile->fd = open(tempfile->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666);
118 if (tempfile->fd < 0) {
119 strbuf_reset(&tempfile->filename);
120 return -1;
122 tempfile->owner = getpid();
123 tempfile->active = 1;
124 if (adjust_shared_perm(tempfile->filename.buf)) {
125 int save_errno = errno;
126 error("cannot fix permission bits on %s", tempfile->filename.buf);
127 delete_tempfile(tempfile);
128 errno = save_errno;
129 return -1;
131 return tempfile->fd;
134 FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode)
136 if (!tempfile->active)
137 die("BUG: fdopen_tempfile() called for inactive object");
138 if (tempfile->fp)
139 die("BUG: fdopen_tempfile() called for open object");
141 tempfile->fp = fdopen(tempfile->fd, mode);
142 return tempfile->fp;
145 const char *get_tempfile_path(struct tempfile *tempfile)
147 if (!tempfile->active)
148 die("BUG: get_tempfile_path() called for inactive object");
149 return tempfile->filename.buf;
152 int get_tempfile_fd(struct tempfile *tempfile)
154 if (!tempfile->active)
155 die("BUG: get_tempfile_fd() called for inactive object");
156 return tempfile->fd;
159 FILE *get_tempfile_fp(struct tempfile *tempfile)
161 if (!tempfile->active)
162 die("BUG: get_tempfile_fp() called for inactive object");
163 return tempfile->fp;
166 int close_tempfile(struct tempfile *tempfile)
168 int fd = tempfile->fd;
169 FILE *fp = tempfile->fp;
170 int err;
172 if (fd < 0)
173 return 0;
175 tempfile->fd = -1;
176 if (fp) {
177 tempfile->fp = NULL;
180 * Note: no short-circuiting here; we want to fclose()
181 * in any case!
183 err = ferror(fp) | fclose(fp);
184 } else {
185 err = close(fd);
188 if (err) {
189 int save_errno = errno;
190 delete_tempfile(tempfile);
191 errno = save_errno;
192 return -1;
195 return 0;
198 int reopen_tempfile(struct tempfile *tempfile)
200 if (0 <= tempfile->fd)
201 die("BUG: reopen_tempfile called for an open object");
202 if (!tempfile->active)
203 die("BUG: reopen_tempfile called for an inactive object");
204 tempfile->fd = open(tempfile->filename.buf, O_WRONLY);
205 return tempfile->fd;
208 int rename_tempfile(struct tempfile *tempfile, const char *path)
210 if (!tempfile->active)
211 die("BUG: rename_tempfile called for inactive object");
213 if (close_tempfile(tempfile))
214 return -1;
216 if (rename(tempfile->filename.buf, path)) {
217 int save_errno = errno;
218 delete_tempfile(tempfile);
219 errno = save_errno;
220 return -1;
223 tempfile->active = 0;
224 strbuf_reset(&tempfile->filename);
225 return 0;
228 void delete_tempfile(struct tempfile *tempfile)
230 if (!tempfile->active)
231 return;
233 if (!close_tempfile(tempfile)) {
234 unlink_or_warn(tempfile->filename.buf);
235 tempfile->active = 0;
236 strbuf_reset(&tempfile->filename);