blob: f401c979f0ed907b40bfb509ab8f06ad2632c19e [file] [log] [blame]
Michael Haggerty697cc8e2014-10-01 12:28:42 +02001#ifndef LOCKFILE_H
2#define LOCKFILE_H
3
4/*
5 * File write-locks as used by Git.
6 *
Michael Haggerty2db69de2015-08-10 11:47:36 +02007 * The lockfile API serves two purposes:
Michael Haggerty697cc8e2014-10-01 12:28:42 +02008 *
Michael Haggerty2db69de2015-08-10 11:47:36 +02009 * * 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.
Michael Haggerty697cc8e2014-10-01 12:28:42 +020017 *
Michael Haggerty2db69de2015-08-10 11:47:36 +020018 * * 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.
Michael Haggerty697cc8e2014-10-01 12:28:42 +020026 *
Michael Haggerty2db69de2015-08-10 11:47:36 +020027 * 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).
Michael Haggerty697cc8e2014-10-01 12:28:42 +020031 *
Michael Haggerty1a9d15d2015-08-10 11:47:41 +020032 * Most of the heavy lifting is done by the tempfile module (see
33 * "tempfile.h").
Michael Haggerty697cc8e2014-10-01 12:28:42 +020034 *
Michael Haggerty2db69de2015-08-10 11:47:36 +020035 * Calling sequence
36 * ----------------
Michael Haggerty697cc8e2014-10-01 12:28:42 +020037 *
Michael Haggerty2db69de2015-08-10 11:47:36 +020038 * The caller:
Michael Haggerty697cc8e2014-10-01 12:28:42 +020039 *
Jeff King5e7f01c2017-09-05 08:15:12 -040040 * * Allocates a `struct lock_file` with whatever storage duration you
41 * desire. The struct does not have to be initialized before being
42 * used, but it is good practice to do so using by setting it to
43 * all-zeros (or using the LOCK_INIT macro). This puts the object in a
44 * consistent state that allows you to call rollback_lock_file() even
45 * if the lock was never taken (in which case it is a noop).
Michael Haggerty697cc8e2014-10-01 12:28:42 +020046 *
Ralf Thielowaae42e42015-08-28 18:55:52 +020047 * * Attempts to create a lockfile by calling `hold_lock_file_for_update()`.
Michael Haggerty2db69de2015-08-10 11:47:36 +020048 *
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 *
Ben Wijen05d1ed62016-08-22 14:47:55 +020058 * 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 *
Michael Haggerty2db69de2015-08-10 11:47:36 +020062 * 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
Jeff King83a30692017-09-05 08:14:33 -040072 * lockfile by calling `close_lock_file_gently()`, and later call
Michael Haggerty2db69de2015-08-10 11:47:36 +020073 * `commit_lock_file()`, `commit_lock_file_to()`,
74 * `rollback_lock_file()`, or `reopen_lock_file()`.
75 *
Jeff King5e7f01c2017-09-05 08:15:12 -040076 * After the lockfile is committed or rolled back, the `lock_file`
77 * object can be discarded or reused.
Michael Haggerty2db69de2015-08-10 11:47:36 +020078 *
79 * If the program exits before `commit_lock_file()`,
Michael Haggerty1a9d15d2015-08-10 11:47:41 +020080 * `commit_lock_file_to()`, or `rollback_lock_file()` is called, the
81 * tempfile module will close and remove the lockfile, thereby rolling
82 * back any uncommitted changes.
Michael Haggerty2db69de2015-08-10 11:47:36 +020083 *
84 * If you need to close the file descriptor you obtained from a
85 * `hold_lock_file_for_*()` function yourself, do so by calling
Jeff King83a30692017-09-05 08:14:33 -040086 * `close_lock_file_gently()`. See "tempfile.h" for more information.
Michael Haggerty1a9d15d2015-08-10 11:47:41 +020087 *
88 *
89 * Under the covers, a lockfile is just a tempfile with a few helper
90 * functions. In particular, the state diagram and the cleanup
91 * machinery are all implemented in the tempfile module.
92 *
Michael Haggerty2db69de2015-08-10 11:47:36 +020093 *
94 * Error handling
95 * --------------
96 *
97 * The `hold_lock_file_for_*()` functions return a file descriptor on
98 * success or -1 on failure (unless `LOCK_DIE_ON_ERROR` is used; see
99 * "flags" below). On errors, `errno` describes the reason for
100 * failure. Errors can be reported by passing `errno` to
101 * `unable_to_lock_message()` or `unable_to_lock_die()`.
102 *
103 * Similarly, `commit_lock_file`, `commit_lock_file_to`, and
104 * `close_lock_file` return 0 on success. On failure they set `errno`
Jeff King83a30692017-09-05 08:14:33 -0400105 * appropriately and return -1. The `commit` variants (but not `close`)
106 * do their best to delete the temporary file before returning.
Michael Haggerty697cc8e2014-10-01 12:28:42 +0200107 */
108
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200109#include "tempfile.h"
110
Michael Haggerty697cc8e2014-10-01 12:28:42 +0200111struct lock_file {
Jeff King076aa2c2017-09-05 08:15:08 -0400112 struct tempfile *tempfile;
Michael Haggerty697cc8e2014-10-01 12:28:42 +0200113};
114
Jeff King5e7f01c2017-09-05 08:15:12 -0400115#define LOCK_INIT { NULL }
116
Michael Haggerty697cc8e2014-10-01 12:28:42 +0200117/* String appended to a filename to derive the lockfile name: */
118#define LOCK_SUFFIX ".lock"
119#define LOCK_SUFFIX_LEN 5
120
Michael Haggerty2db69de2015-08-10 11:47:36 +0200121
122/*
123 * Flags
124 * -----
125 *
Ralf Thielowaae42e42015-08-28 18:55:52 +0200126 * The following flags can be passed to `hold_lock_file_for_update()`.
Michael Haggerty2db69de2015-08-10 11:47:36 +0200127 */
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
Junio C Hamano3f061bf2016-12-07 10:56:26 -0800132 * is already locked silently returns -1 to the caller, or ...
Michael Haggerty2db69de2015-08-10 11:47:36 +0200133 */
Michael Haggerty697cc8e2014-10-01 12:28:42 +0200134#define LOCK_DIE_ON_ERROR 1
Michael Haggerty2db69de2015-08-10 11:47:36 +0200135
136/*
Junio C Hamano3f061bf2016-12-07 10:56:26 -0800137 * ... this flag can be passed instead to return -1 and give the usual
138 * error message upon an error.
139 */
Junio C Hamanob22d7482016-12-27 09:12:09 -0800140#define LOCK_REPORT_ON_ERROR 4
Junio C Hamano3f061bf2016-12-07 10:56:26 -0800141
142/*
Michael Haggerty2db69de2015-08-10 11:47:36 +0200143 * 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 */
Michael Haggerty697cc8e2014-10-01 12:28:42 +0200152#define LOCK_NO_DEREF 2
153
Michael Haggerty2db69de2015-08-10 11:47:36 +0200154/*
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 */
Michael Haggerty044b6a92015-05-11 12:35:25 +0200162extern int hold_lock_file_for_update_timeout(
163 struct lock_file *lk, const char *path,
164 int flags, long timeout_ms);
165
Michael Haggerty2db69de2015-08-10 11:47:36 +0200166/*
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 */
Michael Haggerty044b6a92015-05-11 12:35:25 +0200171static 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
Michael Haggerty2db69de2015-08-10 11:47:36 +0200178/*
Michael Haggerty0978f4b2017-05-22 16:17:39 +0200179 * Return a nonzero value iff `lk` is currently locked.
180 */
181static inline int is_lock_file_locked(struct lock_file *lk)
182{
Jeff King076aa2c2017-09-05 08:15:08 -0400183 return is_tempfile_active(lk->tempfile);
Michael Haggerty0978f4b2017-05-22 16:17:39 +0200184}
185
186/*
Michael Haggerty2db69de2015-08-10 11:47:36 +0200187 * Append an appropriate error message to `buf` following the failure
Ralf Thielowaae42e42015-08-28 18:55:52 +0200188 * of `hold_lock_file_for_update()` to lock `path`. `err` should be the
189 * `errno` set by the failing call.
Michael Haggerty2db69de2015-08-10 11:47:36 +0200190 */
191extern void unable_to_lock_message(const char *path, int err,
192 struct strbuf *buf);
193
194/*
195 * Emit an appropriate error message and `die()` following the failure
Ralf Thielowaae42e42015-08-28 18:55:52 +0200196 * of `hold_lock_file_for_update()` to lock `path`. `err` should be the
197 * `errno` set by the failing
Michael Haggerty2db69de2015-08-10 11:47:36 +0200198 * 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
Jeff King83a30692017-09-05 08:14:33 -0400205 * error. The stream is closed automatically when
206 * `close_lock_file_gently()` is called or when the file is committed or
207 * rolled back.
Michael Haggerty2db69de2015-08-10 11:47:36 +0200208 */
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200209static inline FILE *fdopen_lock_file(struct lock_file *lk, const char *mode)
210{
Jeff King076aa2c2017-09-05 08:15:08 -0400211 return fdopen_tempfile(lk->tempfile, mode);
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200212}
Michael Haggerty2db69de2015-08-10 11:47:36 +0200213
Michael Haggertyb4fb09e2015-08-10 11:47:39 +0200214/*
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.
217 */
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200218static inline const char *get_lock_file_path(struct lock_file *lk)
219{
Jeff King076aa2c2017-09-05 08:15:08 -0400220 return get_tempfile_path(lk->tempfile);
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200221}
Michael Haggertyb4fb09e2015-08-10 11:47:39 +0200222
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200223static inline int get_lock_file_fd(struct lock_file *lk)
224{
Jeff King076aa2c2017-09-05 08:15:08 -0400225 return get_tempfile_fd(lk->tempfile);
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200226}
227
228static inline FILE *get_lock_file_fp(struct lock_file *lk)
229{
Jeff King076aa2c2017-09-05 08:15:08 -0400230 return get_tempfile_fp(lk->tempfile);
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200231}
Michael Haggertyc99a4c22015-08-10 11:47:38 +0200232
Michael Haggerty2db69de2015-08-10 11:47:36 +0200233/*
234 * Return the path of the file that is locked by the specified
235 * lock_file object. The caller must free the memory.
236 */
237extern char *get_locked_file_path(struct lock_file *lk);
238
239/*
240 * If the lockfile is still open, close it (and the file pointer if it
241 * has been opened using `fdopen_lock_file()`) without renaming the
242 * lockfile over the file being locked. Return 0 upon success. On
Martin Ă…grend6135762017-10-05 22:32:05 +0200243 * failure to `close(2)`, return a negative value (the lockfile is not
244 * rolled back). Usually `commit_lock_file()`, `commit_lock_file_to()`,
Jeff King83a30692017-09-05 08:14:33 -0400245 * or `rollback_lock_file()` should eventually be called.
Michael Haggerty2db69de2015-08-10 11:47:36 +0200246 */
Jeff King83a30692017-09-05 08:14:33 -0400247static inline int close_lock_file_gently(struct lock_file *lk)
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200248{
Jeff King076aa2c2017-09-05 08:15:08 -0400249 return close_tempfile_gently(lk->tempfile);
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200250}
Michael Haggerty2db69de2015-08-10 11:47:36 +0200251
252/*
Jeff King83a30692017-09-05 08:14:33 -0400253 * Re-open a lockfile that has been closed using `close_lock_file_gently()`
Michael Haggerty2db69de2015-08-10 11:47:36 +0200254 * but not yet committed or rolled back. This can be used to implement
255 * a sequence of operations like the following:
256 *
257 * * Lock file.
258 *
Jeff King83a30692017-09-05 08:14:33 -0400259 * * Write new contents to lockfile, then `close_lock_file_gently()` to
Michael Haggerty2db69de2015-08-10 11:47:36 +0200260 * cause the contents to be written to disk.
261 *
262 * * Pass the name of the lockfile to another program to allow it (and
263 * nobody else) to inspect the contents you wrote, while still
264 * holding the lock yourself.
265 *
266 * * `reopen_lock_file()` to reopen the lockfile. Make further updates
267 * to the contents.
268 *
269 * * `commit_lock_file()` to make the final version permanent.
270 */
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200271static inline int reopen_lock_file(struct lock_file *lk)
272{
Jeff King076aa2c2017-09-05 08:15:08 -0400273 return reopen_tempfile(lk->tempfile);
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200274}
Michael Haggerty2db69de2015-08-10 11:47:36 +0200275
276/*
277 * Commit the change represented by `lk`: close the file descriptor
278 * and/or file pointer if they are still open and rename the lockfile
279 * to its final destination. Return 0 upon success. On failure, roll
280 * back the lock file and return -1, with `errno` set to the value
281 * from the failing call to `close(2)` or `rename(2)`. It is a bug to
282 * call `commit_lock_file()` for a `lock_file` object that is not
283 * currently locked.
284 */
285extern int commit_lock_file(struct lock_file *lk);
286
287/*
288 * Like `commit_lock_file()`, but rename the lockfile to the provided
289 * `path`. `path` must be on the same filesystem as the lock file.
290 */
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200291static inline int commit_lock_file_to(struct lock_file *lk, const char *path)
292{
293 return rename_tempfile(&lk->tempfile, path);
294}
Michael Haggerty2db69de2015-08-10 11:47:36 +0200295
296/*
297 * Roll back `lk`: close the file descriptor and/or file pointer and
298 * remove the lockfile. It is a NOOP to call `rollback_lock_file()`
299 * for a `lock_file` object that has already been committed or rolled
300 * back.
301 */
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200302static inline void rollback_lock_file(struct lock_file *lk)
303{
304 delete_tempfile(&lk->tempfile);
305}
Michael Haggerty697cc8e2014-10-01 12:28:42 +0200306
307#endif /* LOCKFILE_H */