From a84df061609ed937c0bbc45491fd352d62caa0fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D0=BE=D0=BC=D0=B0=D0=BD=20=D0=94=D0=BE=D0=BD=D1=87?= =?UTF-8?q?=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Thu, 21 Mar 2019 23:45:59 +0300 Subject: [PATCH] platform_utils_win32: remove an unnecessary workaround The comment in _create_symlink is incorrect. The return value of CreateSymbolicLink is as documented, it was just declared with the wrong return type. The actual return type is BOOLEAN, not BOOL. Fixing this allows us to simplify the code a bit. Change-Id: I4d2190a50d45ba41dd9814bf7079a5784fc0a366 --- platform_utils_win32.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/platform_utils_win32.py b/platform_utils_win32.py index a6431216..7ab2bf04 100644 --- a/platform_utils_win32.py +++ b/platform_utils_win32.py @@ -17,7 +17,7 @@ import errno from ctypes import WinDLL, get_last_error, FormatError, WinError, addressof from ctypes import c_buffer -from ctypes.wintypes import BOOL, LPCWSTR, DWORD, HANDLE, POINTER, c_ubyte +from ctypes.wintypes import BOOL, BOOLEAN, LPCWSTR, DWORD, HANDLE, POINTER, c_ubyte from ctypes.wintypes import WCHAR, USHORT, LPVOID, Structure, Union, ULONG from ctypes.wintypes import byref @@ -33,7 +33,7 @@ ERROR_PRIVILEGE_NOT_HELD = 1314 # Win32 API entry points CreateSymbolicLinkW = kernel32.CreateSymbolicLinkW -CreateSymbolicLinkW.restype = BOOL +CreateSymbolicLinkW.restype = BOOLEAN CreateSymbolicLinkW.argtypes = (LPCWSTR, # lpSymlinkFileName In LPCWSTR, # lpTargetFileName In DWORD) # dwFlags In @@ -145,19 +145,12 @@ def create_dirsymlink(source, link_name): def _create_symlink(source, link_name, dwFlags): - # Note: Win32 documentation for CreateSymbolicLink is incorrect. - # On success, the function returns "1". - # On error, the function returns some random value (e.g. 1280). - # The best bet seems to use "GetLastError" and check for error/success. - CreateSymbolicLinkW(link_name, source, dwFlags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE) - code = get_last_error() - if code != ERROR_SUCCESS: + if not CreateSymbolicLinkW(link_name, source, dwFlags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE): # See https://github.com/golang/go/pull/24307/files#diff-b87bc12e4da2497308f9ef746086e4f0 # "the unprivileged create flag is unsupported below Windows 10 (1703, v10.0.14972). # retry without it." - CreateSymbolicLinkW(link_name, source, dwFlags) - code = get_last_error() - if code != ERROR_SUCCESS: + if not CreateSymbolicLinkW(link_name, source, dwFlags): + code = get_last_error() error_desc = FormatError(code).strip() if code == ERROR_PRIVILEGE_NOT_HELD: raise OSError(errno.EPERM, error_desc, link_name)