Move deleteFromStore()

This commit is contained in:
Eelco Dolstra 2021-10-14 13:34:48 +02:00
parent 0be8cc1466
commit 0317ffdad3
2 changed files with 25 additions and 26 deletions

View file

@ -479,26 +479,6 @@ struct LocalStore::GCState
}; };
void LocalStore::deleteFromStore(GCState & state, std::string_view baseName)
{
Path path = storeDir + "/" + std::string(baseName);
Path realPath = realStoreDir + "/" + std::string(baseName);
printInfo("deleting '%1%'", path);
state.results.paths.insert(path);
uint64_t bytesFreed;
deletePath(realPath, bytesFreed);
state.results.bytesFreed += bytesFreed;
if (state.results.bytesFreed > state.options.maxFreed) {
printInfo("deleted more than %d bytes; stopping", state.options.maxFreed);
throw GCLimitReached();
}
}
/* Unlink all files in /nix/store/.links that have a link count of 1, /* Unlink all files in /nix/store/.links that have a link count of 1,
which indicates that there are no other links and so they can be which indicates that there are no other links and so they can be
safely deleted. FIXME: race condition with optimisePath(): we safely deleted. FIXME: race condition with optimisePath(): we
@ -677,10 +657,31 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
state.roots.insert(root.first); state.roots.insert(root.first);
} }
/* Helper function that deletes a path from the store and throws
GCLimitReached if we've deleted enough garbage. */
auto deleteFromStore = [&](std::string_view baseName)
{
Path path = storeDir + "/" + std::string(baseName);
Path realPath = realStoreDir + "/" + std::string(baseName);
printInfo("deleting '%1%'", path);
state.results.paths.insert(path);
uint64_t bytesFreed;
deletePath(realPath, bytesFreed);
state.results.bytesFreed += bytesFreed;
if (state.results.bytesFreed > state.options.maxFreed) {
printInfo("deleted more than %d bytes; stopping", state.options.maxFreed);
throw GCLimitReached();
}
};
/* Helper function that visits all paths reachable from `start` /* Helper function that visits all paths reachable from `start`
via the referrers edges and optionally derivers and derivation via the referrers edges and optionally derivers and derivation
output edges. If any of those paths is a root, then we cannot output edges. If none of those paths are roots, then all
delete this path. */ visited paths are garbage and are deleted. */
auto deleteReferrersClosure = [&](const StorePath & start) { auto deleteReferrersClosure = [&](const StorePath & start) {
StorePathSet visited; StorePathSet visited;
std::queue<StorePath> todo; std::queue<StorePath> todo;
@ -769,7 +770,7 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
if (!state.dead.insert(path).second) continue; if (!state.dead.insert(path).second) continue;
if (state.shouldDelete) { if (state.shouldDelete) {
invalidatePathChecked(path); invalidatePathChecked(path);
deleteFromStore(state, path.to_string()); deleteFromStore(path.to_string());
} }
} }
}; };
@ -813,7 +814,7 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
if (auto storePath = maybeParseStorePath(storeDir + "/" + name)) if (auto storePath = maybeParseStorePath(storeDir + "/" + name))
deleteReferrersClosure(*storePath); deleteReferrersClosure(*storePath);
else else
deleteFromStore(state, name); deleteFromStore(name);
} }
} catch (GCLimitReached & e) { } catch (GCLimitReached & e) {

View file

@ -240,8 +240,6 @@ private:
struct GCState; struct GCState;
void deleteFromStore(GCState & state, std::string_view baseName);
void findRoots(const Path & path, unsigned char type, Roots & roots); void findRoots(const Path & path, unsigned char type, Roots & roots);
void findRootsNoTemp(Roots & roots, bool censor); void findRootsNoTemp(Roots & roots, bool censor);