@Override public Channel openChannel(int flags, int perm) throws IOException { return createChannel(ModeFlags.createModeFlags(flags)); }
public ModeFlags(String flagString) throws InvalidValueException { this.flags = getOFlagsFromString(flagString); }
/** * Produce a Java IO mode string from the flags in this object. * * @return A Java string suitable for opening files with RandomAccessFile */ public String toJavaModeString() { // Do not open as 'rw' by default since a file with read-only permissions will fail on 'rw' if (isWritable() || isCreate() || isTruncate()) { // Java requires "w" for creating a file that does not exist return "rw"; } else { return "r"; } }
/** * Check whether the target set of flags is a superset of this one; used to * ensure that a file is not re-opened with more privileges than it already * had. * * @param superset The ModeFlags object which should be a superset of this one * @return true if the object is a superset, false otherwise */ public boolean isSubsetOf(ModeFlags superset) { // TODO: Make sure all appropriate open flags are added to this check. if ((!superset.isReadable() && isReadable()) || (!superset.isWritable() && isWritable()) || !superset.isAppendable() && isAppendable()) { return false; } return true; }
@Override public String toString() { StringBuilder buf = new StringBuilder("ModeFlags(" + flags + "): "); if (isAppendable()) buf.append("APPENDABLE "); if (isBinary()) buf.append("BINARY "); if (isCreate()) buf.append("CREATE "); if (isExclusive()) buf.append("EXCLUSIVE "); if (isReadOnly()) buf.append("READONLY "); if (isText()) buf.append("TEXT "); if (isTruncate()) buf.append("TRUNCATE "); if (isWritable()) { if (isReadable()) { buf.append("RDWR"); } else { buf.append("WRITABLE "); } } return buf.toString(); }
JRubyFile theFile = JRubyFile.create(cwd,path); if (theFile.isDirectory() && modes.isWritable()) { throw runtime.newErrnoEISDirError(path); if (modes.isCreate()) { if (theFile.exists() && modes.isExclusive()) { throw runtime.newErrnoEEXISTError("File exists - " + path); RandomAccessFile file = new RandomAccessFile(theFile, modes.toJavaModeString()); if (modes.isTruncate()) file.setLength(0L); if (modes.isAppendable()) lseek(0, SEEK_END); } catch (PipeException pe) {
@Override public Channel openChannel(final int flags, int perm) throws IOException { final ModeFlags modeFlags = ModeFlags.createModeFlags(flags); if (posix.isNative() && !Platform.IS_WINDOWS) { int fd = posix.open(absolutePath(), modeFlags.getFlags(), perm); if (fd < 0) throwFromErrno(posix.errno()); posix.fcntlInt(fd, Fcntl.F_SETFD, posix.fcntl(fd, Fcntl.F_GETFD) | FcntlLibrary.FD_CLOEXEC); if (modeFlags.isCreate()) { boolean fileCreated; try { if (!fileCreated && modeFlags.isExclusive()) { throw new ResourceException.FileExists(absolutePath());
EncodingUtils.extractModeEncoding(context, this, pm, opt, oflags_p, fmode_p); oflags_p[0] = descriptor.getOriginalModes().getFlags(); ofmode = ModeFlags.getOpenFileFlagsFor(oflags_p[0]); if (pm[EncodingUtils.VMODE] == null || pm[EncodingUtils.VMODE].isNil()) { fmode_p[0] = ofmode; ModeFlags modes = ModeFlags.createModeFlags(oflags_p[0]);
private static Stream maybeWrapWithLineEndingWrapper(Stream stream, ModeFlags modes) { if (modes.isText() || // FIXME: Remove this one textmode is part of transcoding. (Platform.IS_WINDOWS && stream.getDescriptor().getChannel() instanceof SeekableByteChannel && !modes.isBinary())) { return new CRLFStreamWrapper(stream); } return stream; }
private void initializeOpen(IOOptions ioOptions) { getRuntime().getPosix().chmod(path, 0600); MakeOpenFile(); openFile.setMode(ioOptions.getModeFlags().getOpenFileFlags()); openFile.setPath(path); sysopenInternal19(path, ioOptions.getModeFlags().getOpenFileFlags(), 0600); }
/** * Convert the flags in this object to a set of flags appropriate for the * OpenFile structure and logic therein. * * @return an int of flags appropriate for OpenFile */ public int getOpenFileFlags() { return getOpenFileFlagsFor(flags); } /**
protected void openInternal(String path, String modeString) { if (path.startsWith("jar:")) { path = path.substring(4); } MakeOpenFile(); IOOptions modes = newIOOptions(getRuntime(), modeString); openFile.setMode(modes.getModeFlags().getOpenFileFlags()); if (getRuntime().is1_9() && modes.getModeFlags().isBinary()) enc = ASCIIEncoding.INSTANCE; openFile.setPath(path); openFile.setMainStream(fopen(path, modes.getModeFlags())); }
/** * Write the bytes in the specified byte list to the associated channel. * * @param buffer the byte list containing the bytes to be written * @return the number of bytes actually written * @throws java.io.IOException if there is an exception during IO * @throws org.jruby.util.io.BadDescriptorException if the associated * channel is already closed */ public int internalWrite(ByteBuffer buffer) throws IOException, BadDescriptorException { checkOpen(); // TODO: It would be nice to throw a better error for this if (!isWritable()) { throw new BadDescriptorException(); } WritableByteChannel writeChannel = (WritableByteChannel)channel; // if appendable, we always seek to the end before writing if (isSeekable() && originalModes.isAppendable()) { // if already in append mode, we don't do our own seeking if (!isInAppendMode) { FileChannel fileChannel = (FileChannel)channel; fileChannel.position(fileChannel.size()); } } return writeChannel.write(buffer); }
oflags_p[0] = ModeFlags.oflagsFrom(runtime.getPosix(), fd.ch); ofmode = ModeFlags.getOpenFileFlagsFor(oflags_p[0]); if (EncodingUtils.vmode(pm) == null || EncodingUtils.vmode(pm).isNil()) { fmode_p[0] = ofmode;
/** * @deprecated use {@link #openChannel(int, int)} instead * * @param flags * @param perm * @return channel * @throws ResourceException */ default Channel openChannel(ModeFlags flags, int perm) throws ResourceException { try { return openChannel(flags.getFlags(), perm); } catch (ResourceException ex) { throw ex; } catch (IOException ex) { throw new ResourceException.IOError(ex); } }
@Override public String toString() { StringBuilder buf = new StringBuilder("ModeFlags(" + flags + "): "); if (isAppendable()) buf.append("APPENDABLE "); if (isBinary()) buf.append("BINARY "); if (isCreate()) buf.append("CREATE "); if (isExclusive()) buf.append("EXCLUSIVE "); if (isReadOnly()) buf.append("READONLY "); if (isText()) buf.append("TEXT "); if (isTruncate()) buf.append("TRUNCATE "); if (isWritable()) { if (isReadable()) { buf.append("RDWR"); } else { buf.append("WRITABLE "); } } return buf.toString(); }
JRubyFile theFile = JRubyFile.create(cwd,path); if (theFile.isDirectory() && modes.isWritable()) { throw runtime.newErrnoEISDirError(path); if (modes.isCreate()) { if (theFile.exists() && modes.isExclusive()) { throw runtime.newErrnoEEXISTError("File exists - " + path); RandomAccessFile file = new RandomAccessFile(theFile, modes.toJavaModeString()); if (modes.isTruncate()) file.setLength(0L); if (modes.isAppendable()) lseek(0, SEEK_END); } catch (PipeException pe) {
/** * Produce a Java IO mode string from the flags in this object. * * @return A Java string suitable for opening files with RandomAccessFile */ public String toJavaModeString() { // Do not open as 'rw' by default since a file with read-only permissions will fail on 'rw' if (isWritable() || isCreate() || isTruncate()) { // Java requires "w" for creating a file that does not exist return "rw"; } else { return "r"; } }