Mike Frysinger | f601376 | 2019-06-13 02:30:51 -0400 | [diff] [blame] | 1 | # -*- coding:utf-8 -*- |
Renaud Paquay | d5cec5e | 2016-11-01 11:24:03 -0700 | [diff] [blame] | 2 | # |
| 3 | # Copyright (C) 2016 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | import errno |
| 18 | |
Rostislav Krasny | b71d61d | 2020-01-24 22:29:54 +0200 | [diff] [blame] | 19 | from pyversion import is_python3 |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 20 | from ctypes import WinDLL, get_last_error, FormatError, WinError, addressof |
| 21 | from ctypes import c_buffer |
Remy Böhmer | dbd277c | 2020-01-07 08:48:55 +0100 | [diff] [blame] | 22 | from ctypes.wintypes import BOOL, BOOLEAN, LPCWSTR, DWORD, HANDLE |
| 23 | from ctypes.wintypes import WCHAR, USHORT, LPVOID, ULONG |
| 24 | if is_python3(): |
| 25 | from ctypes import c_ubyte, Structure, Union, byref |
| 26 | from ctypes.wintypes import LPDWORD |
| 27 | else: |
| 28 | # For legacy Python2 different imports are needed. |
| 29 | from ctypes.wintypes import POINTER, c_ubyte, Structure, Union, byref |
| 30 | LPDWORD = POINTER(DWORD) |
Renaud Paquay | d5cec5e | 2016-11-01 11:24:03 -0700 | [diff] [blame] | 31 | |
| 32 | kernel32 = WinDLL('kernel32', use_last_error=True) |
| 33 | |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 34 | UCHAR = c_ubyte |
| 35 | |
Renaud Paquay | d5cec5e | 2016-11-01 11:24:03 -0700 | [diff] [blame] | 36 | # Win32 error codes |
| 37 | ERROR_SUCCESS = 0 |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 38 | ERROR_NOT_SUPPORTED = 50 |
Renaud Paquay | d5cec5e | 2016-11-01 11:24:03 -0700 | [diff] [blame] | 39 | ERROR_PRIVILEGE_NOT_HELD = 1314 |
| 40 | |
| 41 | # Win32 API entry points |
| 42 | CreateSymbolicLinkW = kernel32.CreateSymbolicLinkW |
Роман Донченко | a84df06 | 2019-03-21 23:45:59 +0300 | [diff] [blame] | 43 | CreateSymbolicLinkW.restype = BOOLEAN |
Renaud Paquay | d5cec5e | 2016-11-01 11:24:03 -0700 | [diff] [blame] | 44 | CreateSymbolicLinkW.argtypes = (LPCWSTR, # lpSymlinkFileName In |
| 45 | LPCWSTR, # lpTargetFileName In |
| 46 | DWORD) # dwFlags In |
| 47 | |
| 48 | # Symbolic link creation flags |
| 49 | SYMBOLIC_LINK_FLAG_FILE = 0x00 |
| 50 | SYMBOLIC_LINK_FLAG_DIRECTORY = 0x01 |
Renaud Paquay | 2b42d28 | 2018-10-01 14:59:48 -0700 | [diff] [blame] | 51 | # symlink support for CreateSymbolicLink() starting with Windows 10 (1703, v10.0.14972) |
| 52 | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE = 0x02 |
Renaud Paquay | d5cec5e | 2016-11-01 11:24:03 -0700 | [diff] [blame] | 53 | |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 54 | GetFileAttributesW = kernel32.GetFileAttributesW |
| 55 | GetFileAttributesW.restype = DWORD |
| 56 | GetFileAttributesW.argtypes = (LPCWSTR,) # lpFileName In |
| 57 | |
| 58 | INVALID_FILE_ATTRIBUTES = 0xFFFFFFFF |
| 59 | FILE_ATTRIBUTE_REPARSE_POINT = 0x00400 |
| 60 | |
| 61 | CreateFileW = kernel32.CreateFileW |
| 62 | CreateFileW.restype = HANDLE |
| 63 | CreateFileW.argtypes = (LPCWSTR, # lpFileName In |
| 64 | DWORD, # dwDesiredAccess In |
| 65 | DWORD, # dwShareMode In |
| 66 | LPVOID, # lpSecurityAttributes In_opt |
| 67 | DWORD, # dwCreationDisposition In |
| 68 | DWORD, # dwFlagsAndAttributes In |
| 69 | HANDLE) # hTemplateFile In_opt |
| 70 | |
| 71 | CloseHandle = kernel32.CloseHandle |
| 72 | CloseHandle.restype = BOOL |
| 73 | CloseHandle.argtypes = (HANDLE,) # hObject In |
| 74 | |
| 75 | INVALID_HANDLE_VALUE = HANDLE(-1).value |
| 76 | OPEN_EXISTING = 3 |
| 77 | FILE_FLAG_BACKUP_SEMANTICS = 0x02000000 |
| 78 | FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000 |
| 79 | |
| 80 | DeviceIoControl = kernel32.DeviceIoControl |
| 81 | DeviceIoControl.restype = BOOL |
| 82 | DeviceIoControl.argtypes = (HANDLE, # hDevice In |
| 83 | DWORD, # dwIoControlCode In |
| 84 | LPVOID, # lpInBuffer In_opt |
| 85 | DWORD, # nInBufferSize In |
| 86 | LPVOID, # lpOutBuffer Out_opt |
| 87 | DWORD, # nOutBufferSize In |
| 88 | LPDWORD, # lpBytesReturned Out_opt |
| 89 | LPVOID) # lpOverlapped Inout_opt |
| 90 | |
| 91 | # Device I/O control flags and options |
| 92 | FSCTL_GET_REPARSE_POINT = 0x000900A8 |
| 93 | IO_REPARSE_TAG_MOUNT_POINT = 0xA0000003 |
| 94 | IO_REPARSE_TAG_SYMLINK = 0xA000000C |
| 95 | MAXIMUM_REPARSE_DATA_BUFFER_SIZE = 0x4000 |
| 96 | |
| 97 | |
| 98 | class GENERIC_REPARSE_BUFFER(Structure): |
| 99 | _fields_ = (('DataBuffer', UCHAR * 1),) |
| 100 | |
| 101 | |
| 102 | class SYMBOLIC_LINK_REPARSE_BUFFER(Structure): |
| 103 | _fields_ = (('SubstituteNameOffset', USHORT), |
| 104 | ('SubstituteNameLength', USHORT), |
| 105 | ('PrintNameOffset', USHORT), |
| 106 | ('PrintNameLength', USHORT), |
| 107 | ('Flags', ULONG), |
| 108 | ('PathBuffer', WCHAR * 1)) |
| 109 | |
| 110 | @property |
| 111 | def PrintName(self): |
| 112 | arrayt = WCHAR * (self.PrintNameLength // 2) |
| 113 | offset = type(self).PathBuffer.offset + self.PrintNameOffset |
| 114 | return arrayt.from_address(addressof(self) + offset).value |
| 115 | |
| 116 | |
| 117 | class MOUNT_POINT_REPARSE_BUFFER(Structure): |
| 118 | _fields_ = (('SubstituteNameOffset', USHORT), |
| 119 | ('SubstituteNameLength', USHORT), |
| 120 | ('PrintNameOffset', USHORT), |
| 121 | ('PrintNameLength', USHORT), |
| 122 | ('PathBuffer', WCHAR * 1)) |
| 123 | |
| 124 | @property |
| 125 | def PrintName(self): |
| 126 | arrayt = WCHAR * (self.PrintNameLength // 2) |
| 127 | offset = type(self).PathBuffer.offset + self.PrintNameOffset |
| 128 | return arrayt.from_address(addressof(self) + offset).value |
| 129 | |
| 130 | |
| 131 | class REPARSE_DATA_BUFFER(Structure): |
| 132 | class REPARSE_BUFFER(Union): |
| 133 | _fields_ = (('SymbolicLinkReparseBuffer', SYMBOLIC_LINK_REPARSE_BUFFER), |
| 134 | ('MountPointReparseBuffer', MOUNT_POINT_REPARSE_BUFFER), |
| 135 | ('GenericReparseBuffer', GENERIC_REPARSE_BUFFER)) |
| 136 | _fields_ = (('ReparseTag', ULONG), |
| 137 | ('ReparseDataLength', USHORT), |
| 138 | ('Reserved', USHORT), |
| 139 | ('ReparseBuffer', REPARSE_BUFFER)) |
| 140 | _anonymous_ = ('ReparseBuffer',) |
| 141 | |
Renaud Paquay | d5cec5e | 2016-11-01 11:24:03 -0700 | [diff] [blame] | 142 | |
| 143 | def create_filesymlink(source, link_name): |
| 144 | """Creates a Windows file symbolic link source pointing to link_name.""" |
| 145 | _create_symlink(source, link_name, SYMBOLIC_LINK_FLAG_FILE) |
| 146 | |
| 147 | |
| 148 | def create_dirsymlink(source, link_name): |
| 149 | """Creates a Windows directory symbolic link source pointing to link_name. |
| 150 | """ |
| 151 | _create_symlink(source, link_name, SYMBOLIC_LINK_FLAG_DIRECTORY) |
| 152 | |
| 153 | |
| 154 | def _create_symlink(source, link_name, dwFlags): |
David Pursehouse | 3cda50a | 2020-02-13 13:17:03 +0900 | [diff] [blame] | 155 | if not CreateSymbolicLinkW(link_name, source, |
| 156 | dwFlags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE): |
Renaud Paquay | 2b42d28 | 2018-10-01 14:59:48 -0700 | [diff] [blame] | 157 | # See https://github.com/golang/go/pull/24307/files#diff-b87bc12e4da2497308f9ef746086e4f0 |
| 158 | # "the unprivileged create flag is unsupported below Windows 10 (1703, v10.0.14972). |
| 159 | # retry without it." |
Роман Донченко | a84df06 | 2019-03-21 23:45:59 +0300 | [diff] [blame] | 160 | if not CreateSymbolicLinkW(link_name, source, dwFlags): |
| 161 | code = get_last_error() |
Renaud Paquay | 2b42d28 | 2018-10-01 14:59:48 -0700 | [diff] [blame] | 162 | error_desc = FormatError(code).strip() |
| 163 | if code == ERROR_PRIVILEGE_NOT_HELD: |
| 164 | raise OSError(errno.EPERM, error_desc, link_name) |
| 165 | _raise_winerror( |
| 166 | code, |
| 167 | 'Error creating symbolic link \"%s\"'.format(link_name)) |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 168 | |
| 169 | |
| 170 | def islink(path): |
| 171 | result = GetFileAttributesW(path) |
| 172 | if result == INVALID_FILE_ATTRIBUTES: |
| 173 | return False |
| 174 | return bool(result & FILE_ATTRIBUTE_REPARSE_POINT) |
| 175 | |
| 176 | |
| 177 | def readlink(path): |
| 178 | reparse_point_handle = CreateFileW(path, |
| 179 | 0, |
| 180 | 0, |
| 181 | None, |
| 182 | OPEN_EXISTING, |
| 183 | FILE_FLAG_OPEN_REPARSE_POINT | |
| 184 | FILE_FLAG_BACKUP_SEMANTICS, |
| 185 | None) |
| 186 | if reparse_point_handle == INVALID_HANDLE_VALUE: |
| 187 | _raise_winerror( |
| 188 | get_last_error(), |
Rostislav Krasny | 9da67fe | 2020-01-24 23:15:09 +0200 | [diff] [blame] | 189 | 'Error opening symbolic link \"%s\"'.format(path)) |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 190 | target_buffer = c_buffer(MAXIMUM_REPARSE_DATA_BUFFER_SIZE) |
| 191 | n_bytes_returned = DWORD() |
| 192 | io_result = DeviceIoControl(reparse_point_handle, |
| 193 | FSCTL_GET_REPARSE_POINT, |
| 194 | None, |
| 195 | 0, |
| 196 | target_buffer, |
| 197 | len(target_buffer), |
| 198 | byref(n_bytes_returned), |
| 199 | None) |
| 200 | CloseHandle(reparse_point_handle) |
| 201 | if not io_result: |
| 202 | _raise_winerror( |
| 203 | get_last_error(), |
Rostislav Krasny | 9da67fe | 2020-01-24 23:15:09 +0200 | [diff] [blame] | 204 | 'Error reading symbolic link \"%s\"'.format(path)) |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 205 | rdb = REPARSE_DATA_BUFFER.from_buffer(target_buffer) |
| 206 | if rdb.ReparseTag == IO_REPARSE_TAG_SYMLINK: |
| 207 | return _preserve_encoding(path, rdb.SymbolicLinkReparseBuffer.PrintName) |
| 208 | elif rdb.ReparseTag == IO_REPARSE_TAG_MOUNT_POINT: |
| 209 | return _preserve_encoding(path, rdb.MountPointReparseBuffer.PrintName) |
| 210 | # Unsupported reparse point type |
| 211 | _raise_winerror( |
| 212 | ERROR_NOT_SUPPORTED, |
Rostislav Krasny | 9da67fe | 2020-01-24 23:15:09 +0200 | [diff] [blame] | 213 | 'Error reading symbolic link \"%s\"'.format(path)) |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 214 | |
| 215 | |
| 216 | def _preserve_encoding(source, target): |
| 217 | """Ensures target is the same string type (i.e. unicode or str) as source.""" |
Rostislav Krasny | b71d61d | 2020-01-24 22:29:54 +0200 | [diff] [blame] | 218 | |
| 219 | if is_python3(): |
| 220 | return target |
| 221 | |
David Pursehouse | a46bf7d | 2020-02-15 12:45:53 +0900 | [diff] [blame] | 222 | if isinstance(source, unicode): # noqa: F821 |
| 223 | return unicode(target) # noqa: F821 |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 224 | return str(target) |
| 225 | |
| 226 | |
| 227 | def _raise_winerror(code, error_desc): |
| 228 | win_error_desc = FormatError(code).strip() |
| 229 | error_desc = "%s: %s".format(error_desc, win_error_desc) |
| 230 | raise WinError(code, error_desc) |