Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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;
Expand Down Expand Up @@ -113,7 +114,15 @@ protected override void ProcessRecord()
{
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));
}

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8770,7 +8770,11 @@ internal static void DeleteFileStream(string path, string streamName)
}
string resultPath = path + adjustedStreamName;

NativeMethods.DeleteFile(resultPath);
if (!NativeMethods.DeleteFile(resultPath))
{
int error = Marshal.GetLastWin32Error();
throw new Win32Exception(error);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be a non-terminating error? The following should behave similarly:

dir | Unblock-File
dir | Remove-Item

In the Remove-Item case, a read only file reports an error, but all other files are deleted.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!
AlternateDataStreamUtilities uses the pattern. So maybe add try-catch in Unblock-File cmdlet file?

}
}

internal static void SetZoneOfOrigin(string path, SecurityZone securityZone)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function Test-UnblockFile {
return $true
}
}

return $false
}

Expand Down Expand Up @@ -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" {
Expand All @@ -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"
}
}