Avoid extra SMB queries when formatting file names (#28038)

This commit is contained in:
Dongbo Wang authored and GitHub committed 2026-10-09 09:35:18 -07:00
1 parent 29a743d40f
commit c4ae347dc9
2 files changed
+16 -1

No files matched your search

@@ -1953,7 +1953,10 @@ namespace Microsoft.PowerShell.Commands
{
if (instance?.BaseObject is FileSystemInfo fileInfo)
{
if (InternalSymbolicLinkLinkCodeMethods.IsReparsePointLikeSymlink(fileInfo))
// Checking the cached attributes first avoids an extra filesystem query for every non-link,
// which is particularly expensive when formatting items on an SMB share.
if (InternalSymbolicLinkLinkCodeMethods.IsReparsePoint(fileInfo)
&& InternalSymbolicLinkLinkCodeMethods.IsReparsePointLikeSymlink(fileInfo))
{
return $"{PSStyle.Instance.FileInfo.SymbolicLink}{fileInfo.Name}{PSStyle.Instance.Reset} -> {fileInfo.LinkTarget}";
}
@@ -237,6 +237,18 @@ Describe 'Formatting for FileInfo objects' -Tags 'CI' {
$dir.NameString | Should -BeExactly "$($PSStyle.FileInfo.Directory + $dir.Name + $PSStyle.Reset)"
}
It 'NameString should not query link metadata for a cached non-reparse point' -Skip:(-not $IsWindows) {
$testFile = Join-Path -Path $TestDrive -ChildPath 'cached-metadata'
$file = New-Item -ItemType File -Path $testFile
$null = $file.Attributes
# Removing the file makes an unnecessary second filesystem query observable.
[System.IO.File]::Delete($testFile)
# Without the cached-attributes check, NameString queries the deleted path for link metadata and throws.
[Microsoft.PowerShell.Commands.FileSystemProvider]::NameString($file) | Should -BeExactly $file.Name
}
It 'Executable should have correct color' {
if ($IsWindows) {
$exePath = Join-Path -Path $TestDrive -ChildPath 'myExe.exe'