From cad304420f012c69a8ff36c100674c0b868bbc87 Mon Sep 17 00:00:00 2001 From: Emily Date: Sun, 3 Aug 2025 19:06:02 +0100 Subject: [PATCH] libstore: use `makeTemp{,Sibling}Path` more Change-Id: I6a6a69641f9c9d3ab339d25d061dad6cd1f8416d --- lix/libstore/local-binary-cache-store.cc | 3 +-- lix/libstore/local-store.cc | 2 +- lix/libutil/file-system.cc | 17 +++-------------- 3 files changed, 5 insertions(+), 17 deletions(-) diff --git a/lix/libstore/local-binary-cache-store.cc b/lix/libstore/local-binary-cache-store.cc index f61593937..10c3a0066 100644 --- a/lix/libstore/local-binary-cache-store.cc +++ b/lix/libstore/local-binary-cache-store.cc @@ -69,8 +69,7 @@ protected: ) override try { auto path2 = binaryCacheDir + "/" + path; - static std::atomic counter{0}; - Path tmp = fmt("%s.tmp.%d.%d", path2, getpid(), ++counter); + Path tmp = makeTempPath(path2); AutoDelete del(tmp, false); StreamToSourceAdapter source(istream); writeFile(tmp, source); diff --git a/lix/libstore/local-store.cc b/lix/libstore/local-store.cc index 5373f175b..c75001e4f 100644 --- a/lix/libstore/local-store.cc +++ b/lix/libstore/local-store.cc @@ -1734,7 +1734,7 @@ try { createDirs(dirOf(logPath)); - auto tmpFile = fmt("%s.tmp.%d", logPath, getpid()); + auto tmpFile = makeTempSiblingPath(logPath); writeFile(tmpFile, compress("bzip2", log)); diff --git a/lix/libutil/file-system.cc b/lix/libutil/file-system.cc index 6a62e0dd1..3b1d3f676 100644 --- a/lix/libutil/file-system.cc +++ b/lix/libutil/file-system.cc @@ -711,20 +711,9 @@ void createSymlink(const Path & target, const Path & link) void replaceSymlink(const Path & target, const Path & link) { - for (unsigned int n = 0; true; n++) { - Path tmp = canonPath(fmt("%s/.%d_%s", dirOf(link), n, baseNameOf(link))); - - try { - createSymlink(target, tmp); - } catch (SysError & e) { - if (e.errNo == EEXIST) continue; - throw; - } - - renameFile(tmp, link); - - break; - } + Path tmp = canonPath(makeTempSiblingPath(link)); + createSymlink(target, tmp); + renameFile(tmp, link); } void setWriteTime(const fs::path & p, const struct stat & st)