tempfile: do not delete tempfile on failed close
[git/git.git] / lockfile.h
CommitLineData
697cc8ef
MH
1#ifndef LOCKFILE_H
2#define LOCKFILE_H
3
4/*
5 * File write-locks as used by Git.
6 *
2db69de8
MH
7 * The lockfile API serves two purposes:
8 *
9 * * Mutual exclusion and atomic file updates. When we want to change
10 * a file, we create a lockfile `<filename>.lock`, write the new
11 * file contents into it, and then rename the lockfile to its final
12 * destination `<filename>`. We create the `<filename>.lock` file
13 * with `O_CREAT|O_EXCL` so that we can notice and fail if somebody
14 * else has already locked the file, then atomically rename the
15 * lockfile to its final destination to commit the changes and
16 * unlock the file.
17 *
18 * * Automatic cruft removal. If the program exits after we lock a
19 * file but before the changes have been committed, we want to make
20 * sure that we remove the lockfile. This is done by remembering the
21 * lockfiles we have created in a linked list and setting up an
22 * `atexit(3)` handler and a signal handler that clean up the
23 * lockfiles. This mechanism ensures that outstanding lockfiles are
24 * cleaned up if the program exits (including when `die()` is
25 * called) or if the program is terminated by a signal.
26 *
27 * Please note that lockfiles only block other writers. Readers do not
28 * block, but they are guaranteed to see either the old contents of
29 * the file or the new contents of the file (assuming that the
30 * filesystem implements `rename(2)` atomically).
31 *
1a9d15db
MH
32 * Most of the heavy lifting is done by the tempfile module (see
33 * "tempfile.h").
2db69de8
MH
34 *
35 * Calling sequence
36 * ----------------
37 *
38 * The caller:
39 *
40 * * Allocates a `struct lock_file` either as a static variable or on
41 * the heap, initialized to zeros. Once you use the structure to
42 * call the `hold_lock_file_for_*()` family of functions, it belongs
43 * to the lockfile subsystem and its storage must remain valid
44 * throughout the life of the program (i.e. you cannot use an
45 * on-stack variable to hold this structure).
46 *
aae42e43 47 * * Attempts to create a lockfile by calling `hold_lock_file_for_update()`.
2db69de8
MH
48 *
49 * * Writes new content for the destination file by either:
50 *
51 * * writing to the file descriptor returned by the
52 * `hold_lock_file_for_*()` functions (also available via
53 * `lock->fd`).
54 *
55 * * calling `fdopen_lock_file()` to get a `FILE` pointer for the
56 * open file and writing to the file using stdio.
57 *
05d1ed61
BW
58 * Note that the file descriptor returned by hold_lock_file_for_update()
59 * is marked O_CLOEXEC, so the new contents must be written by the
60 * current process, not a spawned one.
61 *
2db69de8
MH
62 * When finished writing, the caller can:
63 *
64 * * Close the file descriptor and rename the lockfile to its final
65 * destination by calling `commit_lock_file()` or
66 * `commit_lock_file_to()`.
67 *
68 * * Close the file descriptor and remove the lockfile by calling
69 * `rollback_lock_file()`.
70 *
71 * * Close the file descriptor without removing or renaming the
72 * lockfile by calling `close_lock_file()`, and later call
73 * `commit_lock_file()`, `commit_lock_file_to()`,
74 * `rollback_lock_file()`, or `reopen_lock_file()`.
75 *
76 * Even after the lockfile is committed or rolled back, the
77 * `lock_file` object must not be freed or altered by the caller.
78 * However, it may be reused; just pass it to another call of
aae42e43 79 * `hold_lock_file_for_update()`.
2db69de8
MH
80 *
81 * If the program exits before `commit_lock_file()`,
1a9d15db
MH
82 * `commit_lock_file_to()`, or `rollback_lock_file()` is called, the
83 * tempfile module will close and remove the lockfile, thereby rolling
84 * back any uncommitted changes.
2db69de8
MH
85 *
86 * If you need to close the file descriptor you obtained from a
87 * `hold_lock_file_for_*()` function yourself, do so by calling
1a9d15db
MH
88 * `close_lock_file()`. See "tempfile.h" for more information.
89 *
90 *
91 * Under the covers, a lockfile is just a tempfile with a few helper
92 * functions. In particular, the state diagram and the cleanup
93 * machinery are all implemented in the tempfile module.
94 *
2db69de8
MH
95 *
96 * Error handling
97 * --------------
98 *
99 * The `hold_lock_file_for_*()` functions return a file descriptor on
100 * success or -1 on failure (unless `LOCK_DIE_ON_ERROR` is used; see
101 * "flags" below). On errors, `errno` describes the reason for
102 * failure. Errors can be reported by passing `errno` to
103 * `unable_to_lock_message()` or `unable_to_lock_die()`.
104 *
105 * Similarly, `commit_lock_file`, `commit_lock_file_to`, and
106 * `close_lock_file` return 0 on success. On failure they set `errno`
107 * appropriately, do their best to roll back the lockfile, and return
108 * -1.
697cc8ef
MH
109 */
110
1a9d15db
MH
111#include "tempfile.h"
112
697cc8ef 113struct lock_file {
1a9d15db 114 struct tempfile tempfile;
697cc8ef
MH
115};
116
117/* String appended to a filename to derive the lockfile name: */
118#define LOCK_SUFFIX ".lock"
119#define LOCK_SUFFIX_LEN 5
120
2db69de8
MH
121
122/*
123 * Flags
124 * -----
125 *
aae42e43 126 * The following flags can be passed to `hold_lock_file_for_update()`.
2db69de8
MH
127 */
128
129/*
130 * If a lock is already taken for the file, `die()` with an error
131 * message. If this flag is not specified, trying to lock a file that
3f061bf5 132 * is already locked silently returns -1 to the caller, or ...
2db69de8 133 */
697cc8ef 134#define LOCK_DIE_ON_ERROR 1
2db69de8 135
3f061bf5
JH
136/*
137 * ... this flag can be passed instead to return -1 and give the usual
138 * error message upon an error.
139 */
b22d7484 140#define LOCK_REPORT_ON_ERROR 4
3f061bf5 141
2db69de8
MH
142/*
143 * Usually symbolic links in the destination path are resolved. This
144 * means that (1) the lockfile is created by adding ".lock" to the
145 * resolved path, and (2) upon commit, the resolved path is
146 * overwritten. However, if `LOCK_NO_DEREF` is set, then the lockfile
147 * is created by adding ".lock" to the path argument itself. This
148 * option is used, for example, when detaching a symbolic reference,
149 * which for backwards-compatibility reasons, can be a symbolic link
150 * containing the name of the referred-to-reference.
151 */
697cc8ef
MH
152#define LOCK_NO_DEREF 2
153
2db69de8
MH
154/*
155 * Attempt to create a lockfile for the file at `path` and return a
156 * file descriptor for writing to it, or -1 on error. If the file is
157 * currently locked, retry with quadratic backoff for at least
158 * timeout_ms milliseconds. If timeout_ms is 0, try exactly once; if
159 * timeout_ms is -1, retry indefinitely. The flags argument and error
160 * handling are described above.
161 */
044b6a9e
MH
162extern int hold_lock_file_for_update_timeout(
163 struct lock_file *lk, const char *path,
164 int flags, long timeout_ms);
165
2db69de8
MH
166/*
167 * Attempt to create a lockfile for the file at `path` and return a
168 * file descriptor for writing to it, or -1 on error. The flags
169 * argument and error handling are described above.
170 */
044b6a9e
MH
171static inline int hold_lock_file_for_update(
172 struct lock_file *lk, const char *path,
173 int flags)
174{
175 return hold_lock_file_for_update_timeout(lk, path, flags, 0);
176}
177
0978f4ba
MH
178/*
179 * Return a nonzero value iff `lk` is currently locked.
180 */
181static inline int is_lock_file_locked(struct lock_file *lk)
182{
183 return is_tempfile_active(&lk->tempfile);
184}
185
2db69de8
MH
186/*
187 * Append an appropriate error message to `buf` following the failure
aae42e43
RT
188 * of `hold_lock_file_for_update()` to lock `path`. `err` should be the
189 * `errno` set by the failing call.
2db69de8
MH
190 */
191extern void unable_to_lock_message(const char *path, int err,
192 struct strbuf *buf);
044b6a9e 193
2db69de8
MH
194/*
195 * Emit an appropriate error message and `die()` following the failure
aae42e43
RT
196 * of `hold_lock_file_for_update()` to lock `path`. `err` should be the
197 * `errno` set by the failing
2db69de8
MH
198 * call.
199 */
200extern NORETURN void unable_to_lock_die(const char *path, int err);
201
202/*
203 * Associate a stdio stream with the lockfile (which must still be
204 * open). Return `NULL` (*without* rolling back the lockfile) on
205 * error. The stream is closed automatically when `close_lock_file()`
206 * is called or when the file is committed or rolled back.
207 */
1a9d15db
MH
208static inline FILE *fdopen_lock_file(struct lock_file *lk, const char *mode)
209{
210 return fdopen_tempfile(&lk->tempfile, mode);
211}
2db69de8 212
b4fb09e4
MH
213/*
214 * Return the path of the lockfile. The return value is a pointer to a
215 * field within the lock_file object and should not be freed.
216 */
1a9d15db
MH
217static inline const char *get_lock_file_path(struct lock_file *lk)
218{
219 return get_tempfile_path(&lk->tempfile);
220}
b4fb09e4 221
1a9d15db
MH
222static inline int get_lock_file_fd(struct lock_file *lk)
223{
224 return get_tempfile_fd(&lk->tempfile);
225}
226
227static inline FILE *get_lock_file_fp(struct lock_file *lk)
228{
229 return get_tempfile_fp(&lk->tempfile);
230}
c99a4c2d 231
2db69de8
MH
232/*
233 * Return the path of the file that is locked by the specified
234 * lock_file object. The caller must free the memory.
235 */
236extern char *get_locked_file_path(struct lock_file *lk);
237
238/*
239 * If the lockfile is still open, close it (and the file pointer if it
240 * has been opened using `fdopen_lock_file()`) without renaming the
241 * lockfile over the file being locked. Return 0 upon success. On
242 * failure to `close(2)`, return a negative value and roll back the
243 * lock file. Usually `commit_lock_file()`, `commit_lock_file_to()`,
244 * or `rollback_lock_file()` should eventually be called if
245 * `close_lock_file()` succeeds.
246 */
1a9d15db
MH
247static inline int close_lock_file(struct lock_file *lk)
248{
49bd0fc2
JK
249 int ret = close_tempfile_gently(&lk->tempfile);
250 if (ret) {
251 int saved_errno = errno;
252 delete_tempfile(&lk->tempfile);
253 errno = saved_errno;
254 }
255 return ret;
1a9d15db 256}
2db69de8
MH
257
258/*
259 * Re-open a lockfile that has been closed using `close_lock_file()`
260 * but not yet committed or rolled back. This can be used to implement
261 * a sequence of operations like the following:
262 *
263 * * Lock file.
264 *
265 * * Write new contents to lockfile, then `close_lock_file()` to
266 * cause the contents to be written to disk.
267 *
268 * * Pass the name of the lockfile to another program to allow it (and
269 * nobody else) to inspect the contents you wrote, while still
270 * holding the lock yourself.
271 *
272 * * `reopen_lock_file()` to reopen the lockfile. Make further updates
273 * to the contents.
274 *
275 * * `commit_lock_file()` to make the final version permanent.
276 */
1a9d15db
MH
277static inline int reopen_lock_file(struct lock_file *lk)
278{
279 return reopen_tempfile(&lk->tempfile);
280}
2db69de8
MH
281
282/*
283 * Commit the change represented by `lk`: close the file descriptor
284 * and/or file pointer if they are still open and rename the lockfile
285 * to its final destination. Return 0 upon success. On failure, roll
286 * back the lock file and return -1, with `errno` set to the value
287 * from the failing call to `close(2)` or `rename(2)`. It is a bug to
288 * call `commit_lock_file()` for a `lock_file` object that is not
289 * currently locked.
290 */
291extern int commit_lock_file(struct lock_file *lk);
292
293/*
294 * Like `commit_lock_file()`, but rename the lockfile to the provided
295 * `path`. `path` must be on the same filesystem as the lock file.
296 */
1a9d15db
MH
297static inline int commit_lock_file_to(struct lock_file *lk, const char *path)
298{
299 return rename_tempfile(&lk->tempfile, path);
300}
2db69de8
MH
301
302/*
303 * Roll back `lk`: close the file descriptor and/or file pointer and
304 * remove the lockfile. It is a NOOP to call `rollback_lock_file()`
305 * for a `lock_file` object that has already been committed or rolled
306 * back.
307 */
1a9d15db
MH
308static inline void rollback_lock_file(struct lock_file *lk)
309{
310 delete_tempfile(&lk->tempfile);
311}
697cc8ef
MH
312
313#endif /* LOCKFILE_H */