public OpenFile getOpenFileChecked() { checkInitialized(); openFile.checkClosed(); return openFile; }
/** * returns non-nil if input available without blocking, false if EOF or not open/readable, otherwise nil. */ @JRubyMethod(name = "ready?") public static IRubyObject ready(ThreadContext context, IRubyObject _io) { RubyIO io = (RubyIO)_io; Ruby runtime = context.runtime; OpenFile fptr; // ioctl_arg n; fptr = io.getOpenFileChecked(); fptr.checkReadable(context); if (fptr.readPending() != 0) return runtime.getTrue(); // TODO: better effort to get available bytes from our channel // if (!FIONREAD_POSSIBLE_P(fptr->fd)) return Qnil; // if (ioctl(fptr->fd, FIONREAD, &n)) return Qnil; // if (n > 0) return Qtrue; return runtime.newBoolean(fptr.readyNow(context)); }
public void READ_CHECK(ThreadContext context) { if (!READ_DATA_PENDING()) { checkClosed(); } }
static OpenFile of = new OpenFile(); // should be static for accessing within main method public static void main(String[] args) { of.createFile(); intro(); createAndShowRibbon(); createAndShowNormalUI(); }
private static int rb_io_fptr_finalize(Ruby runtime, OpenFile fptr) { if (fptr == null) return 0; fptr.setPath(null);; if (fptr.fd() != null) fptr.cleanup(runtime, true); fptr.write_lock = null; if (fptr.rbuf.ptr != null) { fptr.rbuf.ptr = null; } if (fptr.wbuf.ptr != null) { fptr.wbuf.ptr = null; } fptr.clearCodeConversion(); return 1; }
if (fptr.IS_PREP_STDIO()) { if ((fptr.stdio_file == System.in && !orig.isReadable()) || (fptr.stdio_file == System.out && !orig.isWritable()) || (fptr.stdio_file == System.err && !orig.isWritable())) { throw runtime.newArgumentError(fptr.PREP_STDIO_NAME() + " can't change access mode from \"" + fptr.getModeAsString(runtime) + "\" to \"" + orig.getModeAsString(runtime) + "\""); boolean locked = fptr.lock(); try { if (fptr.isWritable()) { if (fptr.io_fflush(context) < 0) throw runtime.newErrnoFromErrno(fptr.errno(), fptr.getPath()); } else { fptr.tell(context); if (locked) fptr.unlock(); locked = orig.lock(); try { if (orig.isReadable()) { pos = orig.tell(context); if (orig.isWritable()) { if (orig.io_fflush(context) < 0) throw runtime.newErrnoFromErrno(orig.errno(), fptr.getPath()); if (locked) orig.unlock(); locked = fptr.lock(); boolean locked2 = orig.lock(); // TODO: This WILL deadlock if two threads try to reopen the same IOs in opposite directions. Fix?
Stream origStream = origFile.getMainStreamSafe(); ChannelDescriptor origDescriptor = origStream.getDescriptor(); boolean origIsSeekable = origDescriptor.isSeekable(); if (origFile.isReadable() && origIsSeekable) { pos = origStream.fgetpos(); if (origFile.getPipeStream() != null) { origFile.getPipeStream().fflush(); } else if (origFile.isWritable()) { origStream.fflush(); if (selfFile.isWritable()) { selfFile.getWriteStreamSafe().fflush(); selfFile.setMode(origFile.getMode()); selfFile.setProcess(origFile.getProcess()); selfFile.setLineNumber(origFile.getLineNumber()); selfFile.setPath(origFile.getPath()); selfFile.setFinalizer(origFile.getFinalizer()); Stream selfStream = selfFile.getMainStreamSafe(); ChannelDescriptor selfDescriptor = selfStream.getDescriptor(); boolean selfIsSeekable = selfDescriptor.isSeekable(); selfStream.setModes(origStream.getModes()); } else { Stream pipeFile = selfFile.getPipeStream(); selfStream.fclose();
boolean locked1 = orig.lock(); boolean locked2 = fptr.lock(); try { io.flush(context); fptr.setMode(orig.getMode() & ~OpenFile.PREP); fptr.encs = orig.encs; fptr.setProcess(orig.getProcess()); fptr.setLineNumber(orig.getLineNumber()); if (orig.getPath() != null) fptr.setPath(orig.getPath()); fptr.setFinalizer(orig.getFinalizer()); fd = orig.fd().dup(); fptr.setFD(fd); pos = orig.tell(context); if (pos == -1) fptr.seek(context, pos, PosixShim.SEEK_SET); } finally { if (locked2) fptr.unlock(); if (locked1) orig.unlock(); if (fptr.isBinmode()) { dest.setBinmode();
if (originalFile.getPipeStream() != null) { originalFile.getPipeStream().fflush(); originalFile.getMainStreamSafe().lseek(0, Stream.SEEK_CUR); } else if (originalFile.isWritable()) { originalFile.getMainStreamSafe().fflush(); } else { originalFile.getMainStreamSafe().lseek(0, Stream.SEEK_CUR); newFile.setMode(originalFile.getMode()); newFile.setProcess(originalFile.getProcess()); newFile.setLineNumber(originalFile.getLineNumber()); newFile.setPath(originalFile.getPath()); newFile.setFinalizer(originalFile.getFinalizer()); if (newFile.isReadable()) { if (newFile.isWritable()) { if (newFile.getPipeStream() != null) { modes = newIOOptions(runtime, ModeFlags.RDONLY); } else { if (newFile.isWritable()) { modes = newIOOptions(runtime, ModeFlags.WRONLY); } else { modes = newIOOptions(runtime, originalFile.getMainStreamSafe().getModes()); ChannelDescriptor descriptor = originalFile.getMainStreamSafe().getDescriptor().dup(); newFile.setMainStream(ChannelStream.fdopen(runtime, descriptor, modes.getModeFlags()));
if (!fptr.isOpen()) return context.nil; boolean locked = fptr.lock(); try { if (fptr.socketChannel() != null) { try { fptr.socketChannel().socket().shutdownInput(); } catch (IOException ioe) { throw runtime.newErrnoFromErrno(Helpers.errnoFromException(ioe), fptr.getPath()); fptr.setMode(fptr.getMode() & ~OpenFile.READABLE); if (!fptr.isWritable()) return rbIoClose(context); return context.nil; boolean locked2 = wfptr.lock(); try { wfptr.setProcess(fptr.getProcess()); wfptr.setPid(fptr.getPid()); fptr.setProcess(null); fptr.setPid(-1); this.openFile = wfptr; fptr.cleanup(runtime, false); if (locked2) wfptr.unlock(); if (fptr.isWritable() && !fptr.isDuplex()) { throw runtime.newIOError("closing non-duplex IO for reading");
boolean locked = write_fptr.lock(); try { if (write_fptr != null && write_fptr.fd() != null) { write_fptr.cleanup(context.runtime, true); if (locked) write_fptr.unlock(); boolean locked = fptr.lock(); try { if (fptr == null) return context.nil; if (fptr.fd() == null) return context.nil; final Ruby runtime = context.runtime; fptr.finalizeFlush(context, false); fptr.finalizeFlush(context, false); fptr.interruptBlockingThreads(context); try { fptr.unlock(); fptr.waitForBlockingThreads(context); } finally { fptr.lock(); fptr.cleanup(runtime, false); if (fptr.getProcess() != null) { context.setLastExitStatus(context.nil);
@JRubyMethod public RubyFixnum rewind(ThreadContext context) { Ruby runtime = context.runtime; OpenFile fptr; fptr = getOpenFileChecked(); boolean locked = fptr.lock(); try { if (fptr.seek(context, 0L, 0) == -1 && fptr.errno() != null) { throw runtime.newErrnoFromErrno(fptr.errno(), fptr.getPath()); } if (RubyArgsFile.ArgsFileData.getArgsFileData(runtime).isCurrentFile(this)) { runtime.setCurrentLine(runtime.getCurrentLine() - fptr.getLineNumber()); } fptr.setLineNumber(0); if (fptr.readconv != null) { fptr.clearReadConversion(); } } finally { if (locked) fptr.unlock(); } return RubyFixnum.zero(runtime); }
@JRubyMethod(name = "syswrite", required = 1) public IRubyObject syswrite(ThreadContext context, IRubyObject str) { Ruby runtime = context.runtime; OpenFile fptr; long n; if (!(str instanceof RubyString)) str = str.asString(); RubyIO io = GetWriteIO(); fptr = io.getOpenFileChecked(); boolean locked = fptr.lock(); try { fptr.checkWritable(context); str = str.convertToString().dupFrozen(); if (fptr.wbuf.len != 0) { runtime.getWarnings().warn("syswrite for buffered IO"); } ByteList strByteList = ((RubyString) str).getByteList(); n = OpenFile.writeInternal(context, fptr, fptr.fd(), strByteList.unsafeBytes(), strByteList.begin(), strByteList.getRealSize()); if (n == -1) throw runtime.newErrnoFromErrno(fptr.errno(), fptr.getPath()); } finally { if (locked) fptr.unlock(); } return runtime.newFixnum(n); }
if (openFile.isOpen()) { openFile.cleanup(runtime, false); openFile.setMode(ioOptions.getModeFlags().getOpenFileFlags()); openFile.setMainStream(fdopen(descriptor, ioOptions.getModeFlags())); } catch (BadDescriptorException ex) { throw runtime.newErrnoEBADFError();
private void incrementLineno(Ruby runtime, OpenFile myOpenFile) { int lineno = myOpenFile.getLineNumber() + 1; myOpenFile.setLineNumber(lineno); runtime.setCurrentLine(lineno); RubyArgsFile.setCurrentLineNumber(runtime.getArgsFile(), lineno); }
public RubyIO(Ruby runtime, InputStream inputStream) { super(runtime, runtime.getIO()); if (inputStream == null) { throw runtime.newRuntimeError("Opening null stream"); } openFile = new OpenFile(); try { openFile.setMainStream(ChannelStream.open(runtime, new ChannelDescriptor(Channels.newChannel(inputStream)))); } catch (InvalidValueException e) { throw getRuntime().newErrnoEINVALError(); } openFile.setMode(OpenFile.READABLE); }
public RubyIO(Ruby runtime, Channel channel) { super(runtime, runtime.getIO()); // We only want IO objects with valid streams (better to error now). if (channel == null) { throw runtime.newRuntimeError("Opening null channel"); } openFile = new OpenFile(); try { openFile.setMainStream(ChannelStream.open(runtime, new ChannelDescriptor(channel))); } catch (InvalidValueException e) { throw getRuntime().newErrnoEINVALError(); } openFile.setMode(openFile.getMainStream().getModes().getOpenFileFlags()); }
private void setBinmode() { OpenFile fptr; fptr = getOpenFileChecked(); boolean locked = fptr.lock(); try { if (fptr.readconv != null) fptr.readconv.binmode(); if (fptr.writeconv != null) fptr.writeconv.binmode(); fptr.setBinmode(); fptr.clearTextMode(); fptr.writeconvPreEcflags &= ~EConvFlags.NEWLINE_DECORATOR_MASK; if (OpenFlags.O_BINARY.defined()) { // TODO: Windows // if (fptr.readconv == null) { // SET_BINARY_MODE_WITH_SEEK_CUR(fptr); // } // else { // TODO: setmode O_BINARY means what via NIO? // setmode(fptr->fd, O_BINARY); // } } } finally { if (locked) fptr.unlock(); } }
protected IRubyObject ioClose(Ruby runtime) { if (openFile == null) return runtime.getNil(); interruptBlockingThreads(); /* FIXME: Why did we go to this trouble and not use these descriptors? ChannelDescriptor main, pipe; if (openFile.getPipeStream() != null) { pipe = openFile.getPipeStream().getDescriptor(); } else { if (openFile.getMainStream() == null) { return runtime.getNil(); } pipe = null; } main = openFile.getMainStream().getDescriptor(); */ // cleanup, raising errors if any openFile.cleanup(runtime, true); // TODO: notify threads waiting on descriptors/IO? probably not... // If this is not a popen3/popen4 stream and it has a process, attempt to shut down that process if (!popenSpecial && openFile.getProcess() != null) { obliterateProcess(openFile.getProcess()); IRubyObject processResult = RubyProcess.RubyStatus.newProcessStatus(runtime, openFile.getProcess().exitValue(), openFile.getPid()); runtime.getCurrentContext().setLastExitStatus(processResult); } return runtime.getNil(); }