Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 27 additions & 0 deletions Include/internal/pycore_fileutils_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,33 @@ static inline BOOL _Py_GetFileInformationByName_ErrorIsTrustworthy(int error)
return FALSE;
}

static inline HANDLE _Py_WinCreateFile(
_In_ LPCWSTR lpFileName,
_In_ DWORD dwDesiredAccess,
_In_ DWORD dwShareMode,
_In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes,
_In_ DWORD dwCreationDisposition,
_In_ DWORD dwFlagsAndAttributes,
_In_opt_ HANDLE hTemplateFile
)
{
#ifndef MS_WINDOWS_DESKTOP
if (dwShareMode == 0)
dwShareMode = FILE_SHARE_READ;

CREATEFILE2_EXTENDED_PARAMETERS ext;
ZeroMemory(&ext, sizeof(CREATEFILE2_EXTENDED_PARAMETERS));
ext.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
ext.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
ext.dwFileFlags = dwFlagsAndAttributes & 0xFFFF0000;

return CreateFile2(lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, &ext);
#else
return CreateFileW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
#endif
}

#endif

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use ``CreateFile2`` on Windows UWP build.
5 changes: 3 additions & 2 deletions Modules/_winapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include <winioctl.h>
#include <crtdbg.h>
#include "winreparse.h"
#include "internal/pycore_fileutils_windows.h"

// PSAPI_VERSION=2 redirects GetProcessMemoryInfo() to
// K32GetProcessMemoryInfo() in kernel32.dll, so we don't need to link
Expand Down Expand Up @@ -546,7 +547,7 @@ _winapi_CreateFile_impl(PyObject *module, LPCWSTR file_name,
}

