gh-82367: Use FindFirstFile Win32 API in ntpath.realpath() (GH-110298)

* Use `FindFirstFile` Win32 API to fix a bug where `ntpath.realpath()`
breaks out of traversing a series of paths where a (handled)
`ERROR_ACCESS_DENIED` or `ERROR_SHARING_VIOLATION` occurs.
* Update docs to reflect that `ntpath.realpath()` eliminates MS-DOS
style names.
This commit is contained in:
박문식 2023-10-05 23:49:07 +09:00 committed by GitHub
parent 2cb62c6437
commit d33aa18f15
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 132 additions and 8 deletions

View file

@ -4809,6 +4809,37 @@ cleanup:
return result;
}
/*[clinic input]
os._findfirstfile
path: path_t
/
A function to get the real file name without accessing the file in Windows.
[clinic start generated code]*/
static PyObject *
os__findfirstfile_impl(PyObject *module, path_t *path)
/*[clinic end generated code: output=106dd3f0779c83dd input=0734dff70f60e1a8]*/
{
PyObject *result;
HANDLE hFindFile;
WIN32_FIND_DATAW wFileData;
WCHAR *wRealFileName;
Py_BEGIN_ALLOW_THREADS
hFindFile = FindFirstFileW(path->wide, &wFileData);
Py_END_ALLOW_THREADS
if (hFindFile == INVALID_HANDLE_VALUE) {
path_error(path);
return NULL;
}
wRealFileName = wFileData.cFileName;
result = PyUnicode_FromWideChar(wRealFileName, wcslen(wRealFileName));
FindClose(hFindFile);
return result;
}
/*[clinic input]
os._getvolumepathname
@ -15961,6 +15992,7 @@ static PyMethodDef posix_methods[] = {
OS__GETFULLPATHNAME_METHODDEF
OS__GETDISKUSAGE_METHODDEF
OS__GETFINALPATHNAME_METHODDEF
OS__FINDFIRSTFILE_METHODDEF
OS__GETVOLUMEPATHNAME_METHODDEF
OS__PATH_SPLITROOT_METHODDEF
OS__PATH_NORMPATH_METHODDEF