From 94c5c7c3b3e5cab5fd71eaa86258302bf8c57c64 Mon Sep 17 00:00:00 2001 From: Jared Vititoe Date: Thu, 5 Feb 2026 11:23:29 -0500 Subject: [PATCH] Fix inefficient DRIVE_MAP declaration pattern Declare DRIVE_MAP as global at function start and populate directly, instead of creating a local array and copying to global. Also added proper variable quoting and function documentation. Fixes: https://code.lotusguild.org/LotusGuild/driveAtlas/issues/1 Co-Authored-By: Claude Opus 4.5 --- driveAtlas.sh | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/driveAtlas.sh b/driveAtlas.sh index 3f7be16..1742343 100644 --- a/driveAtlas.sh +++ b/driveAtlas.sh @@ -243,28 +243,33 @@ get_storage_controllers() { done } +#------------------------------------------------------------------------------ +# build_drive_map +# +# Builds a global associative array mapping physical bay numbers to device names. +# Uses PCI paths from SERVER_MAPPINGS to resolve current device assignments. +# +# Sets: DRIVE_MAP (global associative array) +# Keys: Bay identifiers (1, 2, ..., m2-1, m2-2, etc.) +# Values: Device names (sda, nvme0n1, etc.) +#------------------------------------------------------------------------------ build_drive_map() { - local host=$(hostname) - declare -A drive_map + local host="$(hostname)" + local mapping="${SERVER_MAPPINGS[$host]}" - local mapping=${SERVER_MAPPINGS[$host]} + # Declare global array directly instead of copying from local + declare -g -A DRIVE_MAP=() if [[ -n "$mapping" ]]; then while read -r path slot; do [[ -z "$path" || -z "$slot" ]] && continue if [[ -L "/dev/disk/by-path/$path" ]]; then - local drive=$(readlink -f "/dev/disk/by-path/$path" | sed 's/.*\///') - drive_map[$slot]=$drive + local drive="$(readlink -f "/dev/disk/by-path/$path" | sed 's/.*\///')" + DRIVE_MAP[$slot]="$drive" fi done <<< "$mapping" fi - - # Make drive_map available globally - declare -g -A DRIVE_MAP=() - for key in "${!drive_map[@]}"; do - DRIVE_MAP[$key]=${drive_map[$key]} - done } get_drive_smart_info() {