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
No known key found for this signature in database
GPG Key ID: 47D3346D758A37D8
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"

View File

@ -27,72 +27,67 @@ IFS=$'\n'
YADM_ENCRYPT_INCLUDE_FILES=( $YADM_ENCRYPT_INCLUDE_FILES )
IFS="$OLD_IFS"
CHECKSUM_ALGORITHM="512"
CHECKSUM_ALGORITHM_NAME="SHA-512"
WARNING_MESSAGE="Checksums were not verified."
WARNING_MESSAGE="Checksums were not verified"
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"
exit "$YADM_HOOK_EXIT"
}
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."
# signal error
return 1
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 whether file with checksums exists
# if there is no checksum file, exit with original status of yadm
# command
if [ ! -f "$YADM_CHECKSUMS" ]; then
# return exit status of the yadm command
exit "$YADM_HOOK_EXIT"
fi
# check if "shasum" exists and supports algorithm
ensure_command shasum
ensure_algorithm
# check encrypted files for differences and capture output
YADM_CHECKSUM_OUTPUT=$(shasum --algorithm "$CHECKSUM_ALGORITHM" --check "$YADM_CHECKSUMS" 2> /dev/null)
# get checksum command
CHECKSUM_COMMAND=$(get_checksum_command)
ERROR_CODE=$?
# some checksums do not match
# no command found
if [ $ERROR_CODE -ne 0 ]; then
# return original exit status of yadm command
exit "$YADM_HOOK_EXIT"
fi
# check encrypted files for differences and capture output and error
# messages
YADM_CHECKSUM_OUTPUT=$($CHECKSUM_COMMAND --check "$YADM_CHECKSUMS" 2>&1)
ERROR_CODE=$?
# handle mismatched checksums and errors
if [ $ERROR_CODE -ne 0 ]; then
echo
echo "Some $CHECKSUM_ALGORITHM_NAME sums do not match:"
echo "Some SHA-256 sums do not match (or an error occurred):"
# set output color to red
# display differing files and errors (highlighted in red)
echo -e "\033[0;31m"
# display mismatching files
while IFS= read -r line; do
# try to beautify output
# try to beautify output (requires "grep" and "sed")
if command -v grep > /dev/null && command -v sed > /dev/null; then
echo "$line" | grep -iv "\sok$" | sed 's/^/ / ; s/: FAILED$//'
echo "$line" | grep -iv "\sok$" | sed 's/^/ / ; s/: FAILED$// ; /^.*WARNING:.*did NOT match$/ d'
else
echo "$line"
fi
@ -100,8 +95,8 @@ if [ $ERROR_CODE -ne 0 ]; then
# reset output color
echo -e "\033[0m"
echo "Consider running either \"yadm encrypt\" or \"yadm decrypt\"."
# signal error
exit $ERROR_CODE
# display advice for differing files and signal error
echo "Consider running either \"yadm encrypt\" or \"yadm decrypt\"."
exit $ERROR_CODE
fi