termux-packages/scripts/build/termux_download.sh
Leonid Pliushch 5ddf251200 build-package.sh: retry if downloading of Release{,.gpg} failed when fast-build mode is on
Release or Release.gpg files may be absent if a new .deb file was uploaded
to apt repository recently. We may need to do a multiple attempts in a loop
to retry downloading of these files.

Needed after 99e9ab67b634a89d0be9eca12c4f1cdc091ee6e8, where I have disabled
looping in termux_download but curl doesn't do retries on 404 error. We
want immediate failure only for 404's on source and build tool URLs.
2020-08-21 15:46:41 +03:00

44 lines
1.3 KiB
Bash
Executable File

termux_download() {
if [ $# != 3 ]; then
termux_error_exit "termux_download(): Invalid arguments - expected \$URL \$DESTINATION \$CHECKSUM"
fi
local URL="$1"
local DESTINATION="$2"
local CHECKSUM="$3"
if [ -f "$DESTINATION" ] && [ "$CHECKSUM" != "SKIP_CHECKSUM" ]; then
# Keep existing file if checksum matches.
local EXISTING_CHECKSUM
EXISTING_CHECKSUM=$(sha256sum "$DESTINATION" | cut -f 1 -d ' ')
if [ "$EXISTING_CHECKSUM" = "$CHECKSUM" ]; then return; fi
fi
local TMPFILE
TMPFILE=$(mktemp "$TERMUX_PKG_TMPDIR/download.$TERMUX_PKG_NAME.XXXXXXXXX")
echo "Downloading ${URL}"
if curl --fail --retry 20 --retry-connrefused --retry-delay 30 --location --output "$TMPFILE" "$URL"; then
local ACTUAL_CHECKSUM
ACTUAL_CHECKSUM=$(sha256sum "$TMPFILE" | cut -f 1 -d ' ')
if [ "$CHECKSUM" != "SKIP_CHECKSUM" ]; then
if [ "$CHECKSUM" != "$ACTUAL_CHECKSUM" ]; then
>&2 printf "Wrong checksum for %s:\nExpected: %s\nActual: %s\n" \
"$URL" "$CHECKSUM" "$ACTUAL_CHECKSUM"
return 1
fi
else
printf "WARNING: No checksum check for %s:\nActual: %s\n" \
"$URL" "$ACTUAL_CHECKSUM"
fi
mv "$TMPFILE" "$DESTINATION"
return 0
fi
echo "Failed to download $URL" >&2
return 1
}
# Make script standalone executable as well as sourceable
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
termux_download "$@"
fi