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