private static void checkSharedExclusive(Ruby runtime, OpenFile openFile, int lockMode) { // This logic used to attempt a shared lock instead of an exclusive // lock, because LOCK_EX on some systems (as reported in JRUBY-1214) // allow exclusively locking a read-only file. However, the JDK // APIs do not allow acquiring an exclusive lock on files that are // not open for read, and there are other platforms (such as Solaris, // see JRUBY-5627) that refuse at an *OS* level to exclusively lock // files opened only for read. As a result, this behavior is platform- // dependent, and so we will obey the JDK's policy of disallowing // exclusive locks on files opened only for read. if (!openFile.isWritable() && (lockMode & LOCK_EX) > 0) { throw runtime.newErrnoEBADFError("cannot acquire exclusive lock on File not opened for write"); } // Likewise, JDK does not allow acquiring a shared lock on files // that have not been opened for read. We comply here. if (!openFile.isReadable() && (lockMode & LOCK_SH) > 0) { throw runtime.newErrnoEBADFError("cannot acquire shared lock on File not opened for read"); } }
private static void checkSharedExclusive(Ruby runtime, OpenFile openFile, int lockMode) { // This logic used to attempt a shared lock instead of an exclusive // lock, because LOCK_EX on some systems (as reported in JRUBY-1214) // allow exclusively locking a read-only file. However, the JDK // APIs do not allow acquiring an exclusive lock on files that are // not open for read, and there are other platforms (such as Solaris, // see JRUBY-5627) that refuse at an *OS* level to exclusively lock // files opened only for read. As a result, this behavior is platform- // dependent, and so we will obey the JDK's policy of disallowing // exclusive locks on files opened only for read. if (!openFile.isWritable() && (lockMode & LOCK_EX) > 0) { throw runtime.newErrnoEBADFError("cannot acquire exclusive lock on File not opened for write"); } // Likewise, JDK does not allow acquiring a shared lock on files // that have not been opened for read. We comply here. if (!openFile.isReadable() && (lockMode & LOCK_SH) > 0) { throw runtime.newErrnoEBADFError("cannot acquire shared lock on File not opened for read"); } }
private void trySelectWrite(ThreadContext context, Map<Character,Integer> attachment, OpenFile fptr) throws IOException { if (fptr.selectChannel() == null || false == registerSelect(getSelector(context, fptr.selectChannel()), attachment, fptr.selectChannel(), WRITE_CONNECT_OPS)) { selectedReads++; if (fptr.isWritable()) { getUnselectableWrites()[attachment.get('w')] = true; } } }
private void trySelectWrite(ThreadContext context, Map<Character,Integer> attachment, OpenFile fptr) throws IOException { if (fptr.selectChannel() == null || false == registerSelect(getSelector(context, fptr.selectChannel()), attachment, fptr.selectChannel(), WRITE_CONNECT_OPS)) { selectedReads++; if (fptr.isWritable()) { getUnselectableWrites()[attachment.get('w')] = true; } } }
@JRubyMethod public IRubyObject external_encoding(ThreadContext context) { EncodingService encodingService = context.runtime.getEncodingService(); if (openFile.encs.enc2 != null) return encodingService.getEncoding(openFile.encs.enc2); if (openFile.isWritable()) { return openFile.encs.enc == null ? context.nil : encodingService.getEncoding(openFile.encs.enc); } return encodingService.getEncoding(getReadEncoding()); }
@JRubyMethod public IRubyObject external_encoding(ThreadContext context) { EncodingService encodingService = context.runtime.getEncodingService(); if (openFile.encs.enc2 != null) return encodingService.getEncoding(openFile.encs.enc2); if (openFile.isWritable()) { return openFile.encs.enc == null ? context.nil : encodingService.getEncoding(openFile.encs.enc); } return encodingService.getEncoding(getReadEncoding()); }
@JRubyMethod(compat = RUBY1_9) public IRubyObject external_encoding(ThreadContext context) { EncodingService encodingService = context.runtime.getEncodingService(); if (enc2 != null) return encodingService.getEncoding(enc2); if (openFile.isWritable()) { return enc == null ? context.runtime.getNil() : encodingService.getEncoding(enc); } return encodingService.getEncoding(getReadEncoding()); }
@JRubyMethod(compat = RUBY1_9) public IRubyObject external_encoding(ThreadContext context) { EncodingService encodingService = context.runtime.getEncodingService(); if (enc2 != null) return encodingService.getEncoding(enc2); if (openFile.isWritable()) { return enc == null ? context.runtime.getNil() : encodingService.getEncoding(enc); } return encodingService.getEncoding(getReadEncoding()); }
@Override @JRubyMethod public IRubyObject close_read(ThreadContext context) { Ruby runtime = context.runtime; if (!openFile.isOpen()) { throw context.runtime.newIOError("not opened for reading"); } if (!openFile.isWritable()) { close(); } else { // shutdown read try { shutdownInternal(context, 0); } catch (BadDescriptorException e) { throw runtime.newErrnoEBADFError(); } } return context.nil; }
@Override @JRubyMethod public IRubyObject close_read(ThreadContext context) { Ruby runtime = context.runtime; if (!openFile.isOpen()) { throw context.runtime.newIOError("not opened for reading"); } if (!openFile.isWritable()) { close(); } else { // shutdown read try { shutdownInternal(context, 0); } catch (BadDescriptorException e) { throw runtime.newErrnoEBADFError(); } } return context.nil; }
@Override @JRubyMethod public IRubyObject close_write(ThreadContext context) { Ruby runtime = context.runtime; if (!openFile.isWritable()) { return runtime.getNil(); } if (openFile.getPipeStream() == null && openFile.isReadable()) { throw runtime.newIOError("closing non-duplex IO for writing"); } if (!openFile.isReadable()) { close(); } else { // shutdown write try { shutdownInternal(context, 1); } catch (BadDescriptorException e) { throw runtime.newErrnoEBADFError(); } } return context.nil; }
@Override @JRubyMethod public IRubyObject close_write(ThreadContext context) { Ruby runtime = context.runtime; if (!openFile.isWritable()) { return runtime.getNil(); } if (openFile.getPipeStream() == null && openFile.isReadable()) { throw runtime.newIOError("closing non-duplex IO for writing"); } if (!openFile.isReadable()) { close(); } else { // shutdown write try { shutdownInternal(context, 1); } catch (BadDescriptorException e) { throw runtime.newErrnoEBADFError(); } } return context.nil; }
@JRubyMethod(required = 1) public IRubyObject truncate(ThreadContext context, IRubyObject len) { Ruby runtime = context.runtime; OpenFile fptr; long pos; pos = RubyNumeric.num2int(len); fptr = getOpenFileChecked(); if (!fptr.isWritable()) { throw runtime.newIOError("not opened for writing"); } flushRaw(context, false); if (pos < 0) { throw runtime.newErrnoEINVALError(openFile.getPath()); } if (fptr.posix.ftruncate(fptr.fd(), pos) < 0) { throw runtime.newErrnoFromErrno(fptr.posix.errno, fptr.getPath()); } return RubyFixnum.zero(runtime); }
@JRubyMethod(required = 1) public IRubyObject truncate(ThreadContext context, IRubyObject len) { Ruby runtime = context.runtime; OpenFile fptr; long pos; pos = RubyNumeric.num2int(len); fptr = getOpenFileChecked(); if (!fptr.isWritable()) { throw runtime.newIOError("not opened for writing"); } flushRaw(context, false); if (pos < 0) { throw runtime.newErrnoEINVALError(openFile.getPath()); } if (fptr.posix.ftruncate(fptr.fd(), pos) < 0) { throw runtime.newErrnoFromErrno(fptr.posix.errno, fptr.getPath()); } return RubyFixnum.zero(runtime); }
@JRubyMethod(name = "close_read") public IRubyObject close_read(ThreadContext context) { Ruby runtime = context.runtime; try { OpenFile myOpenFile = getOpenFileChecked(); if (myOpenFile.getPipeStream() == null && myOpenFile.isWritable()) { throw runtime.newIOError("closing non-duplex IO for reading"); } if (myOpenFile.getPipeStream() == null) { close(); } else{ myOpenFile.getMainStreamSafe().fclose(); myOpenFile.setMode(myOpenFile.getMode() & ~OpenFile.READABLE); myOpenFile.setMainStream(myOpenFile.getPipeStream()); myOpenFile.setPipeStream(null); // TODO // n is result of fclose; but perhaps having a SysError below is enough? // if (n != 0) rb_sys_fail(fptr->path); } } catch (BadDescriptorException bde) { throw runtime.newErrnoEBADFError(); } catch (IOException ioe) { // I believe Ruby bails out with a "bug" if closing fails throw runtime.newIOErrorFromException(ioe); } return this; }
if (openFile.isWritable() && process.hasOutput()) { Channel outChannel; if (process.getOutput() != null) {
@JRubyMethod(name = "close_read") public IRubyObject close_read(ThreadContext context) { Ruby runtime = context.runtime; try { OpenFile myOpenFile = getOpenFileChecked(); if (myOpenFile.getPipeStream() == null && myOpenFile.isWritable()) { throw runtime.newIOError("closing non-duplex IO for reading"); } if (myOpenFile.getPipeStream() == null) { close(); } else{ myOpenFile.getMainStreamSafe().fclose(); myOpenFile.setMode(myOpenFile.getMode() & ~OpenFile.READABLE); myOpenFile.setMainStream(myOpenFile.getPipeStream()); myOpenFile.setPipeStream(null); // TODO // n is result of fclose; but perhaps having a SysError below is enough? // if (n != 0) rb_sys_fail(fptr->path); } } catch (BadDescriptorException bde) { throw runtime.newErrnoEBADFError(); } catch (IOException ioe) { // I believe Ruby bails out with a "bug" if closing fails throw runtime.newIOErrorFromException(ioe); } return this; }
throw runtime.newIOError("sysseek for buffered IO"); if (fptr.isWritable() && fptr.wbuf.len != 0) { runtime.getWarnings().warn("sysseek for buffered IO");
throw context.runtime.newIOError("sysseek for buffered IO"); if (myOpenFile.isWritable() && myOpenFile.isWriteBuffered()) { context.runtime.getWarnings().warn(ID.SYSSEEK_BUFFERED_IO, "sysseek for buffered IO");
throw context.runtime.newIOError("sysseek for buffered IO"); if (myOpenFile.isWritable() && myOpenFile.isWriteBuffered()) { context.runtime.getWarnings().warn(ID.SYSSEEK_BUFFERED_IO, "sysseek for buffered IO");