1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
from rpython.rlib import rwin32
from rpython.rtyper.lltypesystem import lltype, rffi
from rpython.rlib.rwin32file import make_win32_traits
from rpython.rlib._os_support import UnicodeTraits
from rpython.translator import cdir
from rpython.translator.tool.cbuild import ExternalCompilationInfo
# XXX: pypy_GetFinalPathNameByHandle is needed to call the dynamically
# found GetFinalPathNameByHandle function with a non-standard calling
# convention. currently FuncType pointer calls w/ non-standard calling
# conventions don't work after translation
separate_module_source = """\
DWORD
pypy_GetFinalPathNameByHandle(FARPROC address, HANDLE hFile,
LPTSTR lpszFilePath, DWORD cchFilePath,
DWORD dwFlags) {
DWORD (WINAPI *func)(HANDLE, LPTSTR, DWORD, DWORD);
*(FARPROC*)&func = address;
return func(hFile, lpszFilePath, cchFilePath, dwFlags);
}
"""
eci = ExternalCompilationInfo(
includes=['windows.h'],
include_dirs=[cdir],
post_include_bits=[
"RPY_EXTERN DWORD "
"pypy_GetFinalPathNameByHandle(FARPROC, HANDLE, LPTSTR, DWORD, DWORD);"],
separate_module_sources=[separate_module_source])
pypy_GetFinalPathNameByHandle = rffi.llexternal(
'pypy_GetFinalPathNameByHandle',
[rffi.VOIDP, rwin32.HANDLE, rffi.CWCHARP, rwin32.DWORD, rwin32.DWORD],
rwin32.DWORD, compilation_info=eci)
# plain NotImplementedError is invalid RPython
class LLNotImplemented(NotImplementedError):
def __init__(self, msg):
self.msg = msg
def make_traits(traits):
win32traits = make_win32_traits(traits)
class NTTraits(win32traits):
GetFinalPathNameByHandle_HANDLE = lltype.nullptr(rffi.VOIDP.TO)
def check_GetFinalPathNameByHandle(self):
if (self.GetFinalPathNameByHandle_HANDLE !=
lltype.nullptr(rffi.VOIDP.TO)):
return True
from rpython.rlib.rdynload import GetModuleHandle, dlsym
hKernel32 = GetModuleHandle("KERNEL32")
try:
func = dlsym(hKernel32, 'GetFinalPathNameByHandleW')
except KeyError:
return False
self.GetFinalPathNameByHandle_HANDLE = func
return True
def GetFinalPathNameByHandle(self, *args):
assert (self.GetFinalPathNameByHandle_HANDLE !=
lltype.nullptr(rffi.VOIDP.TO))
return pypy_GetFinalPathNameByHandle(
self.GetFinalPathNameByHandle_HANDLE, *args)
return NTTraits()
def make__getfileinformation_impl(traits):
win32traits = make_traits(traits)
def _getfileinformation_llimpl(fd):
hFile = rwin32.get_osfhandle(fd)
with lltype.scoped_alloc(
win32traits.BY_HANDLE_FILE_INFORMATION) as info:
if win32traits.GetFileInformationByHandle(hFile, info) == 0:
raise rwin32.lastSavedWindowsError("_getfileinformation")
return (rffi.cast(lltype.Signed, info.c_dwVolumeSerialNumber),
rffi.cast(lltype.Signed, info.c_nFileIndexHigh),
rffi.cast(lltype.Signed, info.c_nFileIndexLow))
return _getfileinformation_llimpl
def make__getfinalpathname_impl(traits):
assert traits.str is unicode, 'Currently only handles unicode paths'
win32traits = make_traits(traits)
def _getfinalpathname_llimpl(path):
if not win32traits.check_GetFinalPathNameByHandle():
raise LLNotImplemented("GetFinalPathNameByHandle not available on "
"this platform")
hFile = win32traits.CreateFile(path, 0, 0, None,
win32traits.OPEN_EXISTING,
win32traits.FILE_FLAG_BACKUP_SEMANTICS,
rwin32.NULL_HANDLE)
if hFile == rwin32.INVALID_HANDLE_VALUE:
raise rwin32.lastSavedWindowsError("CreateFile")
VOLUME_NAME_DOS = rffi.cast(rwin32.DWORD, win32traits.VOLUME_NAME_DOS)
try:
usize = win32traits.GetFinalPathNameByHandle(
hFile,
lltype.nullptr(traits.CCHARP.TO),
rffi.cast(rwin32.DWORD, 0),
VOLUME_NAME_DOS)
if usize == 0:
raise rwin32.lastSavedWindowsError("GetFinalPathNameByHandle")
size = rffi.cast(lltype.Signed, usize)
with rffi.scoped_alloc_unicodebuffer(size + 1) as buf:
result = win32traits.GetFinalPathNameByHandle(
hFile,
buf.raw,
usize,
VOLUME_NAME_DOS)
if result == 0:
raise rwin32.lastSavedWindowsError("GetFinalPathNameByHandle")
return buf.str(rffi.cast(lltype.Signed, result))
finally:
rwin32.CloseHandle(hFile)
return _getfinalpathname_llimpl
_getfileinformation = make__getfileinformation_impl(UnicodeTraits())
_getfinalpathname = make__getfinalpathname_impl(UnicodeTraits())
|