select one of several checksum commands
This commit is contained in:
parent
c190333fdf
commit
3c204119fb
2 changed files with 97 additions and 92 deletions
|
@ -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 print_warning_and_exit {
|
function get_checksum_command {
|
||||||
MESSAGE=$1
|
# 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
|
# reset output color
|
||||||
echo -e "\033[1;33m"
|
echo -e "\033[0m" >&2
|
||||||
echo "WARNING: $MESSAGE $WARNING_MESSAGE"
|
|
||||||
|
|
||||||
# reset output color
|
# signal error
|
||||||
echo -e "\033[0m"
|
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"
|
exit "$YADM_HOOK_EXIT"
|
||||||
}
|
fi
|
||||||
|
|
||||||
|
# empty (or create) checksum file
|
||||||
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
|
|
||||||
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"
|
||||||
|
|
||||||
# signal errors
|
# calculate checksums
|
||||||
if [ $? -ne 0 ]; then
|
$CHECKSUM_COMMAND "$included" >> "$YADM_CHECKSUMS"
|
||||||
exit $?
|
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
|
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 print_warning_and_exit {
|
function get_checksum_command {
|
||||||
MESSAGE=$1
|
# 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
|
# reset output color
|
||||||
echo -e "\033[1;33m"
|
echo -e "\033[0m" >&2
|
||||||
echo "WARNING: $MESSAGE $WARNING_MESSAGE"
|
|
||||||
|
|
||||||
# reset output color
|
# signal error
|
||||||
echo -e "\033[0m"
|
return 1
|
||||||
|
|
||||||
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."
|
|
||||||
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
|
|
||||||
|
|
||||||
# check encrypted files for differences and capture output
|
|
||||||
YADM_CHECKSUM_OUTPUT=$(shasum --algorithm "$CHECKSUM_ALGORITHM" --check "$YADM_CHECKSUMS" 2> /dev/null)
|
|
||||||
ERROR_CODE=$?
|
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
|
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…
Reference in a new issue