aboutsummaryrefslogtreecommitdiffstats
path: root/coin/provisioning/common/unix/DownloadURL.sh
diff options
context:
space:
mode:
authorLiang Qi <liang.qi@qt.io>2020-01-10 12:18:00 +0100
committerLiang Qi <liang.qi@qt.io>2020-01-10 12:19:10 +0100
commitb540aedf0a25021da963a8124d8816272329ec9d (patch)
tree4e0aa0abbb08ba989fd710d48b97efc943d5d3c7 /coin/provisioning/common/unix/DownloadURL.sh
parent4adeebfdcf55657103b42a3b737db131d03a4204 (diff)
parent255e87ea249a503ee7136e34d9845a0d2c59447e (diff)
Merge remote-tracking branch 'origin/5.12.6' into 5.12
Conflicts: .gitmodules Change-Id: I82e695715beec4fd0cd58e31b11606e24afc4b0f
Diffstat (limited to 'coin/provisioning/common/unix/DownloadURL.sh')
-rwxr-xr-xcoin/provisioning/common/unix/DownloadURL.sh104
1 files changed, 79 insertions, 25 deletions
diff --git a/coin/provisioning/common/unix/DownloadURL.sh b/coin/provisioning/common/unix/DownloadURL.sh
index 996c99da..5bafc33d 100755
--- a/coin/provisioning/common/unix/DownloadURL.sh
+++ b/coin/provisioning/common/unix/DownloadURL.sh
@@ -1,8 +1,8 @@
-#!/usr/bin/env bash
+#!/bin/sh
#############################################################################
##
-## Copyright (C) 2017 The Qt Company Ltd.
+## Copyright (C) 2019 The Qt Company Ltd.
## Contact: http://www.qt.io/licensing/
##
## This file is part of the provisioning scripts of the Qt Toolkit.
@@ -33,31 +33,85 @@
##
#############################################################################
+
# A helper script used for downloading a file from a URL or an alternative
-# URL. Also the SHA1 is checked for the file. Target filename should also
-# be given.
-#
-# If called directly from another script, it will exit the parent script
-# as well, if not called in its own subshell with parentheses.
+# URL. Also the SHA is checked for the file (SHA algorithm is autodetected
+# based on the SHA length). Target filename should also be given.
-set -ex
+############################ BOILERPLATE ###############################
+command -v sha1sum >/dev/null || alias sha1sum='shasum -a 1'
+command -v sha256sum >/dev/null || alias sha256sum='shasum -a 256'
+command -v sha384sum >/dev/null || alias sha384sum='shasum -a 384'
+command -v sha512sum >/dev/null || alias sha512sum='shasum -a 512'
+########################################################################
-function DownloadURL {
- url=$1
- url_alt=$2
- expectedSha1=$3
- targetFile=$4
-
- echo "Downloading from primary URL '$url'"
- curl --fail -L --retry 5 --retry-delay 5 -o "$targetFile" "$url" || {
- echo "Failed to download '$url' multiple times"
- echo "Downloading from alternative URL '$url_alt'"
- curl --fail -L --retry 5 --retry-delay 5 -o "$targetFile" "$url_alt" || { echo 'Failed to download even from alternative url'; exit 1; }
- }
-
- echo "Checking SHA1 on PKG '$targetFile'"
- echo "$expectedSha1 *$targetFile" > "$targetFile.sha1"
- sha1sum --check "$targetFile.sha1"
- rm -f "$targetFile.sha1"
+
+Download () {
+ url="$1"
+ targetFile="$2"
+
+ command -v curl >/dev/null \
+ && curl --fail -L --retry 5 --retry-delay 5 -o "$targetFile" "$url" \
+ || wget --tries 5 -O "$targetFile" "$url"
}
+VerifyHash () {
+ file=$1
+ expectedHash=$2
+
+ if [ ! -f "$file" ]
+ then return 2 # file does not exist
+ fi
+
+
+ hashLength="$(echo "$expectedHash" | wc -c | sed 's/ *//g')"
+ # Use backticks because of bug with bash-3 (default on macOS),
+ # caused when there are unbalanced parentheses inside $()
+ # shellcheck disable=SC2006
+ hash=`case "$hashLength" in
+ 41) sha1sum "$file" ;;
+ 65) sha256sum "$file" ;;
+ 97) sha384sum "$file" ;;
+ 129) sha512sum "$file" ;;
+ *) echo "FATAL! Unknown hash length: $hashLength" 1>&2 && exit 1 ;;
+ esac | cut -d ' ' -f 1`
+
+ if [ ! "$expectedHash" = "$hash" ]
+ then
+ echo "FAIL! wrong file hash: $file $hash" 1>&2
+ return 1
+ fi
+ echo "OK verified integrity of: $file"
+}
+
+# Check if file already exists and is good, otherwise download it
+DownloadURL () {
+ url=$1
+ url2=$2
+ expectedHash=$3
+ # Optional argument $4: destination filename
+ if [ x"$4" = x ]
+ then
+ # defaults to the last component of $url
+ targetFile=$(echo $url | sed 's|^.*/||')
+ else
+ targetFile=$4
+ fi
+
+ if VerifyHash "$targetFile" "$expectedHash"
+ then
+ echo "Skipping download, found and validated existing file: $targetFile"
+ else
+ echo "Downloading from primary URL: $url"
+ if ! Download "$url" "$targetFile"
+ then
+ echo "FAIL! to download, trying alternative URL: $url2" 1>&2
+ if ! Download "$url2" "$targetFile"
+ then
+ echo 'FAIL! to download even from alternative URL' 1>&2
+ return 1
+ fi
+ fi
+ VerifyHash "$targetFile" "$expectedHash"
+ fi
+}