def makeStdIn(): vat = currentVat.get() uv_loop = vat.uv_loop stdinKind = ruv.guess_handle(0) if stdinKind == ruv.HANDLE_TTY: stdin = ruv.alloc_tty(uv_loop, 0, True) return StreamFount(ruv.rffi.cast(ruv.stream_tp, stdin), vat) else: return FileFount(ruv.alloc_fs(), 0, vat)
def run(self, data): if self.closed: raise userError(u"run/1: Couldn't write to closged file") sb = ruv.scopedBufs([data], self) bufs = sb.allocate() fs = ruv.alloc_fs() ruv.stashFS(fs, (self._vat, sb)) ruv.fsWrite(self._vat.uv_loop, fs, self._fd, bufs, 1, -1, writeFileCB)
def makeStdErr(): vat = currentVat.get() uv_loop = vat.uv_loop stderrKind = ruv.guess_handle(2) if stderrKind == ruv.HANDLE_TTY: tty = ruv.alloc_tty(uv_loop, 2, False) # ruv.TTYSetMode(tty, ruv.TTY_MODE_RAW) return StreamDrain(ruv.rffi.cast(ruv.stream_tp, tty), vat) else: return FileDrain(ruv.alloc_fs(), 2, vat)
def rename(self, dest): p, r = makePromise() vat = currentVat.get() uv_loop = vat.uv_loop fs = ruv.alloc_fs() src = self.asBytes() ruv.stashFS(fs, (vat, r)) ruv.fsRename(uv_loop, fs, src, dest, renameCB) return p
def stderr(self): vat = currentVat.get() uv_loop = vat.uv_loop kind = ruv.guess_handle(2) if kind == ruv.HANDLE_TTY: stderr = ruv.alloc_tty(uv_loop, 2, False) stream = ruv.wrapStream(ruv.rffi.cast(ruv.stream_tp, stderr), 1) return StreamSink(stream, vat) else: return FileSink(ruv.alloc_fs(), 2, vat)
def stdin(self): vat = currentVat.get() uv_loop = vat.uv_loop kind = ruv.guess_handle(0) if kind == ruv.HANDLE_TTY: stdin = ruv.alloc_tty(uv_loop, 0, True) stream = ruv.wrapStream(ruv.rffi.cast(ruv.stream_tp, stdin), 1) return StreamSource(stream, vat) else: return FileSource(ruv.alloc_fs(), 0, vat)
def run(self, sink): p, r = makePromise() self._queue.append((r, sink)) with scoped_alloc(ruv.rffi.CArray(ruv.buf_t), 1) as bufs: bufs[0].c_base = self._buf.c_base bufs[0].c_len = self._buf.c_len fs = ruv.alloc_fs() ruv.stashFS(fs, (self._vat, self)) ruv.fsRead(self._vat.uv_loop, fs, self._fd, bufs, 1, -1, readFileCB) return p
def makeStdOut(): vat = currentVat.get() uv_loop = vat.uv_loop stdoutKind = ruv.guess_handle(1) if stdoutKind == ruv.HANDLE_TTY: tty = ruv.alloc_tty(uv_loop, 1, False) # XXX works exactly as expected, including disabling most TTY signal # generation # ruv.TTYSetMode(tty, ruv.TTY_MODE_RAW) return StreamDrain(ruv.rffi.cast(ruv.stream_tp, tty), vat) else: return FileDrain(ruv.alloc_fs(), 1, vat)
def recv(self, atom, args): if atom is GETCONTENTS_0: return self.open(openGetContentsCB, flags=os.O_RDONLY, mode=0000) if atom is SETCONTENTS_1: data = unwrapBytes(args[0]) sibling = self.temporarySibling(".setContents") p, r = makePromise() vat = currentVat.get() uv_loop = vat.uv_loop fs = ruv.alloc_fs() path = sibling.asBytes() # Use CREAT | EXCL to cause a failure if the temporary file # already exists. flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL sc = SetContents(vat, data, r, sibling, self) ruv.stashFS(fs, (vat, sc)) ruv.fsOpen(uv_loop, fs, path, flags, 0777, openSetContentsCB) return p if atom is OPENFOUNT_0: return self.open(openFountCB, flags=os.O_RDONLY, mode=0000) if atom is OPENDRAIN_0: # Create the file if it doesn't yet exist, and truncate it if it # does. Trust the umask to be reasonable for now. flags = os.O_CREAT | os.O_WRONLY # XXX this behavior should be configurable via namedarg? flags |= os.O_TRUNC return self.open(openDrainCB, flags=flags, mode=0777) if atom is RENAME_1: fr = args[0] if not isinstance(fr, FileResource): raise userError(u"rename/1: Must be file resource") return self.rename(fr.asBytes()) if atom is SIBLING_1: name = unwrapStr(args[0]) if u'/' in name: raise userError(u"sibling/1: Illegal file name '%s'" % name) return self.sibling(name.encode("utf-8")) if atom is TEMPORARYSIBLING_0: return self.temporarySibling(".new") raise Refused(self, atom, args)
def open(self, callback, flags=None, mode=None): # Always call this as .open(callback, flags=..., mode=...) assert flags is not None assert mode is not None p, r = makePromise() vat = currentVat.get() uv_loop = vat.uv_loop fs = ruv.alloc_fs() path = self.asBytes() log.log(["fs"], u"makeFileResource: Opening file '%s'" % path.decode("utf-8")) ruv.stashFS(fs, (vat, r)) ruv.fsOpen(uv_loop, fs, path, flags, mode, callback) return p
def setContents(self, data): sibling = self.temporarySibling(".setContents") p, r = makePromise() vat = currentVat.get() uv_loop = vat.uv_loop fs = ruv.alloc_fs() path = sibling.asBytes() # Use CREAT | EXCL to cause a failure if the temporary file # already exists. flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL sc = SetContents(vat, data, r, sibling, self) ruv.stashFS(fs, (vat, sc)) ruv.fsOpen(uv_loop, fs, path, flags, 0777, openSetContentsCB) return p
def _cleanup(self): fs = ruv.alloc_fs() ruv.stashFS(fs, (self._vat, self)) ruv.fsClose(self._vat.uv_loop, fs, self._fd, ruv.fsUnstashAndDiscard) self.closed = True
def _cleanup(self): uv_loop = self._vat.uv_loop fs = ruv.alloc_fs() ruv.stashFS(fs, (self._vat, self)) ruv.fsClose(uv_loop, fs, self._fd, ruv.fsUnstashAndDiscard) ruv.freeBuf(self._buf)