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
18 changes: 18 additions & 0 deletions src/System.Management.Automation/namespaces/FileSystemProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4953,6 +4953,23 @@ protected override string NormalizeRelativePath(
return String.Empty;
}

#if UNIX
// We don't use the Directory class for Unix because the path
// may contain additional globbing patterns such as '[ab]'
// which Directory.EnumerateFiles() processes, giving undesireable
// results in this context.
if (!Utils.NativeItemExists(result))
{
String error = StringUtil.Format(FileSystemProviderStrings.ItemDoesNotExist, path);
Exception e = new IOException(error);
WriteError(new ErrorRecord(
e,
"ItemDoesNotExist",
ErrorCategory.ObjectNotFound,
path));
break;
}
#else
string leafName = GetChildName(result);

// Use the Directory class to get the real path (this will
Expand All @@ -4978,6 +4995,7 @@ protected override string NormalizeRelativePath(
}

result = files.First();
#endif

if (result.StartsWith(basePath, StringComparison.CurrentCulture))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,49 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" {
}
}

Describe "Handling of globbing patterns" -Tags "CI" {

Context "Handling of Unix [ab] globbing patterns in literal paths" {
BeforeAll {
$filePath = Join-Path $TESTDRIVE "file[txt].txt"
$newPath = Join-Path $TESTDRIVE "file.txt.txt"
$dirPath = Join-Path $TESTDRIVE "subdir"
}
BeforeEach {
$file = New-Item -ItemType File -Path $filePath -Force
}
AfterEach
{
Remove-Item -Force -Recurse -Path $dirPath -ErrorAction SilentlyContinue
Remove-Item -Force -LiteralPath $newPath -ErrorAction SilentlyContinue
}

It "Rename-Item -LiteralPath can rename a file with Unix globbing characters" {
Rename-Item -LiteralPath $file.FullName -NewName $newPath
Test-Path -LiteralPath $file.FullName | Should Be $false
Test-Path -LiteralPath $newPath | Should Be $true
}

It "Remove-Item -LiteralPath can delete a file with Unix globbing characters" {
Remove-Item -LiteralPath $file.FullName
Test-Path -LiteralPath $file.FullName | Should Be $false
}

It "Move-Item -LiteralPath can move a file with Unix globbing characters" {
$dir = New-Item -ItemType Directory -Path $dirPath
Move-Item -LiteralPath $file.FullName -Destination $dir.FullName
Test-Path -LiteralPath $file.FullName | Should Be $false
$newPath = Join-Path $dir.FullName $file.Name
Test-Path -LiteralPath $newPath | Should Be $true
}

It "Copy-Item -LiteralPath can copy a file with Unix globbing characters" {
Copy-Item -LiteralPath $file.FullName -Destination $newPath
Test-Path -LiteralPath $newPath | Should Be $true
}
}
}

Describe "Hard link and symbolic link tests" -Tags "CI", "RequireAdminOnWindows" {
BeforeAll {
# on macOS, the /tmp directory is a symlink, so we'll resolve it here
Expand Down