1 /*
  2  * Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 package jdk.internal.jrtfs;
 26 
 27 import jdk.internal.jimage.ImageReader.Node;
 28 
 29 import java.io.IOException;
 30 import java.io.UncheckedIOException;
 31 import java.nio.file.DirectoryStream;
 32 import java.nio.file.FileSystemException;
 33 import java.nio.file.Files;
 34 import java.nio.file.Path;
 35 import java.nio.file.Paths;
 36 import java.nio.file.attribute.BasicFileAttributes;
 37 import java.util.ArrayList;
 38 import java.util.HashMap;
 39 import java.util.HashSet;
 40 import java.util.List;
 41 import java.util.Map;
 42 import java.util.Objects;
 43 import java.util.Set;
 44 import java.util.stream.Stream;
 45 
 46 import static java.util.stream.Collectors.toList;
 47 
 48 /**
 49  * A jrt file system built on $JAVA_HOME/modules directory ('exploded modules
 50  * build')
 51  *
 52  * @implNote This class needs to maintain JDK 8 source compatibility.
 53  *
 54  * It is used internally in the JDK to implement jimage/jrtfs access,
 55  * but also compiled and delivered as part of the jrtfs.jar to support access
 56  * to the jimage file provided by the shipped JDK by tools running on JDK 8.
 57  */
 58 class ExplodedImage extends SystemImage {
 59 
 60     private static final String MODULES = "/modules/";
 61     private static final String PACKAGES = "/packages/";
 62     private static final Path META_INF_DIR = Paths.get("META-INF");
 63     private static final Path PREVIEW_DIR = META_INF_DIR.resolve("preview");
 64 
 65     private final Path modulesDir;
 66     private final boolean isPreviewMode;
 67     private final String separator;
 68     private final Map<String, PathNode> nodes = new HashMap<>();
 69     private final BasicFileAttributes modulesDirAttrs;
 70 
 71     ExplodedImage(Path modulesDir, boolean isPreviewMode) throws IOException {
 72         this.modulesDir = modulesDir;
 73         this.isPreviewMode = isPreviewMode;
 74         String str = modulesDir.getFileSystem().getSeparator();
 75         separator = str.equals("/") ? null : str;
 76         modulesDirAttrs = Files.readAttributes(modulesDir, BasicFileAttributes.class);
 77         initNodes();
 78     }
 79 
 80     // A Node that is backed by actual default file system Path
 81     private final class PathNode extends Node {
 82         // Path in underlying default file system relative to modulesDir.
 83         // In preview mode this need not correspond to the node's name.
 84         private Path relPath;
 85         private PathNode link;
 86         private List<String> childNames;
 87 
 88         /**
 89          * Creates a file based node with the given file attributes.
 90          *
 91          * <p>If the underlying path is a directory, then it is created in an
 92          * "incomplete" state, and its child names will be determined lazily.
 93          */
 94         private PathNode(String name, Path path, BasicFileAttributes attrs) {  // path
 95             super(name, attrs);
 96             this.relPath = modulesDir.relativize(path);
 97             if (relPath.isAbsolute() || relPath.getNameCount() == 0) {
 98                 throw new IllegalArgumentException("Invalid node path (must be relative): " + path);
 99             }
100         }
101 
102         /** Creates a symbolic link node to the specified target. */
103         private PathNode(String name, Node link) {              // link
104             super(name, link.getFileAttributes());
105             this.link = (PathNode)link;
106         }
107 
108         /** Creates a completed directory node based a list of child nodes. */
109         private PathNode(String name, List<PathNode> children) {    // dir
110             super(name, modulesDirAttrs);
111             this.childNames = children.stream().map(Node::getName).collect(toList());
112         }
113 
114         @Override
115         public boolean isResource() {
116             return link == null && !getFileAttributes().isDirectory();
117         }
118 
119         @Override
120         public boolean isDirectory() {
121             return childNames != null ||
122                     (link == null && getFileAttributes().isDirectory());
123         }
124 
125         @Override
126         public boolean isLink() {
127             return link != null;
128         }
129 
130         @Override
131         public PathNode resolveLink(boolean recursive) {
132             if (link == null)
133                 return this;
134             return recursive && link.isLink() ? link.resolveLink(true) : link;
135         }
136 
137         private byte[] getContent() throws IOException {
138             if (!getFileAttributes().isRegularFile())
139                 throw new FileSystemException(getName() + " is not file");
140             return Files.readAllBytes(modulesDir.resolve(relPath));
141         }
142 
143         @Override
144         public Stream<String> getChildNames() {
145             if (!isDirectory())
146                 throw new IllegalStateException("not a directory: " + getName());
147             List<String> names = childNames;
148             if (names == null) {
149                 names = completeDirectory();
150             }
151             return names.stream();
152         }
153 
154         private synchronized List<String> completeDirectory() {
155             if (childNames != null) {
156                 return childNames;
157             }
158             // Process preview nodes first, so if nodes are created they take
159             // precedence in the cache.
160             Set<String> childNameSet = new HashSet<>();
161             if (isPreviewMode && relPath.getNameCount() > 1 && !relPath.getName(1).equals(META_INF_DIR)) {
162                 Path absPreviewDir = modulesDir
163                         .resolve(relPath.getName(0))
164                         .resolve(PREVIEW_DIR)
165                         .resolve(relPath.subpath(1, relPath.getNameCount()));
166                 if (Files.exists(absPreviewDir)) {
167                     collectChildNodeNames(absPreviewDir, childNameSet);
168                 }
169             }
170             collectChildNodeNames(modulesDir.resolve(relPath), childNameSet);
171             return childNames = childNameSet.stream().sorted().collect(toList());
172         }
173 
174         private void collectChildNodeNames(Path absPath, Set<String> childNameSet) {
175             try (DirectoryStream<Path> stream = Files.newDirectoryStream(absPath)) {
176                 for (Path p : stream) {
177                     PathNode node = (PathNode) findNode(getName() + "/" + p.getFileName().toString());
178                     if (node != null) {  // findNode may choose to hide certain files!
179                         childNameSet.add(node.getName());
180                     }
181                 }
182             } catch (IOException ex) {
183                 throw new UncheckedIOException(ex);
184             }
185         }
186 
187         @Override
188         public long size() {
189             try {
190                 return isDirectory() ? 0 : Files.size(modulesDir.resolve(relPath));
191             } catch (IOException ex) {
192                 throw new UncheckedIOException(ex);
193             }
194         }
195     }
196 
197     @Override
198     public synchronized void close() throws IOException {
199         nodes.clear();
200     }
201 
202     @Override
203     public byte[] getResource(Node node) throws IOException {
204         return ((PathNode)node).getContent();
205     }
206 
207     @Override
208     public synchronized Node findNode(String name) {
209         PathNode node = nodes.get(name);
210         if (node != null) {
211             return node;
212         }
213         // If null, this was not the name of "/modules/..." node, and since all
214         // "/packages/..." nodes were created and cached in advance, the name
215         // cannot reference a valid node.
216         Path path = underlyingModulesPath(name);
217         if (path == null) {
218             return null;
219         }
220         // This can still return null for hidden files.
221         return createModulesNode(name, path);
222     }
223 
224     /**
225      * Returns the expected file path for name in the "/modules/..." namespace,
226      * or {@code null} if the name is not in the "/modules/..." namespace or the
227      * path does not reference a file.
228      */
229     private Path underlyingModulesPath(String name) {
230         if (!isNonEmptyModulesName(name)) {
231             return null;
232         }
233         String relName = name.substring(MODULES.length());
234         Path relPath = Paths.get(frontSlashToNativeSlash(relName));
235         // The first path segment must exist due to check above.
236         Path modDir = relPath.getName(0);
237         Path previewDir = modDir.resolve(PREVIEW_DIR);
238         if (relPath.startsWith(previewDir)) {
239             return null;
240         }
241         Path path = modulesDir.resolve(relPath);
242         // Non-preview directories take precedence.
243         if (Files.isDirectory(path)) {
244             return path;
245         }
246         // Otherwise prefer preview resources over non-preview ones.
247         if (isPreviewMode
248                 && relPath.getNameCount() > 1
249                 && !modDir.equals(META_INF_DIR)) {
250             Path previewPath = modulesDir
251                     .resolve(previewDir)
252                     .resolve(relPath.subpath(1, relPath.getNameCount()));
253             if (Files.exists(previewPath)) {
254                 return previewPath;
255             }
256         }
257         return Files.exists(path) ? path : null;
258     }
259 
260     /**
261      * Lazily creates and caches a {@code Node} for the given "/modules/..." name
262      * and corresponding path to a file or directory.
263      *
264      * @param name a resource or directory node name, of the form "/modules/...".
265      * @param path the path of a file for a resource or directory.
266      * @return the newly created and cached node, or {@code null} if the given
267      *     path references a file which must be hidden in the node hierarchy.
268      */
269     private PathNode createModulesNode(String name, Path path) {
270         assert !nodes.containsKey(name) : "Node must not already exist: " + name;
271         assert isNonEmptyModulesName(name) : "Invalid modules name: " + name;
272 
273         try {
274             // We only know if we're creating a resource of directory when we
275             // look up file attributes, and we only do that once. Thus, we can
276             // only reject "marker files" here, rather than by inspecting the
277             // given name string, since it doesn't apply to directories.
278             BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
279             if (attrs.isRegularFile()) {
280                 Path f = path.getFileName();
281                 if (f.toString().startsWith("_the.")) {
282                     return null;
283                 }
284             } else if (!attrs.isDirectory()) {
285                 return null;
286             }
287             PathNode node = new PathNode(name, path, attrs);
288             nodes.put(name, node);
289             return node;
290         } catch (IOException x) {
291             // Since the path references a file errors should not be ignored.
292             throw new UncheckedIOException(x);
293         }
294     }
295 
296     private static boolean isNonEmptyModulesName(String name) {
297         // Don't just check the prefix, there must be something after it too
298         // (otherwise you end up with an empty string after trimming).
299         // Also make sure we can't be tricked by "/modules//absolute/path" or
300         // "/modules/../../escaped/path".
301         // Don't use regex as 'name' is untrusted (avoids stack overflow risk)
302         // and performance isn't an issue here.
303         return name.startsWith("/modules/")
304                 && !name.contains("//")
305                 && !name.contains("/./")
306                 && !name.contains("/../")
307                 && !name.endsWith("/")
308                 && !name.endsWith("/.")
309                 && !name.endsWith("/..");
310     }
311 
312     // convert "/" to platform path separator
313     private String frontSlashToNativeSlash(String str) {
314         return separator == null ? str : str.replace("/", separator);
315     }
316 
317     // convert "/"s to "."s
318     private String slashesToDots(String str) {
319         return str.replace(separator != null ? separator : "/", ".");
320     }
321 
322     // initialize file system Nodes
323     private void initNodes() throws IOException {
324         // same package prefix may exist in multiple modules. This Map
325         // is filled by walking "jdk modules" directory recursively!
326         Map<String, List<String>> packageToModules = new HashMap<>();
327         try (DirectoryStream<Path> stream = Files.newDirectoryStream(modulesDir)) {
328             for (Path moduleDir : stream) {
329                 if (Files.isDirectory(moduleDir)) {
330                     processModuleDirectory(moduleDir, packageToModules);
331                 }
332             }
333         }
334         // create "/modules" directory
335         // "nodes" map contains only /modules/<foo> nodes only so far and so add all as children of /modules
336         PathNode modulesRootNode = new PathNode("/modules", new ArrayList<>(nodes.values()));
337         nodes.put(modulesRootNode.getName(), modulesRootNode);
338 
339         // create children under "/packages"
340         List<PathNode> packagesChildren = new ArrayList<>(packageToModules.size());
341         for (Map.Entry<String, List<String>> entry : packageToModules.entrySet()) {
342             String pkgName = entry.getKey();
343             List<String> moduleNameList = entry.getValue();
344             List<PathNode> moduleLinkNodes = new ArrayList<>(moduleNameList.size());
345             for (String moduleName : moduleNameList) {
346                 Node moduleNode = Objects.requireNonNull(nodes.get(MODULES + moduleName));
347                 PathNode linkNode = new PathNode(PACKAGES + pkgName + "/" + moduleName, moduleNode);
348                 nodes.put(linkNode.getName(), linkNode);
349                 moduleLinkNodes.add(linkNode);
350             }
351             PathNode pkgDir = new PathNode(PACKAGES + pkgName, moduleLinkNodes);
352             nodes.put(pkgDir.getName(), pkgDir);
353             packagesChildren.add(pkgDir);
354         }
355         // "/packages" dir
356         PathNode packagesRootNode = new PathNode("/packages", packagesChildren);
357         nodes.put(packagesRootNode.getName(), packagesRootNode);
358 
359         // finally "/" dir!
360         List<PathNode> rootChildren = new ArrayList<>();
361         rootChildren.add(packagesRootNode);
362         rootChildren.add(modulesRootNode);
363         PathNode root = new PathNode("/", rootChildren);
364         nodes.put(root.getName(), root);
365     }
366 
367     private void processModuleDirectory(Path moduleDir, Map<String, List<String>> packageToModules)
368             throws IOException {
369         String moduleName = moduleDir.getFileName().toString();
370         // Make sure "/modules/<moduleName>" is created
371         Objects.requireNonNull(createModulesNode(MODULES + moduleName, moduleDir));
372         // Skip the first path (it's always the given root directory).
373         try (Stream<Path> contentsStream = Files.walk(moduleDir).skip(1)) {
374             contentsStream
375                     // Non-empty relative directory paths inside each module.
376                     .filter(Files::isDirectory)
377                     .map(moduleDir::relativize)
378                     // Map paths inside preview directory to non-preview versions.
379                     .filter(p -> isPreviewMode || !p.startsWith(PREVIEW_DIR))
380                     .map(p -> isPreviewSubpath(p) ? PREVIEW_DIR.relativize(p) : p)
381                     // Ignore special META-INF directory (including preview directory itself).
382                     .filter(p -> !p.startsWith(META_INF_DIR))
383                     // Extract unique package names.
384                     .map(p -> slashesToDots(p.toString()))
385                     .distinct()
386                     .forEach(pkgName ->
387                             packageToModules
388                                     .computeIfAbsent(pkgName, k -> new ArrayList<>())
389                                     .add(moduleName));
390         }
391     }
392 
393     private static boolean isPreviewSubpath(Path p) {
394         return p.startsWith(PREVIEW_DIR) && p.getNameCount() > PREVIEW_DIR.getNameCount();
395     }
396 }