From 0b76ed9744c3ef70d16218e667a9e68bbcde7a77 Mon Sep 17 00:00:00 2001 From: Kroese Date: Sat, 8 Aug 2026 02:21:53 +0200 Subject: [PATCH] fix: Reduce amount of state variables (#2107) --- docs/environment.md | 1 - src/define.sh | 78 ++++++++----------------- src/image.sh | 21 +------ src/install.sh | 86 ++++++++-------------------- src/mido.sh | 136 +++----------------------------------------- 5 files changed, 60 insertions(+), 262 deletions(-) diff --git a/docs/environment.md b/docs/environment.md index 4bbc0679..afde749c 100644 --- a/docs/environment.md +++ b/docs/environment.md @@ -175,6 +175,5 @@ Also see [Dynamic memory allocation](https://github.com/qemus/qemu/blob/master/d | `DEBUG` | `N` | Enables verbose debug output. | | `TRACE` | `N` | Enables shell command tracing. | | `LOG` | `N` | Saves all output from `install.bat` to `C:\OEM\install.log` for troubleshooting. | -| `DETECTED` | | Overrides the automatically detected Windows image identifier. | | `SERIAL` | `mon:stdio` | QEMU serial device configuration. | | `MONITOR` | | QEMU monitor configuration. | diff --git a/src/define.sh b/src/define.sh index 73b1b32f..d6c66ece 100644 --- a/src/define.sh +++ b/src/define.sh @@ -13,7 +13,6 @@ set -Eeuo pipefail : "${REMOVE:=""}" : "${VERSION:=""}" : "${COMMAND:=""}" -: "${DETECTED:=""}" : "${KEYBOARD:=""}" : "${LANGUAGE:=""}" : "${USERNAME:=""}" @@ -41,7 +40,7 @@ MIRRORS=4 parseVersion() { - SUGGEST="" + DETECTED="" VERSION=$(strip "$VERSION") [ -z "$VERSION" ] && VERSION="win11" @@ -51,41 +50,49 @@ parseVersion() { "11e" | "win11e" | "windows11e" | "windows 11e" ) VERSION="win11x64-enterprise-eval" ;; "11l" | "11ltsc" | "ltsc11" | "win11l" | "win11-ltsc" | "win11x64-ltsc" ) - VERSION="win11x64-enterprise-ltsc-eval" ;; + VERSION="win11x64-enterprise-ltsc-eval" + DETECTED="win11x64-ltsc" ;; "11i" | "11iot" | "iot11" | "win11i" | "win11-iot" | "win11x64-iot" ) - VERSION="win11x64-enterprise-iot-eval" ;; + VERSION="win11x64-enterprise-iot-eval" + DETECTED="win11x64-iot" ;; "10" | "10p" | "win10" | "pro10" | "win10p" | "windows10" | "windows 10" ) VERSION="win10x64" ;; "10e" | "win10e" | "windows10e" | "windows 10e" ) VERSION="win10x64-enterprise-eval" ;; "10l" | "10ltsc" | "ltsc10" | "win10l" | "win10-ltsc" | "win10x64-ltsc" ) - VERSION="win10x64-enterprise-ltsc-eval" ;; + VERSION="win10x64-enterprise-ltsc-eval" + DETECTED="win10x64-ltsc" ;; "10i" | "10iot" | "iot10" | "win10i" | "win10-iot" | "win10x64-iot" ) - VERSION="win10x64-enterprise-iot-eval" ;; + VERSION="win10x64-enterprise-iot-eval" + DETECTED="win10x64-iot" ;; "8" | "8p" | "81" | "81p" | "pro8" | "8.1" | "win8" | "win8p" | "win81" | "win81p" | "windows 8" ) VERSION="win81x64" ;; "8e" | "81e" | "8.1e" | "win8e" | "win81e" | "windows 8e" ) VERSION="win81x64-enterprise-eval" ;; "7" | "win7" | "windows7" | "windows 7" ) - VERSION="win7x64" ;; + VERSION="win7x64" + DETECTED="win7x64-ultimate" ;; "7u" | "win7u" | "windows7u" | "windows 7u" ) VERSION="win7x64-ultimate" ;; "7e" | "win7e" | "windows7e" | "windows 7e" ) VERSION="win7x64-enterprise" ;; "7x86" | "win7x86" | "win732" | "windows7x86" ) - VERSION="win7x86" ;; + VERSION="win7x86" + DETECTED="win7x86-ultimate" ;; "7ux86" | "7u32" | "win7x86-ultimate" ) VERSION="win7x86-ultimate" ;; "7ex86" | "7e32" | "win7x86-enterprise" ) VERSION="win7x86-enterprise" ;; "vista" | "vs" | "6" | "winvista" | "windowsvista" | "windows vista" ) - VERSION="winvistax64" ;; + VERSION="winvistax64" + DETECTED="winvistax64-ultimate" ;; "vistu" | "vu" | "6u" | "winvistu" ) VERSION="winvistax64-ultimate" ;; "viste" | "ve" | "6e" | "winviste" ) VERSION="winvistax64-enterprise" ;; "vistax86" | "vista32" | "6x86" | "winvistax86" | "windowsvistax86" ) - VERSION="winvistax86" ;; + VERSION="winvistax86" + DETECTED="winvistax86-ultimate" ;; "vux86" | "vu32" | "winvistax86-ultimate" ) VERSION="winvistax86-ultimate" ;; "vex86" | "ve32" | "winvistax86-enterprise" ) @@ -113,45 +120,18 @@ parseVersion() { "2003" | "2003r2" | "win2003" | "win2003r2" | "windows2003" | "windows 2003" ) VERSION="win2003r2" ;; "core11" | "core 11" ) - VERSION="core11" ;; + VERSION="core11" + DETECTED="win11x64" ;; "tiny11" | "tiny 11" ) - VERSION="tiny11" ;; + VERSION="tiny11" + DETECTED="win11x64" ;; "tiny10" | "tiny 10" ) - VERSION="tiny10" ;; + VERSION="tiny10" + DETECTED="win10x64-ltsc" ;; "reactos" | "react os" ) VERSION="reactos" ;; esac - SUGGEST=$(getSuggestedVersion "$VERSION") - - return 0 -} - -getSuggestedVersion() { - - local id="${1,,}" - - [[ "$id" == http* ]] && return 0 - - case "$id" in - "win10x64" | "win11x64" ) - echo "$id" ;; - "win7x64" | "win7x86" | "winvistax64" | "winvistax86" ) - echo "$id-ultimate" ;; - "tiny10" ) - echo "win10x64-ltsc" ;; - *"-enterprise-ltsc-eval" ) - echo "${id%-enterprise-ltsc-eval}-ltsc" ;; - *"-enterprise-iot-eval" ) - echo "${id%-enterprise-iot-eval}-iot" ;; - *"-enterprise-ltsc" ) - echo "${id%-enterprise-ltsc}-ltsc" ;; - *"-enterprise-iot" ) - echo "${id%-enterprise-iot}-iot" ;; - *"-eval" ) - echo "${id%-eval}" ;; - esac - return 0 } @@ -299,7 +279,7 @@ getLanguage() { "pt-"* ) short="pp" lang="Portuguese" - culture="pt-BR" ;; + culture="pt-PT" ;; "ro" | "ro-"* | "romanian" | "română" | "romana" ) [[ "$input" == "romanian" || "$input" == "română" || "$input" == "romana" ]] && id="ro" short="ro" @@ -1031,16 +1011,6 @@ getDriverFolder() { return 0 } -switchEdition() { - - local version="$1" - - [[ "${version,,}" == *"-eval" ]] || return 1 - - echo "${version::-5}" - return 0 -} - getMido() { local id="$1" diff --git a/src/image.sh b/src/image.sh index cd9c3279..ea2e7ade 100644 --- a/src/image.sh +++ b/src/image.sh @@ -1307,6 +1307,7 @@ parseWimHeader() { return 0 } + findImage() { local dir="$1" @@ -1353,16 +1354,6 @@ readImageInfo() { return 0 } -getSuggestion() { - - [ -z "$CUSTOM" ] || return 0 - [ -n "${REUSED_ISO:-}" ] || return 0 - - # A reused ISO may still correspond to the originally requested catalog - # version, but the suggestion remains only a preference during detection. - echo "${SUGGEST:-}" -} - validateEdition() { [ -n "$EDITION" ] || return 0 @@ -1470,21 +1461,15 @@ detectImageInfo() { local image_info="$1" - local desc suggested index rc + local desc index rc checkPlatform "$image_info" || { enabled "$DEBUG" && echo "Platform validation failed for the Windows image metadata." >&2 exit 67 } - suggested=$(getSuggestion) || { - rc=$? - enabled "$DEBUG" && echo "Failed to determine the suggested Windows image edition (status $rc)." >&2 - return "$rc" - } - local output - output=$(detectVersion "$image_info" "$suggested") || { + output=$(detectVersion "$image_info") || { enabled "$DEBUG" && echo "Version detection failed while parsing the Windows image metadata." >&2 error "Failed to detect Windows version from image metadata!" return 1 diff --git a/src/install.sh b/src/install.sh index b8746bbd..d0df1b04 100644 --- a/src/install.sh +++ b/src/install.sh @@ -43,8 +43,15 @@ selectWindowsImage() { local dir="$2" local boot="$3" - # Known versions already provide the required image metadata. - if resolveImage "$VERSION"; then + XML="" + FB="falling back to manual installation!" + + # Known catalog versions already provide the required image metadata. + if [ -z "$DETECTED" ] && [ -z "$CUSTOM" ] && [[ "${VERSION,,}" != "http"* ]]; then + DETECTED="$VERSION" + fi + + if [ -n "$DETECTED" ]; then if ! setImage; then return 70 @@ -99,7 +106,7 @@ configureMachine() { desc=$(printVariant "$DETECTED" "$DETECTED") || return 78 if ! checkMemory "$DETECTED" "$desc"; then - if [ -n "${REUSED_ISO:-}" ]; then + if [ -z "$CUSTOM" ]; then useOriginalImage "$iso" || return 79 fi return 79 @@ -220,18 +227,6 @@ startInstall() { BOOT="$STORAGE/$boot" - REUSED_ISO="" - [ -s "$BOOT" ] && REUSED_ISO="Y" - - # Use the suggested answer file for a new automatic download. When an - # existing ISO is reused, leave DETECTED empty so its actual image can - # be inspected instead. - if [ -n "$DETECTED" ]; then - DETECTED_ORG="Y" - elif [ -z "$REUSED_ISO" ]; then - DETECTED="$SUGGEST" - fi - fi TMP="$STORAGE/tmp" @@ -275,7 +270,7 @@ startInstall() { if [ -z "$CUSTOM" ]; then - if [ -n "$REUSED_ISO" ]; then + if [ -s "$BOOT" ]; then ISO="$TMP/$(basename "$BOOT")" else ISO="$TMP/$file" @@ -283,9 +278,9 @@ startInstall() { fi - # Keep reusable media at its persistent path until all storage cleanup has + # Keep existing media at its persistent path until all storage cleanup has # completed successfully, so a later failure cannot strand it under $TMP. - if [ -n "$CUSTOM" ] || [ -z "$REUSED_ISO" ]; then + if [ -n "$CUSTOM" ] || [ ! -s "$BOOT" ]; then if ! rm -f -- "$BOOT"; then error "Failed to remove obsolete ISO file \"$BOOT\" !" exit 50 @@ -307,7 +302,7 @@ startInstall() { exit 50 fi - if [ -z "$CUSTOM" ] && [ -z "$REUSED_ISO" ] && [[ "${VERSION,,}" != "http"* ]]; then + if [ -z "$CUSTOM" ] && [[ "${VERSION,,}" != "http"* ]]; then checkMemory "$VERSION" || exit 67 fi @@ -592,10 +587,18 @@ detectCustom() { CUSTOM="" findFile "custom.iso" || return 1 - [ -n "$CUSTOM" ] && return 0 + + if [ -n "$CUSTOM" ]; then + DETECTED="" + return 0 + fi findFile "boot.iso" || return 1 - [ -n "$CUSTOM" ] && return 0 + + if [ -n "$CUSTOM" ]; then + DETECTED="" + return 0 + fi return 0 } @@ -621,47 +624,6 @@ removeImage() { return 0 } -resolveImage() { - - local version="$1" - - XML="" - FB="falling back to manual installation!" - - [ -z "$DETECTED" ] || return 0 - - # Reused and arbitrary URL media must be inspected because their actual - # contents may no longer match the requested VERSION. - [ -z "${REUSED_ISO:-}" ] || return 1 - [[ "${version,,}" != "http"* ]] || return 1 - - # Only direct-boot custom media can safely bypass content detection. - if [ -n "$CUSTOM" ]; then - supportsUnattended "$version" && return 1 - DETECTED="$version" - return 0 - fi - - local file="/run/assets/$version.xml" - - if [ -s "$file" ]; then - DETECTED="$version" - return 0 - fi - - # Evaluation media may reuse the normal edition's answer-file template. - if [[ "${version,,}" == *"-eval" ]]; then - local source="/run/assets/${version%-eval}.xml" - - if [ -s "$source" ]; then - DETECTED="$version" - return 0 - fi - fi - - return 1 -} - setImage() { supportsXML "${DETECTED,,}" || return 0 diff --git a/src/mido.sh b/src/mido.sh index b6b8028c..c0b9e0de 100644 --- a/src/mido.sh +++ b/src/mido.sh @@ -549,97 +549,6 @@ downloadWindowsEval() { return 0 } -getMidoDetected() { - - # Return the answer-file identity for the Microsoft source that actually - # succeeded without changing the global DETECTED value. - - local version="${1,,}" - local source="${2,,}" - local current="$3" - local default="$version" - local detected - - [ -z "$source" ] && source="$version" - - # Preserve a DETECTED value that existed before SUGGEST was assigned. - if enabled "${DETECTED_ORG:-}"; then - echo "$current" - return 0 - fi - - # Derive the normal answer-file identity from the requested download route. - case "$default" in - *"-enterprise-ltsc-eval" ) - default="${default%-enterprise-ltsc-eval}-ltsc" ;; - *"-enterprise-iot-eval" ) - default="${default%-enterprise-iot-eval}-iot" ;; - *"-eval" ) - default="${default%-eval}" ;; - esac - - # Preserve a genuinely different DETECTED override. - if [ -n "$current" ] && [[ "${current,,}" != "$default" ]]; then - echo "$current" - return 0 - fi - - # Select the answer-file identity for the source that actually succeeded. - case "$source" in - *"-enterprise-ltsc-eval" ) - detected="${source%-enterprise-ltsc-eval}-ltsc-eval" ;; - *"-enterprise-iot-eval" ) - detected="${source%-enterprise-iot-eval}-iot-eval" ;; - *"-eval" ) - detected="$source" ;; - * ) - detected="${current:-$default}" ;; - esac - - echo "$detected" - return 0 -} - -downloadWindowsLtsc() { - - local id="$1" - local lang="$2" - local desc="$3" - - local alternate alternate_desc rc - - case "${id,,}" in - "win11${PLATFORM,,}-enterprise-iot-eval" ) - alternate="win11${PLATFORM,,}-enterprise-ltsc-eval" ;; - "win11${PLATFORM,,}-enterprise-ltsc-eval" ) - alternate="win11${PLATFORM,,}-enterprise-iot-eval" ;; - * ) - error "Invalid VERSION specified, value \"$id\" is not recognized!" - return 1 ;; - esac - - # IoT and LTSC share related evaluation sources and may become unavailable - # independently, so use the sibling edition as a compatibility fallback. - if downloadWindowsEval "$id" "$lang" "$desc" > /dev/null 2>&1; then - MIDO_SOURCE="$id" - return 0 - else - rc=$? - (( rc == 1 )) || return "$rc" - fi - - alternate_desc=$(printEdition "$alternate" "$alternate" "Y") - - info "Primary download source failed, trying $alternate_desc instead..." - - downloadWindowsEval "$alternate" "$lang" "$alternate_desc" || return - - MIDO_SOURCE="$alternate" - warn "the requested $desc was unavailable, using $alternate_desc instead." - - return 0 -} - getWindows() { local version="$1" @@ -649,7 +558,6 @@ getWindows() { local language edition rc - MIDO_SOURCE="" MIDO_STATIC="N" language=$(getLanguage "$lang" "desc") @@ -693,17 +601,6 @@ getWindows() { "win10x64" | "win11${PLATFORM,,}" ) if downloadWindows "$version" "$lang" "$edition"; then - MIDO_SOURCE="$version" - return 0 - else - rc=$? - (( rc == 1 )) || return "$rc" - fi ;; - - "win11${PLATFORM,,}-enterprise-iot-eval" | \ - "win11${PLATFORM,,}-enterprise-ltsc-eval" ) - - if downloadWindowsLtsc "$version" "$lang" "$edition"; then return 0 else rc=$? @@ -713,7 +610,6 @@ getWindows() { "win11${PLATFORM,,}-enterprise"* ) if downloadWindowsEval "$version" "$lang" "$edition"; then - MIDO_SOURCE="$version" return 0 else rc=$? @@ -724,7 +620,6 @@ getWindows() { "win2019-hv" | "win2016-eval" | "win2012r2-eval" ) if downloadWindowsEval "$version" "$lang" "$edition"; then - MIDO_SOURCE="$version" return 0 else rc=$? @@ -745,12 +640,6 @@ getWindows() { MIDO_STATIC="Y" - if [[ "${version,,}" == "win2008r2"* ]]; then - MIDO_SOURCE="win2008r2-eval" - return 0 - fi - - MIDO_SOURCE="$version" return 0 } @@ -1373,10 +1262,9 @@ downloadImage() { local version="$2" local lang="$3" - local detected="$DETECTED" - local requested="$version" switched="" + local requested="$version" local tried="n" success="n" seconds="5" - local i url sum size base language desc web_desc metadata rc + local i url sum size base language desc web_desc rc if [[ "${version,,}" == "http"* ]]; then @@ -1436,17 +1324,15 @@ downloadImage() { if [[ "$success" == "y" ]]; then - detected=$(getMidoDetected "$version" "$MIDO_SOURCE" "$DETECTED") - metadata="${MIDO_SOURCE:-$version}" - url=$(getMido "$metadata" "$lang" "") + url=$(getMido "$version" "$lang" "") sum="" size="" # Apply the metadata belonging to the configured static URL. if [[ "${MIDO_URL%%\?*}" == "${url%%\?*}" ]]; then - size=$(getMido "$metadata" "$lang" "size") - sum=$(getMido "$metadata" "$lang" "sum") + size=$(getMido "$version" "$lang" "size") + sum=$(getMido "$version" "$lang" "sum") fi local download_desc="$desc" @@ -1455,8 +1341,6 @@ downloadImage() { fi if tryDownload "$iso" "$MIDO_URL" "$sum" "$size" "$download_desc" "$seconds" "$web_desc"; then - # Commit the candidate only after the image was downloaded and verified. - DETECTED="$detected" return 0 else rc=$? @@ -1468,14 +1352,12 @@ downloadImage() { # If an evaluation version was requested, switch to the # normal edition since none of our mirrors provide those. - if switched=$(switchEdition "$version"); then + if [[ "${version,,}" == *"-eval" ]]; then - validDownload "$switched" || return 1 - version="$switched" + version="${version::-5}" + validDownload "$version" || return 1 - if ! enabled "${DETECTED_ORG:-}"; then - DETECTED="${SUGGEST:-$version}" - fi + [ -n "$DETECTED" ] || DETECTED="$version" desc=$(printVariant "$DETECTED" "" "Y") web_desc=$(printVariant "$DETECTED" "")