Merge branch 'bw/clone-recursive-quiet' into maint
[git.git] / tempfile.h
blob2f0038decd5b6d00b55fa03ec8988a3810d1784f
1 #ifndef TEMPFILE_H
2 #define TEMPFILE_H
4 /*
5 * Handle temporary files.
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.
13 * Calling sequence
14 * ----------------
16 * The caller:
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).
25 * * Attempts to create a temporary file by calling
26 * `create_tempfile()`.
28 * * Writes new content to the file by either:
30 * * writing to the file descriptor returned by `create_tempfile()`
31 * (also available via `tempfile->fd`).
33 * * calling `fdopen_tempfile()` to get a `FILE` pointer for the
34 * open file and writing to the file using stdio.
36 * Note that the file descriptor returned by create_tempfile()
37 * is marked O_CLOEXEC, so the new contents must be written by
38 * the current process, not any spawned one.
40 * When finished writing, the caller can:
42 * * Close the file descriptor and remove the temporary file by
43 * calling `delete_tempfile()`.
45 * * Close the temporary file and rename it atomically to a specified
46 * filename by calling `rename_tempfile()`. This relinquishes
47 * control of the file.
49 * * Close the file descriptor without removing or renaming the
50 * temporary file by calling `close_tempfile()`, and later call
51 * `delete_tempfile()` or `rename_tempfile()`.
53 * Even after the temporary file is renamed or deleted, the `tempfile`
54 * object must not be freed or altered by the caller. However, it may
55 * be reused; just pass it to another call of `create_tempfile()`.
57 * If the program exits before `rename_tempfile()` or
58 * `delete_tempfile()` is called, an `atexit(3)` handler will close
59 * and remove the temporary file.
61 * If you need to close the file descriptor yourself, do so by calling
62 * `close_tempfile()`. You should never call `close(2)` or `fclose(3)`
63 * yourself, otherwise the `struct tempfile` structure would still
64 * think that the file descriptor needs to be closed, and a later
65 * cleanup would result in duplicate calls to `close(2)`. Worse yet,
66 * if you close and then later open another file descriptor for a
67 * completely different purpose, then the unrelated file descriptor
68 * might get closed.
71 * Error handling
72 * --------------
74 * `create_tempfile()` returns a file descriptor on success or -1 on
75 * failure. On errors, `errno` describes the reason for failure.
77 * `delete_tempfile()`, `rename_tempfile()`, and `close_tempfile()`
78 * return 0 on success. On failure they set `errno` appropriately, do
79 * their best to delete the temporary file, and return -1.
82 struct tempfile {
83 struct tempfile *volatile next;
84 volatile sig_atomic_t active;
85 volatile int fd;
86 FILE *volatile fp;
87 volatile pid_t owner;
88 char on_list;
89 struct strbuf filename;
93 * Attempt to create a temporary file at the specified `path`. Return
94 * a file descriptor for writing to it, or -1 on error. It is an error
95 * if a file already exists at that path.
97 extern int create_tempfile(struct tempfile *tempfile, const char *path);
100 * Register an existing file as a tempfile, meaning that it will be
101 * deleted when the program exits. The tempfile is considered closed,
102 * but it can be worked with like any other closed tempfile (for
103 * example, it can be opened using reopen_tempfile()).
105 extern void register_tempfile(struct tempfile *tempfile, const char *path);
109 * mks_tempfile functions
111 * The following functions attempt to create and open temporary files
112 * with names derived automatically from a template, in the manner of
113 * mkstemps(), and arrange for them to be deleted if the program ends
114 * before they are deleted explicitly. There is a whole family of such
115 * functions, named according to the following pattern:
117 * x?mks_tempfile_t?s?m?()
119 * The optional letters have the following meanings:
121 * x - die if the temporary file cannot be created.
123 * t - create the temporary file under $TMPDIR (as opposed to
124 * relative to the current directory). When these variants are
125 * used, template should be the pattern for the filename alone,
126 * without a path.
128 * s - template includes a suffix that is suffixlen characters long.
130 * m - the temporary file should be created with the specified mode
131 * (otherwise, the mode is set to 0600).
133 * None of these functions modify template. If the caller wants to
134 * know the (absolute) path of the file that was created, it can be
135 * read from tempfile->filename.
137 * On success, the functions return a file descriptor that is open for
138 * writing the temporary file. On errors, they return -1 and set errno
139 * appropriately (except for the "x" variants, which die() on errors).
142 /* See "mks_tempfile functions" above. */
143 extern int mks_tempfile_sm(struct tempfile *tempfile,
144 const char *template, int suffixlen, int mode);
146 /* See "mks_tempfile functions" above. */
147 static inline int mks_tempfile_s(struct tempfile *tempfile,
148 const char *template, int suffixlen)
150 return mks_tempfile_sm(tempfile, template, suffixlen, 0600);
153 /* See "mks_tempfile functions" above. */
154 static inline int mks_tempfile_m(struct tempfile *tempfile,
155 const char *template, int mode)
157 return mks_tempfile_sm(tempfile, template, 0, mode);
160 /* See "mks_tempfile functions" above. */
161 static inline int mks_tempfile(struct tempfile *tempfile,
162 const char *template)
164 return mks_tempfile_sm(tempfile, template, 0, 0600);
167 /* See "mks_tempfile functions" above. */
168 extern int mks_tempfile_tsm(struct tempfile *tempfile,
169 const char *template, int suffixlen, int mode);
171 /* See "mks_tempfile functions" above. */
172 static inline int mks_tempfile_ts(struct tempfile *tempfile,
173 const char *template, int suffixlen)
175 return mks_tempfile_tsm(tempfile, template, suffixlen, 0600);
178 /* See "mks_tempfile functions" above. */
179 static inline int mks_tempfile_tm(struct tempfile *tempfile,
180 const char *template, int mode)
182 return mks_tempfile_tsm(tempfile, template, 0, mode);
185 /* See "mks_tempfile functions" above. */
186 static inline int mks_tempfile_t(struct tempfile *tempfile,
187 const char *template)
189 return mks_tempfile_tsm(tempfile, template, 0, 0600);
192 /* See "mks_tempfile functions" above. */
193 extern int xmks_tempfile_m(struct tempfile *tempfile,
194 const char *template, int mode);
196 /* See "mks_tempfile functions" above. */
197 static inline int xmks_tempfile(struct tempfile *tempfile,
198 const char *template)
200 return xmks_tempfile_m(tempfile, template, 0600);
204 * Associate a stdio stream with the temporary file (which must still
205 * be open). Return `NULL` (*without* deleting the file) on error. The
206 * stream is closed automatically when `close_tempfile()` is called or
207 * when the file is deleted or renamed.
209 extern FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode);
211 static inline int is_tempfile_active(struct tempfile *tempfile)
213 return tempfile->active;
217 * Return the path of the lockfile. The return value is a pointer to a
218 * field within the lock_file object and should not be freed.
220 extern const char *get_tempfile_path(struct tempfile *tempfile);
222 extern int get_tempfile_fd(struct tempfile *tempfile);
223 extern FILE *get_tempfile_fp(struct tempfile *tempfile);
226 * If the temporary file is still open, close it (and the file pointer
227 * too, if it has been opened using `fdopen_tempfile()`) without
228 * deleting the file. Return 0 upon success. On failure to `close(2)`,
229 * return a negative value and delete the file. Usually
230 * `delete_tempfile()` or `rename_tempfile()` should eventually be
231 * called if `close_tempfile()` succeeds.
233 extern int close_tempfile(struct tempfile *tempfile);
236 * Re-open a temporary file that has been closed using
237 * `close_tempfile()` but not yet deleted or renamed. This can be used
238 * to implement a sequence of operations like the following:
240 * * Create temporary file.
242 * * Write new contents to file, then `close_tempfile()` to cause the
243 * contents to be written to disk.
245 * * Pass the name of the temporary file to another program to allow
246 * it (and nobody else) to inspect or even modify the file's
247 * contents.
249 * * `reopen_tempfile()` to reopen the temporary file. Make further
250 * updates to the contents.
252 * * `rename_tempfile()` to move the file to its permanent location.
254 extern int reopen_tempfile(struct tempfile *tempfile);
257 * Close the file descriptor and/or file pointer and remove the
258 * temporary file associated with `tempfile`. It is a NOOP to call
259 * `delete_tempfile()` for a `tempfile` object that has already been
260 * deleted or renamed.
262 extern void delete_tempfile(struct tempfile *tempfile);
265 * Close the file descriptor and/or file pointer if they are still
266 * open, and atomically rename the temporary file to `path`. `path`
267 * must be on the same filesystem as the lock file. Return 0 on
268 * success. On failure, delete the temporary file and return -1, with
269 * `errno` set to the value from the failing call to `close(2)` or
270 * `rename(2)`. It is a bug to call `rename_tempfile()` for a
271 * `tempfile` object that is not currently active.
273 extern int rename_tempfile(struct tempfile *tempfile, const char *path);
275 #endif /* TEMPFILE_H */