1 /* 2 * Copyright (c) 2015, 2018, 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 26 package jdk.nio.zipfs; 27 28 import java.io.IOException; 29 import java.io.InputStream; 30 import java.lang.Runtime.Version; 31 import java.nio.file.NoSuchFileException; 32 import java.nio.file.Path; 33 import java.util.Arrays; 34 import java.util.HashMap; 35 import java.util.Map; 36 import java.util.TreeMap; 37 import java.util.function.Consumer; 38 import java.util.function.Function; 39 import java.util.jar.Attributes; 40 import java.util.jar.Manifest; 41 42 /** 43 * Adds aliasing to ZipFileSystem to support multi-release jar files. An alias map 44 * is created by {@link JarFileSystem#createVersionedLinks(int)}. The map is then 45 * consulted when an entry is looked up in {@link JarFileSystem#getEntry(byte[])} 46 * to determine if the entry has a corresponding versioned entry. If so, the 47 * versioned entry is returned. 48 * 49 * @author Steve Drach 50 */ 51 class JarFileSystem extends ZipFileSystem { 52 private Function<byte[],byte[]> lookup; 53 54 @Override 55 IndexNode getInode(byte[] path) { 56 // check for an alias to a versioned entry 57 byte[] versionedPath = lookup.apply(path); 58 return versionedPath == null ? super.getInode(path) : super.getInode(versionedPath); 59 } 60 61 JarFileSystem(ZipFileSystemProvider provider, Path zfpath, Map<String,?> env) 62 throws IOException { 63 super(provider, zfpath, env); 64 lookup = path -> path; // lookup needs to be set before isMultiReleaseJar is called 65 // because it eventually calls getEntry 66 if (isMultiReleaseJar()) { 67 int version; 68 Object o = env.get("multi-release"); 69 if (o instanceof String) { 70 String s = (String)o; 71 if (s.equals("runtime")) { 72 version = Runtime.version().feature(); 73 } else { 74 version = Integer.parseInt(s); 75 } 76 } else if (o instanceof Integer) { 77 version = (Integer)o; 78 } else if (o instanceof Version) { 79 version = ((Version)o).feature(); 80 } else { 81 throw new IllegalArgumentException("env parameter must be String, Integer, " 82 + "or Version"); 83 } 84 lookup = createVersionedLinks(version < 0 ? 0 : version); 85 setReadOnly(); 86 } 87 } 88 89 private boolean isMultiReleaseJar() throws IOException { 90 try (InputStream is = newInputStream(getBytes("/META-INF/MANIFEST.MF"))) { 91 String multiRelease = new Manifest(is).getMainAttributes() 92 .getValue(Attributes.Name.MULTI_RELEASE); 93 return "true".equalsIgnoreCase(multiRelease); 94 } catch (NoSuchFileException x) { 95 return false; 96 } 97 } 98 99 /** 100 * create a map of aliases for versioned entries, for example: 101 * version/PackagePrivate.class -> META-INF/versions/9/version/PackagePrivate.class 102 * version/PackagePrivate.java -> META-INF/versions/9/version/PackagePrivate.java 103 * version/Version.class -> META-INF/versions/10/version/Version.class 104 * version/Version.java -> META-INF/versions/10/version/Version.java 105 * 106 * then wrap the map in a function that getEntry can use to override root 107 * entry lookup for entries that have corresponding versioned entries 108 */ 109 private Function<byte[],byte[]> createVersionedLinks(int version) { 110 HashMap<IndexNode,byte[]> aliasMap = new HashMap<>(); 111 IndexNode verdir = getInode(getBytes("/META-INF/versions")); 112 if (verdir != null) { 113 getVersionMap(version, verdir).values() 114 .forEach(versionNode -> { // for each META-INF/versions/{n} directory 115 // put all the leaf inodes, i.e. entries, into the alias map 116 // possibly shadowing lower versioned entries 117 walk(versionNode, entryNode -> { 118 byte[] rootName = getRootName(versionNode, entryNode); 119 if (rootName != null) { 120 IndexNode rootNode = getInode(rootName); 121 if (rootNode == null) { // no matching root node, make a virtual one 122 rootNode = IndexNode.keyOf(rootName); 123 } 124 aliasMap.put(rootNode, entryNode.name); 125 } 126 }); 127 }); 128 } 129 return path -> aliasMap.get(IndexNode.keyOf(path)); 130 } 131 132 /** 133 * create a sorted version map of version -> inode, for inodes <= max version 134 * 9 -> META-INF/versions/9 135 * 10 -> META-INF/versions/10 136 */ 137 private TreeMap<Integer, IndexNode> getVersionMap(int version, IndexNode metaInfVersions) { 138 TreeMap<Integer,IndexNode> map = new TreeMap<>(); 139 IndexNode child = metaInfVersions.child; 140 while (child != null) { 141 Integer key = getVersion(child.name, metaInfVersions.name.length + 1); 142 if (key != null && key <= version) { 143 map.put(key, child); 144 } 145 child = child.sibling; 146 } 147 return map; 148 } 149 150 /** 151 * extract the integer version number -- META-INF/versions/9 returns 9 152 */ 153 private Integer getVersion(byte[] name, int offset) { 154 try { 155 return Integer.parseInt(getString(Arrays.copyOfRange(name, offset, name.length))); 156 } catch (NumberFormatException x) { 157 // ignore this even though it might indicate issues with the JAR structure 158 return null; 159 } 160 } 161 162 /** 163 * walk the IndexNode tree processing all leaf nodes 164 */ 165 private void walk(IndexNode inode, Consumer<IndexNode> process) { 166 if (inode == null) return; 167 if (inode.isDir()) { 168 walk(inode.child, process); 169 } else { 170 process.accept(inode); 171 } 172 walk(inode.sibling, process); 173 } 174 175 /** 176 * extract the root name from a versioned entry name 177 * given inode for META-INF/versions/9/foo/bar.class 178 * and prefix META-INF/versions/9/ 179 * returns foo/bar.class 180 */ 181 private byte[] getRootName(IndexNode prefix, IndexNode inode) { 182 int offset = prefix.name.length; 183 byte[] fullName = inode.name; 184 return Arrays.copyOfRange(fullName, offset, fullName.length); 185 } 186 }