1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-12-02 15:11:00 +01:00

Merge pull request #130 from DeterminateSystems/improve-nix-store-delete-errors

nix store delete: Show why deletion fails
This commit is contained in:
Eelco Dolstra 2025-06-30 15:04:19 +00:00 committed by GitHub
commit e809a5626e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 62 additions and 36 deletions

View file

@ -730,6 +730,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
options.action = (GCOptions::GCAction) readInt(conn.from);
options.pathsToDelete = WorkerProto::Serialise<StorePathSet>::read(*store, rconn);
conn.from >> options.ignoreLiveness >> options.maxFreed;
options.censor = !trusted;
// obsolete fields
readInt(conn.from);
readInt(conn.from);

View file

@ -208,7 +208,7 @@ void LocalStore::findTempRoots(Roots & tempRoots, bool censor)
while ((end = contents.find((char) 0, pos)) != std::string::npos) {
Path root(contents, pos, end - pos);
debug("got temporary root '%s'", root);
tempRoots[parseStorePath(root)].emplace(censor ? censored : fmt("{temp:%d}", pid));
tempRoots[parseStorePath(root)].emplace(censor ? censored : fmt("{nix-process:%d}", pid));
pos = end + 1;
}
}
@ -458,13 +458,14 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
bool gcKeepOutputs = settings.gcKeepOutputs;
bool gcKeepDerivations = settings.gcKeepDerivations;
std::unordered_set<StorePath> roots, dead, alive;
Roots roots;
std::unordered_set<StorePath> dead, alive;
struct Shared
{
// The temp roots only store the hash part to make it easier to
// ignore suffixes like '.lock', '.chroot' and '.check'.
std::unordered_set<std::string> tempRoots;
std::unordered_map<std::string, GcRootInfo> tempRoots;
// Hash part of the store path currently being deleted, if
// any.
@ -573,7 +574,8 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
debug("got new GC root '%s'", path);
auto hashPart = std::string(storePath->hashPart());
auto shared(_shared.lock());
shared->tempRoots.insert(hashPart);
// FIXME: could get the PID from the socket.
shared->tempRoots.insert_or_assign(hashPart, "{nix-process:unknown}");
/* If this path is currently being
deleted, then we have to wait until
deletion is finished to ensure that
@ -612,19 +614,18 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
/* Find the roots. Since we've grabbed the GC lock, the set of
permanent roots cannot increase now. */
printInfo("finding garbage collector roots...");
Roots rootMap;
if (!options.ignoreLiveness)
findRootsNoTemp(rootMap, true);
for (auto & i : rootMap) roots.insert(i.first);
findRootsNoTemp(roots, options.censor);
/* Read the temporary roots created before we acquired the global
GC root. Any new roots will be sent to our socket. */
Roots tempRoots;
findTempRoots(tempRoots, true);
for (auto & root : tempRoots) {
_shared.lock()->tempRoots.insert(std::string(root.first.hashPart()));
roots.insert(root.first);
{
Roots tempRoots;
findTempRoots(tempRoots, options.censor);
for (auto & root : tempRoots)
_shared.lock()->tempRoots.insert_or_assign(
std::string(root.first.hashPart()),
*root.second.begin());
}
/* Synchronisation point for testing, see tests/functional/gc-non-blocking.sh. */
@ -716,21 +717,35 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
} catch (InvalidPath &) { }
};
if (options.action == GCOptions::gcDeleteSpecific
&& !options.pathsToDelete.count(*path))
{
throw Error(
"Cannot delete path '%s' because it's referenced by path '%s'.",
printStorePath(start),
printStorePath(*path));
}
/* If this is a root, bail out. */
if (roots.count(*path)) {
if (auto i = roots.find(*path); i != roots.end()) {
if (options.action == GCOptions::gcDeleteSpecific)
throw Error(
"Cannot delete path '%s' because it's referenced by the GC root '%s'.",
printStorePath(start),
*i->second.begin());
debug("cannot delete '%s' because it's a root", printStorePath(*path));
return markAlive();
}
if (options.action == GCOptions::gcDeleteSpecific
&& !options.pathsToDelete.count(*path))
return;
{
auto hashPart = std::string(path->hashPart());
auto shared(_shared.lock());
if (shared->tempRoots.count(hashPart)) {
debug("cannot delete '%s' because it's a temporary root", printStorePath(*path));
if (auto i = shared->tempRoots.find(hashPart); i != shared->tempRoots.end()) {
if (options.action == GCOptions::gcDeleteSpecific)
throw Error(
"Cannot delete path '%s' because it's in use by '%s'.",
printStorePath(start),
i->second);
return markAlive();
}
shared->pending = hashPart;
@ -789,12 +804,7 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
for (auto & i : options.pathsToDelete) {
deleteReferrersClosure(i);
if (!dead.count(i))
throw Error(
"Cannot delete path '%1%' since it is still alive. "
"To find out why, use: "
"nix-store --query --roots and nix-store --query --referrers",
printStorePath(i));
assert(dead.count(i));
}
} else if (options.maxFreed > 0) {

View file

@ -7,8 +7,11 @@
namespace nix {
// FIXME: should turn this into an std::variant to represent the
// several root types.
using GcRootInfo = std::string;
typedef std::unordered_map<StorePath, std::unordered_set<std::string>> Roots;
typedef std::unordered_map<StorePath, std::unordered_set<GcRootInfo>> Roots;
struct GCOptions
@ -53,6 +56,12 @@ struct GCOptions
* Stop after at least `maxFreed` bytes have been freed.
*/
uint64_t maxFreed{std::numeric_limits<uint64_t>::max()};
/**
* Whether to hide potentially sensitive information about GC
* roots (such as PIDs).
*/
bool censor = false;
};