提交 73924d21 编写于 作者: S sla

8033917: Keep track of file paths in file streams and channels for instrumentation purposes

Reviewed-by: alanb, dsamersoff
上级 558d8d76
...@@ -51,6 +51,12 @@ class FileInputStream extends InputStream ...@@ -51,6 +51,12 @@ class FileInputStream extends InputStream
/* File Descriptor - handle to the open file */ /* File Descriptor - handle to the open file */
private final FileDescriptor fd; private final FileDescriptor fd;
/**
* The path of the referenced file
* (null if the stream is created with a file descriptor)
*/
private final String path;
private FileChannel channel = null; private FileChannel channel = null;
private final Object closeLock = new Object(); private final Object closeLock = new Object();
...@@ -128,6 +134,7 @@ class FileInputStream extends InputStream ...@@ -128,6 +134,7 @@ class FileInputStream extends InputStream
} }
fd = new FileDescriptor(); fd = new FileDescriptor();
fd.attach(this); fd.attach(this);
path = name;
open(name); open(name);
} }
...@@ -164,6 +171,7 @@ class FileInputStream extends InputStream ...@@ -164,6 +171,7 @@ class FileInputStream extends InputStream
security.checkRead(fdObj); security.checkRead(fdObj);
} }
fd = fdObj; fd = fdObj;
path = null;
/* /*
* FileDescriptor is being shared by streams. * FileDescriptor is being shared by streams.
...@@ -349,7 +357,7 @@ class FileInputStream extends InputStream ...@@ -349,7 +357,7 @@ class FileInputStream extends InputStream
public FileChannel getChannel() { public FileChannel getChannel() {
synchronized (this) { synchronized (this) {
if (channel == null) { if (channel == null) {
channel = FileChannelImpl.open(fd, true, false, this); channel = FileChannelImpl.open(fd, path, true, false, this);
} }
return channel; return channel;
} }
......
...@@ -67,6 +67,12 @@ class FileOutputStream extends OutputStream ...@@ -67,6 +67,12 @@ class FileOutputStream extends OutputStream
*/ */
private FileChannel channel; private FileChannel channel;
/**
* The path of the referenced file
* (null if the stream is created with a file descriptor)
*/
private final String path;
private final Object closeLock = new Object(); private final Object closeLock = new Object();
private volatile boolean closed = false; private volatile boolean closed = false;
...@@ -202,6 +208,7 @@ class FileOutputStream extends OutputStream ...@@ -202,6 +208,7 @@ class FileOutputStream extends OutputStream
this.fd = new FileDescriptor(); this.fd = new FileDescriptor();
fd.attach(this); fd.attach(this);
this.append = append; this.append = append;
this.path = name;
open(name, append); open(name, append);
} }
...@@ -239,6 +246,7 @@ class FileOutputStream extends OutputStream ...@@ -239,6 +246,7 @@ class FileOutputStream extends OutputStream
} }
this.fd = fdObj; this.fd = fdObj;
this.append = false; this.append = false;
this.path = null;
fd.attach(this); fd.attach(this);
} }
...@@ -376,7 +384,7 @@ class FileOutputStream extends OutputStream ...@@ -376,7 +384,7 @@ class FileOutputStream extends OutputStream
public FileChannel getChannel() { public FileChannel getChannel() {
synchronized (this) { synchronized (this) {
if (channel == null) { if (channel == null) {
channel = FileChannelImpl.open(fd, false, true, append, this); channel = FileChannelImpl.open(fd, path, false, true, append, this);
} }
return channel; return channel;
} }
......
...@@ -62,6 +62,12 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable { ...@@ -62,6 +62,12 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable {
private FileChannel channel = null; private FileChannel channel = null;
private boolean rw; private boolean rw;
/**
* The path of the referenced file
* (null if the stream is created with a file descriptor)
*/
private final String path;
private Object closeLock = new Object(); private Object closeLock = new Object();
private volatile boolean closed = false; private volatile boolean closed = false;
...@@ -233,6 +239,7 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable { ...@@ -233,6 +239,7 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable {
} }
fd = new FileDescriptor(); fd = new FileDescriptor();
fd.attach(this); fd.attach(this);
path = name;
open(name, imode); open(name, imode);
} }
...@@ -272,7 +279,7 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable { ...@@ -272,7 +279,7 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable {
public final FileChannel getChannel() { public final FileChannel getChannel() {
synchronized (this) { synchronized (this) {
if (channel == null) { if (channel == null) {
channel = FileChannelImpl.open(fd, true, rw, this); channel = FileChannelImpl.open(fd, path, true, rw, this);
} }
return channel; return channel;
} }
......
...@@ -29,10 +29,20 @@ import java.io.FileDescriptor; ...@@ -29,10 +29,20 @@ import java.io.FileDescriptor;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer; import java.nio.MappedByteBuffer;
import java.nio.channels.*; import java.nio.channels.ClosedByInterruptException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.FileLockInterruptionException;
import java.nio.channels.NonReadableChannelException;
import java.nio.channels.NonWritableChannelException;
import java.nio.channels.OverlappingFileLockException;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.security.AccessController;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.security.AccessController;
import sun.misc.Cleaner; import sun.misc.Cleaner;
import sun.security.action.GetPropertyAction; import sun.security.action.GetPropertyAction;
...@@ -56,13 +66,17 @@ public class FileChannelImpl ...@@ -56,13 +66,17 @@ public class FileChannelImpl
// Required to prevent finalization of creating stream (immutable) // Required to prevent finalization of creating stream (immutable)
private final Object parent; private final Object parent;
// The path of the referenced file
// (null if the parent stream is created with a file descriptor)
private final String path;
// Thread-safe set of IDs of native threads, for signalling // Thread-safe set of IDs of native threads, for signalling
private final NativeThreadSet threads = new NativeThreadSet(2); private final NativeThreadSet threads = new NativeThreadSet(2);
// Lock for operations involving position and size // Lock for operations involving position and size
private final Object positionLock = new Object(); private final Object positionLock = new Object();
private FileChannelImpl(FileDescriptor fd, boolean readable, private FileChannelImpl(FileDescriptor fd, String path, boolean readable,
boolean writable, boolean append, Object parent) boolean writable, boolean append, Object parent)
{ {
this.fd = fd; this.fd = fd;
...@@ -70,23 +84,24 @@ public class FileChannelImpl ...@@ -70,23 +84,24 @@ public class FileChannelImpl
this.writable = writable; this.writable = writable;
this.append = append; this.append = append;
this.parent = parent; this.parent = parent;
this.path = path;
this.nd = new FileDispatcherImpl(append); this.nd = new FileDispatcherImpl(append);
} }
// Used by FileInputStream.getChannel() and RandomAccessFile.getChannel() // Used by FileInputStream.getChannel() and RandomAccessFile.getChannel()
public static FileChannel open(FileDescriptor fd, public static FileChannel open(FileDescriptor fd, String path,
boolean readable, boolean writable, boolean readable, boolean writable,
Object parent) Object parent)
{ {
return new FileChannelImpl(fd, readable, writable, false, parent); return new FileChannelImpl(fd, path, readable, writable, false, parent);
} }
// Used by FileOutputStream.getChannel // Used by FileOutputStream.getChannel
public static FileChannel open(FileDescriptor fd, public static FileChannel open(FileDescriptor fd, String path,
boolean readable, boolean writable, boolean readable, boolean writable,
boolean append, Object parent) boolean append, Object parent)
{ {
return new FileChannelImpl(fd, readable, writable, append, parent); return new FileChannelImpl(fd, path, readable, writable, append, parent);
} }
private void ensureOpen() throws IOException { private void ensureOpen() throws IOException {
......
...@@ -149,7 +149,7 @@ class SolarisUserDefinedFileAttributeView ...@@ -149,7 +149,7 @@ class SolarisUserDefinedFileAttributeView
int afd = openat(fd, nameAsBytes(file,name), (O_RDONLY|O_XATTR), 0); int afd = openat(fd, nameAsBytes(file,name), (O_RDONLY|O_XATTR), 0);
// wrap with channel // wrap with channel
FileChannel fc = UnixChannelFactory.newFileChannel(afd, true, false); FileChannel fc = UnixChannelFactory.newFileChannel(afd, file.toString(), true, false);
// read to EOF (nothing we can do if I/O error occurs) // read to EOF (nothing we can do if I/O error occurs)
try { try {
...@@ -190,7 +190,7 @@ class SolarisUserDefinedFileAttributeView ...@@ -190,7 +190,7 @@ class SolarisUserDefinedFileAttributeView
UnixFileModeAttribute.ALL_PERMISSIONS); UnixFileModeAttribute.ALL_PERMISSIONS);
// wrap with channel // wrap with channel
FileChannel fc = UnixChannelFactory.newFileChannel(afd, false, true); FileChannel fc = UnixChannelFactory.newFileChannel(afd, file.toString(), false, true);
// write value (nothing we can do if I/O error occurs) // write value (nothing we can do if I/O error occurs)
try { try {
......
...@@ -100,10 +100,10 @@ class UnixChannelFactory { ...@@ -100,10 +100,10 @@ class UnixChannelFactory {
/** /**
* Constructs a file channel from an existing (open) file descriptor * Constructs a file channel from an existing (open) file descriptor
*/ */
static FileChannel newFileChannel(int fd, boolean reading, boolean writing) { static FileChannel newFileChannel(int fd, String path, boolean reading, boolean writing) {
FileDescriptor fdObj = new FileDescriptor(); FileDescriptor fdObj = new FileDescriptor();
fdAccess.set(fdObj, fd); fdAccess.set(fdObj, fd);
return FileChannelImpl.open(fdObj, reading, writing, null); return FileChannelImpl.open(fdObj, path, reading, writing, null);
} }
/** /**
...@@ -134,7 +134,7 @@ class UnixChannelFactory { ...@@ -134,7 +134,7 @@ class UnixChannelFactory {
throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed"); throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");
FileDescriptor fdObj = open(dfd, path, pathForPermissionCheck, flags, mode); FileDescriptor fdObj = open(dfd, path, pathForPermissionCheck, flags, mode);
return FileChannelImpl.open(fdObj, flags.read, flags.write, flags.append, null); return FileChannelImpl.open(fdObj, path.toString(), flags.read, flags.write, flags.append, null);
} }
/** /**
......
...@@ -25,19 +25,22 @@ ...@@ -25,19 +25,22 @@
package sun.nio.fs; package sun.nio.fs;
import java.nio.file.*;
import java.nio.channels.*;
import java.io.FileDescriptor; import java.io.FileDescriptor;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.FileChannel;
import java.nio.file.LinkOption;
import java.nio.file.OpenOption;
import java.nio.file.StandardOpenOption;
import java.util.Set;
import com.sun.nio.file.ExtendedOpenOption; import com.sun.nio.file.ExtendedOpenOption;
import sun.misc.JavaIOFileDescriptorAccess;
import sun.misc.SharedSecrets;
import sun.nio.ch.FileChannelImpl; import sun.nio.ch.FileChannelImpl;
import sun.nio.ch.ThreadPool; import sun.nio.ch.ThreadPool;
import sun.nio.ch.WindowsAsynchronousFileChannelImpl; import sun.nio.ch.WindowsAsynchronousFileChannelImpl;
import sun.misc.SharedSecrets;
import sun.misc.JavaIOFileDescriptorAccess;
import static sun.nio.fs.WindowsNativeDispatcher.*; import static sun.nio.fs.WindowsNativeDispatcher.*;
import static sun.nio.fs.WindowsConstants.*; import static sun.nio.fs.WindowsConstants.*;
...@@ -157,7 +160,7 @@ class WindowsChannelFactory { ...@@ -157,7 +160,7 @@ class WindowsChannelFactory {
throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed"); throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");
FileDescriptor fdObj = open(pathForWindows, pathToCheck, flags, pSecurityDescriptor); FileDescriptor fdObj = open(pathForWindows, pathToCheck, flags, pSecurityDescriptor);
return FileChannelImpl.open(fdObj, flags.read, flags.write, flags.append, null); return FileChannelImpl.open(fdObj, pathForWindows, flags.read, flags.write, flags.append, null);
} }
/** /**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册