aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2021-08-02 16:13:53 +0200
committerGitHub <noreply@github.com>2021-08-02 16:13:53 +0200
commit47e96bb533f8cacc171bec9b688b134de31a48a9 (patch)
treec30747920c712b035ec146dd989c700a52f0d373
parent94ec9e47030c2a7280503d338f0dca7ad92811f5 (diff)
parent5c99ec374cbd62fc59e6337d96f8d43414e1117b (diff)
Merge pull request #5082 from manveru/fix-git-init-race
fix git init race condition
-rw-r--r--src/libfetchers/git.cc9
1 files changed, 8 insertions, 1 deletions
diff --git a/src/libfetchers/git.cc b/src/libfetchers/git.cc
index bc1930138..d89763ccd 100644
--- a/src/libfetchers/git.cc
+++ b/src/libfetchers/git.cc
@@ -4,6 +4,7 @@
#include "tarfile.hh"
#include "store-api.hh"
#include "url-parts.hh"
+#include "pathlocks.hh"
#include <sys/time.h>
#include <sys/wait.h>
@@ -317,11 +318,17 @@ struct GitInputScheme : InputScheme
Path cacheDir = getCacheDir() + "/nix/gitv3/" + hashString(htSHA256, actualUrl).to_string(Base32, false);
repoDir = cacheDir;
+ Path cacheDirLock = cacheDir + ".lock";
+ createDirs(dirOf(cacheDir));
+ AutoCloseFD lock = openLockFile(cacheDirLock, true);
+ lockFile(lock.get(), ltWrite, true);
+
if (!pathExists(cacheDir)) {
- createDirs(dirOf(cacheDir));
runProgram("git", true, { "init", "--bare", repoDir });
}
+ deleteLockFile(cacheDirLock, lock.get());
+
Path localRefFile =
input.getRef()->compare(0, 5, "refs/") == 0
? cacheDir + "/" + *input.getRef()