select one of several checksum commands

pull/188/head
Martin Zuther 5 years ago
parent c190333fdf
commit 3c204119fb
No known key found for this signature in database
GPG Key ID: 47D3346D758A37D8

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

@ -27,72 +27,67 @@ IFS=$'\n'
YADM_ENCRYPT_INCLUDE_FILES=( $YADM_ENCRYPT_INCLUDE_FILES ) YADM_ENCRYPT_INCLUDE_FILES=( $YADM_ENCRYPT_INCLUDE_FILES )
IFS="$OLD_IFS" IFS="$OLD_IFS"
CHECKSUM_ALGORITHM="512" WARNING_MESSAGE="Checksums were not verified"
CHECKSUM_ALGORITHM_NAME="SHA-512"
WARNING_MESSAGE="Checksums were not verified."
function get_checksum_command {
# check if "shasum" exists and supports the algorithm (which is
function print_warning_and_exit { # tested by sending an empty string to "shasum")
MESSAGE=$1 if command -v "shasum" > /dev/null && echo -n | shasum --algorithm "256" &> /dev/null; then
echo "shasum --algorithm 256"
# set output color to yellow # check if "sha256sum" exists
echo -e "\033[1;33m" elif command -v "sha256sum" > /dev/null; then
echo "WARNING: $MESSAGE $WARNING_MESSAGE" echo "sha256sum"
# check if "gsha256sum" exists
# reset output color elif command -v "gsha256sum" > /dev/null; then
echo -e "\033[0m" echo "gsha256sum"
else
exit "$YADM_HOOK_EXIT" # display warning in bright yellow
} echo -e "\033[1;33m" >&2
echo -n "WARNING: \"shasum\", \"sha256sum\" and \"gsha256sum\" not found. $WARNING_MESSAGE." >&2
function ensure_command { # reset output color
COMMAND_NAME=$1 echo -e "\033[0m" >&2
# check if command exists # signal error
if ! command -v "$COMMAND_NAME" > /dev/null; then return 1
print_warning_and_exit "command \"$COMMAND_NAME\" not found."
fi fi
} }
function ensure_algorithm { # if there is no checksum file, exit with original status of yadm
# check if "shasum" supports algorithm by hashing an empty string # command
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 [ ! -f "$YADM_CHECKSUMS" ]; then if [ ! -f "$YADM_CHECKSUMS" ]; then
# return exit status of the yadm command
exit "$YADM_HOOK_EXIT" exit "$YADM_HOOK_EXIT"
fi fi
# check if "shasum" exists and supports algorithm # get checksum command
ensure_command shasum CHECKSUM_COMMAND=$(get_checksum_command)
ensure_algorithm ERROR_CODE=$?
# 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 # check encrypted files for differences and capture output and error
YADM_CHECKSUM_OUTPUT=$(shasum --algorithm "$CHECKSUM_ALGORITHM" --check "$YADM_CHECKSUMS" 2> /dev/null) # messages
YADM_CHECKSUM_OUTPUT=$($CHECKSUM_COMMAND --check "$YADM_CHECKSUMS" 2>&1)
ERROR_CODE=$? ERROR_CODE=$?
# some checksums do not match # handle mismatched checksums and errors
if [ $ERROR_CODE -ne 0 ]; then if [ $ERROR_CODE -ne 0 ]; then
echo 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" echo -e "\033[0;31m"
# display mismatching files
while IFS= read -r line; do 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 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 else
echo "$line" echo "$line"
fi fi
@ -100,8 +95,8 @@ if [ $ERROR_CODE -ne 0 ]; then
# reset output color # reset output color
echo -e "\033[0m" echo -e "\033[0m"
echo "Consider running either \"yadm encrypt\" or \"yadm decrypt\"."
# signal error # display advice for differing files and signal error
exit $ERROR_CODE echo "Consider running either \"yadm encrypt\" or \"yadm decrypt\"."
exit $ERROR_CODE
fi fi

Loading…
Cancel
Save