Skip to content

Commit

Permalink
(chocolateyGH-616) Capture 7z's stdout in Get-ChocolateyUnzip
Browse files Browse the repository at this point in the history
Sometimes, Install-ChocolateyZipPackage and Get-ChocolateyUnzip capture
files that don't belong to the package, record them in the
<packagename>Install.zip.txt file, and cause them to be removed upon
uninstallation by UnInstall-ChocolateyZipPackage. This fix uses 7z's
standard output instead to determine which files belong to the package.
This also avoids scanning possibly large directory trees.
  • Loading branch information
Erlkoenig90 committed Feb 8, 2016
1 parent 3b13c02 commit 93797b3
Showing 1 changed file with 34 additions and 22 deletions.
56 changes: 34 additions & 22 deletions src/chocolatey.resources/helpers/functions/Get-ChocolateyUnzip.ps1
Expand Up @@ -91,32 +91,44 @@ param(
$destination32 = $destination
}

$exitCode = -1
$unzipOps = {
param($7zip, $destination, $fileFullPath, [ref]$exitCodeRef)
$params = "x -aoa -o`"$destination`" -y `"$fileFullPath`""
Write-Debug "Executing command ['$7zip' $params]"
$process = New-Object System.Diagnostics.Process
$process.StartInfo = new-object System.Diagnostics.ProcessStartInfo($7zip, $params)
$process.StartInfo.UseShellExecute = $false
$process.StartInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden

# Make structures used by .NET for starting the 7z process

# 7z command line
$params = "x -aoa -o`"$destination`" -y `"$fileFullPath`""
$process = New-Object System.Diagnostics.Process
$process.StartInfo = new-object System.Diagnostics.ProcessStartInfo($7zip, $params)
# Required for stdout redirect
$process.StartInfo.UseShellExecute = $false
$process.StartInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden

Write-Debug "Executing command ['$7zip' $params]"
if ($zipExtractLogFullPath) {
# Redirect stdout for processing by choco
$process.StartInfo.RedirectStandardOutput = $true
$process.Start() | Out-Null
$process.WaitForExit()
$processExitCode = $process.ExitCode
$process.Dispose()
Write-Debug "Command ['$7zip' $params] exited with `'$processExitCode`'."

$exitCodeRef.Value = $processExitCode
}

if ($zipExtractLogFullPath) {
Write-Debug "wrapping 7za invocation with Write-FileUpdateLog"
Write-FileUpdateLog -logFilePath $zipExtractLogFullPath -locationToMonitor $destination -scriptToRun $unzipOps -argumentList $7zip,$destination32,$fileFullPath32,([ref]$exitCode)
# Read each line from 7z's stdout synchroneously (ReadLine blocks).
# Since stderr is not redirected, it gets automatically printed to the console, avoiding deadlocks.
while(($process.StandardOutput -ne $null) -and (($line = $process.StandardOutput.ReadLine()) -ne $null)) {
if($line.StartsWith("Extracting")) {
# This is a line indicating an extracted file
$file = $destination + "\" + $line.Substring(12)
# Save the filename
Add-Content $zipExtractLogFullPath $file
}
# Print the line, such that it looks as if stdout was not redirected
Write-Debug $line
}
} else {
Write-Debug "calling 7za directly"
Invoke-Command $unzipOps -ArgumentList $7zip,$destination32,$fileFullPath32,([ref]$exitCode)
# If we don't want to capture the file list, just execute 7z without stdout redirection
$process.Start() | Out-Null
}

# Wait for 7z to finish. Even if 7z has closed its stdout, and all lines have been read, the process might not have quit yet.
$process.WaitForExit()
$exitCode = $process.ExitCode
$process.Dispose()
Write-Debug "Command ['$7zip' $params] exited with `'$exitCode`'."

Write-Debug "7za exit code: $exitCode"
switch ($exitCode) {
Expand Down

0 comments on commit 93797b3

Please sign in to comment.