Skip to content

Commit 8ed0377

Browse files
jimisjujokini
authored andcommitted
Refactor DownloadURL.sh
Auto-detects and verifies all kinds of hashes, not only SHA1. Hash verification is now an autonomous function. If the file is found at the destination, skip redownloading it if the hash is OK. Runs on POSIX sh, not requiring bash. Retain compatibility with previous version. Change-Id: I9930336030aa776f49dc460fe766f3e831700255 Reviewed-by: Tony Sarajärvi <[email protected]> (cherry picked from commit c0fce7a) Reviewed-by: Heikki Halmet <[email protected]>
1 parent fcf4cca commit 8ed0377

File tree

1 file changed

+61
-23
lines changed

1 file changed

+61
-23
lines changed

coin/provisioning/common/unix/DownloadURL.sh

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#!/usr/bin/env bash
1+
#!/bin/sh
22

33
#############################################################################
44
##
5-
## Copyright (C) 2017 The Qt Company Ltd.
5+
## Copyright (C) 2019 The Qt Company Ltd.
66
## Contact: http://www.qt.io/licensing/
77
##
88
## This file is part of the provisioning scripts of the Qt Toolkit.
@@ -33,31 +33,69 @@
3333
##
3434
#############################################################################
3535

36+
3637
# 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
4256

43-
set -ex
4457

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 () {
4680
url=$1
47-
url_alt=$2
48-
expectedSha1=$3
81+
url2=$2
82+
expectedHash=$3
4983
targetFile=$4
5084

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
62101
}
63-

0 commit comments

Comments
 (0)