aboutsummaryrefslogtreecommitdiff
path: root/src/libutil
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2020-03-30 19:14:17 +0200
committerEelco Dolstra <edolstra@gmail.com>2020-03-30 19:16:45 +0200
commite1a94ad852e91fcdcf4efb60fe7e9b9e328df7ac (patch)
tree3bae2236d6bd11e3960394b99278191de021eff8 /src/libutil
parent367577d9a6ec465d773f7b3ba53a5656253f514d (diff)
Backport 'nix dev-shell' from the flakes branch
This also adds a '--profile' option to 'nix build' (replacing 'nix-env --set').
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/util.cc11
-rw-r--r--src/libutil/util.hh12
2 files changed, 19 insertions, 4 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 097ff210a..332c1c43a 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -478,6 +478,17 @@ 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");
+ // Strictly speaking, this is UB, but who cares...
+ AutoCloseFD fd(mkstemp((char *) tmpl.c_str()));
+ if (!fd)
+ throw SysError("creating temporary file '%s'", tmpl);
+ return {std::move(fd), tmpl};
+}
+
+
std::string getUserName()
{
auto pw = getpwuid(geteuid());
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index 7c3a30242..1f85c7c46 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -122,10 +122,6 @@ void deletePath(const Path & path);
void deletePath(const Path & path, unsigned long long & bytesFreed);
-/* Create a temporary directory. */
-Path createTempDir(const Path & tmpRoot = "", const Path & prefix = "nix",
- bool includePid = true, bool useGlobalCounter = true, mode_t mode = 0755);
-
std::string getUserName();
/* Return $HOME or the user's home directory from /etc/passwd. */
@@ -205,6 +201,14 @@ public:
};
+/* Create a temporary directory. */
+Path createTempDir(const Path & tmpRoot = "", 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. */
+std::pair<AutoCloseFD, Path> createTempFile(const Path & prefix = "nix");
+
+
class Pipe
{
public: