From c6a97e3b74289fdc8e57189212a0db3d0e6896e0 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 24 Nov 2006 20:24:14 +0000 Subject: [PATCH] * Doh! Path sizes need to be computed recursively of course. (NIX-70) --- src/libstore/gc.cc | 8 ++------ src/libutil/util.cc | 23 +++++++++++++++++++++++ src/libutil/util.hh | 3 +++ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/libstore/gc.cc b/src/libstore/gc.cc index ed1b1a84a..3a626deda 100644 --- a/src/libstore/gc.cc +++ b/src/libstore/gc.cc @@ -528,12 +528,8 @@ void collectGarbage(GCAction action, const PathSet & pathsToDelete, /* If just returning the set of dead paths, we also return the space that would be freed if we deleted them. */ - if (action == gcReturnDead) { - struct stat st; - if (lstat(i->c_str(), &st) == -1) - st.st_size = 0; - bytesFreed += st.st_size; - } + if (action == gcReturnDead) + bytesFreed += computePathSize(*i); if (action == gcDeleteDead || action == gcDeleteSpecific) { diff --git a/src/libutil/util.cc b/src/libutil/util.cc index 38e32bfa9..f80fb7dbf 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 52ef2c6eb..7f3d41e76 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. */