From 5943c519f173936227645361d1c93e0da8a1a441 Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Tue, 24 Jul 2018 20:43:32 -0700 Subject: [PATCH 1/2] IsHardLink() check fails on some files which causes the Mode property not to be defined --- .../namespaces/FileSystemProvider.cs | 13 +++++-------- .../Get-ChildItem.Tests.ps1 | 16 ++++++++++------ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index c559c1f4c8e..009ca8bbc9a 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -8136,15 +8136,12 @@ internal static bool WinIsHardLink(ref IntPtr handle) BY_HANDLE_FILE_INFORMATION handleInfo; bool succeeded = InternalSymbolicLinkLinkCodeMethods.GetFileInformationByHandle(handle, out handleInfo); - if (!succeeded) + if (succeeded) { - int lastError = Marshal.GetLastWin32Error(); - throw new Win32Exception(lastError); - } - - if (handleInfo.NumberOfLinks > 1) - { - return true; + if (handleInfo.NumberOfLinks > 1) + { + return true; + } } return false; diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-ChildItem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-ChildItem.Tests.ps1 index 347de090281..d8c42850b64 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-ChildItem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-ChildItem.Tests.ps1 @@ -76,14 +76,22 @@ Describe "Get-ChildItem" -Tags "CI" { Pop-Location } - It "Should have a the proper fields and be populated" { + It "Should have all the proper fields and be populated" { $var = Get-Childitem . $var.Name.Length | Should -BeGreaterThan 0 $var.Mode.Length | Should -BeGreaterThan 0 $var.LastWriteTime | Should -BeGreaterThan 0 $var.Length.Length | Should -BeGreaterThan 0 + } + It "Should have mode property populated for protected files on Windows" -Skip:(!$IsWindows) { + $files = Get-Childitem -Force ~\NT* + $files.Count | Should -BeGreaterThan 0 + foreach ($file in $files) + { + $file.Mode | Should -Not -BeNullOrEmpty + } } It "Should list files in sorted order" { @@ -153,11 +161,7 @@ Describe "Get-ChildItem" -Tags "CI" { (Get-ChildItem -Path $searchRoot -Directory -Recurse).Count | Should -Be 1 } - It "Should give .sys file if the fullpath is specified with hidden and force parameter" -Pending:$true { - # Enable the test after move to .Net Core 2.1.1 - # The tracking issue https://github.com/dotnet/corefx/issues/29782 - # - #It "Should give .sys file if the fullpath is specified with hidden and force parameter" -Skip:(!$IsWindows){ + It "Should give .sys file if the fullpath is specified with hidden and force parameter" -Skip:(!$IsWindows) { # Don't remove!!! It is special test for hidden and opened file with exclusive lock. $file = Get-ChildItem -path "$env:SystemDrive\\pagefile.sys" -Hidden $file | Should not be $null From 4e006446864eba30721c9bc1b98c0a3eb769905e Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Wed, 25 Jul 2018 09:25:01 -0700 Subject: [PATCH 2/2] address Ilya's feedback --- .../namespaces/FileSystemProvider.cs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 009ca8bbc9a..bedf94e60eb 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -8135,16 +8135,7 @@ internal static bool WinIsHardLink(ref IntPtr handle) { BY_HANDLE_FILE_INFORMATION handleInfo; bool succeeded = InternalSymbolicLinkLinkCodeMethods.GetFileInformationByHandle(handle, out handleInfo); - - if (succeeded) - { - if (handleInfo.NumberOfLinks > 1) - { - return true; - } - } - - return false; + return succeeded && (handleInfo.NumberOfLinks > 1); } private static string InternalGetTarget(SafeFileHandle handle)