summary refs log tree commit diff
path: root/src/libutil
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2006-11-24 20:24:14 +0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2006-11-24 20:24:14 +0000
commitc6a97e3b74289fdc8e57189212a0db3d0e6896e0 (patch)
tree45e9076f8a22a433725569148d24f7196991f76e /src/libutil
parenta76efaeb3f2c1d7de6d41bd0e883b92e2d0f3d7f (diff)
downloadguix-c6a97e3b74289fdc8e57189212a0db3d0e6896e0.tar.gz
* Doh! Path sizes need to be computed recursively of course.
  (NIX-70)

Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/util.cc23
-rw-r--r--src/libutil/util.hh3
2 files changed, 26 insertions, 0 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 38e32bfa9b..f80fb7dbf7 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -238,6 +238,29 @@ void writeFile(const Path & path, const string & s)
 }
 
 
+unsigned long long computePathSize(const Path & path)
+{
+    unsigned long long size = 0;
+    
+    checkInterrupt();
+
+    struct stat st;
+    if (lstat(path.c_str(), &st))
+	throw SysError(format("getting attributes of path `%1%'") % path);
+
+    size += st.st_size;
+
+    if (S_ISDIR(st.st_mode)) {
+	Strings names = readDirectory(path);
+
+	for (Strings::iterator i = names.begin(); i != names.end(); ++i)
+            size += computePathSize(path + "/" + *i);
+    }
+
+    return size;
+}
+
+
 static void _deletePath(const Path & path, unsigned long long & bytesFreed)
 {
     checkInterrupt();
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index 52ef2c6eb2..7f3d41e76e 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -56,6 +56,9 @@ string readFile(const Path & path);
 /* Write a string to a file. */
 void writeFile(const Path & path, const string & s);
 
+/* Compute the sum of the sizes of all files in `path'. */
+unsigned long long computePathSize(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.  The second variant
    returns the number of bytes freed. */