diff --git a/jdk/make/mapfiles/libjava/mapfile-vers b/jdk/make/mapfiles/libjava/mapfile-vers index b14ae84c1a9..08466af08f3 100644 --- a/jdk/make/mapfiles/libjava/mapfile-vers +++ b/jdk/make/mapfiles/libjava/mapfile-vers @@ -75,6 +75,7 @@ SUNWprivate_1.1 { Java_java_io_FileDescriptor_initIDs; Java_java_io_FileDescriptor_sync; + Java_java_io_FileDescriptor_getAppend; Java_java_io_FileInputStream_available; Java_java_io_FileInputStream_close0; Java_java_io_FileInputStream_initIDs; diff --git a/jdk/src/java.base/share/classes/java/io/File.java b/jdk/src/java.base/share/classes/java/io/File.java index 306cc84097d..5aa6b200163 100644 --- a/jdk/src/java.base/share/classes/java/io/File.java +++ b/jdk/src/java.base/share/classes/java/io/File.java @@ -1588,7 +1588,7 @@ public class File /** * A convenience method to set the owner's read permission for this abstract * pathname. On some platforms it may be possible to start the Java virtual - * machine with special privileges that allow it to read files that that are + * machine with special privileges that allow it to read files that are * marked as unreadable. * *

An invocation of this method of the form file.setReadable(arg) diff --git a/jdk/src/java.base/share/classes/java/io/FileOutputStream.java b/jdk/src/java.base/share/classes/java/io/FileOutputStream.java index 281a695e6df..43a7d053bbb 100644 --- a/jdk/src/java.base/share/classes/java/io/FileOutputStream.java +++ b/jdk/src/java.base/share/classes/java/io/FileOutputStream.java @@ -26,6 +26,8 @@ package java.io; import java.nio.channels.FileChannel; +import sun.misc.SharedSecrets; +import sun.misc.JavaIOFileDescriptorAccess; import sun.nio.ch.FileChannelImpl; @@ -52,16 +54,17 @@ import sun.nio.ch.FileChannelImpl; public class FileOutputStream extends OutputStream { + /** + * Access to FileDescriptor internals. + */ + private static final JavaIOFileDescriptorAccess fdAccess = + SharedSecrets.getJavaIOFileDescriptorAccess(); + /** * The system dependent file descriptor. */ private final FileDescriptor fd; - /** - * True if the file is opened for append. - */ - private final boolean append; - /** * The associated channel, initialized lazily. */ @@ -207,7 +210,6 @@ class FileOutputStream extends OutputStream } this.fd = new FileDescriptor(); fd.attach(this); - this.append = append; this.path = name; open(name, append); @@ -245,7 +247,6 @@ class FileOutputStream extends OutputStream security.checkWrite(fdObj); } this.fd = fdObj; - this.append = false; this.path = null; fd.attach(this); @@ -287,7 +288,7 @@ class FileOutputStream extends OutputStream * @exception IOException if an I/O error occurs. */ public void write(int b) throws IOException { - write(b, append); + write(b, fdAccess.getAppend(fd)); } /** @@ -310,7 +311,7 @@ class FileOutputStream extends OutputStream * @exception IOException if an I/O error occurs. */ public void write(byte b[]) throws IOException { - writeBytes(b, 0, b.length, append); + writeBytes(b, 0, b.length, fdAccess.getAppend(fd)); } /** @@ -323,7 +324,7 @@ class FileOutputStream extends OutputStream * @exception IOException if an I/O error occurs. */ public void write(byte b[], int off, int len) throws IOException { - writeBytes(b, off, len, append); + writeBytes(b, off, len, fdAccess.getAppend(fd)); } /** @@ -395,7 +396,7 @@ class FileOutputStream extends OutputStream public FileChannel getChannel() { synchronized (this) { if (channel == null) { - channel = FileChannelImpl.open(fd, path, false, true, append, this); + channel = FileChannelImpl.open(fd, path, false, true, this); } return channel; } diff --git a/jdk/src/java.base/share/classes/java/nio/file/FileStore.java b/jdk/src/java.base/share/classes/java/nio/file/FileStore.java index 008f6f722c1..ad94cc90040 100644 --- a/jdk/src/java.base/share/classes/java/nio/file/FileStore.java +++ b/jdk/src/java.base/share/classes/java/nio/file/FileStore.java @@ -208,7 +208,7 @@ public abstract class FileStore { * @param attribute * the attribute to read - * @return the attribute value; {@code null} may be a valid for some + * @return the attribute value; {@code null} may be valid for some * attributes * * @throws UnsupportedOperationException diff --git a/jdk/src/java.base/share/classes/java/nio/file/attribute/package-info.java b/jdk/src/java.base/share/classes/java/nio/file/attribute/package-info.java index c1bf6b6482b..be46551c60d 100644 --- a/jdk/src/java.base/share/classes/java/nio/file/attribute/package-info.java +++ b/jdk/src/java.base/share/classes/java/nio/file/attribute/package-info.java @@ -51,7 +51,7 @@ *

An attribute view provides a read-only or updatable view of the non-opaque * values, or metadata, associated with objects in a file system. * The {@link java.nio.file.attribute.FileAttributeView} interface is - * extended by several other interfaces that views to specific sets of file + * extended by several other interfaces that provide views to specific sets of file * attributes. {@code FileAttributeViews} are selected by invoking the {@link * java.nio.file.Files#getFileAttributeView} method with a * type-token to identify the required view. Views can also be identified diff --git a/jdk/src/java.base/share/classes/sun/misc/JavaIOFileDescriptorAccess.java b/jdk/src/java.base/share/classes/sun/misc/JavaIOFileDescriptorAccess.java index 9e987e6c371..478c4a8c8cc 100644 --- a/jdk/src/java.base/share/classes/sun/misc/JavaIOFileDescriptorAccess.java +++ b/jdk/src/java.base/share/classes/sun/misc/JavaIOFileDescriptorAccess.java @@ -33,6 +33,8 @@ import java.io.FileDescriptor; public interface JavaIOFileDescriptorAccess { public void set(FileDescriptor obj, int fd); public int get(FileDescriptor fd); + public void setAppend(FileDescriptor obj, boolean append); + public boolean getAppend(FileDescriptor obj); // Only valid on Windows public void setHandle(FileDescriptor obj, long handle); diff --git a/jdk/src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java b/jdk/src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java index bb127e8a5f2..2c3102feadd 100644 --- a/jdk/src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java +++ b/jdk/src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java @@ -44,6 +44,8 @@ import java.util.ArrayList; import java.util.List; import sun.misc.Cleaner; +import sun.misc.JavaIOFileDescriptorAccess; +import sun.misc.SharedSecrets; import sun.security.action.GetPropertyAction; public class FileChannelImpl @@ -52,6 +54,10 @@ public class FileChannelImpl // Memory allocation size for mapping buffers private static final long allocationGranularity; + // Access to FileDispatcher internals + private static final JavaIOFileDescriptorAccess fdAccess = + SharedSecrets.getJavaIOFileDescriptorAccess(); + // Used to make native read and write calls private final FileDispatcher nd; @@ -61,7 +67,6 @@ public class FileChannelImpl // File access mode (immutable) private final boolean writable; private final boolean readable; - private final boolean append; // Required to prevent finalization of creating stream (immutable) private final Object parent; @@ -77,31 +82,23 @@ public class FileChannelImpl private final Object positionLock = new Object(); private FileChannelImpl(FileDescriptor fd, String path, boolean readable, - boolean writable, boolean append, Object parent) + boolean writable, Object parent) { this.fd = fd; this.readable = readable; this.writable = writable; - this.append = append; this.parent = parent; this.path = path; - this.nd = new FileDispatcherImpl(append); + this.nd = new FileDispatcherImpl(); } - // Used by FileInputStream.getChannel() and RandomAccessFile.getChannel() + // Used by FileInputStream.getChannel(), FileOutputStream.getChannel + // and RandomAccessFile.getChannel() public static FileChannel open(FileDescriptor fd, String path, boolean readable, boolean writable, Object parent) { - return new FileChannelImpl(fd, path, readable, writable, false, parent); - } - - // Used by FileOutputStream.getChannel - public static FileChannel open(FileDescriptor fd, String path, - boolean readable, boolean writable, - boolean append, Object parent) - { - return new FileChannelImpl(fd, path, readable, writable, append, parent); + return new FileChannelImpl(fd, path, readable, writable, parent); } private void ensureOpen() throws IOException { @@ -109,7 +106,6 @@ public class FileChannelImpl throw new ClosedChannelException(); } - // -- Standard channel operations -- protected void implCloseChannel() throws IOException { @@ -258,6 +254,7 @@ public class FileChannelImpl ti = threads.add(); if (!isOpen()) return 0; + boolean append = fdAccess.getAppend(fd); do { // in append-mode then position is advanced to end before writing p = (append) ? nd.size(fd) : position0(fd, -1); @@ -284,7 +281,7 @@ public class FileChannelImpl if (!isOpen()) return null; do { - p = position0(fd, newPosition); + p = position0(fd, newPosition); } while ((p == IOStatus.INTERRUPTED) && isOpen()); return this; } finally { diff --git a/jdk/src/java.base/share/native/libjava/io_util.h b/jdk/src/java.base/share/native/libjava/io_util.h index 120594fe804..1d7920512ba 100644 --- a/jdk/src/java.base/share/native/libjava/io_util.h +++ b/jdk/src/java.base/share/native/libjava/io_util.h @@ -28,6 +28,7 @@ extern jfieldID IO_fd_fdID; extern jfieldID IO_handle_fdID; +extern jfieldID IO_append_fdID; #ifdef _ALLBSD_SOURCE #include diff --git a/jdk/src/java.base/unix/classes/java/io/FileDescriptor.java b/jdk/src/java.base/unix/classes/java/io/FileDescriptor.java index 4017b3a2a9a..35518e4493d 100644 --- a/jdk/src/java.base/unix/classes/java/io/FileDescriptor.java +++ b/jdk/src/java.base/unix/classes/java/io/FileDescriptor.java @@ -51,16 +51,22 @@ public final class FileDescriptor { private List otherParents; private boolean closed; + /** + * true, if file is opened for appending. + */ + private boolean append; + /** * Constructs an (invalid) FileDescriptor * object. */ - public /**/ FileDescriptor() { + public FileDescriptor() { fd = -1; } - private /* */ FileDescriptor(int fd) { + private FileDescriptor(int fd) { this.fd = fd; + this.append = getAppend(fd); } /** @@ -149,6 +155,14 @@ public final class FileDescriptor { return obj.fd; } + public void setAppend(FileDescriptor obj, boolean append) { + obj.append = append; + } + + public boolean getAppend(FileDescriptor obj) { + return obj.append; + } + public void setHandle(FileDescriptor obj, long handle) { throw new UnsupportedOperationException(); } @@ -160,6 +174,11 @@ public final class FileDescriptor { ); } + /** + * Returns true, if the file was opened for appending. + */ + private static native boolean getAppend(int fd); + /* * Package private methods to track referents. * If multiple streams point to the same FileDescriptor, we cycle diff --git a/jdk/src/java.base/unix/classes/sun/nio/ch/FileDispatcherImpl.java b/jdk/src/java.base/unix/classes/sun/nio/ch/FileDispatcherImpl.java index 2c7504caa4a..c6c85f7fac9 100644 --- a/jdk/src/java.base/unix/classes/sun/nio/ch/FileDispatcherImpl.java +++ b/jdk/src/java.base/unix/classes/sun/nio/ch/FileDispatcherImpl.java @@ -35,10 +35,6 @@ class FileDispatcherImpl extends FileDispatcher init(); } - FileDispatcherImpl(boolean append) { - /* append is ignored */ - } - FileDispatcherImpl() { } diff --git a/jdk/src/java.base/unix/classes/sun/nio/fs/UnixChannelFactory.java b/jdk/src/java.base/unix/classes/sun/nio/fs/UnixChannelFactory.java index c6f074e5941..bc194f960ae 100644 --- a/jdk/src/java.base/unix/classes/sun/nio/fs/UnixChannelFactory.java +++ b/jdk/src/java.base/unix/classes/sun/nio/fs/UnixChannelFactory.java @@ -134,7 +134,7 @@ class UnixChannelFactory { throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed"); FileDescriptor fdObj = open(dfd, path, pathForPermissionCheck, flags, mode); - return FileChannelImpl.open(fdObj, path.toString(), flags.read, flags.write, flags.append, null); + return FileChannelImpl.open(fdObj, path.toString(), flags.read, flags.write, null); } /** @@ -288,6 +288,7 @@ class UnixChannelFactory { // create java.io.FileDescriptor FileDescriptor fdObj = new FileDescriptor(); fdAccess.set(fdObj, fd); + fdAccess.setAppend(fdObj, flags.append); return fdObj; } } diff --git a/jdk/src/java.base/unix/native/libjava/FileDescriptor_md.c b/jdk/src/java.base/unix/native/libjava/FileDescriptor_md.c index 7147a14c544..11b96bb6f90 100644 --- a/jdk/src/java.base/unix/native/libjava/FileDescriptor_md.c +++ b/jdk/src/java.base/unix/native/libjava/FileDescriptor_md.c @@ -23,6 +23,9 @@ * questions. */ +#include +#include + #include "jvm.h" #include "io_util_md.h" @@ -35,6 +38,9 @@ /* field id for jint 'fd' in java.io.FileDescriptor */ jfieldID IO_fd_fdID; +/* field id for jboolean 'append' in java.io.FileDescriptor */ +jfieldID IO_append_fdID; + /************************************************************** * static methods to store field ID's in initializers */ @@ -42,6 +48,7 @@ jfieldID IO_fd_fdID; JNIEXPORT void JNICALL Java_java_io_FileDescriptor_initIDs(JNIEnv *env, jclass fdClass) { IO_fd_fdID = (*env)->GetFieldID(env, fdClass, "fd", "I"); + IO_append_fdID = (*env)->GetFieldID(env, fdClass, "append", "Z"); } /************************************************************** @@ -55,3 +62,9 @@ Java_java_io_FileDescriptor_sync(JNIEnv *env, jobject this) { JNU_ThrowByName(env, "java/io/SyncFailedException", "sync failed"); } } + +JNIEXPORT jboolean JNICALL +Java_java_io_FileDescriptor_getAppend(JNIEnv *env, jclass fdClass, jint fd) { + int flags = fcntl(fd, F_GETFL); + return ((flags & O_APPEND) == 0) ? JNI_FALSE : JNI_TRUE; +} diff --git a/jdk/src/java.base/unix/native/libjava/io_util_md.c b/jdk/src/java.base/unix/native/libjava/io_util_md.c index 4c539ecdafb..ff7b9b83d46 100644 --- a/jdk/src/java.base/unix/native/libjava/io_util_md.c +++ b/jdk/src/java.base/unix/native/libjava/io_util_md.c @@ -107,7 +107,15 @@ fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags) #endif fd = handleOpen(ps, flags, 0666); if (fd != -1) { + jobject fdobj; + jboolean append; SET_FD(this, fd, fid); + + fdobj = (*env)->GetObjectField(env, this, fid); + if (fdobj != NULL) { + append = (flags & O_APPEND) == 0 ? JNI_FALSE : JNI_TRUE; + (*env)->SetBooleanField(env, fdobj, IO_append_fdID, append); + } } else { throwFileNotFoundException(env, path); } diff --git a/jdk/src/java.base/windows/classes/java/io/FileDescriptor.java b/jdk/src/java.base/windows/classes/java/io/FileDescriptor.java index c8cbc218df3..0ce43ef1c9d 100644 --- a/jdk/src/java.base/windows/classes/java/io/FileDescriptor.java +++ b/jdk/src/java.base/windows/classes/java/io/FileDescriptor.java @@ -50,6 +50,11 @@ public final class FileDescriptor { private List otherParents; private boolean closed; + /** + * true, if file is opened for appending. + */ + private boolean append; + /** * Constructs an (invalid) FileDescriptor * object. @@ -75,6 +80,14 @@ public final class FileDescriptor { return obj.fd; } + public void setAppend(FileDescriptor obj, boolean append) { + obj.append = append; + } + + public boolean getAppend(FileDescriptor obj) { + return obj.append; + } + public void setHandle(FileDescriptor obj, long handle) { obj.handle = handle; } diff --git a/jdk/src/java.base/windows/classes/sun/nio/ch/FileDispatcherImpl.java b/jdk/src/java.base/windows/classes/sun/nio/ch/FileDispatcherImpl.java index ccab64d12a4..19997d0c1ec 100644 --- a/jdk/src/java.base/windows/classes/sun/nio/ch/FileDispatcherImpl.java +++ b/jdk/src/java.base/windows/classes/sun/nio/ch/FileDispatcherImpl.java @@ -31,22 +31,14 @@ import sun.misc.JavaIOFileDescriptorAccess; class FileDispatcherImpl extends FileDispatcher { + private static final JavaIOFileDescriptorAccess fdAccess = + SharedSecrets.getJavaIOFileDescriptorAccess(); + static { IOUtil.load(); } - /** - * Indicates if the dispatcher should first advance the file position - * to the end of file when writing. - */ - private final boolean append; - - FileDispatcherImpl(boolean append) { - this.append = append; - } - FileDispatcherImpl() { - this(false); } @Override @@ -71,7 +63,7 @@ class FileDispatcherImpl extends FileDispatcher } int write(FileDescriptor fd, long address, int len) throws IOException { - return write0(fd, address, len, append); + return write0(fd, address, len, fdAccess.getAppend(fd)); } int pwrite(FileDescriptor fd, long address, int len, long position) @@ -81,7 +73,7 @@ class FileDispatcherImpl extends FileDispatcher } long writev(FileDescriptor fd, long address, int len) throws IOException { - return writev0(fd, address, len, append); + return writev0(fd, address, len, fdAccess.getAppend(fd)); } int force(FileDescriptor fd, boolean metaData) throws IOException { @@ -112,8 +104,6 @@ class FileDispatcherImpl extends FileDispatcher FileDescriptor duplicateForMapping(FileDescriptor fd) throws IOException { // on Windows we need to keep a handle to the file - JavaIOFileDescriptorAccess fdAccess = - SharedSecrets.getJavaIOFileDescriptorAccess(); FileDescriptor result = new FileDescriptor(); long handle = duplicateHandle(fdAccess.getHandle(fd)); fdAccess.setHandle(result, handle); diff --git a/jdk/src/java.base/windows/classes/sun/nio/fs/WindowsChannelFactory.java b/jdk/src/java.base/windows/classes/sun/nio/fs/WindowsChannelFactory.java index 46d063ca172..de8cc8d7f5c 100644 --- a/jdk/src/java.base/windows/classes/sun/nio/fs/WindowsChannelFactory.java +++ b/jdk/src/java.base/windows/classes/sun/nio/fs/WindowsChannelFactory.java @@ -160,7 +160,7 @@ class WindowsChannelFactory { throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed"); FileDescriptor fdObj = open(pathForWindows, pathToCheck, flags, pSecurityDescriptor); - return FileChannelImpl.open(fdObj, pathForWindows, flags.read, flags.write, flags.append, null); + return FileChannelImpl.open(fdObj, pathForWindows, flags.read, flags.write, null); } /** @@ -339,6 +339,7 @@ class WindowsChannelFactory { // create FileDescriptor and return FileDescriptor fdObj = new FileDescriptor(); fdAccess.setHandle(fdObj, handle); + fdAccess.setAppend(fdObj, flags.append); return fdObj; } } diff --git a/jdk/src/java.base/windows/native/libjava/FileDescriptor_md.c b/jdk/src/java.base/windows/native/libjava/FileDescriptor_md.c index db04cd47653..7921a9b1d2f 100644 --- a/jdk/src/java.base/windows/native/libjava/FileDescriptor_md.c +++ b/jdk/src/java.base/windows/native/libjava/FileDescriptor_md.c @@ -42,6 +42,9 @@ jfieldID IO_fd_fdID; /* field id for jlong 'handle' in java.io.FileDescriptor */ jfieldID IO_handle_fdID; +/* field id for jboolean 'append' in java.io.FileDescriptor */ +jfieldID IO_append_fdID; + /************************************************************** * static methods to store field IDs in initializers */ @@ -50,6 +53,7 @@ JNIEXPORT void JNICALL Java_java_io_FileDescriptor_initIDs(JNIEnv *env, jclass fdClass) { CHECK_NULL(IO_fd_fdID = (*env)->GetFieldID(env, fdClass, "fd", "I")); CHECK_NULL(IO_handle_fdID = (*env)->GetFieldID(env, fdClass, "handle", "J")); + CHECK_NULL(IO_append_fdID = (*env)->GetFieldID(env, fdClass, "append", "Z")); } JNIEXPORT jlong JNICALL diff --git a/jdk/src/java.base/windows/native/libjava/io_util_md.c b/jdk/src/java.base/windows/native/libjava/io_util_md.c index ba2db4de401..a37ecb6e28b 100644 --- a/jdk/src/java.base/windows/native/libjava/io_util_md.c +++ b/jdk/src/java.base/windows/native/libjava/io_util_md.c @@ -275,7 +275,15 @@ fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags) { FD h = winFileHandleOpen(env, path, flags); if (h >= 0) { + jobject fdobj; + jboolean append; SET_FD(this, h, fid); + + fdobj = (*env)->GetObjectField(env, this, fid); + if (fdobj != NULL) { + append = (flags & O_APPEND) == 0 ? JNI_FALSE : JNI_TRUE; + (*env)->SetBooleanField(env, fdobj, IO_append_fdID, append); + } } } diff --git a/jdk/test/java/io/FileDescriptor/RememberAppend.java b/jdk/test/java/io/FileDescriptor/RememberAppend.java new file mode 100644 index 00000000000..cc08f66924c --- /dev/null +++ b/jdk/test/java/io/FileDescriptor/RememberAppend.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8023173 + * @summary FileDescriptor should respect append flag + */ + +import java.io.File; +import java.io.FileDescriptor; +import java.io.FileOutputStream; + +public class RememberAppend { + private static final byte[] bytes = "ABC ".getBytes(); + + public static void main(String[] args) throws Throwable { + File f = File.createTempFile("tmp.file", null); + f.deleteOnExit(); + + try (FileOutputStream fos1 = new FileOutputStream(f.getPath(), true)) { + fos1.write(bytes); + } + + try (FileOutputStream fos1 = new FileOutputStream(f.getPath(), true); + FileOutputStream fos2 = new FileOutputStream(fos1.getFD())) { + fos2.write(bytes); + } + + if (f.length() != 2 * bytes.length) { + throw new RuntimeException("Append flag ignored"); + } + } +}