| import os |
| import pathlib |
| import platform |
| import stat |
| import sys |
| from logging import getLogger |
| from typing import Union |
|
|
| if sys.platform == "win32": |
| import ctypes |
| from ctypes.wintypes import BOOL, DWORD, HANDLE, LPCWSTR, LPDWORD, LPVOID, LPWSTR |
|
|
| _stdcall_libraries = {} |
| _stdcall_libraries["kernel32"] = ctypes.WinDLL("kernel32") |
| CloseHandle = _stdcall_libraries["kernel32"].CloseHandle |
| CreateFileW = _stdcall_libraries["kernel32"].CreateFileW |
| DeviceIoControl = _stdcall_libraries["kernel32"].DeviceIoControl |
| GetFileAttributesW = _stdcall_libraries["kernel32"].GetFileAttributesW |
| OPEN_EXISTING = 3 |
| GENERIC_READ = 2147483648 |
| FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000 |
| FSCTL_GET_REPARSE_POINT = 0x000900A8 |
| FILE_FLAG_BACKUP_SEMANTICS = 0x02000000 |
| IO_REPARSE_TAG_MOUNT_POINT = 0xA0000003 |
| IO_REPARSE_TAG_SYMLINK = 0xA000000C |
| MAXIMUM_REPARSE_DATA_BUFFER_SIZE = 16 * 1024 |
|
|
| def _check_bit(val: int, flag: int) -> bool: |
| return bool(val & flag == flag) |
|
|
| class SymbolicLinkReparseBuffer(ctypes.Structure): |
| """Implementing the below in Python: |
| |
| typedef struct _REPARSE_DATA_BUFFER { |
| ULONG ReparseTag; |
| USHORT ReparseDataLength; |
| USHORT Reserved; |
| union { |
| struct { |
| USHORT SubstituteNameOffset; |
| USHORT SubstituteNameLength; |
| USHORT PrintNameOffset; |
| USHORT PrintNameLength; |
| ULONG Flags; |
| WCHAR PathBuffer[1]; |
| } SymbolicLinkReparseBuffer; |
| struct { |
| USHORT SubstituteNameOffset; |
| USHORT SubstituteNameLength; |
| USHORT PrintNameOffset; |
| USHORT PrintNameLength; |
| WCHAR PathBuffer[1]; |
| } MountPointReparseBuffer; |
| struct { |
| UCHAR DataBuffer[1]; |
| } GenericReparseBuffer; |
| } DUMMYUNIONNAME; |
| } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| """ |
|
|
| |
| _fields_ = [ |
| ("flags", ctypes.c_ulong), |
| ("path_buffer", ctypes.c_byte * (MAXIMUM_REPARSE_DATA_BUFFER_SIZE - 20)), |
| ] |
|
|
| class MountReparseBuffer(ctypes.Structure): |
| _fields_ = [ |
| ("path_buffer", ctypes.c_byte * (MAXIMUM_REPARSE_DATA_BUFFER_SIZE - 16)), |
| ] |
|
|
| class ReparseBufferField(ctypes.Union): |
| _fields_ = [ |
| ("symlink", SymbolicLinkReparseBuffer), |
| ("mount", MountReparseBuffer), |
| ] |
|
|
| class ReparseBuffer(ctypes.Structure): |
| _anonymous_ = ("u",) |
| _fields_ = [ |
| ("reparse_tag", ctypes.c_ulong), |
| ("reparse_data_length", ctypes.c_ushort), |
| ("reserved", ctypes.c_ushort), |
| ("substitute_name_offset", ctypes.c_ushort), |
| ("substitute_name_length", ctypes.c_ushort), |
| ("print_name_offset", ctypes.c_ushort), |
| ("print_name_length", ctypes.c_ushort), |
| ("u", ReparseBufferField), |
| ] |
|
|
| def is_reparse_point(path: str | pathlib.Path) -> bool: |
| GetFileAttributesW.argtypes = [LPCWSTR] |
| GetFileAttributesW.restype = DWORD |
| return _check_bit(GetFileAttributesW(str(path)), stat.FILE_ATTRIBUTE_REPARSE_POINT) |
|
|
| def readlink(path: str | pathlib.Path) -> str | pathlib.WindowsPath: |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| target_is_path = isinstance(path, pathlib.Path) |
| target = str(path) |
| CreateFileW.argtypes = [LPWSTR, DWORD, DWORD, LPVOID, DWORD, DWORD, HANDLE] |
| CreateFileW.restype = HANDLE |
| DeviceIoControl.argtypes = [ |
| HANDLE, |
| DWORD, |
| LPVOID, |
| DWORD, |
| LPVOID, |
| DWORD, |
| LPDWORD, |
| LPVOID, |
| ] |
| DeviceIoControl.restype = BOOL |
| handle = HANDLE( |
| CreateFileW( |
| target, |
| GENERIC_READ, |
| 0, |
| None, |
| OPEN_EXISTING, |
| FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, |
| 0, |
| ) |
| ) |
| buf = ReparseBuffer() |
| ret = DWORD(0) |
| status = DeviceIoControl( |
| handle, |
| FSCTL_GET_REPARSE_POINT, |
| None, |
| 0, |
| ctypes.byref(buf), |
| MAXIMUM_REPARSE_DATA_BUFFER_SIZE, |
| ctypes.byref(ret), |
| None, |
| ) |
| CloseHandle(handle) |
| if not status: |
| logger = getLogger(__file__) |
| logger.error(f"Failed IOCTL access to REPARSE_POINT {target})") |
| raise ValueError("not a symbolic link or access permission violation") |
|
|
| if buf.reparse_tag == IO_REPARSE_TAG_SYMLINK: |
| offset = buf.substitute_name_offset |
| ending = offset + buf.substitute_name_length |
| rpath = bytearray(buf.symlink.path_buffer)[offset:ending].decode("UTF-16-LE") |
| elif buf.reparse_tag == IO_REPARSE_TAG_MOUNT_POINT: |
| offset = buf.substitute_name_offset |
| ending = offset + buf.substitute_name_length |
| rpath = bytearray(buf.mount.path_buffer)[offset:ending].decode("UTF-16-LE") |
| else: |
| raise ValueError("not a symbolic link") |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if rpath.startswith("\\??\\"): |
| rpath = "\\\\" + rpath[2:] |
| if target_is_path: |
| return pathlib.WindowsPath(rpath) |
| else: |
| return rpath |
|
|
|
|
| def is_windows_native_python() -> bool: |
| return ( |
| sys.platform == "win32" |
| and os.name == "nt" |
| and "cygwin" not in platform.system().lower() |
| and "cygwin" not in sys.platform |
| ) |
|
|
|
|
| def is_windows_unc_path(path) -> bool: |
| return sys.platform == "win32" and path.drive.startswith("\\\\") |
|
|