7 * Handle temporary files.
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.
20 * * Allocates a `struct tempfile`. Once the structure is passed to
21 * `create_tempfile()`, its storage must remain valid until
22 * `delete_tempfile()` or `rename_tempfile()` is called on it.
24 * * Attempts to create a temporary file by calling
25 * `create_tempfile()`.
27 * * Writes new content to the file by either:
29 * * writing to the file descriptor returned by `create_tempfile()`
30 * (also available via `tempfile->fd`).
32 * * calling `fdopen_tempfile()` to get a `FILE` pointer for the
33 * open file and writing to the file using stdio.
35 * Note that the file descriptor returned by create_tempfile()
36 * is marked O_CLOEXEC, so the new contents must be written by
37 * the current process, not any spawned one.
39 * When finished writing, the caller can:
41 * * Close the file descriptor and remove the temporary file by
42 * calling `delete_tempfile()`.
44 * * Close the temporary file and rename it atomically to a specified
45 * filename by calling `rename_tempfile()`. This relinquishes
46 * control of the file.
48 * * Close the file descriptor without removing or renaming the
49 * temporary file by calling `close_tempfile_gently()`, and later call
50 * `delete_tempfile()` or `rename_tempfile()`.
52 * After the temporary file is renamed or deleted, the `tempfile`
53 * object may be reused or freed.
55 * If the program exits before `rename_tempfile()` or
56 * `delete_tempfile()` is called, an `atexit(3)` handler will close
57 * and remove the temporary file.
59 * If you need to close the file descriptor yourself, do so by calling
60 * `close_tempfile_gently()`. You should never call `close(2)` or `fclose(3)`
61 * yourself, otherwise the `struct tempfile` structure would still
62 * think that the file descriptor needs to be closed, and a later
63 * cleanup would result in duplicate calls to `close(2)`. Worse yet,
64 * if you close and then later open another file descriptor for a
65 * completely different purpose, then the unrelated file descriptor
72 * `create_tempfile()` returns a file descriptor on success or -1 on
73 * failure. On errors, `errno` describes the reason for failure.
75 * `delete_tempfile()`, `rename_tempfile()`, and `close_tempfile_gently()`
76 * return 0 on success. On failure they set `errno` appropriately and return
77 * -1. `delete` and `rename` (but not `close`) do their best to delete the
78 * temporary file before returning.
82 volatile struct volatile_list_head list
;
83 volatile sig_atomic_t active
;
87 struct strbuf filename
;
91 * Attempt to create a temporary file at the specified `path`. Return
92 * a file descriptor for writing to it, or -1 on error. It is an error
93 * if a file already exists at that path.
95 extern int create_tempfile(struct tempfile
*tempfile
, const char *path
);
98 * Register an existing file as a tempfile, meaning that it will be
99 * deleted when the program exits. The tempfile is considered closed,
100 * but it can be worked with like any other closed tempfile (for
101 * example, it can be opened using reopen_tempfile()).
103 extern void register_tempfile(struct tempfile
*tempfile
, const char *path
);
107 * mks_tempfile functions
109 * The following functions attempt to create and open temporary files
110 * with names derived automatically from a template, in the manner of
111 * mkstemps(), and arrange for them to be deleted if the program ends
112 * before they are deleted explicitly. There is a whole family of such
113 * functions, named according to the following pattern:
115 * x?mks_tempfile_t?s?m?()
117 * The optional letters have the following meanings:
119 * x - die if the temporary file cannot be created.
121 * t - create the temporary file under $TMPDIR (as opposed to
122 * relative to the current directory). When these variants are
123 * used, template should be the pattern for the filename alone,
126 * s - template includes a suffix that is suffixlen characters long.
128 * m - the temporary file should be created with the specified mode
129 * (otherwise, the mode is set to 0600).
131 * None of these functions modify template. If the caller wants to
132 * know the (absolute) path of the file that was created, it can be
133 * read from tempfile->filename.
135 * On success, the functions return a file descriptor that is open for
136 * writing the temporary file. On errors, they return -1 and set errno
137 * appropriately (except for the "x" variants, which die() on errors).
140 /* See "mks_tempfile functions" above. */
141 extern int mks_tempfile_sm(struct tempfile
*tempfile
,
142 const char *template, int suffixlen
, int mode
);
144 /* See "mks_tempfile functions" above. */
145 static inline int mks_tempfile_s(struct tempfile
*tempfile
,
146 const char *template, int suffixlen
)
148 return mks_tempfile_sm(tempfile
, template, suffixlen
, 0600);
151 /* See "mks_tempfile functions" above. */
152 static inline int mks_tempfile_m(struct tempfile
*tempfile
,
153 const char *template, int mode
)
155 return mks_tempfile_sm(tempfile
, template, 0, mode
);
158 /* See "mks_tempfile functions" above. */
159 static inline int mks_tempfile(struct tempfile
*tempfile
,
160 const char *template)
162 return mks_tempfile_sm(tempfile
, template, 0, 0600);
165 /* See "mks_tempfile functions" above. */
166 extern int mks_tempfile_tsm(struct tempfile
*tempfile
,
167 const char *template, int suffixlen
, int mode
);
169 /* See "mks_tempfile functions" above. */
170 static inline int mks_tempfile_ts(struct tempfile
*tempfile
,
171 const char *template, int suffixlen
)
173 return mks_tempfile_tsm(tempfile
, template, suffixlen
, 0600);
176 /* See "mks_tempfile functions" above. */
177 static inline int mks_tempfile_tm(struct tempfile
*tempfile
,
178 const char *template, int mode
)
180 return mks_tempfile_tsm(tempfile
, template, 0, mode
);
183 /* See "mks_tempfile functions" above. */
184 static inline int mks_tempfile_t(struct tempfile
*tempfile
,
185 const char *template)
187 return mks_tempfile_tsm(tempfile
, template, 0, 0600);
190 /* See "mks_tempfile functions" above. */
191 extern int xmks_tempfile_m(struct tempfile
*tempfile
,
192 const char *template, int mode
);
194 /* See "mks_tempfile functions" above. */
195 static inline int xmks_tempfile(struct tempfile
*tempfile
,
196 const char *template)
198 return xmks_tempfile_m(tempfile
, template, 0600);
202 * Associate a stdio stream with the temporary file (which must still
203 * be open). Return `NULL` (*without* deleting the file) on error. The
204 * stream is closed automatically when `close_tempfile_gently()` is called or
205 * when the file is deleted or renamed.
207 extern FILE *fdopen_tempfile(struct tempfile
*tempfile
, const char *mode
);
209 static inline int is_tempfile_active(struct tempfile
*tempfile
)
211 return tempfile
&& tempfile
->active
;
215 * Return the path of the lockfile. The return value is a pointer to a
216 * field within the lock_file object and should not be freed.
218 extern const char *get_tempfile_path(struct tempfile
*tempfile
);
220 extern int get_tempfile_fd(struct tempfile
*tempfile
);
221 extern FILE *get_tempfile_fp(struct tempfile
*tempfile
);
224 * If the temporary file is still open, close it (and the file pointer
225 * too, if it has been opened using `fdopen_tempfile()`) without
226 * deleting the file. Return 0 upon success. On failure to `close(2)`,
227 * return a negative value. Usually `delete_tempfile()` or `rename_tempfile()`
228 * should eventually be called regardless of whether `close_tempfile_gently()`
231 extern int close_tempfile_gently(struct tempfile
*tempfile
);
234 * Re-open a temporary file that has been closed using
235 * `close_tempfile_gently()` but not yet deleted or renamed. This can be used
236 * to implement a sequence of operations like the following:
238 * * Create temporary file.
240 * * Write new contents to file, then `close_tempfile_gently()` to cause the
241 * contents to be written to disk.
243 * * Pass the name of the temporary file to another program to allow
244 * it (and nobody else) to inspect or even modify the file's
247 * * `reopen_tempfile()` to reopen the temporary file. Make further
248 * updates to the contents.
250 * * `rename_tempfile()` to move the file to its permanent location.
252 extern int reopen_tempfile(struct tempfile
*tempfile
);
255 * Close the file descriptor and/or file pointer and remove the
256 * temporary file associated with `tempfile`. It is a NOOP to call
257 * `delete_tempfile()` for a `tempfile` object that has already been
258 * deleted or renamed.
260 extern void delete_tempfile(struct tempfile
*tempfile
);
263 * Close the file descriptor and/or file pointer if they are still
264 * open, and atomically rename the temporary file to `path`. `path`
265 * must be on the same filesystem as the lock file. Return 0 on
266 * success. On failure, delete the temporary file and return -1, with
267 * `errno` set to the value from the failing call to `close(2)` or
268 * `rename(2)`. It is a bug to call `rename_tempfile()` for a
269 * `tempfile` object that is not currently active.
271 extern int rename_tempfile(struct tempfile
*tempfile
, const char *path
);
273 #endif /* TEMPFILE_H */