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
This commit is contained in:
Emily
2025-11-14 14:58:15 +01:00
committed by Qyriad
parent c5221e508f
commit ea0a2c8e74
4 changed files with 12 additions and 8 deletions
+1 -1
View File
@@ -6,7 +6,7 @@
namespace nix {
Path createTempDir(const Path & prefix, mode_t mode)
Path createTempDir(const std::optional<Path> & prefix, mode_t mode)
{
return createTempSubdir(defaultTempDir(), prefix, mode);
}
+1 -1
View File
@@ -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<Path> & prefix = "nix", mode_t mode = 0755);
/**
* Create a temporary file, returning a file handle and its path.
+7 -3
View File
@@ -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<Path> & 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<Path> & prefix)
{
static thread_local std::random_device generator{};
std::uniform_int_distribution<uint64_t> 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)
+3 -3
View File
@@ -312,16 +312,16 @@ typedef std::unique_ptr<DIR, DIRDeleter> 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<Path> & prefix = "nix",
mode_t mode = 0755);
/**
* Return temporary path constructed by appending to a root path.
*
* The constructed path looks like `<root><prefix>-<unique>`. To create a
* The constructed path looks like `<root>[<prefix>-]<unique>`. 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<Path> & prefix = ".tmp");
/**
* Return temporary path in the same directory as a given path.