about summary refs log tree commit diff
path: root/src/util.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.cc')
-rw-r--r--src/util.cc20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/util.cc b/src/util.cc
index bedd031def3d..ed7562a29d73 100644
--- a/src/util.cc
+++ b/src/util.cc
@@ -171,6 +171,26 @@ void makePathReadOnly(const string & path)
 }
 
 
+static string tempName()
+{
+    static int counter = 0;
+    char * s = getenv("TMPDIR");
+    string tmpRoot = s ? canonPath(string(s)) : "/tmp";
+    return (format("%1%/nix-%2%-%3%") % tmpRoot % getpid() % counter++).str();
+}
+
+
+string createTempDir()
+{
+    while (1) {
+	string tmpDir = tempName();
+	if (mkdir(tmpDir.c_str(), 0777) == 0) return tmpDir;
+	if (errno != EEXIST)
+	    throw SysError(format("creating directory `%1%'") % tmpDir);
+    }
+}
+
+
 Verbosity verbosity = lvlError;
 
 static int nestingLevel = 0;