From 2029515f40f7289793da95972bd4b81ff25053d2 Mon Sep 17 00:00:00 2001 From: DavHau Date: Tue, 1 Jul 2025 12:23:42 +0700 Subject: [PATCH] patchShebangs: fix crash on read-only files Fixes a regression introduced by c1cc6ff0c0680ae6d8228953ebba93c131e04775 where patchShebangs would crash if a file is read-only --- pkgs/build-support/setup-hooks/patch-shebangs.sh | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkgs/build-support/setup-hooks/patch-shebangs.sh b/pkgs/build-support/setup-hooks/patch-shebangs.sh index 316803dd4dd0..7fb6dc86c87a 100644 --- a/pkgs/build-support/setup-hooks/patch-shebangs.sh +++ b/pkgs/build-support/setup-hooks/patch-shebangs.sh @@ -72,12 +72,25 @@ patchShebangs() { _sponge() { local content local target + local restoreReadOnly content="" target="$1" + + # Make file writable if it is read-only + if [[ ! -w "$target" ]]; then + chmod +w "$target" + restoreReadOnly=true + fi + while IFS= read -r line || [[ -n "$line" ]]; do content+="$line"$'\n' done printf '%s' "$content" > "$target" + + # Restore read-only if it was read-only before + if [[ -n "${restoreReadOnly:-}" ]]; then + chmod -w "$target" + fi } local f