Commit 33854afb authored by Alex Pasadyn's avatar Alex Pasadyn Committed by Alexandre Julliard

- Use HeapAlloc instead of GlobalAlloc in FindNextFile.

- Add test for FindNextFile that checks last error value.
parent d4f13811
...@@ -918,6 +918,23 @@ void test_FindFirstFileA() ...@@ -918,6 +918,23 @@ void test_FindFirstFileA()
ok ( FindClose(handle) == TRUE, "Failed to close handle"); ok ( FindClose(handle) == TRUE, "Failed to close handle");
} }
void test_FindNextFileA()
{
HANDLE handle;
WIN32_FIND_DATAA search_results;
int err;
handle = FindFirstFileA("C:\\*",&search_results);
ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed" );
while (FindNextFile(handle, &search_results))
{
/* get to the end of the files */
}
ok ( FindClose(handle) == TRUE, "Failed to close handle");
err = GetLastError();
ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES");
}
START_TEST(file) START_TEST(file)
{ {
test__hread( ); test__hread( );
...@@ -935,6 +952,7 @@ START_TEST(file) ...@@ -935,6 +952,7 @@ START_TEST(file)
test_DeleteFileA(); test_DeleteFileA();
test_DeleteFileW(); test_DeleteFileW();
test_FindFirstFileA(); test_FindFirstFileA();
test_FindNextFileA();
test_LockFile(); test_LockFile();
test_offset_in_overlapped_structure(); test_offset_in_overlapped_structure();
} }
...@@ -147,6 +147,7 @@ typedef struct ...@@ -147,6 +147,7 @@ typedef struct
BYTE attr; BYTE attr;
int drive; int drive;
int cur_pos; int cur_pos;
CRITICAL_SECTION cs;
union union
{ {
DOS_DIR *dos_dir; DOS_DIR *dos_dir;
...@@ -1995,7 +1996,6 @@ HANDLE WINAPI FindFirstFileExW( ...@@ -1995,7 +1996,6 @@ HANDLE WINAPI FindFirstFileExW(
LPVOID lpSearchFilter, LPVOID lpSearchFilter,
DWORD dwAdditionalFlags) DWORD dwAdditionalFlags)
{ {
HGLOBAL handle;
FIND_FIRST_INFO *info; FIND_FIRST_INFO *info;
if (!lpFileName) if (!lpFileName)
...@@ -2023,20 +2023,15 @@ HANDLE WINAPI FindFirstFileExW( ...@@ -2023,20 +2023,15 @@ HANDLE WINAPI FindFirstFileExW(
if (lpFileName[0] == '\\' && lpFileName[1] == '\\') if (lpFileName[0] == '\\' && lpFileName[1] == '\\')
{ {
ERR("UNC path name\n"); ERR("UNC path name\n");
if (!(handle = GlobalAlloc(GMEM_MOVEABLE, sizeof(FIND_FIRST_INFO)))) break; if (!(info = HeapAlloc( GetProcessHeap(), 0, sizeof(FIND_FIRST_INFO)))) break;
info = (FIND_FIRST_INFO *)GlobalLock( handle );
info->u.smb_dir = SMB_FindFirst(lpFileName); info->u.smb_dir = SMB_FindFirst(lpFileName);
if(!info->u.smb_dir) if(!info->u.smb_dir)
{ {
GlobalUnlock( handle ); HeapFree(GetProcessHeap(), 0, info);
GlobalFree(handle);
break; break;
} }
info->drive = -1; info->drive = -1;
RtlInitializeCriticalSection( &info->cs );
GlobalUnlock( handle );
} }
else else
{ {
...@@ -2053,8 +2048,8 @@ HANDLE WINAPI FindFirstFileExW( ...@@ -2053,8 +2048,8 @@ HANDLE WINAPI FindFirstFileExW(
} }
} }
if (!DOSFS_GetFullName( lpFileName, FALSE, &full_name )) break; if (!DOSFS_GetFullName( lpFileName, FALSE, &full_name )) break;
if (!(handle = GlobalAlloc(GMEM_MOVEABLE, sizeof(FIND_FIRST_INFO)))) break; if (!(info = HeapAlloc( GetProcessHeap(), 0, sizeof(FIND_FIRST_INFO)))) break;
info = (FIND_FIRST_INFO *)GlobalLock( handle ); RtlInitializeCriticalSection( &info->cs );
info->path = HeapAlloc( GetProcessHeap(), 0, strlen(full_name.long_name)+1 ); info->path = HeapAlloc( GetProcessHeap(), 0, strlen(full_name.long_name)+1 );
strcpy( info->path, full_name.long_name ); strcpy( info->path, full_name.long_name );
...@@ -2071,15 +2066,14 @@ HANDLE WINAPI FindFirstFileExW( ...@@ -2071,15 +2066,14 @@ HANDLE WINAPI FindFirstFileExW(
info->cur_pos = 0; info->cur_pos = 0;
info->u.dos_dir = DOSFS_OpenDir( codepage, info->path ); info->u.dos_dir = DOSFS_OpenDir( codepage, info->path );
GlobalUnlock( handle );
} }
if (!FindNextFileW( handle, data )) if (!FindNextFileW( (HANDLE) info, data ))
{ {
FindClose( handle ); FindClose( (HANDLE) info );
SetLastError( ERROR_FILE_NOT_FOUND ); SetLastError( ERROR_FILE_NOT_FOUND );
break; break;
} }
return handle; return (HANDLE) info;
} }
break; break;
default: default:
...@@ -2163,12 +2157,13 @@ BOOL WINAPI FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data ) ...@@ -2163,12 +2157,13 @@ BOOL WINAPI FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data )
BOOL ret = FALSE; BOOL ret = FALSE;
DWORD gle = ERROR_NO_MORE_FILES; DWORD gle = ERROR_NO_MORE_FILES;
if ((handle == INVALID_HANDLE_VALUE) || if (handle == INVALID_HANDLE_VALUE)
!(info = (FIND_FIRST_INFO *)GlobalLock( handle )))
{ {
SetLastError( ERROR_INVALID_HANDLE ); SetLastError( ERROR_INVALID_HANDLE );
return ret; return ret;
} }
info = (FIND_FIRST_INFO*) handle;
RtlEnterCriticalSection( &info->cs );
if (info->drive == -1) if (info->drive == -1)
{ {
ret = SMB_FindNext( info->u.smb_dir, data ); ret = SMB_FindNext( info->u.smb_dir, data );
...@@ -2194,7 +2189,7 @@ BOOL WINAPI FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data ) ...@@ -2194,7 +2189,7 @@ BOOL WINAPI FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data )
} }
ret = TRUE; ret = TRUE;
done: done:
GlobalUnlock( handle ); RtlLeaveCriticalSection( &info->cs );
if( !ret ) SetLastError( gle ); if( !ret ) SetLastError( gle );
return ret; return ret;
} }
...@@ -2226,13 +2221,14 @@ BOOL WINAPI FindNextFileA( HANDLE handle, WIN32_FIND_DATAA *data ) ...@@ -2226,13 +2221,14 @@ BOOL WINAPI FindNextFileA( HANDLE handle, WIN32_FIND_DATAA *data )
*/ */
BOOL WINAPI FindClose( HANDLE handle ) BOOL WINAPI FindClose( HANDLE handle )
{ {
FIND_FIRST_INFO *info; FIND_FIRST_INFO *info = (FIND_FIRST_INFO*) handle;
if (handle == INVALID_HANDLE_VALUE) goto error; if (handle == INVALID_HANDLE_VALUE) goto error;
__TRY __TRY
{ {
if ((info = (FIND_FIRST_INFO *)GlobalLock( handle ))) RtlEnterCriticalSection( &info->cs );
if (info)
{ {
if (info->u.dos_dir) DOSFS_CloseDir( info->u.dos_dir ); if (info->u.dos_dir) DOSFS_CloseDir( info->u.dos_dir );
if (info->path) HeapFree( GetProcessHeap(), 0, info->path ); if (info->path) HeapFree( GetProcessHeap(), 0, info->path );
...@@ -2247,8 +2243,9 @@ BOOL WINAPI FindClose( HANDLE handle ) ...@@ -2247,8 +2243,9 @@ BOOL WINAPI FindClose( HANDLE handle )
} }
__ENDTRY __ENDTRY
if (!info) goto error; if (!info) goto error;
GlobalUnlock( handle ); RtlLeaveCriticalSection( &info->cs );
GlobalFree( handle ); RtlDeleteCriticalSection( &info->cs );
HeapFree(GetProcessHeap(), 0, info);
return TRUE; return TRUE;
error: error:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment