lockfile.c: rename static functions
[git/git.git] / lockfile.c
CommitLineData
021b6e45
JH
1/*
2 * Copyright (c) 2005, Junio C Hamano
3 */
021b6e45 4#include "cache.h"
4a16d072 5#include "sigchain.h"
021b6e45 6
0a06f148
MH
7/*
8 * File write-locks as used by Git.
9 *
10 * For an overview of how to use the lockfile API, please see
11 *
12 * Documentation/technical/api-lockfile.txt
13 *
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.
20 *
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
24 * its parent.
25 *
707103fd 26 * The possible states of a lock_file object are as follows:
0a06f148
MH
27 *
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.
32 *
33 * - Locked, lockfile open (after hold_lock_file_for_update(),
34 * hold_lock_file_for_append(), or reopen_lock_file()). In this
707103fd
MH
35 * state:
36 * - the lockfile exists
37 * - active is set
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
0a06f148 41 *
8e86c155
MH
42 * - Locked, lockfile closed (after successful close_lock_file()).
43 * Same as the previous state, except that the lockfile is closed
44 * and fd is -1.
0a06f148 45 *
751baced
MH
46 * - Unlocked (after commit_lock_file(), commit_lock_file_to(),
47 * rollback_lock_file(), a failed attempt to lock, or a failed
48 * close_lock_file()). In this state:
707103fd 49 * - active is unset
cf6950d3
MH
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.
707103fd
MH
53 * - fd is -1
54 * - the object is left registered in the lock_file_list, and
55 * on_list is set.
0a06f148
MH
56 */
57
2091c506 58static struct lock_file *volatile lock_file_list;
021b6e45 59
316683bd 60static void remove_lock_files(void)
021b6e45 61{
5e635e39
JH
62 pid_t me = getpid();
63
021b6e45 64 while (lock_file_list) {
a1754bcc
MH
65 if (lock_file_list->owner == me)
66 rollback_lock_file(lock_file_list);
021b6e45
JH
67 lock_file_list = lock_file_list->next;
68 }
69}
70
316683bd 71static void remove_lock_files_on_signal(int signo)
021b6e45 72{
316683bd 73 remove_lock_files();
4a16d072 74 sigchain_pop(signo);
021b6e45
JH
75 raise(signo);
76}
77
5d5a7a67 78/*
0c0d6e86 79 * path = absolute or relative path name
5d5a7a67 80 *
0c0d6e86
MH
81 * Remove the last path name element from path (leaving the preceding
82 * "/", if any). If path is empty or the root directory ("/"), set
83 * path to the empty string.
5d5a7a67 84 */
0c0d6e86 85static void trim_last_path_component(struct strbuf *path)
5d5a7a67 86{
0c0d6e86 87 int i = path->len;
5d5a7a67
BS
88
89 /* back up past trailing slashes, if any */
0c0d6e86
MH
90 while (i && path->buf[i - 1] == '/')
91 i--;
5d5a7a67
BS
92
93 /*
0c0d6e86
MH
94 * then go backwards until a slash, or the beginning of the
95 * string
5d5a7a67 96 */
0c0d6e86
MH
97 while (i && path->buf[i - 1] != '/')
98 i--;
99
100 strbuf_setlen(path, i);
5d5a7a67
BS
101}
102
103
104/* We allow "recursive" symbolic links. Only within reason, though */
105#define MAXDEPTH 5
106
107/*
6cad8053 108 * path contains a path that might be a symlink.
5d5a7a67 109 *
6cad8053
MH
110 * If path is a symlink, attempt to overwrite it with a path to the
111 * real file or directory (which may or may not exist), following a
112 * chain of symlinks if necessary. Otherwise, leave path unmodified.
5d5a7a67 113 *
6cad8053
MH
114 * This is a best-effort routine. If an error occurs, path will
115 * either be left unmodified or will name a different symlink in a
116 * symlink chain that started with the original path.
5d5a7a67 117 */
6cad8053 118static void resolve_symlink(struct strbuf *path)
5d5a7a67
BS
119{
120 int depth = MAXDEPTH;
5025d845 121 static struct strbuf link = STRBUF_INIT;
5d5a7a67
BS
122
123 while (depth--) {
6cad8053 124 if (strbuf_readlink(&link, path->buf, path->len) < 0)
5025d845 125 break;
5d5a7a67 126
6cad8053 127 if (is_absolute_path(link.buf))
5d5a7a67 128 /* absolute path simply replaces p */
6cad8053 129 strbuf_reset(path);
0c0d6e86 130 else
5d5a7a67 131 /*
5025d845 132 * link is a relative path, so replace the
5d5a7a67
BS
133 * last element of p with it.
134 */
0c0d6e86 135 trim_last_path_component(path);
6cad8053
MH
136
137 strbuf_addbuf(path, &link);
5d5a7a67 138 }
5025d845 139 strbuf_reset(&link);
5d5a7a67
BS
140}
141
447ff1bf 142/* Make sure errno contains a meaningful value on error */
acd3b9ec 143static int lock_file(struct lock_file *lk, const char *path, int flags)
021b6e45 144{
6cad8053
MH
145 size_t pathlen = strlen(path);
146
04e57d4d
MH
147 if (!lock_file_list) {
148 /* One-time initialization */
316683bd
MH
149 sigchain_push_common(remove_lock_files_on_signal);
150 atexit(remove_lock_files);
04e57d4d
MH
151 }
152
707103fd
MH
153 if (lk->active)
154 die("BUG: cannot lock_file(\"%s\") using active struct lock_file",
155 path);
04e57d4d
MH
156 if (!lk->on_list) {
157 /* Initialize *lk and add it to lock_file_list: */
158 lk->fd = -1;
707103fd 159 lk->active = 0;
04e57d4d 160 lk->owner = 0;
6cad8053 161 strbuf_init(&lk->filename, pathlen + LOCK_SUFFIX_LEN);
04e57d4d
MH
162 lk->next = lock_file_list;
163 lock_file_list = lk;
164 lk->on_list = 1;
cf6950d3
MH
165 } else if (lk->filename.len) {
166 /* This shouldn't happen, but better safe than sorry. */
167 die("BUG: lock_file(\"%s\") called with improperly-reset lock_file object",
168 path);
04e57d4d
MH
169 }
170
6cad8053 171 strbuf_add(&lk->filename, path, pathlen);
47ba4662 172 if (!(flags & LOCK_NO_DEREF))
6cad8053 173 resolve_symlink(&lk->filename);
cf6950d3
MH
174 strbuf_addstr(&lk->filename, LOCK_SUFFIX);
175 lk->fd = open(lk->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666);
e31e949b 176 if (lk->fd < 0) {
cf6950d3 177 strbuf_reset(&lk->filename);
e31e949b
MH
178 return -1;
179 }
180 lk->owner = getpid();
707103fd 181 lk->active = 1;
cf6950d3 182 if (adjust_shared_perm(lk->filename.buf)) {
e31e949b 183 int save_errno = errno;
cf6950d3 184 error("cannot fix permission bits on %s", lk->filename.buf);
e31e949b
MH
185 rollback_lock_file(lk);
186 errno = save_errno;
187 return -1;
188 }
4723ee99 189 return lk->fd;
021b6e45
JH
190}
191
6af926e8 192void unable_to_lock_message(const char *path, int err, struct strbuf *buf)
e43a6fd3 193{
bdfd739d 194 if (err == EEXIST) {
6af926e8 195 strbuf_addf(buf, "Unable to create '%s.lock': %s.\n\n"
e43a6fd3
MM
196 "If no other git process is currently running, this probably means a\n"
197 "git process crashed in this repository earlier. Make sure no other git\n"
198 "process is running and remove the file manually to continue.",
e2a57aac 199 absolute_path(path), strerror(err));
1b018fd9 200 } else
6af926e8 201 strbuf_addf(buf, "Unable to create '%s.lock': %s",
e2a57aac 202 absolute_path(path), strerror(err));
1b018fd9
MV
203}
204
205int unable_to_lock_error(const char *path, int err)
206{
6af926e8
RS
207 struct strbuf buf = STRBUF_INIT;
208
209 unable_to_lock_message(path, err, &buf);
210 error("%s", buf.buf);
211 strbuf_release(&buf);
1b018fd9
MV
212 return -1;
213}
214
e197c218 215NORETURN void unable_to_lock_die(const char *path, int err)
1b018fd9 216{
6af926e8
RS
217 struct strbuf buf = STRBUF_INIT;
218
219 unable_to_lock_message(path, err, &buf);
220 die("%s", buf.buf);
e43a6fd3
MM
221}
222
447ff1bf 223/* This should return a meaningful errno on failure */
acd3b9ec 224int hold_lock_file_for_update(struct lock_file *lk, const char *path, int flags)
40aaae88 225{
acd3b9ec
JH
226 int fd = lock_file(lk, path, flags);
227 if (fd < 0 && (flags & LOCK_DIE_ON_ERROR))
e197c218 228 unable_to_lock_die(path, errno);
40aaae88
JH
229 return fd;
230}
231
acd3b9ec 232int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
ea3cd5c7
DB
233{
234 int fd, orig_fd;
235
acd3b9ec 236 fd = lock_file(lk, path, flags);
ea3cd5c7 237 if (fd < 0) {
acd3b9ec 238 if (flags & LOCK_DIE_ON_ERROR)
e197c218 239 unable_to_lock_die(path, errno);
ea3cd5c7
DB
240 return fd;
241 }
242
243 orig_fd = open(path, O_RDONLY);
244 if (orig_fd < 0) {
245 if (errno != ENOENT) {
acd3b9ec 246 if (flags & LOCK_DIE_ON_ERROR)
ea3cd5c7 247 die("cannot open '%s' for copying", path);
ebb8e380 248 rollback_lock_file(lk);
ea3cd5c7
DB
249 return error("cannot open '%s' for copying", path);
250 }
251 } else if (copy_fd(orig_fd, fd)) {
acd3b9ec 252 if (flags & LOCK_DIE_ON_ERROR)
ea3cd5c7 253 exit(128);
ebb8e380 254 rollback_lock_file(lk);
ea3cd5c7
DB
255 return -1;
256 }
257 return fd;
258}
259
d6cf61bf
BC
260int close_lock_file(struct lock_file *lk)
261{
262 int fd = lk->fd;
419f0c0f
MH
263
264 if (fd < 0)
265 return 0;
266
d6cf61bf 267 lk->fd = -1;
8e86c155
MH
268 if (close(fd)) {
269 int save_errno = errno;
270 rollback_lock_file(lk);
271 errno = save_errno;
272 return -1;
273 }
274 return 0;
d6cf61bf
BC
275}
276
93dcaea2
JH
277int reopen_lock_file(struct lock_file *lk)
278{
279 if (0 <= lk->fd)
280 die(_("BUG: reopen a lockfile that is still open"));
707103fd 281 if (!lk->active)
93dcaea2 282 die(_("BUG: reopen a lockfile that has been committed"));
cf6950d3 283 lk->fd = open(lk->filename.buf, O_WRONLY);
93dcaea2
JH
284 return lk->fd;
285}
286
751baced 287int commit_lock_file_to(struct lock_file *lk, const char *path)
021b6e45 288{
707103fd 289 if (!lk->active)
751baced 290 die("BUG: attempt to commit unlocked object to \"%s\"", path);
8a1c7533 291
419f0c0f 292 if (close_lock_file(lk))
d6cf61bf 293 return -1;
4f4713df 294
751baced 295 if (rename(lk->filename.buf, path)) {
1b1648f4
MH
296 int save_errno = errno;
297 rollback_lock_file(lk);
298 errno = save_errno;
d6cf61bf 299 return -1;
1b1648f4
MH
300 }
301
707103fd 302 lk->active = 0;
cf6950d3 303 strbuf_reset(&lk->filename);
d6cf61bf 304 return 0;
021b6e45
JH
305}
306
751baced
MH
307int commit_lock_file(struct lock_file *lk)
308{
309 static struct strbuf result_file = STRBUF_INIT;
310 int err;
311
312 if (!lk->active)
313 die("BUG: attempt to commit unlocked object");
314
315 if (lk->filename.len <= LOCK_SUFFIX_LEN ||
316 strcmp(lk->filename.buf + lk->filename.len - LOCK_SUFFIX_LEN, LOCK_SUFFIX))
317 die("BUG: lockfile filename corrupt");
318
319 /* remove ".lock": */
320 strbuf_add(&result_file, lk->filename.buf,
321 lk->filename.len - LOCK_SUFFIX_LEN);
322 err = commit_lock_file_to(lk, result_file.buf);
323 strbuf_reset(&result_file);
324 return err;
325}
326
30ca07a2
JH
327int hold_locked_index(struct lock_file *lk, int die_on_error)
328{
acd3b9ec
JH
329 return hold_lock_file_for_update(lk, get_index_file(),
330 die_on_error
331 ? LOCK_DIE_ON_ERROR
332 : 0);
30ca07a2
JH
333}
334
021b6e45
JH
335void rollback_lock_file(struct lock_file *lk)
336{
707103fd 337 if (!lk->active)
9085f8e2
MH
338 return;
339
8e86c155 340 if (!close_lock_file(lk)) {
cf6950d3 341 unlink_or_warn(lk->filename.buf);
707103fd 342 lk->active = 0;
cf6950d3 343 strbuf_reset(&lk->filename);
8e86c155 344 }
021b6e45 345}