git
/
lockfile.c
218 строк · 5.7 Кб
1/*
2* Copyright (c) 2005, Junio C Hamano
3*/
4
5#include "git-compat-util.h"6#include "abspath.h"7#include "gettext.h"8#include "lockfile.h"9
10/*
11* path = absolute or relative path name
12*
13* Remove the last path name element from path (leaving the preceding
14* "/", if any). If path is empty or the root directory ("/"), set
15* path to the empty string.
16*/
17static void trim_last_path_component(struct strbuf *path)18{
19int i = path->len;20
21/* back up past trailing slashes, if any */22while (i && path->buf[i - 1] == '/')23i--;24
25/*26* then go backwards until a slash, or the beginning of the
27* string
28*/
29while (i && path->buf[i - 1] != '/')30i--;31
32strbuf_setlen(path, i);33}
34
35
36/* We allow "recursive" symbolic links. Only within reason, though */
37#define MAXDEPTH 538
39/*
40* path contains a path that might be a symlink.
41*
42* If path is a symlink, attempt to overwrite it with a path to the
43* real file or directory (which may or may not exist), following a
44* chain of symlinks if necessary. Otherwise, leave path unmodified.
45*
46* This is a best-effort routine. If an error occurs, path will
47* either be left unmodified or will name a different symlink in a
48* symlink chain that started with the original path.
49*/
50static void resolve_symlink(struct strbuf *path)51{
52int depth = MAXDEPTH;53static struct strbuf link = STRBUF_INIT;54
55while (depth--) {56if (strbuf_readlink(&link, path->buf, path->len) < 0)57break;58
59if (is_absolute_path(link.buf))60/* absolute path simply replaces p */61strbuf_reset(path);62else63/*64* link is a relative path, so replace the
65* last element of p with it.
66*/
67trim_last_path_component(path);68
69strbuf_addbuf(path, &link);70}71strbuf_reset(&link);72}
73
74/* Make sure errno contains a meaningful value on error */
75static int lock_file(struct lock_file *lk, const char *path, int flags,76int mode)77{
78struct strbuf filename = STRBUF_INIT;79
80strbuf_addstr(&filename, path);81if (!(flags & LOCK_NO_DEREF))82resolve_symlink(&filename);83
84strbuf_addstr(&filename, LOCK_SUFFIX);85lk->tempfile = create_tempfile_mode(filename.buf, mode);86strbuf_release(&filename);87return lk->tempfile ? lk->tempfile->fd : -1;88}
89
90/*
91* Constants defining the gaps between attempts to lock a file. The
92* first backoff period is approximately INITIAL_BACKOFF_MS
93* milliseconds. The longest backoff period is approximately
94* (BACKOFF_MAX_MULTIPLIER * INITIAL_BACKOFF_MS) milliseconds.
95*/
96#define INITIAL_BACKOFF_MS 1L97#define BACKOFF_MAX_MULTIPLIER 100098
99/*
100* Try locking path, retrying with quadratic backoff for at least
101* timeout_ms milliseconds. If timeout_ms is 0, try locking the file
102* exactly once. If timeout_ms is -1, try indefinitely.
103*/
104static int lock_file_timeout(struct lock_file *lk, const char *path,105int flags, long timeout_ms, int mode)106{
107int n = 1;108int multiplier = 1;109long remaining_ms = 0;110static int random_initialized = 0;111
112if (timeout_ms == 0)113return lock_file(lk, path, flags, mode);114
115if (!random_initialized) {116srand((unsigned int)getpid());117random_initialized = 1;118}119
120if (timeout_ms > 0)121remaining_ms = timeout_ms;122
123while (1) {124long backoff_ms, wait_ms;125int fd;126
127fd = lock_file(lk, path, flags, mode);128
129if (fd >= 0)130return fd; /* success */131else if (errno != EEXIST)132return -1; /* failure other than lock held */133else if (timeout_ms > 0 && remaining_ms <= 0)134return -1; /* failure due to timeout */135
136backoff_ms = multiplier * INITIAL_BACKOFF_MS;137/* back off for between 0.75*backoff_ms and 1.25*backoff_ms */138wait_ms = (750 + rand() % 500) * backoff_ms / 1000;139sleep_millisec(wait_ms);140remaining_ms -= wait_ms;141
142/* Recursion: (n+1)^2 = n^2 + 2n + 1 */143multiplier += 2*n + 1;144if (multiplier > BACKOFF_MAX_MULTIPLIER)145multiplier = BACKOFF_MAX_MULTIPLIER;146else147n++;148}149}
150
151void unable_to_lock_message(const char *path, int err, struct strbuf *buf)152{
153if (err == EEXIST) {154strbuf_addf(buf, _("Unable to create '%s.lock': %s.\n\n"155"Another git process seems to be running in this repository, e.g.\n"156"an editor opened by 'git commit'. Please make sure all processes\n"157"are terminated then try again. If it still fails, a git process\n"158"may have crashed in this repository earlier:\n"159"remove the file manually to continue."),160absolute_path(path), strerror(err));161} else162strbuf_addf(buf, _("Unable to create '%s.lock': %s"),163absolute_path(path), strerror(err));164}
165
166NORETURN void unable_to_lock_die(const char *path, int err)167{
168struct strbuf buf = STRBUF_INIT;169
170unable_to_lock_message(path, err, &buf);171die("%s", buf.buf);172}
173
174/* This should return a meaningful errno on failure */
175int hold_lock_file_for_update_timeout_mode(struct lock_file *lk,176const char *path, int flags,177long timeout_ms, int mode)178{
179int fd = lock_file_timeout(lk, path, flags, timeout_ms, mode);180if (fd < 0) {181if (flags & LOCK_DIE_ON_ERROR)182unable_to_lock_die(path, errno);183if (flags & LOCK_REPORT_ON_ERROR) {184struct strbuf buf = STRBUF_INIT;185unable_to_lock_message(path, errno, &buf);186error("%s", buf.buf);187strbuf_release(&buf);188}189}190return fd;191}
192
193char *get_locked_file_path(struct lock_file *lk)194{
195struct strbuf ret = STRBUF_INIT;196
197strbuf_addstr(&ret, get_tempfile_path(lk->tempfile));198if (ret.len <= LOCK_SUFFIX_LEN ||199strcmp(ret.buf + ret.len - LOCK_SUFFIX_LEN, LOCK_SUFFIX))200BUG("get_locked_file_path() called for malformed lock object");201/* remove ".lock": */202strbuf_setlen(&ret, ret.len - LOCK_SUFFIX_LEN);203return strbuf_detach(&ret, NULL);204}
205
206int commit_lock_file(struct lock_file *lk)207{
208char *result_path = get_locked_file_path(lk);209
210if (commit_lock_file_to(lk, result_path)) {211int save_errno = errno;212free(result_path);213errno = save_errno;214return -1;215}216free(result_path);217return 0;218}
219