Skip to content

Commit

Permalink
Show better error message when an extension file can't be overwritten
Browse files Browse the repository at this point in the history
  • Loading branch information
mlocati committed Nov 8, 2019
1 parent 541a4a3 commit 7166d88
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
35 changes: 35 additions & 0 deletions PhpManager/private/Test-IsFileWritable.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function Test-IsFileWritable {
<#
.Synopsis
Check if one or more existing files are writable
.Parameter Path
The path to the file.
.Parameter IfNotExist
How to consider the case if Path is not a file ($false: file is not writable, $true: file is writable - default: $true)
#>
[OutputType([bool])]
param (
[Parameter(Mandatory = $true, Position = 0)]
[ValidateNotNull()]
[ValidateLength(1, [int]::MaxValue)]
[string]$Path,
[Parameter(Mandatory = $false, Position = 1)]
[bool]$IfNotExist = $true
)
if (Test-Path -LiteralPath $Path -PathType Leaf) {
try {
[System.IO.File]::OpenWrite($Path).Close()
$result = $true
}
catch [System.IO.IOException] {
Write-Verbose $_.Exception.Message
$result = $false
}
}
else {
$result = $IfNotExist
}
$result
}
34 changes: 23 additions & 11 deletions PhpManager/public/Install-PhpExtension.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
process {
if ($null -eq $Path -or $Path -eq '') {
$phpVersion = [PhpVersionInstalled]::FromEnvironmentOne()
} else {
}
else {
$phpVersion = [PhpVersionInstalled]::FromPath($Path)
}
if ($phpVersion.ExtensionsPath -eq '') {
Expand All @@ -66,7 +67,8 @@
throw 'You can''t specify the -MinimumStability argument if you specify an existing file with the -Extension argument'
}
$dllPath = [System.IO.Path]::GetFullPath($Extension)
} else {
}
else {
if ($null -eq $MinimumStability -or $MinimumStability -eq '') {
$MinimumStability = $Script:PEARSTATE_STABLE
}
Expand Down Expand Up @@ -94,8 +96,9 @@
$archiveUrl = Get-PeclArchiveUrl -PackageHandle $peclPackageHandle -PackageVersion $peclPackageVersion -PhpVersion $phpVersion -MinimumStability $MinimumStability
if ($archiveUrl -eq '') {
Write-Verbose ("No Windows DLLs found for PECL package {0} {1} compatible with {2}" -f $peclPackageHandle, $peclPackageVersion, $phpVersion.DisplayName)
} else {
$availablePackageVersion = @{PackageVersion = $peclPackageVersion; PackageArchiveUrl = $archiveUrl}
}
else {
$availablePackageVersion = @{PackageVersion = $peclPackageVersion; PackageArchiveUrl = $archiveUrl }
break
}
}
Expand All @@ -118,31 +121,38 @@
throw ("Multiple PHP DLL found in archive downloaded from {0}" -f $availablePackageVersion.PackageArchiveUrl)
}
$dllPath = $phpDlls[0].FullName
} catch {
}
catch {
$keepZip = $false
throw
} finally {
}
finally {
if (-Not($keepZip)) {
try {
Remove-Item -Path $zip -Force
} catch {
}
catch {
Write-Debug 'Failed to remove temporary zip file'
}
}
}
}
$newExtension = Get-PhpExtensionDetail -PhpVersion $phpVersion -Path $dllPath
$oldExtension = Get-PhpExtension -Path $phpVersion.ExecutablePath | Where-Object {$_.Handle -eq $newExtension.Handle}
$oldExtension = Get-PhpExtension -Path $phpVersion.ExecutablePath | Where-Object { $_.Handle -eq $newExtension.Handle }
if ($null -ne $oldExtension) {
if ($oldExtension.Type -eq $Script:EXTENSIONTYPE_BUILTIN) {
Write-Verbose ("'{0}' is a builtin extension" -f $oldExtension.Name)
}
Write-Verbose ("Upgrading extension '{0}' from version {1} to version {2}" -f $oldExtension.Name, $oldExtension.Version, $newExtension.Version)
if (-Not(Test-IsFileWritable($oldExtension.Filename))) {
throw "Unable to write to the file $($oldExtension.Filename)"
}
Move-Item -Path $dllPath -Destination $oldExtension.Filename -Force
if ($oldExtension.State -eq $Script:EXTENSIONSTATE_DISABLED -and -Not($DontEnable)) {
Enable-PhpExtension -Extension $oldExtension.Name -Path $phpVersion.ExecutablePath
}
} else {
}
else {
Write-Verbose ("Installing new extension '{0}' version {1}" -f $newExtension.Name, $newExtension.Version)
Install-PhpExtensionPrerequisite -PhpVersion $phpVersion -Extension $newExtension
$newExtensionFilename = [System.IO.Path]::Combine($phpVersion.ExtensionsPath, [System.IO.Path]::GetFileName($dllPath))
Expand All @@ -153,11 +163,13 @@
Enable-PhpExtension -Extension $newExtension.Name -Path $phpVersion.ExecutablePath
}
}
} finally {
}
finally {
if ($null -ne $tempFolder) {
try {
Remove-Item -Path $tempFolder -Recurse -Force
} catch {
}
catch {
Write-Debug 'Failed to remove temporary folder'
}
}
Expand Down

0 comments on commit 7166d88

Please sign in to comment.