< prev index next >

src/java.base/share/classes/jdk/internal/jrtfs/ExplodedImage.java

Print this page
*** 1,7 ***
  /*
!  * Copyright (c) 2015, 2025, 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.  Oracle designates this
--- 1,7 ---
  /*
!  * Copyright (c) 2015, 2026, 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.  Oracle designates this

*** 28,17 ***
--- 28,23 ---
  import java.io.UncheckedIOException;
  import java.nio.file.DirectoryStream;
  import java.nio.file.FileSystemException;
  import java.nio.file.Files;
  import java.nio.file.Path;
+ import java.nio.file.Paths;
  import java.nio.file.attribute.BasicFileAttributes;
  import java.util.ArrayList;
  import java.util.HashMap;
+ import java.util.LinkedHashSet;
  import java.util.List;
  import java.util.Map;
  import java.util.Objects;
+ import java.util.Set;
+ import java.util.function.UnaryOperator;
+ import java.util.stream.Collectors;
  import java.util.stream.Stream;
+ import java.util.stream.StreamSupport;
  
  import jdk.internal.jimage.ImageReader.Node;
  
  /**
   * A jrt file system built on $JAVA_HOME/modules directory ('exploded modules

*** 52,56 ***
   */
  class ExplodedImage extends SystemImage {
  
      private static final String MODULES = "/modules/";
      private static final String PACKAGES = "/packages/";
  
      private final Path modulesDir;
!     private final String separator;
      private final Map<String, PathNode> nodes = new HashMap<>();
      private final BasicFileAttributes modulesDirAttrs;
  
!     ExplodedImage(Path modulesDir) throws IOException {
          this.modulesDir = modulesDir;
!         String str = modulesDir.getFileSystem().getSeparator();
-         separator = str.equals("/") ? null : str;
          modulesDirAttrs = Files.readAttributes(modulesDir, BasicFileAttributes.class);
          initNodes();
      }
  
!     // A Node that is backed by actual default file system Path
      private final class PathNode extends Node {
  
!         // Path in underlying default file system
!         private Path path;
!         private PathNode link;
!         private List<Node> children;
  
!         private PathNode(String name, Path path, BasicFileAttributes attrs) {  // path
              super(name, attrs);
!             this.path = path;
          }
  
!         private PathNode(String name, Node link) {              // link
              super(name, link.getFileAttributes());
!             this.link = (PathNode)link;
          }
  
!         private PathNode(String name, List<Node> children) {    // dir
              super(name, modulesDirAttrs);
!             this.children = children;
          }
  
          @Override
          public boolean isResource() {
!             return link == null && !getFileAttributes().isDirectory();
          }
  
          @Override
          public boolean isDirectory() {
!             return children != null ||
-                    (link == null && getFileAttributes().isDirectory());
          }
  
          @Override
          public boolean isLink() {
              return link != null;
--- 58,98 ---
   */
  class ExplodedImage extends SystemImage {
  
      private static final String MODULES = "/modules/";
      private static final String PACKAGES = "/packages/";
+     // This directory cannot be preview overridden.
+     private static final Path META_INF_DIR = Paths.get("META-INF");
+     // Root of the preview override of a module relative to root of that module.
+     // This directory never appears in either non-preview or preview images.
+     private static final Path PREVIEW_DIR = META_INF_DIR.resolve("preview");
  
      private final Path modulesDir;
!     private final boolean isPreviewMode;
      private final Map<String, PathNode> nodes = new HashMap<>();
      private final BasicFileAttributes modulesDirAttrs;
  
!     ExplodedImage(Path modulesDir, boolean isPreviewMode) throws IOException {
          this.modulesDir = modulesDir;
!         this.isPreviewMode = isPreviewMode;
          modulesDirAttrs = Files.readAttributes(modulesDir, BasicFileAttributes.class);
          initNodes();
      }
  
!     // A Node that is backed by absolute Paths on the default FS
+     // This is thread-safe, guaranteed by synchronized findNode
      private final class PathNode extends Node {
+         // Regular file
+         private final Path file;
+         // Symbolic link
+         private final PathNode link;
+         // Directories
+         // `directories` is written before and read after `childNames`
+         private List<Path> directories;
+         private volatile List<String> childNames; // Has no duplicates
  
!         /**
!          * Creates a file based node with the given file attributes.
!          * Used for all /modules/... files.
!          */
+         private PathNode(String name, Path file, BasicFileAttributes attrs) {
+             super(name, attrs);
+             this.file = Objects.requireNonNull(file);
+             this.link = null;
+             this.directories = null;
+             this.childNames = null;
+         }
  
!         /**
+          * Creates a directory based node with the given file attributes.
+          * Used for all /modules/... directories.  It is created in an
+          * "incomplete" state, and its child names are determined lazily.
+          */
+         private PathNode(String name, List<Path> directories, BasicFileAttributes attrs) {
              super(name, attrs);
!             this.file = null;
+             this.link = null;
+             this.directories = Objects.requireNonNull(directories);
+             this.childNames = null;
          }
  
!         /**
+          * Creates a symbolic link node to the specified target.
+          * Used for each module-named directory that are leafs of /packages/...
+          */
+         private PathNode(String name, PathNode link) {
              super(name, link.getFileAttributes());
!             this.file = null;
+             this.link = Objects.requireNonNull(link);
+             this.directories = null;
+             this.childNames = null;
          }
  
!         /**
+          * Creates a completed directory node based a list of child nodes.
+          * Used for the root, /modules, /packages, and /packages/... non-leaf
+          * directories, all created in initNodes().
+          */
+         private PathNode(String name, List<PathNode> children) {
              super(name, modulesDirAttrs);
!             this.file = null;
+             this.link = null;
+             this.directories = null;
+             this.childNames = children.stream().map(Node::getName).collect(Collectors.toList());
          }
  
          @Override
          public boolean isResource() {
!             return file != null;
          }
  
          @Override
          public boolean isDirectory() {
!             return childNames != null || directories != null;
          }
  
          @Override
          public boolean isLink() {
              return link != null;

*** 113,42 ***
                  return this;
              return recursive && link.isLink() ? link.resolveLink(true) : link;
          }
  
          private byte[] getContent() throws IOException {
!             if (!getFileAttributes().isRegularFile())
                  throw new FileSystemException(getName() + " is not file");
!             return Files.readAllBytes(path);
          }
  
          @Override
          public Stream<String> getChildNames() {
              if (!isDirectory())
!                 throw new IllegalArgumentException("not a directory: " + getName());
!             if (children == null) {
!                 List<Node> list = new ArrayList<>();
!                 try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
!                     for (Path p : stream) {
!                         p = modulesDir.relativize(p);
!                         String pName = MODULES + nativeSlashToFrontSlash(p.toString());
!                         Node node = findNode(pName);
!                         if (node != null) {  // findNode may choose to hide certain files!
!                             list.add(node);
!                         }
                      }
-                 } catch (IOException x) {
-                     return null;
                  }
!                 children = list;
              }
-             return children.stream().map(Node::getName);
          }
  
          @Override
          public long size() {
              try {
!                 return isDirectory() ? 0 : Files.size(path);
              } catch (IOException ex) {
                  throw new UncheckedIOException(ex);
              }
          }
      }
--- 161,56 ---
                  return this;
              return recursive && link.isLink() ? link.resolveLink(true) : link;
          }
  
          private byte[] getContent() throws IOException {
!             if (!isResource())
                  throw new FileSystemException(getName() + " is not file");
!             return Files.readAllBytes(file);
          }
  
          @Override
          public Stream<String> getChildNames() {
              if (!isDirectory())
!                 throw new IllegalStateException("not a directory: " + getName());
!             List<String> names = childNames;
!             if (names == null) {
!                 names = completeDirectory();
!             }
!             return names.stream();
!         }
! 
!         private synchronized List<String> completeDirectory() {
!             if (childNames != null) {
!                 return childNames;
+             }
+ 
+             Set<String> childNameSet = new LinkedHashSet<>();
+             for (Path path : directories) {
+                 collectChildNodeNames(path, childNameSet);
+             }
+             directories = null;
+             return childNames = new ArrayList<>(childNameSet);
+         }
+ 
+         private void collectChildNodeNames(Path absPath, Set<String> childNameSet) {
+             try (DirectoryStream<Path> stream = Files.newDirectoryStream(absPath)) {
+                 for (Path p : stream) {
+                     PathNode node = (PathNode) findNode(getName() + "/" + p.getFileName().toString());
+                     if (node != null) {  // findNode may choose to hide certain files!
+                         childNameSet.add(node.getName());
                      }
                  }
!             } catch (IOException ex) {
+                 throw new UncheckedIOException(ex);
              }
          }
  
          @Override
          public long size() {
              try {
!                 return !isResource() ? 0 : Files.size(file);
              } catch (IOException ex) {
                  throw new UncheckedIOException(ex);
              }
          }
      }

*** 167,130 ***
      public synchronized Node findNode(String name) {
          PathNode node = nodes.get(name);
          if (node != null) {
              return node;
          }
!         // If null, this was not the name of "/modules/..." node, and since all
!         // "/packages/..." nodes were created and cached in advance, the name
!         // cannot reference a valid node.
!         Path path = underlyingModulesPath(name);
!         if (path == null) {
              return null;
          }
!         // This can still return null for hidden files.
!         return createModulesNode(name, path);
      }
  
      /**
       * Lazily creates and caches a {@code Node} for the given "/modules/..." name
       * and corresponding path to a file or directory.
       *
       * @param name a resource or directory node name, of the form "/modules/...".
-      * @param path the path of a file for a resource or directory.
       * @return the newly created and cached node, or {@code null} if the given
       *     path references a file which must be hidden in the node hierarchy.
       */
!     private Node createModulesNode(String name, Path path) {
          assert !nodes.containsKey(name) : "Node must not already exist: " + name;
-         assert isNonEmptyModulesPath(name) : "Invalid modules name: " + name;
  
          try {
!             // We only know if we're creating a resource of directory when we
-             // look up file attributes, and we only do that once. Thus, we can
-             // only reject "marker files" here, rather than by inspecting the
-             // given name string, since it doesn't apply to directories.
-             BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
-             if (attrs.isRegularFile()) {
-                 Path f = path.getFileName();
-                 if (f.toString().startsWith("_the.")) {
-                     return null;
-                 }
-             } else if (!attrs.isDirectory()) {
-                 return null;
-             }
-             PathNode node = new PathNode(name, path, attrs);
-             nodes.put(name, node);
-             return node;
          } catch (IOException x) {
!             // Since the path reference a file, any errors should not be ignored.
              throw new UncheckedIOException(x);
          }
-     }
  
!     /**
!      * Returns the expected file path for name in the "/modules/..." namespace,
!      * or {@code null} if the name is not in the "/modules/..." namespace or the
!      * path does not reference a file.
!      */
!     private Path underlyingModulesPath(String name) {
!         if (isNonEmptyModulesPath(name)) {
!             Path path = modulesDir.resolve(frontSlashToNativeSlash(name.substring(MODULES.length())));
!             return Files.exists(path) ? path : null;
          }
!         return null;
      }
  
!     private static boolean isNonEmptyModulesPath(String name) {
          // Don't just check the prefix, there must be something after it too
          // (otherwise you end up with an empty string after trimming).
!         return name.startsWith(MODULES) && name.length() > MODULES.length();
!     }
! 
!     // convert "/" to platform path separator
!     private String frontSlashToNativeSlash(String str) {
!         return separator == null ? str : str.replace("/", separator);
      }
  
!     // convert platform path separator to "/"
-     private String nativeSlashToFrontSlash(String str) {
-         return separator == null ? str : str.replace(separator, "/");
-     }
- 
-     // convert "/"s to "."s
-     private String slashesToDots(String str) {
-         return str.replace(separator != null ? separator : "/", ".");
-     }
- 
-     // initialize file system Nodes
      private void initNodes() throws IOException {
          // same package prefix may exist in multiple modules. This Map
          // is filled by walking "jdk modules" directory recursively!
          Map<String, List<String>> packageToModules = new HashMap<>();
!         try (DirectoryStream<Path> stream = Files.newDirectoryStream(modulesDir)) {
!             for (Path module : stream) {
!                 if (Files.isDirectory(module)) {
!                     String moduleName = module.getFileName().toString();
-                     // make sure "/modules/<moduleName>" is created
-                     Objects.requireNonNull(createModulesNode(MODULES + moduleName, module));
-                     try (Stream<Path> contentsStream = Files.walk(module)) {
-                         contentsStream.filter(Files::isDirectory).forEach((p) -> {
-                             p = module.relativize(p);
-                             String pkgName = slashesToDots(p.toString());
-                             // skip META-INF and empty strings
-                             if (!pkgName.isEmpty() && !pkgName.startsWith("META-INF")) {
-                                 packageToModules
-                                         .computeIfAbsent(pkgName, k -> new ArrayList<>())
-                                         .add(moduleName);
-                             }
-                         });
-                     }
-                 }
              }
          }
          // create "/modules" directory
!         // "nodes" map contains only /modules/<foo> nodes only so far and so add all as children of /modules
-         PathNode modulesRootNode = new PathNode("/modules", new ArrayList<>(nodes.values()));
          nodes.put(modulesRootNode.getName(), modulesRootNode);
  
          // create children under "/packages"
!         List<Node> packagesChildren = new ArrayList<>(packageToModules.size());
          for (Map.Entry<String, List<String>> entry : packageToModules.entrySet()) {
              String pkgName = entry.getKey();
              List<String> moduleNameList = entry.getValue();
!             List<Node> moduleLinkNodes = new ArrayList<>(moduleNameList.size());
              for (String moduleName : moduleNameList) {
!                 Node moduleNode = Objects.requireNonNull(nodes.get(MODULES + moduleName));
                  PathNode linkNode = new PathNode(PACKAGES + pkgName + "/" + moduleName, moduleNode);
                  nodes.put(linkNode.getName(), linkNode);
                  moduleLinkNodes.add(linkNode);
              }
              PathNode pkgDir = new PathNode(PACKAGES + pkgName, moduleLinkNodes);
--- 229,151 ---
      public synchronized Node findNode(String name) {
          PathNode node = nodes.get(name);
          if (node != null) {
              return node;
          }
! 
!         return createPathInModulesNodeIfValid(name);
!     }
! 
!     // `rest` nullable means name points to the root of a module
+     private Path candidatePath(Path module, Path rest, boolean preview) {
+         if (preview && rest != null && rest.startsWith(META_INF_DIR)) {
+             // Nothing in META-INF has a preview override
              return null;
          }
!         Path now = modulesDir.resolve(module);
!         if (preview) {
+             now = now.resolve(PREVIEW_DIR);
+         }
+         if (rest != null) {
+             now = now.resolve(rest);
+         }
+         return Files.exists(now) ? now : null;
      }
  
      /**
       * Lazily creates and caches a {@code Node} for the given "/modules/..." name
       * and corresponding path to a file or directory.
       *
       * @param name a resource or directory node name, of the form "/modules/...".
       * @return the newly created and cached node, or {@code null} if the given
       *     path references a file which must be hidden in the node hierarchy.
       */
!     private PathNode createPathInModulesNodeIfValid(String name) {
+         // We anticipate the name of a "/modules/..." node for lazy creation.
+         // All "/packages/..." nodes are created by initNodes() instead.
+         if (!isPathInModulesName(name)) {
+             return null;
+         }
+ 
          assert !nodes.containsKey(name) : "Node must not already exist: " + name;
  
+         // Extract the module name and the remaining parts of the path
+         Path moduleName; // Exactly a single name element
+         Path remainderPath; // May be null
+         {
+             String relativeName = name.substring(MODULES.length());
+             Path relativePath = Paths.get("", relativeName.split("/"));
+ 
+             moduleName = relativePath.getName(0);
+             int nameCount = relativePath.getNameCount();
+             remainderPath = nameCount > 1 ? relativePath.subpath(1, nameCount) : null;
+         }
+ 
+         // Filter any path to in META-INF/preview consistently
+         if (remainderPath != null && remainderPath.startsWith(PREVIEW_DIR)) {
+             return null;
+         }
+ 
+         // Find valid regular and preview paths
+         Path regularPath = candidatePath(moduleName, remainderPath, false);
+         Path previewPath = isPreviewMode ? candidatePath(moduleName, remainderPath, true) : null;
+         if (regularPath == null && previewPath == null) {
+             return null;
+         }
+ 
+         // Select a path for source of attributes
+         Path selected;
+         if (regularPath != null && Files.isDirectory(regularPath)) {
+             // Non-preview directories take precedence.
+             selected = regularPath;
+         } else {
+             // Otherwise prefer preview resources over non-preview ones.
+             selected = previewPath == null ? regularPath : previewPath;
+         }
+ 
+         // Read the file attributes
+         BasicFileAttributes attrs;
          try {
!             attrs = Files.readAttributes(selected, BasicFileAttributes.class);
          } catch (IOException x) {
!             // Since the path references a file, errors should not be ignored.
              throw new UncheckedIOException(x);
          }
  
!         // Create the right PathNode
!         PathNode node;
!         if (attrs.isRegularFile()) {
!             Path f = selected.getFileName();
!             // Only reject "marker files", doesn't apply to directories
!             if (f.toString().startsWith("_the.")) {
!                 return null;
!             }
!             node = new PathNode(name, selected, attrs);
+         } else if (attrs.isDirectory()) {
+             List<Path> directories = Stream.of(regularPath, previewPath)
+                     .filter(Objects::nonNull)
+                     .collect(Collectors.toList());
+             node = new PathNode(name, directories, attrs);
+         } else {
+             return null;
          }
!         nodes.put(name, node);
+         return node;
      }
  
!     // Ensures this is a name taking form /modules/... with no trailing slash.
+     private static boolean isPathInModulesName(String name) {
          // Don't just check the prefix, there must be something after it too
          // (otherwise you end up with an empty string after trimming).
!         // Also make sure we can't be tricked by "/modules//absolute/path" or
!         // "/modules/../../escaped/path".
!         // Don't use regex as 'name' is untrusted (avoids stack overflow risk)
!         // and performance isn't an issue here.
!         return name.startsWith("/modules/")
!                 && !name.contains("//")
+                 && !name.contains("/./")
+                 && !name.contains("/../")
+                 && !name.endsWith("/")
+                 && !name.endsWith("/.")
+                 && !name.endsWith("/..");
      }
  
!     // initialize the root /modules, /packages, and the symbolic link Nodes
      private void initNodes() throws IOException {
          // same package prefix may exist in multiple modules. This Map
          // is filled by walking "jdk modules" directory recursively!
          Map<String, List<String>> packageToModules = new HashMap<>();
!         List<PathNode> modules = new ArrayList<>();
!         try (DirectoryStream<Path> stream = Files.newDirectoryStream(modulesDir, Files::isDirectory)) {
!             for (Path moduleDir : stream) {
!                 modules.add(findPackagesAndCreateModuleNode(moduleDir, packageToModules));
              }
          }
          // create "/modules" directory
!         PathNode modulesRootNode = new PathNode("/modules", modules);
          nodes.put(modulesRootNode.getName(), modulesRootNode);
  
          // create children under "/packages"
!         List<PathNode> packagesChildren = new ArrayList<>(packageToModules.size());
          for (Map.Entry<String, List<String>> entry : packageToModules.entrySet()) {
              String pkgName = entry.getKey();
              List<String> moduleNameList = entry.getValue();
!             List<PathNode> moduleLinkNodes = new ArrayList<>(moduleNameList.size());
              for (String moduleName : moduleNameList) {
!                 PathNode moduleNode = Objects.requireNonNull(nodes.get(MODULES + moduleName));
                  PathNode linkNode = new PathNode(PACKAGES + pkgName + "/" + moduleName, moduleNode);
                  nodes.put(linkNode.getName(), linkNode);
                  moduleLinkNodes.add(linkNode);
              }
              PathNode pkgDir = new PathNode(PACKAGES + pkgName, moduleLinkNodes);

*** 300,12 ***
          // "/packages" dir
          PathNode packagesRootNode = new PathNode("/packages", packagesChildren);
          nodes.put(packagesRootNode.getName(), packagesRootNode);
  
          // finally "/" dir!
!         List<Node> rootChildren = new ArrayList<>();
          rootChildren.add(packagesRootNode);
          rootChildren.add(modulesRootNode);
          PathNode root = new PathNode("/", rootChildren);
          nodes.put(root.getName(), root);
      }
  }
--- 383,42 ---
          // "/packages" dir
          PathNode packagesRootNode = new PathNode("/packages", packagesChildren);
          nodes.put(packagesRootNode.getName(), packagesRootNode);
  
          // finally "/" dir!
!         List<PathNode> rootChildren = new ArrayList<>();
          rootChildren.add(packagesRootNode);
          rootChildren.add(modulesRootNode);
          PathNode root = new PathNode("/", rootChildren);
          nodes.put(root.getName(), root);
      }
+ 
+     private PathNode findPackagesAndCreateModuleNode(Path moduleDir, Map<String, List<String>> packageToModules)
+             throws IOException {
+         String moduleName = moduleDir.getFileName().toString();
+         UnaryOperator<Path> previewExtractor = isPreviewMode
+                 ? (p -> p.startsWith(PREVIEW_DIR) ? PREVIEW_DIR.relativize(p) : p)
+                 : UnaryOperator.identity();
+         try (Stream<Path> contentsStream = Files.find(moduleDir, Integer.MAX_VALUE, (path, attr) -> attr.isDirectory())) {
+             contentsStream
+                     .map(moduleDir::relativize)
+                     // When in preview mode, map paths inside preview directory
+                     // to non-preview versions.
+                     .map(previewExtractor)
+                     // Ignore the special META-INF directory (including
+                     // unextracted preview).
+                     .filter(p -> !p.startsWith(META_INF_DIR))
+                     // Extract unique package names.
+                     .map(str -> StreamSupport.stream(str.spliterator(), false)
+                             .map(Path::toString)
+                             .collect(Collectors.joining(".")))
+                     // Ignore the root directories, regular or preview
+                     .filter(st -> !st.isEmpty())
+                     .distinct()
+                     .forEach(pkgName ->
+                             packageToModules
+                                     .computeIfAbsent(pkgName, k -> new ArrayList<>())
+                                     .add(moduleName));
+         }
+         return Objects.requireNonNull(createPathInModulesNodeIfValid(MODULES + moduleName));
+     }
  }
< prev index next >