From 04c9ab9af23745689cbc2d6feb179c269f592640 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 3 Oct 2017 15:20:46 -0700 Subject: [PATCH 1/5] enable using filesystem from a UNC location --- .../namespaces/NavigationProviderBase.cs | 7 +++++++ .../FileSystem.Tests.ps1 | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/System.Management.Automation/namespaces/NavigationProviderBase.cs b/src/System.Management.Automation/namespaces/NavigationProviderBase.cs index dbfad2c48f6..cdb791fd8ce 100644 --- a/src/System.Management.Automation/namespaces/NavigationProviderBase.cs +++ b/src/System.Management.Automation/namespaces/NavigationProviderBase.cs @@ -424,6 +424,13 @@ protected string MakePath(string parent, string child, bool childIsLeaf) // Joins the paths StringBuilder builder = new StringBuilder(parent, parent.Length + child.Length + 1); +#if !UNIX + // On Windows, if we have a single backslash for the parent, it's a UNC path so we need to add another backslash back + if (String.Compare(parent, StringLiterals.DefaultPathSeparatorString, StringComparison.Ordinal) == 0) + { + builder.Append('\\'); + } +#endif if (parent.EndsWith(StringLiterals.DefaultPathSeparatorString, StringComparison.Ordinal)) { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index 909cc24de08..5461cb76461 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -1301,3 +1301,19 @@ Describe "Extended FileSystem Path/Location Cmdlet Provider Tests" -Tags "Featur } } } + +Describe "UNC paths" -Tags 'CI' { + It "Can Get-ChildItems from a UNC location" -Skip:(!$IsWindows) { + try { + $systemDrive = ($env:SystemDrive).Replace(":","$") + $testPath = Join-Path "\\localhost" $systemDrive + Push-Location $testPath + Get-Location | Should BeExactly "Microsoft.PowerShell.Core\FileSystem::$testPath" + $children = { Get-ChildItem -ErrorAction Stop } | Should Not Throw + $children.Count | Should BeGreaterThan 0 + } + finally { + Pop-Location + } + } +} From 7d4db2118f25fe75e3a2e230aad9060fbbac4472 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 3 Oct 2017 17:29:01 -0700 Subject: [PATCH 2/5] [feature] address PR feedback --- .../namespaces/NavigationProviderBase.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/namespaces/NavigationProviderBase.cs b/src/System.Management.Automation/namespaces/NavigationProviderBase.cs index cdb791fd8ce..3ae68610ecf 100644 --- a/src/System.Management.Automation/namespaces/NavigationProviderBase.cs +++ b/src/System.Management.Automation/namespaces/NavigationProviderBase.cs @@ -426,9 +426,10 @@ protected string MakePath(string parent, string child, bool childIsLeaf) StringBuilder builder = new StringBuilder(parent, parent.Length + child.Length + 1); #if !UNIX // On Windows, if we have a single backslash for the parent, it's a UNC path so we need to add another backslash back - if (String.Compare(parent, StringLiterals.DefaultPathSeparatorString, StringComparison.Ordinal) == 0) + if (this.ProviderInfo.FullName.Equals(@"Microsoft.PowerShell.Core\FileSystem", StringComparison.OrdinalIgnoreCase) && + String.Compare(parent, StringLiterals.DefaultPathSeparatorString, StringComparison.Ordinal) == 0) { - builder.Append('\\'); + builder.Append(StringLiterals.DefaultPathSeparator); } #endif From cf3736cbb5bedf955acb550ae4f12500a0951fa8 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 3 Oct 2017 18:22:56 -0700 Subject: [PATCH 3/5] [feature] removed changed in NavigationProviderBase and made change in FileSystemProvider where it should belong --- .../namespaces/FileSystemProvider.cs | 10 +++++++++- .../namespaces/NavigationProviderBase.cs | 8 -------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index eca208ec5ad..c24cfa4ff92 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -98,7 +98,15 @@ public FileSystemProvider() /// private static string NormalizePath(string path) { - return path.Replace(StringLiterals.AlternatePathSeparator, StringLiterals.DefaultPathSeparator); + path = path.Replace(StringLiterals.AlternatePathSeparator, StringLiterals.DefaultPathSeparator); +#if !UNIX + // since this is an absolute path, if it starts with a single backslash, we need to add another to make it a UNC path + if (path.Length >= 2 && path[0] == StringLiterals.DefaultPathSeparator && path[1] != StringLiterals.DefaultPathSeparator) + { + path = StringLiterals.DefaultPathSeparatorString + path; + } +#endif + return path; } // NormalizePath diff --git a/src/System.Management.Automation/namespaces/NavigationProviderBase.cs b/src/System.Management.Automation/namespaces/NavigationProviderBase.cs index 3ae68610ecf..dbfad2c48f6 100644 --- a/src/System.Management.Automation/namespaces/NavigationProviderBase.cs +++ b/src/System.Management.Automation/namespaces/NavigationProviderBase.cs @@ -424,14 +424,6 @@ protected string MakePath(string parent, string child, bool childIsLeaf) // Joins the paths StringBuilder builder = new StringBuilder(parent, parent.Length + child.Length + 1); -#if !UNIX - // On Windows, if we have a single backslash for the parent, it's a UNC path so we need to add another backslash back - if (this.ProviderInfo.FullName.Equals(@"Microsoft.PowerShell.Core\FileSystem", StringComparison.OrdinalIgnoreCase) && - String.Compare(parent, StringLiterals.DefaultPathSeparatorString, StringComparison.Ordinal) == 0) - { - builder.Append(StringLiterals.DefaultPathSeparator); - } -#endif if (parent.EndsWith(StringLiterals.DefaultPathSeparatorString, StringComparison.Ordinal)) { From 070f70bddb99d60aa991df19c0257004ec1d8570 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 3 Oct 2017 21:06:00 -0700 Subject: [PATCH 4/5] added variations of tests for set-location and push-location no need to run [feature] anymore since it passed previously and the test case added is CI --- .../FileSystem.Tests.ps1 | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index 5461cb76461..504d28a2bf4 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -1303,17 +1303,22 @@ Describe "Extended FileSystem Path/Location Cmdlet Provider Tests" -Tags "Featur } Describe "UNC paths" -Tags 'CI' { - It "Can Get-ChildItems from a UNC location" -Skip:(!$IsWindows) { + It "Can Get-ChildItems from a UNC location using " -Skip:(!$IsWindows) -TestCases @( + @{cmdlet="Push-Location"}, + @{cmdlet="Set-Location"} + ) { + param($cmdlet) + $originalLocation = Get-Location try { $systemDrive = ($env:SystemDrive).Replace(":","$") $testPath = Join-Path "\\localhost" $systemDrive - Push-Location $testPath + & $cmdlet $testPath Get-Location | Should BeExactly "Microsoft.PowerShell.Core\FileSystem::$testPath" $children = { Get-ChildItem -ErrorAction Stop } | Should Not Throw $children.Count | Should BeGreaterThan 0 } finally { - Pop-Location + Set-Location $originalLocation } } } From 87b153cc7389b4a3ffae37c5a96bad09decd3378 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 4 Oct 2017 07:21:01 -0700 Subject: [PATCH 5/5] [feature] move code to reproduce UNC path to GetParentPath() --- .../namespaces/FileSystemProvider.cs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index c24cfa4ff92..172e8849570 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -98,15 +98,7 @@ public FileSystemProvider() /// private static string NormalizePath(string path) { - path = path.Replace(StringLiterals.AlternatePathSeparator, StringLiterals.DefaultPathSeparator); -#if !UNIX - // since this is an absolute path, if it starts with a single backslash, we need to add another to make it a UNC path - if (path.Length >= 2 && path[0] == StringLiterals.DefaultPathSeparator && path[1] != StringLiterals.DefaultPathSeparator) - { - path = StringLiterals.DefaultPathSeparatorString + path; - } -#endif - return path; + return path.Replace(StringLiterals.AlternatePathSeparator, StringLiterals.DefaultPathSeparator); } // NormalizePath @@ -4825,6 +4817,13 @@ protected override string GetParentPath(string path, string root) { parentPath = EnsureDriveIsRooted(parentPath); } +#if !UNIX + else if (parentPath.Equals(StringLiterals.DefaultPathSeparatorString, StringComparison.Ordinal)) + { + // make sure we return two backslashes so it still results in a UNC path + parentPath = "\\\\"; + } +#endif return parentPath; } // GetParentPath