Debian Codegraph @

pypy/module/posix/interp_nt.py

raw symbols SCIP
1 from rpython.rlib import rwin32
2 from rpython.rtyper.lltypesystem import lltype, rffi
3 from rpython.rlib.objectmodel import specialize
4 from rpython.rlib.rwin32file import make_win32_traits
5 from rpython.rlib._os_support import utf8_traits
6 from rpython.translator import cdir
7 from rpython.translator.tool.cbuild import ExternalCompilationInfo
8 from rpython.rlib.rposix import get_saved_errno
9 from rpython.rlib.rutf8 import codepoints_in_utf8
10
11
12 # XXX: pypy_GetFinalPathNameByHandle is needed to call the dynamically
13 # found GetFinalPathNameByHandle function with a non-standard calling
14 # convention. currently FuncType pointer calls w/ non-standard calling
15 # conventions don't work after translation
16 separate_module_source = """\
17 DWORD
18 pypy_GetFinalPathNameByHandle(FARPROC address, HANDLE hFile,
19 LPTSTR lpszFilePath, DWORD cchFilePath,
20 DWORD dwFlags) {
21 DWORD (WINAPI *func)(HANDLE, LPTSTR, DWORD, DWORD);
22 *(FARPROC*)&func = address;
23 return func(hFile, lpszFilePath, cchFilePath, dwFlags);
24 }
25
26 DWORD
27 pypy_Getppid()
28 /* Copied from CPython win32_getppid */
29 {
30 HANDLE snapshot;
31 DWORD mypid;
32 DWORD result = (DWORD)(-1);
33 BOOL have_record;
34 PROCESSENTRY32 pe;
35
36 mypid = getpid(); /* This function never fails */
37
38 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
39 if (snapshot == INVALID_HANDLE_VALUE)
40 return (DWORD)(-1);
41 pe.dwSize = sizeof(pe);
42 have_record = Process32First(snapshot, &pe);
43 while (have_record) {
44 if (mypid == pe.th32ProcessID) {
45 result = pe.th32ParentProcessID;
46 break;
47 }
48 have_record = Process32Next(snapshot, &pe);
49 }
50 CloseHandle(snapshot);
51 return result;
52 }
53 """
54 eci = ExternalCompilationInfo(
55 includes=['windows.h', 'tlhelp32.h'],
56 include_dirs=[cdir],
57 post_include_bits=[
58 "RPY_EXTERN DWORD "
59 "pypy_GetFinalPathNameByHandle(FARPROC, HANDLE, LPTSTR, DWORD, DWORD);",
60 "RPY_EXTERN DWORD ",
61 "pypy_Getppid();",
62 ],
63 separate_module_sources=[separate_module_source])
64 pypy_GetFinalPathNameByHandle = rffi.llexternal(
65 'pypy_GetFinalPathNameByHandle',
66 [rffi.VOIDP, rwin32.HANDLE, rffi.CWCHARP, rwin32.DWORD, rwin32.DWORD],
67 rwin32.DWORD, compilation_info=eci)
68 pypy_Getppid = rffi.llexternal(
69 'pypy_Getppid', [], rwin32.DWORD, compilation_info=eci)
70
71
72 # plain NotImplementedError is invalid RPython
73 class LLNotImplemented(Exception):
74
75 def __init__(self, msg):
76 self.msg = msg
77
78
79 def make_traits(traits):
80 win32traits = make_win32_traits(traits)
81
82 class NTTraits(win32traits):
83
84 GetFinalPathNameByHandle_HANDLE = lltype.nullptr(rffi.VOIDP.TO)
85
86 def check_GetFinalPathNameByHandle(self):
87 if (self.GetFinalPathNameByHandle_HANDLE !=
88 lltype.nullptr(rffi.VOIDP.TO)):
89 return True
90
91 from rpython.rlib.rdynload import GetModuleHandle, dlsym
92 hKernel32 = GetModuleHandle("KERNEL32")
93 try:
94 func = dlsym(hKernel32, 'GetFinalPathNameByHandleW')
95 except KeyError:
96 return False
97
98 self.GetFinalPathNameByHandle_HANDLE = func
99 return True
100
101 def GetFinalPathNameByHandle(self, *args):
102 assert (self.GetFinalPathNameByHandle_HANDLE !=
103 lltype.nullptr(rffi.VOIDP.TO))
104 return pypy_GetFinalPathNameByHandle(
105 self.GetFinalPathNameByHandle_HANDLE, *args)
106
107 return NTTraits()
108
109
110 def make__getfileinformation_impl(traits):
111 win32traits = make_traits(traits)
112
113 def _getfileinformation_llimpl(fd):
114 hFile = rwin32.get_osfhandle(fd)
115 with lltype.scoped_alloc(
116 win32traits.BY_HANDLE_FILE_INFORMATION) as info:
117 if win32traits.GetFileInformationByHandle(hFile, info) == 0:
118 raise rwin32.lastSavedWindowsError("_getfileinformation")
119 return (rffi.cast(lltype.Signed, info.c_dwVolumeSerialNumber),
120 rffi.cast(lltype.Signed, info.c_nFileIndexHigh),
121 rffi.cast(lltype.Signed, info.c_nFileIndexLow))
122
123 return _getfileinformation_llimpl
124
125
126 def make__getfinalpathname_impl(traits):
127 win32traits = make_traits(traits)
128
129 def _getfinalpathname_llimpl(utf8):
130 if not win32traits.check_GetFinalPathNameByHandle():
131 raise LLNotImplemented("GetFinalPathNameByHandle not available on "
132 "this platform")
133 with rffi.scoped_utf82wcharp(utf8, codepoints_in_utf8(utf8)) as buf:
134 hFile = win32traits.CreateFile(buf, 0, 0, None,
135 win32traits.OPEN_EXISTING,
136 win32traits.FILE_FLAG_BACKUP_SEMANTICS,
137 rwin32.NULL_HANDLE)
138 if hFile == rwin32.INVALID_HANDLE_VALUE:
139 raise rwin32.lastSavedWindowsError("CreateFile")
140
141 VOLUME_NAME_DOS = rffi.cast(rwin32.DWORD, win32traits.VOLUME_NAME_DOS)
142 try:
143 usize = win32traits.GetFinalPathNameByHandle(
144 hFile,
145 lltype.nullptr(traits.CCHARP.TO),
146 rffi.cast(rwin32.DWORD, 0),
147 VOLUME_NAME_DOS)
148 if usize == 0:
149 raise rwin32.lastSavedWindowsError("GetFinalPathNameByHandle")
150
151 size = rffi.cast(lltype.Signed, usize)
152 with rffi.scoped_alloc_unicodebuffer(size + 1) as buf:
153 result = win32traits.GetFinalPathNameByHandle(
154 hFile,
155 buf.raw,
156 usize,
157 VOLUME_NAME_DOS)
158 if result == 0:
159 raise rwin32.lastSavedWindowsError("GetFinalPathNameByHandle")
160 res = buf.str(rffi.cast(lltype.Signed, result))
161 return res.encode('utf8'), len(res)
162 finally:
163 rwin32.CloseHandle(hFile)
164
165 return _getfinalpathname_llimpl
166
167
168 _getfileinformation = make__getfileinformation_impl(utf8_traits)
169 _getfinalpathname = make__getfinalpathname_impl(utf8_traits)
170
171
172 def win32_getppid():
173 ret = pypy_Getppid()
174 if ret == rffi.cast(rwin32.DWORD, -1):
175 raise OSError(get_saved_errno(), 'getppid failed')
176 return ret