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