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
37 changes: 26 additions & 11 deletions src/System.Management.Automation/namespaces/LocationGlobber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1665,9 +1665,18 @@ internal static bool IsAbsolutePath(string path)

if (index > 0)
{
// We must have a drive specified

result = true;
// see if there are any path separators before the colon which would mean the
// colon is part of a file or folder name and not a drive: ./foo:bar vs foo:bar
int separator = path.IndexOf(StringLiterals.DefaultPathSeparator, 0, index-1);
if (separator == -1)
{
separator = path.IndexOf(StringLiterals.AlternatePathSeparator, 0, index-1);
}
if (separator == -1 || index < separator)
{
// We must have a drive specified
result = true;
}
}
} while (false);

Expand Down Expand Up @@ -3404,21 +3413,27 @@ private static string RemoveDriveQualifier(string path)

string result = path;

// Find the drive separator
// Find the drive separator only if it's before a path separator

int index = path.IndexOf(":", StringComparison.Ordinal);

if (index != -1)
{
// Remove the \ or / if it follows the drive indicator

if (path[index + 1] == '\\' ||
path[index + 1] == '/')
int separator = path.IndexOf(StringLiterals.DefaultPathSeparator, 0, index);
if (separator == -1)
{
++index;
separator = path.IndexOf(StringLiterals.AlternatePathSeparator, 0, index);
}
if (separator == -1 || index < separator)
{
// Remove the \ or / if it follows the drive indicator
if (path[index + 1] == '\\' ||
path[index + 1] == '/')
{
++index;
}

result = path.Substring(index + 1);
result = path.Substring(index + 1);
}
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,43 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" {
New-Item -Path $testFile -ItemType File -Force -ErrorAction SilentlyContinue
}
}

It "Set-Location on Unix succeeds with folder with colon: <path>" -Skip:($IsWindows) -TestCases @(
@{path="\hello:world"},
@{path="\:world"},
@{path="/hello:"}
) {
param($path)
try {
New-Item -Path "$testdrive$path" -ItemType Directory > $null
Set-Location "$testdrive"
Set-Location ".$path"
(Get-Location).Path | Should Be "$testdrive/$($path.Substring(1,$path.Length-1))"
}
finally {
Remove-Item -Path "$testdrive$path" -ErrorAction SilentlyContinue
}
}

It "Get-Content on Unix succeeds with folder and file with colon: <path>" -Skip:($IsWindows) -TestCases @(
@{path="\foo:bar.txt"},
@{path="/foo:"},
@{path="\:bar"}
) {
param($path)
try {
$testPath = "$testdrive/hello:world"
New-Item -Path "$testPath" -ItemType Directory > $null
Set-Content -Path "$testPath$path" -Value "Hello"
$files = Get-ChildItem "$testPath"
$files.Count | Should Be 1
$files[0].Name | Should BeExactly $path.Substring(1,$path.Length-1)
$files[0] | Get-Content | Should BeExactly "Hello"
}
finally {
Remove-Item -Path $testPath -Recurse -Force -ErrorAction SilentlyContinue
}
}
}

Context "Validate behavior when access is denied" {
Expand Down