wrapper.h: move declarations for wrapper.c functions from cache.h
[git/debian.git] / tempfile.c
blobcdd2cab3bada725e7276665ace87a2d7cab353db
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 "cache.h"
46 #include "tempfile.h"
47 #include "sigchain.h"
48 #include "wrapper.h"
50 static VOLATILE_LIST_HEAD(tempfile_list);
52 static void remove_template_directory(struct tempfile *tempfile,
53 int in_signal_handler)
55 if (tempfile->directory) {
56 if (in_signal_handler)
57 rmdir(tempfile->directory);
58 else
59 rmdir_or_warn(tempfile->directory);
63 static void remove_tempfiles(int in_signal_handler)
65 pid_t me = getpid();
66 volatile struct volatile_list_head *pos;
68 list_for_each(pos, &tempfile_list) {
69 struct tempfile *p = list_entry(pos, struct tempfile, list);
71 if (!is_tempfile_active(p) || p->owner != me)
72 continue;
74 if (p->fd >= 0)
75 close(p->fd);
77 if (in_signal_handler)
78 unlink(p->filename.buf);
79 else
80 unlink_or_warn(p->filename.buf);
81 remove_template_directory(p, in_signal_handler);
85 static void remove_tempfiles_on_exit(void)
87 remove_tempfiles(0);
90 static void remove_tempfiles_on_signal(int signo)
92 remove_tempfiles(1);
93 sigchain_pop(signo);
94 raise(signo);
97 static struct tempfile *new_tempfile(void)
99 struct tempfile *tempfile = xmalloc(sizeof(*tempfile));
100 tempfile->fd = -1;
101 tempfile->fp = NULL;
102 tempfile->owner = 0;
103 INIT_LIST_HEAD(&tempfile->list);
104 strbuf_init(&tempfile->filename, 0);
105 tempfile->directory = NULL;
106 return tempfile;
109 static void activate_tempfile(struct tempfile *tempfile)
111 static int initialized;
113 if (!initialized) {
114 sigchain_push_common(remove_tempfiles_on_signal);
115 atexit(remove_tempfiles_on_exit);
116 initialized = 1;
119 volatile_list_add(&tempfile->list, &tempfile_list);
120 tempfile->owner = getpid();
123 static void deactivate_tempfile(struct tempfile *tempfile)
125 volatile_list_del(&tempfile->list);
126 strbuf_release(&tempfile->filename);
127 free(tempfile->directory);
128 free(tempfile);
131 /* Make sure errno contains a meaningful value on error */
132 struct tempfile *create_tempfile_mode(const char *path, int mode)
134 struct tempfile *tempfile = new_tempfile();
136 strbuf_add_absolute_path(&tempfile->filename, path);
137 tempfile->fd = open(tempfile->filename.buf,
138 O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, mode);
139 if (O_CLOEXEC && tempfile->fd < 0 && errno == EINVAL)
140 /* Try again w/o O_CLOEXEC: the kernel might not support it */
141 tempfile->fd = open(tempfile->filename.buf,
142 O_RDWR | O_CREAT | O_EXCL, mode);
143 if (tempfile->fd < 0) {
144 deactivate_tempfile(tempfile);
145 return NULL;
147 activate_tempfile(tempfile);
148 if (adjust_shared_perm(tempfile->filename.buf)) {
149 int save_errno = errno;
150 error("cannot fix permission bits on %s", tempfile->filename.buf);
151 delete_tempfile(&tempfile);
152 errno = save_errno;
153 return NULL;
156 return tempfile;
159 struct tempfile *register_tempfile(const char *path)
161 struct tempfile *tempfile = new_tempfile();
162 strbuf_add_absolute_path(&tempfile->filename, path);
163 activate_tempfile(tempfile);
164 return tempfile;
167 struct tempfile *mks_tempfile_sm(const char *filename_template, int suffixlen, int mode)
169 struct tempfile *tempfile = new_tempfile();
171 strbuf_add_absolute_path(&tempfile->filename, filename_template);
172 tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
173 if (tempfile->fd < 0) {
174 deactivate_tempfile(tempfile);
175 return NULL;
177 activate_tempfile(tempfile);
178 return tempfile;
181 struct tempfile *mks_tempfile_tsm(const char *filename_template, int suffixlen, int mode)
183 struct tempfile *tempfile = new_tempfile();
184 const char *tmpdir;
186 tmpdir = getenv("TMPDIR");
187 if (!tmpdir)
188 tmpdir = "/tmp";
190 strbuf_addf(&tempfile->filename, "%s/%s", tmpdir, filename_template);
191 tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
192 if (tempfile->fd < 0) {
193 deactivate_tempfile(tempfile);
194 return NULL;
196 activate_tempfile(tempfile);
197 return tempfile;
200 struct tempfile *mks_tempfile_dt(const char *directory_template,
201 const char *filename)
203 struct tempfile *tempfile;
204 const char *tmpdir;
205 struct strbuf sb = STRBUF_INIT;
206 int fd;
207 size_t directorylen;
209 if (!ends_with(directory_template, "XXXXXX")) {
210 errno = EINVAL;
211 return NULL;
214 tmpdir = getenv("TMPDIR");
215 if (!tmpdir)
216 tmpdir = "/tmp";
218 strbuf_addf(&sb, "%s/%s", tmpdir, directory_template);
219 directorylen = sb.len;
220 if (!mkdtemp(sb.buf)) {
221 int orig_errno = errno;
222 strbuf_release(&sb);
223 errno = orig_errno;
224 return NULL;
227 strbuf_addf(&sb, "/%s", filename);
228 fd = open(sb.buf, O_CREAT | O_EXCL | O_RDWR, 0600);
229 if (fd < 0) {
230 int orig_errno = errno;
231 strbuf_setlen(&sb, directorylen);
232 rmdir(sb.buf);
233 strbuf_release(&sb);
234 errno = orig_errno;
235 return NULL;
238 tempfile = new_tempfile();
239 strbuf_swap(&tempfile->filename, &sb);
240 tempfile->directory = xmemdupz(tempfile->filename.buf, directorylen);
241 tempfile->fd = fd;
242 activate_tempfile(tempfile);
243 return tempfile;
246 struct tempfile *xmks_tempfile_m(const char *filename_template, int mode)
248 struct tempfile *tempfile;
249 struct strbuf full_template = STRBUF_INIT;
251 strbuf_add_absolute_path(&full_template, filename_template);
252 tempfile = mks_tempfile_m(full_template.buf, mode);
253 if (!tempfile)
254 die_errno("Unable to create temporary file '%s'",
255 full_template.buf);
257 strbuf_release(&full_template);
258 return tempfile;
261 FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode)
263 if (!is_tempfile_active(tempfile))
264 BUG("fdopen_tempfile() called for inactive object");
265 if (tempfile->fp)
266 BUG("fdopen_tempfile() called for open object");
268 tempfile->fp = fdopen(tempfile->fd, mode);
269 return tempfile->fp;
272 const char *get_tempfile_path(struct tempfile *tempfile)
274 if (!is_tempfile_active(tempfile))
275 BUG("get_tempfile_path() called for inactive object");
276 return tempfile->filename.buf;
279 int get_tempfile_fd(struct tempfile *tempfile)
281 if (!is_tempfile_active(tempfile))
282 BUG("get_tempfile_fd() called for inactive object");
283 return tempfile->fd;
286 FILE *get_tempfile_fp(struct tempfile *tempfile)
288 if (!is_tempfile_active(tempfile))
289 BUG("get_tempfile_fp() called for inactive object");
290 return tempfile->fp;
293 int close_tempfile_gently(struct tempfile *tempfile)
295 int fd;
296 FILE *fp;
297 int err;
299 if (!is_tempfile_active(tempfile) || tempfile->fd < 0)
300 return 0;
302 fd = tempfile->fd;
303 fp = tempfile->fp;
304 tempfile->fd = -1;
305 if (fp) {
306 tempfile->fp = NULL;
307 if (ferror(fp)) {
308 err = -1;
309 if (!fclose(fp))
310 errno = EIO;
311 } else {
312 err = fclose(fp);
314 } else {
315 err = close(fd);
318 return err ? -1 : 0;
321 int reopen_tempfile(struct tempfile *tempfile)
323 if (!is_tempfile_active(tempfile))
324 BUG("reopen_tempfile called for an inactive object");
325 if (0 <= tempfile->fd)
326 BUG("reopen_tempfile called for an open object");
327 tempfile->fd = open(tempfile->filename.buf, O_WRONLY|O_TRUNC);
328 return tempfile->fd;
331 int rename_tempfile(struct tempfile **tempfile_p, const char *path)
333 struct tempfile *tempfile = *tempfile_p;
335 if (!is_tempfile_active(tempfile))
336 BUG("rename_tempfile called for inactive object");
338 if (close_tempfile_gently(tempfile)) {
339 delete_tempfile(tempfile_p);
340 return -1;
343 if (rename(tempfile->filename.buf, path)) {
344 int save_errno = errno;
345 delete_tempfile(tempfile_p);
346 errno = save_errno;
347 return -1;
350 deactivate_tempfile(tempfile);
351 *tempfile_p = NULL;
352 return 0;
355 void delete_tempfile(struct tempfile **tempfile_p)
357 struct tempfile *tempfile = *tempfile_p;
359 if (!is_tempfile_active(tempfile))
360 return;
362 close_tempfile_gently(tempfile);
363 unlink_or_warn(tempfile->filename.buf);
364 remove_template_directory(tempfile, 0);
365 deactivate_tempfile(tempfile);
366 *tempfile_p = NULL;