From 3703e38135d9920a0b60b56cc1ee8288a9058ed9 Mon Sep 17 00:00:00 2001 From: Rhys-T <108157737+Rhys-T@users.noreply.github.com> Date: Sun, 9 Feb 2025 12:44:31 -0500 Subject: [PATCH] noBrokenSymlinks: check for unreadable symlinks Resolves #380681. --- .../setup-hooks/no-broken-symlinks.sh | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/pkgs/build-support/setup-hooks/no-broken-symlinks.sh b/pkgs/build-support/setup-hooks/no-broken-symlinks.sh index e2694c2b7bde..c51e4ae22ce2 100644 --- a/pkgs/build-support/setup-hooks/no-broken-symlinks.sh +++ b/pkgs/build-support/setup-hooks/no-broken-symlinks.sh @@ -13,7 +13,8 @@ postFixupHooks+=(noBrokenSymlinksInAllOutputs) # A symlink is "dangling" if it points to a non-existent target. # A symlink is "reflexive" if it points to itself. -# A symlink is considered "broken" if it is either dangling or reflexive. +# A symlink is "unreadable" if the readlink command fails, e.g. because of permission errors. +# A symlink is considered "broken" if it is either dangling, reflexive or unreadable. noBrokenSymlinks() { local -r output="${1:?}" local path @@ -21,6 +22,7 @@ noBrokenSymlinks() { local symlinkTarget local -i numDanglingSymlinks=0 local -i numReflexiveSymlinks=0 + local -i numUnreadableSymlinks=0 # NOTE(@connorbaker): This hook doesn't check for cycles in symlinks. @@ -33,7 +35,11 @@ noBrokenSymlinks() { # NOTE: path is absolute because we're running `find` against an absolute path (`output`). while IFS= read -r -d $'\0' path; do pathParent="$(dirname "$path")" - symlinkTarget="$(readlink "$path")" + if ! symlinkTarget="$(readlink "$path")"; then + nixErrorLog "the symlink $path is unreadable" + numUnreadableSymlinks+=1 + continue + fi # Canonicalize symlinkTarget to an absolute path. if [[ $symlinkTarget == /* ]]; then @@ -61,8 +67,8 @@ noBrokenSymlinks() { fi done < <(find "$output" -type l -print0) - if ((numDanglingSymlinks > 0 || numReflexiveSymlinks > 0)); then - nixErrorLog "found $numDanglingSymlinks dangling symlinks and $numReflexiveSymlinks reflexive symlinks" + if ((numDanglingSymlinks > 0 || numReflexiveSymlinks > 0 || numUnreadableSymlinks > 0)); then + nixErrorLog "found $numDanglingSymlinks dangling symlinks, $numReflexiveSymlinks reflexive symlinks and $numUnreadableSymlinks unreadable symlinks" exit 1 fi return 0