about summary refs log tree commit diff
path: root/src/libutil
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2008-06-18T09·34+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2008-06-18T09·34+0000
commita72709afd8ffe35613a6bacd698a36395e095a48 (patch)
tree2f549bafbed92a53bd6faea1da43667307af7593 /src/libutil
parent934c58aa381f5eacc86304ba7f5c6775ff456cd5 (diff)
* Some refactoring: put the GC options / results in separate structs.
* The garbage collector now also prints the number of blocks freed.

Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/serialise.cc31
-rw-r--r--src/libutil/serialise.hh2
-rw-r--r--src/libutil/util.cc35
-rw-r--r--src/libutil/util.hh8
4 files changed, 61 insertions, 15 deletions
diff --git a/src/libutil/serialise.cc b/src/libutil/serialise.cc
index c3fd4dd10c..c13e8c7e38 100644
--- a/src/libutil/serialise.cc
+++ b/src/libutil/serialise.cc
@@ -41,6 +41,21 @@ void writeInt(unsigned int n, Sink & sink)
 }
 
 
+void writeLongLong(unsigned long long n, Sink & sink)
+{
+    unsigned char buf[8];
+    buf[0] = n & 0xff;
+    buf[1] = (n >> 8) & 0xff;
+    buf[2] = (n >> 16) & 0xff;
+    buf[3] = (n >> 24) & 0xff;
+    buf[4] = (n >> 32) & 0xff;
+    buf[5] = (n >> 40) & 0xff;
+    buf[6] = (n >> 48) & 0xff;
+    buf[7] = (n >> 56) & 0xff;
+    sink(buf, sizeof(buf));
+}
+
+
 void writeString(const string & s, Sink & sink)
 {
     unsigned int len = s.length();
@@ -84,6 +99,22 @@ unsigned int readInt(Source & source)
 }
 
 
+unsigned long long readLongLong(Source & source)
+{
+    unsigned char buf[8];
+    source(buf, sizeof(buf));
+    return
+        ((unsigned long long) buf[0]) |
+        ((unsigned long long) buf[1] << 8) |
+        ((unsigned long long) buf[2] << 16) |
+        ((unsigned long long) buf[3] << 24) |
+        ((unsigned long long) buf[4] << 32) |
+        ((unsigned long long) buf[5] << 40) |
+        ((unsigned long long) buf[6] << 48) |
+        ((unsigned long long) buf[7] << 56);
+}
+
+
 string readString(Source & source)
 {
     unsigned int len = readInt(source);
diff --git a/src/libutil/serialise.hh b/src/libutil/serialise.hh
index c18e82463b..75d17bc605 100644
--- a/src/libutil/serialise.hh
+++ b/src/libutil/serialise.hh
@@ -95,11 +95,13 @@ struct StringSource : Source
 
 void writePadding(unsigned int len, Sink & sink);
 void writeInt(unsigned int n, Sink & sink);
+void writeLongLong(unsigned long long n, Sink & sink);
 void writeString(const string & s, Sink & sink);
 void writeStringSet(const StringSet & ss, Sink & sink);
 
 void readPadding(unsigned int len, Source & source);
 unsigned int readInt(Source & source);
+unsigned long long readLongLong(Source & source);
 string readString(Source & source);
 StringSet readStringSet(Source & source);
 
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index e18f9841fd..1873ccfe53 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -229,30 +229,38 @@ void writeFile(const Path & path, const string & s)
 }
 
 
-unsigned long long computePathSize(const Path & path)
+static void _computePathSize(const Path & path,
+    unsigned long long & bytes, unsigned long long & blocks)
 {
-    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;
+    bytes += st.st_size;
+    blocks += st.st_blocks;
 
     if (S_ISDIR(st.st_mode)) {
 	Strings names = readDirectory(path);
 
 	for (Strings::iterator i = names.begin(); i != names.end(); ++i)
-            size += computePathSize(path + "/" + *i);
+            _computePathSize(path + "/" + *i, bytes, blocks);
     }
+}
 
-    return size;
+
+void computePathSize(const Path & path,
+    unsigned long long & bytes, unsigned long long & blocks)
+{
+    bytes = 0;
+    blocks = 0;
+    _computePathSize(path, bytes, blocks);
 }
 
 
-static void _deletePath(const Path & path, unsigned long long & bytesFreed)
+static void _deletePath(const Path & path, unsigned long long & bytesFreed,
+    unsigned long long & blocksFreed)
 {
     checkInterrupt();
 
@@ -263,6 +271,7 @@ static void _deletePath(const Path & path, unsigned long long & bytesFreed)
 	throw SysError(format("getting attributes of path `%1%'") % path);
 
     bytesFreed += st.st_size;
+    blocksFreed += st.st_blocks;
 
     if (S_ISDIR(st.st_mode)) {
 	Strings names = readDirectory(path);
@@ -274,7 +283,7 @@ static void _deletePath(const Path & path, unsigned long long & bytesFreed)
 	}
 
 	for (Strings::iterator i = names.begin(); i != names.end(); ++i)
-            _deletePath(path + "/" + *i, bytesFreed);
+            _deletePath(path + "/" + *i, bytesFreed, blocksFreed);
     }
 
     if (remove(path.c_str()) == -1)
@@ -284,17 +293,19 @@ static void _deletePath(const Path & path, unsigned long long & bytesFreed)
 
 void deletePath(const Path & path)
 {
-    unsigned long long dummy;
-    deletePath(path, dummy);
+    unsigned long long dummy1, dummy2;
+    deletePath(path, dummy1, dummy2);
 }
 
 
-void deletePath(const Path & path, unsigned long long & bytesFreed)
+void deletePath(const Path & path, unsigned long long & bytesFreed,
+    unsigned long long & blocksFreed)
 {
     startNest(nest, lvlDebug,
         format("recursively deleting path `%1%'") % path);
     bytesFreed = 0;
-    _deletePath(path, bytesFreed);
+    blocksFreed = 0;
+    _deletePath(path, bytesFreed, blocksFreed);
 }
 
 
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index d52ab3e4d9..d1e30fa6b9 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -61,14 +61,16 @@ string readFile(const Path & path);
 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);
+void computePathSize(const Path & path,
+    unsigned long long & bytes, unsigned long long & blocks);
 
 /* 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. */
+   returns the number of bytes and blocks freed. */
 void deletePath(const Path & path);
 
-void deletePath(const Path & path, unsigned long long & bytesFreed);
+void deletePath(const Path & path, unsigned long long & bytesFreed,
+    unsigned long long & blocksFreed);
 
 /* Make a path read-only recursively. */
 void makePathReadOnly(const Path & path);