From 3d1a586c73aba43a9fb212e3963e7455b89f03fa Mon Sep 17 00:00:00 2001 From: rbalsleyMSFT <53497092+rbalsleyMSFT@users.noreply.github.com> Date: Thu, 20 Nov 2025 17:05:31 -0800 Subject: [PATCH] Refactor driver cleanup logic into reusable helper function Extracts duplicate folder removal code across multiple driver modules (Dell, HP, Lenovo, Microsoft) into a centralized `Remove-DriverModelFolder` helper function. This new utility includes safety checks to prevent accidental deletion of the drivers root directory or paths outside the drivers folder hierarchy. Also moves model path construction in the Microsoft driver module earlier in the function to ensure consistent path handling before any operations that might fail. Benefits improved maintainability, reduces code duplication, and adds protective safeguards for destructive operations during error handling. --- .../FFUUI.Core/FFUUI.Core.Drivers.Dell.psm1 | 1 + .../FFUUI.Core/FFUUI.Core.Drivers.HP.psm1 | 5 +- .../FFUUI.Core/FFUUI.Core.Drivers.Lenovo.psm1 | 1 + .../FFUUI.Core.Drivers.Microsoft.psm1 | 9 +-- .../FFUUI.Core/FFUUI.Core.Drivers.psm1 | 56 ++++++++++++++++--- 5 files changed, 56 insertions(+), 16 deletions(-) diff --git a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Drivers.Dell.psm1 b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Drivers.Dell.psm1 index 643c8b3..7a970af 100644 --- a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Drivers.Dell.psm1 +++ b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Drivers.Dell.psm1 @@ -362,6 +362,7 @@ function Save-DellDriversTask { catch { $errorStatus = "Error: $($_.Exception.Message)" WriteLog "Save-DellDriversTask error for $($modelDisplay): $($_.Exception.ToString())" + Remove-DriverModelFolder -DriversFolder $DriversFolder -TargetFolder $modelPath -Description $modelDisplay if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelDisplay -Status $errorStatus } return [pscustomobject]@{ Model = $modelDisplay; Status = $errorStatus; Success = $false; DriverPath = $null } } diff --git a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Drivers.HP.psm1 b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Drivers.HP.psm1 index 1e96a95..ef5fc97 100644 --- a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Drivers.HP.psm1 +++ b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Drivers.HP.psm1 @@ -426,10 +426,7 @@ function Save-HPDriversTask { $finalStatus = "Error: $($_.Exception.Message.Split([Environment]::NewLine)[0])" $successState = $false $driverRelativePath = $null # Ensure path is null on error - if (Test-Path -Path $modelSpecificFolder -PathType Container) { - WriteLog "Attempting to remove partially created folder $modelSpecificFolder due to error." - Remove-Item -Path $modelSpecificFolder -Recurse -Force -ErrorAction SilentlyContinue - } + Remove-DriverModelFolder -DriversFolder $DriversFolder -TargetFolder $modelSpecificFolder -Description $identifier } if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status $finalStatus } diff --git a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Drivers.Lenovo.psm1 b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Drivers.Lenovo.psm1 index e59c3ae..64f4c00 100644 --- a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Drivers.Lenovo.psm1 +++ b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Drivers.Lenovo.psm1 @@ -459,6 +459,7 @@ function Save-LenovoDriversTask { $status = "Error: $($_.Exception.Message)" WriteLog "Error saving Lenovo drivers for '$identifier': $($_.Exception.ToString())" $success = $false + Remove-DriverModelFolder -DriversFolder $DriversFolder -TargetFolder $modelPath -Description $identifier if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status $status } return [PSCustomObject]@{ Identifier = $identifier; Status = $status; Success = $success; DriverPath = $null } } diff --git a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Drivers.Microsoft.psm1 b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Drivers.Microsoft.psm1 index 1c80d2b..2896160 100644 --- a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Drivers.Microsoft.psm1 +++ b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Drivers.Microsoft.psm1 @@ -106,6 +106,10 @@ function Save-MicrosoftDriversTask { $driverRelativePath = Join-Path -Path $make -ChildPath $modelName # Relative path for the driver folder $status = "Getting download link..." # Initial local status $success = $false + $sanitizedModelName = ConvertTo-SafeName -Name $modelName + if ($sanitizedModelName -ne $modelName) { WriteLog "Sanitized model name: '$modelName' -> '$sanitizedModelName'" } + $makeDriversPath = Join-Path -Path $DriversFolder -ChildPath $make + $modelPath = Join-Path -Path $makeDriversPath -ChildPath $sanitizedModelName # Initial status update if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status "Checking..." } @@ -235,10 +239,6 @@ function Save-MicrosoftDriversTask { WriteLog "Creating Drivers folder: $DriversFolder" New-Item -Path $DriversFolder -ItemType Directory -Force | Out-Null } - $sanitizedModelName = ConvertTo-SafeName -Name $modelName - if ($sanitizedModelName -ne $modelName) { WriteLog "Sanitized model name: '$modelName' -> '$sanitizedModelName'" } - $makeDriversPath = Join-Path -Path $DriversFolder -ChildPath $Make - $modelPath = Join-Path -Path $makeDriversPath -ChildPath $sanitizedModelName if (-Not (Test-Path -Path $modelPath)) { WriteLog "Creating model folder: $modelPath" New-Item -Path $modelPath -ItemType Directory -Force | Out-Null @@ -422,6 +422,7 @@ function Save-MicrosoftDriversTask { $status = "Error: $($_.Exception.Message.Split('.')[0])" # Shorten error message WriteLog "Error saving Microsoft drivers for $($modelName): $($_.Exception.Message)" $success = $false + Remove-DriverModelFolder -DriversFolder $DriversFolder -TargetFolder $modelPath -Description $modelName # Enqueue the error status before returning if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status $status } # Ensure return object is created even on error diff --git a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Drivers.psm1 b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Drivers.psm1 index 4c824b8..540c38f 100644 --- a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Drivers.psm1 +++ b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Drivers.psm1 @@ -92,15 +92,55 @@ function Convert-DriverItemToJsonModel { MachineType = $machineType } } - default { - WriteLog "Convert-DriverItemToJsonModel: Unsupported Make '$makeName'." - return $null + default { + WriteLog "Convert-DriverItemToJsonModel: Unsupported Make '$makeName'." + return $null + } + } } - } -} - -# Helper function to get models for a selected Make and standardize them -function Get-ModelsForMake { + + function Remove-DriverModelFolder { + param( + [Parameter(Mandatory = $true)] + [string]$DriversFolder, + [Parameter(Mandatory = $true)] + [string]$TargetFolder, + [string]$Description + ) + + if ([string]::IsNullOrWhiteSpace($DriversFolder) -or [string]::IsNullOrWhiteSpace($TargetFolder)) { + return + } + + try { + if (-not (Test-Path -Path $TargetFolder -PathType Container)) { + return + } + + $driversRoot = [System.IO.Path]::GetFullPath((Resolve-Path -Path $DriversFolder -ErrorAction Stop).ProviderPath) + $targetPath = [System.IO.Path]::GetFullPath((Resolve-Path -Path $TargetFolder -ErrorAction Stop).ProviderPath) + + if ($targetPath -eq $driversRoot) { + WriteLog "Remove-DriverModelFolder skipped deleting Drivers root: $targetPath" + return + } + + if (-not ($targetPath.StartsWith($driversRoot, [System.StringComparison]::OrdinalIgnoreCase))) { + WriteLog "Remove-DriverModelFolder skipped path outside Drivers root: $targetPath" + return + } + + $contextMessage = if ([string]::IsNullOrWhiteSpace($Description)) { $targetPath } else { "$Description ($targetPath)" } + WriteLog "Removing driver folder $contextMessage due to failure." + Remove-Item -Path $targetPath -Recurse -Force -ErrorAction SilentlyContinue + } + catch { + WriteLog "Remove-DriverModelFolder failed for $($TargetFolder): $($_.Exception.Message)" + } + } + + # Helper function to get models for a selected Make and standardize them + function Get-ModelsForMake { param( [Parameter(Mandatory = $true)] [string]$SelectedMake,