From c0018b5370adc38dd1daf77ca9c6195a36d4a345 Mon Sep 17 00:00:00 2001 From: Ana Maria Martinez Gomez Date: Thu, 26 Oct 2023 15:25:26 +0200 Subject: [PATCH 1/2] common.vm: remove unzipLocation from helper We introduced unziplocation as a parameter for VM-Install-From-Zip as we were planing to use it for psnotify.vm, but we realized that it is better not to use the helper for this package. So this argument is unused. Remove it to make it easier to maintain the function. --- packages/common.vm/common.vm.nuspec | 2 +- packages/common.vm/tools/vm.common/vm.common.psm1 | 13 +++---------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/packages/common.vm/common.vm.nuspec b/packages/common.vm/common.vm.nuspec index 9325d71e7..62fa0fbd8 100755 --- a/packages/common.vm/common.vm.nuspec +++ b/packages/common.vm/common.vm.nuspec @@ -2,7 +2,7 @@ common.vm - 0.0.0.20231020 + 0.0.0.20231027 Common libraries for VM-packages Mandiant diff --git a/packages/common.vm/tools/vm.common/vm.common.psm1 b/packages/common.vm/tools/vm.common/vm.common.psm1 index 31b04f05b..fe5dcba43 100755 --- a/packages/common.vm/tools/vm.common/vm.common.psm1 +++ b/packages/common.vm/tools/vm.common/vm.common.psm1 @@ -329,17 +329,10 @@ function VM-Install-From-Zip { [Parameter(Mandatory=$false)] [string] $executableName, # Executable name, needed if different from "$toolName.exe" [Parameter(Mandatory=$false)] - [switch] $withoutBinFile, # Tool should not be installed as a bin file - [Parameter(Mandatory=$false)] - [string] $unzipLocation + [switch] $withoutBinFile # Tool should not be installed as a bin file ) try { - if ($unzipLocation) { - $toolDir = Join-Path $unzipLocation $toolName - } else { - $toolDir = Join-Path ${Env:RAW_TOOLS_DIR} $toolName - $unzipLocation = $toolDir - } + $toolDir = Join-Path ${Env:RAW_TOOLS_DIR} $toolName # Remove files from previous zips for upgrade VM-Remove-PreviousZipPackage ${Env:chocolateyPackageFolder} @@ -353,7 +346,7 @@ function VM-Install-From-Zip { # Download and unzip $packageArgs = @{ packageName = ${Env:ChocolateyPackageName} - unzipLocation = $unzipLocation + unzipLocation = $toolDir url = $zipUrl checksum = $zipSha256 checksumType = 'sha256' From 6d9ff6254afaada01a5415eaf72768a4c2f23470 Mon Sep 17 00:00:00 2001 From: Ana Maria Martinez Gomez Date: Thu, 26 Oct 2023 15:29:17 +0200 Subject: [PATCH 2/2] common.vm: simplify code that handles upgrades By unzipping directly in the tools directory, chocolatey takes care of maintaining the files to delete on an update. --- .../common.vm/tools/vm.common/vm.common.psm1 | 59 +++---------------- 1 file changed, 9 insertions(+), 50 deletions(-) diff --git a/packages/common.vm/tools/vm.common/vm.common.psm1 b/packages/common.vm/tools/vm.common/vm.common.psm1 index fe5dcba43..c865b722e 100755 --- a/packages/common.vm/tools/vm.common/vm.common.psm1 +++ b/packages/common.vm/tools/vm.common/vm.common.psm1 @@ -207,47 +207,23 @@ function VM-Install-Raw-GitHub-Repo { # Remove files from previous zips for upgrade VM-Remove-PreviousZipPackage ${Env:chocolateyPackageFolder} - # Create a temp directory to download zip - $tempDownloadDir = Join-Path ${Env:chocolateyPackageFolder} "temp_$([guid]::NewGuid())" - # Download and unzip $packageArgs = @{ packageName = ${Env:ChocolateyPackageName} - unzipLocation = $tempDownloadDir + unzipLocation = $toolDir url = $zipUrl checksum = $zipSha256 checksumType = 'sha256' } Install-ChocolateyZipPackage @packageArgs | Out-Null - VM-Assert-Path $tempDownloadDir + VM-Assert-Path $toolDir - # Make sure our tool directory exists - if (-Not (Test-Path $toolDir)) { - New-Item -Path $toolDir -ItemType Directory -Force | Out-Null + # GitHub ZIP files typically unzip to a single folder that contains the tools. + $dirList = Get-ChildItem $toolDir -Directory + if ($dirList.Count -eq 1) { + $toolDir = Join-Path $toolDir $dirList[0].Name -Resolve } - # Get the unzipped directory - $unzippedDir = (Get-ChildItem -Directory $tempDownloadDir | Where-Object {$_.PSIsContainer} | Select-Object -f 1).FullName - - # Copy all the items in the unzipped directory to their correct directory - Get-ChildItem -Path $unzippedDir | Move-Item -Destination $toolDir -Force -ea 0 - - # Sleep to help prevent file system race conditions - Start-Sleep -Milliseconds 500 - - # Rename all entries in to where the files were actually copied - $zip_name = Join-Path ${Env:chocolateyPackageFolder} ((Split-Path $unzippedDir -Leaf) + ".zip.txt") - (Get-Content $zip_name) | Foreach-Object {$_.Replace($unzippedDir, $toolDir)} | Set-Content $zip_name - - # Remove first line of *.zip.txt file so we don't delete the entire directory on upgrade - (Get-Content $zip_name | Select-Object -Skip 1) | Set-Content $zip_name - - # Sleep to help prevent file system race conditions - Start-Sleep -Milliseconds 500 - - # Attempt to remove temporary directory - Remove-Item $tempDownloadDir -Recurse -Force -ea 0 - if ($powershellCommand) { $executableArgs = "-ExecutionPolicy Bypass -NoExit -Command $powershellCommand" $powershellPath = Join-Path "${Env:WinDir}\system32\WindowsPowerShell\v1.0" "powershell.exe" -Resolve @@ -337,12 +313,6 @@ function VM-Install-From-Zip { # Remove files from previous zips for upgrade VM-Remove-PreviousZipPackage ${Env:chocolateyPackageFolder} - # Snapshot all folders in $toolDir - $oldDirList = @() - if (Test-Path $toolDir) { - $oldDirList = @(Get-ChildItem $toolDir | Where-Object {$_.PSIsContainer}) - } - # Download and unzip $packageArgs = @{ packageName = ${Env:ChocolateyPackageName} @@ -356,23 +326,12 @@ function VM-Install-From-Zip { Install-ChocolateyZipPackage @packageArgs VM-Assert-Path $toolDir - # Diff and find new folders in $toolDir - $newDirList = @(Get-ChildItem $toolDir | Where-Object {$_.PSIsContainer}) - $diffDirs = Compare-Object -ReferenceObject $oldDirList -DifferenceObject $newDirList -PassThru - # If $innerFolder is set to $true, after unzipping only a single folder should be new. + # If $innerFolder is set to $true, after unzipping there should be only one folder # GitHub ZIP files typically unzip to a single folder that contains the tools. if ($innerFolder) { - # First time install, use the single resulting folder name from Install-ChocolateyZipPackage. - if ($diffDirs.Count -eq 1) { - # Save the "new tool directory" to assist with upgrading. - $newToolDir = Join-Path $toolDir $diffDirs[0].Name -Resolve - Set-Content (Join-Path ${Env:chocolateyPackageFolder} "innerFolder.txt") $newToolDir - $toolDir = $newToolDir - } else { - # On upgrade there may be no new directory, in this case retrieve previous "new tool directory" from saved file. - $toolDir = Get-Content (Join-Path ${Env:chocolateyPackageFolder} "innerFolder.txt") - } + $dirList = Get-ChildItem $toolDir -Directory + $toolDir = Join-Path $toolDir $dirList[0].Name -Resolve } if (-Not $executableName) { $executableName = "$toolName.exe" }