diff --git a/lix/libutil/file-system.cc b/lix/libutil/file-system.cc index 36fe09748..8686714bd 100644 --- a/lix/libutil/file-system.cc +++ b/lix/libutil/file-system.cc @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "c-calls.hh" @@ -699,11 +700,14 @@ Path createTempSubdir(const Path & parent, const Path & prefix, } } -Path makeTempPath(const Path & root, const Path & suffix) +Path makeTempPath(const Path & root, const Path & prefix) { - // start the counter at a random value to minimize issues with preexisting temp paths - static std::atomic_uint_fast32_t counter(std::random_device{}()); - return fmt("%1%%2%-%3%-%4%", root, suffix, getpid(), counter.fetch_add(1, std::memory_order_relaxed)); + static thread_local std::random_device generator{}; + std::uniform_int_distribution uniform_dist{}; + const uint64_t entropy[2] = {uniform_dist(generator), uniform_dist(generator)}; + auto unique = base32Encode(std::as_bytes(std::span(entropy))); + + return fmt("%s%s-%s", root, prefix, unique); } Path makeTempSiblingPath(const Path & path) diff --git a/lix/libutil/file-system.hh b/lix/libutil/file-system.hh index 395eed0ea..2c224aa4f 100644 --- a/lix/libutil/file-system.hh +++ b/lix/libutil/file-system.hh @@ -311,12 +311,12 @@ Path createTempSubdir(const Path & parent, const Path & prefix = "nix", bool includePid = true, bool useGlobalCounter = true, mode_t mode = 0755); /** - * Return temporary path constructed by appending a suffix to a root path. + * Return temporary path constructed by appending to a root path. * - * The constructed path looks like `--`. To create a - * path nested in a directory, provide a suffix starting with `/`. + * The constructed path looks like `-`. To create a + * path nested in a directory, provide a root ending with `/`. */ -Path makeTempPath(const Path & root, const Path & suffix = ".tmp"); +Path makeTempPath(const Path & root, const Path & prefix = ".tmp"); /** * Return temporary path in the same directory as a given path.