mirror of
https://github.com/PowerShell/PowerShell.git
synced 2024-11-23 17:53:58 +08:00
Fix unblock read only file (#4395)
Adds a new non-terminating error when a file is read-only
This commit is contained in:
parent
e83fcca277
commit
813e4a661a
@ -7,6 +7,7 @@ Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Management.Automation;
|
||||
using System.Management.Automation.Internal;
|
||||
@ -113,7 +114,15 @@ namespace Microsoft.PowerShell.Commands
|
||||
{
|
||||
if (ShouldProcess(path))
|
||||
{
|
||||
AlternateDataStreamUtilities.DeleteFileStream(path, "Zone.Identifier");
|
||||
try
|
||||
{
|
||||
AlternateDataStreamUtilities.DeleteFileStream(path, "Zone.Identifier");
|
||||
}
|
||||
catch (Win32Exception accessException)
|
||||
{
|
||||
WriteError(new ErrorRecord(accessException, "RemoveItemUnauthorizedAccessError", ErrorCategory.PermissionDenied, path));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8762,7 +8762,11 @@ namespace System.Management.Automation.Internal
|
||||
}
|
||||
string resultPath = path + adjustedStreamName;
|
||||
|
||||
NativeMethods.DeleteFile(resultPath);
|
||||
if (!NativeMethods.DeleteFile(resultPath))
|
||||
{
|
||||
int error = Marshal.GetLastWin32Error();
|
||||
throw new Win32Exception(error);
|
||||
}
|
||||
}
|
||||
|
||||
internal static void SetZoneOfOrigin(string path, SecurityZone securityZone)
|
||||
|
@ -7,7 +7,7 @@ function Test-UnblockFile {
|
||||
return $true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $false
|
||||
}
|
||||
|
||||
@ -37,23 +37,11 @@ Describe "Unblock-File" -Tags "CI" {
|
||||
}
|
||||
|
||||
It "With '-Path': no file exist" {
|
||||
try {
|
||||
Unblock-File -Path nofileexist.ttt -ErrorAction Stop
|
||||
throw "No Exception!"
|
||||
}
|
||||
catch {
|
||||
$_.FullyQualifiedErrorId | Should Be "FileNotFound,Microsoft.PowerShell.Commands.UnblockFileCommand"
|
||||
}
|
||||
{ Unblock-File -Path nofileexist.ttt -ErrorAction Stop } | ShouldBeErrorId "FileNotFound,Microsoft.PowerShell.Commands.UnblockFileCommand"
|
||||
}
|
||||
|
||||
It "With '-LiteralPath': no file exist" {
|
||||
try {
|
||||
Unblock-File -LiteralPath nofileexist.ttt -ErrorAction Stop
|
||||
throw "No Exception!"
|
||||
}
|
||||
catch {
|
||||
$_.FullyQualifiedErrorId | Should Be "FileNotFound,Microsoft.PowerShell.Commands.UnblockFileCommand"
|
||||
}
|
||||
{ Unblock-File -LiteralPath nofileexist.ttt -ErrorAction Stop } | ShouldBeErrorId "FileNotFound,Microsoft.PowerShell.Commands.UnblockFileCommand"
|
||||
}
|
||||
|
||||
It "With '-Path': file exist" {
|
||||
@ -65,4 +53,21 @@ Describe "Unblock-File" -Tags "CI" {
|
||||
Unblock-File -LiteralPath $testfilepath
|
||||
Test-UnblockFile | Should Be $true
|
||||
}
|
||||
|
||||
It "Write an error if a file is read only" {
|
||||
$TestFile = Join-Path $TestDrive "testfileunlock.ps1"
|
||||
Set-Content -Path $TestFile -value 'test'
|
||||
$ZoneIdentifier = {
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
}
|
||||
Set-Content -Path $TestFile -Value $ZoneIdentifier -Stream 'Zone.Identifier'
|
||||
Set-ItemProperty -Path $TestFile -Name IsReadOnly -Value $True
|
||||
|
||||
$TestFileCreated = Get-ChildItem $TestFile
|
||||
$TestFileCreated.IsReadOnly | Should Be $true
|
||||
|
||||
{ Unblock-File -LiteralPath $TestFile -ErrorAction SilentlyContinue } | Should Not Throw
|
||||
$error[0].FullyQualifiedErrorId | Should Be "RemoveItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.UnblockFileCommand"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user