From 51855f22ef66177431ee032f9c7ef99d91c812aa Mon Sep 17 00:00:00 2001 From: Curran McConnell Date: Sun, 21 Jul 2024 14:02:47 -0400 Subject: [PATCH] make createTempDir take an optional for temp dir root Change-Id: Iae5b2bacce8514de51038fe67940db6c640917ed --- src/libstore/build/local-derivation-goal.cc | 2 +- src/libstore/local-store.cc | 2 +- src/libstore/ssh.cc | 2 +- src/libutil/file-system.cc | 12 ++++++------ src/libutil/file-system.hh | 5 +++-- src/nix-build/nix-build.cc | 2 +- src/nix/develop.cc | 4 ++-- 7 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/libstore/build/local-derivation-goal.cc b/src/libstore/build/local-derivation-goal.cc index 660512e49..22f8e7f10 100644 --- a/src/libstore/build/local-derivation-goal.cc +++ b/src/libstore/build/local-derivation-goal.cc @@ -491,7 +491,7 @@ void LocalDerivationGoal::startBuilder() /* Create a temporary directory where the build will take place. */ - tmpDir = createTempDir(settings.buildDir.get().value_or(""), "nix-build-" + std::string(drvPath.name()), false, false, 0700); + tmpDir = createTempDir(settings.buildDir.get(), "nix-build-" + std::string(drvPath.name()), false, false, 0700); chownToBuilder(tmpDir); diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc index 10d43ee3e..209a4417b 100644 --- a/src/libstore/local-store.cc +++ b/src/libstore/local-store.cc @@ -1494,7 +1494,7 @@ std::pair LocalStore::createTempDirInStore() /* There is a slight possibility that `tmpDir' gets deleted by the GC between createTempDir() and when we acquire a lock on it. We'll repeat until 'tmpDir' exists and we've locked it. */ - tmpDirFn = createTempDir(realStoreDir, "tmp"); + tmpDirFn = createTempDir(std::optional(realStoreDir), "tmp"); tmpDirFd = AutoCloseFD{open(tmpDirFn.c_str(), O_RDONLY | O_DIRECTORY)}; if (tmpDirFd.get() < 0) { continue; diff --git a/src/libstore/ssh.cc b/src/libstore/ssh.cc index 0d7bfa01d..0d9a4b421 100644 --- a/src/libstore/ssh.cc +++ b/src/libstore/ssh.cc @@ -20,7 +20,7 @@ SSHMaster::SSHMaster(const std::string & host, const std::string & keyFile, cons throw Error("invalid SSH host name '%s'", host); auto state(state_.lock()); - state->tmpDir = std::make_unique(createTempDir("", "nix", true, true, 0700)); + state->tmpDir = std::make_unique(createTempDir(std::optional(), "nix", true, true, 0700)); } void SSHMaster::addCommonSSHOpts(Strings & args) diff --git a/src/libutil/file-system.cc b/src/libutil/file-system.cc index f0199d36f..fe4bf2807 100644 --- a/src/libutil/file-system.cc +++ b/src/libutil/file-system.cc @@ -531,17 +531,17 @@ std::string defaultTempDir() { return getEnvNonEmpty("TMPDIR").value_or("/tmp"); } -static Path tempName(Path tmpRoot, const Path & prefix, bool includePid, +static Path tempName(const std::optional & tmpRoot, const Path & prefix, bool includePid, std::atomic & counter) { - tmpRoot = canonPath(tmpRoot.empty() ? defaultTempDir() : tmpRoot, true); + auto canonTmpRoot = canonPath(tmpRoot ? *tmpRoot : defaultTempDir(), true); if (includePid) - return fmt("%1%/%2%-%3%-%4%", tmpRoot, prefix, getpid(), counter++); + return fmt("%1%/%2%-%3%-%4%", canonTmpRoot, prefix, getpid(), counter++); else - return fmt("%1%/%2%-%3%", tmpRoot, prefix, counter++); + return fmt("%1%/%2%-%3%", canonTmpRoot, prefix, counter++); } -Path createTempDir(const Path & tmpRoot, const Path & prefix, +Path createTempDir(const std::optional & tmpRoot, const Path & prefix, bool includePid, bool useGlobalCounter, mode_t mode) { static std::atomic globalCounter = 0; @@ -680,7 +680,7 @@ void moveFile(const Path & oldName, const Path & newName) auto newPath = fs::path(newName); // For the move to be as atomic as possible, copy to a temporary // directory - fs::path temp = createTempDir(newPath.parent_path(), "rename-tmp"); + fs::path temp = createTempDir(std::optional(newPath.parent_path()), "rename-tmp"); Finally removeTemp = [&]() { fs::remove(temp); }; auto tempCopyTarget = temp / "copy-target"; if (e.code().value() == EXDEV) { diff --git a/src/libutil/file-system.hh b/src/libutil/file-system.hh index e49323e84..eecef0ef5 100644 --- a/src/libutil/file-system.hh +++ b/src/libutil/file-system.hh @@ -266,8 +266,9 @@ typedef std::unique_ptr AutoCloseDir; /** * Create a temporary directory. */ -Path createTempDir(const Path & tmpRoot = "", const Path & prefix = "nix", - bool includePid = true, bool useGlobalCounter = true, mode_t mode = 0755); +Path createTempDir(const std::optional & tmpRoot = std::optional(), + const Path & prefix = "nix", bool includePid = true, bool useGlobalCounter = true, + mode_t mode = 0755); /** * Create a temporary file, returning a file handle and its path. diff --git a/src/nix-build/nix-build.cc b/src/nix-build/nix-build.cc index 4b8d7a2fa..3d35ee47f 100644 --- a/src/nix-build/nix-build.cc +++ b/src/nix-build/nix-build.cc @@ -53,7 +53,7 @@ static void main_nix_build(int argc, char * * argv) std::string script; std::vector savedArgs; - AutoDelete tmpDir(createTempDir("", myName)); + AutoDelete tmpDir(createTempDir(std::optional(), myName)); std::string outLink = "./result"; diff --git a/src/nix/develop.cc b/src/nix/develop.cc index fb144c904..9169f44f8 100644 --- a/src/nix/develop.cc +++ b/src/nix/develop.cc @@ -557,7 +557,7 @@ struct CmdDevelop : Common, MixEnvironment auto [rcFileFd, rcFilePath] = createTempFile("nix-shell"); - AutoDelete tmpDir(createTempDir("", "nix-develop"), true); + AutoDelete tmpDir(createTempDir(std::optional(), "nix-develop"), true); auto script = makeRcScript(store, buildEnvironment, (Path) tmpDir); @@ -694,7 +694,7 @@ struct CmdPrintDevEnv : Common, MixJSON if (json) { logger->writeToStdout(buildEnvironment.toJSON()); } else { - AutoDelete tmpDir(createTempDir("", "nix-dev-env"), true); + AutoDelete tmpDir(createTempDir(std::optional(), "nix-dev-env"), true); logger->writeToStdout(makeRcScript(store, buildEnvironment, tmpDir)); } }