From 7bd90f6357bf657f21daa2a7e29eed499bea0228 Mon Sep 17 00:00:00 2001 From: TomIO Date: Wed, 31 Jul 2024 14:06:54 +0200 Subject: [PATCH] fix(scripts/termux_step_install_license): fix logic error Move counter advance to the end of the while loop. This fixes a logic error where the counter wouldn't advance if the first license was "generic", and the second one was author specific. This would cause `cp` to attempt to copy through the dangling symlink to the generic license in `termux-licenses` which fails and dies. --- scripts/build/termux_step_install_license.sh | 64 +++++++++++--------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/scripts/build/termux_step_install_license.sh b/scripts/build/termux_step_install_license.sh index b9e7bc886d96a65..20f1df49ae66339 100644 --- a/scripts/build/termux_step_install_license.sh +++ b/scripts/build/termux_step_install_license.sh @@ -8,6 +8,7 @@ termux_step_install_license() { # Was a license file specified? if [[ -n "${TERMUX_PKG_LICENSE_FILE}" ]]; then COUNTER=1 + local FROM_SOURCES=0 local LICENSE_FILEPATH local -A INSTALLED_LICENSES=() while read -r LICENSE; do @@ -52,35 +53,42 @@ termux_step_install_license() { 'BSD'|'BSD 2-Clause'|'BSD 3-Clause'|'BSD Simplified'\ |'curl'|'HPND'|'ISC'|'Libpng'|'MIT'|'Openfont-1.1'\ |'PythonPL'|'X11'|'ZLIB') - # Find the license file in the source files - for FILE in "${COMMON_LICENSE_FILES[@]}"; do - [[ -f "$TERMUX_PKG_SRCDIR/$FILE" ]] && { - if (( COUNTER )); then - cp -f "${TERMUX_PKG_SRCDIR}/$FILE" "${TERMUX_PREFIX}/share/doc/${TERMUX_PKG_NAME}/copyright.${COUNTER}" - else - cp -f "${TERMUX_PKG_SRCDIR}/$FILE" "${TERMUX_PREFIX}/share/doc/${TERMUX_PKG_NAME}/copyright" - fi - # since this is a post-increment, (( 0 )) would be falsey - # thus `set -e` would kill the script on the first iteration - # using `true` prevents this - : $(( COUNTER++ )) - } - done + # We only want to include the license files from the source files once + if (( ! FROM_SOURCES )); then + for FILE in "${COMMON_LICENSE_FILES[@]}"; do + # Find the license file(s) in the source files + [[ -f "$TERMUX_PKG_SRCDIR/$FILE" ]] && { + if (( COUNTER )); then + cp -f "${TERMUX_PKG_SRCDIR}/$FILE" "${TERMUX_PREFIX}/share/doc/${TERMUX_PKG_NAME}/copyright.${COUNTER}" + else + cp -f "${TERMUX_PKG_SRCDIR}/$FILE" "${TERMUX_PREFIX}/share/doc/${TERMUX_PKG_NAME}/copyright" + fi + (( ++COUNTER, ++FROM_SOURCES )) + } + done + # If we have not found any licenses after searching, that's an error. + if (( ! FROM_SOURCES )); then + termux_error_exit "${TERMUX_PKG_NAME}: Could not find a license file for $LICENSE in the package sources" + fi + fi ;; - # For the rest we can use a link to the generic license file - *) [[ -f "$TERMUX_SCRIPTDIR/packages/termux-licenses/LICENSES/${LICENSE}.txt" ]] && { - # the link target depends on the libc being used - if [[ "$TERMUX_PACKAGE_LIBRARY" == 'bionic' ]]; then - TO_LICENSE="../../LICENSES/${LICENSE}.txt" - elif [[ "$TERMUX_PACKAGE_LIBRARY" == 'glibc' ]]; then - TO_LICENSE="../../../../share/LICENSES/${LICENSE}.txt" - fi - if (( COUNTER )); then - ln -sf "$TO_LICENSE" "$TERMUX_PREFIX/share/doc/$TERMUX_PKG_NAME/copyright.$((COUNTER++))" - else - ln -sf "$TO_LICENSE" "$TERMUX_PREFIX/share/doc/$TERMUX_PKG_NAME/copyright" - fi - } + *) # For the rest we can use a link to the generic license file + [[ -f "$TERMUX_SCRIPTDIR/packages/termux-licenses/LICENSES/${LICENSE}.txt" ]] || { + # If we get here, no license file could be found + termux_error_exit "${TERMUX_PKG_NAME}: Could not find a license file for $LICENSE in packages/termux-licenses" + } + # the link target depends on the libc being used + case "$TERMUX_PACKAGE_LIBRARY" in + 'bionic') TO_LICENSE="../../LICENSES/${LICENSE}.txt";; + 'glibc') TO_LICENSE="../../../../share/LICENSES/${LICENSE}.txt";; + *) termux_error_exit "'$TERMUX_PACKAGE_LIBRARY' is not a supported libc";; + esac + if (( COUNTER )); then + ln -sf "$TO_LICENSE" "$TERMUX_PREFIX/share/doc/$TERMUX_PKG_NAME/copyright.${COUNTER}" + else + ln -sf "$TO_LICENSE" "$TERMUX_PREFIX/share/doc/$TERMUX_PKG_NAME/copyright" + fi + (( ++COUNTER )) ;; esac done <<< "${TERMUX_PKG_LICENSE//,/$'\n'}"