From d18d1c2cae2c1c43c56d5c10adb469266d1eb08b Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Sat, 3 May 2025 04:16:10 +0100 Subject: lbmk: unified execution on find commands We have a lot of places in lbmk where the output of find is used, and then some function is executed on the result. This is messy, and bloats several of these functions. Now this is unified, into a new function: fx_ What fx_ does is execute a given function, for each result found, with the arguments for a find command appended. For example: find -name ".git" If you wanted to do: foo "$arg" Where "arg" is a search result from find, and you wanted to execute "foo" on each one, you would do: fx_ foo -name ".git" The find utility does have an -exec feature, but I've found that it only works for executables, not functions. fx_ does not return errors, so "foo" in this example would have to do its own error handling. Signed-off-by: Leah Rowe --- include/inject.sh | 43 ++++++++++++++++++------------------------- include/lib.sh | 11 +++++++++++ 2 files changed, 29 insertions(+), 25 deletions(-) (limited to 'include') diff --git a/include/inject.sh b/include/inject.sh index e5282861..0886adc8 100644 --- a/include/inject.sh +++ b/include/inject.sh @@ -282,19 +282,18 @@ extract_tbfw() { chkvars TBFW_size # size in bytes, matching TBFW's flash IC x_ mkdir -p tmp - x_ rm -f tmp/tb.bin - find "$appdir" -type f -name "TBT.bin" > "tmp/tb.txt" || \ - $err "extract_tbfw $_dest: Can't extract TBT.bin - $dontflash" - while read -r f; do - [ -f "$f" ] || continue - [ -L "$f" ] && continue - x_ cp "$f" "tmp/tb.bin" - break - done < "tmp/tb.txt" + + x_ rm -f tmp/tb.bin && fx_ copy_tbfw "$appdir" -type f -name "TBT.bin" + x_ dd if=/dev/null of=tmp/tb.bin bs=1 seek=$TBFW_size x_ cp "tmp/tb.bin" "$_dest" } +copy_tbfw() +{ + [ -f "$1" ] && [ ! -L "$1" ] && x_ cp "$1" "tmp/tb.bin" && return 1; : +} + extract_fspm() { copy_fsp M; : @@ -419,16 +418,10 @@ patch_release_roms() hashfile="$_hashes" && break; : done - x_ mkdir -p "tmp" && [ -L "tmp/rom.list" ] && \ - $err "'$archive' -> tmp/rom.list is a symlink - $dontflash" - - find "$tmpromdir" -maxdepth 1 -type f -name "*.rom" > "tmp/rom.list" \ - || $err "'$archive' -> Can't make tmp/rom.list - $dontflash" + x_ mkdir -p "tmp" if readkconfig; then - while read -r _xrom ; do - process_release_rom "$_xrom" || break - done < "tmp/rom.list" + fx_ prep_rom "$tmpromdir" -maxdepth 1 -type f -name "*.rom" [ "$nukemode" != "nuke" ] || \ printf "Make sure you inserted vendor files: %s\n" \ "$vguide" > "$tmpromdir/README.md" || : @@ -462,7 +455,7 @@ patch_release_roms() "$archive" || $err "'$archive' -> Can't overwrite - $dontflash"; : } -process_release_rom() +prep_rom() { _xrom="$1" _xromname="${1##*/}" @@ -578,13 +571,7 @@ modify_mac() [ "$new_mac" != "restore" ] && x_ make -C util/nvmutil && \ x_ "$nvm" tmp/gbe setmac "$new_mac" - find "$tmpromdir" -maxdepth 1 -type f -name "*.rom" > "tmp/rom.list" \ - || $err "'$archive' -> Can't make tmp/rom.list - $dontflash" - - while read -r _xrom; do - e "$_xrom" f && xchanged="y" && x_ \ - "$ifdtool" $ifdprefix -i GbE:tmp/gbe "$_xrom" -O "$_xrom" - done < "tmp/rom.list" + fx_ newmac "$tmpromdir" -maxdepth 1 -type f -name "*.rom" printf "\nGbE NVM written to '%s':\n" "$archive" x_ "$nvm" tmp/gbe dump | grep -v "bytes read from file" || : @@ -593,3 +580,9 @@ modify_mac() printf "\nDefault GbE file '%s' written, unmodified.\n" \ "${CONFIG_GBE_BIN_PATH##*../}"; : } + +newmac() +{ + e "$1" f && xchanged="y" && x_ \ + "$ifdtool" $ifdprefix -i GbE:tmp/gbe "$1" -O "$1"; : +} diff --git a/include/lib.sh b/include/lib.sh index bef2a16f..507a37c1 100644 --- a/include/lib.sh +++ b/include/lib.sh @@ -128,6 +128,17 @@ setvars() printf "%s\n" "${_setvars% }" } +fx_() +{ + fd="`mktemp`" + xx="$1" && shift 1 + find "$@" | sort > "$fd" || $err "!find $(echo "$@") > \"$fd\"" + while read -r fx; do + "$xx" "$fx" || break; : + done < "$fd" + x_ rm -f "$fd" +} + x_() { [ $# -lt 1 ] || "$@" || $err "Unhandled error for: $(echo "$@")"; : -- cgit v1.2.1