diff options
author | Robert Hensing <robert@roberthensing.nl> | 2024-03-22 22:41:50 +0100 |
---|---|---|
committer | K900 <me@0upti.me> | 2024-06-24 11:30:32 +0300 |
commit | d86009bd76ae85e56e1d26aab26d38f90ecb6439 (patch) | |
tree | 99a2396cc247c9857bf896910a74b4aa2f51321e /src/libutil | |
parent | 4c3d93611f2848c56ebc69c85f2b1e18001ed3c7 (diff) |
Add build-dir setting, clean up default TMPDIR handling
This is a squash of upstream PRs #10303, #10312 and #10883.
fix: Treat empty TMPDIR as unset
Fixes an instance of
nix: src/libutil/util.cc:139: nix::Path nix::canonPath(PathView, bool): Assertion `path != ""' failed.
... which I've been getting in one of my shells for some reason.
I have yet to find out why TMPDIR was empty, but it's no reason for
Nix to break.
(cherry picked from commit c3fb2aa1f9d1fa756dac38d3588c836c5a5395dc)
fix: Treat empty XDG_RUNTIME_DIR as unset
See preceding commit. Not observed in the wild, but is sensible
and consistent with TMPDIR behavior.
(cherry picked from commit b9e7f5aa2df3f0e223f5c44b8089cbf9b81be691)
local-derivation-goal.cc: Reuse defaultTempDir()
(cherry picked from commit fd31945742710984de22805ee8d97fbd83c3f8eb)
fix: remove usage of XDG_RUNTIME_DIR for TMP
(cherry picked from commit 1363f51bcb24ab9948b7b5093490a009947f7453)
tests/functional: Add count()
(cherry picked from commit 6221770c9de4d28137206bdcd1a67eea12e1e499)
Remove uncalled for message
(cherry picked from commit b1fe388d33530f0157dcf9f461348b61eda13228)
Add build-dir setting
(cherry picked from commit 8b16cced18925aa612049d08d5e78eccbf0530e4)
Change-Id: Ic7b75ff0b6a3b19e50a4ac8ff2d70f15c683c16a
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/file-system.cc | 8 | ||||
-rw-r--r-- | src/libutil/file-system.hh | 5 |
2 files changed, 11 insertions, 2 deletions
diff --git a/src/libutil/file-system.cc b/src/libutil/file-system.cc index f51f3c092..871707468 100644 --- a/src/libutil/file-system.cc +++ b/src/libutil/file-system.cc @@ -511,10 +511,14 @@ void AutoDelete::reset(const Path & p, bool recursive) { ////////////////////////////////////////////////////////////////////// +std::string defaultTempDir() { + return getEnvNonEmpty("TMPDIR").value_or("/tmp"); +} + static Path tempName(Path tmpRoot, const Path & prefix, bool includePid, std::atomic<unsigned int> & counter) { - tmpRoot = canonPath(tmpRoot.empty() ? getEnv("TMPDIR").value_or("/tmp") : tmpRoot, true); + tmpRoot = canonPath(tmpRoot.empty() ? defaultTempDir() : tmpRoot, true); if (includePid) return fmt("%1%/%2%-%3%-%4%", tmpRoot, prefix, getpid(), counter++); else @@ -554,7 +558,7 @@ Path createTempDir(const Path & tmpRoot, const Path & prefix, std::pair<AutoCloseFD, Path> createTempFile(const Path & prefix) { - Path tmpl(getEnv("TMPDIR").value_or("/tmp") + "/" + prefix + ".XXXXXX"); + Path tmpl(defaultTempDir() + "/" + prefix + ".XXXXXX"); // Strictly speaking, this is UB, but who cares... // FIXME: use O_TMPFILE. AutoCloseFD fd(mkstemp((char *) tmpl.c_str())); diff --git a/src/libutil/file-system.hh b/src/libutil/file-system.hh index 64d884227..17f5da062 100644 --- a/src/libutil/file-system.hh +++ b/src/libutil/file-system.hh @@ -259,6 +259,11 @@ Path createTempDir(const Path & tmpRoot = "", const Path & prefix = "nix", std::pair<AutoCloseFD, Path> createTempFile(const Path & prefix = "nix"); /** + * Return `TMPDIR`, or the default temporary directory if unset or empty. + */ +Path defaultTempDir(); + +/** * Used in various places. */ typedef std::function<bool(const Path & path)> PathFilter; |