From c190333fdf6005e16dc85b3803e72d64019239ed Mon Sep 17 00:00:00 2001 From: Martin Zuther Date: Sat, 28 Dec 2019 16:09:19 +0100 Subject: [PATCH] correctly export array to subscript * fix shellcheck errors and warnings --- contrib/hooks/post_encrypt | 21 ++++++++++++++------- contrib/hooks/post_status | 23 +++++++++++++++-------- yadm | 16 ++++++++++++++-- 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/contrib/hooks/post_encrypt b/contrib/hooks/post_encrypt index c4808c8..ae9874c 100755 --- a/contrib/hooks/post_encrypt +++ b/contrib/hooks/post_encrypt @@ -17,9 +17,16 @@ # along with this program. If not, see . -YADM_DIRECTORY=$(dirname $YADM_HOOK_REPO) +YADM_DIRECTORY=$(dirname "$YADM_HOOK_REPO") YADM_CHECKSUMS=$YADM_DIRECTORY/files.checksums +# unpack exported array; filenames including a newline character (\n) +# are NOT supported +OLD_IFS="$IFS" +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." @@ -35,7 +42,7 @@ function print_warning_and_exit { # reset output color echo -e "\033[0m" - exit $YADM_HOOK_EXIT + exit "$YADM_HOOK_EXIT" } @@ -50,7 +57,7 @@ function ensure_command { function ensure_algorithm { - # check if "shasum" supports algorithm + # check if "shasum" supports algorithm by hashing an empty string echo -n | shasum --algorithm "$CHECKSUM_ALGORITHM" &> /dev/null if [ $? -ne 0 ]; then @@ -64,11 +71,11 @@ ensure_command shasum ensure_algorithm # empty checksum file -echo -n > $YADM_CHECKSUMS +echo -n > "$YADM_CHECKSUMS" # calculate checksums for encrypted files -for included in "${YADM_ENCRYPT_INCLUDE_FILES[@]}"; do - shasum --algorithm $CHECKSUM_ALGORITHM $included >> $YADM_CHECKSUMS +for included in ${YADM_ENCRYPT_INCLUDE_FILES[*]}; do + shasum --algorithm $CHECKSUM_ALGORITHM "$included" >> "$YADM_CHECKSUMS" # signal errors if [ $? -ne 0 ]; then @@ -79,4 +86,4 @@ done echo "Wrote checksums: $YADM_CHECKSUMS ($CHECKSUM_ALGORITHM_NAME)" # return exit status of the yadm command -exit $YADM_HOOK_EXIT +exit "$YADM_HOOK_EXIT" diff --git a/contrib/hooks/post_status b/contrib/hooks/post_status index 1190785..5e91c9e 100755 --- a/contrib/hooks/post_status +++ b/contrib/hooks/post_status @@ -17,9 +17,16 @@ # along with this program. If not, see . -YADM_DIRECTORY=$(dirname $YADM_HOOK_REPO) +YADM_DIRECTORY=$(dirname "$YADM_HOOK_REPO") YADM_CHECKSUMS=$YADM_DIRECTORY/files.checksums +# unpack exported array; filenames including a newline character (\n) +# are NOT supported +OLD_IFS="$IFS" +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." @@ -35,7 +42,7 @@ function print_warning_and_exit { # reset output color echo -e "\033[0m" - exit $YADM_HOOK_EXIT + exit "$YADM_HOOK_EXIT" } @@ -50,7 +57,7 @@ function ensure_command { function ensure_algorithm { - # check if "shasum" supports algorithm + # check if "shasum" supports algorithm by hashing an empty string echo -n | shasum --algorithm "$CHECKSUM_ALGORITHM" &> /dev/null if [ $? -ne 0 ]; then @@ -60,9 +67,9 @@ function ensure_algorithm { # 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 # check if "shasum" exists and supports algorithm @@ -70,7 +77,7 @@ 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) +YADM_CHECKSUM_OUTPUT=$(shasum --algorithm "$CHECKSUM_ALGORITHM" --check "$YADM_CHECKSUMS" 2> /dev/null) ERROR_CODE=$? # some checksums do not match @@ -85,9 +92,9 @@ if [ $ERROR_CODE -ne 0 ]; then while IFS= read -r line; do # try to beautify output 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$//' else - echo $line + echo "$line" fi done <<< "$YADM_CHECKSUM_OUTPUT" diff --git a/yadm b/yadm index 0410482..90b2672 100755 --- a/yadm +++ b/yadm @@ -1577,7 +1577,11 @@ function invoke_hook() { YADM_HOOK_FULL_COMMAND=$FULL_COMMAND YADM_HOOK_REPO=$YADM_REPO YADM_HOOK_WORK=$YADM_WORK - YADM_ENCRYPT_INCLUDE_FILES=${ENCRYPT_INCLUDE_FILES[@]} + + # pack array to export it; filenames including a newline character (\n) + # are NOT supported + YADM_ENCRYPT_INCLUDE_FILES=$(join_string $'\n' "${ENCRYPT_INCLUDE_FILES[@]}") + export YADM_HOOK_COMMAND export YADM_HOOK_EXIT export YADM_HOOK_FULL_COMMAND @@ -1821,6 +1825,13 @@ function auto_bootstrap() { } +# ****** Helper Functions ****** + +function join_string { + local IFS="$1" + echo "${*:2}" +} + # ****** Prerequisites Functions ****** function require_archive() { @@ -1888,7 +1899,7 @@ function readlink_available() { return 1 } -# ****** Directory tranlations ****** +# ****** Directory translations ****** function unix_path() { # for paths used by bash/yadm @@ -1908,6 +1919,7 @@ function mixed_path() { } # ****** echo replacements ****** + function echo() { IFS=' ' printf '%s\n' "$*"