Skip to content

Commit

Permalink
Merge branch 'stable'
Browse files Browse the repository at this point in the history
* stable:
  (GH-826) SYSTEM user always uses machine TEMP
  (GH-808) Allow silent args as array
  (GH-821) ValidExitCodes not recognized return 0
  (GH-818) Allow disk to catch up - sleep
  (GH-822) Provide pending override and file wait
  (GH-818) Wait for processes to exit + 2 seconds
  (maint) formatting
  (GH-819) Fix - NotSilent install failure binding
  (GH-775) remove 7za
  (GH-775) Use 7z instead of 7za
  • Loading branch information
ferventcoder committed Jun 23, 2016
2 parents fc1c895 + b9d8759 commit 40f3066
Show file tree
Hide file tree
Showing 18 changed files with 100 additions and 29 deletions.
11 changes: 8 additions & 3 deletions src/chocolatey.resources/chocolatey.resources.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@
<EmbeddedResource Include="redirects\cuninst.exe.ignore" />
<EmbeddedResource Include="redirects\cup.exe.ignore" />
<EmbeddedResource Include="redirects\cver.exe.ignore" />
<EmbeddedResource Include="tools\7za.exe.ignore" />
<EmbeddedResource Include="tools\7za.exe.manifest" />
<EmbeddedResource Include="tools\checksum.exe.ignore" />
<EmbeddedResource Include="tools\shimgen.exe.ignore" />
</ItemGroup>
Expand All @@ -97,7 +95,6 @@
<EmbeddedResource Include="redirects\cuninst.exe" />
<EmbeddedResource Include="redirects\cup.exe" />
<EmbeddedResource Include="redirects\cver.exe" />
<EmbeddedResource Include="tools\7za.exe" />
<EmbeddedResource Include="tools\7zip.license.txt" />
<EmbeddedResource Include="tools\checksum.exe" />
<EmbeddedResource Include="tools\checksum.license.txt" />
Expand Down Expand Up @@ -152,6 +149,14 @@
<ItemGroup>
<EmbeddedResource Include="helpers\functions\Get-UninstallRegistryKey.ps1" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="tools\7z.dll" />
<EmbeddedResource Include="tools\7z.exe" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="tools\7z.exe.ignore" />
<EmbeddedResource Include="tools\7z.exe.manifest" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ $extensionsPath = Join-Path $nugetPath 'extensions'
$chocInstallVariableName = "ChocolateyInstall"
$chocoTools = Join-Path $nuGetPath 'tools'
$nugetExe = Join-Path $chocoTools 'nuget.exe'
$7zip = Join-Path $chocoTools '7za.exe'
$7zip = Join-Path $chocoTools '7z.exe'
$ShimGen = Join-Path $chocoTools 'shimgen.exe'
$checksumExe = Join-Path $chocoTools 'checksum.exe'

