Skip to content

Commit

Permalink
Progress
Browse files Browse the repository at this point in the history
  • Loading branch information
jgowdy committed May 9, 2024
1 parent d3c9feb commit 9b71bd9
Show file tree
Hide file tree
Showing 13 changed files with 1,669 additions and 1,405 deletions.
14 changes: 0 additions & 14 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,5 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.cc]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.h]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
9 changes: 0 additions & 9 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,8 @@
},
'defines': [ 'NAPI_CPP_EXCEPTIONS', 'NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS', 'NODE_ADDON_API_DISABLE_DEPRECATED', 'NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED' ],
'sources': [
'lib/libasherah.h',
'src/asherah.h',
'src/asherah.cc',
'src/asherah_async_worker.h',
'src/asherah_async_worker.cc',
'src/logging.cc',
'src/logging.h',
'src/cobhan_napi_interop.h',
'src/cobhan.h',
'src/cobhan.cc',
'src/hints.h'
],
'libraries': [ '../lib/libasherah.a' ]
}
Expand Down
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@
"license": "MIT",
"files": [
"binding.gyp",
"src/asherah.h",
"src/asherah.cc",
"src/asherah_async_worker.h",
"src/asherah_async_worker.cc",
"src/cobhan_buffer_napi.h",
"src/cobhan_buffer.h",
"src/hints.h",
"src/logging.h",
"src/logging.cc",
"src/cobhan_napi_interop.h",
"src/cobhan.h",
"src/cobhan.cc",
"src/logging.h",
"src/napi_utils.h",
"src/scoped_allocate.h",
"src/asherah.d.ts",
"scripts/download-libraries.sh",
"scripts/build.sh",
Expand Down
277 changes: 229 additions & 48 deletions scripts/download-libraries.sh
Original file line number Diff line number Diff line change
@@ -1,58 +1,239 @@
#!/bin/bash

echo "Downloading Asherah libraries"

# shellcheck source=/dev/null
source .asherah-version

mkdir -p lib
cd lib || exit 1

OS=$(uname)
MACHINE=$(uname -m)

if [[ "${OS}" == 'Linux' ]]; then
if [[ ${MACHINE} == 'x86_64' ]]; then
echo "Linux x64"
ARCHIVE=libasherah-x64.a
HEADER=libasherah-x64-archive.h
SUMS=SHA256SUMS
elif [[ ${MACHINE} == 'aarch64' ]]; then
echo "Linux arm64"
ARCHIVE=libasherah-arm64.a
HEADER=libasherah-arm64-archive.h
SUMS=SHA256SUMS
set -e # Exit on any command failure

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

# Function to check if a specific file download is necessary
function check_download_required {
local file=$1
local no_cache=$2
local check_interval_seconds=$3
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)
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)
fi

# Check if the .etag file is older than the interval
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS uses 'stat -f'
last_check=$(stat -f %m "$etag_file")
else
echo "Unsupported CPU architecture"
exit 1
fi
elif [[ ${OS} == 'Darwin' ]]; then
if [[ ${MACHINE} == 'x86_64' ]]; then
echo "MacOS x64"
ARCHIVE=libasherah-darwin-x64.a
HEADER=libasherah-darwin-x64-archive.h
SUMS=SHA256SUMS-darwin
elif [[ ${MACHINE} == 'arm64' ]]; then
echo "MacOS arm64"
ARCHIVE=libasherah-darwin-arm64.a
HEADER=libasherah-darwin-arm64-archive.h
SUMS=SHA256SUMS-darwin
# 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)
fi

return 0 # false (download not required)
}

# Function to download a file
function download_file {
local url=$1
local file=$2
local etag_file="${file}.etag"

if ! curl -s -L --fail --etag-save "$etag_file" --etag-compare "$etag_file" -O "$url"; then
echo "Failed to download $url" >&2
exit 1
fi

# Explicitly touch the etag file to update its modification time only if successful
touch "$etag_file"
}

