Path: blob/master/src/jdk.jartool/share/classes/sun/tools/jar/Main.java
66646 views
/*1* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jar;2627import java.io.*;28import java.lang.module.Configuration;29import java.lang.module.FindException;30import java.lang.module.InvalidModuleDescriptorException;31import java.lang.module.ModuleDescriptor;32import java.lang.module.ModuleDescriptor.Exports;33import java.lang.module.ModuleDescriptor.Opens;34import java.lang.module.ModuleDescriptor.Provides;35import java.lang.module.ModuleDescriptor.Version;36import java.lang.module.ModuleFinder;37import java.lang.module.ModuleReader;38import java.lang.module.ModuleReference;39import java.lang.module.ResolvedModule;40import java.net.URI;41import java.nio.ByteBuffer;42import java.nio.file.Files;43import java.nio.file.Path;44import java.nio.file.Paths;45import java.nio.file.StandardCopyOption;46import java.text.MessageFormat;47import java.util.*;48import java.util.function.Consumer;49import java.util.jar.Attributes;50import java.util.jar.JarFile;51import java.util.jar.JarOutputStream;52import java.util.jar.Manifest;53import java.util.regex.Pattern;54import java.util.stream.Collectors;55import java.util.stream.Stream;56import java.util.zip.CRC32;57import java.util.zip.ZipEntry;58import java.util.zip.ZipFile;59import java.util.zip.ZipInputStream;60import java.util.zip.ZipOutputStream;61import java.util.concurrent.TimeUnit;62import jdk.internal.module.Checks;63import jdk.internal.module.ModuleHashes;64import jdk.internal.module.ModuleHashesBuilder;65import jdk.internal.module.ModuleInfo;66import jdk.internal.module.ModuleInfoExtender;67import jdk.internal.module.ModuleResolution;68import jdk.internal.module.ModuleTarget;69import jdk.internal.util.jar.JarIndex;70import java.time.LocalDateTime;71import java.time.ZoneOffset;7273import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;74import static java.util.jar.JarFile.MANIFEST_NAME;75import static java.util.stream.Collectors.joining;76import static jdk.internal.util.jar.JarIndex.INDEX_NAME;7778/**79* This class implements a simple utility for creating files in the JAR80* (Java Archive) file format. The JAR format is based on the ZIP file81* format, with optional meta-information stored in a MANIFEST entry.82*/83public class Main {84String program;85PrintWriter out, err;86String fname, mname, ename;87String zname = "";88String rootjar = null;8990private static final int BASE_VERSION = 0;9192private static class Entry {93final String name;94final File file;95final boolean isDir;9697Entry(File file, String name, boolean isDir) {98this.file = file;99this.isDir = isDir;100this.name = name;101}102103@Override104public boolean equals(Object o) {105if (this == o) return true;106if (!(o instanceof Entry)) return false;107return this.file.equals(((Entry)o).file);108}109110@Override111public int hashCode() {112return file.hashCode();113}114}115116// An entryName(path)->Entry map generated during "expand", it helps to117// decide whether or not an existing entry in a jar file needs to be118// replaced, during the "update" operation.119Map<String, Entry> entryMap = new HashMap<>();120121// All entries need to be added/updated.122Set<Entry> entries = new LinkedHashSet<>();123124// module-info.class entries need to be added/updated.125Map<String,byte[]> moduleInfos = new HashMap<>();126127// A paths Set for each version, where each Set contains directories128// specified by the "-C" operation.129Map<Integer,Set<String>> pathsMap = new HashMap<>();130131// There's also a files array per version132// base version is the first entry and then follow with the version given133// from the --release option in the command-line order.134// The value of each entry is the files given in the command-line order.135Map<Integer,String[]> filesMap = new LinkedHashMap<>();136137// Do we think this is a multi-release jar? Set to true138// if --release option found followed by at least file139boolean isMultiRelease;140141// The last parsed --release value, if any. Used in conjunction with142// "-d,--describe-module" to select the operative module descriptor.143int releaseValue = -1;144145/*146* cflag: create147* uflag: update148* xflag: xtract149* tflag: table150* vflag: verbose151* flag0: no zip compression (store only)152* Mflag: DO NOT generate a manifest file (just ZIP)153* iflag: generate jar index154* nflag: Perform jar normalization at the end155* pflag: preserve/don't strip leading slash and .. component from file name156* dflag: print module descriptor157*/158boolean cflag, uflag, xflag, tflag, vflag, flag0, Mflag, iflag, pflag, dflag, validate;159160boolean suppressDeprecateMsg = false;161162/* To support additional GNU Style informational options */163Consumer<PrintWriter> info;164165/* Modular jar related options */166Version moduleVersion;167Pattern modulesToHash;168ModuleResolution moduleResolution = ModuleResolution.empty();169ModuleFinder moduleFinder = ModuleFinder.of();170171static final String MODULE_INFO = "module-info.class";172static final String MANIFEST_DIR = "META-INF/";173static final String VERSIONS_DIR = MANIFEST_DIR + "versions/";174static final String VERSION = "1.0";175static final int VERSIONS_DIR_LENGTH = VERSIONS_DIR.length();176private static ResourceBundle rsrc;177178/* Date option for entry timestamps resolved to UTC Local time */179LocalDateTime date;180181/**182* If true, maintain compatibility with JDK releases prior to 6.0 by183* timestamping extracted files with the time at which they are extracted.184* Default is to use the time given in the archive.185*/186private static final boolean useExtractionTime =187Boolean.getBoolean("sun.tools.jar.useExtractionTime");188189/**190* Initialize ResourceBundle191*/192static {193try {194rsrc = ResourceBundle.getBundle("sun.tools.jar.resources.jar");195} catch (MissingResourceException e) {196throw new Error("Fatal: Resource for jar is missing");197}198}199200static String getMsg(String key) {201try {202return (rsrc.getString(key));203} catch (MissingResourceException e) {204throw new Error("Error in message file", e);205}206}207208static String formatMsg(String key, String arg) {209String msg = getMsg(key);210String[] args = new String[1];211args[0] = arg;212return MessageFormat.format(msg, (Object[]) args);213}214215static String formatMsg2(String key, String arg, String arg1) {216String msg = getMsg(key);217String[] args = new String[2];218args[0] = arg;219args[1] = arg1;220return MessageFormat.format(msg, (Object[]) args);221}222223public Main(PrintStream out, PrintStream err, String program) {224this.out = new PrintWriter(out, true);225this.err = new PrintWriter(err, true);226this.program = program;227}228229public Main(PrintWriter out, PrintWriter err, String program) {230this.out = out;231this.err = err;232this.program = program;233}234235/**236* Creates a new empty temporary file in the same directory as the237* specified file. A variant of File.createTempFile.238*/239private static File createTempFileInSameDirectoryAs(File file)240throws IOException {241File dir = file.getParentFile();242if (dir == null)243dir = new File(".");244return File.createTempFile("jartmp", null, dir);245}246247private boolean ok;248249/**250* Starts main program with the specified arguments.251*/252@SuppressWarnings({"removal"})253public synchronized boolean run(String args[]) {254ok = true;255if (!parseArgs(args)) {256return false;257}258File tmpFile = null;259try {260if (cflag || uflag) {261if (fname != null) {262// The name of the zip file as it would appear as its own263// zip file entry. We use this to make sure that we don't264// add the zip file to itself.265zname = fname.replace(File.separatorChar, '/');266if (zname.startsWith("./")) {267zname = zname.substring(2);268}269}270}271if (cflag) {272Manifest manifest = null;273if (!Mflag) {274if (mname != null) {275try (InputStream in = new FileInputStream(mname)) {276manifest = new Manifest(new BufferedInputStream(in));277}278} else {279manifest = new Manifest();280}281addVersion(manifest);282addCreatedBy(manifest);283if (isAmbiguousMainClass(manifest)) {284return false;285}286if (ename != null) {287addMainClass(manifest, ename);288}289if (isMultiRelease) {290addMultiRelease(manifest);291}292}293expand();294if (!moduleInfos.isEmpty()) {295// All actual file entries (excl manifest and module-info.class)296Set<String> jentries = new HashSet<>();297// all packages if it's a class or resource298Set<String> packages = new HashSet<>();299entries.stream()300.filter(e -> !e.isDir)301.forEach( e -> {302addPackageIfNamed(packages, e.name);303jentries.add(e.name);304});305addExtendedModuleAttributes(moduleInfos, packages);306307// Basic consistency checks for modular jars.308if (!checkModuleInfo(moduleInfos.get(MODULE_INFO), jentries))309return false;310311} else if (moduleVersion != null || modulesToHash != null) {312error(getMsg("error.module.options.without.info"));313return false;314}315if (vflag && fname == null) {316// Disable verbose output so that it does not appear317// on stdout along with file data318// error("Warning: -v option ignored");319vflag = false;320}321final String tmpbase = (fname == null)322? "tmpjar"323: fname.substring(fname.indexOf(File.separatorChar) + 1);324325tmpFile = createTemporaryFile(tmpbase, ".jar");326try (OutputStream out = new FileOutputStream(tmpFile)) {327create(new BufferedOutputStream(out, 4096), manifest);328}329validateAndClose(tmpFile);330} else if (uflag) {331File inputFile = null;332if (fname != null) {333inputFile = new File(fname);334tmpFile = createTempFileInSameDirectoryAs(inputFile);335} else {336vflag = false;337tmpFile = createTemporaryFile("tmpjar", ".jar");338}339expand();340try (FileInputStream in = (fname != null) ? new FileInputStream(inputFile)341: new FileInputStream(FileDescriptor.in);342FileOutputStream out = new FileOutputStream(tmpFile);343InputStream manifest = (!Mflag && (mname != null)) ?344(new FileInputStream(mname)) : null;345) {346boolean updateOk = update(in, new BufferedOutputStream(out),347manifest, moduleInfos, null);348if (ok) {349ok = updateOk;350}351}352validateAndClose(tmpFile);353} else if (tflag) {354replaceFSC(filesMap);355// For the "list table contents" action, access using the356// ZipFile class is always most efficient since only a357// "one-finger" scan through the central directory is required.358String[] files = filesMapToFiles(filesMap);359if (fname != null) {360list(fname, files);361} else {362InputStream in = new FileInputStream(FileDescriptor.in);363try {364list(new BufferedInputStream(in), files);365} finally {366in.close();367}368}369} else if (xflag) {370replaceFSC(filesMap);371// For the extract action, when extracting all the entries,372// access using the ZipInputStream class is most efficient,373// since only a single sequential scan through the zip file is374// required. When using the ZipFile class, a "two-finger" scan375// is required, but this is likely to be more efficient when a376// partial extract is requested. In case the zip file has377// "leading garbage", we fall back from the ZipInputStream378// implementation to the ZipFile implementation, since only the379// latter can handle it.380381String[] files = filesMapToFiles(filesMap);382if (fname != null && files != null) {383extract(fname, files);384} else {385InputStream in = (fname == null)386? new FileInputStream(FileDescriptor.in)387: new FileInputStream(fname);388try {389if (!extract(new BufferedInputStream(in), files) && fname != null) {390extract(fname, files);391}392} finally {393in.close();394}395}396} else if (iflag) {397String[] files = filesMap.get(BASE_VERSION); // base entries only, can be null398genIndex(rootjar, files);399} else if (dflag) {400boolean found;401if (fname != null) {402try (ZipFile zf = new ZipFile(fname)) {403found = describeModule(zf);404}405} else {406try (FileInputStream fin = new FileInputStream(FileDescriptor.in)) {407found = describeModuleFromStream(fin);408}409}410if (!found)411error(getMsg("error.module.descriptor.not.found"));412} else if (validate) {413File file;414if (fname != null) {415file = new File(fname);416} else {417file = createTemporaryFile("tmpJar", ".jar");418try (InputStream in = new FileInputStream(FileDescriptor.in)) {419Files.copy(in, file.toPath());420}421}422ok = validateJar(file);423}424} catch (IOException e) {425fatalError(e);426ok = false;427} catch (Error ee) {428ee.printStackTrace();429ok = false;430} catch (Throwable t) {431t.printStackTrace();432ok = false;433} finally {434if (tmpFile != null && tmpFile.exists())435tmpFile.delete();436}437out.flush();438err.flush();439return ok;440}441442private boolean validateJar(File file) throws IOException {443try (ZipFile zf = new ZipFile(file)) {444return Validator.validate(this, zf);445} catch (IOException e) {446error(formatMsg2("error.validator.jarfile.exception", fname, e.getMessage()));447return true;448}449}450451private void validateAndClose(File tmpfile) throws IOException {452if (ok && isMultiRelease) {453ok = validateJar(tmpfile);454if (!ok) {455error(formatMsg("error.validator.jarfile.invalid", fname));456}457}458Path path = tmpfile.toPath();459try {460if (ok) {461if (fname != null) {462Files.move(path, Paths.get(fname), StandardCopyOption.REPLACE_EXISTING);463} else {464Files.copy(path, new FileOutputStream(FileDescriptor.out));465}466}467} finally {468Files.deleteIfExists(path);469}470}471472private String[] filesMapToFiles(Map<Integer,String[]> filesMap) {473if (filesMap.isEmpty()) return null;474return filesMap.entrySet()475.stream()476.flatMap(this::filesToEntryNames)477.toArray(String[]::new);478}479480Stream<String> filesToEntryNames(Map.Entry<Integer,String[]> fileEntries) {481int version = fileEntries.getKey();482Set<String> cpaths = pathsMap.get(version);483return Stream.of(fileEntries.getValue())484.map(f -> toVersionedName(toEntryName(f, cpaths, false), version));485}486487/**488* Parses command line arguments.489*/490boolean parseArgs(String args[]) {491/* Preprocess and expand @file arguments */492try {493args = CommandLine.parse(args);494} catch (FileNotFoundException e) {495fatalError(formatMsg("error.cant.open", e.getMessage()));496return false;497} catch (IOException e) {498fatalError(e);499return false;500}501/* parse flags */502int count = 1;503try {504String flags = args[0];505506// Note: flags.length == 2 can be treated as the short version of507// the GNU option since the there cannot be any other options,508// excluding -C, as per the old way.509if (flags.startsWith("--") ||510(flags.startsWith("-") && flags.length() == 2)) {511try {512count = GNUStyleOptions.parseOptions(this, args);513} catch (GNUStyleOptions.BadArgs x) {514if (info == null) {515if (x.showUsage) {516usageError(x.getMessage());517} else {518error(x.getMessage());519}520return false;521}522}523if (info != null) {524info.accept(out);525return true;526}527} else {528// Legacy/compatibility options529if (flags.startsWith("-")) {530flags = flags.substring(1);531}532for (int i = 0; i < flags.length(); i++) {533switch (flags.charAt(i)) {534case 'c':535if (xflag || tflag || uflag || iflag) {536usageError(getMsg("error.multiple.main.operations"));537return false;538}539cflag = true;540break;541case 'u':542if (cflag || xflag || tflag || iflag) {543usageError(getMsg("error.multiple.main.operations"));544return false;545}546uflag = true;547break;548case 'x':549if (cflag || uflag || tflag || iflag) {550usageError(getMsg("error.multiple.main.operations"));551return false;552}553xflag = true;554break;555case 't':556if (cflag || uflag || xflag || iflag) {557usageError(getMsg("error.multiple.main.operations"));558return false;559}560tflag = true;561break;562case 'M':563Mflag = true;564break;565case 'v':566vflag = true;567break;568case 'f':569fname = args[count++];570break;571case 'm':572mname = args[count++];573break;574case '0':575flag0 = true;576break;577case 'i':578if (cflag || uflag || xflag || tflag) {579usageError(getMsg("error.multiple.main.operations"));580return false;581}582// do not increase the counter, files will contain rootjar583rootjar = args[count++];584iflag = true;585break;586case 'e':587ename = args[count++];588break;589case 'P':590pflag = true;591break;592default:593usageError(formatMsg("error.illegal.option",594String.valueOf(flags.charAt(i))));595return false;596}597}598}599} catch (ArrayIndexOutOfBoundsException e) {600usageError(getMsg("main.usage.summary"));601return false;602}603if (!cflag && !tflag && !xflag && !uflag && !iflag && !dflag && !validate) {604usageError(getMsg("error.bad.option"));605return false;606}607608/* parse file arguments */609int n = args.length - count;610if (n > 0) {611int version = BASE_VERSION;612int k = 0;613String[] nameBuf = new String[n];614pathsMap.put(version, new HashSet<>());615try {616for (int i = count; i < args.length; i++) {617if (args[i].equals("-C")) {618if (dflag) {619// "--describe-module/-d" does not require file argument(s),620// but does accept --release621usageError(getMsg("error.bad.dflag"));622return false;623}624/* change the directory */625String dir = args[++i];626dir = (dir.endsWith(File.separator) ?627dir : (dir + File.separator));628dir = dir.replace(File.separatorChar, '/');629630boolean hasUNC = (File.separatorChar == '\\'&& dir.startsWith("//"));631while (dir.indexOf("//") > -1) {632dir = dir.replace("//", "/");633}634if (hasUNC) { // Restore Windows UNC path.635dir = "/" + dir;636}637pathsMap.get(version).add(dir);638nameBuf[k++] = dir + args[++i];639} else if (args[i].startsWith("--release")) {640int v = BASE_VERSION;641try {642v = Integer.valueOf(args[++i]);643} catch (NumberFormatException x) {644error(formatMsg("error.release.value.notnumber", args[i]));645// this will fall into the next error, thus returning false646}647if (v < 9) {648usageError(formatMsg("error.release.value.toosmall", String.valueOf(v)));649return false;650}651// associate the files, if any, with the previous version number652if (k > 0) {653String[] files = new String[k];654System.arraycopy(nameBuf, 0, files, 0, k);655filesMap.put(version, files);656isMultiRelease = version > BASE_VERSION;657}658// reset the counters and start with the new version number659k = 0;660nameBuf = new String[n];661version = v;662releaseValue = version;663pathsMap.put(version, new HashSet<>());664} else {665if (dflag) {666// "--describe-module/-d" does not require file argument(s),667// but does accept --release668usageError(getMsg("error.bad.dflag"));669return false;670}671nameBuf[k++] = args[i];672}673}674} catch (ArrayIndexOutOfBoundsException e) {675usageError(getMsg("error.bad.file.arg"));676return false;677}678// associate remaining files, if any, with a version679if (k > 0) {680String[] files = new String[k];681System.arraycopy(nameBuf, 0, files, 0, k);682filesMap.put(version, files);683isMultiRelease = version > BASE_VERSION;684}685} else if (cflag && (mname == null)) {686usageError(getMsg("error.bad.cflag"));687return false;688} else if (uflag) {689if ((mname != null) || (ename != null) || moduleVersion != null) {690/* just want to update the manifest */691return true;692} else {693usageError(getMsg("error.bad.uflag"));694return false;695}696}697return true;698}699700/*701* Add the package of the given resource name if it's a .class702* or a resource in a named package.703*/704void addPackageIfNamed(Set<String> packages, String name) {705if (name.startsWith(VERSIONS_DIR)) {706// trim the version dir prefix707int i0 = VERSIONS_DIR_LENGTH;708int i = name.indexOf('/', i0);709if (i <= 0) {710warn(formatMsg("warn.release.unexpected.versioned.entry", name));711return;712}713while (i0 < i) {714char c = name.charAt(i0);715if (c < '0' || c > '9') {716warn(formatMsg("warn.release.unexpected.versioned.entry", name));717return;718}719i0++;720}721name = name.substring(i + 1, name.length());722}723String pn = toPackageName(name);724// add if this is a class or resource in a package725if (Checks.isPackageName(pn)) {726packages.add(pn);727}728}729730private String toEntryName(String name, Set<String> cpaths, boolean isDir) {731name = name.replace(File.separatorChar, '/');732if (isDir) {733name = name.endsWith("/") ? name : name + "/";734}735String matchPath = "";736for (String path : cpaths) {737if (name.startsWith(path) && path.length() > matchPath.length()) {738matchPath = path;739}740}741name = safeName(name.substring(matchPath.length()));742// the old implementaton doesn't remove743// "./" if it was led by "/" (?)744if (name.startsWith("./")) {745name = name.substring(2);746}747return name;748}749750private static String toVersionedName(String name, int version) {751return version > BASE_VERSION752? VERSIONS_DIR + version + "/" + name : name;753}754755private static String toPackageName(String path) {756int index = path.lastIndexOf('/');757if (index != -1) {758return path.substring(0, index).replace('/', '.');759} else {760return "";761}762}763764private void expand() throws IOException {765for (int version : filesMap.keySet()) {766String[] files = filesMap.get(version);767expand(null, files, pathsMap.get(version), version);768}769}770771/**772* Expands list of files to process into full list of all files that773* can be found by recursively descending directories.774*775* @param dir parent directory776* @param files list of files to expand777* @param cpaths set of directories specified by -C option for the files778* @throws IOException if an I/O error occurs779*/780private void expand(File dir, String[] files, Set<String> cpaths, int version)781throws IOException782{783if (files == null) {784return;785}786787for (int i = 0; i < files.length; i++) {788File f;789if (dir == null) {790f = new File(files[i]);791} else {792f = new File(dir, files[i]);793}794795boolean isDir = f.isDirectory();796String name = toEntryName(f.getPath(), cpaths, isDir);797798if (version != BASE_VERSION) {799if (name.startsWith(VERSIONS_DIR)) {800// the entry starts with VERSIONS_DIR and version != BASE_VERSION,801// which means the "[dirs|files]" in --release v [dirs|files]802// includes VERSIONS_DIR-ed entries --> warning and skip (?)803error(formatMsg2("error.release.unexpected.versioned.entry",804name, String.valueOf(version)));805ok = false;806return;807}808name = toVersionedName(name, version);809}810811if (f.isFile()) {812Entry e = new Entry(f, name, false);813if (isModuleInfoEntry(name)) {814moduleInfos.putIfAbsent(name, Files.readAllBytes(f.toPath()));815if (uflag) {816entryMap.put(name, e);817}818} else if (entries.add(e)) {819if (uflag) {820entryMap.put(name, e);821}822}823} else if (isDir) {824Entry e = new Entry(f, name, true);825if (entries.add(e)) {826// utilize entryMap for the duplicate dir check even in827// case of cflag == true.828// dir name conflict/duplicate could happen with -C option.829// just remove the last "e" from the "entries" (zos will fail830// with "duplicated" entries), but continue expanding the831// sub tree832if (entryMap.containsKey(name)) {833entries.remove(e);834} else {835entryMap.put(name, e);836}837String[] dirFiles = f.list();838// Ensure files list is sorted for reproducible jar content839if (dirFiles != null) {840Arrays.sort(dirFiles);841}842expand(f, dirFiles, cpaths, version);843}844} else {845error(formatMsg("error.nosuch.fileordir", String.valueOf(f)));846ok = false;847}848}849}850851/**852* Creates a new JAR file.853*/854void create(OutputStream out, Manifest manifest) throws IOException855{856try (ZipOutputStream zos = new JarOutputStream(out)) {857if (flag0) {858zos.setMethod(ZipOutputStream.STORED);859}860// TODO: check module-info attributes against manifest ??861if (manifest != null) {862if (vflag) {863output(getMsg("out.added.manifest"));864}865ZipEntry e = new ZipEntry(MANIFEST_DIR);866setZipEntryTime(e);867e.setSize(0);868e.setCrc(0);869zos.putNextEntry(e);870e = new ZipEntry(MANIFEST_NAME);871setZipEntryTime(e);872if (flag0) {873crc32Manifest(e, manifest);874}875zos.putNextEntry(e);876manifest.write(zos);877zos.closeEntry();878}879updateModuleInfo(moduleInfos, zos);880for (Entry entry : entries) {881addFile(zos, entry);882}883}884}885886private char toUpperCaseASCII(char c) {887return (c < 'a' || c > 'z') ? c : (char) (c + 'A' - 'a');888}889890/**891* Compares two strings for equality, ignoring case. The second892* argument must contain only upper-case ASCII characters.893* We don't want case comparison to be locale-dependent (else we894* have the notorious "turkish i bug").895*/896private boolean equalsIgnoreCase(String s, String upper) {897assert upper.toUpperCase(java.util.Locale.ENGLISH).equals(upper);898int len;899if ((len = s.length()) != upper.length())900return false;901for (int i = 0; i < len; i++) {902char c1 = s.charAt(i);903char c2 = upper.charAt(i);904if (c1 != c2 && toUpperCaseASCII(c1) != c2)905return false;906}907return true;908}909910/**911* Updates an existing jar file.912*/913boolean update(InputStream in, OutputStream out,914InputStream newManifest,915Map<String,byte[]> moduleInfos,916JarIndex jarIndex) throws IOException917{918ZipInputStream zis = new ZipInputStream(in);919ZipOutputStream zos = new JarOutputStream(out);920ZipEntry e = null;921boolean foundManifest = false;922boolean updateOk = true;923924// All actual entries added/updated/existing, in the jar file (excl manifest925// and module-info.class ).926Set<String> jentries = new HashSet<>();927928if (jarIndex != null) {929addIndex(jarIndex, zos);930}931932// put the old entries first, replace if necessary933while ((e = zis.getNextEntry()) != null) {934String name = e.getName();935936boolean isManifestEntry = equalsIgnoreCase(name, MANIFEST_NAME);937boolean isModuleInfoEntry = isModuleInfoEntry(name);938939if ((jarIndex != null && equalsIgnoreCase(name, INDEX_NAME))940|| (Mflag && isManifestEntry)) {941continue;942} else if (isManifestEntry && ((newManifest != null) ||943(ename != null) || isMultiRelease)) {944foundManifest = true;945if (newManifest != null) {946// Don't read from the newManifest InputStream, as we947// might need it below, and we can't re-read the same data948// twice.949try (FileInputStream fis = new FileInputStream(mname)) {950if (isAmbiguousMainClass(new Manifest(fis))) {951return false;952}953}954}955// Update the manifest.956Manifest old = new Manifest(zis);957if (newManifest != null) {958old.read(newManifest);959}960if (!updateManifest(old, zos)) {961return false;962}963} else if (moduleInfos != null && isModuleInfoEntry) {964moduleInfos.putIfAbsent(name, zis.readAllBytes());965} else {966boolean isDir = e.isDirectory();967if (!entryMap.containsKey(name)) { // copy the old stuff968// do our own compression969ZipEntry e2 = new ZipEntry(name);970e2.setMethod(e.getMethod());971setZipEntryTime(e2, e.getTime());972e2.setComment(e.getComment());973e2.setExtra(e.getExtra());974if (e.getMethod() == ZipEntry.STORED) {975e2.setSize(e.getSize());976e2.setCrc(e.getCrc());977}978zos.putNextEntry(e2);979copy(zis, zos);980} else { // replace with the new files981Entry ent = entryMap.get(name);982addFile(zos, ent);983entryMap.remove(name);984entries.remove(ent);985isDir = ent.isDir;986}987if (!isDir) {988jentries.add(name);989}990}991}992993// add the remaining new files994for (Entry entry : entries) {995addFile(zos, entry);996if (!entry.isDir) {997jentries.add(entry.name);998}999}1000if (!foundManifest) {1001if (newManifest != null) {1002Manifest m = new Manifest(newManifest);1003updateOk = !isAmbiguousMainClass(m);1004if (updateOk) {1005if (!updateManifest(m, zos)) {1006updateOk = false;1007}1008}1009} else if (ename != null) {1010if (!updateManifest(new Manifest(), zos)) {1011updateOk = false;1012}1013}1014}1015if (updateOk) {1016if (moduleInfos != null && !moduleInfos.isEmpty()) {1017Set<String> pkgs = new HashSet<>();1018jentries.forEach( je -> addPackageIfNamed(pkgs, je));1019addExtendedModuleAttributes(moduleInfos, pkgs);1020updateOk = checkModuleInfo(moduleInfos.get(MODULE_INFO), jentries);1021updateModuleInfo(moduleInfos, zos);1022// TODO: check manifest main classes, etc1023} else if (moduleVersion != null || modulesToHash != null) {1024error(getMsg("error.module.options.without.info"));1025updateOk = false;1026}1027}1028zis.close();1029zos.close();1030return updateOk;1031}10321033private void addIndex(JarIndex index, ZipOutputStream zos)1034throws IOException1035{1036ZipEntry e = new ZipEntry(INDEX_NAME);1037setZipEntryTime(e);1038if (flag0) {1039CRC32OutputStream os = new CRC32OutputStream();1040index.write(os);1041os.updateEntry(e);1042}1043zos.putNextEntry(e);1044index.write(zos);1045zos.closeEntry();1046}10471048private void updateModuleInfo(Map<String,byte[]> moduleInfos, ZipOutputStream zos)1049throws IOException1050{1051String fmt = uflag ? "out.update.module-info": "out.added.module-info";1052for (Map.Entry<String,byte[]> mi : moduleInfos.entrySet()) {1053String name = mi.getKey();1054byte[] bytes = mi.getValue();1055ZipEntry e = new ZipEntry(name);1056setZipEntryTime(e);1057if (flag0) {1058crc32ModuleInfo(e, bytes);1059}1060zos.putNextEntry(e);1061zos.write(bytes);1062zos.closeEntry();1063if (vflag) {1064output(formatMsg(fmt, name));1065}1066}1067}10681069private boolean updateManifest(Manifest m, ZipOutputStream zos)1070throws IOException1071{1072addVersion(m);1073addCreatedBy(m);1074if (ename != null) {1075addMainClass(m, ename);1076}1077if (isMultiRelease) {1078addMultiRelease(m);1079}1080ZipEntry e = new ZipEntry(MANIFEST_NAME);1081setZipEntryTime(e);1082if (flag0) {1083crc32Manifest(e, m);1084}1085zos.putNextEntry(e);1086m.write(zos);1087if (vflag) {1088output(getMsg("out.update.manifest"));1089}1090return true;1091}10921093private static final boolean isWinDriveLetter(char c) {1094return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));1095}10961097private String safeName(String name) {1098if (!pflag) {1099int len = name.length();1100int i = name.lastIndexOf("../");1101if (i == -1) {1102i = 0;1103} else {1104i += 3; // strip any dot-dot components1105}1106if (File.separatorChar == '\\') {1107// the spec requests no drive letter. skip if1108// the entry name has one.1109while (i < len) {1110int off = i;1111if (i + 1 < len &&1112name.charAt(i + 1) == ':' &&1113isWinDriveLetter(name.charAt(i))) {1114i += 2;1115}1116while (i < len && name.charAt(i) == '/') {1117i++;1118}1119if (i == off) {1120break;1121}1122}1123} else {1124while (i < len && name.charAt(i) == '/') {1125i++;1126}1127}1128if (i != 0) {1129name = name.substring(i);1130}1131}1132return name;1133}11341135private void addVersion(Manifest m) {1136Attributes global = m.getMainAttributes();1137if (global.getValue(Attributes.Name.MANIFEST_VERSION) == null) {1138global.put(Attributes.Name.MANIFEST_VERSION, VERSION);1139}1140}11411142private void addCreatedBy(Manifest m) {1143Attributes global = m.getMainAttributes();1144if (global.getValue(new Attributes.Name("Created-By")) == null) {1145String javaVendor = System.getProperty("java.vendor");1146String jdkVersion = System.getProperty("java.version");1147global.put(new Attributes.Name("Created-By"), jdkVersion + " (" +1148javaVendor + ")");1149}1150}11511152private void addMainClass(Manifest m, String mainApp) {1153Attributes global = m.getMainAttributes();11541155// overrides any existing Main-Class attribute1156global.put(Attributes.Name.MAIN_CLASS, mainApp);1157}11581159private void addMultiRelease(Manifest m) {1160Attributes global = m.getMainAttributes();1161global.put(Attributes.Name.MULTI_RELEASE, "true");1162}11631164private boolean isAmbiguousMainClass(Manifest m) {1165if (ename != null) {1166Attributes global = m.getMainAttributes();1167if ((global.get(Attributes.Name.MAIN_CLASS) != null)) {1168usageError(getMsg("error.bad.eflag"));1169return true;1170}1171}1172return false;1173}11741175/**1176* Adds a new file entry to the ZIP output stream.1177*/1178void addFile(ZipOutputStream zos, Entry entry) throws IOException {11791180File file = entry.file;1181String name = entry.name;1182boolean isDir = entry.isDir;11831184if (name.isEmpty() || name.equals(".") || name.equals(zname)) {1185return;1186} else if ((name.equals(MANIFEST_DIR) || name.equals(MANIFEST_NAME))1187&& !Mflag) {1188if (vflag) {1189output(formatMsg("out.ignore.entry", name));1190}1191return;1192} else if (name.equals(MODULE_INFO)) {1193throw new Error("Unexpected module info: " + name);1194}11951196long size = isDir ? 0 : file.length();11971198if (vflag) {1199out.print(formatMsg("out.adding", name));1200}1201ZipEntry e = new ZipEntry(name);1202setZipEntryTime(e, file.lastModified());1203if (size == 0) {1204e.setMethod(ZipEntry.STORED);1205e.setSize(0);1206e.setCrc(0);1207} else if (flag0) {1208crc32File(e, file);1209}1210zos.putNextEntry(e);1211if (!isDir) {1212copy(file, zos);1213}1214zos.closeEntry();1215/* report how much compression occurred. */1216if (vflag) {1217size = e.getSize();1218long csize = e.getCompressedSize();1219out.print(formatMsg2("out.size", String.valueOf(size),1220String.valueOf(csize)));1221if (e.getMethod() == ZipEntry.DEFLATED) {1222long ratio = 0;1223if (size != 0) {1224ratio = ((size - csize) * 100) / size;1225}1226output(formatMsg("out.deflated", String.valueOf(ratio)));1227} else {1228output(getMsg("out.stored"));1229}1230}1231}12321233/**1234* A buffer for use only by copy(InputStream, OutputStream).1235* Not as clean as allocating a new buffer as needed by copy,1236* but significantly more efficient.1237*/1238private byte[] copyBuf = new byte[8192];12391240/**1241* Copies all bytes from the input stream to the output stream.1242* Does not close or flush either stream.1243*1244* @param from the input stream to read from1245* @param to the output stream to write to1246* @throws IOException if an I/O error occurs1247*/1248private void copy(InputStream from, OutputStream to) throws IOException {1249int n;1250while ((n = from.read(copyBuf)) != -1)1251to.write(copyBuf, 0, n);1252}12531254/**1255* Copies all bytes from the input file to the output stream.1256* Does not close or flush the output stream.1257*1258* @param from the input file to read from1259* @param to the output stream to write to1260* @throws IOException if an I/O error occurs1261*/1262private void copy(File from, OutputStream to) throws IOException {1263try (InputStream in = new FileInputStream(from)) {1264copy(in, to);1265}1266}12671268/**1269* Copies all bytes from the input stream to the output file.1270* Does not close the input stream.1271*1272* @param from the input stream to read from1273* @param to the output file to write to1274* @throws IOException if an I/O error occurs1275*/1276private void copy(InputStream from, File to) throws IOException {1277try (OutputStream out = new FileOutputStream(to)) {1278copy(from, out);1279}1280}12811282/**1283* Computes the crc32 of a module-info.class. This is necessary when the1284* ZipOutputStream is in STORED mode.1285*/1286private void crc32ModuleInfo(ZipEntry e, byte[] bytes) throws IOException {1287CRC32OutputStream os = new CRC32OutputStream();1288ByteArrayInputStream in = new ByteArrayInputStream(bytes);1289in.transferTo(os);1290os.updateEntry(e);1291}12921293/**1294* Computes the crc32 of a Manifest. This is necessary when the1295* ZipOutputStream is in STORED mode.1296*/1297private void crc32Manifest(ZipEntry e, Manifest m) throws IOException {1298CRC32OutputStream os = new CRC32OutputStream();1299m.write(os);1300os.updateEntry(e);1301}13021303/**1304* Computes the crc32 of a File. This is necessary when the1305* ZipOutputStream is in STORED mode.1306*/1307private void crc32File(ZipEntry e, File f) throws IOException {1308CRC32OutputStream os = new CRC32OutputStream();1309copy(f, os);1310if (os.n != f.length()) {1311throw new JarException(formatMsg(1312"error.incorrect.length", f.getPath()));1313}1314os.updateEntry(e);1315}13161317void replaceFSC(Map<Integer, String []> filesMap) {1318filesMap.keySet().forEach(version -> {1319String[] files = filesMap.get(version);1320if (files != null) {1321for (int i = 0; i < files.length; i++) {1322files[i] = files[i].replace(File.separatorChar, '/');1323}1324}1325});1326}13271328@SuppressWarnings("serial")1329Set<ZipEntry> newDirSet() {1330return new HashSet<ZipEntry>() {1331public boolean add(ZipEntry e) {1332return ((e == null || useExtractionTime) ? false : super.add(e));1333}};1334}13351336void updateLastModifiedTime(Set<ZipEntry> zes) throws IOException {1337for (ZipEntry ze : zes) {1338long lastModified = ze.getTime();1339if (lastModified != -1) {1340String name = safeName(ze.getName().replace(File.separatorChar, '/'));1341if (name.length() != 0) {1342File f = new File(name.replace('/', File.separatorChar));1343f.setLastModified(lastModified);1344}1345}1346}1347}13481349/**1350* Extracts specified entries from JAR file.1351*1352* @return whether entries were found and successfully extracted1353* (indicating this was a zip file without "leading garbage")1354*/1355boolean extract(InputStream in, String files[]) throws IOException {1356ZipInputStream zis = new ZipInputStream(in);1357ZipEntry e;1358// Set of all directory entries specified in archive. Disallows1359// null entries. Disallows all entries if using pre-6.0 behavior.1360boolean entriesFound = false;1361Set<ZipEntry> dirs = newDirSet();1362while ((e = zis.getNextEntry()) != null) {1363entriesFound = true;1364if (files == null) {1365dirs.add(extractFile(zis, e));1366} else {1367String name = e.getName();1368for (String file : files) {1369if (name.startsWith(file)) {1370dirs.add(extractFile(zis, e));1371break;1372}1373}1374}1375}13761377// Update timestamps of directories specified in archive with their1378// timestamps as given in the archive. We do this after extraction,1379// instead of during, because creating a file in a directory changes1380// that directory's timestamp.1381updateLastModifiedTime(dirs);13821383return entriesFound;1384}13851386/**1387* Extracts specified entries from JAR file, via ZipFile.1388*/1389void extract(String fname, String files[]) throws IOException {1390ZipFile zf = new ZipFile(fname);1391Set<ZipEntry> dirs = newDirSet();1392Enumeration<? extends ZipEntry> zes = zf.entries();1393while (zes.hasMoreElements()) {1394ZipEntry e = zes.nextElement();1395if (files == null) {1396dirs.add(extractFile(zf.getInputStream(e), e));1397} else {1398String name = e.getName();1399for (String file : files) {1400if (name.startsWith(file)) {1401dirs.add(extractFile(zf.getInputStream(e), e));1402break;1403}1404}1405}1406}1407zf.close();1408updateLastModifiedTime(dirs);1409}14101411/**1412* Extracts next entry from JAR file, creating directories as needed. If1413* the entry is for a directory which doesn't exist prior to this1414* invocation, returns that entry, otherwise returns null.1415*/1416ZipEntry extractFile(InputStream is, ZipEntry e) throws IOException {1417ZipEntry rc = null;1418// The spec requres all slashes MUST be forward '/', it is possible1419// an offending zip/jar entry may uses the backwards slash in its1420// name. It might cause problem on Windows platform as it skips1421// our "safe" check for leading slahs and dot-dot. So replace them1422// with '/'.1423String name = safeName(e.getName().replace(File.separatorChar, '/'));1424if (name.length() == 0) {1425return rc; // leading '/' or 'dot-dot' only path1426}1427File f = new File(name.replace('/', File.separatorChar));1428if (e.isDirectory()) {1429if (f.exists()) {1430if (!f.isDirectory()) {1431throw new IOException(formatMsg("error.create.dir",1432f.getPath()));1433}1434} else {1435if (!f.mkdirs()) {1436throw new IOException(formatMsg("error.create.dir",1437f.getPath()));1438} else {1439rc = e;1440}1441}14421443if (vflag) {1444output(formatMsg("out.create", name));1445}1446} else {1447if (f.getParent() != null) {1448File d = new File(f.getParent());1449if (!d.exists() && !d.mkdirs() || !d.isDirectory()) {1450throw new IOException(formatMsg(1451"error.create.dir", d.getPath()));1452}1453}1454try {1455copy(is, f);1456} finally {1457if (is instanceof ZipInputStream)1458((ZipInputStream)is).closeEntry();1459else1460is.close();1461}1462if (vflag) {1463if (e.getMethod() == ZipEntry.DEFLATED) {1464output(formatMsg("out.inflated", name));1465} else {1466output(formatMsg("out.extracted", name));1467}1468}1469}1470if (!useExtractionTime) {1471long lastModified = e.getTime();1472if (lastModified != -1) {1473f.setLastModified(lastModified);1474}1475}1476return rc;1477}14781479/**1480* Lists contents of JAR file.1481*/1482void list(InputStream in, String files[]) throws IOException {1483ZipInputStream zis = new ZipInputStream(in);1484ZipEntry e;1485while ((e = zis.getNextEntry()) != null) {1486/*1487* In the case of a compressed (deflated) entry, the entry size1488* is stored immediately following the entry data and cannot be1489* determined until the entry is fully read. Therefore, we close1490* the entry first before printing out its attributes.1491*/1492zis.closeEntry();1493printEntry(e, files);1494}1495}14961497/**1498* Lists contents of JAR file, via ZipFile.1499*/1500void list(String fname, String files[]) throws IOException {1501ZipFile zf = new ZipFile(fname);1502Enumeration<? extends ZipEntry> zes = zf.entries();1503while (zes.hasMoreElements()) {1504printEntry(zes.nextElement(), files);1505}1506zf.close();1507}15081509/**1510* Outputs the class index table to the INDEX.LIST file of the1511* root jar file.1512*/1513void dumpIndex(String rootjar, JarIndex index) throws IOException {1514File jarFile = new File(rootjar);1515Path jarPath = jarFile.toPath();1516Path tmpPath = createTempFileInSameDirectoryAs(jarFile).toPath();1517try {1518if (update(Files.newInputStream(jarPath),1519Files.newOutputStream(tmpPath),1520null, null, index)) {1521try {1522Files.move(tmpPath, jarPath, REPLACE_EXISTING);1523} catch (IOException e) {1524throw new IOException(getMsg("error.write.file"), e);1525}1526}1527} finally {1528Files.deleteIfExists(tmpPath);1529}1530}15311532private HashSet<String> jarPaths = new HashSet<String>();15331534/**1535* Generates the transitive closure of the Class-Path attribute for1536* the specified jar file.1537*/1538List<String> getJarPath(String jar) throws IOException {1539List<String> files = new ArrayList<String>();1540files.add(jar);1541jarPaths.add(jar);15421543// take out the current path1544String path = jar.substring(0, Math.max(0, jar.lastIndexOf('/') + 1));15451546// class path attribute will give us jar file name with1547// '/' as separators, so we need to change them to the1548// appropriate one before we open the jar file.1549JarFile rf = new JarFile(jar.replace('/', File.separatorChar));15501551if (rf != null) {1552Manifest man = rf.getManifest();1553if (man != null) {1554Attributes attr = man.getMainAttributes();1555if (attr != null) {1556String value = attr.getValue(Attributes.Name.CLASS_PATH);1557if (value != null) {1558StringTokenizer st = new StringTokenizer(value);1559while (st.hasMoreTokens()) {1560String ajar = st.nextToken();1561if (!ajar.endsWith("/")) { // it is a jar file1562ajar = path.concat(ajar);1563/* check on cyclic dependency */1564if (! jarPaths.contains(ajar)) {1565files.addAll(getJarPath(ajar));1566}1567}1568}1569}1570}1571}1572}1573rf.close();1574return files;1575}15761577/**1578* Generates class index file for the specified root jar file.1579*/1580void genIndex(String rootjar, String[] files) throws IOException {1581List<String> jars = getJarPath(rootjar);1582int njars = jars.size();1583String[] jarfiles;15841585if (njars == 1 && files != null) {1586// no class-path attribute defined in rootjar, will1587// use command line specified list of jars1588for (int i = 0; i < files.length; i++) {1589jars.addAll(getJarPath(files[i]));1590}1591njars = jars.size();1592}1593jarfiles = jars.toArray(new String[njars]);1594JarIndex index = new JarIndex(jarfiles);1595dumpIndex(rootjar, index);1596}15971598/**1599* Prints entry information, if requested.1600*/1601void printEntry(ZipEntry e, String[] files) throws IOException {1602if (files == null) {1603printEntry(e);1604} else {1605String name = e.getName();1606for (String file : files) {1607if (name.startsWith(file)) {1608printEntry(e);1609return;1610}1611}1612}1613}16141615/**1616* Prints entry information.1617*/1618void printEntry(ZipEntry e) throws IOException {1619if (vflag) {1620StringBuilder sb = new StringBuilder();1621String s = Long.toString(e.getSize());1622for (int i = 6 - s.length(); i > 0; --i) {1623sb.append(' ');1624}1625sb.append(s).append(' ').append(new Date(e.getTime()).toString());1626sb.append(' ').append(e.getName());1627output(sb.toString());1628} else {1629output(e.getName());1630}1631}16321633/**1634* Prints usage message.1635*/1636void usageError(String s) {1637err.println(s);1638err.println(getMsg("main.usage.summary.try"));1639}16401641/**1642* A fatal exception has been caught. No recovery possible1643*/1644void fatalError(Exception e) {1645e.printStackTrace();1646}16471648/**1649* A fatal condition has been detected; message is "s".1650* No recovery possible1651*/1652void fatalError(String s) {1653error(program + ": " + s);1654}16551656/**1657* Print an output message; like verbose output and the like1658*/1659protected void output(String s) {1660out.println(s);1661}16621663/**1664* Print an error message; like something is broken1665*/1666void error(String s) {1667err.println(s);1668}16691670/**1671* Print a warning message1672*/1673void warn(String s) {1674err.println(s);1675}16761677/**1678* Main routine to start program.1679*/1680public static void main(String args[]) {1681Main jartool = new Main(System.out, System.err, "jar");1682System.exit(jartool.run(args) ? 0 : 1);1683}16841685/**1686* An OutputStream that doesn't send its output anywhere, (but could).1687* It's here to find the CRC32 of an input file, necessary for STORED1688* mode in ZIP.1689*/1690private static class CRC32OutputStream extends java.io.OutputStream {1691final CRC32 crc = new CRC32();1692long n = 0;16931694CRC32OutputStream() {}16951696public void write(int r) throws IOException {1697crc.update(r);1698n++;1699}17001701public void write(byte[] b, int off, int len) throws IOException {1702crc.update(b, off, len);1703n += len;1704}17051706/**1707* Updates a ZipEntry which describes the data read by this1708* output stream, in STORED mode.1709*/1710public void updateEntry(ZipEntry e) {1711e.setMethod(ZipEntry.STORED);1712e.setSize(n);1713e.setCrc(crc.getValue());1714}1715}17161717/**1718* Attempt to create temporary file in the system-provided temporary folder, if failed attempts1719* to create it in the same folder as the file in parameter (if any)1720*/1721private File createTemporaryFile(String tmpbase, String suffix) {1722File tmpfile = null;17231724try {1725tmpfile = File.createTempFile(tmpbase, suffix);1726} catch (IOException | SecurityException e) {1727// Unable to create file due to permission violation or security exception1728}1729if (tmpfile == null) {1730// Were unable to create temporary file, fall back to temporary file in the same folder1731if (fname != null) {1732try {1733File tmpfolder = new File(fname).getAbsoluteFile().getParentFile();1734tmpfile = File.createTempFile(fname, ".tmp" + suffix, tmpfolder);1735} catch (IOException ioe) {1736// Last option failed - fall gracefully1737fatalError(ioe);1738}1739} else {1740// No options left - we can not compress to stdout without access to the temporary folder1741fatalError(new IOException(getMsg("error.create.tempfile")));1742}1743}1744return tmpfile;1745}17461747// Modular jar support17481749/**1750* Associates a module descriptor's zip entry name along with its1751* bytes and an optional URI. Used when describing modules.1752*/1753interface ModuleInfoEntry {1754String name();1755Optional<String> uriString();1756InputStream bytes() throws IOException;1757}17581759static class ZipFileModuleInfoEntry implements ModuleInfoEntry {1760private final ZipFile zipFile;1761private final ZipEntry entry;1762ZipFileModuleInfoEntry(ZipFile zipFile, ZipEntry entry) {1763this.zipFile = zipFile;1764this.entry = entry;1765}1766@Override public String name() { return entry.getName(); }1767@Override public InputStream bytes() throws IOException {1768return zipFile.getInputStream(entry);1769}1770/** Returns an optional containing the effective URI. */1771@Override public Optional<String> uriString() {1772String uri = (Paths.get(zipFile.getName())).toUri().toString();1773uri = "jar:" + uri + "!/" + entry.getName();1774return Optional.of(uri);1775}1776}17771778static class StreamedModuleInfoEntry implements ModuleInfoEntry {1779private final String name;1780private final byte[] bytes;1781StreamedModuleInfoEntry(String name, byte[] bytes) {1782this.name = name;1783this.bytes = bytes;1784}1785@Override public String name() { return name; }1786@Override public InputStream bytes() throws IOException {1787return new ByteArrayInputStream(bytes);1788}1789/** Returns an empty optional. */1790@Override public Optional<String> uriString() {1791return Optional.empty(); // no URI can be derived1792}1793}17941795/** Describes a module from a given zip file. */1796private boolean describeModule(ZipFile zipFile) throws IOException {1797ZipFileModuleInfoEntry[] infos = zipFile.stream()1798.filter(e -> isModuleInfoEntry(e.getName()))1799.sorted(ENTRY_COMPARATOR)1800.map(e -> new ZipFileModuleInfoEntry(zipFile, e))1801.toArray(ZipFileModuleInfoEntry[]::new);18021803if (infos.length == 0) {1804// No module descriptor found, derive and describe the automatic module1805String fn = zipFile.getName();1806ModuleFinder mf = ModuleFinder.of(Paths.get(fn));1807try {1808Set<ModuleReference> mref = mf.findAll();1809if (mref.isEmpty()) {1810output(formatMsg("error.unable.derive.automodule", fn));1811return true;1812}1813ModuleDescriptor md = mref.iterator().next().descriptor();1814output(getMsg("out.automodule") + "\n");1815describeModule(md, null, null, "");1816} catch (FindException e) {1817String msg = formatMsg("error.unable.derive.automodule", fn);1818Throwable t = e.getCause();1819if (t != null)1820msg = msg + "\n" + t.getMessage();1821output(msg);1822}1823} else {1824return describeModuleFromEntries(infos);1825}1826return true;1827}18281829private boolean describeModuleFromStream(FileInputStream fis)1830throws IOException1831{1832List<ModuleInfoEntry> infos = new LinkedList<>();18331834try (BufferedInputStream bis = new BufferedInputStream(fis);1835ZipInputStream zis = new ZipInputStream(bis)) {1836ZipEntry e;1837while ((e = zis.getNextEntry()) != null) {1838String ename = e.getName();1839if (isModuleInfoEntry(ename)) {1840infos.add(new StreamedModuleInfoEntry(ename, zis.readAllBytes()));1841}1842}1843}18441845if (infos.size() == 0)1846return false;18471848ModuleInfoEntry[] sorted = infos.stream()1849.sorted(Comparator.comparing(ModuleInfoEntry::name, ENTRYNAME_COMPARATOR))1850.toArray(ModuleInfoEntry[]::new);18511852return describeModuleFromEntries(sorted);1853}18541855private boolean lessThanEqualReleaseValue(ModuleInfoEntry entry) {1856return intVersionFromEntry(entry) <= releaseValue ? true : false;1857}18581859private static String versionFromEntryName(String name) {1860String s = name.substring(VERSIONS_DIR_LENGTH);1861return s.substring(0, s.indexOf("/"));1862}18631864private static int intVersionFromEntry(ModuleInfoEntry entry) {1865String name = entry.name();1866if (!name.startsWith(VERSIONS_DIR))1867return BASE_VERSION;18681869String s = name.substring(VERSIONS_DIR_LENGTH);1870s = s.substring(0, s.indexOf('/'));1871return Integer.valueOf(s);1872}18731874/**1875* Describes a single module descriptor, determined by the specified1876* --release, if any, from the given ordered entries.1877* The given infos must be ordered as per ENTRY_COMPARATOR.1878*/1879private boolean describeModuleFromEntries(ModuleInfoEntry[] infos)1880throws IOException1881{1882assert infos.length > 0;18831884// Informative: output all non-root descriptors, if any1885String releases = Arrays.stream(infos)1886.filter(e -> !e.name().equals(MODULE_INFO))1887.map(ModuleInfoEntry::name)1888.map(Main::versionFromEntryName)1889.collect(joining(" "));1890if (!releases.isEmpty())1891output("releases: " + releases + "\n");18921893// Describe the operative descriptor for the specified --release, if any1894if (releaseValue != -1) {1895ModuleInfoEntry entry = null;1896int i = 0;1897while (i < infos.length && lessThanEqualReleaseValue(infos[i])) {1898entry = infos[i];1899i++;1900}19011902if (entry == null) {1903output(formatMsg("error.no.operative.descriptor",1904String.valueOf(releaseValue)));1905return false;1906}19071908String uriString = entry.uriString().orElse("");1909try (InputStream is = entry.bytes()) {1910describeModule(is, uriString);1911}1912} else {1913// no specific --release specified, output the root, if any1914if (infos[0].name().equals(MODULE_INFO)) {1915String uriString = infos[0].uriString().orElse("");1916try (InputStream is = infos[0].bytes()) {1917describeModule(is, uriString);1918}1919} else {1920// no root, output message to specify --release1921output(getMsg("error.no.root.descriptor"));1922}1923}1924return true;1925}19261927static <T> String toLowerCaseString(Collection<T> set) {1928if (set.isEmpty()) { return ""; }1929return " " + set.stream().map(e -> e.toString().toLowerCase(Locale.ROOT))1930.sorted().collect(joining(" "));1931}19321933static <T> String toString(Collection<T> set) {1934if (set.isEmpty()) { return ""; }1935return " " + set.stream().map(e -> e.toString()).sorted().collect(joining(" "));1936}19371938private void describeModule(InputStream entryInputStream, String uriString)1939throws IOException1940{1941ModuleInfo.Attributes attrs = ModuleInfo.read(entryInputStream, null);1942ModuleDescriptor md = attrs.descriptor();1943ModuleTarget target = attrs.target();1944ModuleHashes hashes = attrs.recordedHashes();19451946describeModule(md, target, hashes, uriString);1947}19481949private void describeModule(ModuleDescriptor md,1950ModuleTarget target,1951ModuleHashes hashes,1952String uriString)1953throws IOException1954{1955StringBuilder sb = new StringBuilder();19561957sb.append(md.toNameAndVersion());19581959if (!uriString.isEmpty())1960sb.append(" ").append(uriString);1961if (md.isOpen())1962sb.append(" open");1963if (md.isAutomatic())1964sb.append(" automatic");1965sb.append("\n");19661967// unqualified exports (sorted by package)1968md.exports().stream()1969.sorted(Comparator.comparing(Exports::source))1970.filter(e -> !e.isQualified())1971.forEach(e -> sb.append("exports ").append(e.source())1972.append(toLowerCaseString(e.modifiers()))1973.append("\n"));19741975// dependences1976md.requires().stream().sorted()1977.forEach(r -> sb.append("requires ").append(r.name())1978.append(toLowerCaseString(r.modifiers()))1979.append("\n"));19801981// service use and provides1982md.uses().stream().sorted()1983.forEach(s -> sb.append("uses ").append(s).append("\n"));19841985md.provides().stream()1986.sorted(Comparator.comparing(Provides::service))1987.forEach(p -> sb.append("provides ").append(p.service())1988.append(" with")1989.append(toString(p.providers()))1990.append("\n"));19911992// qualified exports1993md.exports().stream()1994.sorted(Comparator.comparing(Exports::source))1995.filter(Exports::isQualified)1996.forEach(e -> sb.append("qualified exports ").append(e.source())1997.append(" to").append(toLowerCaseString(e.targets()))1998.append("\n"));19992000// open packages2001md.opens().stream()2002.sorted(Comparator.comparing(Opens::source))2003.filter(o -> !o.isQualified())2004.forEach(o -> sb.append("opens ").append(o.source())2005.append(toLowerCaseString(o.modifiers()))2006.append("\n"));20072008md.opens().stream()2009.sorted(Comparator.comparing(Opens::source))2010.filter(Opens::isQualified)2011.forEach(o -> sb.append("qualified opens ").append(o.source())2012.append(toLowerCaseString(o.modifiers()))2013.append(" to").append(toLowerCaseString(o.targets()))2014.append("\n"));20152016// non-exported/non-open packages2017Set<String> concealed = new TreeSet<>(md.packages());2018md.exports().stream().map(Exports::source).forEach(concealed::remove);2019md.opens().stream().map(Opens::source).forEach(concealed::remove);2020concealed.forEach(p -> sb.append("contains ").append(p).append("\n"));20212022md.mainClass().ifPresent(v -> sb.append("main-class ").append(v).append("\n"));20232024if (target != null) {2025String targetPlatform = target.targetPlatform();2026if (!targetPlatform.isEmpty())2027sb.append("platform ").append(targetPlatform).append("\n");2028}20292030if (hashes != null) {2031hashes.names().stream().sorted().forEach(2032mod -> sb.append("hashes ").append(mod).append(" ")2033.append(hashes.algorithm()).append(" ")2034.append(toHex(hashes.hashFor(mod)))2035.append("\n"));2036}20372038output(sb.toString());2039}20402041private static String toHex(byte[] ba) {2042StringBuilder sb = new StringBuilder(ba.length << 1);2043for (byte b: ba) {2044sb.append(String.format("%02x", b & 0xff));2045}2046return sb.toString();2047}20482049static String toBinaryName(String classname) {2050return (classname.replace('.', '/')) + ".class";2051}20522053private boolean checkModuleInfo(byte[] moduleInfoBytes, Set<String> entries)2054throws IOException2055{2056boolean ok = true;2057if (moduleInfoBytes != null) { // no root module-info.class if null2058try {2059// ModuleDescriptor.read() checks open/exported pkgs vs packages2060ModuleDescriptor md = ModuleDescriptor.read(ByteBuffer.wrap(moduleInfoBytes));2061// A module must have the implementation class of the services it 'provides'.2062if (md.provides().stream().map(Provides::providers).flatMap(List::stream)2063.filter(p -> !entries.contains(toBinaryName(p)))2064.peek(p -> fatalError(formatMsg("error.missing.provider", p)))2065.count() != 0) {2066ok = false;2067}2068} catch (InvalidModuleDescriptorException x) {2069fatalError(x.getMessage());2070ok = false;2071}2072}2073return ok;2074}20752076/**2077* Adds extended modules attributes to the given module-info's. The given2078* Map values are updated in-place. Returns false if an error occurs.2079*/2080private void addExtendedModuleAttributes(Map<String,byte[]> moduleInfos,2081Set<String> packages)2082throws IOException2083{2084for (Map.Entry<String,byte[]> e: moduleInfos.entrySet()) {2085ModuleDescriptor md = ModuleDescriptor.read(ByteBuffer.wrap(e.getValue()));2086e.setValue(extendedInfoBytes(md, e.getValue(), packages));2087}2088}20892090static boolean isModuleInfoEntry(String name) {2091// root or versioned module-info.class2092if (name.endsWith(MODULE_INFO)) {2093int end = name.length() - MODULE_INFO.length();2094if (end == 0)2095return true;2096if (name.startsWith(VERSIONS_DIR)) {2097int off = VERSIONS_DIR_LENGTH;2098if (off == end) // meta-inf/versions/module-info.class2099return false;2100while (off < end - 1) {2101char c = name.charAt(off++);2102if (c < '0' || c > '9')2103return false;2104}2105return name.charAt(off) == '/';2106}2107}2108return false;2109}21102111/**2112* Returns a byte array containing the given module-info.class plus any2113* extended attributes.2114*2115* If --module-version, --main-class, or other options were provided2116* then the corresponding class file attributes are added to the2117* module-info here.2118*/2119private byte[] extendedInfoBytes(ModuleDescriptor md,2120byte[] miBytes,2121Set<String> packages)2122throws IOException2123{2124ByteArrayOutputStream baos = new ByteArrayOutputStream();2125InputStream is = new ByteArrayInputStream(miBytes);2126ModuleInfoExtender extender = ModuleInfoExtender.newExtender(is);21272128// Add (or replace) the Packages attribute2129extender.packages(packages);21302131// --main-class2132if (ename != null)2133extender.mainClass(ename);21342135// --module-version2136if (moduleVersion != null)2137extender.version(moduleVersion);21382139// --hash-modules2140if (modulesToHash != null) {2141String mn = md.name();2142Hasher hasher = new Hasher(md, fname);2143ModuleHashes moduleHashes = hasher.computeHashes(mn);2144if (moduleHashes != null) {2145extender.hashes(moduleHashes);2146} else {2147warn("warning: no module is recorded in hash in " + mn);2148}2149}21502151if (moduleResolution.value() != 0) {2152extender.moduleResolution(moduleResolution);2153}21542155extender.write(baos);2156return baos.toByteArray();2157}21582159/**2160* Compute and record hashes2161*/2162private class Hasher {2163final ModuleHashesBuilder hashesBuilder;2164final ModuleFinder finder;2165final Set<String> modules;2166Hasher(ModuleDescriptor descriptor, String fname) throws IOException {2167// Create a module finder that finds the modular JAR2168// being created/updated2169URI uri = Paths.get(fname).toUri();2170ModuleReference mref = new ModuleReference(descriptor, uri) {2171@Override2172public ModuleReader open() {2173throw new UnsupportedOperationException("should not reach here");2174}2175};21762177// Compose a module finder with the module path and2178// the modular JAR being created or updated2179this.finder = ModuleFinder.compose(moduleFinder,2180new ModuleFinder() {2181@Override2182public Optional<ModuleReference> find(String name) {2183if (descriptor.name().equals(name))2184return Optional.of(mref);2185else2186return Optional.empty();2187}21882189@Override2190public Set<ModuleReference> findAll() {2191return Collections.singleton(mref);2192}2193});21942195// Determine the modules that matches the pattern {@code modulesToHash}2196Set<String> roots = finder.findAll().stream()2197.map(ref -> ref.descriptor().name())2198.filter(mn -> modulesToHash.matcher(mn).find())2199.collect(Collectors.toSet());22002201// use system module path unless it creates a modular JAR for2202// a module that is present in the system image e.g. upgradeable2203// module2204ModuleFinder system;2205String name = descriptor.name();2206if (name != null && ModuleFinder.ofSystem().find(name).isPresent()) {2207system = ModuleFinder.of();2208} else {2209system = ModuleFinder.ofSystem();2210}2211// get a resolved module graph2212Configuration config =2213Configuration.empty().resolve(system, finder, roots);22142215// filter modules resolved from the system module finder2216this.modules = config.modules().stream()2217.map(ResolvedModule::name)2218.filter(mn -> roots.contains(mn) && !system.find(mn).isPresent())2219.collect(Collectors.toSet());22202221this.hashesBuilder = new ModuleHashesBuilder(config, modules);2222}22232224/**2225* Compute hashes of the specified module.2226*2227* It records the hashing modules that depend upon the specified2228* module directly or indirectly.2229*/2230ModuleHashes computeHashes(String name) {2231if (hashesBuilder == null)2232return null;22332234return hashesBuilder.computeHashes(Set.of(name)).get(name);2235}2236}22372238// sort base entries before versioned entries, and sort entry classes with2239// nested classes so that the outter class appears before the associated2240// nested class2241static Comparator<String> ENTRYNAME_COMPARATOR = (s1, s2) -> {22422243if (s1.equals(s2)) return 0;2244boolean b1 = s1.startsWith(VERSIONS_DIR);2245boolean b2 = s2.startsWith(VERSIONS_DIR);2246if (b1 && !b2) return 1;2247if (!b1 && b2) return -1;2248int n = 0; // starting char for String compare2249if (b1 && b2) {2250// normally strings would be sorted so "10" goes before "9", but2251// version number strings need to be sorted numerically2252n = VERSIONS_DIR.length(); // skip the common prefix2253int i1 = s1.indexOf('/', n);2254int i2 = s2.indexOf('/', n);2255if (i1 == -1) throw new Validator.InvalidJarException(s1);2256if (i2 == -1) throw new Validator.InvalidJarException(s2);2257// shorter version numbers go first2258if (i1 != i2) return i1 - i2;2259// otherwise, handle equal length numbers below2260}2261int l1 = s1.length();2262int l2 = s2.length();2263int lim = Math.min(l1, l2);2264for (int k = n; k < lim; k++) {2265char c1 = s1.charAt(k);2266char c2 = s2.charAt(k);2267if (c1 != c2) {2268// change natural ordering so '.' comes before '$'2269// i.e. outer classes come before nested classes2270if (c1 == '$' && c2 == '.') return 1;2271if (c1 == '.' && c2 == '$') return -1;2272return c1 - c2;2273}2274}2275return l1 - l2;2276};22772278static Comparator<ZipEntry> ENTRY_COMPARATOR =2279Comparator.comparing(ZipEntry::getName, ENTRYNAME_COMPARATOR);22802281// Set the ZipEntry dostime using date if specified otherwise the current time2282private void setZipEntryTime(ZipEntry e) {2283setZipEntryTime(e, System.currentTimeMillis());2284}22852286// Set the ZipEntry dostime using the date if specified2287// otherwise the original time2288private void setZipEntryTime(ZipEntry e, long origTime) {2289if (date != null) {2290e.setTimeLocal(date);2291} else {2292e.setTime(origTime);2293}2294}2295}229622972298