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 * A lock_file object can be in several states:
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
35 * state, the lockfile exists, filename holds the filename of the
36 * lockfile, fd holds a file descriptor open for writing to the
37 * lockfile, and owner holds the PID of the process that locked the
40 * - Locked, lockfile closed (after close_lock_file()). Same as the
41 * previous state, except that the lockfile is closed and fd is -1.
43 * - Unlocked (after commit_lock_file(), rollback_lock_file(), or a
44 * failed attempt to lock). In this state, filename[0] == '\0' and
45 * fd is -1. The object is left registered in the lock_file_list,
49 static struct lock_file
*lock_file_list
;
51 static void remove_lock_file(void)
55 while (lock_file_list
) {
56 if (lock_file_list
->owner
== me
&&
57 lock_file_list
->filename
[0]) {
58 if (lock_file_list
->fd
>= 0)
59 close(lock_file_list
->fd
);
60 unlink_or_warn(lock_file_list
->filename
);
62 lock_file_list
= lock_file_list
->next
;
66 static void remove_lock_file_on_signal(int signo
)
74 * p = absolute or relative path name
76 * Return a pointer into p showing the beginning of the last path name
77 * element. If p is empty or the root directory ("/"), just return p.
79 static char *last_path_elm(char *p
)
81 /* r starts pointing to null at the end of the string */
82 char *r
= strchr(p
, '\0');
85 return p
; /* just return empty string */
87 r
--; /* back up to last non-null character */
89 /* back up past trailing slashes, if any */
90 while (r
> p
&& *r
== '/')
94 * then go backwards until I hit a slash, or the beginning of
97 while (r
> p
&& *(r
-1) != '/')
103 /* We allow "recursive" symbolic links. Only within reason, though */
107 * p = path that may be a symlink
110 * If p is a symlink, attempt to overwrite p with a path to the real
111 * file or directory (which may or may not exist), following a chain of
112 * symlinks if necessary. Otherwise, leave p unmodified.
114 * This is a best-effort routine. If an error occurs, p will either be
115 * left unmodified or will name a different symlink in a symlink chain
116 * that started with p's initial contents.
121 static char *resolve_symlink(char *p
, size_t s
)
123 int depth
= MAXDEPTH
;
127 int link_len
= readlink(p
, link
, sizeof(link
));
129 /* not a symlink anymore */
132 else if (link_len
< sizeof(link
))
133 /* readlink() never null-terminates */
134 link
[link_len
] = '\0';
136 warning("%s: symlink too long", p
);
140 if (is_absolute_path(link
)) {
141 /* absolute path simply replaces p */
145 warning("%s: symlink too long", p
);
150 * link is a relative path, so I must replace the
151 * last element of p with it.
153 char *r
= (char *)last_path_elm(p
);
154 if (r
- p
+ link_len
< s
)
157 warning("%s: symlink too long", p
);
165 /* Make sure errno contains a meaningful value on error */
166 static int lock_file(struct lock_file
*lk
, const char *path
, int flags
)
169 * subtract LOCK_SUFFIX_LEN from size to make sure there's
170 * room for adding ".lock" for the lock file name:
172 static const size_t max_path_len
= sizeof(lk
->filename
) -
175 if (!lock_file_list
) {
176 /* One-time initialization */
177 sigchain_push_common(remove_lock_file_on_signal
);
178 atexit(remove_lock_file
);
182 /* Initialize *lk and add it to lock_file_list: */
186 lk
->next
= lock_file_list
;
191 if (strlen(path
) >= max_path_len
) {
192 errno
= ENAMETOOLONG
;
195 strcpy(lk
->filename
, path
);
196 if (!(flags
& LOCK_NODEREF
))
197 resolve_symlink(lk
->filename
, max_path_len
);
198 strcat(lk
->filename
, LOCK_SUFFIX
);
199 lk
->fd
= open(lk
->filename
, O_RDWR
| O_CREAT
| O_EXCL
, 0666);
204 lk
->owner
= getpid();
205 if (adjust_shared_perm(lk
->filename
)) {
206 int save_errno
= errno
;
207 error("cannot fix permission bits on %s", lk
->filename
);
208 rollback_lock_file(lk
);
215 void unable_to_lock_message(const char *path
, int err
, struct strbuf
*buf
)
218 strbuf_addf(buf
, "Unable to create '%s.lock': %s.\n\n"
219 "If no other git process is currently running, this probably means a\n"
220 "git process crashed in this repository earlier. Make sure no other git\n"
221 "process is running and remove the file manually to continue.",
222 absolute_path(path
), strerror(err
));
224 strbuf_addf(buf
, "Unable to create '%s.lock': %s",
225 absolute_path(path
), strerror(err
));
228 int unable_to_lock_error(const char *path
, int err
)
230 struct strbuf buf
= STRBUF_INIT
;
232 unable_to_lock_message(path
, err
, &buf
);
233 error("%s", buf
.buf
);
234 strbuf_release(&buf
);
238 NORETURN
void unable_to_lock_die(const char *path
, int err
)
240 struct strbuf buf
= STRBUF_INIT
;
242 unable_to_lock_message(path
, err
, &buf
);
246 /* This should return a meaningful errno on failure */
247 int hold_lock_file_for_update(struct lock_file
*lk
, const char *path
, int flags
)
249 int fd
= lock_file(lk
, path
, flags
);
250 if (fd
< 0 && (flags
& LOCK_DIE_ON_ERROR
))
251 unable_to_lock_die(path
, errno
);
255 int hold_lock_file_for_append(struct lock_file
*lk
, const char *path
, int flags
)
259 fd
= lock_file(lk
, path
, flags
);
261 if (flags
& LOCK_DIE_ON_ERROR
)
262 unable_to_lock_die(path
, errno
);
266 orig_fd
= open(path
, O_RDONLY
);
268 if (errno
!= ENOENT
) {
269 if (flags
& LOCK_DIE_ON_ERROR
)
270 die("cannot open '%s' for copying", path
);
271 rollback_lock_file(lk
);
272 return error("cannot open '%s' for copying", path
);
274 } else if (copy_fd(orig_fd
, fd
)) {
275 if (flags
& LOCK_DIE_ON_ERROR
)
277 rollback_lock_file(lk
);
283 int close_lock_file(struct lock_file
*lk
)
294 int reopen_lock_file(struct lock_file
*lk
)
297 die(_("BUG: reopen a lockfile that is still open"));
298 if (!lk
->filename
[0])
299 die(_("BUG: reopen a lockfile that has been committed"));
300 lk
->fd
= open(lk
->filename
, O_WRONLY
);
304 int commit_lock_file(struct lock_file
*lk
)
306 char result_file
[PATH_MAX
];
308 if (close_lock_file(lk
))
310 strcpy(result_file
, lk
->filename
);
311 i
= strlen(result_file
) - LOCK_SUFFIX_LEN
; /* .lock */
313 if (rename(lk
->filename
, result_file
))
319 int hold_locked_index(struct lock_file
*lk
, int die_on_error
)
321 return hold_lock_file_for_update(lk
, get_index_file(),
327 void rollback_lock_file(struct lock_file
*lk
)
329 if (!lk
->filename
[0])
333 unlink_or_warn(lk
->filename
);