def scandir(path): path = pathobj.os_stringify(path).encode('utf-8') req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True) dirent = lltype.malloc(uv.dirent_ptr.TO, flavor='raw', zero=True) try: response = uv_callback.fs(req) response.wait( uv.fs_scandir( response.ec.uv_loop, req, path, 0, # TODO: check if there are meaningful flags for this. uv_callback.fs.cb)) if req.c_result < 0: raise uv_callback.to_error(req.c_result) listing = [] while True: res = uv.fs_scandir_next(req, dirent) if res == uv.EOF: break elif res < 0: raise uv_callback.to_error(res) entry = Exnihilo() entry.setattr(u"path", from_cstring(rffi.charp2str(dirent.c_name))) if dirent.c_type in uv.dirent2name: entry.setattr(u"type", String(uv.dirent2name[dirent.c_type])) else: entry.setattr(u"type", Integer(rffi.r_long(dirent.c_type))) listing.append(entry) return List(listing) finally: uv.fs_req_cleanup(req) lltype.free(dirent, flavor='raw') lltype.free(req, flavor='raw')
def UDP_recv(self): self.check_closed() ec = core.get_ec() if len(self.read_queue) == 0: uv_callback.push(ec.uv__udp_recv, self) status = uv.udp_recv_start(self.udp, _udp_alloc_callback_, _udp_read_callback_once_) if status < 0: uv_callback.drop(ec.uv__udp_recv, self.udp) raise uv_callback.to_error(status) if len(self.read_queue) == 0: if self.read_greenlet is not None: raise unwind(LError(u"async collision")) self.read_greenlet = ec.current core.switch([ec.eventloop]) array, nread, status, addr, flags = self.read_queue.pop(0) if nread < 0: raise uv_callback.to_error(nread) if status < 0: raise uv_callback.to_error(status) if array is None: array = Uint8Slice(lltype.nullptr(rffi.UCHARP.TO), 0, None) elif array.length != nread: array = array.subslice(nread) res = Exnihilo() res.setattr(u"data", array) res.setattr(u"addr", addr) res.setattr(u"flags", Integer(rffi.r_long(flags))) return res
def Stream_read(self): self.check_closed() ec = core.get_ec() if len(self.read_queue) == 0: uv_callback.push(ec.uv__read, self) status = uv.read_start(self.stream, _alloc_callback_, _read_callback_once_) if status < 0: uv_callback.drop(ec.uv__read, self.stream) raise uv_callback.to_error(status) if len(self.read_queue) == 0: if self.read_greenlet is not None: raise unwind(LError(u"async collision")) self.read_greenlet = ec.current core.switch([ec.eventloop]) array, nread, status = self.read_queue.pop(0) if nread < 0: raise uv_callback.to_error(nread) if status < 0: raise uv_callback.to_error(status) if array is None: return Uint8Slice(lltype.nullptr(rffi.UCHARP.TO), 0, None) if array.length == nread: return array return array.subslice(nread)
def ReadStream_read(self): data = alloc_uint8array(64 * 1024) count = File_pread(self.fileobj, data, Integer(self.offset)) self.offset += count.value if count.value == 0: raise uv_callback.to_error(uv.EOF) return data.subslice(count.value)
def UDP_init(): ec = core.get_ec() udp = lltype.malloc(uv.udp_ptr.TO, flavor="raw", zero=True) status = uv.udp_init(ec.uv_loop, udp) if status < 0: lltype.free(udp, flavor="raw") raise uv_callback.to_error(status) return UDP(udp)
def TCP_init(): ec = core.get_ec() tcp = lltype.malloc(uv.tcp_ptr.TO, flavor="raw", zero=True) status = uv.tcp_init(ec.uv_loop, tcp) if status < 0: lltype.free(tcp, flavor="raw") raise uv_callback.to_error(status) return TCP(tcp)
def alloc_pipe(ipc): ec = core.get_ec() pipe = lltype.malloc(uv.pipe_ptr.TO, flavor="raw", zero=True) status = uv.pipe_init(ec.uv_loop, pipe, ipc) if status < 0: lltype.free(pipe, flavor='raw') raise uv_callback.to_error(status) return Pipe(pipe)
def Watch_init(path): ec = core.get_ec() path = pathobj.os_stringify(path).encode('utf-8') handle = lltype.malloc(uv.fs_event_ptr.TO, flavor='raw', zero=True) res = uv.fs_event_init(ec.uv_loop, handle) if res < 0: lltype.free(handle, flavor='raw') raise uv_callback.to_error(res) self = Watch(handle) uv_callback.push(ec.uv__fs_event, self) res = uv.fs_event_start(self.fs_event, _fs_event_cb_, path, 0) if res < 0: uv_callback.drop(ec.uv__fs_event, self.fs_event) Handle_close(self) raise uv_callback.to_error(res) return self
def Watch_wait(self): if len(self.data) > 0: return self.data.pop(0) elif self.status < 0: raise uv_callback.to_error(self.status) else: if self.greenlet is not None: raise unwind(LError(u"fs.watch retrieval collision")) ec = core.get_ec() self.greenlet = ec.current return core.switch([ec.eventloop])
def Stream_listen(self, backlog): self.check_closed() ec = core.get_ec() uv_callback.push(ec.uv__connection, self) status = uv.listen(self.stream, backlog.value, _listen_callback_) if status < 0: uv_callback.drop(ec.uv__connection, self.stream) raise uv_callback.to_error(status) else: self.listening = True return null
def File_fsync(self): req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True) try: response = uv_callback.fs(req) response.wait( uv.fs_fsync(response.ec.uv_loop, req, self.fd, uv_callback.fs.cb)) if req.c_result < 0: raise uv_callback.to_error(req.c_result) return null finally: uv.fs_req_cleanup(req) lltype.free(req, flavor='raw')
def sendfile(out_file, in_file, in_offset, length): req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True) try: response = uv_callback.fs(req) response.wait( uv.fs_sendfile(response.ec.uv_loop, req, out_file.fd, in_file.fd, in_offset.value, length.value, uv_callback.fs.cb)) if req.c_result < 0: raise uv_callback.to_error(req.c_result) return Integer(rffi.r_long(req.c_result)) finally: uv.fs_req_cleanup(req) lltype.free(req, flavor='raw')
def lstat(path): path = pathobj.os_stringify(path).encode('utf-8') req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True) try: response = uv_callback.fs(req) response.wait( uv.fs_lstat(response.ec.uv_loop, req, path, uv_callback.fs.cb)) if req.c_result < 0: raise uv_callback.to_error(req.c_result) return stat2data(req.c_statbuf) finally: uv.fs_req_cleanup(req) lltype.free(req, flavor='raw')
def realpath(path): path = pathobj.os_stringify(path).encode('utf-8') req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True) try: response = uv_callback.fs(req) response.wait( uv.fs_realpath(response.ec.uv_loop, req, path, uv_callback.fs.cb)) if req.c_result < 0: raise uv_callback.to_error(req.c_result) # hmm? return from_cstring(rffi.charp2str(rffi.cast(rffi.CCHARP, req.c_ptr))) finally: uv.fs_req_cleanup(req) lltype.free(req, flavor='raw')
def mkdtemp(path): path = pathobj.os_stringify(path).encode('utf-8') # TODO: XXXXXX the last six characters must be these. req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True) try: response = uv_callback.fs(req) response.wait( uv.fs_mkdtemp(response.ec.uv_loop, req, path, uv_callback.fs.cb)) if req.c_result < 0: raise uv_callback.to_error(req.c_result) return from_cstring(rffi.charp2str(rffi.cast(rffi.CCHARP, req.c_path))) finally: uv.fs_req_cleanup(req) lltype.free(req, flavor='raw')
def chown(path, uid, gid): path = pathobj.os_stringify(path).encode('utf-8') req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True) try: response = uv_callback.fs(req) response.wait( uv.fs_chown(response.ec.uv_loop, req, path, uid.value, gid.value, uv_callback.fs.cb)) if req.c_result < 0: raise uv_callback.to_error(req.c_result) return null finally: uv.fs_req_cleanup(req) lltype.free(req, flavor='raw')
def getaddrinfo(node, service): req = lltype.malloc(uv.getaddrinfo_ptr.TO, flavor='raw', zero=True) if node is None: node_string = lltype.nullptr(rffi.CCHARP.TO) else: node_string = rffi.str2charp(node.string.encode('utf-8')) if service is None: service_string = lltype.nullptr(rffi.CCHARP.TO) else: service_string = rffi.str2charp(service.string.encode('utf-8')) try: response = uv_callback.getaddrinfo(req) status, res = response.wait( uv.getaddrinfo(response.ec.uv_loop, req, uv_callback.getaddrinfo.cb, node_string, service_string, lltype.nullptr(uv.addrinfo_ptr.TO))) if rffi.r_long(status) < 0: raise uv_callback.to_error(status) this = res result = [] while this != lltype.nullptr(uv.addrinfo_ptr.TO): entry = Exnihilo() entry.setattr(u"flags", Integer(rffi.r_long(this.c_ai_flags))) entry.setattr(u"family", Integer(rffi.r_long(this.c_ai_family))) entry.setattr(u"socktype", Integer(rffi.r_long(this.c_ai_socktype))) entry.setattr(u"protocol", Integer(rffi.r_long(this.c_ai_protocol))) entry.setattr( u"addr", copy_to_uint8array(rffi.cast(rffi.VOIDP, this.c_ai_addr), this.c_ai_addrlen, rffi.sizeof(uv.sockaddr_storage))) if this.c_ai_canonname: entry.setattr( u"canonname", from_cstring(rffi.charp2str(this.c_ai_canonname))) else: entry.setattr(u"canonname", null) result.append(entry) this = rffi.cast(uv.addrinfo_ptr, this.c_ai_next) uv.freeaddrinfo(res) return List(result) finally: if node_string: lltype.free(node_string, flavor='raw') if service_string: lltype.free(service_string, flavor='raw') lltype.free(req, flavor='raw')
def File_pwrite(self, data, offset): bufs, nbufs = uv_callback.obj2bufs(data) req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True) try: response = uv_callback.fs(req) response.wait( uv.fs_write(response.ec.uv_loop, req, self.fd, bufs, nbufs, offset.value, uv_callback.fs.cb)) if req.c_result < 0: raise uv_callback.to_error(req.c_result) return Integer(rffi.r_long(req.c_result)) finally: uv.fs_req_cleanup(req) lltype.free(bufs, flavor='raw') lltype.free(req, flavor='raw')
def mkdir(path, mode): mode = 0777 if mode is None else mode.value path = pathobj.os_stringify(path).encode('utf-8') req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True) try: response = uv_callback.fs(req) response.wait( uv.fs_mkdir(response.ec.uv_loop, req, path, mode, uv_callback.fs.cb)) if req.c_result < 0: raise uv_callback.to_error(req.c_result) return null finally: uv.fs_req_cleanup(req) lltype.free(req, flavor='raw')
def open_(path, flags, mode): mode = 0664 if mode is None else mode.value path = pathobj.os_stringify(path).encode('utf-8') req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True) try: response = uv_callback.fs(req) response.wait( uv.fs_open(response.ec.uv_loop, req, path, flags.value, mode, uv_callback.fs.cb)) if req.c_result < 0: raise uv_callback.to_error(req.c_result) return File(rffi.r_long(req.c_result)) finally: uv.fs_req_cleanup(req) lltype.free(req, flavor='raw')
def getnameinfo(addr, flags): flags = 0 if flags is None else flags.value req = lltype.malloc(uv.getnameinfo_ptr.TO, flavor='raw', zero=True) sockaddr = as_sockaddr(addr) try: response = uv_callback.getnameinfo(req) status, hostname, service = response.wait( uv.getnameinfo(response.ec.uv_loop, req, uv_callback.getnameinfo.cb, sockaddr, flags)) if rffi.r_long(status) < 0: raise uv_callback.to_error(status) return List([ from_cstring(rffi.charp2str(hostname)), from_cstring(rffi.charp2str(service)) ]) finally: lltype.free(req, flavor='raw')
def symlink(path, new_path): path = pathobj.os_stringify(path).encode('utf-8') new_path = pathobj.os_stringify(new_path).encode('utf-8') req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True) try: response = uv_callback.fs(req) response.wait( uv.fs_symlink( response.ec.uv_loop, req, path, new_path, 0, # TODO: FS_SYMLINK_DIR, FS_SYMLINK_JUNCTION -flags. uv_callback.fs.cb)) if req.c_result < 0: raise uv_callback.to_error(req.c_result) return null finally: uv.fs_req_cleanup(req) lltype.free(req, flavor='raw')
def check(result): if rffi.r_long(result) < 0: raise uv_callback.to_error(result)