-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Refactor GetFileShares
#26155
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
Refactor GetFileShares
#26155
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,24 +3,50 @@ | |||||
|
|
||||||
| #nullable enable | ||||||
|
|
||||||
| using System; | ||||||
| using System.Runtime.InteropServices; | ||||||
|
|
||||||
| internal static partial class Interop | ||||||
| { | ||||||
| internal static unsafe partial class Windows | ||||||
| { | ||||||
| internal const int MAX_PREFERRED_LENGTH = -1; | ||||||
| internal const int STYPE_DISKTREE = 0; | ||||||
| internal const int STYPE_MASK = 0x000000FF; | ||||||
| internal const uint MAX_PREFERRED_LENGTH = uint.MaxValue; | ||||||
| internal const uint STYPE_DISKTREE = 0u; | ||||||
| internal const uint STYPE_MASK = 255u; | ||||||
|
|
||||||
| [LibraryImport("Netapi32.dll", StringMarshalling = StringMarshalling.Utf16)] | ||||||
| internal static partial int NetShareEnum( | ||||||
| string serverName, | ||||||
| int level, | ||||||
| out nint bufptr, | ||||||
| int prefMaxLen, | ||||||
| out uint entriesRead, | ||||||
| out uint totalEntries, | ||||||
| ref uint resumeHandle); | ||||||
| private static unsafe partial uint NetShareEnum( | ||||||
| string? servername, | ||||||
| uint level, | ||||||
| out byte* bufptr, | ||||||
| uint prefmaxlen, | ||||||
| out uint entriesread, | ||||||
| out uint totalentries, | ||||||
| uint* resume_handle); | ||||||
|
|
||||||
| [StructLayout(LayoutKind.Sequential)] | ||||||
| internal unsafe struct SHARE_INFO_1 | ||||||
| { | ||||||
| public ushort* netname; | ||||||
| public uint type; | ||||||
| public ushort* remark; | ||||||
| } | ||||||
|
|
||||||
| internal static uint NetShareEnum<T>(string? servername, out T* pShareInfo, out int count) where T : unmanaged | ||||||
|
Collaborator
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. Since it is high level helper it could return just List so that we have no interop/unsafe code in call site.
Contributor
Author
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. We need to free the unmanaged buffer, so we cannot return just a managed type.
Contributor
Author
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. I considered implementing a class deriving from
Collaborator
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. If we build List in the method we can free the buffer in the method too.
Contributor
Author
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.
This is exactly what the
Contributor
Author
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. @iSazonov I think the current changes are a reasonable balance between safety and avoiding unnecessary abstraction. In terms of safety, we now avoid pointer arithmetic and use a span implementation. |
||||||
| { | ||||||
| uint level = (uint)GetLevelFromStructure<T>(); | ||||||
| uint result = NetShareEnum(servername, level, out byte* pBuffer, MAX_PREFERRED_LENGTH, out uint entriesRead, out _, null); | ||||||
| pShareInfo = (T*)pBuffer; | ||||||
| count = (int)entriesRead; | ||||||
| return result; | ||||||
| } | ||||||
|
|
||||||
| private static int GetLevelFromStructure<T>() | ||||||
| { | ||||||
| if (typeof(T) == typeof(SHARE_INFO_1)) | ||||||
| return 1; | ||||||
|
|
||||||
| throw new NotSupportedException(); | ||||||
|
||||||
| throw new NotSupportedException(); | |
| throw new NotSupportedException($"Unsupported structure type: {typeof(T).Name}"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential null reference exception:
Utf16StringMarshaller.ConvertToManagedmay return null ifshareInfo.netnameis null, but the result is used without null checking on line 5221. Add null handling, e.g.,string? share = Utf16StringMarshaller.ConvertToManaged(shareInfo.netname); if (share == null) continue;