aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/local-binary-cache-store.cc
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2021-03-26 17:10:15 +0100
committerEelco Dolstra <edolstra@gmail.com>2021-03-26 17:10:15 +0100
commitdd77f71afe6733e9790dd001125c423cb648b7ce (patch)
tree13c7cafb2e0804c5251c458f63b59abe23104d6e /src/libstore/local-binary-cache-store.cc
parent4638bcfb2cfb74cb5029c0da0af38bb7ca4b4a6f (diff)
LocalBinaryCacheStore::upsertFile(): Fix race
When multiple threads try to upsert the same file, this could fail. Fixes #4667.
Diffstat (limited to 'src/libstore/local-binary-cache-store.cc')
-rw-r--r--src/libstore/local-binary-cache-store.cc5
1 files changed, 4 insertions, 1 deletions
diff --git a/src/libstore/local-binary-cache-store.cc b/src/libstore/local-binary-cache-store.cc
index a58b7733f..964c4017e 100644
--- a/src/libstore/local-binary-cache-store.cc
+++ b/src/libstore/local-binary-cache-store.cc
@@ -2,6 +2,8 @@
#include "globals.hh"
#include "nar-info-disk-cache.hh"
+#include <atomic>
+
namespace nix {
struct LocalBinaryCacheStoreConfig : virtual BinaryCacheStoreConfig
@@ -50,7 +52,8 @@ protected:
const std::string & mimeType) override
{
auto path2 = binaryCacheDir + "/" + path;
- Path tmp = path2 + ".tmp." + std::to_string(getpid());
+ static std::atomic<int> counter{0};
+ Path tmp = fmt("%s.tmp.%d.%d", path2, getpid(), ++counter);
AutoDelete del(tmp, false);
StreamToSourceAdapter source(istream);
writeFile(tmp, source);