diff options
author | Leah Rowe <leah@libreboot.org> | 2024-03-27 01:19:39 +0000 |
---|---|---|
committer | Leah Rowe <leah@libreboot.org> | 2024-03-27 01:50:31 +0000 |
commit | 6ebab10caa5be6fc1cfd244e745851687d4bd70d (patch) | |
tree | 0b983459c0cbc1b8998186975f9a571decb05592 /script/vendor/download | |
parent | 6b11f1b055466982747f65665506173a0b22557b (diff) |
safer, simpler error handling in lbmk
in shell scripts, a function named the same as a program included in
the $PATH will override that program. for example, you could make a
function called ls() and this would override the standand "ls".
in lbmk, a part of it was first trying to run the "fail" command,
deferring to "err", because some scripts call fail() which does
some minor cleanup before calling err.
in most cases, fail() is not defined, and it's possible that the user
could have a program called "fail" in their $PATH, the behaviour of
which we could not determine, and it could have disastrous effects.
lbmk error handling has been re-engineered in such a way that the
err function is defined in a variable, which defaults to err_ which
calls err_, so defined under include/err.sh.
in functions that require cleanup prior to error handling, a fail()
function is still defined, and err is overridden, thus:
err="fail"
this change has made xx_() obsolete, so now only x_ is used. the x_
function is a wrapper that can be used to run a command and exit with
non-zero status (from lbmk) if the command fails. the xx_ command
did the same thing, but called fail() which would have called err();
now everything is $err
example:
rm -f "$filename" || err "could not delete file"
this would now be:
rm -f "$filename" || $err "could not delete file"
overriding of err= must be done *after* including err.sh. for
example:
err="fail"
. "include/err.sh"
^ this is wrong. instead, one must do:
. "include/err.sh"
err="fail"
this is because err is set as a global variable under err.sh
the new error handling is much cleaner, and safer. it also reduces
the chance of mistakes such as: calling err when you meant to
call fail. this is because the standard way is now to call $err,
so you set err="fail" at the top of the script and all is well.
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'script/vendor/download')
-rwxr-xr-x | script/vendor/download | 54 |
1 files changed, 27 insertions, 27 deletions
diff --git a/script/vendor/download b/script/vendor/download index 42cb16ab..b40810cc 100755 --- a/script/vendor/download +++ b/script/vendor/download @@ -25,14 +25,14 @@ eval "$(setvars "" _b _dl EC_url EC_url_bkup EC_hash DL_hash DL_url DL_url_bkup main() { - [ $# -gt 0 ] || err "No argument given" + [ $# -gt 0 ] || $err "No argument given" board="${1}" boarddir="${cbcfgsdir}/${board}" _b="${board%%_*mb}" # shorthand (no duplication per rom size) check_defconfig "${boarddir}" && exit 0 detect_firmware && exit 0 - scan_config "${_b}" "config/vendor" "err" + scan_config "${_b}" "config/vendor" build_dependencies download_vendorfiles @@ -49,7 +49,7 @@ detect_firmware() set -- "${boarddir}/config/"* . "${1}" 2>/dev/null . "${boarddir}/target.cfg" 2>/dev/null - [ -z "$tree" ] && err "detect_firmware $boarddir: tree undefined" + [ -z "$tree" ] && $err "detect_firmware $boarddir: tree undefined" cbdir="src/coreboot/$tree" cbfstool="cbutils/$tree/cbfstool" @@ -110,8 +110,8 @@ fetch() dl_bkup="${3}" dlsum="${4}" [ "${5}" = "/dev/null" ] && return 0 - [ "${5# }" = "$5" ] || err "fetch: space not allowed in _dest: '$5'" - [ "${5#/}" = "$5" ] || err "fetch: absolute path not allowed: '$5'" + [ "${5# }" = "$5" ] || $err "fetch: space not allowed in _dest: '$5'" + [ "${5#/}" = "$5" ] || $err "fetch: absolute path not allowed: '$5'" _dest="${5##*../}" _dl="${vendir}/cache/${dlsum}" dl_fail="n" @@ -129,14 +129,14 @@ fetch() vendor_checksum "${dlsum}" "${_dl}" || dl_fail="n" done [ "${dl_fail}" = "y" ] && \ - err "fetch ${dlsum}: matched file unavailable" + $err "fetch ${dlsum}: matched file unavailable" x_ rm -Rf "${_dl}_extracted" mkdirs "${_dest}" "extract_${dl_type}" || return 0 eval "extract_${dl_type}" [ -f "${_dest}" ] && return 0 - err "extract_${dl_type} (fetch): missing file: '${_dest}'" + $err "extract_${dl_type} (fetch): missing file: '${_dest}'" } vendor_checksum() @@ -152,17 +152,17 @@ mkdirs() printf "mkdirs %s %s: already downloaded\n" "$1" "$2" 1>&2 return 1 fi - mkdir -p "${1%/*}" || err "mkdirs: !mkdir -p ${1%/*}" + mkdir -p "${1%/*}" || $err "mkdirs: !mkdir -p ${1%/*}" remkdir "${appdir}" extract_archive "${_dl}" "${appdir}" || \ [ "${2}" = "extract_e6400vga" ] || \ - err "mkdirs ${1} ${2}: !extract" + $err "mkdirs ${1} ${2}: !extract" } extract_intel_me() { [ ! -f "$mecleaner" ] && \ - err "extract_intel_me $cbdir: me_cleaner missing" + $err "extract_intel_me $cbdir: me_cleaner missing" _me="${PWD}/${_dest}" # must always be an absolute path cdir="${PWD}/${appdir}" # must always be an absolute path @@ -170,10 +170,10 @@ extract_intel_me() [ -f "${_me}" ] && return 0 sdir="$(mktemp -d)" - mkdir -p "$sdir" || err "extract_intel_me: !mkdir -p \"$sdir\"" + mkdir -p "$sdir" || $err "extract_intel_me: !mkdir -p \"$sdir\"" ( [ "${cdir#/a}" != "$cdir" ] && cdir="${cdir#/}" - cd "$cdir" || err "extract_intel_me: !cd \"$cdir\"" + cd "$cdir" || $err "extract_intel_me: !cd \"$cdir\"" for i in *; do [ -f "$_me" ] && break [ -L "$i" ] && continue @@ -195,7 +195,7 @@ extract_intel_me() cd "${cdir}" || : done ) - rm -Rf "${sdir}" || err "extract_intel_me: !rm -Rf ${sdir}" + rm -Rf "${sdir}" || $err "extract_intel_me: !rm -Rf ${sdir}" } extract_archive() @@ -207,44 +207,44 @@ extract_archive() extract_kbc1126ec() { [ ! -f "$kbc1126_ec_dump" ] && \ - err "extract_kbc1126ec $cbdir: kbc1126_ec_dump missing" + $err "extract_kbc1126ec $cbdir: kbc1126_ec_dump missing" ( x_ cd "${appdir}/" mv Rompaq/68*.BIN ec.bin || : if [ ! -f ec.bin ]; then unar -D ROM.CAB Rom.bin || unar -D Rom.CAB Rom.bin || \ - unar -D 68*.CAB Rom.bin || err "can't extract Rom.bin" + unar -D 68*.CAB Rom.bin || $err "can't extract Rom.bin" x_ mv Rom.bin ec.bin fi - [ -f ec.bin ] || err "extract_kbc1126_ec ${board}: can't extract" + [ -f ec.bin ] || $err "extract_kbc1126_ec ${board}: can't extract" "${kbc1126_ec_dump}" ec.bin || \ - err "extract_kbc1126_ec ${board}: can't extract ecfw1/2.bin" - ) || err "can't extract kbc1126 ec firmware" + $err "extract_kbc1126_ec ${board}: can't extract ecfw1/2.bin" + ) || $err "can't extract kbc1126 ec firmware" ec_ex="y" for i in 1 2; do [ -f "${appdir}/ec.bin.fw${i}" ] || ec_ex="n" done [ "${ec_ex}" = "y" ] || \ - err "extract_kbc1126_ec ${board}: didn't extract ecfw1/2.bin" + $err "extract_kbc1126_ec ${board}: didn't extract ecfw1/2.bin" cp "${appdir}/"ec.bin.fw* "${_dest%/*}/" || \ - err "extract_kbc1126_ec ${board}: can't copy ec binaries" + $err "extract_kbc1126_ec ${board}: can't copy ec binaries" } extract_e6400vga() { for v in E6400_VGA_offset E6400_VGA_romname; do - eval "[ -z \"\$$v\" ] && err \"extract_e6400vga: $v undefined\"" + eval "[ -z \"\$$v\" ] && $err \"extract_e6400vga: $v undefined\"" done tail -c +$E6400_VGA_offset "$_dl" | gunzip > "$appdir/bios.bin" || : ( x_ cd "${appdir}" - [ -f "bios.bin" ] || err "extract_e6400vga: can't extract bios.bin" + [ -f "bios.bin" ] || $err "extract_e6400vga: can't extract bios.bin" "${e6400_unpack}" bios.bin || printf "TODO: fix dell extract util\n" [ -f "${E6400_VGA_romname}" ] || \ - err "extract_e6400vga: can't extract vga rom from bios.bin" - ) || err "can't extract e6400 vga rom" + $err "extract_e6400vga: can't extract vga rom from bios.bin" + ) || $err "can't extract e6400 vga rom" cp "${appdir}/${E6400_VGA_romname}" "${_dest}" || \ - err "extract_e6400vga ${board}: can't copy vga rom to ${_dest}" + $err "extract_e6400vga ${board}: can't copy vga rom to ${_dest}" } extract_sch5545ec() @@ -258,9 +258,9 @@ extract_sch5545ec() # this makes the file defined by _sch5545ec_fw available to copy "${uefiextract}" "${_bios}" || \ - err "extract_sch5545ec: cannot extract from uefi image" + $err "extract_sch5545ec: cannot extract from uefi image" cp "${_sch5545ec_fw}" "${_dest}" || \ - err "extract_sch5545ec: cannot copy sch5545ec firmware file" + $err "extract_sch5545ec: cannot copy sch5545ec firmware file" } main $@ |