lock_ref_sha1_basic(): on SCLD_VANISHED, retry

If safe_create_leading_directories() fails because a file along the path unexpectedly vanished, try again (up to 3 times). This can occur if another process is deleting directories at the same time as we are trying to make them. For example, "git pack-refs --all" tries to delete the loose refs and any empty directories that are left behind. If a pack-refs process is running, then it might delete a directory that we need to put a new loose reference in. If safe_create_leading_directories() thinks this might have happened, then take its advice and try again (maximum three attempts). Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Jan 18, 2014 at 23:48 UTC c4c61c763e700d02344490590d6980ee51031a27
1 file changed +10 -1
refs.c
+10 -1
@@ -2039,6 +2039,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
2039 int type, lflags;
2040 int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
2041 int missing = 0;
2042 + int attempts_remaining = 3;
2043
2044 lock = xcalloc(1, sizeof(struct ref_lock));
2045 lock->lock_fd = -1;
@@ -2093,7 +2094,15 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
2094 if ((flags & REF_NODEREF) && (type & REF_ISSYMREF))
2095 lock->force_write = 1;
2096
2096 - if (safe_create_leading_directories(ref_file)) {
2097 + retry:
2098 + switch (safe_create_leading_directories(ref_file)) {
2099 + case SCLD_OK:
2100 + break; /* success */
2101 + case SCLD_VANISHED:
2102 + if (--attempts_remaining > 0)
2103 + goto retry;
2104 + /* fall through */
2105 + default:
2106 last_errno = errno;
2107 error("unable to create directory for %s", ref_file);
2108 goto error_return;