tempfile: use list.h for linked list
[git] / tempfile.h
1 #ifndef TEMPFILE_H
2 #define TEMPFILE_H
3
4 #include "list.h"
5
6 /*
7  * Handle temporary files.
8  *
9  * The tempfile API allows temporary files to be created, deleted, and
10  * atomically renamed. Temporary files that are still active when the
11  * program ends are cleaned up automatically. Lockfiles (see
12  * "lockfile.h") are built on top of this API.
13  *
14  *
15  * Calling sequence
16  * ----------------
17  *
18  * The caller:
19  *
20  * * Allocates a `struct tempfile` either as a static variable or on
21  *   the heap, initialized to zeros. Once you use the structure to
22  *   call `create_tempfile()`, it belongs to the tempfile subsystem
23  *   and its storage must remain valid throughout the life of the
24  *   program (i.e. you cannot use an on-stack variable to hold this
25  *   structure).
26  *
27  * * Attempts to create a temporary file by calling
28  *   `create_tempfile()`.
29  *
30  * * Writes new content to the file by either:
31  *
32  *   * writing to the file descriptor returned by `create_tempfile()`
33  *     (also available via `tempfile->fd`).
34  *
35  *   * calling `fdopen_tempfile()` to get a `FILE` pointer for the
36  *     open file and writing to the file using stdio.
37  *
38  *   Note that the file descriptor returned by create_tempfile()
39  *   is marked O_CLOEXEC, so the new contents must be written by
40  *   the current process, not any spawned one.
41  *
42  * When finished writing, the caller can:
43  *
44  * * Close the file descriptor and remove the temporary file by
45  *   calling `delete_tempfile()`.
46  *
47  * * Close the temporary file and rename it atomically to a specified
48  *   filename by calling `rename_tempfile()`. This relinquishes
49  *   control of the file.
50  *
51  * * Close the file descriptor without removing or renaming the
52  *   temporary file by calling `close_tempfile_gently()`, and later call
53  *   `delete_tempfile()` or `rename_tempfile()`.
54  *
55  * Even after the temporary file is renamed or deleted, the `tempfile`
56  * object must not be freed or altered by the caller. However, it may
57  * be reused; just pass it to another call of `create_tempfile()`.
58  *
59  * If the program exits before `rename_tempfile()` or
60  * `delete_tempfile()` is called, an `atexit(3)` handler will close
61  * and remove the temporary file.
62  *
63  * If you need to close the file descriptor yourself, do so by calling
64  * `close_tempfile_gently()`. You should never call `close(2)` or `fclose(3)`
65  * yourself, otherwise the `struct tempfile` structure would still
66  * think that the file descriptor needs to be closed, and a later
67  * cleanup would result in duplicate calls to `close(2)`. Worse yet,
68  * if you close and then later open another file descriptor for a
69  * completely different purpose, then the unrelated file descriptor
70  * might get closed.
71  *
72  *
73  * Error handling
74  * --------------
75  *
76  * `create_tempfile()` returns a file descriptor on success or -1 on
77  * failure. On errors, `errno` describes the reason for failure.
78  *
79  * `delete_tempfile()`, `rename_tempfile()`, and `close_tempfile_gently()`
80  * return 0 on success. On failure they set `errno` appropriately and return
81  * -1. `delete` and `rename` (but not `close`) do their best to delete the
82  * temporary file before returning.
83  */
84
85 struct tempfile {
86         volatile struct volatile_list_head list;
87         volatile sig_atomic_t active;
88         volatile int fd;
89         FILE *volatile fp;
90         volatile pid_t owner;
91         char on_list;
92         struct strbuf filename;
93 };
94
95 /*
96  * Attempt to create a temporary file at the specified `path`. Return
97  * a file descriptor for writing to it, or -1 on error. It is an error
98  * if a file already exists at that path.
99  */
100 extern int create_tempfile(struct tempfile *tempfile, const char *path);
101
102 /*
103  * Register an existing file as a tempfile, meaning that it will be
104  * deleted when the program exits. The tempfile is considered closed,
105  * but it can be worked with like any other closed tempfile (for
106  * example, it can be opened using reopen_tempfile()).
107  */
108 extern void register_tempfile(struct tempfile *tempfile, const char *path);
109
110
111 /*
112  * mks_tempfile functions
113  *
114  * The following functions attempt to create and open temporary files
115  * with names derived automatically from a template, in the manner of
116  * mkstemps(), and arrange for them to be deleted if the program ends
117  * before they are deleted explicitly. There is a whole family of such
118  * functions, named according to the following pattern:
119  *
120  *     x?mks_tempfile_t?s?m?()
121  *
122  * The optional letters have the following meanings:
123  *
124  *   x - die if the temporary file cannot be created.
125  *
126  *   t - create the temporary file under $TMPDIR (as opposed to
127  *       relative to the current directory). When these variants are
128  *       used, template should be the pattern for the filename alone,
129  *       without a path.
130  *
131  *   s - template includes a suffix that is suffixlen characters long.
132  *
133  *   m - the temporary file should be created with the specified mode
134  *       (otherwise, the mode is set to 0600).
135  *
136  * None of these functions modify template. If the caller wants to
137  * know the (absolute) path of the file that was created, it can be
138  * read from tempfile->filename.
139  *
140  * On success, the functions return a file descriptor that is open for
141  * writing the temporary file. On errors, they return -1 and set errno
142  * appropriately (except for the "x" variants, which die() on errors).
143  */
144
145 /* See "mks_tempfile functions" above. */
146 extern int mks_tempfile_sm(struct tempfile *tempfile,
147                            const char *template, int suffixlen, int mode);
148
149 /* See "mks_tempfile functions" above. */
150 static inline int mks_tempfile_s(struct tempfile *tempfile,
151                                  const char *template, int suffixlen)
152 {
153         return mks_tempfile_sm(tempfile, template, suffixlen, 0600);
154 }
155
156 /* See "mks_tempfile functions" above. */
157 static inline int mks_tempfile_m(struct tempfile *tempfile,
158                                  const char *template, int mode)
159 {
160         return mks_tempfile_sm(tempfile, template, 0, mode);
161 }
162
163 /* See "mks_tempfile functions" above. */
164 static inline int mks_tempfile(struct tempfile *tempfile,
165                                const char *template)
166 {
167         return mks_tempfile_sm(tempfile, template, 0, 0600);
168 }
169
170 /* See "mks_tempfile functions" above. */
171 extern int mks_tempfile_tsm(struct tempfile *tempfile,
172                             const char *template, int suffixlen, int mode);
173
174 /* See "mks_tempfile functions" above. */
175 static inline int mks_tempfile_ts(struct tempfile *tempfile,
176                                   const char *template, int suffixlen)
177 {
178         return mks_tempfile_tsm(tempfile, template, suffixlen, 0600);
179 }
180
181 /* See "mks_tempfile functions" above. */
182 static inline int mks_tempfile_tm(struct tempfile *tempfile,
183                                   const char *template, int mode)
184 {
185         return mks_tempfile_tsm(tempfile, template, 0, mode);
186 }
187
188 /* See "mks_tempfile functions" above. */
189 static inline int mks_tempfile_t(struct tempfile *tempfile,
190                                  const char *template)
191 {
192         return mks_tempfile_tsm(tempfile, template, 0, 0600);
193 }
194
195 /* See "mks_tempfile functions" above. */
196 extern int xmks_tempfile_m(struct tempfile *tempfile,
197                            const char *template, int mode);
198
199 /* See "mks_tempfile functions" above. */
200 static inline int xmks_tempfile(struct tempfile *tempfile,
201                                 const char *template)
202 {
203         return xmks_tempfile_m(tempfile, template, 0600);
204 }
205
206 /*
207  * Associate a stdio stream with the temporary file (which must still
208  * be open). Return `NULL` (*without* deleting the file) on error. The
209  * stream is closed automatically when `close_tempfile_gently()` is called or
210  * when the file is deleted or renamed.
211  */
212 extern FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode);
213
214 static inline int is_tempfile_active(struct tempfile *tempfile)
215 {
216         return tempfile && tempfile->active;
217 }
218
219 /*
220  * Return the path of the lockfile. The return value is a pointer to a
221  * field within the lock_file object and should not be freed.
222  */
223 extern const char *get_tempfile_path(struct tempfile *tempfile);
224
225 extern int get_tempfile_fd(struct tempfile *tempfile);
226 extern FILE *get_tempfile_fp(struct tempfile *tempfile);
227
228 /*
229  * If the temporary file is still open, close it (and the file pointer
230  * too, if it has been opened using `fdopen_tempfile()`) without
231  * deleting the file. Return 0 upon success. On failure to `close(2)`,
232  * return a negative value. Usually `delete_tempfile()` or `rename_tempfile()`
233  * should eventually be called regardless of whether `close_tempfile_gently()`
234  * succeeds.
235  */
236 extern int close_tempfile_gently(struct tempfile *tempfile);
237
238 /*
239  * Re-open a temporary file that has been closed using
240  * `close_tempfile_gently()` but not yet deleted or renamed. This can be used
241  * to implement a sequence of operations like the following:
242  *
243  * * Create temporary file.
244  *
245  * * Write new contents to file, then `close_tempfile_gently()` to cause the
246  *   contents to be written to disk.
247  *
248  * * Pass the name of the temporary file to another program to allow
249  *   it (and nobody else) to inspect or even modify the file's
250  *   contents.
251  *
252  * * `reopen_tempfile()` to reopen the temporary file. Make further
253  *   updates to the contents.
254  *
255  * * `rename_tempfile()` to move the file to its permanent location.
256  */
257 extern int reopen_tempfile(struct tempfile *tempfile);
258
259 /*
260  * Close the file descriptor and/or file pointer and remove the
261  * temporary file associated with `tempfile`. It is a NOOP to call
262  * `delete_tempfile()` for a `tempfile` object that has already been
263  * deleted or renamed.
264  */
265 extern void delete_tempfile(struct tempfile *tempfile);
266
267 /*
268  * Close the file descriptor and/or file pointer if they are still
269  * open, and atomically rename the temporary file to `path`. `path`
270  * must be on the same filesystem as the lock file. Return 0 on
271  * success. On failure, delete the temporary file and return -1, with
272  * `errno` set to the value from the failing call to `close(2)` or
273  * `rename(2)`. It is a bug to call `rename_tempfile()` for a
274  * `tempfile` object that is not currently active.
275  */
276 extern int rename_tempfile(struct tempfile *tempfile, const char *path);
277
278 #endif /* TEMPFILE_H */