-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add FileType enum and SafeFileHandle.GetFileType() method #124561
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
10
commits into
main
Choose a base branch
from
copilot/add-filetype-enum-and-getfiletype-method
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+457
−13
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9ceefd6
Initial plan
Copilot 6f4ff5a
Add FileType enum and GetFileType method to SafeFileHandle
Copilot 71e5a81
Add comprehensive tests for SafeFileHandle.GetFileType
Copilot 603de64
Fix build errors for FileType enum and GetFileType method
Copilot 9edcc96
Address PR feedback: refactor GetFileType, split tests, use SafeHandle
Copilot 1f0655f
Address feedback: move test files, delete GetKernelFileType, use new …
Copilot 8994464
Add GetNamedPipeInfo reference and fix Unix test attribute
Copilot 9834798
Add Unix and Windows Interop references to fix test build errors
Copilot ef2da05
Fix Unix test build errors: use Open correctly, add Task using, add M…
Copilot 28ec926
Remove invalid ConditionalFact from Windows NamedPipe test
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
52 changes: 52 additions & 0 deletions
52
src/libraries/System.Private.CoreLib/src/System/IO/FileType.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace System.IO | ||
| { | ||
| /// <summary> | ||
| /// Specifies the type of a file. | ||
| /// </summary> | ||
| public enum FileType | ||
| { | ||
| /// <summary> | ||
| /// The file type is unknown. | ||
| /// </summary> | ||
| Unknown, | ||
|
|
||
| /// <summary> | ||
| /// The file is a regular file. | ||
| /// </summary> | ||
| RegularFile, | ||
|
|
||
| /// <summary> | ||
| /// The file is a pipe (FIFO). | ||
| /// </summary> | ||
| Pipe, | ||
|
|
||
| /// <summary> | ||
| /// The file is a socket. | ||
| /// </summary> | ||
| Socket, | ||
|
|
||
| /// <summary> | ||
| /// The file is a character device. | ||
| /// </summary> | ||
| CharacterDevice, | ||
|
|
||
| /// <summary> | ||
| /// The file is a directory. | ||
| /// </summary> | ||
| Directory, | ||
|
|
||
| /// <summary> | ||
| /// The file is a symbolic link. | ||
| /// </summary> | ||
| SymbolicLink, | ||
|
|
||
| /// <summary> | ||
| /// The file is a block device. | ||
| /// </summary> | ||
| [System.Runtime.Versioning.UnsupportedOSPlatform("windows")] | ||
| BlockDevice | ||
| } | ||
| } |
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
96 changes: 96 additions & 0 deletions
96
...raries/System.Runtime/tests/System.IO.FileSystem.Tests/SafeFileHandle/GetFileType.Unix.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.DotNet.XUnitExtensions; | ||
| using Microsoft.Win32.SafeHandles; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| namespace System.IO.Tests | ||
| { | ||
| [PlatformSpecific(TestPlatforms.AnyUnix)] | ||
| public class SafeFileHandle_GetFileType_Unix : FileSystemTest | ||
| { | ||
| [Fact] | ||
| public void GetFileType_Directory() | ||
| { | ||
| string path = GetTestFilePath(); | ||
| Directory.CreateDirectory(path); | ||
|
|
||
| using SafeFileHandle handle = Interop.Sys.Open(path, Interop.Sys.OpenFlags.O_RDONLY, 0); | ||
| Assert.False(handle.IsInvalid); | ||
| Assert.Equal(FileType.Directory, handle.GetFileType()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetFileType_NamedPipe() | ||
| { | ||
| string pipePath = GetTestFilePath(); | ||
| Assert.Equal(0, Interop.Sys.MkFifo(pipePath, (int)UnixFileMode.UserRead | (int)UnixFileMode.UserWrite)); | ||
|
|
||
| Task readerTask = Task.Run(() => | ||
| { | ||
| using SafeFileHandle reader = File.OpenHandle(pipePath, FileMode.Open, FileAccess.Read); | ||
| Assert.Equal(FileType.Pipe, reader.GetFileType()); | ||
| }); | ||
|
|
||
| using SafeFileHandle writer = File.OpenHandle(pipePath, FileMode.Open, FileAccess.Write); | ||
| Assert.Equal(FileType.Pipe, writer.GetFileType()); | ||
|
|
||
| readerTask.Wait(); | ||
| } | ||
|
|
||
| [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))] | ||
| public void GetFileType_SymbolicLink() | ||
| { | ||
| string targetPath = GetTestFilePath(); | ||
| string linkPath = GetTestFilePath(); | ||
| File.WriteAllText(targetPath, "test"); | ||
| File.CreateSymbolicLink(linkPath, targetPath); | ||
|
|
||
| using SafeFileHandle handle = Interop.Sys.Open(linkPath, Interop.Sys.OpenFlags.O_RDONLY | Interop.Sys.OpenFlags.O_NOFOLLOW, 0); | ||
|
|
||
| if (!handle.IsInvalid) | ||
| { | ||
| Assert.Equal(FileType.SymbolicLink, handle.GetFileType()); | ||
| } | ||
| } | ||
|
|
||
| [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsPrivilegedProcess))] | ||
| [PlatformSpecific(TestPlatforms.AnyUnix & ~TestPlatforms.Browser & ~TestPlatforms.Wasi)] | ||
| public void GetFileType_BlockDevice() | ||
| { | ||
| string[] possibleBlockDevices = { "/dev/sda", "/dev/loop0", "/dev/vda", "/dev/nvme0n1" }; | ||
|
|
||
| string blockDevice = null; | ||
| foreach (string device in possibleBlockDevices) | ||
| { | ||
| if (File.Exists(device)) | ||
| { | ||
| blockDevice = device; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (blockDevice == null) | ||
| { | ||
| throw new SkipTestException("No accessible block device found for testing"); | ||
| } | ||
|
|
||
| try | ||
| { | ||
| using SafeFileHandle handle = Interop.Sys.Open(blockDevice, Interop.Sys.OpenFlags.O_RDONLY, 0); | ||
| if (handle.IsInvalid) | ||
| { | ||
| throw new SkipTestException($"Could not open {blockDevice}"); | ||
| } | ||
|
|
||
| Assert.Equal(FileType.BlockDevice, handle.GetFileType()); | ||
| } | ||
| catch (UnauthorizedAccessException) | ||
| { | ||
| throw new SkipTestException("Insufficient privileges to open block device"); | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.