From 84f23d593634139ee8273cf98dd0be16ba899a0c Mon Sep 17 00:00:00 2001 From: polarathene <5098581+polarathene@users.noreply.github.com> Date: Wed, 24 Jan 2024 18:26:29 +1300 Subject: [PATCH] fix: Ensure parsed config has final newline appended (when possible) This functionality was handled in `accounts.sh` via a similar sed command (that the linked references also offer). `printf` is better for this, no shellcheck comment required either. We additionally don't attempt to modify files that are read-only. --- target/scripts/helpers/accounts.sh | 8 -------- target/scripts/helpers/utils.sh | 24 ++++++++++++++++++------ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/target/scripts/helpers/accounts.sh b/target/scripts/helpers/accounts.sh index 7499a2ec..16a066a7 100644 --- a/target/scripts/helpers/accounts.sh +++ b/target/scripts/helpers/accounts.sh @@ -25,10 +25,6 @@ function _create_accounts() { _log 'trace' "Regenerating postfix user list" echo "# WARNING: this file is auto-generated. Modify ${DATABASE_ACCOUNTS} to edit the user list." > /etc/postfix/vmailbox - # checking that ${DATABASE_ACCOUNTS} ends with a newline - # shellcheck disable=SC1003 - sed -i -e '$a\' "${DATABASE_ACCOUNTS}" - chown dovecot:dovecot "${DOVECOT_USERDB_FILE}" chmod 640 "${DOVECOT_USERDB_FILE}" @@ -163,10 +159,6 @@ function _create_masters() { _log 'trace' "Regenerating dovecot masters list" - # checking that ${DATABASE_DOVECOT_MASTERS} ends with a newline - # shellcheck disable=SC1003 - sed -i -e '$a\' "${DATABASE_DOVECOT_MASTERS}" - chown dovecot:dovecot "${DOVECOT_MASTERDB_FILE}" chmod 640 "${DOVECOT_MASTERDB_FILE}" diff --git a/target/scripts/helpers/utils.sh b/target/scripts/helpers/utils.sh index 2b16b4d5..239e3b34 100644 --- a/target/scripts/helpers/utils.sh +++ b/target/scripts/helpers/utils.sh @@ -17,15 +17,27 @@ function _escape_for_sed() { # Returns input after filtering out lines that are: # empty, white-space, comments (`#` as the first non-whitespace character) function _get_valid_lines_from_file() { - # Correctly detect missing final newline: - # https://stackoverflow.com/questions/38746/how-to-detect-file-ends-in-newline#comment82380232_25749716 - if [[ $(tail -c1 "${1}" | wc -l) -gt 0 ]]; then - _log 'warn' "${1} is missing a final newline. The last line will not be processed!" - fi - + _append_final_newline_if_missing "${1}" grep --extended-regexp --invert-match "^\s*$|^\s*#" "${1}" || true } +function _append_final_newline_if_missing() { + # Correctly detect a missing final newline and fix it: + # https://stackoverflow.com/questions/38746/how-to-detect-file-ends-in-newline#comment82380232_25749716 + # https://unix.stackexchange.com/questions/31947/how-to-add-a-newline-to-the-end-of-a-file/441200#441200 + # https://unix.stackexchange.com/questions/159557/how-to-non-invasively-test-for-write-access-to-a-file + if [[ $(tail -c1 "${1}" | wc -l) -gt 0 ]]; then + # Avoid fixing when the destination is read-only: + if [[ test -w "${1}" ]]; then + printf '\n' >> "${1}" + + _log 'warn' "${1} was missing a final newline. This has been fixed." + else + _log 'warn' "${1} is missing a final newline. The last line will not be processed!" + fi + fi +} + # Provide the name of an environment variable to this function # and it will return its value stored in /etc/dms-settings function _get_dms_env_value() {