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