Skip to content

Commit

Permalink
Replace GetFileType with ReOpenFile
Browse files Browse the repository at this point in the history
Signed-off-by: Jesse Schwartzentruber <truber@mozilla.com>
  • Loading branch information
jschwartzentruber committed Jan 11, 2023
1 parent 3eba2d3 commit d37d72f
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions psutil/arch/windows/process_handles.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ psutil_get_open_files(DWORD dwPid, HANDLE hProcess) {
PSYSTEM_HANDLE_INFORMATION_EX handlesList = NULL;
PSYSTEM_HANDLE_TABLE_ENTRY_INFO_EX hHandle = NULL;
HANDLE hFile = NULL;
HANDLE hSafeFile = NULL;
ULONG i = 0;
BOOLEAN errorOccurred = FALSE;
PyObject* py_path = NULL;
Expand Down Expand Up @@ -222,14 +223,17 @@ psutil_get_open_files(DWORD dwPid, HANDLE hProcess) {
continue;
}

if (GetFileType(hFile) != FILE_TYPE_DISK) {
SetLastError(0);
CloseHandle(hFile);
hFile = NULL;
hSafeFile = ReOpenFile(
hFile,
0,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
0);
CloseHandle(hFile);

if (hSafeFile == INVALID_HANDLE_VALUE)
continue;
}

fileName = psutil_threaded_get_filename(hFile);
fileName = psutil_threaded_get_filename(hSafeFile);
if (fileName == NULL)
goto error;

Expand All @@ -248,8 +252,8 @@ psutil_get_open_files(DWORD dwPid, HANDLE hProcess) {
FREE(fileName);
fileName = NULL;
}
CloseHandle(hFile);
hFile = NULL;
CloseHandle(hSafeFile);
hSafeFile = NULL;
}

goto exit;
Expand All @@ -260,8 +264,8 @@ psutil_get_open_files(DWORD dwPid, HANDLE hProcess) {
goto exit;

exit:
if (hFile != NULL)
CloseHandle(hFile);
if (hSafeFile != NULL)
CloseHandle(hSafeFile);
if (fileName != NULL)
FREE(fileName);
if (py_path != NULL)
Expand Down

0 comments on commit d37d72f

Please sign in to comment.