diff options
Diffstat (limited to 'platform_utils_win32.py')
| -rw-r--r-- | platform_utils_win32.py | 17 |
1 files 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 | |||
| 17 | 17 | ||
| 18 | from ctypes import WinDLL, get_last_error, FormatError, WinError, addressof | 18 | from ctypes import WinDLL, get_last_error, FormatError, WinError, addressof |
| 19 | from ctypes import c_buffer | 19 | from ctypes import c_buffer |
| 20 | from ctypes.wintypes import BOOL, LPCWSTR, DWORD, HANDLE, POINTER, c_ubyte | 20 | from ctypes.wintypes import BOOL, BOOLEAN, LPCWSTR, DWORD, HANDLE, POINTER, c_ubyte |
| 21 | from ctypes.wintypes import WCHAR, USHORT, LPVOID, Structure, Union, ULONG | 21 | from ctypes.wintypes import WCHAR, USHORT, LPVOID, Structure, Union, ULONG |
| 22 | from ctypes.wintypes import byref | 22 | from ctypes.wintypes import byref |
| 23 | 23 | ||
| @@ -33,7 +33,7 @@ ERROR_PRIVILEGE_NOT_HELD = 1314 | |||
| 33 | 33 | ||
| 34 | # Win32 API entry points | 34 | # Win32 API entry points |
| 35 | CreateSymbolicLinkW = kernel32.CreateSymbolicLinkW | 35 | CreateSymbolicLinkW = kernel32.CreateSymbolicLinkW |
| 36 | CreateSymbolicLinkW.restype = BOOL | 36 | CreateSymbolicLinkW.restype = BOOLEAN |
| 37 | CreateSymbolicLinkW.argtypes = (LPCWSTR, # lpSymlinkFileName In | 37 | CreateSymbolicLinkW.argtypes = (LPCWSTR, # lpSymlinkFileName In |
| 38 | LPCWSTR, # lpTargetFileName In | 38 | LPCWSTR, # lpTargetFileName In |
| 39 | DWORD) # dwFlags In | 39 | DWORD) # dwFlags In |
| @@ -145,19 +145,12 @@ def create_dirsymlink(source, link_name): | |||
| 145 | 145 | ||
| 146 | 146 | ||
| 147 | def _create_symlink(source, link_name, dwFlags): | 147 | def _create_symlink(source, link_name, dwFlags): |
| 148 | # Note: Win32 documentation for CreateSymbolicLink is incorrect. | 148 | if not CreateSymbolicLinkW(link_name, source, dwFlags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE): |
| 149 | # On success, the function returns "1". | ||
| 150 | # On error, the function returns some random value (e.g. 1280). | ||
| 151 | # The best bet seems to use "GetLastError" and check for error/success. | ||
| 152 | CreateSymbolicLinkW(link_name, source, dwFlags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE) | ||
| 153 | code = get_last_error() | ||
| 154 | if code != ERROR_SUCCESS: | ||
| 155 | # See https://github.com/golang/go/pull/24307/files#diff-b87bc12e4da2497308f9ef746086e4f0 | 149 | # See https://github.com/golang/go/pull/24307/files#diff-b87bc12e4da2497308f9ef746086e4f0 |
| 156 | # "the unprivileged create flag is unsupported below Windows 10 (1703, v10.0.14972). | 150 | # "the unprivileged create flag is unsupported below Windows 10 (1703, v10.0.14972). |
| 157 | # retry without it." | 151 | # retry without it." |
| 158 | CreateSymbolicLinkW(link_name, source, dwFlags) | 152 | if not CreateSymbolicLinkW(link_name, source, dwFlags): |
| 159 | code = get_last_error() | 153 | code = get_last_error() |
| 160 | if code != ERROR_SUCCESS: | ||
| 161 | error_desc = FormatError(code).strip() | 154 | error_desc = FormatError(code).strip() |
| 162 | if code == ERROR_PRIVILEGE_NOT_HELD: | 155 | if code == ERROR_PRIVILEGE_NOT_HELD: |
| 163 | raise OSError(errno.EPERM, error_desc, link_name) | 156 | raise OSError(errno.EPERM, error_desc, link_name) |
