4 The lockfile API serves two purposes:
6 * Mutual exclusion and atomic file updates. When we want to change a
7 file, we create a lockfile `<filename>.lock`, write the new file
8 contents into it, and then rename the lockfile to its final
9 destination `<filename>`. We create the `<filename>.lock` file with
10 `O_CREAT|O_EXCL` so that we can notice and fail if somebody else has
11 already locked the file, then atomically rename the lockfile to its
12 final destination to commit the changes and unlock the file.
14 * Automatic cruft removal. If the program exits after we lock a file
15 but before the changes have been committed, we want to make sure
16 that we remove the lockfile. This is done by remembering the
17 lockfiles we have created in a linked list and setting up an
18 `atexit(3)` handler and a signal handler that clean up the
19 lockfiles. This mechanism ensures that outstanding lockfiles are
20 cleaned up if the program exits (including when `die()` is called)
21 or if the program dies on a signal.
23 Please note that lockfiles only block other writers. Readers do not
24 block, but they are guaranteed to see either the old contents of the
25 file or the new contents of the file (assuming that the filesystem
26 implements `rename(2)` atomically).
34 * Allocates a `struct lock_file` either as a static variable or on the
35 heap, initialized to zeros. Once you use the structure to call the
36 `hold_lock_file_*` family of functions, it belongs to the lockfile
37 subsystem and its storage must remain valid throughout the life of
38 the program (i.e. you cannot use an on-stack variable to hold this
41 * Attempts to create a lockfile by passing that variable and the path
42 of the final destination (e.g. `$GIT_DIR/index`) to
43 `hold_lock_file_for_update` or `hold_lock_file_for_append`.
45 * Writes new content for the destination file by writing to the file
46 descriptor returned by those functions (also available via
49 When finished writing, the caller can:
51 * Close the file descriptor and rename the lockfile to its final
52 destination by calling `commit_lock_file` or `commit_lock_file_to`.
54 * Close the file descriptor and remove the lockfile by calling
57 * Close the file descriptor without removing or renaming the lockfile
58 by calling `close_lock_file`, and later call `commit_lock_file`,
59 `commit_lock_file_to`, `rollback_lock_file`, or `reopen_lock_file`.
61 Even after the lockfile is committed or rolled back, the `lock_file`
62 object must not be freed or altered by the caller. However, it may be
63 reused; just pass it to another call of `hold_lock_file_for_update` or
64 `hold_lock_file_for_append`.
66 If the program exits before you have called one of `commit_lock_file`,
67 `commit_lock_file_to`, `rollback_lock_file`, or `close_lock_file`, an
68 `atexit(3)` handler will close and remove the lockfile, rolling back
69 any uncommitted changes.
71 If you need to close the file descriptor you obtained from a
72 `hold_lock_file_*` function yourself, do so by calling
73 `close_lock_file`. You should never call `close(2)` yourself!
74 Otherwise the `struct lock_file` structure would still think that the
75 file descriptor needs to be closed, and a commit or rollback would
76 result in duplicate calls to `close(2)`. Worse yet, if you `close(2)`
77 and then later open another file descriptor for a completely different
78 purpose, then a commit or rollback might close that unrelated file
85 The `hold_lock_file_*` functions return a file descriptor on success
86 or -1 on failure (unless `LOCK_DIE_ON_ERROR` is used; see below). On
87 errors, `errno` describes the reason for failure. Errors can be
88 reported by passing `errno` to one of the following helper functions:
90 unable_to_lock_message::
92 Append an appropriate error message to a `strbuf`.
94 unable_to_lock_error::
96 Emit an appropriate error message using `error()`.
100 Emit an appropriate error message and `die()`.
102 Similarly, `commit_lock_file`, `commit_lock_file_to`, and
103 `close_lock_file` return 0 on success. On failure they set `errno`
104 appropriately, do their best to roll back the lockfile, and return -1.
110 The following flags can be passed to `hold_lock_file_for_update` or
111 `hold_lock_file_for_append`:
115 Usually symbolic links in the destination path are resolved
116 and the lockfile is created by adding ".lock" to the resolved
117 path. If `LOCK_NODEREF` is set, then the lockfile is created
118 by adding ".lock" to the path argument itself. This option is
119 used, for example, when locking a symbolic reference, which
120 for backwards-compatibility reasons can be a symbolic link
121 containing the name of the referred-to-reference.
125 If a lock is already taken for the file, `die()` with an error
126 message. If this option is not specified, trying to lock a
127 file that is already locked returns -1 to the caller.
133 hold_lock_file_for_update::
135 Take a pointer to `struct lock_file`, the path of the file to
136 be locked (e.g. `$GIT_DIR/index`) and a flags argument (see
137 above). Attempt to create a lockfile for the destination and
138 return the file descriptor for writing to the file.
140 hold_lock_file_for_append::
142 Like `hold_lock_file_for_update`, but before returning copy
143 the existing contents of the file (if any) to the lockfile and
144 position its write pointer at the end of the file.
148 Take a pointer to the `struct lock_file` initialized with an
149 earlier call to `hold_lock_file_for_update` or
150 `hold_lock_file_for_append`, close the file descriptor, and
151 rename the lockfile to its final destination. Return 0 upon
152 success. On failure, roll back the lock file and return -1,
153 with `errno` set to the value from the failing call to
154 `close(2)` or `rename(2)`. It is a bug to call
155 `commit_lock_file` for a `lock_file` object that is not
158 commit_lock_file_to::
160 Like `commit_lock_file()`, except that it takes an explicit
161 `path` argument to which the lockfile should be renamed. The
162 `path` must be on the same filesystem as the lock file.
166 Take a pointer to the `struct lock_file` initialized with an
167 earlier call to `hold_lock_file_for_update` or
168 `hold_lock_file_for_append`, close the file descriptor and
169 remove the lockfile. It is a NOOP to call
170 `rollback_lock_file()` for a `lock_file` object that has
171 already been committed or rolled back.
175 Take a pointer to the `struct lock_file` initialized with an
176 earlier call to `hold_lock_file_for_update` or
177 `hold_lock_file_for_append`, and close the file descriptor.
178 Return 0 upon success. On failure to `close(2)`, return a
179 negative value and roll back the lock file. Usually
180 `commit_lock_file`, `commit_lock_file_to`, or
181 `rollback_lock_file` should eventually be called if
182 `close_lock_file` succeeds.
186 Re-open a lockfile that has been closed (using
187 `close_lock_file`) but not yet committed or rolled back. This
188 can be used to implement a sequence of operations like the
193 * Write new contents to lockfile, then `close_lock_file` to
194 cause the contents to be written to disk.
196 * Pass the name of the lockfile to another program to allow it
197 (and nobody else) to inspect the contents you wrote, while
198 still holding the lock yourself.
200 * `reopen_lock_file` to reopen the lockfile. Make further
201 updates to the contents.
203 * `commit_lock_file` to make the final version permanent.