Path: blob/master/src/jdk.jartool/share/classes/sun/tools/jar/Main.java
41161 views
/*1* Copyright (c) 1996, 2018, 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 jdk.internal.module.Checks;62import jdk.internal.module.ModuleHashes;63import jdk.internal.module.ModuleHashesBuilder;64import jdk.internal.module.ModuleInfo;65import jdk.internal.module.ModuleInfoExtender;66import jdk.internal.module.ModuleResolution;67import jdk.internal.module.ModuleTarget;68import jdk.internal.util.jar.JarIndex;6970import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;71import static java.util.jar.JarFile.MANIFEST_NAME;72import static java.util.stream.Collectors.joining;73import static jdk.internal.util.jar.JarIndex.INDEX_NAME;7475/**76* This class implements a simple utility for creating files in the JAR77* (Java Archive) file format. The JAR format is based on the ZIP file78* format, with optional meta-information stored in a MANIFEST entry.79*/80public class Main {81String program;82PrintWriter out, err;83String fname, mname, ename;84String zname = "";85String rootjar = null;8687private static final int BASE_VERSION = 0;8889private static class Entry {90final String name;91final File file;92final boolean isDir;9394Entry(File file, String name, boolean isDir) {95this.file = file;96this.isDir = isDir;97this.name = name;98}99100@Override101public boolean equals(Object o) {102if (this == o) return true;103if (!(o instanceof Entry)) return false;104return this.file.equals(((Entry)o).file);105}106107@Override108public int hashCode() {109return file.hashCode();110}111}112113// An entryName(path)->Entry map generated during "expand", it helps to114// decide whether or not an existing entry in a jar file needs to be115// replaced, during the "update" operation.116Map<String, Entry> entryMap = new HashMap<>();117118// All entries need to be added/updated.119Set<Entry> entries = new LinkedHashSet<>();120121// module-info.class entries need to be added/updated.122Map<String,byte[]> moduleInfos = new HashMap<>();123124// A paths Set for each version, where each Set contains directories125// specified by the "-C" operation.126Map<Integer,Set<String>> pathsMap = new HashMap<>();127128// There's also a files array per version129Map<Integer,String[]> filesMap = new HashMap<>();130131// Do we think this is a multi-release jar? Set to true132// if --release option found followed by at least file133boolean isMultiRelease;134135// The last parsed --release value, if any. Used in conjunction with136// "-d,--describe-module" to select the operative module descriptor.137int releaseValue = -1;138139/*140* cflag: create141* uflag: update142* xflag: xtract143* tflag: table144* vflag: verbose145* flag0: no zip compression (store only)146* Mflag: DO NOT generate a manifest file (just ZIP)147* iflag: generate jar index148* nflag: Perform jar normalization at the end149* pflag: preserve/don't strip leading slash and .. component from file name150* dflag: print module descriptor151*/152boolean cflag, uflag, xflag, tflag, vflag, flag0, Mflag, iflag, pflag, dflag;153154boolean suppressDeprecateMsg = false;155156/* To support additional GNU Style informational options */157Consumer<PrintWriter> info;158159/* Modular jar related options */160Version moduleVersion;161Pattern modulesToHash;162ModuleResolution moduleResolution = ModuleResolution.empty();163ModuleFinder moduleFinder = ModuleFinder.of();164165static final String MODULE_INFO = "module-info.class";166static final String MANIFEST_DIR = "META-INF/";167static final String VERSIONS_DIR = MANIFEST_DIR + "versions/";168static final String VERSION = "1.0";169static final int VERSIONS_DIR_LENGTH = VERSIONS_DIR.length();170private static ResourceBundle rsrc;171172/**173* If true, maintain compatibility with JDK releases prior to 6.0 by174* timestamping extracted files with the time at which they are extracted.175* Default is to use the time given in the archive.176*/177private static final boolean useExtractionTime =178Boolean.getBoolean("sun.tools.jar.useExtractionTime");179180/**181* Initialize ResourceBundle182*/183static {184try {185rsrc = ResourceBundle.getBundle("sun.tools.jar.resources.jar");186} catch (MissingResourceException e) {187throw new Error("Fatal: Resource for jar is missing");188}189}190191static String getMsg(String key) {192try {193return (rsrc.getString(key));194} catch (MissingResourceException e) {195throw new Error("Error in message file");196}197}198199static String formatMsg(String key, String arg) {200String msg = getMsg(key);201String[] args = new String[1];202args[0] = arg;203return MessageFormat.format(msg, (Object[]) args);204}205206static String formatMsg2(String key, String arg, String arg1) {207String msg = getMsg(key);208String[] args = new String[2];209args[0] = arg;210args[1] = arg1;211return MessageFormat.format(msg, (Object[]) args);212}213214public Main(PrintStream out, PrintStream err, String program) {215this.out = new PrintWriter(out, true);216this.err = new PrintWriter(err, true);217this.program = program;218}219220public Main(PrintWriter out, PrintWriter err, String program) {221this.out = out;222this.err = err;223this.program = program;224}225226/**227* Creates a new empty temporary file in the same directory as the228* specified file. A variant of File.createTempFile.229*/230private static File createTempFileInSameDirectoryAs(File file)231throws IOException {232File dir = file.getParentFile();233if (dir == null)234dir = new File(".");235return File.createTempFile("jartmp", null, dir);236}237238private boolean ok;239240/**241* Starts main program with the specified arguments.242*/243@SuppressWarnings({"removal"})244public synchronized boolean run(String args[]) {245ok = true;246if (!parseArgs(args)) {247return false;248}249File tmpFile = null;250try {251if (cflag || uflag) {252if (fname != null) {253// The name of the zip file as it would appear as its own254// zip file entry. We use this to make sure that we don't255// add the zip file to itself.256zname = fname.replace(File.separatorChar, '/');257if (zname.startsWith("./")) {258zname = zname.substring(2);259}260}261}262if (cflag) {263Manifest manifest = null;264if (!Mflag) {265if (mname != null) {266try (InputStream in = new FileInputStream(mname)) {267manifest = new Manifest(new BufferedInputStream(in));268}269} else {270manifest = new Manifest();271}272addVersion(manifest);273addCreatedBy(manifest);274if (isAmbiguousMainClass(manifest)) {275return false;276}277if (ename != null) {278addMainClass(manifest, ename);279}280if (isMultiRelease) {281addMultiRelease(manifest);282}283}284expand();285if (!moduleInfos.isEmpty()) {286// All actual file entries (excl manifest and module-info.class)287Set<String> jentries = new HashSet<>();288// all packages if it's a class or resource289Set<String> packages = new HashSet<>();290entries.stream()291.filter(e -> !e.isDir)292.forEach( e -> {293addPackageIfNamed(packages, e.name);294jentries.add(e.name);295});296addExtendedModuleAttributes(moduleInfos, packages);297298// Basic consistency checks for modular jars.299if (!checkModuleInfo(moduleInfos.get(MODULE_INFO), jentries))300return false;301302} else if (moduleVersion != null || modulesToHash != null) {303error(getMsg("error.module.options.without.info"));304return false;305}306if (vflag && fname == null) {307// Disable verbose output so that it does not appear308// on stdout along with file data309// error("Warning: -v option ignored");310vflag = false;311}312final String tmpbase = (fname == null)313? "tmpjar"314: fname.substring(fname.indexOf(File.separatorChar) + 1);315316tmpFile = createTemporaryFile(tmpbase, ".jar");317try (OutputStream out = new FileOutputStream(tmpFile)) {318create(new BufferedOutputStream(out, 4096), manifest);319}320validateAndClose(tmpFile);321} else if (uflag) {322File inputFile = null;323if (fname != null) {324inputFile = new File(fname);325tmpFile = createTempFileInSameDirectoryAs(inputFile);326} else {327vflag = false;328tmpFile = createTemporaryFile("tmpjar", ".jar");329}330expand();331try (FileInputStream in = (fname != null) ? new FileInputStream(inputFile)332: new FileInputStream(FileDescriptor.in);333FileOutputStream out = new FileOutputStream(tmpFile);334InputStream manifest = (!Mflag && (mname != null)) ?335(new FileInputStream(mname)) : null;336) {337boolean updateOk = update(in, new BufferedOutputStream(out),338manifest, moduleInfos, null);339if (ok) {340ok = updateOk;341}342}343validateAndClose(tmpFile);344} else if (tflag) {345replaceFSC(filesMap);346// For the "list table contents" action, access using the347// ZipFile class is always most efficient since only a348// "one-finger" scan through the central directory is required.349String[] files = filesMapToFiles(filesMap);350if (fname != null) {351list(fname, files);352} else {353InputStream in = new FileInputStream(FileDescriptor.in);354try {355list(new BufferedInputStream(in), files);356} finally {357in.close();358}359}360} else if (xflag) {361replaceFSC(filesMap);362// For the extract action, when extracting all the entries,363// access using the ZipInputStream class is most efficient,364// since only a single sequential scan through the zip file is365// required. When using the ZipFile class, a "two-finger" scan366// is required, but this is likely to be more efficient when a367// partial extract is requested. In case the zip file has368// "leading garbage", we fall back from the ZipInputStream369// implementation to the ZipFile implementation, since only the370// latter can handle it.371372String[] files = filesMapToFiles(filesMap);373if (fname != null && files != null) {374extract(fname, files);375} else {376InputStream in = (fname == null)377? new FileInputStream(FileDescriptor.in)378: new FileInputStream(fname);379try {380if (!extract(new BufferedInputStream(in), files) && fname != null) {381extract(fname, files);382}383} finally {384in.close();385}386}387} else if (iflag) {388String[] files = filesMap.get(BASE_VERSION); // base entries only, can be null389genIndex(rootjar, files);390} else if (dflag) {391boolean found;392if (fname != null) {393try (ZipFile zf = new ZipFile(fname)) {394found = describeModule(zf);395}396} else {397try (FileInputStream fin = new FileInputStream(FileDescriptor.in)) {398found = describeModuleFromStream(fin);399}400}401if (!found)402error(getMsg("error.module.descriptor.not.found"));403}404} catch (IOException e) {405fatalError(e);406ok = false;407} catch (Error ee) {408ee.printStackTrace();409ok = false;410} catch (Throwable t) {411t.printStackTrace();412ok = false;413} finally {414if (tmpFile != null && tmpFile.exists())415tmpFile.delete();416}417out.flush();418err.flush();419return ok;420}421422private void validateAndClose(File tmpfile) throws IOException {423if (ok && isMultiRelease) {424try (ZipFile zf = new ZipFile(tmpfile)) {425ok = Validator.validate(this, zf);426if (!ok) {427error(formatMsg("error.validator.jarfile.invalid", fname));428}429} catch (IOException e) {430error(formatMsg2("error.validator.jarfile.exception", fname, e.getMessage()));431}432}433Path path = tmpfile.toPath();434try {435if (ok) {436if (fname != null) {437Files.move(path, Paths.get(fname), StandardCopyOption.REPLACE_EXISTING);438} else {439Files.copy(path, new FileOutputStream(FileDescriptor.out));440}441}442} finally {443Files.deleteIfExists(path);444}445}446447private String[] filesMapToFiles(Map<Integer,String[]> filesMap) {448if (filesMap.isEmpty()) return null;449return filesMap.entrySet()450.stream()451.flatMap(this::filesToEntryNames)452.toArray(String[]::new);453}454455Stream<String> filesToEntryNames(Map.Entry<Integer,String[]> fileEntries) {456int version = fileEntries.getKey();457Set<String> cpaths = pathsMap.get(version);458return Stream.of(fileEntries.getValue())459.map(f -> toVersionedName(toEntryName(f, cpaths, false), version));460}461462/**463* Parses command line arguments.464*/465boolean parseArgs(String args[]) {466/* Preprocess and expand @file arguments */467try {468args = CommandLine.parse(args);469} catch (FileNotFoundException e) {470fatalError(formatMsg("error.cant.open", e.getMessage()));471return false;472} catch (IOException e) {473fatalError(e);474return false;475}476/* parse flags */477int count = 1;478try {479String flags = args[0];480481// Note: flags.length == 2 can be treated as the short version of482// the GNU option since the there cannot be any other options,483// excluding -C, as per the old way.484if (flags.startsWith("--") ||485(flags.startsWith("-") && flags.length() == 2)) {486try {487count = GNUStyleOptions.parseOptions(this, args);488} catch (GNUStyleOptions.BadArgs x) {489if (info == null) {490if (x.showUsage) {491usageError(x.getMessage());492} else {493error(x.getMessage());494}495return false;496}497}498if (info != null) {499info.accept(out);500return true;501}502} else {503// Legacy/compatibility options504if (flags.startsWith("-")) {505flags = flags.substring(1);506}507for (int i = 0; i < flags.length(); i++) {508switch (flags.charAt(i)) {509case 'c':510if (xflag || tflag || uflag || iflag) {511usageError(getMsg("error.multiple.main.operations"));512return false;513}514cflag = true;515break;516case 'u':517if (cflag || xflag || tflag || iflag) {518usageError(getMsg("error.multiple.main.operations"));519return false;520}521uflag = true;522break;523case 'x':524if (cflag || uflag || tflag || iflag) {525usageError(getMsg("error.multiple.main.operations"));526return false;527}528xflag = true;529break;530case 't':531if (cflag || uflag || xflag || iflag) {532usageError(getMsg("error.multiple.main.operations"));533return false;534}535tflag = true;536break;537case 'M':538Mflag = true;539break;540case 'v':541vflag = true;542break;543case 'f':544fname = args[count++];545break;546case 'm':547mname = args[count++];548break;549case '0':550flag0 = true;551break;552case 'i':553if (cflag || uflag || xflag || tflag) {554usageError(getMsg("error.multiple.main.operations"));555return false;556}557// do not increase the counter, files will contain rootjar558rootjar = args[count++];559iflag = true;560break;561case 'e':562ename = args[count++];563break;564case 'P':565pflag = true;566break;567default:568usageError(formatMsg("error.illegal.option",569String.valueOf(flags.charAt(i))));570return false;571}572}573}574} catch (ArrayIndexOutOfBoundsException e) {575usageError(getMsg("main.usage.summary"));576return false;577}578if (!cflag && !tflag && !xflag && !uflag && !iflag && !dflag) {579usageError(getMsg("error.bad.option"));580return false;581}582583/* parse file arguments */584int n = args.length - count;585if (n > 0) {586int version = BASE_VERSION;587int k = 0;588String[] nameBuf = new String[n];589pathsMap.put(version, new HashSet<>());590try {591for (int i = count; i < args.length; i++) {592if (args[i].equals("-C")) {593if (dflag) {594// "--describe-module/-d" does not require file argument(s),595// but does accept --release596usageError(getMsg("error.bad.dflag"));597return false;598}599/* change the directory */600String dir = args[++i];601dir = (dir.endsWith(File.separator) ?602dir : (dir + File.separator));603dir = dir.replace(File.separatorChar, '/');604605boolean hasUNC = (File.separatorChar == '\\'&& dir.startsWith("//"));606while (dir.indexOf("//") > -1) {607dir = dir.replace("//", "/");608}609if (hasUNC) { // Restore Windows UNC path.610dir = "/" + dir;611}612pathsMap.get(version).add(dir);613nameBuf[k++] = dir + args[++i];614} else if (args[i].startsWith("--release")) {615int v = BASE_VERSION;616try {617v = Integer.valueOf(args[++i]);618} catch (NumberFormatException x) {619error(formatMsg("error.release.value.notnumber", args[i]));620// this will fall into the next error, thus returning false621}622if (v < 9) {623usageError(formatMsg("error.release.value.toosmall", String.valueOf(v)));624return false;625}626// associate the files, if any, with the previous version number627if (k > 0) {628String[] files = new String[k];629System.arraycopy(nameBuf, 0, files, 0, k);630filesMap.put(version, files);631isMultiRelease = version > BASE_VERSION;632}633// reset the counters and start with the new version number634k = 0;635nameBuf = new String[n];636version = v;637releaseValue = version;638pathsMap.put(version, new HashSet<>());639} else {640if (dflag) {641// "--describe-module/-d" does not require file argument(s),642// but does accept --release643usageError(getMsg("error.bad.dflag"));644return false;645}646nameBuf[k++] = args[i];647}648}649} catch (ArrayIndexOutOfBoundsException e) {650usageError(getMsg("error.bad.file.arg"));651return false;652}653// associate remaining files, if any, with a version654if (k > 0) {655String[] files = new String[k];656System.arraycopy(nameBuf, 0, files, 0, k);657filesMap.put(version, files);658isMultiRelease = version > BASE_VERSION;659}660} else if (cflag && (mname == null)) {661usageError(getMsg("error.bad.cflag"));662return false;663} else if (uflag) {664if ((mname != null) || (ename != null) || moduleVersion != null) {665/* just want to update the manifest */666return true;667} else {668usageError(getMsg("error.bad.uflag"));669return false;670}671}672return true;673}674675/*676* Add the package of the given resource name if it's a .class677* or a resource in a named package.678*/679void addPackageIfNamed(Set<String> packages, String name) {680if (name.startsWith(VERSIONS_DIR)) {681// trim the version dir prefix682int i0 = VERSIONS_DIR_LENGTH;683int i = name.indexOf('/', i0);684if (i <= 0) {685warn(formatMsg("warn.release.unexpected.versioned.entry", name));686return;687}688while (i0 < i) {689char c = name.charAt(i0);690if (c < '0' || c > '9') {691warn(formatMsg("warn.release.unexpected.versioned.entry", name));692return;693}694i0++;695}696name = name.substring(i + 1, name.length());697}698String pn = toPackageName(name);699// add if this is a class or resource in a package700if (Checks.isPackageName(pn)) {701packages.add(pn);702}703}704705private String toEntryName(String name, Set<String> cpaths, boolean isDir) {706name = name.replace(File.separatorChar, '/');707if (isDir) {708name = name.endsWith("/") ? name : name + "/";709}710String matchPath = "";711for (String path : cpaths) {712if (name.startsWith(path) && path.length() > matchPath.length()) {713matchPath = path;714}715}716name = safeName(name.substring(matchPath.length()));717// the old implementaton doesn't remove718// "./" if it was led by "/" (?)719if (name.startsWith("./")) {720name = name.substring(2);721}722return name;723}724725private static String toVersionedName(String name, int version) {726return version > BASE_VERSION727? VERSIONS_DIR + version + "/" + name : name;728}729730private static String toPackageName(String path) {731int index = path.lastIndexOf('/');732if (index != -1) {733return path.substring(0, index).replace('/', '.');734} else {735return "";736}737}738739private void expand() throws IOException {740for (int version : filesMap.keySet()) {741String[] files = filesMap.get(version);742expand(null, files, pathsMap.get(version), version);743}744}745746/**747* Expands list of files to process into full list of all files that748* can be found by recursively descending directories.749*750* @param dir parent directory751* @param files list of files to expand752* @param cpaths set of directories specified by -C option for the files753* @throws IOException if an I/O error occurs754*/755private void expand(File dir, String[] files, Set<String> cpaths, int version)756throws IOException757{758if (files == null)759return;760761for (int i = 0; i < files.length; i++) {762File f;763if (dir == null)764f = new File(files[i]);765else766f = new File(dir, files[i]);767768boolean isDir = f.isDirectory();769String name = toEntryName(f.getPath(), cpaths, isDir);770771if (version != BASE_VERSION) {772if (name.startsWith(VERSIONS_DIR)) {773// the entry starts with VERSIONS_DIR and version != BASE_VERSION,774// which means the "[dirs|files]" in --release v [dirs|files]775// includes VERSIONS_DIR-ed entries --> warning and skip (?)776error(formatMsg2("error.release.unexpected.versioned.entry",777name, String.valueOf(version)));778ok = false;779return;780}781name = toVersionedName(name, version);782}783784if (f.isFile()) {785Entry e = new Entry(f, name, false);786if (isModuleInfoEntry(name)) {787moduleInfos.putIfAbsent(name, Files.readAllBytes(f.toPath()));788if (uflag)789entryMap.put(name, e);790} else if (entries.add(e)) {791if (uflag)792entryMap.put(name, e);793}794} else if (isDir) {795Entry e = new Entry(f, name, true);796if (entries.add(e)) {797// utilize entryMap for the duplicate dir check even in798// case of cflag == true.799// dir name confilict/duplicate could happen with -C option.800// just remove the last "e" from the "entries" (zos will fail801// with "duplicated" entries), but continue expanding the802// sub tree803if (entryMap.containsKey(name)) {804entries.remove(e);805} else {806entryMap.put(name, e);807}808expand(f, f.list(), cpaths, version);809}810} else {811error(formatMsg("error.nosuch.fileordir", String.valueOf(f)));812ok = false;813}814}815}816817/**818* Creates a new JAR file.819*/820void create(OutputStream out, Manifest manifest) throws IOException821{822try (ZipOutputStream zos = new JarOutputStream(out)) {823if (flag0) {824zos.setMethod(ZipOutputStream.STORED);825}826// TODO: check module-info attributes against manifest ??827if (manifest != null) {828if (vflag) {829output(getMsg("out.added.manifest"));830}831ZipEntry e = new ZipEntry(MANIFEST_DIR);832e.setTime(System.currentTimeMillis());833e.setSize(0);834e.setCrc(0);835zos.putNextEntry(e);836e = new ZipEntry(MANIFEST_NAME);837e.setTime(System.currentTimeMillis());838if (flag0) {839crc32Manifest(e, manifest);840}841zos.putNextEntry(e);842manifest.write(zos);843zos.closeEntry();844}845updateModuleInfo(moduleInfos, zos);846for (Entry entry : entries) {847addFile(zos, entry);848}849}850}851852private char toUpperCaseASCII(char c) {853return (c < 'a' || c > 'z') ? c : (char) (c + 'A' - 'a');854}855856/**857* Compares two strings for equality, ignoring case. The second858* argument must contain only upper-case ASCII characters.859* We don't want case comparison to be locale-dependent (else we860* have the notorious "turkish i bug").861*/862private boolean equalsIgnoreCase(String s, String upper) {863assert upper.toUpperCase(java.util.Locale.ENGLISH).equals(upper);864int len;865if ((len = s.length()) != upper.length())866return false;867for (int i = 0; i < len; i++) {868char c1 = s.charAt(i);869char c2 = upper.charAt(i);870if (c1 != c2 && toUpperCaseASCII(c1) != c2)871return false;872}873return true;874}875876/**877* Updates an existing jar file.878*/879boolean update(InputStream in, OutputStream out,880InputStream newManifest,881Map<String,byte[]> moduleInfos,882JarIndex jarIndex) throws IOException883{884ZipInputStream zis = new ZipInputStream(in);885ZipOutputStream zos = new JarOutputStream(out);886ZipEntry e = null;887boolean foundManifest = false;888boolean updateOk = true;889890// All actual entries added/updated/existing, in the jar file (excl manifest891// and module-info.class ).892Set<String> jentries = new HashSet<>();893894if (jarIndex != null) {895addIndex(jarIndex, zos);896}897898// put the old entries first, replace if necessary899while ((e = zis.getNextEntry()) != null) {900String name = e.getName();901902boolean isManifestEntry = equalsIgnoreCase(name, MANIFEST_NAME);903boolean isModuleInfoEntry = isModuleInfoEntry(name);904905if ((jarIndex != null && equalsIgnoreCase(name, INDEX_NAME))906|| (Mflag && isManifestEntry)) {907continue;908} else if (isManifestEntry && ((newManifest != null) ||909(ename != null) || isMultiRelease)) {910foundManifest = true;911if (newManifest != null) {912// Don't read from the newManifest InputStream, as we913// might need it below, and we can't re-read the same data914// twice.915try (FileInputStream fis = new FileInputStream(mname)) {916if (isAmbiguousMainClass(new Manifest(fis))) {917return false;918}919}920}921// Update the manifest.922Manifest old = new Manifest(zis);923if (newManifest != null) {924old.read(newManifest);925}926if (!updateManifest(old, zos)) {927return false;928}929} else if (moduleInfos != null && isModuleInfoEntry) {930moduleInfos.putIfAbsent(name, zis.readAllBytes());931} else {932boolean isDir = e.isDirectory();933if (!entryMap.containsKey(name)) { // copy the old stuff934// do our own compression935ZipEntry e2 = new ZipEntry(name);936e2.setMethod(e.getMethod());937e2.setTime(e.getTime());938e2.setComment(e.getComment());939e2.setExtra(e.getExtra());940if (e.getMethod() == ZipEntry.STORED) {941e2.setSize(e.getSize());942e2.setCrc(e.getCrc());943}944zos.putNextEntry(e2);945copy(zis, zos);946} else { // replace with the new files947Entry ent = entryMap.get(name);948addFile(zos, ent);949entryMap.remove(name);950entries.remove(ent);951isDir = ent.isDir;952}953if (!isDir) {954jentries.add(name);955}956}957}958959// add the remaining new files960for (Entry entry : entries) {961addFile(zos, entry);962if (!entry.isDir) {963jentries.add(entry.name);964}965}966if (!foundManifest) {967if (newManifest != null) {968Manifest m = new Manifest(newManifest);969updateOk = !isAmbiguousMainClass(m);970if (updateOk) {971if (!updateManifest(m, zos)) {972updateOk = false;973}974}975} else if (ename != null) {976if (!updateManifest(new Manifest(), zos)) {977updateOk = false;978}979}980}981if (updateOk) {982if (moduleInfos != null && !moduleInfos.isEmpty()) {983Set<String> pkgs = new HashSet<>();984jentries.forEach( je -> addPackageIfNamed(pkgs, je));985addExtendedModuleAttributes(moduleInfos, pkgs);986updateOk = checkModuleInfo(moduleInfos.get(MODULE_INFO), jentries);987updateModuleInfo(moduleInfos, zos);988// TODO: check manifest main classes, etc989} else if (moduleVersion != null || modulesToHash != null) {990error(getMsg("error.module.options.without.info"));991updateOk = false;992}993}994zis.close();995zos.close();996return updateOk;997}998999private void addIndex(JarIndex index, ZipOutputStream zos)1000throws IOException1001{1002ZipEntry e = new ZipEntry(INDEX_NAME);1003e.setTime(System.currentTimeMillis());1004if (flag0) {1005CRC32OutputStream os = new CRC32OutputStream();1006index.write(os);1007os.updateEntry(e);1008}1009zos.putNextEntry(e);1010index.write(zos);1011zos.closeEntry();1012}10131014private void updateModuleInfo(Map<String,byte[]> moduleInfos, ZipOutputStream zos)1015throws IOException1016{1017String fmt = uflag ? "out.update.module-info": "out.added.module-info";1018for (Map.Entry<String,byte[]> mi : moduleInfos.entrySet()) {1019String name = mi.getKey();1020byte[] bytes = mi.getValue();1021ZipEntry e = new ZipEntry(name);1022e.setTime(System.currentTimeMillis());1023if (flag0) {1024crc32ModuleInfo(e, bytes);1025}1026zos.putNextEntry(e);1027zos.write(bytes);1028zos.closeEntry();1029if (vflag) {1030output(formatMsg(fmt, name));1031}1032}1033}10341035private boolean updateManifest(Manifest m, ZipOutputStream zos)1036throws IOException1037{1038addVersion(m);1039addCreatedBy(m);1040if (ename != null) {1041addMainClass(m, ename);1042}1043if (isMultiRelease) {1044addMultiRelease(m);1045}1046ZipEntry e = new ZipEntry(MANIFEST_NAME);1047e.setTime(System.currentTimeMillis());1048if (flag0) {1049crc32Manifest(e, m);1050}1051zos.putNextEntry(e);1052m.write(zos);1053if (vflag) {1054output(getMsg("out.update.manifest"));1055}1056return true;1057}10581059private static final boolean isWinDriveLetter(char c) {1060return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));1061}10621063private String safeName(String name) {1064if (!pflag) {1065int len = name.length();1066int i = name.lastIndexOf("../");1067if (i == -1) {1068i = 0;1069} else {1070i += 3; // strip any dot-dot components1071}1072if (File.separatorChar == '\\') {1073// the spec requests no drive letter. skip if1074// the entry name has one.1075while (i < len) {1076int off = i;1077if (i + 1 < len &&1078name.charAt(i + 1) == ':' &&1079isWinDriveLetter(name.charAt(i))) {1080i += 2;1081}1082while (i < len && name.charAt(i) == '/') {1083i++;1084}1085if (i == off) {1086break;1087}1088}1089} else {1090while (i < len && name.charAt(i) == '/') {1091i++;1092}1093}1094if (i != 0) {1095name = name.substring(i);1096}1097}1098return name;1099}11001101private void addVersion(Manifest m) {1102Attributes global = m.getMainAttributes();1103if (global.getValue(Attributes.Name.MANIFEST_VERSION) == null) {1104global.put(Attributes.Name.MANIFEST_VERSION, VERSION);1105}1106}11071108private void addCreatedBy(Manifest m) {1109Attributes global = m.getMainAttributes();1110if (global.getValue(new Attributes.Name("Created-By")) == null) {1111String javaVendor = System.getProperty("java.vendor");1112String jdkVersion = System.getProperty("java.version");1113global.put(new Attributes.Name("Created-By"), jdkVersion + " (" +1114javaVendor + ")");1115}1116}11171118private void addMainClass(Manifest m, String mainApp) {1119Attributes global = m.getMainAttributes();11201121// overrides any existing Main-Class attribute1122global.put(Attributes.Name.MAIN_CLASS, mainApp);1123}11241125private void addMultiRelease(Manifest m) {1126Attributes global = m.getMainAttributes();1127global.put(Attributes.Name.MULTI_RELEASE, "true");1128}11291130private boolean isAmbiguousMainClass(Manifest m) {1131if (ename != null) {1132Attributes global = m.getMainAttributes();1133if ((global.get(Attributes.Name.MAIN_CLASS) != null)) {1134usageError(getMsg("error.bad.eflag"));1135return true;1136}1137}1138return false;1139}11401141/**1142* Adds a new file entry to the ZIP output stream.1143*/1144void addFile(ZipOutputStream zos, Entry entry) throws IOException {11451146File file = entry.file;1147String name = entry.name;1148boolean isDir = entry.isDir;11491150if (name.isEmpty() || name.equals(".") || name.equals(zname)) {1151return;1152} else if ((name.equals(MANIFEST_DIR) || name.equals(MANIFEST_NAME))1153&& !Mflag) {1154if (vflag) {1155output(formatMsg("out.ignore.entry", name));1156}1157return;1158} else if (name.equals(MODULE_INFO)) {1159throw new Error("Unexpected module info: " + name);1160}11611162long size = isDir ? 0 : file.length();11631164if (vflag) {1165out.print(formatMsg("out.adding", name));1166}1167ZipEntry e = new ZipEntry(name);1168e.setTime(file.lastModified());1169if (size == 0) {1170e.setMethod(ZipEntry.STORED);1171e.setSize(0);1172e.setCrc(0);1173} else if (flag0) {1174crc32File(e, file);1175}1176zos.putNextEntry(e);1177if (!isDir) {1178copy(file, zos);1179}1180zos.closeEntry();1181/* report how much compression occurred. */1182if (vflag) {1183size = e.getSize();1184long csize = e.getCompressedSize();1185out.print(formatMsg2("out.size", String.valueOf(size),1186String.valueOf(csize)));1187if (e.getMethod() == ZipEntry.DEFLATED) {1188long ratio = 0;1189if (size != 0) {1190ratio = ((size - csize) * 100) / size;1191}1192output(formatMsg("out.deflated", String.valueOf(ratio)));1193} else {1194output(getMsg("out.stored"));1195}1196}1197}11981199/**1200* A buffer for use only by copy(InputStream, OutputStream).1201* Not as clean as allocating a new buffer as needed by copy,1202* but significantly more efficient.1203*/1204private byte[] copyBuf = new byte[8192];12051206/**1207* Copies all bytes from the input stream to the output stream.1208* Does not close or flush either stream.1209*1210* @param from the input stream to read from1211* @param to the output stream to write to1212* @throws IOException if an I/O error occurs1213*/1214private void copy(InputStream from, OutputStream to) throws IOException {1215int n;1216while ((n = from.read(copyBuf)) != -1)1217to.write(copyBuf, 0, n);1218}12191220/**1221* Copies all bytes from the input file to the output stream.1222* Does not close or flush the output stream.1223*1224* @param from the input file to read from1225* @param to the output stream to write to1226* @throws IOException if an I/O error occurs1227*/1228private void copy(File from, OutputStream to) throws IOException {1229try (InputStream in = new FileInputStream(from)) {1230copy(in, to);1231}1232}12331234/**1235* Copies all bytes from the input stream to the output file.1236* Does not close the input stream.1237*1238* @param from the input stream to read from1239* @param to the output file to write to1240* @throws IOException if an I/O error occurs1241*/1242private void copy(InputStream from, File to) throws IOException {1243try (OutputStream out = new FileOutputStream(to)) {1244copy(from, out);1245}1246}12471248/**1249* Computes the crc32 of a module-info.class. This is necessary when the1250* ZipOutputStream is in STORED mode.1251*/1252private void crc32ModuleInfo(ZipEntry e, byte[] bytes) throws IOException {1253CRC32OutputStream os = new CRC32OutputStream();1254ByteArrayInputStream in = new ByteArrayInputStream(bytes);1255in.transferTo(os);1256os.updateEntry(e);1257}12581259/**1260* Computes the crc32 of a Manifest. This is necessary when the1261* ZipOutputStream is in STORED mode.1262*/1263private void crc32Manifest(ZipEntry e, Manifest m) throws IOException {1264CRC32OutputStream os = new CRC32OutputStream();1265m.write(os);1266os.updateEntry(e);1267}12681269/**1270* Computes the crc32 of a File. This is necessary when the1271* ZipOutputStream is in STORED mode.1272*/1273private void crc32File(ZipEntry e, File f) throws IOException {1274CRC32OutputStream os = new CRC32OutputStream();1275copy(f, os);1276if (os.n != f.length()) {1277throw new JarException(formatMsg(1278"error.incorrect.length", f.getPath()));1279}1280os.updateEntry(e);1281}12821283void replaceFSC(Map<Integer, String []> filesMap) {1284filesMap.keySet().forEach(version -> {1285String[] files = filesMap.get(version);1286if (files != null) {1287for (int i = 0; i < files.length; i++) {1288files[i] = files[i].replace(File.separatorChar, '/');1289}1290}1291});1292}12931294@SuppressWarnings("serial")1295Set<ZipEntry> newDirSet() {1296return new HashSet<ZipEntry>() {1297public boolean add(ZipEntry e) {1298return ((e == null || useExtractionTime) ? false : super.add(e));1299}};1300}13011302void updateLastModifiedTime(Set<ZipEntry> zes) throws IOException {1303for (ZipEntry ze : zes) {1304long lastModified = ze.getTime();1305if (lastModified != -1) {1306String name = safeName(ze.getName().replace(File.separatorChar, '/'));1307if (name.length() != 0) {1308File f = new File(name.replace('/', File.separatorChar));1309f.setLastModified(lastModified);1310}1311}1312}1313}13141315/**1316* Extracts specified entries from JAR file.1317*1318* @return whether entries were found and successfully extracted1319* (indicating this was a zip file without "leading garbage")1320*/1321boolean extract(InputStream in, String files[]) throws IOException {1322ZipInputStream zis = new ZipInputStream(in);1323ZipEntry e;1324// Set of all directory entries specified in archive. Disallows1325// null entries. Disallows all entries if using pre-6.0 behavior.1326boolean entriesFound = false;1327Set<ZipEntry> dirs = newDirSet();1328while ((e = zis.getNextEntry()) != null) {1329entriesFound = true;1330if (files == null) {1331dirs.add(extractFile(zis, e));1332} else {1333String name = e.getName();1334for (String file : files) {1335if (name.startsWith(file)) {1336dirs.add(extractFile(zis, e));1337break;1338}1339}1340}1341}13421343// Update timestamps of directories specified in archive with their1344// timestamps as given in the archive. We do this after extraction,1345// instead of during, because creating a file in a directory changes1346// that directory's timestamp.1347updateLastModifiedTime(dirs);13481349return entriesFound;1350}13511352/**1353* Extracts specified entries from JAR file, via ZipFile.1354*/1355void extract(String fname, String files[]) throws IOException {1356ZipFile zf = new ZipFile(fname);1357Set<ZipEntry> dirs = newDirSet();1358Enumeration<? extends ZipEntry> zes = zf.entries();1359while (zes.hasMoreElements()) {1360ZipEntry e = zes.nextElement();1361if (files == null) {1362dirs.add(extractFile(zf.getInputStream(e), e));1363} else {1364String name = e.getName();1365for (String file : files) {1366if (name.startsWith(file)) {1367dirs.add(extractFile(zf.getInputStream(e), e));1368break;1369}1370}1371}1372}1373zf.close();1374updateLastModifiedTime(dirs);1375}13761377/**1378* Extracts next entry from JAR file, creating directories as needed. If1379* the entry is for a directory which doesn't exist prior to this1380* invocation, returns that entry, otherwise returns null.1381*/1382ZipEntry extractFile(InputStream is, ZipEntry e) throws IOException {1383ZipEntry rc = null;1384// The spec requres all slashes MUST be forward '/', it is possible1385// an offending zip/jar entry may uses the backwards slash in its1386// name. It might cause problem on Windows platform as it skips1387// our "safe" check for leading slahs and dot-dot. So replace them1388// with '/'.1389String name = safeName(e.getName().replace(File.separatorChar, '/'));1390if (name.length() == 0) {1391return rc; // leading '/' or 'dot-dot' only path1392}1393File f = new File(name.replace('/', File.separatorChar));1394if (e.isDirectory()) {1395if (f.exists()) {1396if (!f.isDirectory()) {1397throw new IOException(formatMsg("error.create.dir",1398f.getPath()));1399}1400} else {1401if (!f.mkdirs()) {1402throw new IOException(formatMsg("error.create.dir",1403f.getPath()));1404} else {1405rc = e;1406}1407}14081409if (vflag) {1410output(formatMsg("out.create", name));1411}1412} else {1413if (f.getParent() != null) {1414File d = new File(f.getParent());1415if (!d.exists() && !d.mkdirs() || !d.isDirectory()) {1416throw new IOException(formatMsg(1417"error.create.dir", d.getPath()));1418}1419}1420try {1421copy(is, f);1422} finally {1423if (is instanceof ZipInputStream)1424((ZipInputStream)is).closeEntry();1425else1426is.close();1427}1428if (vflag) {1429if (e.getMethod() == ZipEntry.DEFLATED) {1430output(formatMsg("out.inflated", name));1431} else {1432output(formatMsg("out.extracted", name));1433}1434}1435}1436if (!useExtractionTime) {1437long lastModified = e.getTime();1438if (lastModified != -1) {1439f.setLastModified(lastModified);1440}1441}1442return rc;1443}14441445/**1446* Lists contents of JAR file.1447*/1448void list(InputStream in, String files[]) throws IOException {1449ZipInputStream zis = new ZipInputStream(in);1450ZipEntry e;1451while ((e = zis.getNextEntry()) != null) {1452/*1453* In the case of a compressed (deflated) entry, the entry size1454* is stored immediately following the entry data and cannot be1455* determined until the entry is fully read. Therefore, we close1456* the entry first before printing out its attributes.1457*/1458zis.closeEntry();1459printEntry(e, files);1460}1461}14621463/**1464* Lists contents of JAR file, via ZipFile.1465*/1466void list(String fname, String files[]) throws IOException {1467ZipFile zf = new ZipFile(fname);1468Enumeration<? extends ZipEntry> zes = zf.entries();1469while (zes.hasMoreElements()) {1470printEntry(zes.nextElement(), files);1471}1472zf.close();1473}14741475/**1476* Outputs the class index table to the INDEX.LIST file of the1477* root jar file.1478*/1479void dumpIndex(String rootjar, JarIndex index) throws IOException {1480File jarFile = new File(rootjar);1481Path jarPath = jarFile.toPath();1482Path tmpPath = createTempFileInSameDirectoryAs(jarFile).toPath();1483try {1484if (update(Files.newInputStream(jarPath),1485Files.newOutputStream(tmpPath),1486null, null, index)) {1487try {1488Files.move(tmpPath, jarPath, REPLACE_EXISTING);1489} catch (IOException e) {1490throw new IOException(getMsg("error.write.file"), e);1491}1492}1493} finally {1494Files.deleteIfExists(tmpPath);1495}1496}14971498private HashSet<String> jarPaths = new HashSet<String>();14991500/**1501* Generates the transitive closure of the Class-Path attribute for1502* the specified jar file.1503*/1504List<String> getJarPath(String jar) throws IOException {1505List<String> files = new ArrayList<String>();1506files.add(jar);1507jarPaths.add(jar);15081509// take out the current path1510String path = jar.substring(0, Math.max(0, jar.lastIndexOf('/') + 1));15111512// class path attribute will give us jar file name with1513// '/' as separators, so we need to change them to the1514// appropriate one before we open the jar file.1515JarFile rf = new JarFile(jar.replace('/', File.separatorChar));15161517if (rf != null) {1518Manifest man = rf.getManifest();1519if (man != null) {1520Attributes attr = man.getMainAttributes();1521if (attr != null) {1522String value = attr.getValue(Attributes.Name.CLASS_PATH);1523if (value != null) {1524StringTokenizer st = new StringTokenizer(value);1525while (st.hasMoreTokens()) {1526String ajar = st.nextToken();1527if (!ajar.endsWith("/")) { // it is a jar file1528ajar = path.concat(ajar);1529/* check on cyclic dependency */1530if (! jarPaths.contains(ajar)) {1531files.addAll(getJarPath(ajar));1532}1533}1534}1535}1536}1537}1538}1539rf.close();1540return files;1541}15421543/**1544* Generates class index file for the specified root jar file.1545*/1546void genIndex(String rootjar, String[] files) throws IOException {1547List<String> jars = getJarPath(rootjar);1548int njars = jars.size();1549String[] jarfiles;15501551if (njars == 1 && files != null) {1552// no class-path attribute defined in rootjar, will1553// use command line specified list of jars1554for (int i = 0; i < files.length; i++) {1555jars.addAll(getJarPath(files[i]));1556}1557njars = jars.size();1558}1559jarfiles = jars.toArray(new String[njars]);1560JarIndex index = new JarIndex(jarfiles);1561dumpIndex(rootjar, index);1562}15631564/**1565* Prints entry information, if requested.1566*/1567void printEntry(ZipEntry e, String[] files) throws IOException {1568if (files == null) {1569printEntry(e);1570} else {1571String name = e.getName();1572for (String file : files) {1573if (name.startsWith(file)) {1574printEntry(e);1575return;1576}1577}1578}1579}15801581/**1582* Prints entry information.1583*/1584void printEntry(ZipEntry e) throws IOException {1585if (vflag) {1586StringBuilder sb = new StringBuilder();1587String s = Long.toString(e.getSize());1588for (int i = 6 - s.length(); i > 0; --i) {1589sb.append(' ');1590}1591sb.append(s).append(' ').append(new Date(e.getTime()).toString());1592sb.append(' ').append(e.getName());1593output(sb.toString());1594} else {1595output(e.getName());1596}1597}15981599/**1600* Prints usage message.1601*/1602void usageError(String s) {1603err.println(s);1604err.println(getMsg("main.usage.summary.try"));1605}16061607/**1608* A fatal exception has been caught. No recovery possible1609*/1610void fatalError(Exception e) {1611e.printStackTrace();1612}16131614/**1615* A fatal condition has been detected; message is "s".1616* No recovery possible1617*/1618void fatalError(String s) {1619error(program + ": " + s);1620}16211622/**1623* Print an output message; like verbose output and the like1624*/1625protected void output(String s) {1626out.println(s);1627}16281629/**1630* Print an error message; like something is broken1631*/1632void error(String s) {1633err.println(s);1634}16351636/**1637* Print a warning message1638*/1639void warn(String s) {1640err.println(s);1641}16421643/**1644* Main routine to start program.1645*/1646public static void main(String args[]) {1647Main jartool = new Main(System.out, System.err, "jar");1648System.exit(jartool.run(args) ? 0 : 1);1649}16501651/**1652* An OutputStream that doesn't send its output anywhere, (but could).1653* It's here to find the CRC32 of an input file, necessary for STORED1654* mode in ZIP.1655*/1656private static class CRC32OutputStream extends java.io.OutputStream {1657final CRC32 crc = new CRC32();1658long n = 0;16591660CRC32OutputStream() {}16611662public void write(int r) throws IOException {1663crc.update(r);1664n++;1665}16661667public void write(byte[] b, int off, int len) throws IOException {1668crc.update(b, off, len);1669n += len;1670}16711672/**1673* Updates a ZipEntry which describes the data read by this1674* output stream, in STORED mode.1675*/1676public void updateEntry(ZipEntry e) {1677e.setMethod(ZipEntry.STORED);1678e.setSize(n);1679e.setCrc(crc.getValue());1680}1681}16821683/**1684* Attempt to create temporary file in the system-provided temporary folder, if failed attempts1685* to create it in the same folder as the file in parameter (if any)1686*/1687private File createTemporaryFile(String tmpbase, String suffix) {1688File tmpfile = null;16891690try {1691tmpfile = File.createTempFile(tmpbase, suffix);1692} catch (IOException | SecurityException e) {1693// Unable to create file due to permission violation or security exception1694}1695if (tmpfile == null) {1696// Were unable to create temporary file, fall back to temporary file in the same folder1697if (fname != null) {1698try {1699File tmpfolder = new File(fname).getAbsoluteFile().getParentFile();1700tmpfile = File.createTempFile(fname, ".tmp" + suffix, tmpfolder);1701} catch (IOException ioe) {1702// Last option failed - fall gracefully1703fatalError(ioe);1704}1705} else {1706// No options left - we can not compress to stdout without access to the temporary folder1707fatalError(new IOException(getMsg("error.create.tempfile")));1708}1709}1710return tmpfile;1711}17121713// Modular jar support17141715/**1716* Associates a module descriptor's zip entry name along with its1717* bytes and an optional URI. Used when describing modules.1718*/1719interface ModuleInfoEntry {1720String name();1721Optional<String> uriString();1722InputStream bytes() throws IOException;1723}17241725static class ZipFileModuleInfoEntry implements ModuleInfoEntry {1726private final ZipFile zipFile;1727private final ZipEntry entry;1728ZipFileModuleInfoEntry(ZipFile zipFile, ZipEntry entry) {1729this.zipFile = zipFile;1730this.entry = entry;1731}1732@Override public String name() { return entry.getName(); }1733@Override public InputStream bytes() throws IOException {1734return zipFile.getInputStream(entry);1735}1736/** Returns an optional containing the effective URI. */1737@Override public Optional<String> uriString() {1738String uri = (Paths.get(zipFile.getName())).toUri().toString();1739uri = "jar:" + uri + "!/" + entry.getName();1740return Optional.of(uri);1741}1742}17431744static class StreamedModuleInfoEntry implements ModuleInfoEntry {1745private final String name;1746private final byte[] bytes;1747StreamedModuleInfoEntry(String name, byte[] bytes) {1748this.name = name;1749this.bytes = bytes;1750}1751@Override public String name() { return name; }1752@Override public InputStream bytes() throws IOException {1753return new ByteArrayInputStream(bytes);1754}1755/** Returns an empty optional. */1756@Override public Optional<String> uriString() {1757return Optional.empty(); // no URI can be derived1758}1759}17601761/** Describes a module from a given zip file. */1762private boolean describeModule(ZipFile zipFile) throws IOException {1763ZipFileModuleInfoEntry[] infos = zipFile.stream()1764.filter(e -> isModuleInfoEntry(e.getName()))1765.sorted(ENTRY_COMPARATOR)1766.map(e -> new ZipFileModuleInfoEntry(zipFile, e))1767.toArray(ZipFileModuleInfoEntry[]::new);17681769if (infos.length == 0) {1770// No module descriptor found, derive and describe the automatic module1771String fn = zipFile.getName();1772ModuleFinder mf = ModuleFinder.of(Paths.get(fn));1773try {1774Set<ModuleReference> mref = mf.findAll();1775if (mref.isEmpty()) {1776output(formatMsg("error.unable.derive.automodule", fn));1777return true;1778}1779ModuleDescriptor md = mref.iterator().next().descriptor();1780output(getMsg("out.automodule") + "\n");1781describeModule(md, null, null, "");1782} catch (FindException e) {1783String msg = formatMsg("error.unable.derive.automodule", fn);1784Throwable t = e.getCause();1785if (t != null)1786msg = msg + "\n" + t.getMessage();1787output(msg);1788}1789} else {1790return describeModuleFromEntries(infos);1791}1792return true;1793}17941795private boolean describeModuleFromStream(FileInputStream fis)1796throws IOException1797{1798List<ModuleInfoEntry> infos = new LinkedList<>();17991800try (BufferedInputStream bis = new BufferedInputStream(fis);1801ZipInputStream zis = new ZipInputStream(bis)) {1802ZipEntry e;1803while ((e = zis.getNextEntry()) != null) {1804String ename = e.getName();1805if (isModuleInfoEntry(ename)) {1806infos.add(new StreamedModuleInfoEntry(ename, zis.readAllBytes()));1807}1808}1809}18101811if (infos.size() == 0)1812return false;18131814ModuleInfoEntry[] sorted = infos.stream()1815.sorted(Comparator.comparing(ModuleInfoEntry::name, ENTRYNAME_COMPARATOR))1816.toArray(ModuleInfoEntry[]::new);18171818return describeModuleFromEntries(sorted);1819}18201821private boolean lessThanEqualReleaseValue(ModuleInfoEntry entry) {1822return intVersionFromEntry(entry) <= releaseValue ? true : false;1823}18241825private static String versionFromEntryName(String name) {1826String s = name.substring(VERSIONS_DIR_LENGTH);1827return s.substring(0, s.indexOf("/"));1828}18291830private static int intVersionFromEntry(ModuleInfoEntry entry) {1831String name = entry.name();1832if (!name.startsWith(VERSIONS_DIR))1833return BASE_VERSION;18341835String s = name.substring(VERSIONS_DIR_LENGTH);1836s = s.substring(0, s.indexOf('/'));1837return Integer.valueOf(s);1838}18391840/**1841* Describes a single module descriptor, determined by the specified1842* --release, if any, from the given ordered entries.1843* The given infos must be ordered as per ENTRY_COMPARATOR.1844*/1845private boolean describeModuleFromEntries(ModuleInfoEntry[] infos)1846throws IOException1847{1848assert infos.length > 0;18491850// Informative: output all non-root descriptors, if any1851String releases = Arrays.stream(infos)1852.filter(e -> !e.name().equals(MODULE_INFO))1853.map(ModuleInfoEntry::name)1854.map(Main::versionFromEntryName)1855.collect(joining(" "));1856if (!releases.isEmpty())1857output("releases: " + releases + "\n");18581859// Describe the operative descriptor for the specified --release, if any1860if (releaseValue != -1) {1861ModuleInfoEntry entry = null;1862int i = 0;1863while (i < infos.length && lessThanEqualReleaseValue(infos[i])) {1864entry = infos[i];1865i++;1866}18671868if (entry == null) {1869output(formatMsg("error.no.operative.descriptor",1870String.valueOf(releaseValue)));1871return false;1872}18731874String uriString = entry.uriString().orElse("");1875try (InputStream is = entry.bytes()) {1876describeModule(is, uriString);1877}1878} else {1879// no specific --release specified, output the root, if any1880if (infos[0].name().equals(MODULE_INFO)) {1881String uriString = infos[0].uriString().orElse("");1882try (InputStream is = infos[0].bytes()) {1883describeModule(is, uriString);1884}1885} else {1886// no root, output message to specify --release1887output(getMsg("error.no.root.descriptor"));1888}1889}1890return true;1891}18921893static <T> String toLowerCaseString(Collection<T> set) {1894if (set.isEmpty()) { return ""; }1895return " " + set.stream().map(e -> e.toString().toLowerCase(Locale.ROOT))1896.sorted().collect(joining(" "));1897}18981899static <T> String toString(Collection<T> set) {1900if (set.isEmpty()) { return ""; }1901return " " + set.stream().map(e -> e.toString()).sorted().collect(joining(" "));1902}19031904private void describeModule(InputStream entryInputStream, String uriString)1905throws IOException1906{1907ModuleInfo.Attributes attrs = ModuleInfo.read(entryInputStream, null);1908ModuleDescriptor md = attrs.descriptor();1909ModuleTarget target = attrs.target();1910ModuleHashes hashes = attrs.recordedHashes();19111912describeModule(md, target, hashes, uriString);1913}19141915private void describeModule(ModuleDescriptor md,1916ModuleTarget target,1917ModuleHashes hashes,1918String uriString)1919throws IOException1920{1921StringBuilder sb = new StringBuilder();19221923sb.append(md.toNameAndVersion());19241925if (!uriString.isEmpty())1926sb.append(" ").append(uriString);1927if (md.isOpen())1928sb.append(" open");1929if (md.isAutomatic())1930sb.append(" automatic");1931sb.append("\n");19321933// unqualified exports (sorted by package)1934md.exports().stream()1935.sorted(Comparator.comparing(Exports::source))1936.filter(e -> !e.isQualified())1937.forEach(e -> sb.append("exports ").append(e.source())1938.append(toLowerCaseString(e.modifiers()))1939.append("\n"));19401941// dependences1942md.requires().stream().sorted()1943.forEach(r -> sb.append("requires ").append(r.name())1944.append(toLowerCaseString(r.modifiers()))1945.append("\n"));19461947// service use and provides1948md.uses().stream().sorted()1949.forEach(s -> sb.append("uses ").append(s).append("\n"));19501951md.provides().stream()1952.sorted(Comparator.comparing(Provides::service))1953.forEach(p -> sb.append("provides ").append(p.service())1954.append(" with")1955.append(toString(p.providers()))1956.append("\n"));19571958// qualified exports1959md.exports().stream()1960.sorted(Comparator.comparing(Exports::source))1961.filter(Exports::isQualified)1962.forEach(e -> sb.append("qualified exports ").append(e.source())1963.append(" to").append(toLowerCaseString(e.targets()))1964.append("\n"));19651966// open packages1967md.opens().stream()1968.sorted(Comparator.comparing(Opens::source))1969.filter(o -> !o.isQualified())1970.forEach(o -> sb.append("opens ").append(o.source())1971.append(toLowerCaseString(o.modifiers()))1972.append("\n"));19731974md.opens().stream()1975.sorted(Comparator.comparing(Opens::source))1976.filter(Opens::isQualified)1977.forEach(o -> sb.append("qualified opens ").append(o.source())1978.append(toLowerCaseString(o.modifiers()))1979.append(" to").append(toLowerCaseString(o.targets()))1980.append("\n"));19811982// non-exported/non-open packages1983Set<String> concealed = new TreeSet<>(md.packages());1984md.exports().stream().map(Exports::source).forEach(concealed::remove);1985md.opens().stream().map(Opens::source).forEach(concealed::remove);1986concealed.forEach(p -> sb.append("contains ").append(p).append("\n"));19871988md.mainClass().ifPresent(v -> sb.append("main-class ").append(v).append("\n"));19891990if (target != null) {1991String targetPlatform = target.targetPlatform();1992if (!targetPlatform.isEmpty())1993sb.append("platform ").append(targetPlatform).append("\n");1994}19951996if (hashes != null) {1997hashes.names().stream().sorted().forEach(1998mod -> sb.append("hashes ").append(mod).append(" ")1999.append(hashes.algorithm()).append(" ")2000.append(toHex(hashes.hashFor(mod)))2001.append("\n"));2002}20032004output(sb.toString());2005}20062007private static String toHex(byte[] ba) {2008StringBuilder sb = new StringBuilder(ba.length << 1);2009for (byte b: ba) {2010sb.append(String.format("%02x", b & 0xff));2011}2012return sb.toString();2013}20142015static String toBinaryName(String classname) {2016return (classname.replace('.', '/')) + ".class";2017}20182019private boolean checkModuleInfo(byte[] moduleInfoBytes, Set<String> entries)2020throws IOException2021{2022boolean ok = true;2023if (moduleInfoBytes != null) { // no root module-info.class if null2024try {2025// ModuleDescriptor.read() checks open/exported pkgs vs packages2026ModuleDescriptor md = ModuleDescriptor.read(ByteBuffer.wrap(moduleInfoBytes));2027// A module must have the implementation class of the services it 'provides'.2028if (md.provides().stream().map(Provides::providers).flatMap(List::stream)2029.filter(p -> !entries.contains(toBinaryName(p)))2030.peek(p -> fatalError(formatMsg("error.missing.provider", p)))2031.count() != 0) {2032ok = false;2033}2034} catch (InvalidModuleDescriptorException x) {2035fatalError(x.getMessage());2036ok = false;2037}2038}2039return ok;2040}20412042/**2043* Adds extended modules attributes to the given module-info's. The given2044* Map values are updated in-place. Returns false if an error occurs.2045*/2046private void addExtendedModuleAttributes(Map<String,byte[]> moduleInfos,2047Set<String> packages)2048throws IOException2049{2050for (Map.Entry<String,byte[]> e: moduleInfos.entrySet()) {2051ModuleDescriptor md = ModuleDescriptor.read(ByteBuffer.wrap(e.getValue()));2052e.setValue(extendedInfoBytes(md, e.getValue(), packages));2053}2054}20552056static boolean isModuleInfoEntry(String name) {2057// root or versioned module-info.class2058if (name.endsWith(MODULE_INFO)) {2059int end = name.length() - MODULE_INFO.length();2060if (end == 0)2061return true;2062if (name.startsWith(VERSIONS_DIR)) {2063int off = VERSIONS_DIR_LENGTH;2064if (off == end) // meta-inf/versions/module-info.class2065return false;2066while (off < end - 1) {2067char c = name.charAt(off++);2068if (c < '0' || c > '9')2069return false;2070}2071return name.charAt(off) == '/';2072}2073}2074return false;2075}20762077/**2078* Returns a byte array containing the given module-info.class plus any2079* extended attributes.2080*2081* If --module-version, --main-class, or other options were provided2082* then the corresponding class file attributes are added to the2083* module-info here.2084*/2085private byte[] extendedInfoBytes(ModuleDescriptor md,2086byte[] miBytes,2087Set<String> packages)2088throws IOException2089{2090ByteArrayOutputStream baos = new ByteArrayOutputStream();2091InputStream is = new ByteArrayInputStream(miBytes);2092ModuleInfoExtender extender = ModuleInfoExtender.newExtender(is);20932094// Add (or replace) the Packages attribute2095extender.packages(packages);20962097// --main-class2098if (ename != null)2099extender.mainClass(ename);21002101// --module-version2102if (moduleVersion != null)2103extender.version(moduleVersion);21042105// --hash-modules2106if (modulesToHash != null) {2107String mn = md.name();2108Hasher hasher = new Hasher(md, fname);2109ModuleHashes moduleHashes = hasher.computeHashes(mn);2110if (moduleHashes != null) {2111extender.hashes(moduleHashes);2112} else {2113warn("warning: no module is recorded in hash in " + mn);2114}2115}21162117if (moduleResolution.value() != 0) {2118extender.moduleResolution(moduleResolution);2119}21202121extender.write(baos);2122return baos.toByteArray();2123}21242125/**2126* Compute and record hashes2127*/2128private class Hasher {2129final ModuleHashesBuilder hashesBuilder;2130final ModuleFinder finder;2131final Set<String> modules;2132Hasher(ModuleDescriptor descriptor, String fname) throws IOException {2133// Create a module finder that finds the modular JAR2134// being created/updated2135URI uri = Paths.get(fname).toUri();2136ModuleReference mref = new ModuleReference(descriptor, uri) {2137@Override2138public ModuleReader open() {2139throw new UnsupportedOperationException("should not reach here");2140}2141};21422143// Compose a module finder with the module path and2144// the modular JAR being created or updated2145this.finder = ModuleFinder.compose(moduleFinder,2146new ModuleFinder() {2147@Override2148public Optional<ModuleReference> find(String name) {2149if (descriptor.name().equals(name))2150return Optional.of(mref);2151else2152return Optional.empty();2153}21542155@Override2156public Set<ModuleReference> findAll() {2157return Collections.singleton(mref);2158}2159});21602161// Determine the modules that matches the pattern {@code modulesToHash}2162Set<String> roots = finder.findAll().stream()2163.map(ref -> ref.descriptor().name())2164.filter(mn -> modulesToHash.matcher(mn).find())2165.collect(Collectors.toSet());21662167// use system module path unless it creates a modular JAR for2168// a module that is present in the system image e.g. upgradeable2169// module2170ModuleFinder system;2171String name = descriptor.name();2172if (name != null && ModuleFinder.ofSystem().find(name).isPresent()) {2173system = ModuleFinder.of();2174} else {2175system = ModuleFinder.ofSystem();2176}2177// get a resolved module graph2178Configuration config =2179Configuration.empty().resolve(system, finder, roots);21802181// filter modules resolved from the system module finder2182this.modules = config.modules().stream()2183.map(ResolvedModule::name)2184.filter(mn -> roots.contains(mn) && !system.find(mn).isPresent())2185.collect(Collectors.toSet());21862187this.hashesBuilder = new ModuleHashesBuilder(config, modules);2188}21892190/**2191* Compute hashes of the specified module.2192*2193* It records the hashing modules that depend upon the specified2194* module directly or indirectly.2195*/2196ModuleHashes computeHashes(String name) {2197if (hashesBuilder == null)2198return null;21992200return hashesBuilder.computeHashes(Set.of(name)).get(name);2201}2202}22032204// sort base entries before versioned entries, and sort entry classes with2205// nested classes so that the outter class appears before the associated2206// nested class2207static Comparator<String> ENTRYNAME_COMPARATOR = (s1, s2) -> {22082209if (s1.equals(s2)) return 0;2210boolean b1 = s1.startsWith(VERSIONS_DIR);2211boolean b2 = s2.startsWith(VERSIONS_DIR);2212if (b1 && !b2) return 1;2213if (!b1 && b2) return -1;2214int n = 0; // starting char for String compare2215if (b1 && b2) {2216// normally strings would be sorted so "10" goes before "9", but2217// version number strings need to be sorted numerically2218n = VERSIONS_DIR.length(); // skip the common prefix2219int i1 = s1.indexOf('/', n);2220int i2 = s2.indexOf('/', n);2221if (i1 == -1) throw new Validator.InvalidJarException(s1);2222if (i2 == -1) throw new Validator.InvalidJarException(s2);2223// shorter version numbers go first2224if (i1 != i2) return i1 - i2;2225// otherwise, handle equal length numbers below2226}2227int l1 = s1.length();2228int l2 = s2.length();2229int lim = Math.min(l1, l2);2230for (int k = n; k < lim; k++) {2231char c1 = s1.charAt(k);2232char c2 = s2.charAt(k);2233if (c1 != c2) {2234// change natural ordering so '.' comes before '$'2235// i.e. outer classes come before nested classes2236if (c1 == '$' && c2 == '.') return 1;2237if (c1 == '.' && c2 == '$') return -1;2238return c1 - c2;2239}2240}2241return l1 - l2;2242};22432244static Comparator<ZipEntry> ENTRY_COMPARATOR =2245Comparator.comparing(ZipEntry::getName, ENTRYNAME_COMPARATOR);22462247}224822492250