8000 gh-117841: Add C implementation of `ntpath.lexists` by nineteendo · Pull Request #117842 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-117841: Add C implementation of ntpath.lexists #117842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve nt_exists()
Co-authored-by: Eryk Sun <eryksun@gmail.com>
  • Loading branch information
nineteendo and eryksun committed Apr 15, 2024
commit 29633380cb0d8f845c694867967a38e7f90feb46
58 changes: 49 additions & 9 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -5285,6 +5285,7 @@ nt_exists(PyObject *path, int follow_symlinks)
{
path_t _path = PATH_T_INITIALIZE("exists", "path", 0, 1);
HANDLE hfile;
BOOL traverse = follow_symlinks;
int result = 0;

if (!path_converter(path, &_path)) {
Expand All @@ -5307,7 +5308,7 @@ nt_exists(PyObject *path, int follow_symlinks)
BOOL slow_path = TRUE;
FILE_STAT_BASIC_INFORMATION statInfo;
if (_Py_GetFileInformationByName(_path.wide, FileStatBasicByNameInfo,
&statInfo, sizeof(statInfo)))
&statInfo, sizeof(statInfo)))
{
if (!(statInfo.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) ||
!follow_symlinks &&
Expand All @@ -5316,27 +5317,66 @@ nt_exists(PyObject *path, int follow_symlinks)
slow_path = FALSE;
result = 1;
}
} else if (_Py_GetFileInformationByName_ErrorIsTrustworthy(
else {
// reparse point but not name-surrogate
traverse = TRUE;
}
}
else if (_Py_GetFileInformationByName_ErrorIsTrustworthy(
GetLastError()))
{
slow_path = FALSE;
}
if (slow_path) {
STRUCT_STAT st;
if (!follow_symlinks) {
if (!LSTAT(_path.wide, &st)) {
result = 1;
BOOL traverse = follow_symlinks;
if (!traverse) {
hfile = CreateFileW(_path.wide, FILE_READ_ATTRIBUTES, 0, NULL,
OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT |
FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (hfile != INVALID_HANDLE_VALUE) {
FILE_ATTRIBUTE_TAG_INFO info;
if (GetFileInformationByHandleEx(hfile,
FileAttributeTagInfo, &info, sizeof(info)))
{
if (!(info.FileAttributes &
FILE_ATTRIBUTE_REPARSE_POINT) ||
IsReparseTagNameSurrogate(info.ReparseTag))
{
result = 1;
}
else {
// reparse point but not name-surrogate
traverse = TRUE;
}
}
else {
// device or legacy filesystem
result = 1;
}
CloseHandle(hfile);
}
else {
STRUCT_STAT st;
switch (GetLastError()) {
case ERROR_ACCESS_DENIED:
case ERROR_SHARING_VIOLATION:
case ERROR_CANT_ACCESS_FILE:
case ERROR_INVALID_PARAMETER:
if (!LSTAT(_path.wide, &st)) {
result = 1;
}
}
}
}
else {
if (traverse) {
hfile = CreateFileW(_path.wide, FILE_READ_ATTRIBUTES, 0, NULL,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS,
NULL);
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (hfile != INVALID_HANDLE_VALUE) {
CloseHandle(hfile);
result = 1;
}
else {
STRUCT_STAT st;
switch (GetLastError()) {
case ERROR_ACCESS_DENIED:
case ERROR_SHARING_VIOLATION:
Expand Down
0