Skip to content
Snippets Groups Projects
Commit d66175d7 authored by Kaushik Lingarkar's avatar Kaushik Lingarkar
Browse files

LockFile: Retry lock creation if parent dirs were removed


In the small window between creation of the lock file's parent dirs and
the lock file itself, the parent dirs may be cleaned by an external
process packing refs in the repository. When this scenario occurs, retry
creating the lock file (along with its parent dirs).

Change-Id: Id7ec60c3f7f373b59f1dc8de6b8fa6df6bdf2570
Signed-off-by: default avatarKaushik Lingarkar <quic_kaushikl@quicinc.com>
parent e431a0d8
No related branches found
No related tags found
No related merge requests found
......@@ -24,6 +24,7 @@
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.FileTime;
import java.text.MessageFormat;
......@@ -141,9 +142,8 @@ public boolean lock() throws IOException {
throw new IllegalStateException(
MessageFormat.format(JGitText.get().lockAlreadyHeld, ref));
}
FileUtils.mkdirs(lck.getParentFile(), true);
try {
token = FS.DETECTED.createNewFileAtomic(lck);
token = createLockFileWithRetry();
} catch (IOException e) {
LOG.error(JGitText.get().failedCreateLockFile, lck, e);
throw e;
......@@ -160,6 +160,19 @@ public boolean lock() throws IOException {
return obtainedLock;
}
private FS.LockToken createLockFileWithRetry() throws IOException {
try {
return createLockFile();
} catch (NoSuchFileException e) {
return createLockFile();
}
}
private FS.LockToken createLockFile() throws IOException {
FileUtils.mkdirs(lck.getParentFile(), true);
return FS.DETECTED.createNewFileAtomic(lck);
}
/**
* Try to establish the lock for appending.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment