summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Reisner <dreisner@archlinux.org>2014-12-14 13:45:06 -0500
committerAllan McRae <allan@archlinux.org>2014-12-18 16:36:18 +1000
commit768b65e93472914c52cc1159c4932d4fc1438c14 (patch)
tree4050c7257c622ed5b3385a54236d2738307b5e60
parent16259d728e87c10533cd24f88aa7127fff48db2b (diff)
downloadpacman-768b65e93472914c52cc1159c4932d4fc1438c14.tar.xz
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>
-rw-r--r--contrib/updpkgsums.sh.in37
1 files changed, 23 insertions, 14 deletions
diff --git a/contrib/updpkgsums.sh.in b/contrib/updpkgsums.sh.in
index e0524bd5..a40845b6 100644
--- a/contrib/updpkgsums.sh.in
+++ b/contrib/updpkgsums.sh.in
@@ -40,6 +40,11 @@ version() {
echo 'Copyright (C) 2012-2013 Dave Reisner <dreisner@archlinux.org>'
}
+die() {
+ printf "==> ERROR: $1\n" "${@:2}" >&2
+ exit 1
+}
+
case $1 in
-h|--help) usage; exit ;;
-V|--version) version; exit ;;
@@ -47,8 +52,7 @@ esac
buildfile=${1:-PKGBUILD}
if [[ ! -f $buildfile ]]; then
- printf "==> ERROR: %s not found or is not a file\n" "$buildfile"
- exit 1
+ die "%s not found or is not a file" "$buildfile"
fi
# 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
if [[ ! -w . ]]; then
- printf "==> ERROR: No write permission in '%s'\n" "$PWD"
- exit 1
+ die "No write permission in '%s'" "$PWD"
fi
-{
- # Generate the new sums and try to unlink the file before writing stdin back
- # into it. This final precaution shouldn't fail based on the previous checks,
- # but it's better to be extra careful before unlinking files.
- export BUILDDIR=$(mktemp -d --tmpdir updpkgsums.XXXXXX)
- trap "rm -rf '$BUILDDIR'" EXIT
- newsums=$(makepkg -g -p "$buildfile") && rm -f "$buildfile" &&
- awk -v newsums="$newsums" '
+# Generate the new sums
+export BUILDDIR=$(mktemp -d --tmpdir updpkgsums.XXXXXX)
+newbuildfile=$(mktemp --tmpdir updpkgsums.XXXXXX)
+
+# In case the eventual replacement fails, we don't want to leave behind
+# $newbuildfile as garbage in $TMPDIR. This fails silently if the replacement
+# succeeds.
+trap "rm -rf '$BUILDDIR' '$newbuildfile'" EXIT
+newsums=$(makepkg -g -p "$buildfile") &&
+awk -v newsums="$newsums" '
/^[[:blank:]]*(md|sha)[[:digit:]]+sums(_[^=]+)?=/,/\)[[:blank:]]*(#.*)?$/ {
if (!w) {
print newsums
@@ -93,7 +98,11 @@ fi
1
END { if (!w) print newsums }
- ' > "$buildfile"
-} < "$buildfile"
+' "$buildfile" > "$newbuildfile"
+
+# 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: