From 8c03b886c4d4b9bcfb1cb30b3704b8af561c2f45 Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Sun, 1 Oct 2023 06:33:43 +0100 Subject: Greatly simplify error handling in shell scripts Instead of having detailed error messages, run most commands through a function that calls err() under fault conditions. Where detail is still required, err() is still called manually. Where it isn't, the error message is simply whatever command was executed to cause the error. This results in a massive sloccount reduction for lbmk; specifically, 178 sloc reduction, or a 8.1% reduction. The total sloccount is now 2022, for shell scripts. Signed-off-by: Leah Rowe --- include/blobutil.sh | 12 ++++++------ include/err.sh | 9 +++++++++ include/mrc.sh | 18 +++++++----------- include/option.sh | 3 +-- 4 files changed, 23 insertions(+), 19 deletions(-) (limited to 'include') diff --git a/include/blobutil.sh b/include/blobutil.sh index 51f11de9..3b59ba2e 100755 --- a/include/blobutil.sh +++ b/include/blobutil.sh @@ -52,14 +52,14 @@ fetch() _dest="${5##*../}" _dl="${blobdir}/cache/${dlsum}" - mkdir -p "${_dl%/*}" || err "fetch: !mkdir ${_dl%/*}" + x_ mkdir -p "${_dl%/*}" dl_fail="y" vendor_checksum "${dlsum}" "${_dl}" && dl_fail="n" for url in "${dl}" "${dl_bkup}"; do [ "${dl_fail}" = "n" ] && break [ -z "${url}" ] && continue - rm -f "${_dl}" || err "fetch: !rm -f ${_dl}" + x_ rm -f "${_dl}" curl --location --retry 3 -A "${_ua}" "${url}" -o "${_dl}" || \ wget --tries 3 -U "${_ua}" "${url}" -O "${_dl}" || \ continue @@ -68,7 +68,7 @@ fetch() [ "${dl_fail}" = "y" ] && \ err "fetch ${dlsum}: matched file unavailable" - rm -Rf "${_dl}_extracted" || err "!rm ${_dl}_extracted" + x_ rm -Rf "${_dl}_extracted" mkdirs "${_dest}" "extract_${dl_type}" || return 0 eval "extract_${dl_type}" @@ -89,9 +89,9 @@ mkdirs() { [ -f "${1}" ] && \ printf "mkdirs ${1} ${2}: already downloaded\n" 1>&2 && return 1 - mkdir -p "${1%/*}" || err "mkdirs ${1} ${2}: !mkdir ${1%/*}" - rm -Rf "${appdir}" || err "mkdirs ${1} ${2}: can't remove ${appdir}" - mkdir -p "${appdir}/" || err "mkdirs ${1} ${2}: !mkdir ${appdir}" + x_ mkdir -p "${1%/*}" + x_ rm -Rf "${appdir}" + x_ mkdir -p "${appdir}/" extract_archive "${_dl}" "${appdir}" || \ [ "${2}" = "extract_e6400vga" ] || err "mkdirs ${1} ${2}: !extract" } diff --git a/include/err.sh b/include/err.sh index 9f7c63b0..466e427b 100755 --- a/include/err.sh +++ b/include/err.sh @@ -1,6 +1,15 @@ # SPDX-License-Identifier: MIT # SPDX-FileCopyrightText: 2022, 2023 Leah Rowe +xfail="" + +x_() { + [ $# -lt 1 ] || ${@} || err "${@}" +} +xx_() { + [ $# -lt 1 ] || ${@} || fail "${@}" +} + setvars() { _setvars="" diff --git a/include/mrc.sh b/include/mrc.sh index 21ce5f01..43a59d82 100755 --- a/include/mrc.sh +++ b/include/mrc.sh @@ -14,15 +14,14 @@ extract_mrc() _file="${_file%.zip}" ( - cd "${appdir}" || err "extract_mrc: !cd ${appdir}" + x_ cd "${appdir}" extract_partition ROOT-A "${_file}" root-a.ext2 extract_shellball root-a.ext2 chromeos-firmwareupdate-${MRC_board} extract_coreboot chromeos-firmwareupdate-${MRC_board} ) - "${cbfstool}" "${appdir}/"coreboot-*.bin extract -n mrc.bin \ - -f "${_dest}" -r RO_SECTION || \ - err "extract_mrc: could not fetch mrc.bin" + x_ "${cbfstool}" "${appdir}/"coreboot-*.bin extract -n mrc.bin \ + -f "${_dest}" -r RO_SECTION } extract_partition() @@ -39,9 +38,8 @@ extract_partition() START=$(( $( echo ${ROOTP} | cut -f2 -d\ | tr -d "B" ) )) SIZE=$(( $( echo ${ROOTP} | cut -f4 -d\ | tr -d "B" ) )) - dd if="${FILE}" of="${ROOTFS}" bs=${_bs} skip=$(( ${START} / ${_bs} )) \ - count=$(( ${SIZE} / ${_bs} )) || \ - err "extract_partition: can't extract root file system" + x_ dd if="${FILE}" of="${ROOTFS}" bs=${_bs} \ + skip=$(( ${START} / ${_bs} )) count=$(( ${SIZE} / ${_bs} )) } extract_shellball() @@ -62,8 +60,7 @@ extract_coreboot() printf "Extracting coreboot image\n" [ -f "${_shellball}" ] || \ err "extract_coreboot: shellball missing in google cros image" - sh "${_shellball}" --unpack "${_unpacked}" || \ - err "extract_coreboot: shellball exits with non-zero status" + x_ sh "${_shellball}" --unpack "${_unpacked}" # TODO: audit the f* out of that shellball, for each mrc version. # it has to be updated for each mrc update. we should ideally @@ -75,6 +72,5 @@ extract_coreboot() _version=$( cat "${_unpacked}/VERSION" | grep BIOS\ version: | \ cut -f2 -d: | tr -d \ ) - cp "${_unpacked}/bios.bin" "coreboot-${_version}.bin" || \ - err "extract_coreboot: cannot copy google cros rom" + x_ cp "${_unpacked}/bios.bin" "coreboot-${_version}.bin" } diff --git a/include/option.sh b/include/option.sh index 98f91c51..07ef83c8 100755 --- a/include/option.sh +++ b/include/option.sh @@ -38,6 +38,5 @@ scan_config() done << EOF $(eval "awk '${awkstr}' \"${revfile}\"") EOF - rm -f "${revfile}" || \ - "${_fail}" "scan_config: Cannot remove tmpfile" + rm -f "${revfile}" || "${_fail}" "scan_config: Cannot remove tmpfile" } -- cgit v1.2.1