Py_BEGIN_ALLOW_THREADS
handle = CreateFileW(file_name, desired_access,
handle = _Py_WinCreateFile(file_name, desired_access,
share_mode, security_attributes,
creation_disposition,
flags_and_attributes, template_file);
Expand Down Expand Up @@ -721,7 +722,7 @@ _winapi_CreateJunction_impl(PyObject *module, LPCWSTR src_path,
if (!CreateDirectoryW(dst_path, NULL))
goto cleanup;

junction = CreateFileW(dst_path, GENERIC_READ | GENERIC_WRITE, 0, NULL,
junction = _Py_WinCreateFile(dst_path, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING,
FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (junction == INVALID_HANDLE_VALUE)
Expand Down
3 changes: 2 additions & 1 deletion Modules/getpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#ifdef MS_WINDOWS
# include <windows.h> // GetFullPathNameW(), MAX_PATH
# include <pathcch.h>
# include "internal/pycore_fileutils_windows.h"
#endif

#ifdef __APPLE__
Expand Down Expand Up @@ -527,7 +528,7 @@ getpath_realpath(PyObject *Py_UNUSED(self) , PyObject *args)
}

Py_BEGIN_ALLOW_THREADS
hFile = CreateFileW(path, 0, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
hFile = _Py_WinCreateFile(path, 0, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (hFile != INVALID_HANDLE_VALUE) {
len = GetFinalPathNameByHandleW(hFile, resolved, MAXPATHLEN, VOLUME_NAME_DOS);
err = len ? 0 : GetLastError();
Expand Down
8 changes: 6 additions & 2 deletions Modules/overlapped.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
#include "Python.h"
#include "pycore_tuple.h" // _PyTuple_FromPairSteal

#define WINDOWS_LEAN_AND_MEAN
#ifdef MS_WINDOWS
# define WINDOWS_LEAN_AND_MEAN
# include "internal/pycore_fileutils_windows.h"
#endif

#include <winsock2.h>
#include <ws2tcpip.h>
#include <mswsock.h>
Expand Down Expand Up @@ -1635,7 +1639,7 @@ _overlapped_Overlapped_ConnectPipe_impl(OverlappedObject *self,
HANDLE PipeHandle;

Py_BEGIN_ALLOW_THREADS
PipeHandle = CreateFileW(Address,
PipeHandle = _Py_WinCreateFile(Address,
GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, NULL);
Expand Down
22 changes: 11 additions & 11 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2124,7 +2124,7 @@ win32_xstat_slow_impl(const wchar_t *path, struct _Py_stat_struct *result,
flags |= FILE_FLAG_OPEN_REPARSE_POINT;
}

hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL);
hFile = _Py_WinCreateFile(path, access, 0, NULL, OPEN_EXISTING, flags, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
/* Either the path doesn't exist, or the caller lacks access. */
error = GetLastError();
Expand Down Expand Up @@ -2159,7 +2159,7 @@ win32_xstat_slow_impl(const wchar_t *path, struct _Py_stat_struct *result,

case ERROR_INVALID_PARAMETER:
/* \\.\con requires read or write access. */
hFile = CreateFileW(path, access | GENERIC_READ,
hFile = _Py_WinCreateFile(path, access | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, flags, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
Expand All @@ -2173,7 +2173,7 @@ win32_xstat_slow_impl(const wchar_t *path, struct _Py_stat_struct *result,
if (traverse) {
traverse = FALSE;
isUnhandledTag = TRUE;
hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING,
hFile = _Py_WinCreateFile(path, access, 0, NULL, OPEN_EXISTING,
flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
}
if (hFile == INVALID_HANDLE_VALUE) {
Expand Down Expand Up @@ -4107,7 +4107,7 @@ os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
result = win32_fchmod(path->fd, mode);
}
else if (follow_symlinks) {
HANDLE hfile = CreateFileW(path->wide,
HANDLE hfile = _Py_WinCreateFile(path->wide,
FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES,
0, NULL,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
Expand Down Expand Up @@ -5384,7 +5384,7 @@ os__path_isdevdrive_impl(PyObject *module, path_t *path)
/* only care about local dev drives */
r = Py_False;
} else {
HANDLE hVolume = CreateFileW(
HANDLE hVolume = _Py_WinCreateFile(
volume,
FILE_READ_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE,
Expand Down Expand Up @@ -5532,7 +5532,7 @@ os__getfinalpathname_impl(PyObject *module, path_t *path)
PyObject *result;

Py_BEGIN_ALLOW_THREADS
hFile = CreateFileW(
hFile = _Py_WinCreateFile(
path->wide,
0, /* desired access */
0, /* share mode */
Expand Down Expand Up @@ -5807,7 +5807,7 @@ _testFileTypeByName(LPCWSTR path, int testedType)
if (testedType != PY_IFREG && testedType != PY_IFDIR) {
flags |= FILE_FLAG_OPEN_REPARSE_POINT;
}
HANDLE hfile = CreateFileW(path, FILE_READ_ATTRIBUTES, 0, NULL,
HANDLE hfile = _Py_WinCreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL,
OPEN_EXISTING, flags, NULL);
if (hfile != INVALID_HANDLE_VALUE) {
BOOL result = _testFileTypeByHandle(hfile, testedType, FALSE);
Expand Down Expand Up @@ -5863,7 +5863,7 @@ _testFileExistsByName(LPCWSTR path, BOOL followLinks)
if (!followLinks) {
flags |= FILE_FLAG_OPEN_REPARSE_POINT;
}
HANDLE hfile = CreateFileW(path, FILE_READ_ATTRIBUTES, 0, NULL,
HANDLE hfile = _Py_WinCreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL,
OPEN_EXISTING, flags, NULL);
if (hfile != INVALID_HANDLE_VALUE) {
if (followLinks) {
Expand All @@ -5876,7 +5876,7 @@ _testFileExistsByName(LPCWSTR path, BOOL followLinks)
if (!result) {
return TRUE;
}
hfile = CreateFileW(path, FILE_READ_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
hfile = _Py_WinCreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (hfile != INVALID_HANDLE_VALUE) {
CloseHandle(hfile);
Expand Down Expand Up @@ -7172,7 +7172,7 @@ os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,

#ifdef MS_WINDOWS
Py_BEGIN_ALLOW_THREADS
hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
hFile = _Py_WinCreateFile(path->wide, FILE_WRITE_ATTRIBUTES, 0,
NULL, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, NULL);
Py_END_ALLOW_THREADS
Expand Down Expand Up @@ -10954,7 +10954,7 @@ os_readlink_impl(PyObject *module, path_t *path, int dir_fd)

/* First get a handle to the reparse point */
Py_BEGIN_ALLOW_THREADS
reparse_point_handle = CreateFileW(
reparse_point_handle = _Py_WinCreateFile(
path->wide,
0,
0,
Expand Down
Loading