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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
from rpython.rlib import rwin32
from rpython.rtyper.lltypesystem import lltype, rffi
from rpython.rlib.objectmodel import specialize
from rpython.rlib.rwin32file import make_win32_traits
from rpython.rlib._os_support import utf8_traits
from rpython.translator import cdir
from rpython.translator.tool.cbuild import ExternalCompilationInfo
from rpython.rlib.rposix import get_saved_errno
from rpython.rlib.rutf8 import codepoints_in_utf8
# 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);
}
DWORD
pypy_Getppid()
/* Copied from CPython win32_getppid */
{
HANDLE snapshot;
DWORD mypid;
DWORD result = (DWORD)(-1);
BOOL have_record;
PROCESSENTRY32 pe;
mypid = getpid(); /* This function never fails */
snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snapshot == INVALID_HANDLE_VALUE)
return (DWORD)(-1);
pe.dwSize = sizeof(pe);
have_record = Process32First(snapshot, &pe);
while (have_record) {
if (mypid == pe.th32ProcessID) {
result = pe.th32ParentProcessID;
break;
}
have_record = Process32Next(snapshot, &pe);
}
CloseHandle(snapshot);
return result;
}
"""
eci = ExternalCompilationInfo(
includes=['windows.h', 'tlhelp32.h'],
include_dirs=[cdir],
post_include_bits=[
"RPY_EXTERN DWORD "
"pypy_GetFinalPathNameByHandle(FARPROC, HANDLE, LPTSTR, DWORD, DWORD);",
"RPY_EXTERN DWORD ",
"pypy_Getppid();",
],
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)
pypy_Getppid = rffi.llexternal(
'pypy_Getppid', [], rwin32.DWORD, compilation_info=eci)
# plain NotImplementedError is invalid RPython
class LLNotImplemented(Exception):
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):
win32traits = make_traits(traits)
def _getfinalpathname_llimpl(utf8):
if not win32traits.check_GetFinalPathNameByHandle():
raise LLNotImplemented("GetFinalPathNameByHandle not available on "
"this platform")
with rffi.scoped_utf82wcharp(utf8, codepoints_in_utf8(utf8)) as buf:
hFile = win32traits.CreateFile(buf, 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")
res = buf.str(rffi.cast(lltype.Signed, result))
return res.encode('utf8'), len(res)
finally:
rwin32.CloseHandle(hFile)
return _getfinalpathname_llimpl
_getfileinformation = make__getfileinformation_impl(utf8_traits)
_getfinalpathname = make__getfinalpathname_impl(utf8_traits)
def win32_getppid():
ret = pypy_Getppid()
if ret == rffi.cast(rwin32.DWORD, -1):
raise OSError(get_saved_errno(), 'getppid failed')
return ret
|