about summary refs log tree commit diff
path: root/src/libutil
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2005-02-01T22·07+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2005-02-01T22·07+0000
commita37338815de6affd44f927712143f626c8e6d79d (patch)
treee3ea2dbd932702a06110c33a26c94da3e1094e92 /src/libutil
parent2e6bf723e4d63d661d26443a4477a040a96c7257 (diff)
* A GC setting `gc-keep-outputs' to specify whether output paths of
  derivations should be kept.

Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/util.cc21
-rw-r--r--src/libutil/util.hh4
2 files changed, 25 insertions, 0 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 108c054b7f41..2fb3e9ee6853 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -164,6 +164,27 @@ Strings readDirectory(const Path & path)
 }
 
 
+string readFile(int fd)
+{
+    struct stat st;
+    if (fstat(fd, &st) == -1)
+        throw SysError("statting file");
+    unsigned char buf[st.st_size]; /* !!! stack space */
+    readFull(fd, buf, st.st_size);
+
+    return string((char *) buf, st.st_size);
+}
+
+
+string readFile(const Path & path)
+{
+    AutoCloseFD fd = open(path.c_str(), O_RDONLY);
+    if (fd == -1)
+        throw SysError(format("opening file `%1%'") % path);
+    return readFile(fd);
+}
+
+
 static void _deletePath(const Path & path)
 {
     checkInterrupt();
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index d9d5a7cdfc53..c7f117129f17 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -90,6 +90,10 @@ bool isLink(const Path & path);
    removed. */
 Strings readDirectory(const Path & path);
 
+/* Read the contents of a file into a string. */
+string readFile(int fd);
+string readFile(const Path & path);
+
 /* Delete a path; i.e., in the case of a directory, it is deleted
    recursively.  Don't use this at home, kids. */
 void deletePath(const Path & path);