Skip to content

Commit

Permalink
Fix up download library script
Browse files Browse the repository at this point in the history
  • Loading branch information
jgowdy committed May 9, 2024
1 parent 9b71bd9 commit 2809f81
Showing 1 changed file with 54 additions and 21 deletions.
75 changes: 54 additions & 21 deletions scripts/download-libraries.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -e # Exit on any command failure

# Global Constants
CHECK_INTERVAL_SECONDS=$((5 * 60)) # 5 minutes
MAX_DOWNLOAD_RETRIES=3

# Function to check if a specific file download is necessary
function check_download_required {
Expand All @@ -13,13 +14,23 @@ function check_download_required {
local etag_file="${file}.etag"

# If the file does not exist or the .etag file is missing, download is required
if [[ ! -f "$file" || ! -f "$etag_file" ]]; then
return 1 # true (download required)
if [[ ! -f "$file" ]]; then
echo "${file} does not exist"
return 0 # (download required)
else
echo "${file} exists"
fi

# If the file does not exist or the .etag file is missing, download is required
if [[ ! -f "$etag_file" ]]; then
echo "${etag_file} does not exist"
return 0 # (download required)
fi


# If no-cache is passed, we ignore the modified date of the .etag files
if [[ "$no_cache" == true ]]; then
return 1 # true (download required)
return 0 # (download required)
fi

# Check if the .etag file is older than the interval
Expand All @@ -30,15 +41,15 @@ function check_download_required {
# Linux uses 'stat -c'
last_check=$(stat -c %Y "$etag_file")
fi
#last_check=$(stat -c %Y "$etag_file")

current_time=$(date +%s)
elapsed_time=$((current_time - last_check))

if (( elapsed_time > check_interval_seconds )); then
return 1 # true (download required)
return 0 # (download required)
fi

return 0 # false (download not required)
return 1 # (download not required)
}

# Function to download a file
Expand Down Expand Up @@ -69,22 +80,28 @@ function verify_checksums {
sha_cmd="shasum -a 256"
else
echo "Error: Neither sha256sum nor shasum is available on this system." >&2
exit 1
exit 1 # Exit since this is fatal
fi

if [[ ! -f "${sums}" ]]; then
echo "Error: Checksum file '${sums}' does not exist." >&2
rm -f ./*.a ./*.h ./*.etag
return 1
fi

# Filter the relevant checksums and verify they are not empty
checksums=$(grep -e "${archive}" -e "${header}" "${sums}")
if [[ -z "$checksums" ]]; then
echo "Error: No matching checksums found for ${archive} or ${header} in ${sums}." >&2
exit 1
return 1
fi

echo "$checksums" > ./SHA256SUM # Return value

if ! $sha_cmd -c ./SHA256SUM; then
echo 'SHA256 mismatch!' >&2
rm -f ./*.a ./*.h
exit 1
return 1
fi
}

Expand Down Expand Up @@ -217,22 +234,38 @@ function main {
mkdir -p lib
cd lib || exit 1

# Per-file touch and download logic
# Now, you can access the URLs using the index of the filenames
for i in "${!file_names[@]}"; do
if check_download_required "${file_names[$i]}" "$no_cache" "$CHECK_INTERVAL_SECONDS"; then
download_file "${file_urls[$i]}" "${file_names[$i]}"
local retries=0
local checksums_verified=false
while [[ $checksums_verified == false && $retries -lt $MAX_DOWNLOAD_RETRIES ]]; do
# Per-file touch and download logic
for i in "${!file_names[@]}"; do
if check_download_required "${file_names[$i]}" "$no_cache" "$CHECK_INTERVAL_SECONDS"; then
download_file "${file_urls[$i]}" "${file_names[$i]}"
else
interval_str=$(interval_message "$CHECK_INTERVAL_SECONDS")
echo "${file_names[$i]} is up to date (checked within the last ${interval_str})"
fi
done

# Verify checksums and copy files
if verify_checksums "${archive}" "${header}" "${sums}"; then
copy_files "${archive}" "${header}"
checksums_verified=true
else
interval_str=$(interval_message "$CHECK_INTERVAL_SECONDS")
echo "${file_names[$i]} is up to date (checked within the last ${interval_str})"
echo "Verification failed, re-downloading files..."
((retries++))
# Sleep for a bit before retrying to avoid hammering the server
sleep 1
fi
done

# Verify checksums and copy files
verify_checksums "${archive}" "${header}" "${sums}"
copy_files "${archive}" "${header}"

echo "Asherah libraries downloaded successfully"
if [[ $checksums_verified == true ]]; then
copy_files "${archive}" "${header}"
echo "Asherah libraries downloaded successfully"
else
echo "Failed to download Asherah libraries after $retries retries."
exit 1
fi
}

# Execute the main function
Expand Down

0 comments on commit 2809f81

Please sign in to comment.