diff options
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/serialise.cc | 31 | ||||
-rw-r--r-- | src/libutil/serialise.hh | 2 | ||||
-rw-r--r-- | src/libutil/util.cc | 35 | ||||
-rw-r--r-- | src/libutil/util.hh | 8 |
4 files changed, 61 insertions, 15 deletions
diff --git a/src/libutil/serialise.cc b/src/libutil/serialise.cc index c3fd4dd10cc8..c13e8c7e38e5 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 c18e82463b5a..75d17bc60502 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 e18f9841fd4d..1873ccfe536c 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 d52ab3e4d920..d1e30fa6b9e3 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); |