make createTempDir take an optional for temp dir root

Change-Id: Iae5b2bacce8514de51038fe67940db6c640917ed
This commit is contained in:
Curran McConnell 2024-07-21 14:02:47 -04:00
parent 94a8e5fe0d
commit 76a424fac8
7 changed files with 15 additions and 14 deletions

View file

@ -491,7 +491,7 @@ void LocalDerivationGoal::startBuilder()
/* Create a temporary directory where the build will take /* Create a temporary directory where the build will take
place. */ 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); chownToBuilder(tmpDir);

View file

@ -1494,7 +1494,7 @@ std::pair<Path, AutoCloseFD> LocalStore::createTempDirInStore()
/* There is a slight possibility that `tmpDir' gets deleted by /* There is a slight possibility that `tmpDir' gets deleted by
the GC between createTempDir() and when we acquire a lock on it. the GC between createTempDir() and when we acquire a lock on it.
We'll repeat until 'tmpDir' exists and we've locked 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)}; tmpDirFd = AutoCloseFD{open(tmpDirFn.c_str(), O_RDONLY | O_DIRECTORY)};
if (tmpDirFd.get() < 0) { if (tmpDirFd.get() < 0) {
continue; continue;

View file

@ -20,7 +20,7 @@ SSHMaster::SSHMaster(const std::string & host, const std::string & keyFile, cons
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(std::optional<Path>(), "nix", true, true, 0700));
} }
void SSHMaster::addCommonSSHOpts(Strings & args) void SSHMaster::addCommonSSHOpts(Strings & args)

View file

@ -531,17 +531,17 @@ 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, static Path tempName(const std::optional<Path> & tmpRoot, const Path & prefix, bool includePid,
std::atomic<unsigned int> & counter) std::atomic<unsigned int> & counter)
{ {
tmpRoot = canonPath(tmpRoot.empty() ? defaultTempDir() : tmpRoot, true); auto canonTmpRoot = canonPath(tmpRoot ? *tmpRoot : defaultTempDir(), true);
if (includePid) if (includePid)
return fmt("%1%/%2%-%3%-%4%", tmpRoot, prefix, getpid(), counter++); return fmt("%1%/%2%-%3%-%4%", canonTmpRoot, prefix, getpid(), counter++);
else 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<Path> & tmpRoot, const Path & prefix,
bool includePid, bool useGlobalCounter, mode_t mode) bool includePid, bool useGlobalCounter, mode_t mode)
{ {
static std::atomic<unsigned int> globalCounter = 0; static std::atomic<unsigned int> globalCounter = 0;
@ -680,7 +680,7 @@ void moveFile(const Path & oldName, const Path & newName)
auto newPath = fs::path(newName); auto newPath = fs::path(newName);
// For the move to be as atomic as possible, copy to a temporary // For the move to be as atomic as possible, copy to a temporary
// directory // 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); }; Finally removeTemp = [&]() { fs::remove(temp); };
auto tempCopyTarget = temp / "copy-target"; auto tempCopyTarget = temp / "copy-target";
if (e.code().value() == EXDEV) { if (e.code().value() == EXDEV) {

View file

@ -266,8 +266,9 @@ 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 std::optional<Path> & tmpRoot = std::optional<Path>(),
bool includePid = true, bool useGlobalCounter = true, mode_t mode = 0755); 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. * Create a temporary file, returning a file handle and its path.

View file

@ -53,7 +53,7 @@ static void main_nix_build(int argc, char * * argv)
std::string script; std::string script;
std::vector<std::string> savedArgs; std::vector<std::string> savedArgs;
AutoDelete tmpDir(createTempDir("", myName)); AutoDelete tmpDir(createTempDir(std::optional<Path>(), myName));
std::string outLink = "./result"; std::string outLink = "./result";

View file

@ -557,7 +557,7 @@ struct CmdDevelop : Common, MixEnvironment
auto [rcFileFd, rcFilePath] = createTempFile("nix-shell"); auto [rcFileFd, rcFilePath] = createTempFile("nix-shell");
AutoDelete tmpDir(createTempDir("", "nix-develop"), true); AutoDelete tmpDir(createTempDir(std::optional<Path>(), "nix-develop"), true);
auto script = makeRcScript(store, buildEnvironment, (Path) tmpDir); auto script = makeRcScript(store, buildEnvironment, (Path) tmpDir);
@ -694,7 +694,7 @@ struct CmdPrintDevEnv : Common, MixJSON
if (json) { if (json) {
logger->writeToStdout(buildEnvironment.toJSON()); logger->writeToStdout(buildEnvironment.toJSON());
} else { } else {
AutoDelete tmpDir(createTempDir("", "nix-dev-env"), true); AutoDelete tmpDir(createTempDir(std::optional<Path>(), "nix-dev-env"), true);
logger->writeToStdout(makeRcScript(store, buildEnvironment, tmpDir)); logger->writeToStdout(makeRcScript(store, buildEnvironment, tmpDir));
} }
} }