blob: 8e8ab4f29f3eaf05f056211042b5fd2f01727363 [file] [log] [blame]
Junio C Hamano021b6e42006-06-06 12:51:49 -07001/*
2 * Copyright (c) 2005, Junio C Hamano
3 */
Michael Haggerty2db69de2015-08-10 11:47:36 +02004
Junio C Hamano021b6e42006-06-06 12:51:49 -07005#include "cache.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +02006#include "lockfile.h"
Junio C Hamano021b6e42006-06-06 12:51:49 -07007
Bradford C. Smith5d5a7a62007-07-26 13:34:14 -04008/*
Michael Haggerty0c0d6e82014-10-01 12:28:35 +02009 * path = absolute or relative path name
Bradford C. Smith5d5a7a62007-07-26 13:34:14 -040010 *
Michael Haggerty0c0d6e82014-10-01 12:28:35 +020011 * Remove the last path name element from path (leaving the preceding
12 * "/", if any). If path is empty or the root directory ("/"), set
13 * path to the empty string.
Bradford C. Smith5d5a7a62007-07-26 13:34:14 -040014 */
Michael Haggerty0c0d6e82014-10-01 12:28:35 +020015static void trim_last_path_component(struct strbuf *path)
Bradford C. Smith5d5a7a62007-07-26 13:34:14 -040016{
Michael Haggerty0c0d6e82014-10-01 12:28:35 +020017 int i = path->len;
Bradford C. Smith5d5a7a62007-07-26 13:34:14 -040018
19 /* back up past trailing slashes, if any */
Michael Haggerty0c0d6e82014-10-01 12:28:35 +020020 while (i && path->buf[i - 1] == '/')
21 i--;
Bradford C. Smith5d5a7a62007-07-26 13:34:14 -040022
23 /*
Michael Haggerty0c0d6e82014-10-01 12:28:35 +020024 * then go backwards until a slash, or the beginning of the
25 * string
Bradford C. Smith5d5a7a62007-07-26 13:34:14 -040026 */
Michael Haggerty0c0d6e82014-10-01 12:28:35 +020027 while (i && path->buf[i - 1] != '/')
28 i--;
29
30 strbuf_setlen(path, i);
Bradford C. Smith5d5a7a62007-07-26 13:34:14 -040031}
32
33
34/* We allow "recursive" symbolic links. Only within reason, though */
35#define MAXDEPTH 5
36
37/*
Michael Haggerty6cad8052014-10-01 12:28:34 +020038 * path contains a path that might be a symlink.
Bradford C. Smith5d5a7a62007-07-26 13:34:14 -040039 *
Michael Haggerty6cad8052014-10-01 12:28:34 +020040 * If path is a symlink, attempt to overwrite it with a path to the
41 * real file or directory (which may or may not exist), following a
42 * chain of symlinks if necessary. Otherwise, leave path unmodified.
Bradford C. Smith5d5a7a62007-07-26 13:34:14 -040043 *
Michael Haggerty6cad8052014-10-01 12:28:34 +020044 * This is a best-effort routine. If an error occurs, path will
45 * either be left unmodified or will name a different symlink in a
46 * symlink chain that started with the original path.
Bradford C. Smith5d5a7a62007-07-26 13:34:14 -040047 */
Michael Haggerty6cad8052014-10-01 12:28:34 +020048static void resolve_symlink(struct strbuf *path)
Bradford C. Smith5d5a7a62007-07-26 13:34:14 -040049{
50 int depth = MAXDEPTH;
Michael Haggerty5025d842014-10-01 12:28:33 +020051 static struct strbuf link = STRBUF_INIT;
Bradford C. Smith5d5a7a62007-07-26 13:34:14 -040052
53 while (depth--) {
Michael Haggerty6cad8052014-10-01 12:28:34 +020054 if (strbuf_readlink(&link, path->buf, path->len) < 0)
Michael Haggerty5025d842014-10-01 12:28:33 +020055 break;
Bradford C. Smith5d5a7a62007-07-26 13:34:14 -040056
Michael Haggerty6cad8052014-10-01 12:28:34 +020057 if (is_absolute_path(link.buf))
Bradford C. Smith5d5a7a62007-07-26 13:34:14 -040058 /* absolute path simply replaces p */
Michael Haggerty6cad8052014-10-01 12:28:34 +020059 strbuf_reset(path);
Michael Haggerty0c0d6e82014-10-01 12:28:35 +020060 else
Bradford C. Smith5d5a7a62007-07-26 13:34:14 -040061 /*
Michael Haggerty5025d842014-10-01 12:28:33 +020062 * link is a relative path, so replace the
Bradford C. Smith5d5a7a62007-07-26 13:34:14 -040063 * last element of p with it.
64 */
Michael Haggerty0c0d6e82014-10-01 12:28:35 +020065 trim_last_path_component(path);
Michael Haggerty6cad8052014-10-01 12:28:34 +020066
67 strbuf_addbuf(path, &link);
Bradford C. Smith5d5a7a62007-07-26 13:34:14 -040068 }
Michael Haggerty5025d842014-10-01 12:28:33 +020069 strbuf_reset(&link);
Bradford C. Smith5d5a7a62007-07-26 13:34:14 -040070}
71
Ronnie Sahlberg447ff1b2014-06-20 07:42:48 -070072/* Make sure errno contains a meaningful value on error */
Junio C Hamanoacd3b9e2008-10-17 15:44:39 -070073static int lock_file(struct lock_file *lk, const char *path, int flags)
Junio C Hamano021b6e42006-06-06 12:51:49 -070074{
Michael Haggerty1a9d15d2015-08-10 11:47:41 +020075 struct strbuf filename = STRBUF_INIT;
Michael Haggerty2fbd4f92013-07-06 21:48:52 +020076
Michael Haggerty1a9d15d2015-08-10 11:47:41 +020077 strbuf_addstr(&filename, path);
78 if (!(flags & LOCK_NO_DEREF))
79 resolve_symlink(&filename);
Michael Haggerty04e57d42014-10-01 12:28:13 +020080
Michael Haggerty1a9d15d2015-08-10 11:47:41 +020081 strbuf_addstr(&filename, LOCK_SUFFIX);
Jeff King076aa2c2017-09-05 08:15:08 -040082 lk->tempfile = create_tempfile(filename.buf);
Michael Haggerty1a9d15d2015-08-10 11:47:41 +020083 strbuf_release(&filename);
Jeff King076aa2c2017-09-05 08:15:08 -040084 return lk->tempfile ? lk->tempfile->fd : -1;
Junio C Hamano021b6e42006-06-06 12:51:49 -070085}
86
Michael Haggerty044b6a92015-05-11 12:35:25 +020087/*
88 * Constants defining the gaps between attempts to lock a file. The
89 * first backoff period is approximately INITIAL_BACKOFF_MS
90 * milliseconds. The longest backoff period is approximately
91 * (BACKOFF_MAX_MULTIPLIER * INITIAL_BACKOFF_MS) milliseconds.
92 */
93#define INITIAL_BACKOFF_MS 1L
94#define BACKOFF_MAX_MULTIPLIER 1000
95
96/*
97 * Try locking path, retrying with quadratic backoff for at least
98 * timeout_ms milliseconds. If timeout_ms is 0, try locking the file
99 * exactly once. If timeout_ms is -1, try indefinitely.
100 */
101static int lock_file_timeout(struct lock_file *lk, const char *path,
102 int flags, long timeout_ms)
103{
104 int n = 1;
105 int multiplier = 1;
Johannes Sixta8a17752015-06-05 21:45:06 +0200106 long remaining_ms = 0;
Michael Haggerty044b6a92015-05-11 12:35:25 +0200107 static int random_initialized = 0;
108
109 if (timeout_ms == 0)
110 return lock_file(lk, path, flags);
111
112 if (!random_initialized) {
Johannes Sixt1e9676e2015-06-05 21:45:04 +0200113 srand((unsigned int)getpid());
Michael Haggerty044b6a92015-05-11 12:35:25 +0200114 random_initialized = 1;
115 }
116
Johannes Sixta8a17752015-06-05 21:45:06 +0200117 if (timeout_ms > 0)
118 remaining_ms = timeout_ms;
Michael Haggerty044b6a92015-05-11 12:35:25 +0200119
120 while (1) {
Johannes Sixta8a17752015-06-05 21:45:06 +0200121 long backoff_ms, wait_ms;
Michael Haggerty044b6a92015-05-11 12:35:25 +0200122 int fd;
123
124 fd = lock_file(lk, path, flags);
125
126 if (fd >= 0)
127 return fd; /* success */
128 else if (errno != EEXIST)
129 return -1; /* failure other than lock held */
Johannes Sixta8a17752015-06-05 21:45:06 +0200130 else if (timeout_ms > 0 && remaining_ms <= 0)
Michael Haggerty044b6a92015-05-11 12:35:25 +0200131 return -1; /* failure due to timeout */
132
133 backoff_ms = multiplier * INITIAL_BACKOFF_MS;
134 /* back off for between 0.75*backoff_ms and 1.25*backoff_ms */
Johannes Sixta8a17752015-06-05 21:45:06 +0200135 wait_ms = (750 + rand() % 500) * backoff_ms / 1000;
Johannes Sixt30f81602015-06-05 21:45:07 +0200136 sleep_millisec(wait_ms);
Johannes Sixta8a17752015-06-05 21:45:06 +0200137 remaining_ms -= wait_ms;
Michael Haggerty044b6a92015-05-11 12:35:25 +0200138
139 /* Recursion: (n+1)^2 = n^2 + 2n + 1 */
140 multiplier += 2*n + 1;
141 if (multiplier > BACKOFF_MAX_MULTIPLIER)
142 multiplier = BACKOFF_MAX_MULTIPLIER;
143 else
144 n++;
145 }
146}
147
Ronnie Sahlberg6af926e2014-06-20 07:42:47 -0700148void unable_to_lock_message(const char *path, int err, struct strbuf *buf)
Matthieu Moye43a6fd2009-02-19 13:54:18 +0100149{
John Tapsellbdfd7392009-03-04 15:00:44 +0000150 if (err == EEXIST) {
Matthieu Moy3030c292016-03-01 18:04:09 +0100151 strbuf_addf(buf, _("Unable to create '%s.lock': %s.\n\n"
Matthieu Moyaed74802016-03-01 18:04:10 +0100152 "Another git process seems to be running in this repository, e.g.\n"
153 "an editor opened by 'git commit'. Please make sure all processes\n"
154 "are terminated then try again. If it still fails, a git process\n"
155 "may have crashed in this repository earlier:\n"
156 "remove the file manually to continue."),
Carlos Martín Nietoe2a57aa2011-03-17 12:26:46 +0100157 absolute_path(path), strerror(err));
Miklos Vajna1b018fd2009-09-27 01:15:09 +0200158 } else
Matthieu Moy3030c292016-03-01 18:04:09 +0100159 strbuf_addf(buf, _("Unable to create '%s.lock': %s"),
Carlos Martín Nietoe2a57aa2011-03-17 12:26:46 +0100160 absolute_path(path), strerror(err));
Miklos Vajna1b018fd2009-09-27 01:15:09 +0200161}
162
Michael Haggertye197c212014-10-01 12:28:05 +0200163NORETURN void unable_to_lock_die(const char *path, int err)
Miklos Vajna1b018fd2009-09-27 01:15:09 +0200164{
Ronnie Sahlberg6af926e2014-06-20 07:42:47 -0700165 struct strbuf buf = STRBUF_INIT;
166
167 unable_to_lock_message(path, err, &buf);
168 die("%s", buf.buf);
Matthieu Moye43a6fd2009-02-19 13:54:18 +0100169}
170
Ronnie Sahlberg447ff1b2014-06-20 07:42:48 -0700171/* This should return a meaningful errno on failure */
Michael Haggerty044b6a92015-05-11 12:35:25 +0200172int hold_lock_file_for_update_timeout(struct lock_file *lk, const char *path,
173 int flags, long timeout_ms)
Junio C Hamano40aaae82006-08-12 01:03:47 -0700174{
Michael Haggerty044b6a92015-05-11 12:35:25 +0200175 int fd = lock_file_timeout(lk, path, flags, timeout_ms);
Junio C Hamano3f061bf2016-12-07 10:56:26 -0800176 if (fd < 0) {
177 if (flags & LOCK_DIE_ON_ERROR)
178 unable_to_lock_die(path, errno);
179 if (flags & LOCK_REPORT_ON_ERROR) {
180 struct strbuf buf = STRBUF_INIT;
181 unable_to_lock_message(path, errno, &buf);
182 error("%s", buf.buf);
183 strbuf_release(&buf);
184 }
185 }
Junio C Hamano40aaae82006-08-12 01:03:47 -0700186 return fd;
187}
188
Michael Haggertyec38b4e2014-10-01 12:28:39 +0200189char *get_locked_file_path(struct lock_file *lk)
190{
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200191 struct strbuf ret = STRBUF_INIT;
192
Jeff King076aa2c2017-09-05 08:15:08 -0400193 strbuf_addstr(&ret, get_tempfile_path(lk->tempfile));
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200194 if (ret.len <= LOCK_SUFFIX_LEN ||
195 strcmp(ret.buf + ret.len - LOCK_SUFFIX_LEN, LOCK_SUFFIX))
Johannes Schindelin033abf92018-05-02 11:38:39 +0200196 BUG("get_locked_file_path() called for malformed lock object");
Michael Haggerty9c773812015-08-10 11:47:40 +0200197 /* remove ".lock": */
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200198 strbuf_setlen(&ret, ret.len - LOCK_SUFFIX_LEN);
199 return strbuf_detach(&ret, NULL);
Junio C Hamano021b6e42006-06-06 12:51:49 -0700200}
201
Junio C Hamano021b6e42006-06-06 12:51:49 -0700202int commit_lock_file(struct lock_file *lk)
203{
Michael Haggerty9c773812015-08-10 11:47:40 +0200204 char *result_path = get_locked_file_path(lk);
Junio C Hamano021b6e42006-06-06 12:51:49 -0700205
Michael Haggerty9c773812015-08-10 11:47:40 +0200206 if (commit_lock_file_to(lk, result_path)) {
207 int save_errno = errno;
208 free(result_path);
209 errno = save_errno;
210 return -1;
Johannes Schindelin4723ee92007-11-13 21:05:03 +0100211 }
Michael Haggerty9c773812015-08-10 11:47:40 +0200212 free(result_path);
213 return 0;
Junio C Hamano021b6e42006-06-06 12:51:49 -0700214}