-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Make FileStream.Name work for FileStream instances created out of handle #126629
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
8
commits into
main
Choose a base branch
from
copilot/extend-safefilehandle-name
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.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
da0bff0
Initial plan
Copilot c922ff2
Implement SafeFileHandle.Name property with OS-level path resolution
Copilot b600d32
Address PR review feedback: make API internal, fix UNC handling, use …
Copilot 635eed0
Fix null terminator search to be bounded by PathMaxSize
Copilot 9a7fd3e
Address second round of PR review feedback
Copilot a33d3f9
Fix build failure: remove leaveOpen param, save rawHandle before File…
Copilot f0d33c3
fix OSX build
adamsitnik b97f003
Address reviewer feedback: centralize caching in Path, add MAXPATHLEN…
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
45 changes: 45 additions & 0 deletions
45
src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetFilePathFromHandle.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,45 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Buffers; | ||
| using System.Runtime.InteropServices; | ||
| using System.Text; | ||
| using Microsoft.Win32.SafeHandles; | ||
|
|
||
| internal static partial class Interop | ||
| { | ||
| internal static partial class Sys | ||
| { | ||
| [LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetFilePathFromHandle", SetLastError = true)] | ||
| internal static unsafe partial int GetFilePathFromHandle(SafeFileHandle fd, byte* buffer, int bufferSize); | ||
|
|
||
| internal static unsafe string GetFilePathFromHandle(SafeFileHandle fd) | ||
| { | ||
| // PATH_MAX on Linux is 4096; macOS/BSD MAXPATHLEN is 1024. | ||
| // Using 4096 covers all Unix platforms without requiring buffer growing. | ||
| const int PathMaxSize = 4096; | ||
| byte[] buffer = ArrayPool<byte>.Shared.Rent(PathMaxSize); | ||
| try | ||
| { | ||
| int result; | ||
| fixed (byte* bufPtr = buffer) | ||
| { | ||
| result = GetFilePathFromHandle(fd, bufPtr, PathMaxSize); | ||
| } | ||
|
|
||
| if (result != 0) | ||
| { | ||
| return SR.IO_UnknownFileName; | ||
| } | ||
|
|
||
| int length = buffer.AsSpan(0, PathMaxSize).IndexOf((byte)0); | ||
| return Encoding.UTF8.GetString(buffer, 0, length < 0 ? PathMaxSize : length); | ||
| } | ||
| finally | ||
| { | ||
| ArrayPool<byte>.Shared.Return(buffer); | ||
| } | ||
| } | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |||||||||
| // The .NET Foundation licenses this file to you under the MIT license. | ||||||||||
|
|
||||||||||
| using System; | ||||||||||
| using System.Buffers; | ||||||||||
| using System.ComponentModel; | ||||||||||
| using System.Diagnostics; | ||||||||||
| using System.IO; | ||||||||||
|
|
@@ -461,5 +462,67 @@ unsafe long GetFileLengthCore() | |||||||||
| return storageReadCapacity.DiskLength; | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| internal unsafe string? GetPath() | ||||||||||
| { | ||||||||||
| const int InitialBufferSize = | ||||||||||
| #if DEBUG | ||||||||||
| 26; // use a small size in debug builds to ensure the buffer-growing path is exercised | ||||||||||
| #else | ||||||||||
| 4096; | ||||||||||
| #endif | ||||||||||
| char[] buffer = ArrayPool<char>.Shared.Rent(InitialBufferSize); | ||||||||||
| try | ||||||||||
| { | ||||||||||
| uint result = GetFinalPathNameByHandleHelper(buffer); | ||||||||||
|
|
||||||||||
| // If the function fails because lpszFilePath is too small to hold the string plus the terminating null | ||||||||||
| // character, the return value is the required buffer size, in TCHARs, including the null character. | ||||||||||
| if (result > buffer.Length) | ||||||||||
| { | ||||||||||
| char[] toReturn = buffer; | ||||||||||
| buffer = ArrayPool<char>.Shared.Rent((int)result); | ||||||||||
| ArrayPool<char>.Shared.Return(toReturn); | ||||||||||
|
|
||||||||||
| result = GetFinalPathNameByHandleHelper(buffer); | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it guaranteed that the path cannot change between the two calls? This pattern is often wrapped in a loop (see e.g. runtime/src/libraries/System.Private.CoreLib/src/System/Environment.Variables.Windows.cs Lines 18 to 21 in 2eb5f4c
|
||||||||||
| } | ||||||||||
|
|
||||||||||
| // If the function fails for any other reason, the return value is zero. | ||||||||||
| if (result == 0) | ||||||||||
| { | ||||||||||
| return null; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // GetFinalPathNameByHandle always returns with an extended DOS prefix. | ||||||||||
| // Trim the prefix to keep the result consistent with the path stored in _path. | ||||||||||
| // \\?\UNC\server\share -> \\server\share | ||||||||||
| // \\?\C:\foo -> C:\foo | ||||||||||
| ReadOnlySpan<char> resultSpan = buffer.AsSpan(0, (int)result); | ||||||||||
| if (PathInternal.IsDeviceUNC(resultSpan)) | ||||||||||
| { | ||||||||||
| // \\?\UNC\ (8 chars) -> \\ (2 chars) | ||||||||||
| return string.Concat(PathInternal.UncPathPrefix, resultSpan.Slice(PathInternal.UncExtendedPrefixLength)); | ||||||||||
| } | ||||||||||
| else if (PathInternal.IsExtended(resultSpan)) | ||||||||||
| { | ||||||||||
| // \\?\ (4 chars) -> (empty) | ||||||||||
| return new string(buffer, PathInternal.DevicePrefixLength, (int)result - PathInternal.DevicePrefixLength); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| return new string(buffer, 0, (int)result); | ||||||||||
| } | ||||||||||
| finally | ||||||||||
| { | ||||||||||
| ArrayPool<char>.Shared.Return(buffer); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| uint GetFinalPathNameByHandleHelper(char[] buf) | ||||||||||
| { | ||||||||||
| fixed (char* bufPtr = buf) | ||||||||||
| { | ||||||||||
| return Interop.Kernel32.GetFinalPathNameByHandle(this, bufPtr, (uint)buf.Length, Interop.Kernel32.FILE_NAME_NORMALIZED); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
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
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
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.