1
0
mirror of https://github.com/TheLocehiliosan/yadm synced 2026-03-02 03:49:29 +00:00

select one of several checksum commands

This commit is contained in:
Martin Zuther
2019-12-28 22:35:55 +01:00
parent c190333fdf
commit 3c204119fb
2 changed files with 97 additions and 92 deletions

View File

@@ -27,63 +27,73 @@ IFS=$'\n'
YADM_ENCRYPT_INCLUDE_FILES=( $YADM_ENCRYPT_INCLUDE_FILES )
IFS="$OLD_IFS"
CHECKSUM_ALGORITHM="512"
CHECKSUM_ALGORITHM_NAME="SHA-512"
WARNING_MESSAGE="No checksums were created."
WARNING_MESSAGE="No checksums were created"
function print_warning_and_exit {
MESSAGE=$1
function get_checksum_command {
# check if "shasum" exists and supports the algorithm (which is
# tested by sending an empty string to "shasum")
if command -v "shasum" > /dev/null && echo -n | shasum --algorithm "256" &> /dev/null; then
echo "shasum --algorithm 256"
# check if "sha256sum" exists
elif command -v "sha256sum" > /dev/null; then
echo "sha256sum"
# check if "gsha256sum" exists
elif command -v "gsha256sum" > /dev/null; then
echo "gsha256sum"
else
# display warning in bright yellow
echo -e "\033[1;33m" >&2
echo -n "WARNING: \"shasum\", \"sha256sum\" and \"gsha256sum\" not found. $WARNING_MESSAGE." >&2
# set output color to yellow
echo -e "\033[1;33m"
echo "WARNING: $MESSAGE $WARNING_MESSAGE"
# reset output color
echo -e "\033[0m" >&2
# reset output color
echo -e "\033[0m"
# signal error
return 1
fi
}
# get checksum command
CHECKSUM_COMMAND=$(get_checksum_command)
ERROR_CODE=$?
# no command found
if [ $ERROR_CODE -ne 0 ]; then
# return original exit status of yadm command
exit "$YADM_HOOK_EXIT"
}
fi
function ensure_command {
COMMAND_NAME=$1
# check if command exists
if ! command -v "$COMMAND_NAME" > /dev/null; then
print_warning_and_exit "command \"$COMMAND_NAME\" not found."
fi
}
function ensure_algorithm {
# check if "shasum" supports algorithm by hashing an empty string
echo -n | shasum --algorithm "$CHECKSUM_ALGORITHM" &> /dev/null
if [ $? -ne 0 ]; then
print_warning_and_exit "\"shasum\" does not support $CHECKSUM_ALGORITHM_NAME."
fi
}
# check if "shasum" exists and supports algorithm
ensure_command shasum
ensure_algorithm
# empty checksum file
# empty (or create) checksum file
echo -n > "$YADM_CHECKSUMS"
# calculate checksums for encrypted files
for included in ${YADM_ENCRYPT_INCLUDE_FILES[*]}; do
shasum --algorithm $CHECKSUM_ALGORITHM "$included" >> "$YADM_CHECKSUMS"
# highlight any errors in red
echo -en "\033[0;31m"
# signal errors
if [ $? -ne 0 ]; then
exit $?
# calculate checksums
$CHECKSUM_COMMAND "$included" >> "$YADM_CHECKSUMS"
ERROR_CODE=$?
# reset output color
echo -ne "\033[0m"
# handle errors
if [ $ERROR_CODE -ne 0 ]; then
# display warning in bright yellow
echo -e "\033[1;33m" >&2
echo -n "WARNING: an error occurred. Please inspect the checksum file." >&2
# reset output color
echo -e "\033[0m" >&2
# exit and signal error
exit $ERROR_CODE
fi
done
echo "Wrote checksums: $YADM_CHECKSUMS ($CHECKSUM_ALGORITHM_NAME)"
# return exit status of the yadm command
# announce success and return original exit status of yadm command
echo "Wrote SHA-256 checksums: $YADM_CHECKSUMS"
exit "$YADM_HOOK_EXIT"