# Function to verify checksums
function verify_checksums {
local archive=$1
local header=$2
local sums=$3

# Determine the available SHA hashing utility
if command -v sha256sum &> /dev/null; then
sha_cmd="sha256sum"
elif command -v shasum &> /dev/null; then
sha_cmd="shasum -a 256"
else
echo "Error: Neither sha256sum nor shasum is available on this system." >&2
exit 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
fi

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

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

# Function to copy library and header files
function copy_files {
local archive=$1
local header=$2

if [[ ! -f "${archive}" ]]; then
echo "Error: Source archive file '${archive}' does not exist." >&2
exit 1
fi

if [[ ! -f "${header}" ]]; then
echo "Error: Source header file '${header}' does not exist." >&2
exit 1
fi

if ! cp -f "${archive}" libasherah.a; then
echo "Error: Failed to copy archive file '${archive}' to 'libasherah.a'." >&2
exit 1
fi

if ! cp -f "${header}" libasherah.h; then
echo "Error: Failed to copy header file '${header}' to 'libasherah.h'." >&2
exit 1
fi
}

# Function to detect OS and CPU architecture
function detect_os_and_cpu {
OS=$(uname)
MACHINE=$(uname -m)

#echo "Detected OS: ${OS}"
#echo "Detected CPU architecture: ${MACHINE}"

if [[ "${OS}" == 'Linux' ]]; then
if [[ ${MACHINE} == 'x86_64' ]]; then
#echo "Using Asherah libraries for Linux x86_64"
ARCHIVE="libasherah-x64.a"
HEADER="libasherah-x64-archive.h"
SUMS="SHA256SUMS"
elif [[ ${MACHINE} == 'aarch64' ]]; then
#echo "Using Asherah libraries for Linux aarch64"
ARCHIVE="libasherah-arm64.a"
HEADER="libasherah-arm64-archive.h"
SUMS="SHA256SUMS"
else
#echo "Unsupported CPU architecture: ${MACHINE}" >&2
exit 1
fi
elif [[ "${OS}" == 'Darwin' ]]; then
if [[ ${MACHINE} == 'x86_64' ]]; then
#echo "Using Asherah libraries for MacOS x86_64"
ARCHIVE="libasherah-darwin-x64.a"
HEADER="libasherah-darwin-x64-archive.h"
SUMS="SHA256SUMS-darwin"
elif [[ ${MACHINE} == 'arm64' ]]; then
#echo "Using Asherah libraries for MacOS arm64"
ARCHIVE="libasherah-darwin-arm64.a"
HEADER="libasherah-darwin-arm64-archive.h"
SUMS="SHA256SUMS-darwin"
else
echo "Unsupported CPU architecture: ${MACHINE}" >&2
exit 1
fi
else
echo "Unsupported CPU architecture"
echo "Unsupported operating system: ${OS}" >&2
exit 1
fi
else
echo "Unsupported operating system"
exit 1
fi

curl -s -L --fail --etag-save "${ARCHIVE}.etag" --etag-compare "${ARCHIVE}.etag" -O --retry 999 --retry-max-time 0 "https://github.com/godaddy/asherah-cobhan/releases/download/${ASHERAH_VERSION}/${ARCHIVE}" || echo "Failed to download ${ASHERAH_VERSION}/${ARCHIVE}"
echo "${ARCHIVE}" "${HEADER}" "${SUMS}" # Return value
}

# Parse script arguments
function parse_args {
local no_cache=false
while [[ $# -gt 0 ]]; do
case "$1" in
--no-cache)
no_cache=true
shift
;;
*)
echo "Unknown parameter: $1" >&2
exit 1
;;
esac
done
echo "${no_cache}" # Return value
}

# Function to determine the interval message
function interval_message {
local interval=$1
if (( interval % 60 == 0 )); then
echo "$((interval / 60)) minutes" # Return value
else
echo "$interval seconds" # Return value
fi
}

# Main function
function main {
echo "Downloading Asherah libraries"
source .asherah-version

# Parse arguments
local no_cache
no_cache=$(parse_args "$@")

# Detect OS and CPU architecture
read -r archive header sums < <(detect_os_and_cpu)
echo "Archive: $archive"
echo "Header: $header"
echo "Sums: $sums"
echo "Version: $ASHERAH_VERSION"

# Interpolate the URLs
url_prefix="https://github.com/godaddy/asherah-cobhan/releases/download/${ASHERAH_VERSION}"
file_names=("${archive}" "${header}" "${sums}")
file_urls=(
"${url_prefix}/${archive}"
"${url_prefix}/${header}"
"${url_prefix}/${sums}"
)

# Create the `lib` directory if it doesn't exist
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]}"
else
interval_str=$(interval_message "$CHECK_INTERVAL_SECONDS")
echo "${file_names[$i]} is up to date (checked within the last ${interval_str})"
fi
done

curl -s -L --fail --etag-save "${HEADER}.etag" --etag-compare "${HEADER}.etag" -O --retry 999 --retry-max-time 0 "https://github.com/godaddy/asherah-cobhan/releases/download/${ASHERAH_VERSION}/${HEADER}" || echo "Failed to download ${ASHERAH_VERSION}/${HEADER}"
# Verify checksums and copy files
verify_checksums "${archive}" "${header}" "${sums}"
copy_files "${archive}" "${header}"

curl -s -L --fail --etag-save "${SUMS}.etag" --etag-compare "${SUMS}.etag" -O --retry 999 --retry-max-time 0 "https://github.com/godaddy/asherah-cobhan/releases/download/${ASHERAH_VERSION}/${SUMS}" || echo "Failed to download ${ASHERAH_VERSION}/${SUMS}"
grep -e "${ARCHIVE}" -e "${HEADER}" "${SUMS}" > ./SHA256SUM
shasum -a 256 -c ./SHA256SUM || (echo 'SHA256 mismatch!' ; rm -f ./*.a ./*.h ; exit 1)
echo "Asherah libraries downloaded successfully"
}

cp -f "${ARCHIVE}" libasherah.a
cp -f "${HEADER}" libasherah.h
# Execute the main function
main "$@"
Loading

0 comments on commit 9b71bd9

Please sign in to comment.