patchShebangs: fix crash on read-only files

Fixes a regression introduced by c1cc6ff0c0680ae6d8228953ebba93c131e04775 where patchShebangs would crash if a file is read-only
This commit is contained in:
DavHau 2025-07-01 12:23:42 +07:00
parent f2f55c5267
commit 2029515f40

View File

@ -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