Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 1 | #ifndef LOCKFILE_H |
| 2 | #define LOCKFILE_H |
| 3 | |
| 4 | /* |
| 5 | * File write-locks as used by Git. |
| 6 | * |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 7 | * The lockfile API serves two purposes: |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 8 | * |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 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. |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 17 | * |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 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. |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 26 | * |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 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). |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 31 | * |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 32 | * Most of the heavy lifting is done by the tempfile module (see |
| 33 | * "tempfile.h"). |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 34 | * |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 35 | * Calling sequence |
| 36 | * ---------------- |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 37 | * |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 38 | * The caller: |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 39 | * |
Jeff King | 5e7f01c | 2017-09-05 08:15:12 -0400 | [diff] [blame] | 40 | * * 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 Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 46 | * |
Ralf Thielow | aae42e4 | 2015-08-28 18:55:52 +0200 | [diff] [blame] | 47 | * * Attempts to create a lockfile by calling `hold_lock_file_for_update()`. |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 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 | * |
Ben Wijen | 05d1ed6 | 2016-08-22 14:47:55 +0200 | [diff] [blame] | 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 | * |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 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 |
Jeff King | 83a3069 | 2017-09-05 08:14:33 -0400 | [diff] [blame] | 72 | * lockfile by calling `close_lock_file_gently()`, and later call |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 73 | * `commit_lock_file()`, `commit_lock_file_to()`, |
| 74 | * `rollback_lock_file()`, or `reopen_lock_file()`. |
| 75 | * |
Jeff King | 5e7f01c | 2017-09-05 08:15:12 -0400 | [diff] [blame] | 76 | * After the lockfile is committed or rolled back, the `lock_file` |
| 77 | * object can be discarded or reused. |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 78 | * |
| 79 | * If the program exits before `commit_lock_file()`, |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 80 | * `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 Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 83 | * |
| 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 King | 83a3069 | 2017-09-05 08:14:33 -0400 | [diff] [blame] | 86 | * `close_lock_file_gently()`. See "tempfile.h" for more information. |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 87 | * |
| 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 | * |
Taylor Blau | fa3bff2 | 2020-04-27 10:27:58 -0600 | [diff] [blame] | 93 | * Permission bits |
| 94 | * --------------- |
| 95 | * |
| 96 | * If you call either `hold_lock_file_for_update_mode` or |
| 97 | * `hold_lock_file_for_update_timeout_mode`, you can specify a suggested |
| 98 | * mode for the underlying temporary file. Note that the file isn't |
| 99 | * guaranteed to have this exact mode, since it may be limited by either |
| 100 | * the umask, 'core.sharedRepository', or both. See `adjust_shared_perm` |
| 101 | * for more. |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 102 | * |
| 103 | * Error handling |
| 104 | * -------------- |
| 105 | * |
| 106 | * The `hold_lock_file_for_*()` functions return a file descriptor on |
| 107 | * success or -1 on failure (unless `LOCK_DIE_ON_ERROR` is used; see |
| 108 | * "flags" below). On errors, `errno` describes the reason for |
| 109 | * failure. Errors can be reported by passing `errno` to |
| 110 | * `unable_to_lock_message()` or `unable_to_lock_die()`. |
| 111 | * |
| 112 | * Similarly, `commit_lock_file`, `commit_lock_file_to`, and |
| 113 | * `close_lock_file` return 0 on success. On failure they set `errno` |
Jeff King | 83a3069 | 2017-09-05 08:14:33 -0400 | [diff] [blame] | 114 | * appropriately and return -1. The `commit` variants (but not `close`) |
| 115 | * do their best to delete the temporary file before returning. |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 116 | */ |
| 117 | |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 118 | #include "tempfile.h" |
| 119 | |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 120 | struct lock_file { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 121 | struct tempfile *tempfile; |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 122 | }; |
| 123 | |
Ævar Arnfjörð Bjarmason | 9865b6e | 2021-09-27 14:54:25 +0200 | [diff] [blame] | 124 | #define LOCK_INIT { 0 } |
Jeff King | 5e7f01c | 2017-09-05 08:15:12 -0400 | [diff] [blame] | 125 | |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 126 | /* String appended to a filename to derive the lockfile name: */ |
| 127 | #define LOCK_SUFFIX ".lock" |
| 128 | #define LOCK_SUFFIX_LEN 5 |
| 129 | |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 130 | |
| 131 | /* |
| 132 | * Flags |
| 133 | * ----- |
| 134 | * |
Ralf Thielow | aae42e4 | 2015-08-28 18:55:52 +0200 | [diff] [blame] | 135 | * The following flags can be passed to `hold_lock_file_for_update()`. |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 136 | */ |
| 137 | |
| 138 | /* |
| 139 | * If a lock is already taken for the file, `die()` with an error |
| 140 | * message. If this flag is not specified, trying to lock a file that |
Junio C Hamano | 3f061bf | 2016-12-07 10:56:26 -0800 | [diff] [blame] | 141 | * is already locked silently returns -1 to the caller, or ... |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 142 | */ |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 143 | #define LOCK_DIE_ON_ERROR 1 |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 144 | |
| 145 | /* |
Junio C Hamano | 3f061bf | 2016-12-07 10:56:26 -0800 | [diff] [blame] | 146 | * ... this flag can be passed instead to return -1 and give the usual |
| 147 | * error message upon an error. |
| 148 | */ |
Junio C Hamano | b22d748 | 2016-12-27 09:12:09 -0800 | [diff] [blame] | 149 | #define LOCK_REPORT_ON_ERROR 4 |
Junio C Hamano | 3f061bf | 2016-12-07 10:56:26 -0800 | [diff] [blame] | 150 | |
| 151 | /* |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 152 | * Usually symbolic links in the destination path are resolved. This |
| 153 | * means that (1) the lockfile is created by adding ".lock" to the |
| 154 | * resolved path, and (2) upon commit, the resolved path is |
| 155 | * overwritten. However, if `LOCK_NO_DEREF` is set, then the lockfile |
| 156 | * is created by adding ".lock" to the path argument itself. This |
| 157 | * option is used, for example, when detaching a symbolic reference, |
| 158 | * which for backwards-compatibility reasons, can be a symbolic link |
| 159 | * containing the name of the referred-to-reference. |
| 160 | */ |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 161 | #define LOCK_NO_DEREF 2 |
| 162 | |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 163 | /* |
| 164 | * Attempt to create a lockfile for the file at `path` and return a |
| 165 | * file descriptor for writing to it, or -1 on error. If the file is |
| 166 | * currently locked, retry with quadratic backoff for at least |
| 167 | * timeout_ms milliseconds. If timeout_ms is 0, try exactly once; if |
Taylor Blau | fa3bff2 | 2020-04-27 10:27:58 -0600 | [diff] [blame] | 168 | * timeout_ms is -1, retry indefinitely. The flags argument, error |
| 169 | * handling, and mode are described above. |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 170 | */ |
Taylor Blau | fa3bff2 | 2020-04-27 10:27:58 -0600 | [diff] [blame] | 171 | int hold_lock_file_for_update_timeout_mode( |
Michael Haggerty | 044b6a9 | 2015-05-11 12:35:25 +0200 | [diff] [blame] | 172 | struct lock_file *lk, const char *path, |
Taylor Blau | fa3bff2 | 2020-04-27 10:27:58 -0600 | [diff] [blame] | 173 | int flags, long timeout_ms, int mode); |
| 174 | |
| 175 | static inline int hold_lock_file_for_update_timeout( |
| 176 | struct lock_file *lk, const char *path, |
| 177 | int flags, long timeout_ms) |
| 178 | { |
| 179 | return hold_lock_file_for_update_timeout_mode(lk, path, flags, |
| 180 | timeout_ms, 0666); |
| 181 | } |
Michael Haggerty | 044b6a9 | 2015-05-11 12:35:25 +0200 | [diff] [blame] | 182 | |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 183 | /* |
| 184 | * Attempt to create a lockfile for the file at `path` and return a |
| 185 | * file descriptor for writing to it, or -1 on error. The flags |
| 186 | * argument and error handling are described above. |
| 187 | */ |
Michael Haggerty | 044b6a9 | 2015-05-11 12:35:25 +0200 | [diff] [blame] | 188 | static inline int hold_lock_file_for_update( |
| 189 | struct lock_file *lk, const char *path, |
| 190 | int flags) |
| 191 | { |
| 192 | return hold_lock_file_for_update_timeout(lk, path, flags, 0); |
| 193 | } |
| 194 | |
Taylor Blau | fa3bff2 | 2020-04-27 10:27:58 -0600 | [diff] [blame] | 195 | static inline int hold_lock_file_for_update_mode( |
| 196 | struct lock_file *lk, const char *path, |
| 197 | int flags, int mode) |
| 198 | { |
| 199 | return hold_lock_file_for_update_timeout_mode(lk, path, flags, 0, mode); |
| 200 | } |
| 201 | |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 202 | /* |
Michael Haggerty | 0978f4b | 2017-05-22 16:17:39 +0200 | [diff] [blame] | 203 | * Return a nonzero value iff `lk` is currently locked. |
| 204 | */ |
| 205 | static inline int is_lock_file_locked(struct lock_file *lk) |
| 206 | { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 207 | return is_tempfile_active(lk->tempfile); |
Michael Haggerty | 0978f4b | 2017-05-22 16:17:39 +0200 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /* |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 211 | * Append an appropriate error message to `buf` following the failure |
Ralf Thielow | aae42e4 | 2015-08-28 18:55:52 +0200 | [diff] [blame] | 212 | * of `hold_lock_file_for_update()` to lock `path`. `err` should be the |
| 213 | * `errno` set by the failing call. |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 214 | */ |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 215 | void unable_to_lock_message(const char *path, int err, |
Denton Liu | ad6dad0 | 2019-04-29 04:28:23 -0400 | [diff] [blame] | 216 | struct strbuf *buf); |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 217 | |
| 218 | /* |
| 219 | * Emit an appropriate error message and `die()` following the failure |
Ralf Thielow | aae42e4 | 2015-08-28 18:55:52 +0200 | [diff] [blame] | 220 | * of `hold_lock_file_for_update()` to lock `path`. `err` should be the |
| 221 | * `errno` set by the failing |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 222 | * call. |
| 223 | */ |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 224 | NORETURN void unable_to_lock_die(const char *path, int err); |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 225 | |
| 226 | /* |
| 227 | * Associate a stdio stream with the lockfile (which must still be |
| 228 | * open). Return `NULL` (*without* rolling back the lockfile) on |
Jeff King | 83a3069 | 2017-09-05 08:14:33 -0400 | [diff] [blame] | 229 | * error. The stream is closed automatically when |
| 230 | * `close_lock_file_gently()` is called or when the file is committed or |
| 231 | * rolled back. |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 232 | */ |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 233 | static inline FILE *fdopen_lock_file(struct lock_file *lk, const char *mode) |
| 234 | { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 235 | return fdopen_tempfile(lk->tempfile, mode); |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 236 | } |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 237 | |
Michael Haggerty | b4fb09e | 2015-08-10 11:47:39 +0200 | [diff] [blame] | 238 | /* |
| 239 | * Return the path of the lockfile. The return value is a pointer to a |
| 240 | * field within the lock_file object and should not be freed. |
| 241 | */ |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 242 | static inline const char *get_lock_file_path(struct lock_file *lk) |
| 243 | { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 244 | return get_tempfile_path(lk->tempfile); |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 245 | } |
Michael Haggerty | b4fb09e | 2015-08-10 11:47:39 +0200 | [diff] [blame] | 246 | |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 247 | static inline int get_lock_file_fd(struct lock_file *lk) |
| 248 | { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 249 | return get_tempfile_fd(lk->tempfile); |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static inline FILE *get_lock_file_fp(struct lock_file *lk) |
| 253 | { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 254 | return get_tempfile_fp(lk->tempfile); |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 255 | } |
Michael Haggerty | c99a4c2 | 2015-08-10 11:47:38 +0200 | [diff] [blame] | 256 | |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 257 | /* |
| 258 | * Return the path of the file that is locked by the specified |
| 259 | * lock_file object. The caller must free the memory. |
| 260 | */ |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 261 | char *get_locked_file_path(struct lock_file *lk); |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 262 | |
| 263 | /* |
| 264 | * If the lockfile is still open, close it (and the file pointer if it |
| 265 | * has been opened using `fdopen_lock_file()`) without renaming the |
| 266 | * lockfile over the file being locked. Return 0 upon success. On |
Martin Ågren | d613576 | 2017-10-05 22:32:05 +0200 | [diff] [blame] | 267 | * failure to `close(2)`, return a negative value (the lockfile is not |
| 268 | * rolled back). Usually `commit_lock_file()`, `commit_lock_file_to()`, |
Jeff King | 83a3069 | 2017-09-05 08:14:33 -0400 | [diff] [blame] | 269 | * or `rollback_lock_file()` should eventually be called. |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 270 | */ |
Jeff King | 83a3069 | 2017-09-05 08:14:33 -0400 | [diff] [blame] | 271 | static inline int close_lock_file_gently(struct lock_file *lk) |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 272 | { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 273 | return close_tempfile_gently(lk->tempfile); |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 274 | } |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 275 | |
| 276 | /* |
Jeff King | 83a3069 | 2017-09-05 08:14:33 -0400 | [diff] [blame] | 277 | * Re-open a lockfile that has been closed using `close_lock_file_gently()` |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 278 | * but not yet committed or rolled back. This can be used to implement |
| 279 | * a sequence of operations like the following: |
| 280 | * |
| 281 | * * Lock file. |
| 282 | * |
Jeff King | 83a3069 | 2017-09-05 08:14:33 -0400 | [diff] [blame] | 283 | * * Write new contents to lockfile, then `close_lock_file_gently()` to |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 284 | * cause the contents to be written to disk. |
| 285 | * |
| 286 | * * Pass the name of the lockfile to another program to allow it (and |
| 287 | * nobody else) to inspect the contents you wrote, while still |
| 288 | * holding the lock yourself. |
| 289 | * |
Jeff King | 6c003d6 | 2018-09-04 19:36:43 -0400 | [diff] [blame] | 290 | * * `reopen_lock_file()` to reopen the lockfile, truncating the existing |
| 291 | * contents. Write out the new contents. |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 292 | * |
| 293 | * * `commit_lock_file()` to make the final version permanent. |
| 294 | */ |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 295 | static inline int reopen_lock_file(struct lock_file *lk) |
| 296 | { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 297 | return reopen_tempfile(lk->tempfile); |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 298 | } |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 299 | |
| 300 | /* |
| 301 | * Commit the change represented by `lk`: close the file descriptor |
| 302 | * and/or file pointer if they are still open and rename the lockfile |
| 303 | * to its final destination. Return 0 upon success. On failure, roll |
| 304 | * back the lock file and return -1, with `errno` set to the value |
| 305 | * from the failing call to `close(2)` or `rename(2)`. It is a bug to |
| 306 | * call `commit_lock_file()` for a `lock_file` object that is not |
| 307 | * currently locked. |
| 308 | */ |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 309 | int commit_lock_file(struct lock_file *lk); |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 310 | |
| 311 | /* |
| 312 | * Like `commit_lock_file()`, but rename the lockfile to the provided |
| 313 | * `path`. `path` must be on the same filesystem as the lock file. |
| 314 | */ |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 315 | static inline int commit_lock_file_to(struct lock_file *lk, const char *path) |
| 316 | { |
| 317 | return rename_tempfile(&lk->tempfile, path); |
| 318 | } |
Michael Haggerty | 2db69de | 2015-08-10 11:47:36 +0200 | [diff] [blame] | 319 | |
| 320 | /* |
| 321 | * Roll back `lk`: close the file descriptor and/or file pointer and |
| 322 | * remove the lockfile. It is a NOOP to call `rollback_lock_file()` |
| 323 | * for a `lock_file` object that has already been committed or rolled |
| 324 | * back. |
| 325 | */ |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 326 | static inline void rollback_lock_file(struct lock_file *lk) |
| 327 | { |
| 328 | delete_tempfile(&lk->tempfile); |
| 329 | } |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 330 | |
| 331 | #endif /* LOCKFILE_H */ |