/* * Copyright (c) 2015, 2016, 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 * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package jdk.nio.zipfs; import java.io.IOException; import java.io.InputStream; import java.lang.Runtime.Version; import java.nio.file.Path; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; import java.util.function.Consumer; import java.util.function.Function; import java.util.jar.Attributes; import java.util.jar.Manifest; /** * Adds aliasing to ZipFileSystem to support multi-release jar files. An alias map * is created by {@link JarFileSystem#createVersionedLinks(int)}. The map is then * consulted when an entry is looked up in {@link JarFileSystem#getEntry(byte[])} * to determine if the entry has a corresponding versioned entry. If so, the * versioned entry is returned. * * @author Steve Drach */ class JarFileSystem extends ZipFileSystem { private Function lookup; @Override IndexNode getInode(byte[] path) { // check for an alias to a versioned entry byte[] versionedPath = lookup.apply(path); return versionedPath == null ? super.getInode(path) : super.getInode(versionedPath); } JarFileSystem(ZipFileSystemProvider provider, Path zfpath, Map env) throws IOException { super(provider, zfpath, env); lookup = path -> path; // lookup needs to be set before isMultiReleaseJar is called // because it eventually calls getEntry if (isMultiReleaseJar()) { int version; Object o = env.get("multi-release"); if (o instanceof String) { String s = (String)o; if (s.equals("runtime")) { version = Runtime.version().major(); } else { version = Integer.parseInt(s); } } else if (o instanceof Integer) { version = (Integer)o; } else if (o instanceof Version) { version = ((Version)o).major(); } else { throw new IllegalArgumentException("env parameter must be String, Integer, " + "or Version"); } lookup = createVersionedLinks(version < 0 ? 0 : version); setReadOnly(); } } private boolean isMultiReleaseJar() { try (InputStream is = newInputStream(getBytes("/META-INF/MANIFEST.MF"))) { byte[] b = is.readAllBytes(); // Keep this implementation up to date with // JarFile::checkForSpecialAttributes int i = match(MULTIRELEASE_CHARS, b, MULTIRELEASE_LASTOCC, MULTIRELEASE_OPTOSFT); if (i != -1) { i += MULTIRELEASE_CHARS.length; if (i < b.length) { byte c = b[i++]; // Check that the value is followed by a newline // and does not have a continuation if (c == '\n' && (i == b.length || b[i] != ' ')) { return true; } else if (c == '\r') { if (i == b.length) { return true; } else { c = b[i++]; if (c == '\n') { if (i == b.length || b[i] != ' ') { return true; } } else if (c != ' ') { return true; } } } } } return false; } catch (IOException x) { return false; } } private static final byte[] MULTIRELEASE_CHARS = {'M','U','L','T','I','-','R','E','L','E', 'A', 'S', 'E', ':', ' ', 'T', 'R', 'U', 'E'}; // The bad character shift for "multi-release: true" private static final byte[] MULTIRELEASE_LASTOCC; // The good suffix shift for "multi-release: true" private static final byte[] MULTIRELEASE_OPTOSFT; static { MULTIRELEASE_LASTOCC = new byte[64]; MULTIRELEASE_OPTOSFT = new byte[19]; MULTIRELEASE_LASTOCC[(int)'M' - 32] = 1; MULTIRELEASE_LASTOCC[(int)'I' - 32] = 5; MULTIRELEASE_LASTOCC[(int)'-' - 32] = 6; MULTIRELEASE_LASTOCC[(int)'L' - 32] = 9; MULTIRELEASE_LASTOCC[(int)'A' - 32] = 11; MULTIRELEASE_LASTOCC[(int)'S' - 32] = 12; MULTIRELEASE_LASTOCC[(int)':' - 32] = 14; MULTIRELEASE_LASTOCC[(int)' ' - 32] = 15; MULTIRELEASE_LASTOCC[(int)'T' - 32] = 16; MULTIRELEASE_LASTOCC[(int)'R' - 32] = 17; MULTIRELEASE_LASTOCC[(int)'U' - 32] = 18; MULTIRELEASE_LASTOCC[(int)'E' - 32] = 19; for (int i = 0; i < 17; i++) { MULTIRELEASE_OPTOSFT[i] = 19; } MULTIRELEASE_OPTOSFT[17] = 6; MULTIRELEASE_OPTOSFT[18] = 1; } /** * Returns true if the pattern {@code src} is found in {@code b}. * The {@code lastOcc} array is the precomputed bad character shifts. * Since there are no repeated substring in our search strings, * the good suffix shifts can be replaced with a comparison. */ private static int match(byte[] src, byte[] b, byte[] lastOcc, byte[] optoSft) { int len = src.length; int last = b.length - len; int i = 0; next: while (i <= last) { for (int j = (len - 1); j >= 0; j--) { byte c = b[i + j]; if (c >= ' ' && c <= 'z') { if (c >= 'a') c -= 32; // Canonicalize if (c != src[j]) { // no match int badShift = lastOcc[c - 32]; i += Math.max(j + 1 - badShift, optoSft[j]); continue next; } } else { // no match, character not valid for name i += len; continue next; } } return i; } return -1; } /** * create a map of aliases for versioned entries, for example: * version/PackagePrivate.class -> META-INF/versions/9/version/PackagePrivate.class * version/PackagePrivate.java -> META-INF/versions/9/version/PackagePrivate.java * version/Version.class -> META-INF/versions/10/version/Version.class * version/Version.java -> META-INF/versions/10/version/Version.java * * then wrap the map in a function that getEntry can use to override root * entry lookup for entries that have corresponding versioned entries */ private Function createVersionedLinks(int version) { HashMap aliasMap = new HashMap<>(); getVersionMap(version, getInode(getBytes("/META-INF/versions"))).values() .forEach(versionNode -> { // for each META-INF/versions/{n} directory // put all the leaf inodes, i.e. entries, into the alias map // possibly shadowing lower versioned entries walk(versionNode, entryNode -> { byte[] rootName = getRootName(versionNode, entryNode); if (rootName != null) { IndexNode rootNode = getInode(rootName); if (rootNode == null) { // no matching root node, make a virtual one rootNode = IndexNode.keyOf(rootName); } aliasMap.put(rootNode, entryNode.name); } }); }); return path -> aliasMap.get(IndexNode.keyOf(path)); } /** * create a sorted version map of version -> inode, for inodes <= max version * 9 -> META-INF/versions/9 * 10 -> META-INF/versions/10 */ private TreeMap getVersionMap(int version, IndexNode metaInfVersions) { TreeMap map = new TreeMap<>(); IndexNode child = metaInfVersions.child; while (child != null) { Integer key = getVersion(child.name, metaInfVersions.name.length + 1); if (key != null && key <= version) { map.put(key, child); } child = child.sibling; } return map; } /** * extract the integer version number -- META-INF/versions/9 returns 9 */ private Integer getVersion(byte[] name, int offset) { try { return Integer.parseInt(getString(Arrays.copyOfRange(name, offset, name.length))); } catch (NumberFormatException x) { // ignore this even though it might indicate issues with the JAR structure return null; } } /** * walk the IndexNode tree processing all leaf nodes */ private void walk(IndexNode inode, Consumer process) { if (inode == null) return; if (inode.isDir()) { walk(inode.child, process); } else { process.accept(inode); } walk(inode.sibling, process); } /** * extract the root name from a versioned entry name * given inode for META-INF/versions/9/foo/bar.class * and prefix META-INF/versions/9/ * returns foo/bar.class */ private byte[] getRootName(IndexNode prefix, IndexNode inode) { int offset = prefix.name.length; byte[] fullName = inode.name; return Arrays.copyOfRange(fullName, offset, fullName.length); } }