lockfile.c 5.6 KB

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