lockfile: do not rollback lock on failed close
[git.git] / tempfile.h
blobd854dcdd3eb513c3198a7fb5c58a7a7d0b08a4d8
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_gently()`, 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_gently()`. 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_gently()`
78 * return 0 on success. On failure they set `errno` appropriately and return
79 * -1. `delete` and `rename` (but not `close`) do their best to delete the
80 * temporary file before returning.
83 struct tempfile {
84 struct tempfile *volatile next;
85 volatile sig_atomic_t active;
86 volatile int fd;
87 FILE *volatile fp;
88 volatile pid_t owner;
89 char on_list;
90 struct strbuf filename;
94 * Attempt to create a temporary file at the specified `path`. Return
95 * a file descriptor for writing to it, or -1 on error. It is an error
96 * if a file already exists at that path.
98 extern int create_tempfile(struct tempfile *tempfile, const char *path);
101 * Register an existing file as a tempfile, meaning that it will be
102 * deleted when the program exits. The tempfile is considered closed,
103 * but it can be worked with like any other closed tempfile (for
104 * example, it can be opened using reopen_tempfile()).
106 extern void register_tempfile(struct tempfile *tempfile, const char *path);
110 * mks_tempfile functions
112 * The following functions attempt to create and open temporary files
113 * with names derived automatically from a template, in the manner of
114 * mkstemps(), and arrange for them to be deleted if the program ends
115 * before they are deleted explicitly. There is a whole family of such
116 * functions, named according to the following pattern:
118 * x?mks_tempfile_t?s?m?()
120 * The optional letters have the following meanings:
122 * x - die if the temporary file cannot be created.
124 * t - create the temporary file under $TMPDIR (as opposed to
125 * relative to the current directory). When these variants are
126 * used, template should be the pattern for the filename alone,
127 * without a path.
129 * s - template includes a suffix that is suffixlen characters long.
131 * m - the temporary file should be created with the specified mode
132 * (otherwise, the mode is set to 0600).
134 * None of these functions modify template. If the caller wants to
135 * know the (absolute) path of the file that was created, it can be
136 * read from tempfile->filename.
138 * On success, the functions return a file descriptor that is open for
139 * writing the temporary file. On errors, they return -1 and set errno
140 * appropriately (except for the "x" variants, which die() on errors).
143 /* See "mks_tempfile functions" above. */
144 extern int mks_tempfile_sm(struct tempfile *tempfile,
145 const char *template, int suffixlen, int mode);
147 /* See "mks_tempfile functions" above. */
148 static inline int mks_tempfile_s(struct tempfile *tempfile,
149 const char *template, int suffixlen)
151 return mks_tempfile_sm(tempfile, template, suffixlen, 0600);
154 /* See "mks_tempfile functions" above. */
155 static inline int mks_tempfile_m(struct tempfile *tempfile,
156 const char *template, int mode)
158 return mks_tempfile_sm(tempfile, template, 0, mode);
161 /* See "mks_tempfile functions" above. */
162 static inline int mks_tempfile(struct tempfile *tempfile,
163 const char *template)
165 return mks_tempfile_sm(tempfile, template, 0, 0600);
168 /* See "mks_tempfile functions" above. */
169 extern int mks_tempfile_tsm(struct tempfile *tempfile,
170 const char *template, int suffixlen, int mode);
172 /* See "mks_tempfile functions" above. */
173 static inline int mks_tempfile_ts(struct tempfile *tempfile,
174 const char *template, int suffixlen)
176 return mks_tempfile_tsm(tempfile, template, suffixlen, 0600);
179 /* See "mks_tempfile functions" above. */
180 static inline int mks_tempfile_tm(struct tempfile *tempfile,
181 const char *template, int mode)
183 return mks_tempfile_tsm(tempfile, template, 0, mode);
186 /* See "mks_tempfile functions" above. */
187 static inline int mks_tempfile_t(struct tempfile *tempfile,
188 const char *template)
190 return mks_tempfile_tsm(tempfile, template, 0, 0600);
193 /* See "mks_tempfile functions" above. */
194 extern int xmks_tempfile_m(struct tempfile *tempfile,
195 const char *template, int mode);
197 /* See "mks_tempfile functions" above. */
198 static inline int xmks_tempfile(struct tempfile *tempfile,
199 const char *template)
201 return xmks_tempfile_m(tempfile, template, 0600);
205 * Associate a stdio stream with the temporary file (which must still
206 * be open). Return `NULL` (*without* deleting the file) on error. The
207 * stream is closed automatically when `close_tempfile_gently()` is called or
208 * when the file is deleted or renamed.
210 extern FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode);
212 static inline int is_tempfile_active(struct tempfile *tempfile)
214 return tempfile->active;
218 * Return the path of the lockfile. The return value is a pointer to a
219 * field within the lock_file object and should not be freed.
221 extern const char *get_tempfile_path(struct tempfile *tempfile);
223 extern int get_tempfile_fd(struct tempfile *tempfile);
224 extern FILE *get_tempfile_fp(struct tempfile *tempfile);
227 * If the temporary file is still open, close it (and the file pointer
228 * too, if it has been opened using `fdopen_tempfile()`) without
229 * deleting the file. Return 0 upon success. On failure to `close(2)`,
230 * return a negative value. Usually `delete_tempfile()` or `rename_tempfile()`
231 * should eventually be called regardless of whether `close_tempfile_gently()`
232 * succeeds.
234 extern int close_tempfile_gently(struct tempfile *tempfile);
237 * Re-open a temporary file that has been closed using
238 * `close_tempfile_gently()` but not yet deleted or renamed. This can be used
239 * to implement a sequence of operations like the following:
241 * * Create temporary file.
243 * * Write new contents to file, then `close_tempfile_gently()` to cause the
244 * contents to be written to disk.
246 * * Pass the name of the temporary file to another program to allow
247 * it (and nobody else) to inspect or even modify the file's
248 * contents.
250 * * `reopen_tempfile()` to reopen the temporary file. Make further
251 * updates to the contents.
253 * * `rename_tempfile()` to move the file to its permanent location.
255 extern int reopen_tempfile(struct tempfile *tempfile);
258 * Close the file descriptor and/or file pointer and remove the
259 * temporary file associated with `tempfile`. It is a NOOP to call
260 * `delete_tempfile()` for a `tempfile` object that has already been
261 * deleted or renamed.
263 extern void delete_tempfile(struct tempfile *tempfile);
266 * Close the file descriptor and/or file pointer if they are still
267 * open, and atomically rename the temporary file to `path`. `path`
268 * must be on the same filesystem as the lock file. Return 0 on
269 * success. On failure, delete the temporary file and return -1, with
270 * `errno` set to the value from the failing call to `close(2)` or
271 * `rename(2)`. It is a bug to call `rename_tempfile()` for a
272 * `tempfile` object that is not currently active.
274 extern int rename_tempfile(struct tempfile *tempfile, const char *path);
276 #endif /* TEMPFILE_H */