Add FileHandleType enum and SafeFileHandle.Type property#124561
Add FileHandleType enum and SafeFileHandle.Type property#124561adamsitnik merged 33 commits intomainfrom
Conversation
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
|
Tagging subscribers to this area: @dotnet/area-system-io |
adamsitnik
left a comment
There was a problem hiding this comment.
@copilot please address my feedback
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetNamedPipeInfo.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Unix.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Windows.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Unix.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/SafeFileHandle/GetFileType.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/SafeFileHandle/GetFileType.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/SafeFileHandle/GetFileType.cs
Show resolved
Hide resolved
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
adamsitnik
left a comment
There was a problem hiding this comment.
@copilot address my feedback
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/System.IO.FileSystem.Tests.csproj
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/System.IO.FileSystem.Tests.csproj
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Windows.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Windows.cs
Outdated
Show resolved
Hide resolved
…API in CanSeek Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
adamsitnik
left a comment
There was a problem hiding this comment.
@copilot please address my feedback
...libraries/System.Runtime/tests/System.IO.FileSystem.Tests/SafeFileHandle/GetFileType.Unix.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems
Outdated
Show resolved
Hide resolved
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Co-authored-by: Stephen Toub <stoub@microsoft.com>
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.FILE_ATTRIBUTE_TAG_INFO.cs
Show resolved
Hide resolved
So far, |
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
src/libraries/System.IO.Ports/src/System/IO/Ports/SerialStream.Windows.cs
Outdated
Show resolved
Hide resolved
adamsitnik
left a comment
There was a problem hiding this comment.
@copilot please address my feedback
Description
Adds
System.IO.FileHandleTypeenum andSafeFileHandle.Typeproperty for efficient runtime detection of file handle types. POC implementation to validate API feasibility before formal proposal (issue #122806).Changes
API Surface
System.IO.FileHandleTypeenum: 8 values (Unknown, RegularFile, Pipe, Socket, CharacterDevice, Directory, SymbolicLink, BlockDevice). All values supported cross-platform; BlockDevice returns Unknown on Windows.SafeFileHandle.Typeproperty: Returns cachedFileHandleType. ThrowsObjectDisposedExceptionwhen closed. Single syscall per handle lifetime viavolatile int _cachedFileType.Implementation
Windows (
SafeFileHandle.Windows.cs):GetFileTypeCore(): MapsGetFileType()result to enum valuesFILE_TYPE_PIPE: CallsGetNamedPipeInfo; success → Pipe, ERROR_PIPE_NOT_CONNECTED → Pipe, else → SocketFILE_TYPE_DISK: Queries attributes + reparse tag viaGetFileInformationByHandleEx; only IO_REPARSE_TAG_SYMLINK → SymbolicLink, other reparse points → UnknownCanSeek: Now checksType == RegularFile(excludes directories/symlinks, was FILE_TYPE_DISK)Unix (
SafeFileHandle.Unix.cs):GetFileTypeCore(): fstat + S_IFMT mode bit mappingInit(): Opportunistically caches type when fstat already performed (line 343)Interop:
Interop.GetNamedPipeInfo.cs: Parameter changed SafePipeHandle → SafeHandle (CoreLib cannot reference SafePipeHandle)Interop.FILE_ATTRIBUTE_TAG_INFO.cs: New struct + FileAttributeTagInfo constant for reparse tag queriesInterop.GetFileType_SafeHandle.cs: Added to System.IO.Ports for downlevel multi-targetingSystem.IO.Ports:
SerialStream.Windows.cs: Uses Type property on NET11_0_OR_GREATER, falls back to GetFileType() on older targetsExample
Notes
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.