From d9b9aa1884ac356ee1df38819d909289110f11b6 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Thu, 20 Jun 2024 13:44:09 +0200 Subject: [PATCH] helpers2.1: use positional args for file checksum helpers --- helpers/helpers.v2.1.d/apt | 2 +- helpers/helpers.v2.1.d/backup | 58 +++++++------------------------ helpers/helpers.v2.1.d/config | 25 ++++++++----- helpers/helpers.v2.1.d/go | 4 +-- helpers/helpers.v2.1.d/nginx | 6 ++-- helpers/helpers.v2.1.d/php | 2 +- helpers/helpers.v2.1.d/templating | 4 +-- helpers/helpers.v2.1.d/utils | 2 +- 8 files changed, 39 insertions(+), 64 deletions(-) diff --git a/helpers/helpers.v2.1.d/apt b/helpers/helpers.v2.1.d/apt index 71547abf6..520007e48 100644 --- a/helpers/helpers.v2.1.d/apt +++ b/helpers/helpers.v2.1.d/apt @@ -62,7 +62,7 @@ ynh_apt_install_dependencies() { if [[ -f "$old_php_finalphpconf" ]] then - ynh_backup_if_checksum_is_different --file="$old_php_finalphpconf" + ynh_backup_if_checksum_is_different "$old_php_finalphpconf" ynh_remove_fpm_config fi fi diff --git a/helpers/helpers.v2.1.d/backup b/helpers/helpers.v2.1.d/backup index b5047ddc5..42f7476b0 100644 --- a/helpers/helpers.v2.1.d/backup +++ b/helpers/helpers.v2.1.d/backup @@ -201,34 +201,19 @@ ynh_restore_everything() { done } +_ynh_file_checksum_exists() { + local file=$1 + local checksum_setting_name=checksum_${file//[\/ ]/_} # Replace all '/' and ' ' by '_' + [[ -n "$(ynh_app_setting_get --key=$checksum_setting_name)" ]] +} + # Calculate and store a file checksum into the app settings # -# usage: ynh_store_file_checksum --file=file -# | arg: -f, --file= - The file on which the checksum will performed, then stored. -# -# $app should be defined when calling this helper -# -# Requires YunoHost version 2.6.4 or higher. +# usage: ynh_store_file_checksum /path/to/file ynh_store_file_checksum() { - # ============ Argument parsing ============= - local -A args_array=([f]=file= [u]=update_only) - local file - local update_only - update_only="${update_only:-0}" - ynh_handle_getopts_args "$@" - # =========================================== - + local file=$1 local checksum_setting_name=checksum_${file//[\/ ]/_} # Replace all '/' and ' ' by '_' - # If update only, we don't save the new checksum if no old checksum exist - if [ $update_only -eq 1 ]; then - local checksum_value=$(ynh_app_setting_get --key=$checksum_setting_name) - if [ -z "${checksum_value}" ]; then - unset backup_file_checksum - return 0 - fi - fi - ynh_app_setting_set --key=$checksum_setting_name --value=$(md5sum "$file" | cut --delimiter=' ' --fields=1) if [ ${PACKAGE_CHECK_EXEC:-0} -eq 1 ]; then @@ -250,21 +235,12 @@ ynh_store_file_checksum() { # Verify the checksum and backup the file if it's different # -# usage: ynh_backup_if_checksum_is_different --file=file -# | arg: -f, --file= - The file on which the checksum test will be perfomed. -# | ret: the name of a backup file, or nothing +# usage: ynh_backup_if_checksum_is_different /path/to/file # # This helper is primarily meant to allow to easily backup personalised/manually # modified config files. -# -# Requires YunoHost version 2.6.4 or higher. ynh_backup_if_checksum_is_different() { - # ============ Argument parsing ============= - local -A args_array=([f]=file=) - local file - ynh_handle_getopts_args "$@" - # =========================================== - + local file=$1 local checksum_setting_name=checksum_${file//[\/ ]/_} # Replace all '/' and ' ' by '_' local checksum_value=$(ynh_app_setting_get --key=$checksum_setting_name) # backup_file_checksum isn't declare as local, so it can be reuse by ynh_store_file_checksum @@ -291,19 +267,9 @@ ynh_backup_if_checksum_is_different() { # Delete a file checksum from the app settings # -# usage: ynh_delete_file_checksum --file=file -# | arg: -f, --file= - The file for which the checksum will be deleted -# -# $app should be defined when calling this helper -# -# Requires YunoHost version 3.3.1 or higher. +# usage: ynh_delete_file_checksum /path/to/file ynh_delete_file_checksum() { - # ============ Argument parsing ============= - local -A args_array=([f]=file=) - local file - ynh_handle_getopts_args "$@" - # =========================================== - + local file=$1 local checksum_setting_name=checksum_${file//[\/ ]/_} # Replace all '/' and ' ' by '_' ynh_app_setting_delete --key=$checksum_setting_name } diff --git a/helpers/helpers.v2.1.d/config b/helpers/helpers.v2.1.d/config index 0988562b6..73b5d85b3 100644 --- a/helpers/helpers.v2.1.d/config +++ b/helpers/helpers.v2.1.d/config @@ -75,16 +75,19 @@ _ynh_app_config_apply_one() { fi local bind_file="$(echo "$bind" | sed s@__INSTALL_DIR__@${install_dir:-}@ | sed s/__APP__/$app/)" if [[ "${!short_setting}" == "" ]]; then - ynh_backup_if_checksum_is_different --file="$bind_file" + ynh_backup_if_checksum_is_different "$bind_file" ynh_safe_rm "$bind_file" - ynh_delete_file_checksum --file="$bind_file" + ynh_delete_file_checksum "$bind_file" ynh_print_info "File '$bind_file' removed" else - ynh_backup_if_checksum_is_different --file="$bind_file" + ynh_backup_if_checksum_is_different "$bind_file" if [[ "${!short_setting}" != "$bind_file" ]]; then cp "${!short_setting}" "$bind_file" fi - ynh_store_file_checksum --file="$bind_file" --update_only + if _ynh_file_checksum_exists "$bind_file" + then + ynh_store_file_checksum "$bind_file" + fi ynh_print_info "File '$bind_file' overwritten with ${!short_setting}" fi @@ -99,9 +102,12 @@ _ynh_app_config_apply_one() { ynh_die "For technical reasons, multiline text '${short_setting}' can't be stored automatically in a variable file, you have to create custom getter/setter" fi local bind_file="$(echo "$bind" | sed s@__INSTALL_DIR__@${install_dir:-}@ | sed s/__APP__/$app/)" - ynh_backup_if_checksum_is_different --file="$bind_file" + ynh_backup_if_checksum_is_different "$bind_file" echo "${!short_setting}" >"$bind_file" - ynh_store_file_checksum --file="$bind_file" --update_only + if _ynh_file_checksum_exists "$bind_file" + then + ynh_store_file_checksum "$bind_file" + fi ynh_print_info "File '$bind_file' overwritten with the content provided in question '${short_setting}'" # Set value into a kind of key/value file @@ -115,9 +121,12 @@ _ynh_app_config_apply_one() { bind_key_=${bind_key_:-$short_setting} local bind_file="$(echo "$bind" | cut -d: -f2 | sed s@__INSTALL_DIR__@${install_dir:-}@ | sed s/__APP__/$app/)" - ynh_backup_if_checksum_is_different --file="$bind_file" + ynh_backup_if_checksum_is_different "$bind_file" ynh_write_var_in_file --file="${bind_file}" --key="${bind_key_}" --value="${!short_setting}" --after="${bind_after}" - ynh_store_file_checksum --file="$bind_file" --update_only + if _ynh_file_checksum_exists "$bind_file" + then + ynh_store_file_checksum "$bind_file" + fi # We stored the info in settings in order to be able to upgrade the app ynh_app_setting_set --key=$short_setting --value="${!short_setting}" diff --git a/helpers/helpers.v2.1.d/go b/helpers/helpers.v2.1.d/go index 499dd51be..22797397e 100644 --- a/helpers/helpers.v2.1.d/go +++ b/helpers/helpers.v2.1.d/go @@ -191,7 +191,7 @@ _ynh_go_cleanup () { then # Remove goenv environment configuration ynh_print_info "Removing of goenv" - ynh_secure_remove --file="$goenv_install_dir" - ynh_secure_remove --file="/etc/profile.d/goenv.sh" + ynh_safe_rm "$goenv_install_dir" + ynh_safe_rm "/etc/profile.d/goenv.sh" fi } diff --git a/helpers/helpers.v2.1.d/nginx b/helpers/helpers.v2.1.d/nginx index b3b2836ae..3eb6bf212 100644 --- a/helpers/helpers.v2.1.d/nginx +++ b/helpers/helpers.v2.1.d/nginx @@ -28,7 +28,7 @@ ynh_config_add_nginx() { ynh_replace --match="^#root_path_only" --replace="" --file="$finalnginxconf" fi - ynh_store_file_checksum --file="$finalnginxconf" + ynh_store_file_checksum "$finalnginxconf" ynh_systemd_action --service=nginx --action=reload } @@ -56,8 +56,8 @@ ynh_config_change_url_nginx() { # ynh_config_add inside ynh_config_add_nginx because the path may have # changed if we're changing the domain too...) local old_nginx_conf_path=/etc/nginx/conf.d/$old_domain.d/$app.conf - ynh_backup_if_checksum_is_different --file="$old_nginx_conf_path" - ynh_delete_file_checksum --file="$old_nginx_conf_path" + ynh_backup_if_checksum_is_different "$old_nginx_conf_path" + ynh_delete_file_checksum "$old_nginx_conf_path" ynh_safe_rm "$old_nginx_conf_path" # Regen the nginx conf diff --git a/helpers/helpers.v2.1.d/php b/helpers/helpers.v2.1.d/php index c453c7427..b47259ac5 100644 --- a/helpers/helpers.v2.1.d/php +++ b/helpers/helpers.v2.1.d/php @@ -76,7 +76,7 @@ ynh_config_add_phpfpm() { if [[ -f "$old_php_finalphpconf" ]] then - ynh_backup_if_checksum_is_different --file="$old_php_finalphpconf" + ynh_backup_if_checksum_is_different "$old_php_finalphpconf" ynh_remove_fpm_config fi fi diff --git a/helpers/helpers.v2.1.d/templating b/helpers/helpers.v2.1.d/templating index 208af1be8..b7dc3bc4a 100644 --- a/helpers/helpers.v2.1.d/templating +++ b/helpers/helpers.v2.1.d/templating @@ -91,7 +91,7 @@ ynh_config_add() { ynh_die "The provided template $template doesn't exist" fi - ynh_backup_if_checksum_is_different --file="$destination" + ynh_backup_if_checksum_is_different "$destination" # Make sure to set the permissions before we copy the file # This is to cover a case where an attacker could have @@ -113,7 +113,7 @@ ynh_config_add() { ynh_replace_vars --file="$destination" fi - ynh_store_file_checksum --file="$destination" + ynh_store_file_checksum "$destination" } # Replace variables in a file diff --git a/helpers/helpers.v2.1.d/utils b/helpers/helpers.v2.1.d/utils index debdf1f90..3f985d227 100644 --- a/helpers/helpers.v2.1.d/utils +++ b/helpers/helpers.v2.1.d/utils @@ -422,7 +422,7 @@ _acceptable_path_to_delete() { fi } -# Remove a file or a directory securely +# Remove a file or a directory, checking beforehand that it's not a disastrous location to rm such as entire /var or /home # # usage: ynh_safe_rm path_to_remove #