]> git.ipfire.org Git - thirdparty/git.git/blame - tempfile.h
tempfile: use list.h for linked list
[thirdparty/git.git] / tempfile.h
CommitLineData
1a9d15db
MH
1#ifndef TEMPFILE_H
2#define TEMPFILE_H
3
24d82185
JK
4#include "list.h"
5
1a9d15db
MH
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 *
05d1ed61
BW
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 *
1a9d15db
MH
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
49bd0fc2 52 * temporary file by calling `close_tempfile_gently()`, and later call
1a9d15db
MH
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
49bd0fc2 64 * `close_tempfile_gently()`. You should never call `close(2)` or `fclose(3)`
1a9d15db
MH
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 *
49bd0fc2
JK
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.
1a9d15db
MH
83 */
84
85struct tempfile {
24d82185 86 volatile struct volatile_list_head list;
1a9d15db
MH
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 */
100extern int create_tempfile(struct tempfile *tempfile, const char *path);
101
99397152
MH
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 */
108extern void register_tempfile(struct tempfile *tempfile, const char *path);
109
354ab112
MH
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. */
146extern int mks_tempfile_sm(struct tempfile *tempfile,
147 const char *template, int suffixlen, int mode);
148
149/* See "mks_tempfile functions" above. */
150static 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. */
157static 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. */
164static 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. */
171extern int mks_tempfile_tsm(struct tempfile *tempfile,
172 const char *template, int suffixlen, int mode);
173
174/* See "mks_tempfile functions" above. */
175static 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. */
182static 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. */
189static 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. */
196extern int xmks_tempfile_m(struct tempfile *tempfile,
197 const char *template, int mode);
198
199/* See "mks_tempfile functions" above. */
200static inline int xmks_tempfile(struct tempfile *tempfile,
201 const char *template)
202{
203 return xmks_tempfile_m(tempfile, template, 0600);
204}
205
1a9d15db
MH
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
49bd0fc2 209 * stream is closed automatically when `close_tempfile_gently()` is called or
1a9d15db
MH
210 * when the file is deleted or renamed.
211 */
212extern FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode);
213
214static inline int is_tempfile_active(struct tempfile *tempfile)
215{
f5b4dc76 216 return tempfile && tempfile->active;
1a9d15db
MH
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 */
223extern const char *get_tempfile_path(struct tempfile *tempfile);
224
225extern int get_tempfile_fd(struct tempfile *tempfile);
226extern 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)`,
49bd0fc2
JK
232 * return a negative value. Usually `delete_tempfile()` or `rename_tempfile()`
233 * should eventually be called regardless of whether `close_tempfile_gently()`
234 * succeeds.
1a9d15db 235 */
49bd0fc2 236extern int close_tempfile_gently(struct tempfile *tempfile);
1a9d15db
MH
237
238/*
239 * Re-open a temporary file that has been closed using
49bd0fc2 240 * `close_tempfile_gently()` but not yet deleted or renamed. This can be used
1a9d15db
MH
241 * to implement a sequence of operations like the following:
242 *
243 * * Create temporary file.
244 *
49bd0fc2 245 * * Write new contents to file, then `close_tempfile_gently()` to cause the
1a9d15db
MH
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 */
257extern 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 */
265extern 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 */
276extern int rename_tempfile(struct tempfile *tempfile, const char *path);
277
278#endif /* TEMPFILE_H */