diff options
Diffstat (limited to 'platform_utils_win32.py')
| -rw-r--r-- | platform_utils_win32.py | 29 |
1 files changed, 7 insertions, 22 deletions
diff --git a/platform_utils_win32.py b/platform_utils_win32.py index e20755a4..bf916d47 100644 --- a/platform_utils_win32.py +++ b/platform_utils_win32.py | |||
| @@ -1,5 +1,3 @@ | |||
| 1 | # -*- coding:utf-8 -*- | ||
| 2 | # | ||
| 3 | # Copyright (C) 2016 The Android Open Source Project | 1 | # Copyright (C) 2016 The Android Open Source Project |
| 4 | # | 2 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); | 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| @@ -16,16 +14,13 @@ | |||
| 16 | 14 | ||
| 17 | import errno | 15 | import errno |
| 18 | 16 | ||
| 19 | from pyversion import is_python3 | ||
| 20 | from ctypes import WinDLL, get_last_error, FormatError, WinError, addressof | 17 | from ctypes import WinDLL, get_last_error, FormatError, WinError, addressof |
| 21 | from ctypes import c_buffer | 18 | from ctypes import c_buffer, c_ubyte, Structure, Union, byref |
| 22 | from ctypes.wintypes import BOOL, BOOLEAN, LPCWSTR, DWORD, HANDLE, POINTER, c_ubyte | 19 | from ctypes.wintypes import BOOL, BOOLEAN, LPCWSTR, DWORD, HANDLE |
| 23 | from ctypes.wintypes import WCHAR, USHORT, LPVOID, Structure, Union, ULONG | 20 | from ctypes.wintypes import WCHAR, USHORT, LPVOID, ULONG, LPDWORD |
| 24 | from ctypes.wintypes import byref | ||
| 25 | 21 | ||
| 26 | kernel32 = WinDLL('kernel32', use_last_error=True) | 22 | kernel32 = WinDLL('kernel32', use_last_error=True) |
| 27 | 23 | ||
| 28 | LPDWORD = POINTER(DWORD) | ||
| 29 | UCHAR = c_ubyte | 24 | UCHAR = c_ubyte |
| 30 | 25 | ||
| 31 | # Win32 error codes | 26 | # Win32 error codes |
| @@ -147,7 +142,8 @@ def create_dirsymlink(source, link_name): | |||
| 147 | 142 | ||
| 148 | 143 | ||
| 149 | def _create_symlink(source, link_name, dwFlags): | 144 | def _create_symlink(source, link_name, dwFlags): |
| 150 | if not CreateSymbolicLinkW(link_name, source, dwFlags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE): | 145 | if not CreateSymbolicLinkW(link_name, source, |
| 146 | dwFlags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE): | ||
| 151 | # See https://github.com/golang/go/pull/24307/files#diff-b87bc12e4da2497308f9ef746086e4f0 | 147 | # See https://github.com/golang/go/pull/24307/files#diff-b87bc12e4da2497308f9ef746086e4f0 |
| 152 | # "the unprivileged create flag is unsupported below Windows 10 (1703, v10.0.14972). | 148 | # "the unprivileged create flag is unsupported below Windows 10 (1703, v10.0.14972). |
| 153 | # retry without it." | 149 | # retry without it." |
| @@ -198,26 +194,15 @@ def readlink(path): | |||
| 198 | 'Error reading symbolic link \"%s\"'.format(path)) | 194 | 'Error reading symbolic link \"%s\"'.format(path)) |
| 199 | rdb = REPARSE_DATA_BUFFER.from_buffer(target_buffer) | 195 | rdb = REPARSE_DATA_BUFFER.from_buffer(target_buffer) |
| 200 | if rdb.ReparseTag == IO_REPARSE_TAG_SYMLINK: | 196 | if rdb.ReparseTag == IO_REPARSE_TAG_SYMLINK: |
| 201 | return _preserve_encoding(path, rdb.SymbolicLinkReparseBuffer.PrintName) | 197 | return rdb.SymbolicLinkReparseBuffer.PrintName |
| 202 | elif rdb.ReparseTag == IO_REPARSE_TAG_MOUNT_POINT: | 198 | elif rdb.ReparseTag == IO_REPARSE_TAG_MOUNT_POINT: |
| 203 | return _preserve_encoding(path, rdb.MountPointReparseBuffer.PrintName) | 199 | return rdb.MountPointReparseBuffer.PrintName |
| 204 | # Unsupported reparse point type | 200 | # Unsupported reparse point type |
| 205 | _raise_winerror( | 201 | _raise_winerror( |
| 206 | ERROR_NOT_SUPPORTED, | 202 | ERROR_NOT_SUPPORTED, |
| 207 | 'Error reading symbolic link \"%s\"'.format(path)) | 203 | 'Error reading symbolic link \"%s\"'.format(path)) |
| 208 | 204 | ||
| 209 | 205 | ||
| 210 | def _preserve_encoding(source, target): | ||
| 211 | """Ensures target is the same string type (i.e. unicode or str) as source.""" | ||
| 212 | |||
| 213 | if is_python3(): | ||
| 214 | return target | ||
| 215 | |||
| 216 | if isinstance(source, unicode): | ||
| 217 | return unicode(target) | ||
| 218 | return str(target) | ||
| 219 | |||
| 220 | |||
| 221 | def _raise_winerror(code, error_desc): | 206 | def _raise_winerror(code, error_desc): |
| 222 | win_error_desc = FormatError(code).strip() | 207 | win_error_desc = FormatError(code).strip() |
| 223 | error_desc = "%s: %s".format(error_desc, win_error_desc) | 208 | error_desc = "%s: %s".format(error_desc, win_error_desc) |
