1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-28 01:11:15 +02:00

Consolidate tempName and makeTempPath

This commit is contained in:
Farid Zakaria 2025-05-21 09:01:49 -07:00
parent 6aed9d877c
commit 9e26549c2b
4 changed files with 7 additions and 21 deletions

View file

@ -34,7 +34,7 @@ SSHMaster::SSHMaster(
throw Error("invalid SSH host name '%s'", host); throw Error("invalid SSH host name '%s'", host);
auto state(state_.lock()); auto state(state_.lock());
state->tmpDir = std::make_unique<AutoDelete>(createTempDir("", "nix", true, true, 0700)); state->tmpDir = std::make_unique<AutoDelete>(createTempDir("", "nix", 0700));
} }
void SSHMaster::addCommonSSHOpts(Strings & args) void SSHMaster::addCommonSSHOpts(Strings & args)

View file

@ -1,4 +1,5 @@
#include "nix/store/build/derivation-builder.hh" #include "nix/store/build/derivation-builder.hh"
#include "nix/util/file-system.hh"
#include "nix/store/local-store.hh" #include "nix/store/local-store.hh"
#include "nix/util/processes.hh" #include "nix/util/processes.hh"
#include "nix/store/builtins.hh" #include "nix/store/builtins.hh"
@ -879,7 +880,7 @@ void DerivationBuilderImpl::startBuilder()
/* Create a temporary directory where the build will take /* Create a temporary directory where the build will take
place. */ place. */
topTmpDir = createTempDir(settings.buildDir.get().value_or(""), "nix-build-" + std::string(drvPath.name()), false, false, 0700); topTmpDir = createTempDir(settings.buildDir.get().value_or(""), "nix-build-" + std::string(drvPath.name()), 0700);
#ifdef __APPLE__ #ifdef __APPLE__
if (false) { if (false) {
#else #else

View file

@ -578,26 +578,11 @@ std::string defaultTempDir() {
return getEnvNonEmpty("TMPDIR").value_or("/tmp"); return getEnvNonEmpty("TMPDIR").value_or("/tmp");
} }
static Path tempName(Path tmpRoot, const Path & prefix, bool includePid, Path createTempDir(const Path & tmpRoot, const Path & prefix, mode_t mode)
std::atomic<unsigned int> & counter)
{ {
tmpRoot = canonPath(tmpRoot.empty() ? defaultTempDir() : tmpRoot, true);
if (includePid)
return fmt("%1%/%2%-%3%-%4%", tmpRoot, prefix, getpid(), counter++);
else
return fmt("%1%/%2%-%3%", tmpRoot, prefix, counter++);
}
Path createTempDir(const Path & tmpRoot, const Path & prefix,
bool includePid, bool useGlobalCounter, mode_t mode)
{
static std::atomic<unsigned int> globalCounter = 0;
std::atomic<unsigned int> localCounter = 0;
auto & counter(useGlobalCounter ? globalCounter : localCounter);
while (1) { while (1) {
checkInterrupt(); checkInterrupt();
Path tmpDir = tempName(tmpRoot, prefix, includePid, counter); Path tmpDir = makeTempPath(tmpRoot, prefix);
if (mkdir(tmpDir.c_str() if (mkdir(tmpDir.c_str()
#ifndef _WIN32 // TODO abstract mkdir perms for Windows #ifndef _WIN32 // TODO abstract mkdir perms for Windows
, mode , mode

View file

@ -311,7 +311,7 @@ typedef std::unique_ptr<DIR, DIRDeleter> AutoCloseDir;
* Create a temporary directory. * Create a temporary directory.
*/ */
Path createTempDir(const Path & tmpRoot = "", const Path & prefix = "nix", Path createTempDir(const Path & tmpRoot = "", const Path & prefix = "nix",
bool includePid = true, bool useGlobalCounter = true, mode_t mode = 0755); mode_t mode = 0755);
/** /**
* Create a temporary file, returning a file handle and its path. * Create a temporary file, returning a file handle and its path.