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