updpkgsums: drop in-place rewrite hack, use a tempfile

This apparently exposes (what I think is) a subtle bug in cygwin's
handling of subst'd drives. Let's just drop the hackery and use a
tempfile, which should always work.

Also, introduce a proper die() function which replaces previous
hand-rolled error+exit pattern, but which wrote to stdout.

Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
Dave Reisner 2014-12-14 13:45:06 -05:00 committed by Allan McRae
parent 16259d728e
commit 768b65e934
1 changed files with 23 additions and 14 deletions

View File

@ -40,6 +40,11 @@ version() {
echo 'Copyright (C) 2012-2013 Dave Reisner <dreisner@archlinux.org>' echo 'Copyright (C) 2012-2013 Dave Reisner <dreisner@archlinux.org>'
} }
die() {
printf "==> ERROR: $1\n" "${@:2}" >&2
exit 1
}
case $1 in case $1 in
-h|--help) usage; exit ;; -h|--help) usage; exit ;;
-V|--version) version; exit ;; -V|--version) version; exit ;;
@ -47,8 +52,7 @@ esac
buildfile=${1:-PKGBUILD} buildfile=${1:-PKGBUILD}
if [[ ! -f $buildfile ]]; then if [[ ! -f $buildfile ]]; then
printf "==> ERROR: %s not found or is not a file\n" "$buildfile" die "%s not found or is not a file" "$buildfile"
exit 1
fi fi
# Resolve any symlinks to avoid replacing the symlink with a file. But, we # Resolve any symlinks to avoid replacing the symlink with a file. But, we
@ -71,18 +75,19 @@ fi
# Check $PWD/ for permission to unlink the $buildfile and write a new one # Check $PWD/ for permission to unlink the $buildfile and write a new one
if [[ ! -w . ]]; then if [[ ! -w . ]]; then
printf "==> ERROR: No write permission in '%s'\n" "$PWD" die "No write permission in '%s'" "$PWD"
exit 1
fi fi
{ # Generate the new sums
# Generate the new sums and try to unlink the file before writing stdin back export BUILDDIR=$(mktemp -d --tmpdir updpkgsums.XXXXXX)
# into it. This final precaution shouldn't fail based on the previous checks, newbuildfile=$(mktemp --tmpdir updpkgsums.XXXXXX)
# but it's better to be extra careful before unlinking files.
export BUILDDIR=$(mktemp -d --tmpdir updpkgsums.XXXXXX) # In case the eventual replacement fails, we don't want to leave behind
trap "rm -rf '$BUILDDIR'" EXIT # $newbuildfile as garbage in $TMPDIR. This fails silently if the replacement
newsums=$(makepkg -g -p "$buildfile") && rm -f "$buildfile" && # succeeds.
awk -v newsums="$newsums" ' trap "rm -rf '$BUILDDIR' '$newbuildfile'" EXIT
newsums=$(makepkg -g -p "$buildfile") &&
awk -v newsums="$newsums" '
/^[[:blank:]]*(md|sha)[[:digit:]]+sums(_[^=]+)?=/,/\)[[:blank:]]*(#.*)?$/ { /^[[:blank:]]*(md|sha)[[:digit:]]+sums(_[^=]+)?=/,/\)[[:blank:]]*(#.*)?$/ {
if (!w) { if (!w) {
print newsums print newsums
@ -93,7 +98,11 @@ fi
1 1
END { if (!w) print newsums } END { if (!w) print newsums }
' > "$buildfile" ' "$buildfile" > "$newbuildfile"
} < "$buildfile"
# Replace the original buildfile.
if ! mv -- "$newbuildfile" "$buildfile"; then
die "Failed to update %s. The file has not been modified." "$buildfile"
fi
# vim: set noet: # vim: set noet: