Check-in [d9b5ef323c]
Overview
Comment:Added common downloader script
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:d9b5ef323c12bf83b05d8f5764934cc7afdbf7f9
User & Date: rkeene on 2016-02-24 19:39:12
Other Links: manifest | tags
Context
2016-02-24
19:59
Added some hashes for individual packages check-in: 3c71954242 user: rkeene tags: trunk
19:39
Added common downloader script check-in: d9b5ef323c user: rkeene tags: trunk
19:38
Updated to use common download infrastructure check-in: 6f9977f121 user: rkeene tags: trunk
Changes

Added common/helpers/download version [18dd990a6c].

            1  +#! /usr/bin/env bash
            2  +
            3  +url="$1"
            4  +file="$2"
            5  +hash="$3"
            6  +
            7  +if [ "${#}" != '3' ]; then
            8  +	echo 'Usage: download <url> <file> <hash>' >&2
            9  +
           10  +	exit 1
           11  +fi
           12  +
           13  +for tryDownloadProgram in wget curl; do
           14  +	tryDownloadProgramPath="$(which "${tryDownloadProgram}" 2>/dev/null)"
           15  +
           16  +	if [ -z "${tryDownloadProgramPath}" ]; then
           17  +		continue
           18  +	fi
           19  +
           20  +	if [ -x "${tryDownloadProgramPath}" ]; then
           21  +		downloadProgram="${tryDownloadProgram}"
           22  +		downloadProgramPath="${tryDownloadProgramPath}"
           23  +
           24  +		break
           25  +	fi
           26  +done
           27  +
           28  +case "${hash}" in
           29  +	????????????????????????????????????????)
           30  +		hashMethod='sha1'
           31  +		;;
           32  +	????????????????????????????????????????????????????????????????)
           33  +		hashMethod='sha256'
           34  +		;;
           35  +	-)
           36  +		hashMethod='null'
           37  +		;;
           38  +	*)
           39  +		echo "Unknown hash method for hash ${hash}" >&2
           40  +
           41  +		exit 1
           42  +		;;
           43  +esac
           44  +
           45  +function downloadFile() {
           46  +	local file urls
           47  +	local downloadProgramArgs
           48  +	local url
           49  +	local finalURL
           50  +
           51  +	file="$1"
           52  +	shift
           53  +
           54  +	urls=("$@")
           55  +
           56  +	finalURL="${urls[-1]}"
           57  +
           58  +	case "${downloadProgram}" in
           59  +		curl)
           60  +			downloadProgramArgs=(--header "X-Cache-URL: ${finalURL}" --location --insecure --fail --output "${file}")
           61  +			;;
           62  +		wget)
           63  +			downloadProgramArgs=(--header="X-Cache-URL: ${finalURL}" --no-check-certificate --output-document="${file}")
           64  +			;;
           65  +	esac
           66  +
           67  +	for url in "${urls[@]}" __fail__; do
           68  +		rm -f "${file}"
           69  +
           70  +		if [ "${url}" = '__fail__' ]; then
           71  +			return 1
           72  +		fi
           73  +
           74  +		"${downloadProgram}" "${downloadProgramArgs[@]}" "${url}" && break
           75  +	done
           76  +
           77  +	return 0
           78  +}
           79  +
           80  +function verifyHash() {
           81  +	local file hash hashMethod
           82  +	local checkHash
           83  +
           84  +	file="$1"
           85  +	hash="$2"
           86  +	hashMethod="$3"
           87  +
           88  +	if [ "${hashMethod}" = 'null' ]; then
           89  +		return 0
           90  +	fi
           91  +
           92  +	checkHash="$(openssl "${hashMethod}" "${file}" | sed 's@.*= *@@')"
           93  +
           94  +	if [ "${checkHash}" = "${hash}" ]; then
           95  +		return 0
           96  +	fi
           97  +
           98  +	echo "Hash (${hashMethod}) mismatch: Got: ${checkHash}; Expected: ${hash}" >&2
           99  +
          100  +	return 1
          101  +}
          102  +
          103  +rm -f "${file}.new" || exit 1
          104  +
          105  +urls=("${url}")
          106  +
          107  +if [ "${hashMethod}" != 'null' ]; then
          108  +	urls=(
          109  +		"http://hashcache.rkeene.org/${hashMethod}/${hash}"
          110  +		"${urls[@]}"
          111  +	)
          112  +fi
          113  +
          114  +downloadFile "${file}.new" "${urls[@]}" || exit 1
          115  +
          116  +verifyHash "${file}.new" "${hash}" "${hashMethod}" || exit 1
          117  +
          118  +mv "${file}.new" "${file}" || exit 1
          119  +
          120  +exit 0