Expand Down
28 changes: 17 additions & 11 deletions src/chocolatey.resources/helpers/functions/Get-ChocolateyUnzip.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ function Get-ChocolateyUnzip {
Unzips an archive file and returns the location for further processing.
.DESCRIPTION
This unzips files using the 7-zip standalone command line tool 7za.exe.
Supported archive formats are: 7z, lzma, cab, zip, gzip, bzip2, and tar.
This unzips files using the 7-zip command line tool 7z.exe.
Supported archive formats are listed at:
https://sevenzip.osdn.jp/chm/general/formats.htm
Prior to 0.9.10.1, 7za.exe was used. Supported archive formats for
7za.exe are: 7z, lzma, cab, zip, gzip, bzip2, and tar.
.INPUTS
None
Expand Down Expand Up @@ -98,15 +101,15 @@ param(
Write-Host "Extracting $fileFullPath to $destination..."
if (![System.IO.Directory]::Exists($destination)) { [System.IO.Directory]::CreateDirectory($destination) | Out-Null }

$7zip = Join-Path "$helpersPath" '..\tools\7za.exe'
$7zip = Join-Path "$helpersPath" '..\tools\7z.exe'
if (!([System.IO.File]::Exists($7zip))) {
Update-SessionEnvironment
$7zip = Join-Path "$env:ChocolateyInstall" 'tools\7za.exe'
$7zip = Join-Path "$env:ChocolateyInstall" 'tools\7z.exe'
}
$7zip = [System.IO.Path]::GetFullPath($7zip)
Write-Debug "7zip found at `'$7zip`'"

# 32-bit 7za.exe would not find C:\Windows\System32\config\systemprofile\AppData\Local\Temp,
# 32-bit 7z.exe would not find C:\Windows\System32\config\systemprofile\AppData\Local\Temp,
# because it gets translated to C:\Windows\SysWOW64\... by the WOW redirection layer.
# Replace System32 with sysnative, which does not get redirected.
if ([IntPtr]::Size -ne 4) {
Expand All @@ -117,7 +120,7 @@ param(
$destination32 = $destination
}

$params = "x -aoa -o`"$destination`" -y `"$fileFullPath`""
$params = "x -aoa -bd -bb1 -o`"$destination`" -y `"$fileFullPath`""
Write-Debug "Executing command ['$7zip' $params]"

# Capture 7z's output into a StringBuilder and write it out in blocks, to improve I/O performance.
Expand All @@ -129,8 +132,8 @@ param(
if ($EventArgs.Data -ne $null) {
$line = $EventArgs.Data
Write-Verbose "$line"
if ($line.StartsWith("Extracting")) {
$global:zipFileList.AppendLine($global:zipDestinationFolder + "\" + $line.Substring(12))
if ($line.StartsWith("- ")) {
$global:zipFileList.AppendLine($global:zipDestinationFolder + "\" + $line.Substring(2))
}
}
}
Expand All @@ -143,7 +146,7 @@ param(

$process = New-Object System.Diagnostics.Process
$process.EnableRaisingEvents = $true
Register-ObjectEvent -InputObject $process -SourceIdentifier "LogOutput_ChocolateyZipProc" -EventName OutputDataReceived -Action $writeOutput | Out-Null
Register-ObjectEvent -InputObject $process -SourceIdentifier "LogOutput_ChocolateyZipProc" -EventName OutputDataReceived -Action $writeOutput | Out-Null
Register-ObjectEvent -InputObject $process -SourceIdentifier "LogErrors_ChocolateyZipProc" -EventName ErrorDataReceived -Action $writeError | Out-Null

$process.StartInfo = new-object System.Diagnostics.ProcessStartInfo($7zip, $params)
Expand All @@ -153,7 +156,7 @@ param(
$process.StartInfo.WorkingDirectory = Get-Location
$process.StartInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden

[void] $process.Start()
$process.Start() | Out-Null
if ($process.StartInfo.RedirectStandardOutput) { $process.BeginOutputReadLine() }
if ($process.StartInfo.RedirectStandardError) { $process.BeginErrorReadLine() }
$process.WaitForExit()
Expand All @@ -163,6 +166,9 @@ param(
Unregister-Event -SourceIdentifier "LogOutput_ChocolateyZipProc"
Unregister-Event -SourceIdentifier "LogErrors_ChocolateyZipProc"

# sometimes the process hasn't fully exited yet.
Start-Sleep 1

$exitCode = $process.ExitCode
Set-PowerShellExitCode $exitCode
$process.Dispose()
Expand All @@ -172,7 +178,7 @@ param(
Set-Content $zipExtractLogFullPath $global:zipFileList.ToString() -Encoding UTF8 -Force
}

Write-Debug "7za exit code: $exitCode"
Write-Debug "7z exit code: $exitCode"
switch ($exitCode) {
0 { break }
1 { throw 'Some files could not be extracted' } # this one is returned e.g. for access denied errors
Expand Down
2 changes: 2 additions & 0 deletions src/chocolatey.resources/helpers/functions/Get-FtpFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -198,5 +198,7 @@ param(
if ($ftpresponse -ne $null) {
$ftpresponse.Close()
}

Start-Sleep 1
}
}
2 changes: 2 additions & 0 deletions src/chocolatey.resources/helpers/functions/Get-WebFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ param(
if ($res -ne $null) {
$res.Close()
}

Start-Sleep 1
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,15 @@ param(
[parameter(Mandatory=$true, Position=0)][string] $packageName,
[parameter(Mandatory=$false, Position=1)]
[alias("installerType","installType")][string] $fileType = 'exe',
[parameter(Mandatory=$false, Position=2)][string] $silentArgs = '',
[parameter(Mandatory=$false, Position=2)][string[]] $silentArgs = '',
[parameter(Mandatory=$true, Position=3)][string] $file,
[parameter(Mandatory=$false)] $validExitCodes = @(0),
[parameter(Mandatory=$false)]
[alias("useOnlyPackageSilentArgs")][switch] $useOnlyPackageSilentArguments = $false,
[parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments
)
[string]$silentArgs = $silentArgs -join ' '

Write-Debug "Running 'Install-ChocolateyInstallPackage' for $packageName with file:`'$file`', args: `'$silentArgs`', fileType: `'$fileType`', validExitCodes: `'$validExitCodes`', useOnlyPackageSilentArguments: '$($useOnlyPackageSilentArguments.IsPresent)'";
$installMessage = "Installing $packageName..."
Write-Host $installMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ param(
[parameter(Mandatory=$true, Position=0)][string] $packageName,
[parameter(Mandatory=$false, Position=1)]
[alias("installerType","installType")][string] $fileType = 'exe',
[parameter(Mandatory=$false, Position=2)][string] $silentArgs = '',
[parameter(Mandatory=$false, Position=2)][string[]] $silentArgs = '',
[parameter(Mandatory=$false, Position=3)][string] $url = '',
[parameter(Mandatory=$false, Position=4)]
[alias("url64")][string] $url64bit = '',
Expand All @@ -230,6 +230,7 @@ param(
[alias("useOnlyPackageSilentArgs")][switch] $useOnlyPackageSilentArguments = $false,
[parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments
)
[string]$silentArgs = $silentArgs -join ' '

Write-Debug "Running 'Install-ChocolateyPackage' for $packageName with url:`'$url`', args: `'$silentArgs`', fileType: `'$fileType`', url64bit: `'$url64bit`', checksum: `'$checksum`', checksumType: `'$checksumType`', checksum64: `'$checksum64`', checksumType64: `'$checksumType64`', validExitCodes: `'$validExitCodes`' ";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,14 @@ Install-ChocolateyPackage
Install-ChocolateyInstallPackage
#>
param(
[parameter(Mandatory=$true, Position=0)][string] $statements,
[parameter(Mandatory=$false, Position=0)][string[]] $statements,
[parameter(Mandatory=$false, Position=1)][string] $exeToRun = 'powershell',
[parameter(Mandatory=$false)][switch] $minimized,
[parameter(Mandatory=$false)][switch] $noSleep,
[parameter(Mandatory=$false)] $validExitCodes = @(0),
[parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments
)
[statements]$statements = $statements -join ' '
Write-Debug "Running 'Start-ChocolateyProcessAsAdmin' with exeToRun:`'$exeToRun`', statements: `'$statements`' ";

$wrappedStatements = $statements
Expand Down Expand Up @@ -156,7 +157,7 @@ Elevating Permissions and running [`"$exeToRun`" $wrappedStatements]. This may t

$process = New-Object System.Diagnostics.Process
$process.EnableRaisingEvents = $true
Register-ObjectEvent -InputObject $process -SourceIdentifier "LogOutput_ChocolateyProc" -EventName OutputDataReceived -Action $writeOutput | Out-Null
Register-ObjectEvent -InputObject $process -SourceIdentifier "LogOutput_ChocolateyProc" -EventName OutputDataReceived -Action $writeOutput | Out-Null
Register-ObjectEvent -InputObject $process -SourceIdentifier "LogErrors_ChocolateyProc" -EventName ErrorDataReceived -Action $writeError | Out-Null

#$process.StartInfo = New-Object System.Diagnostics.ProcessStartInfo($exeToRun, $wrappedStatements)
Expand Down Expand Up @@ -191,6 +192,9 @@ Elevating Permissions and running [`"$exeToRun`" $wrappedStatements]. This may t
# them to do so. Without this it never finishes.
Unregister-Event -SourceIdentifier "LogOutput_ChocolateyProc"
Unregister-Event -SourceIdentifier "LogErrors_ChocolateyProc"

# sometimes the process hasn't fully exited yet.
Start-Sleep 1

$exitCode = $process.ExitCode
$process.Dispose()
Expand All @@ -199,6 +203,12 @@ Elevating Permissions and running [`"$exeToRun`" $wrappedStatements]. This may t
if ($validExitCodes -notcontains $exitCode) {
Set-PowerShellExitCode $exitCode
throw "Running [`"$exeToRun`" $statements] was not successful. Exit code was '$exitCode'. See log for possible error messages."
} else {
$chocoSuccessCodes = @(0, 1605, 1614, 1641, 3010)
if ($chocoSuccessCodes -notcontains $exitCode) {
Write-Warning "Exit code '$exitCode' was considered valid, but not as a choco success code. Returning 0"
$exitCode = 0
}
}

Write-Debug "Finishing 'Start-ChocolateyProcessAsAdmin'"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@ param(
[parameter(Mandatory=$true, Position=0)][string] $packageName,
[parameter(Mandatory=$false, Position=1)]
[alias("installerType")][string] $fileType = 'exe',
[parameter(Mandatory=$false, Position=2)][string] $silentArgs = '',
[parameter(Mandatory=$false, Position=2)][string[]] $silentArgs = '',
[parameter(Mandatory=$false, Position=3)][string] $file,
[parameter(Mandatory=$false)] $validExitCodes = @(0),
[parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments
)
[string]$silentArgs = $silentArgs -join ' '

Write-Debug "Running 'Uninstall-ChocolateyPackage' for $packageName with fileType:`'$fileType`', silentArgs: `'$silentArgs`', file: `'$file`'";

$installMessage = "Uninstalling $packageName..."
Expand Down Expand Up @@ -139,6 +141,5 @@ param(
}
}

write-host "$packageName has been uninstalled."
#cutStart-Sleep 3
Write-Host "$packageName has been uninstalled."
}
Binary file added src/chocolatey.resources/tools/7z.dll
Binary file not shown.
Binary file added src/chocolatey.resources/tools/7z.exe
Binary file not shown.
Empty file.
11 changes: 11 additions & 0 deletions src/chocolatey.resources/tools/7z.exe.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" name="7z" processorArchitecture="*" type="win32" />
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
2 changes: 2 additions & 0 deletions src/chocolatey/infrastructure.app/ApplicationParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public static class Environment
public static readonly string Path = "Path";
public static readonly string PathExtensions = "PATHEXT";
public static readonly string PsModulePath = "PSModulePath";
public static readonly string Temp = "TEMP";
public static readonly string SystemUserName = "SYSTEM";
public static readonly string Username = "USERNAME";
public static readonly string ProcessorArchitecture = "PROCESSOR_ARCHITECTURE";
public static readonly string EnvironmentSeparator = ";";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public void RegisterComponents(Container container)
container.Register(() => configuration, Lifestyle.Singleton);
container.Register<IFileSystem, DotNetFileSystem>(Lifestyle.Singleton);
container.Register<IXmlService, XmlService>(Lifestyle.Singleton);
container.Register<IDateTimeService, SystemDateTimeUtcService>(Lifestyle.Singleton);

//nuget
container.Register<ILogger, ChocolateyNugetLogger>(Lifestyle.Singleton);
container.Register<INugetService, NugetService>(Lifestyle.Singleton);
Expand Down Expand Up @@ -119,14 +121,12 @@ public void RegisterComponents(Container container)
{
var list = new List<ITask>
{
new RemovePendingPackagesTask(container.GetInstance<IFileSystem>())
new RemovePendingPackagesTask(container.GetInstance<IFileSystem>(), container.GetInstance<IDateTimeService>())
};

return list.AsReadOnly();
},
Lifestyle.Singleton);

container.Register<IDateTimeService, SystemDateTimeUtcService>(Lifestyle.Singleton);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public string scan_file(string filePath)
private void unzip_die_files(string zipPath, string extractDirectory)
{
//todo: replace with https://github.com/adamhathcock/sharpcompress
var sevenZip = _fileSystem.combine_paths(ApplicationParameters.InstallLocation, "tools", "7za.exe");
var sevenZip = _fileSystem.combine_paths(ApplicationParameters.InstallLocation, "tools", "7z.exe");
CommandExecutor.execute_static(sevenZip, "x -aoa -o\"{0}\" -y \"{1}\"".format_with(extractDirectory, zipPath), 30, _fileSystem.get_current_directory(), (s, e) => { }, (s, e) => { }, false, false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,23 @@ namespace chocolatey.infrastructure.app.tasks
using events;
using filesystem;
using infrastructure.events;
using infrastructure.services;
using infrastructure.tasks;
using logging;
using tolerance;

public class RemovePendingPackagesTask : ITask
{
private readonly IFileSystem _fileSystem;
private readonly IDateTimeService _dateTimeService;
private IDisposable _subscription;
private const int PENDING_FILE_AGE_SECONDS = 10;
private const string PENDING_SKIP_FILE = ".chocolateyPendingSkip";

public RemovePendingPackagesTask(IFileSystem fileSystem)
public RemovePendingPackagesTask(IFileSystem fileSystem, IDateTimeService dateTimeService)
{
_fileSystem = fileSystem;
_dateTimeService = dateTimeService;
}

public void initialize()
Expand All @@ -54,8 +59,25 @@ private void handle_message(PreRunMessage message)
foreach (var pendingFile in pendingFiles.or_empty_list_if_null())
{
var packageFolder = _fileSystem.get_directory_name(pendingFile);
this.Log().Warn("[Pending] Removing incomplete install for '{0}'".format_with(_fileSystem.get_directory_info_for(packageFolder).Name));
var packageFolderName = _fileSystem.get_directory_info_for(packageFolder).Name;

var pendingSkipFiles = _fileSystem.get_files(packageFolder, PENDING_SKIP_FILE, SearchOption.AllDirectories).ToList();
if (pendingSkipFiles.Count != 0)
{
this.Log().Warn("Pending file found for {0}, but a {1} file was also found. Skipping removal".format_with(packageFolderName, PENDING_SKIP_FILE));
continue;
}

// wait for the file to be at least x seconds old
// this allows commands running from the package for configuring sources, etc
var fileInfo = _fileSystem.get_file_info_for(pendingFile);
if (fileInfo.CreationTimeUtc.AddSeconds(PENDING_FILE_AGE_SECONDS) > _dateTimeService.get_current_date_time())
{
this.Log().Debug("Pending file found for {0}, but the file is not {1} seconds old yet.".format_with(packageFolderName, PENDING_FILE_AGE_SECONDS));
continue;
}

this.Log().Warn("[Pending] Removing incomplete install for '{0}'".format_with(packageFolderName));
FaultTolerance.retry(2, () => _fileSystem.delete_directory_if_exists(packageFolder, recursive: true, overrideAttributes: true, isSilent: true), 500, isSilent: true);
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/chocolatey/infrastructure/filesystem/DotNetFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,14 @@ public string get_full_path(string path)

public string get_temp_path()
{
return Path.GetTempPath();
var path = Path.GetTempPath();

if (System.Environment.UserName.contains(ApplicationParameters.Environment.SystemUserName) || path.contains("config\\systemprofile\\appdata"))
{
path = System.Environment.ExpandEnvironmentVariables(System.Environment.GetEnvironmentVariable(ApplicationParameters.Environment.Temp, EnvironmentVariableTarget.Machine).to_string());
}

return path;
}

public char get_path_directory_separator_char()
Expand Down

0 comments on commit 40f3066

Please sign in to comment.