|
| 1 | +#!/usr/bin/bash |
| 2 | +## |
| 3 | +## Copyright 2020 Termux |
| 4 | +## |
| 5 | +## Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +## you may not use this file except in compliance with the License. |
| 7 | +## You may obtain a copy of the License at |
| 8 | +## |
| 9 | +## http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +## |
| 11 | +## Unless required by applicable law or agreed to in writing, software |
| 12 | +## distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +## See the License for the specific language governing permissions and |
| 15 | +## limitations under the License. |
| 16 | +## |
| 17 | + |
| 18 | +termux_download() { |
| 19 | + if [[ $# != 2 ]] && [[ $# != 3 ]]; then |
| 20 | + echo "termux_download(): Invalid arguments - expected <URL> <DESTINATION> [<CHECKSUM>]" 1>&2 |
| 21 | + return 1 |
| 22 | + fi |
| 23 | + local URL="$1" |
| 24 | + local DESTINATION="$2" |
| 25 | + local CHECKSUM="${3:-SKIP_CHECKSUM}" |
| 26 | + |
| 27 | + if [[ "$URL" =~ ^file://(/[^/]+)+$ ]]; then |
| 28 | + local source="${URL:7}" # Remove `file://` prefix |
| 29 | + |
| 30 | + if [ -d "$source" ]; then |
| 31 | + # Create tar file from local directory |
| 32 | + echo "Downloading local source directory at '$source'" |
| 33 | + rm -f "$DESTINATION" |
| 34 | + (cd "$(dirname "$source")" && tar -cf "$DESTINATION" --exclude=".git" "$(basename "$source")") |
| 35 | + return 0 |
| 36 | + elif [ ! -f "$source" ]; then |
| 37 | + echo "No local source file found at path of URL '$URL'" |
| 38 | + return 1 |
| 39 | + fi |
| 40 | + fi |
| 41 | + |
| 42 | + if [ -f "$DESTINATION" ] && [ "$CHECKSUM" != "SKIP_CHECKSUM" ]; then |
| 43 | + # Keep existing file if checksum matches. |
| 44 | + local EXISTING_CHECKSUM |
| 45 | + EXISTING_CHECKSUM=$(sha256sum "$DESTINATION" | cut -d' ' -f1) |
| 46 | + [[ "$EXISTING_CHECKSUM" == "$CHECKSUM" ]] && return |
| 47 | + fi |
| 48 | + |
| 49 | + local TMPFILE |
| 50 | + local -a CURL_OPTIONS=( |
| 51 | + --fail # Consider 4xx and 5xx responses as failures |
| 52 | + --retry 5 # Retry up to 5 times on transient failures |
| 53 | + --retry-connrefused # Also retry on refused connections |
| 54 | + --retry-delay 5 # Wait 5 seconds between retries |
| 55 | + --connect-timeout 30 # Wait at most 30 seconds for a connection to be established |
| 56 | + --retry-max-time 120 # Stop retrying if it's still failing after 120 seconds |
| 57 | + --speed-limit 1000 # Expect at least 1000 Bytes per second |
| 58 | + --speed-time 60 # Fail if the minimum speed isn't met for at least 60 seconds |
| 59 | + --location # Follow redirects |
| 60 | + ) |
| 61 | + TMPFILE=$(mktemp "$TERMUX_PKG_TMPDIR/download.${TERMUX_PKG_NAME-unnamed}.XXXXXXXXX") |
| 62 | + if [[ "${TERMUX_QUIET_BUILD-}" == "true" ]]; then |
| 63 | + CURL_OPTIONS+=(--no-progress-meter) # Don't print out transfer statistics |
| 64 | + fi |
| 65 | + |
| 66 | + echo "Downloading ${URL}" |
| 67 | + if ! curl "${CURL_OPTIONS[@]}" --output "$TMPFILE" "$URL"; then |
| 68 | + local error=1 |
| 69 | + local retry=2 |
| 70 | + local delay=60 |
| 71 | + local try |
| 72 | + for (( try=1; try <= retry; try++ )); do |
| 73 | + echo "Retrying #${try} download ${URL} in ${delay}" |
| 74 | + sleep "${delay}" |
| 75 | + if curl "${CURL_OPTIONS[@]}" --output "$TMPFILE" "$URL"; then |
| 76 | + error=0 |
| 77 | + break |
| 78 | + fi |
| 79 | + done |
| 80 | + if [[ "${error}" != 0 ]]; then |
| 81 | + echo "Failed to download $URL" 1>&2 |
| 82 | + return 1 |
| 83 | + fi |
| 84 | + fi |
| 85 | + |
| 86 | + local ACTUAL_CHECKSUM |
| 87 | + ACTUAL_CHECKSUM=$(sha256sum "$TMPFILE" | cut -d' ' -f1) |
| 88 | + if [[ -z "$CHECKSUM" ]]; then |
| 89 | + printf "WARNING: No checksum check for %s:\nActual: %s\n" \ |
| 90 | + "$URL" "$ACTUAL_CHECKSUM" |
| 91 | + elif [[ "$CHECKSUM" == "SKIP_CHECKSUM" ]]; then |
| 92 | + : |
| 93 | + elif [[ "$CHECKSUM" != "$ACTUAL_CHECKSUM" ]]; then |
| 94 | + printf "Wrong checksum for %s\nExpected: %s\nActual: %s\n" \ |
| 95 | + "$URL" "$CHECKSUM" "$ACTUAL_CHECKSUM" 1>&2 |
| 96 | + return 1 |
| 97 | + fi |
| 98 | + mv "$TMPFILE" "$DESTINATION" |
| 99 | + return 0 |
| 100 | +} |
| 101 | + |
| 102 | +# Make script standalone executable as well as sourceable |
| 103 | +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then |
| 104 | + termux_download "$@" |
| 105 | +fi |
0 commit comments