Add FileType enum and SafeFileHandle.GetFileType() method#124561
Draft
Add FileType enum and SafeFileHandle.GetFileType() method#124561
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>
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-io |
Copilot
AI
changed the title
[WIP] Add FileType enum and extend SafeFileHandle with GetFileType
Add FileType enum and SafeFileHandle.GetFileType() method
Feb 18, 2026
adamsitnik
reviewed
Feb 18, 2026
Member
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>
Contributor
Author
adamsitnik
reviewed
Feb 18, 2026
Member
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>
Contributor
Author
adamsitnik
reviewed
Feb 18, 2026
Member
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
Show resolved
Hide resolved
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Contributor
Author
Member
|
To whom it may concern: I wanted to implement new API to ensure it's possible before proposing it (#122806) |
This was referenced Feb 18, 2026
adamsitnik
reviewed
Feb 19, 2026
Member
adamsitnik
left a comment
There was a problem hiding this comment.
@copilot address my feedback to fix the build errors
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/System.IO.FileSystem.Tests.csproj
Show resolved
Hide resolved
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Contributor
Author
adamsitnik
reviewed
Feb 19, 2026
Member
adamsitnik
left a comment
There was a problem hiding this comment.
@copilot fix following build failures:
##[error]src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/SafeFileHandle/GetFileType.Unix.cs(19,25): error CS0029: (NETCORE_ENGINEERING_TELEMETRY=Build) Cannot implicitly convert type 'Microsoft.Win32.SafeHandles.SafeFileHandle' to 'nint'
/__w/1/s/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/SafeFileHandle/GetFileType.Unix.cs(19,25): error CS0029: Cannot implicitly convert type 'Microsoft.Win32.SafeHandles.SafeFileHandle' to 'nint' [/__w/1/s/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/System.IO.FileSystem.Tests.csproj::TargetFramework=net11.0-unix]
##[error]src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/SafeFileHandle/GetFileType.Unix.cs(29,41): error CS0117: (NETCORE_ENGINEERING_TELEMETRY=Build) 'Interop.Sys' does not contain a definition for 'MkFifo'
/__w/1/s/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/SafeFileHandle/GetFileType.Unix.cs(29,41): error CS0117: 'Interop.Sys' does not contain a definition for 'MkFifo' [/__w/1/s/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/System.IO.FileSystem.Tests.csproj::TargetFramework=net11.0-unix]
##[error]src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/SafeFileHandle/GetFileType.Unix.cs(31,13): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'Task' could not be found (are you missing a using directive or an assembly reference?)
/__w/1/s/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/SafeFileHandle/GetFileType.Unix.cs(31,13): error CS0246: The type or namespace name 'Task' could not be found (are you missing a using directive or an assembly reference?) [/__w/1/s/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/System.IO.FileSystem.Tests.csproj::TargetFramework=net11.0-unix]
##[error]src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/SafeFileHandle/GetFileType.Unix.cs(31,31): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'Task' does not exist in the current context
/__w/1/s/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/SafeFileHandle/GetFileType.Unix.cs(31,31): error CS0103: The name 'Task' does not exist in the current context [/__w/1/s/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/System.IO.FileSystem.Tests.csproj::TargetFramework=net11.0-unix]
##[error]src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/SafeFileHandle/GetFileType.Unix.cs(51,25): error CS0029: (NETCORE_ENGINEERING_TELEMETRY=Build) Cannot implicitly convert type 'Microsoft.Win32.SafeHandles.SafeFileHandle' to 'nint'
/__w/1/s/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/SafeFileHandle/GetFileType.Unix.cs(51,25): error CS0029: Cannot implicitly convert type 'Microsoft.Win32.SafeHandles.SafeFileHandle' to 'nint' [/__w/1/s/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/System.IO.FileSystem.Tests.csproj::TargetFramework=net11.0-unix]
##[error]src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/SafeFileHandle/GetFileType.Unix.cs(83,29): error CS0029: (NETCORE_ENGINEERING_TELEMETRY=Build) Cannot implicitly convert type 'Microsoft.Win32.SafeHandles.SafeFileHandle' to 'nint'
Make sure you can build the repo locally before pushing the fix
…kFifo Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Contributor
Author
adamsitnik
reviewed
Feb 19, 2026
...raries/System.Runtime/tests/System.IO.FileSystem.Tests/SafeFileHandle/GetFileType.Windows.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds
System.IO.FileTypeenum andSafeFileHandle.GetFileType()method to enable runtime detection of file handle types without additional syscalls.Changes
API Surface
FileType enum in
System.IO:Unknown,RegularFile,Pipe,Socket,CharacterDevice,Directory,SymbolicLinkBlockDevice(Unix only, marked[UnsupportedOSPlatform("windows")])SafeFileHandle.GetFileType() public method:
FileTypefor the handleObjectDisposedExceptionon closed handlesImplementation
Shared (
SafeFileHandle.cs):GetFileType()method validates handle state and callsGetFileTypeCore()_cachedFileTypefield (-1 = uncached) shared across all platformsWindows (
SafeFileHandle.Windows.cs):GetFileTypeCore()using Win32 APIsFILE_TYPE_CHAR→CharacterDeviceFILE_TYPE_PIPE→ CallsGetNamedPipeInfo(usingSafeHandleparameter); failure indicatesSocket, success indicatesPipeFILE_TYPE_DISK→ ChecksFILE_ATTRIBUTE_DIRECTORYviaGetFileInformationByHandle, thenFILE_ATTRIBUTE_REPARSE_POINTviaGetFileInformationByHandleExfor symlinks_cachedFileTypefield (removed duplicate_fileTypefield)CanSeekproperty to use newGetFileType()API, checking forFileType.RegularFilefor more precise semanticsUnix (
SafeFileHandle.Unix.cs):GetFileTypeCore()usingfstatsyscallInit()from existingfstatresultS_IFMTmodes:S_IFIFO→Pipe,S_IFSOCK→Socket, etc.FStatfailureInterop (
Interop.GetNamedPipeInfo.cs):SafeHandleinstead ofSafePipeHandleto avoid CoreLib build issues and enable use with SafeFileHandleSystem.Private.CoreLib.Shared.projitemsfor Windows buildsExample Usage
Testing
Comprehensive test coverage split across three platform-organized files:
GetFileType.cs: OS-agnostic tests (regular files, null device viaFile.OpenNullHandle(), anonymous pipes, sockets, error handling, caching)GetFileType.Windows.cs: Windows-specific tests (directories, named pipes, console handles, symlinks)GetFileType.Unix.cs: Unix-specific tests (directories, named pipes, symlinks, block devices)Test files are properly organized in the project file by platform using conditional ItemGroups with all required Interop dependencies:
Interop.Open.cs,Interop.OpenFlags.cs,Interop.MkFifo.csInterop.FileOperations.cs💡 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.