@@ -643,77 +643,69 @@ function download_url() {
643643 validate_url " $url "
644644
645645 # Remove any existing file
646- rm -rf " $image_name "
647-
648- # Retry loop
646+ rm -f " $image_name "
647+
648+ # Retry loop: focus purely on download
649649 while [ $retry_count -lt $DOWNLOAD_MAX_RETRIES ]; do
650650 if [ $retry_count -gt 0 ]; then
651651 warn " Retry attempt $retry_count of $DOWNLOAD_MAX_RETRIES "
652652 sleep $DOWNLOAD_RETRY_DELAY
653653 else
654654 log " Download attempt $(( retry_count + 1 )) of $DOWNLOAD_MAX_RETRIES "
655655 fi
656-
657- # Download with timeout and progress
656+
658657 log " Downloading $( basename " $image_name " ) ..."
659- if curl -fLSs --retry 2 --retry-delay 2 --connect-timeout 60 --max-time $DOWNLOAD_TIMEOUT " $url " -o " ./$image_name " 2>&1 ; then
660-
661- # Verify file exists and has content
662- if [ ! -f " ./$image_name " ] || [ ! -s " ./$image_name " ]; then
663- error " Downloaded file is missing or empty"
664- retry_count=$(( retry_count + 1 ))
665- continue
666- fi
667-
668- log " Download completed, verifying integrity..."
669-
670- # Verify file size
671- if ! verify_file_size " ./$image_name " " $url " ; then
672- warn " File size verification failed, retrying..."
673- rm -f " ./$image_name "
674- retry_count=$(( retry_count + 1 ))
675- continue
676- fi
677-
678- # Verify SHA256 checksum
679- if ! verify_sha256 " ./$image_name " " $expected_sha256 " ; then
680- error " Checksum verification failed, retrying..."
681- rm -f " ./$image_name "
682- retry_count=$(( retry_count + 1 ))
683- continue
684- fi
685-
686- # All verifications passed
687- IMAGE_PATH=" ./$image_name "
688- DOWNLOAD_IMAGE_NAME=" $image_name "
658+ if curl -fLSs --retry 2 --retry-delay 2 --connect-timeout 60 \
659+ --max-time $DOWNLOAD_TIMEOUT " $url " -o " ./$image_name " 2>&1 ; then
689660 download_success=true
690-
691- success " ========================================="
692- success " ✓ Download and verification completed successfully!"
693- success " File: $( basename " $image_name " ) "
694- success " Location: $IMAGE_PATH "
695- success " ========================================="
696- return 0
661+ break
697662 else
698663 local curl_exit=$?
699- error " Download failed with curl error code: $curl_exit "
700-
701- # Provide specific error messages for common curl exit codes
664+ error " Download failed (curl exit code: $curl_exit )"
702665 case $curl_exit in
703- 1) error " Reason: Could not resolve host (DNS failure)" ;;
704- 2) error " Reason: Failed to connect to host" ;;
705- 3) error " Reason: Partial file transfer" ;;
706- 4) error " Reason: HTTP error (404, 403, etc.)" ;;
707- 5) error " Reason: Operation timeout" ;;
708- 6) error " Reason: SSL connection error" ;;
709- * ) error " Reason: See curl manual for error code $curl_exit " ;;
666+ 1) error " Could not resolve host (DNS failure)" ;;
667+ 2) error " Failed to connect to host" ;;
668+ 3) error " Partial file transfer" ;;
669+ 4) error " HTTP error (404/ 403/ etc.)" ;;
670+ 5) error " Operation timeout" ;;
671+ 6) error " SSL connection error" ;;
672+ * ) error " See curl manual for exit code $curl_exit " ;;
710673 esac
711-
712674 rm -f " ./$image_name "
713675 retry_count=$(( retry_count + 1 ))
714676 fi
715677 done
716-
678+
679+ # Verify file existence and content after all download attempts
680+ if [ ! -f " ./$image_name " ] || [ ! -s " ./$image_name " ]; then
681+ error " Downloaded file is missing or empty after $DOWNLOAD_MAX_RETRIES attempts."
682+ return 1
683+ fi
684+
685+ # Perform verification once, after successful download
686+ log " Download completed — running one-time verification checks..."
687+
688+ if ! verify_file_size " ./$image_name " " $url " ; then
689+ warn " File size verification failed; please verify manually."
690+ fi
691+
692+ if ! verify_sha256 " ./$image_name " " $expected_sha256 " ; then
693+ error " Checksum verification failed; downloaded file may be corrupted."
694+ return 1
695+ fi
696+
697+ IMAGE_PATH=" ./$image_name "
698+ DOWNLOAD_IMAGE_NAME=" $image_name "
699+
700+ success " ========================================="
701+ success " ✓ Download and verification completed successfully!"
702+ success " File: $( basename " $image_name " ) "
703+ success " Location: $IMAGE_PATH "
704+ success " ========================================="
705+ return 0
706+ }
707+
708+
717709 # All retries failed
718710 if [ " $download_success " = false ]; then
719711 error " ========================================="
0 commit comments