about summary refs log tree commit diff
path: root/src/libutil
diff options
context:
space:
mode:
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/util.cc11
-rw-r--r--src/libutil/util.hh5
2 files changed, 16 insertions, 0 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 6a032a3f10f3..60b86b162c9c 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -6,6 +6,7 @@
 #include <sys/stat.h>
 #include <unistd.h>
 #include <dirent.h>
+#include <fcntl.h>
 
 #include "util.hh"
 
@@ -192,6 +193,16 @@ Path createTempDir()
 }
 
 
+void writeStringToFile(const Path & path, const string & s)
+{
+    AutoCloseFD fd = open(path.c_str(),
+        O_CREAT | O_EXCL | O_WRONLY, 0666);
+    if (fd == -1)
+        throw SysError(format("creating file `%1%'") % path);
+    writeFull(fd, (unsigned char *) s.c_str(), s.size());
+}
+
+
 Verbosity verbosity = lvlError;
 
 static int nestingLevel = 0;
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index d0e7b3ada873..cca93cdc737a 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -87,6 +87,11 @@ void makePathReadOnly(const Path & path);
 /* Create a temporary directory. */
 Path createTempDir();
 
+/* Create a file and write the given text to it.  The file is written
+   in binary mode (i.e., no end-of-line conversions).  The path should
+   not already exist. */
+void writeStringToFile(const Path & path, const string & s);
+
 
 /* Messages. */