Skip to content

Commit b0a3755

Browse files
SteveL-MSFTAndrew
authored andcommitted
Minor fix for recursion into OneDrive - Change FindFirstFileEx() to use SafeFindHandle type (PowerShell#10405)
1 parent 5594674 commit b0a3755

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/System.Management.Automation/namespaces/FileSystemProvider.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7859,8 +7859,23 @@ internal static extern IntPtr CreateFile(
78597859
FileAttributes dwFlagsAndAttributes,
78607860
IntPtr hTemplateFile);
78617861

7862-
[DllImport(PinvokeDllNames.FindFirstFileDllName, EntryPoint = "FindFirstFileExW", SetLastError = true, CharSet = CharSet.Unicode)]
7863-
private static extern SafeFileHandle FindFirstFileEx(string lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, ref WIN32_FIND_DATA lpFindFileData, FINDEX_SEARCH_OPS fSearchOp, IntPtr lpSearchFilter, int dwAdditionalFlags);
7862+
internal sealed class SafeFindHandle : SafeHandleZeroOrMinusOneIsInvalid
7863+
{
7864+
private SafeFindHandle() : base(true) { }
7865+
7866+
protected override bool ReleaseHandle()
7867+
{
7868+
return FindClose(this.handle);
7869+
}
7870+
7871+
[DllImport(PinvokeDllNames.FindCloseDllName)]
7872+
[return: MarshalAs(UnmanagedType.Bool)]
7873+
private static extern bool FindClose(IntPtr handle);
7874+
}
7875+
7876+
// SetLastError is false as the use of this API doesn't not require GetLastError() to be called
7877+
[DllImport(PinvokeDllNames.FindFirstFileDllName, EntryPoint = "FindFirstFileExW", SetLastError = false, CharSet = CharSet.Unicode)]
7878+
private static extern SafeFindHandle FindFirstFileEx(string lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, ref WIN32_FIND_DATA lpFindFileData, FINDEX_SEARCH_OPS fSearchOp, IntPtr lpSearchFilter, int dwAdditionalFlags);
78647879

78657880
internal enum FINDEX_INFO_LEVELS : uint
78667881
{
@@ -8055,7 +8070,7 @@ internal static bool IsReparsePointWithTarget(FileSystemInfo fileInfo)
80558070
}
80568071
#if !UNIX
80578072
var data = new WIN32_FIND_DATA();
8058-
using (SafeFileHandle handle = FindFirstFileEx(fileInfo.FullName, FINDEX_INFO_LEVELS.FindExInfoBasic, ref data, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero, 0))
8073+
using (var handle = FindFirstFileEx(fileInfo.FullName, FINDEX_INFO_LEVELS.FindExInfoBasic, ref data, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero, 0))
80598074
{
80608075
// Name surrogates (0x20000000) are reparse points that point to other named entities local to the filesystem (like symlinks)
80618076
// In the case of OneDrive, they are not surrogates and would be safe to recurse into.

0 commit comments

Comments
 (0)