@@ -7859,8 +7859,23 @@ internal static extern IntPtr CreateFile(
7859
7859
FileAttributes dwFlagsAndAttributes ,
7860
7860
IntPtr hTemplateFile ) ;
7861
7861
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 ) ;
7864
7879
7865
7880
internal enum FINDEX_INFO_LEVELS : uint
7866
7881
{
@@ -8055,7 +8070,7 @@ internal static bool IsReparsePointWithTarget(FileSystemInfo fileInfo)
8055
8070
}
8056
8071
#if ! UNIX
8057
8072
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 ) )
8059
8074
{
8060
8075
// Name surrogates (0x20000000) are reparse points that point to other named entities local to the filesystem (like symlinks)
8061
8076
// In the case of OneDrive, they are not surrogates and would be safe to recurse into.
0 commit comments