forked from lix-project/lix
* `nix-store --gc' prints out the number of bytes freed on stdout
(even when it is interrupted by a signal).
This commit is contained in:
parent
5144f750c4
commit
530b27df1e
|
@ -303,9 +303,11 @@ static Paths topoSort(const PathSet & paths)
|
|||
}
|
||||
|
||||
|
||||
void collectGarbage(GCAction action, PathSet & result)
|
||||
void collectGarbage(GCAction action, PathSet & result,
|
||||
unsigned long long & bytesFreed)
|
||||
{
|
||||
result.clear();
|
||||
bytesFreed = 0;
|
||||
|
||||
bool gcKeepOutputs =
|
||||
queryBoolSetting("gc-keep-outputs", false);
|
||||
|
@ -452,7 +454,9 @@ void collectGarbage(GCAction action, PathSet & result)
|
|||
printMsg(lvlInfo, format("deleting `%1%'") % *i);
|
||||
|
||||
/* Okay, it's safe to delete. */
|
||||
deleteFromStore(*i);
|
||||
unsigned long long freed;
|
||||
deleteFromStore(*i, freed);
|
||||
bytesFreed += freed;
|
||||
|
||||
if (fdLock != -1)
|
||||
/* Write token to stale (deleted) lock file. */
|
||||
|
|
|
@ -19,7 +19,8 @@ typedef enum {
|
|||
closure of) the roots. If `action' is `gcReturnDead', return the
|
||||
set of paths not reachable from the roots. If `action' is
|
||||
`gcDeleteDead', actually delete the latter set. */
|
||||
void collectGarbage(GCAction action, PathSet & result);
|
||||
void collectGarbage(GCAction action, PathSet & result,
|
||||
unsigned long long & bytesFreed);
|
||||
|
||||
/* Register a temporary GC root. This root will automatically
|
||||
disappear when this process exits. WARNING: this function should
|
||||
|
|
|
@ -746,8 +746,9 @@ Path addTextToStore(const string & suffix, const string & s,
|
|||
}
|
||||
|
||||
|
||||
void deleteFromStore(const Path & _path)
|
||||
void deleteFromStore(const Path & _path, unsigned long long & bytesFreed)
|
||||
{
|
||||
bytesFreed = 0;
|
||||
Path path(canonPath(_path));
|
||||
|
||||
assertStorePath(path);
|
||||
|
@ -763,7 +764,7 @@ void deleteFromStore(const Path & _path)
|
|||
}
|
||||
txn.commit();
|
||||
|
||||
deletePath(path);
|
||||
deletePath(path, bytesFreed);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -155,7 +155,7 @@ Path addTextToStore(const string & suffix, const string & s,
|
|||
const PathSet & references);
|
||||
|
||||
/* Delete a value from the nixStore directory. */
|
||||
void deleteFromStore(const Path & path);
|
||||
void deleteFromStore(const Path & path, unsigned long long & bytesFreed);
|
||||
|
||||
void verifyStore(bool checkContents);
|
||||
|
||||
|
|
|
@ -194,7 +194,7 @@ void writeFile(const Path & path, const string & s)
|
|||
}
|
||||
|
||||
|
||||
static void _deletePath(const Path & path)
|
||||
static void _deletePath(const Path & path, unsigned long long & bytesFreed)
|
||||
{
|
||||
checkInterrupt();
|
||||
|
||||
|
@ -204,6 +204,8 @@ static void _deletePath(const Path & path)
|
|||
if (lstat(path.c_str(), &st))
|
||||
throw SysError(format("getting attributes of path `%1%'") % path);
|
||||
|
||||
bytesFreed += st.st_size;
|
||||
|
||||
if (S_ISDIR(st.st_mode)) {
|
||||
Strings names = readDirectory(path);
|
||||
|
||||
|
@ -214,7 +216,7 @@ static void _deletePath(const Path & path)
|
|||
}
|
||||
|
||||
for (Strings::iterator i = names.begin(); i != names.end(); ++i)
|
||||
_deletePath(path + "/" + *i);
|
||||
_deletePath(path + "/" + *i, bytesFreed);
|
||||
}
|
||||
|
||||
if (remove(path.c_str()) == -1)
|
||||
|
@ -223,10 +225,18 @@ static void _deletePath(const Path & path)
|
|||
|
||||
|
||||
void deletePath(const Path & path)
|
||||
{
|
||||
unsigned long long dummy;
|
||||
deletePath(path, dummy);
|
||||
}
|
||||
|
||||
|
||||
void deletePath(const Path & path, unsigned long long & bytesFreed)
|
||||
{
|
||||
startNest(nest, lvlDebug,
|
||||
format("recursively deleting path `%1%'") % path);
|
||||
_deletePath(path);
|
||||
bytesFreed = 0;
|
||||
_deletePath(path, bytesFreed);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -98,9 +98,12 @@ string readFile(const Path & path);
|
|||
void writeFile(const Path & path, const string & s);
|
||||
|
||||
/* Delete a path; i.e., in the case of a directory, it is deleted
|
||||
recursively. Don't use this at home, kids. */
|
||||
recursively. Don't use this at home, kids. The second variant
|
||||
returns the number of bytes freed. */
|
||||
void deletePath(const Path & path);
|
||||
|
||||
void deletePath(const Path & path, unsigned long long & bytesFreed);
|
||||
|
||||
/* Make a path read-only recursively. */
|
||||
void makePathReadOnly(const Path & path);
|
||||
|
||||
|
|
|
@ -489,6 +489,20 @@ static void opCheckValidity(Strings opFlags, Strings opArgs)
|
|||
}
|
||||
|
||||
|
||||
struct PrintFreed
|
||||
{
|
||||
bool show;
|
||||
unsigned long long bytesFreed;
|
||||
PrintFreed(bool _show) : bytesFreed(0), show(_show) { }
|
||||
~PrintFreed()
|
||||
{
|
||||
if (show)
|
||||
cout << format("%d bytes freed (%.2f MiB)\n")
|
||||
% bytesFreed % (bytesFreed / (1024.0 * 1024.0));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
static void opGC(Strings opFlags, Strings opArgs)
|
||||
{
|
||||
GCAction action = gcDeleteDead;
|
||||
|
@ -503,7 +517,8 @@ static void opGC(Strings opFlags, Strings opArgs)
|
|||
else throw UsageError(format("bad sub-operation `%1%' in GC") % *i);
|
||||
|
||||
PathSet result;
|
||||
collectGarbage(action, result);
|
||||
PrintFreed freed(action == gcDeleteDead);
|
||||
collectGarbage(action, result, freed.bytesFreed);
|
||||
|
||||
if (action != gcDeleteDead) {
|
||||
for (PathSet::iterator i = result.begin(); i != result.end(); ++i)
|
||||
|
|
Loading…
Reference in a new issue