From 1113e90c0aca57cdd7176ec98dab70ef0717c649 Mon Sep 17 00:00:00 2001 From: ale5000 <15793015+ale5000-git@users.noreply.github.com> Date: Sat, 25 Jan 2025 19:06:20 +0100 Subject: [PATCH] Improve disk space / filesize checks, ref #107 --- zip-content/customize.sh | 7 +++--- zip-content/inc/common-functions.sh | 33 ++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/zip-content/customize.sh b/zip-content/customize.sh index b9d8dd7a..72c93aa1 100644 --- a/zip-content/customize.sh +++ b/zip-content/customize.sh @@ -338,13 +338,14 @@ fi "${OUR_BB:?}" mkdir -p "${TMP_PATH:?}" || ui_error "Failed to create the temp folder" set_perm_safe 0 0 0755 "${TMP_PATH:?}" -PREVIOUS_PATH="${PATH}" +PREVIOUS_PATH="${PATH:-%empty}" DEVICE_GETPROP="$(command -v getprop)" || DEVICE_GETPROP='' +DEVICE_STAT="$(command -v stat)" || DEVICE_STAT='' DEVICE_MOUNT="$(command -v mount)" || DEVICE_MOUNT='' DEVICE_PM="$(command -v pm)" || DEVICE_PM='' DEVICE_AM="$(command -v am)" || DEVICE_AM='' -readonly PREVIOUS_PATH DEVICE_GETPROP DEVICE_MOUNT DEVICE_PM DEVICE_AM -export PREVIOUS_PATH DEVICE_GETPROP DEVICE_MOUNT DEVICE_PM DEVICE_AM +readonly PREVIOUS_PATH DEVICE_GETPROP DEVICE_STAT DEVICE_MOUNT DEVICE_PM DEVICE_AM +export PREVIOUS_PATH DEVICE_GETPROP DEVICE_STAT DEVICE_MOUNT DEVICE_PM DEVICE_AM if test "${TEST_INSTALL:-false}" = 'false'; then create_dir_safe "${TMP_PATH:?}/bin" diff --git a/zip-content/inc/common-functions.sh b/zip-content/inc/common-functions.sh index c724951a..bb0684aa 100644 --- a/zip-content/inc/common-functions.sh +++ b/zip-content/inc/common-functions.sh @@ -1124,11 +1124,38 @@ _is_free_space_error() return 1 # NOT found } +get_size_of_file() +{ + local _stat_result + + if _stat_result="$(stat 2> /dev/null -c '%s' -- "${1:?}")"; then + : # OK + elif test -n "${DEVICE_STAT?}" && _stat_result="$(PATH="${PREVIOUS_PATH:?}" "${DEVICE_STAT:?}" -c '%s' -- "${1:?}")"; then + : # OK + else + _stat_result='' + fi + + if test -n "${_stat_result?}" && printf '%s\n' "${_stat_result:?}"; then + return 0 + fi + + return 1 +} + get_free_disk_space_of_partition() { local _stat_result - if _stat_result="$(PATH="${PATH:-%empty}:${PREVIOUS_PATH:-%empty}" stat -f -c '%f * %S' -- "${1:?}")" && test -n "${_stat_result?}" && printf '%s\n' "$((_stat_result))"; then + if _stat_result="$(stat 2> /dev/null -f -c '%f * %S' -- "${1:?}")"; then + : # OK + elif test -n "${DEVICE_STAT?}" && _stat_result="$(PATH="${PREVIOUS_PATH:?}" "${DEVICE_STAT:?}" -f -c '%f * %S' -- "${1:?}")"; then + : # OK + else + _stat_result='' + fi + + if test -n "${_stat_result?}" && printf '%s\n' "$((_stat_result))"; then return 0 fi @@ -1204,7 +1231,7 @@ verify_disk_space() if _free_space_bytes="$(get_free_disk_space_of_partition "${1:?}")" && test -n "${_free_space_bytes?}"; then ui_msg "Free disk space: $(convert_bytes_to_mb "${_free_space_bytes:?}" || :) MB ($(convert_bytes_to_human_readable_format "${_free_space_bytes:?}" || :))" else - ui_warning "Unable to get free disk space, output for '${1?}' => $(PATH="${PATH:-%empty}:${PREVIOUS_PATH:-%empty}" stat -f -c '%f * %S' -- "${1:?}" || :)" + ui_warning "Unable to get free disk space, output for '${1?}' => $(stat 2> /dev/null -f -c '%f * %S' -- "${1:?}" || :)" _free_space_bytes='-1' fi @@ -1947,7 +1974,7 @@ setup_app() fi move_rename_file "${TMP_PATH:?}/origin/${_dir:?}/${_filename:?}.apk" "${TMP_PATH:?}/files/${_output_dir:?}/${_output_name:?}.apk" || ui_error "Failed to setup the app => '${_vanity_name?}'" - if test "${CURRENTLY_ROLLBACKING:-false}" != 'true' && test "${_optional:?}" = 'true' && test "$(stat -c '%s' -- "${TMP_PATH:?}/files/${_output_dir:?}/${_output_name:?}.apk" || printf '0' || true)" -gt 300000; then + if test "${CURRENTLY_ROLLBACKING:-false}" != 'true' && test "${_optional:?}" = 'true' && test "$(get_size_of_file "${TMP_PATH:?}/files/${_output_dir:?}/${_output_name:?}.apk" || printf '0' || :)" -gt 300000; then _installed_file_list="${_installed_file_list#|}" printf '%s\n' "${_vanity_name:?}|${_output_dir:?}/${_output_name:?}.apk|${_installed_file_list?}" 1>> "${TMP_PATH:?}/processed-${_dir:?}s.log" || ui_error "Failed to update processed-${_dir?}s.log" fi