| 1 | #!/usr/bin/env bash |
| 2 | # Source after packaging/env.sh, which supplies sha256(). Keep the verified cache usable when |
| 3 | # an official asset was pre-seeded by a tool that created it without an execute bit. |
| 4 | |
| 5 | download_verified() { |
| 6 | local url="$1" dest="$2" expected="$3" executable="${4:-false}" tmp |
| 7 | if [[ -f "$dest" ]] && [[ "$(sha256 "$dest")" = "$expected" ]]; then |
| 8 | if [[ "$executable" = true ]] && [[ ! -x "$dest" ]] && ! chmod 755 "$dest"; then |
| 9 | echo "error: verified asset is not executable and cannot be chmod'd: $dest" >&2 |
| 10 | return 1 |
| 11 | fi |
| 12 | return 0 |
| 13 | fi |
| 14 | tmp="$(mktemp "$dest.XXXXXX")" |
| 15 | if ! curl -fsSL -o "$tmp" "$url"; then rm -f "$tmp"; return 1; fi |
| 16 | if [[ "$(sha256 "$tmp")" != "$expected" ]]; then |
| 17 | echo "error: SHA-256 mismatch for $url" >&2 |
| 18 | rm -f "$tmp" |
| 19 | return 1 |
| 20 | fi |
| 21 | if [[ "$executable" = true ]]; then |
| 22 | if ! chmod 755 "$tmp"; then rm -f "$tmp"; return 1; fi |
| 23 | else |
| 24 | if ! chmod 644 "$tmp"; then rm -f "$tmp"; return 1; fi |
| 25 | fi |
| 26 | if ! mv -f "$tmp" "$dest"; then rm -f "$tmp"; return 1; fi |
| 27 | } |
| 28 | |
| 29 | verify_appimagetool_override() { |
| 30 | local tool="$1" expected="$2" version="$3" arch="$4" |
| 31 | if [[ ! -f "$tool" ]] || [[ "$(sha256 "$tool")" != "$expected" ]]; then |
| 32 | echo "error: APPIMAGETOOL must be the verified $version $arch release asset" >&2 |
| 33 | return 1 |
| 34 | fi |
| 35 | if [[ ! -x "$tool" ]]; then |
| 36 | echo "error: APPIMAGETOOL must be executable (chmod +x $tool)" >&2 |
| 37 | return 1 |
| 38 | fi |
| 39 | } |