|
1 |
| -#!/usr/bin/env bash |
| 1 | +#!/bin/sh |
2 | 2 |
|
3 | 3 | #############################################################################
|
4 | 4 | ##
|
5 |
| -## Copyright (C) 2017 The Qt Company Ltd. |
| 5 | +## Copyright (C) 2019 The Qt Company Ltd. |
6 | 6 | ## Contact: http://www.qt.io/licensing/
|
7 | 7 | ##
|
8 | 8 | ## This file is part of the provisioning scripts of the Qt Toolkit.
|
|
33 | 33 | ##
|
34 | 34 | #############################################################################
|
35 | 35 |
|
| 36 | + |
36 | 37 | # A helper script used for downloading a file from a URL or an alternative
|
37 |
| -# URL. Also the SHA1 is checked for the file. Target filename should also |
38 |
| -# be given. |
39 |
| -# |
40 |
| -# If called directly from another script, it will exit the parent script |
41 |
| -# as well, if not called in its own subshell with parentheses. |
| 38 | +# URL. Also the SHA is checked for the file (SHA algorithm is autodetected |
| 39 | +# based on the SHA length). Target filename should also be given. |
| 40 | + |
| 41 | +############################ BOILERPLATE ############################### |
| 42 | +command -v sha1sum >/dev/null || alias sha1sum='shasum -a 1' |
| 43 | +command -v sha256sum >/dev/null || alias sha256sum='shasum -a 256' |
| 44 | +command -v sha384sum >/dev/null || alias sha384sum='shasum -a 384' |
| 45 | +command -v sha512sum >/dev/null || alias sha512sum='shasum -a 512' |
| 46 | +######################################################################## |
| 47 | + |
| 48 | + |
| 49 | +VerifyHash () { |
| 50 | + file=$1 |
| 51 | + expectedHash=$2 |
| 52 | + |
| 53 | + if [ ! -f "$file" ] |
| 54 | + then return 2 # file does not exist |
| 55 | + fi |
42 | 56 |
|
43 |
| -set -ex |
44 | 57 |
|
45 |
| -function DownloadURL { |
| 58 | + hashLength="$(echo "$expectedHash" | wc -c | sed 's/ *//g')" |
| 59 | + # Use backticks because of bug with bash-3 (default on macOS), |
| 60 | + # caused when there are unbalanced parentheses inside $() |
| 61 | + # shellcheck disable=SC2006 |
| 62 | + hash=`case "$hashLength" in |
| 63 | + 41) sha1sum "$file" ;; |
| 64 | + 65) sha256sum "$file" ;; |
| 65 | + 97) sha384sum "$file" ;; |
| 66 | + 129) sha512sum "$file" ;; |
| 67 | + *) echo "FATAL! Unknown hash length: $hashLength" 1>&2 && exit 1 ;; |
| 68 | + esac | cut -d ' ' -f 1` |
| 69 | + |
| 70 | + if [ ! "$expectedHash" = "$hash" ] |
| 71 | + then |
| 72 | + echo "FAIL! wrong file hash: $file $hash" 1>&2 |
| 73 | + return 1 |
| 74 | + fi |
| 75 | + echo "OK verified integrity of: $file" |
| 76 | +} |
| 77 | + |
| 78 | +# Check if file already exists and is good, otherwise download it |
| 79 | +DownloadURL () { |
46 | 80 | url=$1
|
47 |
| - url_alt=$2 |
48 |
| - expectedSha1=$3 |
| 81 | + url2=$2 |
| 82 | + expectedHash=$3 |
49 | 83 | targetFile=$4
|
50 | 84 |
|
51 |
| - echo "Downloading from primary URL '$url'" |
52 |
| - curl --fail -L --retry 5 --retry-delay 5 -o "$targetFile" "$url" || { |
53 |
| - echo "Failed to download '$url' multiple times" |
54 |
| - echo "Downloading from alternative URL '$url_alt'" |
55 |
| - curl --fail -L --retry 5 --retry-delay 5 -o "$targetFile" "$url_alt" || { echo 'Failed to download even from alternative url'; exit 1; } |
56 |
| - } |
57 |
| - |
58 |
| - echo "Checking SHA1 on PKG '$targetFile'" |
59 |
| - echo "$expectedSha1 *$targetFile" > "$targetFile.sha1" |
60 |
| - sha1sum --check "$targetFile.sha1" |
61 |
| - rm -f "$targetFile.sha1" |
| 85 | + if VerifyHash "$targetFile" "$expectedHash" |
| 86 | + then |
| 87 | + echo "Skipping download, found and validated existing file: $targetFile" |
| 88 | + else |
| 89 | + echo "Downloading from primary URL: $url" |
| 90 | + if ! curl --fail -L --retry 5 --retry-delay 5 -o "$targetFile" "$url" |
| 91 | + then |
| 92 | + echo "FAIL! to download, trying alternative URL: $url2" 1>&2 |
| 93 | + if ! curl --fail -L --retry 5 --retry-delay 5 -o "$targetFile" "$url2" |
| 94 | + then |
| 95 | + echo 'FAIL! to download even from alternative url' 1>&2 |
| 96 | + return 1 |
| 97 | + fi |
| 98 | + fi |
| 99 | + VerifyHash "$targetFile" "$expectedHash" |
| 100 | + fi |
62 | 101 | }
|
63 |
| - |
|
0 commit comments