libstore/profiles: do not dereference current generation if it doesn't exist

If the profile inode is invalid, e.g. invalid symlink, the current
generation cannot be discovered.

Nonetheless, this should not be a reason for an assert failure, instead
of crashing, just raise an error.

Fixes fj#801.

Change-Id: I63937672173bc3bf37196de98307800adc5757e1
Signed-off-by: Raito Bezarius <raito@lix.systems>
Co-authored-by: Qyriad <qyriad@qyriad.me>
This commit is contained in:
Raito Bezarius
2025-05-11 01:07:18 +02:00
co-authored by Qyriad
parent 8a76db3ab7
commit d19a9e3039
3 changed files with 16 additions and 1 deletions
+4 -1
View File
@@ -153,8 +153,11 @@ void deleteGenerations(
auto [gens, curGen] = findGenerations(profile);
if (!curGen)
throw Error("cannot find current generation of profile '%1%', is it a valid symlink?", profile);
if (gensToDelete.count(*curGen))
throw Error("cannot delete current version of profile %1%'", profile);
throw Error("cannot delete current version of profile '%1%'", profile);
for (auto & i : gens) {
if (!gensToDelete.count(i.number)) continue;
@@ -68,6 +68,11 @@ clearProfiles() {
rm -rf "$profiles"
}
linkProfilesTo() {
target="$1"
ln -s "$HOME"/.local/state/nix/profiles "$target"
}
clearStore() {
echo "clearing store..."
chmod -R +w "$NIX_STORE_DIR"
+7
View File
@@ -4,6 +4,13 @@ if [ -z "${storeCleared-}" ]; then
clearStore
fi
# Test if invalid profiles symlinks are supported.
# https://git.lix.systems/lix-project/lix/issues/801
VOID=$(mktemp -d)
rm -rf "$VOID"
linkProfilesTo $VOID
expectStderr 1 nix-env --delete-generations 0 | grepQuiet -E "error: cannot find current generation of profile '.*local/state/nix/profiles/profile', is it a valid symlink?"
clearProfiles
# Query installed: should be empty.