# Shared by tools/build-*.sh (sourced). # clone_tag URL TAG DIR: a shallow clone of TAG into DIR, tried three times: a transfer that # stalls (GitHub's sometimes do) is given up after a minute, a connection that hangs by curl's # own timeout; a slow one is waited for. With SRC_CACHE set, a clone made is kept there and # the next build copies it rather than fetching it again (the scripts check its commit either # way). clone_tag() { local url=$1 tag=$2 dir=$3 try cached="" if [ -n "${SRC_CACHE:-}" ]; then cached="$SRC_CACHE/$(basename "$url")@$tag" if [ -d "$cached/.git" ]; then # Its files as its commit has them, whatever happened to the copy since. rm -rf "$dir" && cp -a "$cached" "$dir" && git -C "$dir" reset -q --hard && git -C "$dir" clean -qfdx && return 0 fi fi for try in 1 2 3; do rm -rf "$dir" if git -c advice.detachedHead=false -c http.lowSpeedLimit=1000 -c http.lowSpeedTime=60 \ clone -q --depth 1 --branch "$tag" "$url" "$dir"; then if [ -n "$cached" ]; then mkdir -p "$SRC_CACHE" && rm -rf "$cached.tmp" && cp -a "$dir" "$cached.tmp" && mv "$cached.tmp" "$cached" fi return 0 fi echo "cloning $url ($tag) failed, try $try of 3" >&2 sleep 5 done return 1 } # clone_commit URL COMMIT DIR: COMMIT alone, for a project without release tags; cached and # tried again as clone_tag is. clone_commit() { local url=$1 commit=$2 dir=$3 try cached="" if [ -n "${SRC_CACHE:-}" ]; then cached="$SRC_CACHE/$(basename "$url")@$commit" if [ -d "$cached/.git" ]; then rm -rf "$dir" && cp -a "$cached" "$dir" && git -C "$dir" reset -q --hard && git -C "$dir" clean -qfdx && return 0 fi fi for try in 1 2 3; do rm -rf "$dir" && mkdir -p "$dir" if git -C "$dir" init -q && git -C "$dir" remote add origin "$url" && git -C "$dir" -c http.lowSpeedLimit=1000 -c http.lowSpeedTime=60 fetch -q --depth 1 origin "$commit" && git -C "$dir" -c advice.detachedHead=false checkout -q FETCH_HEAD; then if [ -n "$cached" ]; then mkdir -p "$SRC_CACHE" && rm -rf "$cached.tmp" && cp -a "$dir" "$cached.tmp" && mv "$cached.tmp" "$cached" fi return 0 fi echo "fetching $url ($commit) failed, try $try of 3" >&2 sleep 5 done return 1 } # fetch_pinned URL SHA256 FILE: a download, checked by its hash; kept in SRC_CACHE. fetch_pinned() { local url=$1 sha=$2 out=$3 cached="" if [ -n "${SRC_CACHE:-}" ]; then cached="$SRC_CACHE/$sha" if [ -f "$cached" ] && echo "$sha $cached" | sha256sum -c --quiet 2>/dev/null; then cp "$cached" "$out" && return 0 fi fi # Whole, unless it stalls: some networks cut TLS to some hosts after about 16 KB (as # zapret2's cutoff option knows). Then in ranges below that, each on a connection of its own. if ! curl -fsSL --connect-timeout 30 --speed-limit 1 --speed-time 15 --max-time 900 --retry 2 -o "$out" "$url"; then echo "$url stalled; fetching it in pieces" >&2 local size from=0 step=12000 tries=0 size=$(curl -fsSIL --connect-timeout 30 --max-time 60 "$url" | tr -d '\r' | awk 'tolower($1) == "content-length:" { n = $2 } END { print n }') [[ "$size" =~ ^[0-9]+$ ]] && [ "$size" -gt 0 ] || { echo "$url: no size to fetch it in pieces by" >&2; return 1; } : >"$out" while [ "$from" -lt "$size" ]; do if curl -fsSL --connect-timeout 30 --max-time 60 -r "$from-$((from + step - 1))" "$url" >>"$out.piece" 2>/dev/null && [ "$(stat -c %s "$out.piece")" -gt 0 ]; then cat "$out.piece" >>"$out" from=$(stat -c %s "$out") tries=0 else tries=$((tries + 1)) [ "$tries" -lt 5 ] || { rm -f "$out.piece"; echo "$url: a piece failed five times" >&2; return 1; } fi rm -f "$out.piece" done fi echo "$sha $out" | sha256sum -c --quiet || { echo "$url: not the pinned file" >&2; return 1; } if [ -n "$cached" ]; then mkdir -p "$SRC_CACHE" && cp "$out" "$cached"; fi }