From ea0a2c8e74af3609fdf63d6bc5bf5ab7323d276e Mon Sep 17 00:00:00 2001 From: Emily Date: Thu, 31 Jul 2025 17:40:59 +0100 Subject: [PATCH] libstore: make temporary path prefixes optional This is not the same thing as passing an empty string, because it avoids the `-` separator. Change-Id: I6a6a696451667cbf500914e2dfbca2a4646ff20b --- lix/libstore/temporary-dir.cc | 2 +- lix/libstore/temporary-dir.hh | 2 +- lix/libutil/file-system.cc | 10 +++++++--- lix/libutil/file-system.hh | 6 +++--- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/lix/libstore/temporary-dir.cc b/lix/libstore/temporary-dir.cc index 384d064b0..fd58cd3f2 100644 --- a/lix/libstore/temporary-dir.cc +++ b/lix/libstore/temporary-dir.cc @@ -6,7 +6,7 @@ namespace nix { -Path createTempDir(const Path & prefix, mode_t mode) +Path createTempDir(const std::optional & prefix, mode_t mode) { return createTempSubdir(defaultTempDir(), prefix, mode); } diff --git a/lix/libstore/temporary-dir.hh b/lix/libstore/temporary-dir.hh index a2a8a0d18..a2a9587b2 100644 --- a/lix/libstore/temporary-dir.hh +++ b/lix/libstore/temporary-dir.hh @@ -8,7 +8,7 @@ namespace nix { /** * Create a temporary directory. */ -Path createTempDir(const Path & prefix = "nix", mode_t mode = 0755); +Path createTempDir(const std::optional & prefix = "nix", mode_t mode = 0755); /** * Create a temporary file, returning a file handle and its path. diff --git a/lix/libutil/file-system.cc b/lix/libutil/file-system.cc index e2552e07f..0d054e6cf 100644 --- a/lix/libutil/file-system.cc +++ b/lix/libutil/file-system.cc @@ -693,7 +693,7 @@ void AutoDelete::reset(const Path & p, bool recursive) { ////////////////////////////////////////////////////////////////////// -Path createTempSubdir(const Path & parent, const Path & prefix, +Path createTempSubdir(const Path & parent, const std::optional & prefix, mode_t mode) { checkInterrupt(); @@ -717,14 +717,18 @@ Path createTempSubdir(const Path & parent, const Path & prefix, throw SysError("creating directory '%1%'", tmpDir); } -Path makeTempPath(const Path & root, const Path & prefix) +Path makeTempPath(const Path & root, const std::optional & prefix) { 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); + if (prefix) { + return fmt("%s%s-%s", root, *prefix, unique); + } else { + return root + unique; + } } Path makeTempSiblingPath(const Path & path) diff --git a/lix/libutil/file-system.hh b/lix/libutil/file-system.hh index b228e434f..c05155594 100644 --- a/lix/libutil/file-system.hh +++ b/lix/libutil/file-system.hh @@ -312,16 +312,16 @@ typedef std::unique_ptr AutoCloseDir; /** * Create a temporary directory in a given parent directory. */ -Path createTempSubdir(const Path & parent, const Path & prefix = "nix", +Path createTempSubdir(const Path & parent, const std::optional & prefix = "nix", mode_t mode = 0755); /** * Return temporary path constructed by appending to a root path. * - * The constructed path looks like `-`. To create a + * 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 & prefix = ".tmp"); +Path makeTempPath(const Path & root, const std::optional & prefix = ".tmp"); /** * Return temporary path in the same directory as a given path.