From 622fda4b321a1f93ff12793bb1d8d174299b5742 Mon Sep 17 00:00:00 2001 From: justin Date: Mon, 26 Jan 2026 14:32:47 -0500 Subject: [PATCH] Fix download_with_progress() content_length calc --- misc/alpine-tools.func | 16 ++++++++++++---- misc/tools.func | 9 +++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/misc/alpine-tools.func b/misc/alpine-tools.func index 6d7ed215a..a830c220c 100644 --- a/misc/alpine-tools.func +++ b/misc/alpine-tools.func @@ -196,11 +196,19 @@ ensure_usr_local_bin_persist() { download_with_progress() { # $1 url, $2 dest - local url="$1" out="$2" cl + local url="$1" out="$2" content_length need_tool curl pv || return 1 - cl=$(curl -fsSLI "$url" 2>/dev/null | awk 'tolower($0) ~ /^content-length:/ {print $2}' | tr -d '\r') - if [ -n "$cl" ]; then - curl -fsSL "$url" | pv -s "$cl" >"$out" || { + + content_length=$( + curl -fsSLI "$url" 2>/dev/null | + # May return multiple values on redirect. i.e., 0 and content-length + # Cast $2 to int by adding 0 to it + awk '(tolower($1) ~ /^content-length:/) && ($2 + 0 > 0) {print $2+0}' | + tr -cd '[:digit:]' || true + ) + + if [ -n "$content_length" ]; then + curl -fsSL "$url" | pv -s "$content_length" >"$out" || { msg_error "Download failed: $url" return 1 } diff --git a/misc/tools.func b/misc/tools.func index 70e8c3fa9..44484ae5b 100644 --- a/misc/tools.func +++ b/misc/tools.func @@ -1692,7 +1692,13 @@ function download_with_progress() { # Content-Length aus HTTP-Header holen local content_length - content_length=$(curl -fsSLI "$url" | awk '/Content-Length/ {print $2}' | tr -d '\r' || true) + content_length=$( + curl -fsSLI "$url" 2>/dev/null | + # May return multiple values on redirect. i.e., 0 and content_length + # Add 0 to $2 to cast it to int + awk '(tolower($1) ~ /^content-length:/) && ($2 + 0 > 0) {print $2+0}' | + tr -cd '[:digit:]' || true + ) if [[ -z "$content_length" ]]; then if ! curl -fL# -o "$output" "$url"; then @@ -6205,4 +6211,3 @@ function fetch_and_deploy_archive() { msg_ok "Successfully deployed archive to $directory" return 0 } -