2 * Copyright (c) 2005, Junio C Hamano
8 * File write-locks as used by Git.
10 * For an overview of how to use the lockfile API, please see
12 * Documentation/technical/api-lockfile.txt
14 * This module keeps track of all locked files in lock_file_list for
15 * use at cleanup. This list and the lock_file objects that comprise
16 * it must be kept in self-consistent states at all time, because the
17 * program can be interrupted any time by a signal, in which case the
18 * signal handler will walk through the list attempting to clean up
19 * any open lock files.
21 * A lockfile is owned by the process that created it. The lock_file
22 * object has an "owner" field that records its owner. This field is
23 * used to prevent a forked process from closing a lockfile created by
26 * The possible states of a lock_file object are as follows:
28 * - Uninitialized. In this state the object's on_list field must be
29 * zero but the rest of its contents need not be initialized. As
30 * soon as the object is used in any way, it is irrevocably
31 * registered in the lock_file_list, and on_list is set.
33 * - Locked, lockfile open (after hold_lock_file_for_update(),
34 * hold_lock_file_for_append(), or reopen_lock_file()). In this
36 * - the lockfile exists
38 * - filename holds the filename of the lockfile
39 * - fd holds a file descriptor open for writing to the lockfile
40 * - owner holds the PID of the process that locked the file
42 * - Locked, lockfile closed (after successful close_lock_file()).
43 * Same as the previous state, except that the lockfile is closed
46 * - Unlocked (after commit_lock_file(), rollback_lock_file(), a
47 * failed attempt to lock, or a failed close_lock_file()). In this
50 * - filename is empty (usually, though there are transitory
51 * states in which this condition doesn't hold). Client code should
52 * *not* rely on the filename being empty in this state.
54 * - the object is left registered in the lock_file_list, and
58 static struct lock_file
*volatile lock_file_list
;
60 static void remove_lock_file(void)
64 while (lock_file_list
) {
65 if (lock_file_list
->owner
== me
)
66 rollback_lock_file(lock_file_list
);
67 lock_file_list
= lock_file_list
->next
;
71 static void remove_lock_file_on_signal(int signo
)
79 * p = absolute or relative path name
81 * Return a pointer into p showing the beginning of the last path name
82 * element. If p is empty or the root directory ("/"), just return p.
84 static char *last_path_elm(char *p
)
86 /* r starts pointing to null at the end of the string */
87 char *r
= strchr(p
, '\0');
90 return p
; /* just return empty string */
92 r
--; /* back up to last non-null character */
94 /* back up past trailing slashes, if any */
95 while (r
> p
&& *r
== '/')
99 * then go backwards until I hit a slash, or the beginning of
102 while (r
> p
&& *(r
-1) != '/')
108 /* We allow "recursive" symbolic links. Only within reason, though */
112 * p = path that may be a symlink
115 * If p is a symlink, attempt to overwrite p with a path to the real
116 * file or directory (which may or may not exist), following a chain of
117 * symlinks if necessary. Otherwise, leave p unmodified.
119 * This is a best-effort routine. If an error occurs, p will either be
120 * left unmodified or will name a different symlink in a symlink chain
121 * that started with p's initial contents.
126 static char *resolve_symlink(char *p
, size_t s
)
128 int depth
= MAXDEPTH
;
129 static struct strbuf link
= STRBUF_INIT
;
132 if (strbuf_readlink(&link
, p
, strlen(p
)) < 0)
135 if (is_absolute_path(link
.buf
)) {
136 /* absolute path simply replaces p */
140 warning("%s: symlink too long", p
);
145 * link is a relative path, so replace the
146 * last element of p with it.
148 char *r
= (char *)last_path_elm(p
);
149 if (r
- p
+ link
.len
< s
)
152 warning("%s: symlink too long", p
);
161 /* Make sure errno contains a meaningful value on error */
162 static int lock_file(struct lock_file
*lk
, const char *path
, int flags
)
164 if (!lock_file_list
) {
165 /* One-time initialization */
166 sigchain_push_common(remove_lock_file_on_signal
);
167 atexit(remove_lock_file
);
171 die("BUG: cannot lock_file(\"%s\") using active struct lock_file",
174 /* Initialize *lk and add it to lock_file_list: */
178 strbuf_init(&lk
->filename
, PATH_MAX
);
179 lk
->next
= lock_file_list
;
182 } else if (lk
->filename
.len
) {
183 /* This shouldn't happen, but better safe than sorry. */
184 die("BUG: lock_file(\"%s\") called with improperly-reset lock_file object",
188 strbuf_addstr(&lk
->filename
, path
);
189 if (!(flags
& LOCK_NODEREF
)) {
190 resolve_symlink(lk
->filename
.buf
, lk
->filename
.alloc
);
191 strbuf_setlen(&lk
->filename
, strlen(lk
->filename
.buf
));
193 strbuf_addstr(&lk
->filename
, LOCK_SUFFIX
);
194 lk
->fd
= open(lk
->filename
.buf
, O_RDWR
| O_CREAT
| O_EXCL
, 0666);
196 strbuf_reset(&lk
->filename
);
199 lk
->owner
= getpid();
201 if (adjust_shared_perm(lk
->filename
.buf
)) {
202 int save_errno
= errno
;
203 error("cannot fix permission bits on %s", lk
->filename
.buf
);
204 rollback_lock_file(lk
);
211 void unable_to_lock_message(const char *path
, int err
, struct strbuf
*buf
)
214 strbuf_addf(buf
, "Unable to create '%s.lock': %s.\n\n"
215 "If no other git process is currently running, this probably means a\n"
216 "git process crashed in this repository earlier. Make sure no other git\n"
217 "process is running and remove the file manually to continue.",
218 absolute_path(path
), strerror(err
));
220 strbuf_addf(buf
, "Unable to create '%s.lock': %s",
221 absolute_path(path
), strerror(err
));
224 int unable_to_lock_error(const char *path
, int err
)
226 struct strbuf buf
= STRBUF_INIT
;
228 unable_to_lock_message(path
, err
, &buf
);
229 error("%s", buf
.buf
);
230 strbuf_release(&buf
);
234 NORETURN
void unable_to_lock_die(const char *path
, int err
)
236 struct strbuf buf
= STRBUF_INIT
;
238 unable_to_lock_message(path
, err
, &buf
);
242 /* This should return a meaningful errno on failure */
243 int hold_lock_file_for_update(struct lock_file
*lk
, const char *path
, int flags
)
245 int fd
= lock_file(lk
, path
, flags
);
246 if (fd
< 0 && (flags
& LOCK_DIE_ON_ERROR
))
247 unable_to_lock_die(path
, errno
);
251 int hold_lock_file_for_append(struct lock_file
*lk
, const char *path
, int flags
)
255 fd
= lock_file(lk
, path
, flags
);
257 if (flags
& LOCK_DIE_ON_ERROR
)
258 unable_to_lock_die(path
, errno
);
262 orig_fd
= open(path
, O_RDONLY
);
264 if (errno
!= ENOENT
) {
265 if (flags
& LOCK_DIE_ON_ERROR
)
266 die("cannot open '%s' for copying", path
);
267 rollback_lock_file(lk
);
268 return error("cannot open '%s' for copying", path
);
270 } else if (copy_fd(orig_fd
, fd
)) {
271 if (flags
& LOCK_DIE_ON_ERROR
)
273 rollback_lock_file(lk
);
279 int close_lock_file(struct lock_file
*lk
)
288 int save_errno
= errno
;
289 rollback_lock_file(lk
);
296 int reopen_lock_file(struct lock_file
*lk
)
299 die(_("BUG: reopen a lockfile that is still open"));
301 die(_("BUG: reopen a lockfile that has been committed"));
302 lk
->fd
= open(lk
->filename
.buf
, O_WRONLY
);
306 int commit_lock_file(struct lock_file
*lk
)
308 static struct strbuf result_file
= STRBUF_INIT
;
312 die("BUG: attempt to commit unlocked object");
314 if (close_lock_file(lk
))
317 /* remove ".lock": */
318 strbuf_add(&result_file
, lk
->filename
.buf
,
319 lk
->filename
.len
- LOCK_SUFFIX_LEN
);
320 err
= rename(lk
->filename
.buf
, result_file
.buf
);
321 strbuf_reset(&result_file
);
323 int save_errno
= errno
;
324 rollback_lock_file(lk
);
330 strbuf_reset(&lk
->filename
);
334 int hold_locked_index(struct lock_file
*lk
, int die_on_error
)
336 return hold_lock_file_for_update(lk
, get_index_file(),
342 void rollback_lock_file(struct lock_file
*lk
)
347 if (!close_lock_file(lk
)) {
348 unlink_or_warn(lk
->filename
.buf
);
350 strbuf_reset(&lk
->filename
);