1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-27 08:31:16 +02:00

* Some refactoring: put the GC options / results in separate structs.

* The garbage collector now also prints the number of blocks freed.
This commit is contained in:
Eelco Dolstra 2008-06-18 09:34:17 +00:00
parent 934c58aa38
commit a72709afd8
15 changed files with 252 additions and 166 deletions

View file

@ -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);

View file

@ -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);

View file

@ -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;
}
static void _deletePath(const Path & path, unsigned long long & bytesFreed)
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,
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);
}

View file

@ -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);