# HG changeset patch # User martin # Date 1541647400 28800 # Wed Nov 07 19:23:20 2018 -0800 # Node ID 3d594d4a2c758228a25b55275cf457e97c561f52 # Parent 5b82f10dc82306d72c5b3a2fdc6b8765daa60667 8213406: (fs) More than one instance of built-in FileSystem observed in heap Reviewed-by: alanb, cushon, weijun diff --git a/src/java.base/linux/classes/sun/nio/fs/DefaultFileSystemProvider.java b/src/java.base/linux/classes/sun/nio/fs/BuiltinFileSystemProvider.java rename from src/java.base/linux/classes/sun/nio/fs/DefaultFileSystemProvider.java rename to src/java.base/linux/classes/sun/nio/fs/BuiltinFileSystemProvider.java --- a/src/java.base/linux/classes/sun/nio/fs/DefaultFileSystemProvider.java +++ b/src/java.base/linux/classes/sun/nio/fs/BuiltinFileSystemProvider.java @@ -25,19 +25,21 @@ package sun.nio.fs; -import java.nio.file.spi.FileSystemProvider; +import java.net.URI; +import java.nio.file.FileSystem; /** - * Creates this platform's default FileSystemProvider. + * Creates this platform's builtin FileSystemProvider and FileSystem. */ -public class DefaultFileSystemProvider { - private DefaultFileSystemProvider() { } +public class BuiltinFileSystemProvider { + private BuiltinFileSystemProvider() { } - /** - * Returns the default FileSystemProvider. - */ - public static FileSystemProvider create() { - return new LinuxFileSystemProvider(); - } + /** The unique instance of the builtin file system provider. */ + public static final LinuxFileSystemProvider BUILTIN_FILE_SYSTEM_PROVIDER + = new LinuxFileSystemProvider(); + + /** The unique instance of the builtin file system. */ + public static final FileSystem BUILTIN_FILE_SYSTEM + = BUILTIN_FILE_SYSTEM_PROVIDER.getFileSystem(URI.create("file:///")); } diff --git a/src/java.base/linux/classes/sun/nio/fs/LinuxFileSystemProvider.java b/src/java.base/linux/classes/sun/nio/fs/LinuxFileSystemProvider.java --- a/src/java.base/linux/classes/sun/nio/fs/LinuxFileSystemProvider.java +++ b/src/java.base/linux/classes/sun/nio/fs/LinuxFileSystemProvider.java @@ -36,7 +36,7 @@ * Linux implementation of FileSystemProvider */ -public class LinuxFileSystemProvider extends UnixFileSystemProvider { +class LinuxFileSystemProvider extends UnixFileSystemProvider { public LinuxFileSystemProvider() { super(); } diff --git a/src/java.base/share/classes/java/io/FilePermission.java b/src/java.base/share/classes/java/io/FilePermission.java --- a/src/java.base/share/classes/java/io/FilePermission.java +++ b/src/java.base/share/classes/java/io/FilePermission.java @@ -36,10 +36,10 @@ import jdk.internal.access.JavaIOFilePermissionAccess; import jdk.internal.access.SharedSecrets; -import sun.nio.fs.DefaultFileSystemProvider; import sun.security.action.GetPropertyAction; import sun.security.util.FilePermCompat; import sun.security.util.SecurityConstants; +import static sun.nio.fs.BuiltinFileSystemProvider.BUILTIN_FILE_SYSTEM; /** * This class represents access to a file or directory. A FilePermission consists @@ -198,20 +198,15 @@ private static final long serialVersionUID = 7930732926638008763L; - /** - * Always use the internal default file system, in case it was modified - * with java.nio.file.spi.DefaultFileSystemProvider. - */ - private static final java.nio.file.FileSystem builtInFS = - DefaultFileSystemProvider.create() - .getFileSystem(URI.create("file:///")); + // Always use the internal default file system, in case it was modified + // with the java.nio.file.spi.DefaultFileSystemProvider system property. - private static final Path here = builtInFS.getPath( + private static final Path here = BUILTIN_FILE_SYSTEM.getPath( GetPropertyAction.privilegedGetProperty("user.dir")); - private static final Path EMPTY_PATH = builtInFS.getPath(""); - private static final Path DASH_PATH = builtInFS.getPath("-"); - private static final Path DOTDOT_PATH = builtInFS.getPath(".."); + private static final Path EMPTY_PATH = BUILTIN_FILE_SYSTEM.getPath(""); + private static final Path DASH_PATH = BUILTIN_FILE_SYSTEM.getPath("-"); + private static final Path DOTDOT_PATH = BUILTIN_FILE_SYSTEM.getPath(".."); /** * A private constructor that clones some and updates some, @@ -326,7 +321,7 @@ if (name.equals("<>")) { allFiles = true; - npath = builtInFS.getPath(""); + npath = EMPTY_PATH; // other fields remain default return; } @@ -341,7 +336,7 @@ try { // new File() can "normalize" some name, for example, "/C:/X" on // Windows. Some JDK codes generate such illegal names. - npath = builtInFS.getPath(new File(name).getPath()) + npath = BUILTIN_FILE_SYSTEM.getPath(new File(name).getPath()) .normalize(); // lastName should always be non-null now Path lastName = npath.getFileName(); @@ -351,13 +346,13 @@ npath = npath.getParent(); } if (npath == null) { - npath = builtInFS.getPath(""); + npath = EMPTY_PATH; } invalid = false; } catch (InvalidPathException ipe) { // Still invalid. For compatibility reason, accept it // but make this permission useless. - npath = builtInFS.getPath("-u-s-e-l-e-s-s-"); + npath = BUILTIN_FILE_SYSTEM.getPath("-u-s-e-l-e-s-s-"); invalid = true; } diff --git a/src/java.base/share/classes/java/nio/file/FileSystems.java b/src/java.base/share/classes/java/nio/file/FileSystems.java --- a/src/java.base/share/classes/java/nio/file/FileSystems.java +++ b/src/java.base/share/classes/java/nio/file/FileSystems.java @@ -37,6 +37,8 @@ import java.util.ServiceLoader; import jdk.internal.misc.VM; +import static sun.nio.fs.BuiltinFileSystemProvider.BUILTIN_FILE_SYSTEM; +import static sun.nio.fs.BuiltinFileSystemProvider.BUILTIN_FILE_SYSTEM_PROVIDER; /** * Factory methods for file systems. This class defines the {@link #getDefault @@ -88,16 +90,6 @@ public final class FileSystems { private FileSystems() { } - // Built-in file system provider - private static final FileSystemProvider builtinFileSystemProvider = - sun.nio.fs.DefaultFileSystemProvider.create(); - - // built-in file system - private static class BuiltinFileSystemHolder { - static final FileSystem builtinFileSystem = - builtinFileSystemProvider.getFileSystem(URI.create("file:///")); - } - // lazy initialization of default file system private static class DefaultFileSystemHolder { static final FileSystem defaultFileSystem = defaultFileSystem(); @@ -118,7 +110,7 @@ // returns default provider private static FileSystemProvider getDefaultProvider() { - FileSystemProvider provider = builtinFileSystemProvider; + FileSystemProvider provider = BUILTIN_FILE_SYSTEM_PROVIDER; // if the property java.nio.file.spi.DefaultFileSystemProvider is // set then its value is the name of the default provider (or a list) @@ -189,7 +181,7 @@ if (VM.isModuleSystemInited()) { return DefaultFileSystemHolder.defaultFileSystem; } else { - return BuiltinFileSystemHolder.builtinFileSystem; + return BUILTIN_FILE_SYSTEM; } }