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