src/jdk.jartool/share/classes/sun/tools/jar/Main.java
author sherman
Wed, 29 Nov 2017 09:25:25 -0800
changeset 47958 d34958cb3163
parent 47216 71c04702a3d5
child 50130 aefd3c7f5373
permissions -rw-r--r--
8186087: jar tool fails to create a multi-release jar when validating nested classes Reviewed-by: psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
     2
 * Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3288
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3288
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3288
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3288
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3288
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.jar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
    29
import java.lang.module.Configuration;
44267
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
    30
import java.lang.module.FindException;
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
    31
import java.lang.module.InvalidModuleDescriptorException;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
    32
import java.lang.module.ModuleDescriptor;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
    33
import java.lang.module.ModuleDescriptor.Exports;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
    34
import java.lang.module.ModuleDescriptor.Provides;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
    35
import java.lang.module.ModuleDescriptor.Opens;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
    36
import java.lang.module.ModuleDescriptor.Requires;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
    37
import java.lang.module.ModuleDescriptor.Version;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
    38
import java.lang.module.ModuleFinder;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
    39
import java.lang.module.ModuleReader;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
    40
import java.lang.module.ModuleReference;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
    41
import java.lang.module.ResolutionException;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
    42
import java.lang.module.ResolvedModule;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
    43
import java.net.URI;
38468
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
    44
import java.nio.ByteBuffer;
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
    45
import java.nio.file.Path;
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    46
import java.nio.file.Files;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
    47
import java.nio.file.Paths;
40251
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
    48
import java.nio.file.StandardCopyOption;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.*;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
    50
import java.util.function.Consumer;
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
    51
import java.util.function.Supplier;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
    52
import java.util.regex.Pattern;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
    53
import java.util.stream.Collectors;
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
    54
import java.util.stream.Stream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.util.zip.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.util.jar.*;
21348
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
    57
import java.util.jar.Pack200.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.util.jar.Manifest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import java.text.MessageFormat;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
    60
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
    61
import jdk.internal.module.Checks;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
    62
import jdk.internal.module.ModuleHashes;
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
    63
import jdk.internal.module.ModuleHashesBuilder;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
    64
import jdk.internal.module.ModuleInfo;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
    65
import jdk.internal.module.ModuleInfoExtender;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
    66
import jdk.internal.module.ModuleResolution;
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44033
diff changeset
    67
import jdk.internal.module.ModuleTarget;
36745
51effd3e92d0 8152190: Move sun.misc.JarIndex and InvalidJarIndexException to an internal package
chegar
parents: 36511
diff changeset
    68
import jdk.internal.util.jar.JarIndex;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
    69
36745
51effd3e92d0 8152190: Move sun.misc.JarIndex and InvalidJarIndexException to an internal package
chegar
parents: 36511
diff changeset
    70
import static jdk.internal.util.jar.JarIndex.INDEX_NAME;
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
    71
import static java.util.jar.JarFile.MANIFEST_NAME;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
    72
import static java.util.stream.Collectors.joining;
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
    73
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * This class implements a simple utility for creating files in the JAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * (Java Archive) file format. The JAR format is based on the ZIP file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * format, with optional meta-information stored in a MANIFEST entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 */
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
    80
public class Main {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    String program;
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41221
diff changeset
    82
    PrintWriter out, err;
19409
d7c7b9d56631 8022921: Remove experimental Profile attribute
alanb
parents: 15682
diff changeset
    83
    String fname, mname, ename;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    String zname = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    String rootjar = null;
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
    86
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
    87
    private static final int BASE_VERSION = 0;
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
    88
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
    89
    private static class Entry {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
    90
        final String name;
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
    91
        final File file;
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
    92
        final boolean isDir;
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
    93
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
    94
        Entry(File file, String name, boolean isDir) {
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
    95
            this.file = file;
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
    96
            this.isDir = isDir;
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
    97
            this.name = name;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
    98
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
    99
41221
eb0dc5ea2302 8165944: jar utility doesn't process more than one -C argument
sdrach
parents: 40251
diff changeset
   100
        @Override
eb0dc5ea2302 8165944: jar utility doesn't process more than one -C argument
sdrach
parents: 40251
diff changeset
   101
        public boolean equals(Object o) {
eb0dc5ea2302 8165944: jar utility doesn't process more than one -C argument
sdrach
parents: 40251
diff changeset
   102
            if (this == o) return true;
eb0dc5ea2302 8165944: jar utility doesn't process more than one -C argument
sdrach
parents: 40251
diff changeset
   103
            if (!(o instanceof Entry)) return false;
eb0dc5ea2302 8165944: jar utility doesn't process more than one -C argument
sdrach
parents: 40251
diff changeset
   104
            return this.file.equals(((Entry)o).file);
eb0dc5ea2302 8165944: jar utility doesn't process more than one -C argument
sdrach
parents: 40251
diff changeset
   105
        }
eb0dc5ea2302 8165944: jar utility doesn't process more than one -C argument
sdrach
parents: 40251
diff changeset
   106
eb0dc5ea2302 8165944: jar utility doesn't process more than one -C argument
sdrach
parents: 40251
diff changeset
   107
        @Override
eb0dc5ea2302 8165944: jar utility doesn't process more than one -C argument
sdrach
parents: 40251
diff changeset
   108
        public int hashCode() {
eb0dc5ea2302 8165944: jar utility doesn't process more than one -C argument
sdrach
parents: 40251
diff changeset
   109
            return file.hashCode();
eb0dc5ea2302 8165944: jar utility doesn't process more than one -C argument
sdrach
parents: 40251
diff changeset
   110
        }
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   111
    }
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   112
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   113
    // An entryName(path)->Entry map generated during "expand", it helps to
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   114
    // decide whether or not an existing entry in a jar file needs to be
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   115
    // replaced, during the "update" operation.
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   116
    Map<String, Entry> entryMap = new HashMap<>();
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   117
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   118
    // All entries need to be added/updated.
41221
eb0dc5ea2302 8165944: jar utility doesn't process more than one -C argument
sdrach
parents: 40251
diff changeset
   119
    Set<Entry> entries = new LinkedHashSet<>();
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   120
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   121
    // module-info.class entries need to be added/updated.
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   122
    Map<String,byte[]> moduleInfos = new HashMap<>();
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   123
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   124
    // A paths Set for each version, where each Set contains directories
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   125
    // specified by the "-C" operation.
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   126
    Map<Integer,Set<String>> pathsMap = new HashMap<>();
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   127
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   128
    // There's also a files array per version
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   129
    Map<Integer,String[]> filesMap = new HashMap<>();
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   130
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   131
    // Do we think this is a multi-release jar?  Set to true
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   132
    // if --release option found followed by at least file
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   133
    boolean isMultiRelease;
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   134
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
   135
    // The last parsed --release value, if any. Used in conjunction with
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
   136
    // "-d,--describe-module" to select the operative module descriptor.
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
   137
    int releaseValue = -1;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
   138
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * cflag: create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * uflag: update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * xflag: xtract
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * tflag: table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * vflag: verbose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * flag0: no zip compression (store only)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Mflag: DO NOT generate a manifest file (just ZIP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * iflag: generate jar index
21348
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
   148
     * nflag: Perform jar normalization at the end
29914
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
   149
     * pflag: preserve/don't strip leading slash and .. component from file name
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   150
     * dflag: print module descriptor
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   152
    boolean cflag, uflag, xflag, tflag, vflag, flag0, Mflag, iflag, nflag, pflag, dflag;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   154
    /* To support additional GNU Style informational options */
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   155
    Consumer<PrintWriter> info;
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   156
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   157
    /* Modular jar related options */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   158
    Version moduleVersion;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
   159
    Pattern modulesToHash;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
   160
    ModuleResolution moduleResolution = ModuleResolution.empty();
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37779
diff changeset
   161
    ModuleFinder moduleFinder = ModuleFinder.of();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   162
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   163
    static final String MODULE_INFO = "module-info.class";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    static final String MANIFEST_DIR = "META-INF/";
38468
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
   165
    static final String VERSIONS_DIR = MANIFEST_DIR + "versions/";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    static final String VERSION = "1.0";
43731
bc7110b230c1 8165640: Enhance jar tool to allow module-info in versioned directories but not in base in modular multi-release jar files
sherman
parents: 43712
diff changeset
   167
    static final int VERSIONS_DIR_LENGTH = VERSIONS_DIR.length();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private static ResourceBundle rsrc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * If true, maintain compatibility with JDK releases prior to 6.0 by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * timestamping extracted files with the time at which they are extracted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Default is to use the time given in the archive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private static final boolean useExtractionTime =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        Boolean.getBoolean("sun.tools.jar.useExtractionTime");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Initialize ResourceBundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            rsrc = ResourceBundle.getBundle("sun.tools.jar.resources.jar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            throw new Error("Fatal: Resource for jar is missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   189
    static String getMsg(String key) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return (rsrc.getString(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            throw new Error("Error in message file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   197
    static String formatMsg(String key, String arg) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        String msg = getMsg(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        String[] args = new String[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        args[0] = arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return MessageFormat.format(msg, (Object[]) args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   204
    static String formatMsg2(String key, String arg, String arg1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        String msg = getMsg(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        String[] args = new String[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        args[0] = arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        args[1] = arg1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return MessageFormat.format(msg, (Object[]) args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public Main(PrintStream out, PrintStream err, String program) {
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41221
diff changeset
   213
        this.out = new PrintWriter(out, true);
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41221
diff changeset
   214
        this.err = new PrintWriter(err, true);
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41221
diff changeset
   215
        this.program = program;
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41221
diff changeset
   216
    }
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41221
diff changeset
   217
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41221
diff changeset
   218
    public Main(PrintWriter out, PrintWriter err, String program) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        this.out = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        this.err = err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        this.program = program;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   224
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   225
     * Creates a new empty temporary file in the same directory as the
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   226
     * specified file.  A variant of File.createTempFile.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   227
     */
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   228
    private static File createTempFileInSameDirectoryAs(File file)
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   229
        throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   230
        File dir = file.getParentFile();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   231
        if (dir == null)
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   232
            dir = new File(".");
13154
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents: 13041
diff changeset
   233
        return File.createTempFile("jartmp", null, dir);
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   234
    }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   235
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    private boolean ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   238
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Starts main program with the specified arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public synchronized boolean run(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (!parseArgs(args)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
44033
ab43ed5eab17 8174996: jar leaves temporary file when exception occur in creating jar
sherman
parents: 43731
diff changeset
   246
        File tmpFile = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if (cflag || uflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                if (fname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    // The name of the zip file as it would appear as its own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    // zip file entry. We use this to make sure that we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    // add the zip file to itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    zname = fname.replace(File.separatorChar, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    if (zname.startsWith("./")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        zname = zname.substring(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if (cflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                Manifest manifest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                if (!Mflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    if (mname != null) {
40251
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   263
                        try (InputStream in = new FileInputStream(mname)) {
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   264
                            manifest = new Manifest(new BufferedInputStream(in));
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   265
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        manifest = new Manifest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    addVersion(manifest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    addCreatedBy(manifest);
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   271
                    if (isAmbiguousMainClass(manifest)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    if (ename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                        addMainClass(manifest, ename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    }
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   277
                    if (isMultiRelease) {
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   278
                        addMultiRelease(manifest);
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   279
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                }
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   281
                expand();
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   282
                if (!moduleInfos.isEmpty()) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   283
                    // All actual file entries (excl manifest and module-info.class)
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   284
                    Set<String> jentries = new HashSet<>();
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   285
                    // all packages if it's a class or resource
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   286
                    Set<String> packages = new HashSet<>();
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   287
                    entries.stream()
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   288
                           .filter(e -> !e.isDir)
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   289
                           .forEach( e -> {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   290
                               addPackageIfNamed(packages, e.name);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   291
                               jentries.add(e.name);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   292
                    });
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   293
                    addExtendedModuleAttributes(moduleInfos, packages);
38468
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
   294
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
   295
                    // Basic consistency checks for modular jars.
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   296
                    if (!checkModuleInfo(moduleInfos.get(MODULE_INFO), jentries))
38468
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
   297
                        return false;
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
   298
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
   299
                } else if (moduleVersion != null || modulesToHash != null) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   300
                    error(getMsg("error.module.options.without.info"));
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   301
                    return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   302
                }
40251
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   303
                if (vflag && fname == null) {
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   304
                    // Disable verbose output so that it does not appear
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   305
                    // on stdout along with file data
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   306
                    // error("Warning: -v option ignored");
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   307
                    vflag = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                }
21348
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
   309
                final String tmpbase = (fname == null)
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
   310
                        ? "tmpjar"
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
   311
                        : fname.substring(fname.indexOf(File.separatorChar) + 1);
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   312
44033
ab43ed5eab17 8174996: jar leaves temporary file when exception occur in creating jar
sherman
parents: 43731
diff changeset
   313
                tmpFile = createTemporaryFile(tmpbase, ".jar");
ab43ed5eab17 8174996: jar leaves temporary file when exception occur in creating jar
sherman
parents: 43731
diff changeset
   314
                try (OutputStream out = new FileOutputStream(tmpFile)) {
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   315
                    create(new BufferedOutputStream(out, 4096), manifest);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                }
26714
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
   317
                if (nflag) {
40251
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   318
                    File packFile = createTemporaryFile(tmpbase, ".pack");
21348
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
   319
                    try {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
   320
                        Packer packer = Pack200.newPacker();
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
   321
                        Map<String, String> p = packer.properties();
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
   322
                        p.put(Packer.EFFORT, "1"); // Minimal effort to conserve CPU
44033
ab43ed5eab17 8174996: jar leaves temporary file when exception occur in creating jar
sherman
parents: 43731
diff changeset
   323
                        try (JarFile jarFile = new JarFile(tmpFile.getCanonicalPath());
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   324
                             OutputStream pack = new FileOutputStream(packFile))
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   325
                        {
40251
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   326
                            packer.pack(jarFile, pack);
21348
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
   327
                        }
44033
ab43ed5eab17 8174996: jar leaves temporary file when exception occur in creating jar
sherman
parents: 43731
diff changeset
   328
                        if (tmpFile.exists()) {
ab43ed5eab17 8174996: jar leaves temporary file when exception occur in creating jar
sherman
parents: 43731
diff changeset
   329
                            tmpFile.delete();
21348
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
   330
                        }
44033
ab43ed5eab17 8174996: jar leaves temporary file when exception occur in creating jar
sherman
parents: 43731
diff changeset
   331
                        tmpFile = createTemporaryFile(tmpbase, ".jar");
ab43ed5eab17 8174996: jar leaves temporary file when exception occur in creating jar
sherman
parents: 43731
diff changeset
   332
                        try (OutputStream out = new FileOutputStream(tmpFile);
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   333
                             JarOutputStream jos = new JarOutputStream(out))
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   334
                        {
40251
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   335
                            Unpacker unpacker = Pack200.newUnpacker();
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   336
                            unpacker.unpack(packFile, jos);
21348
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
   337
                        }
40251
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   338
                    } finally {
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   339
                        Files.deleteIfExists(packFile.toPath());
21348
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
   340
                    }
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
   341
                }
44033
ab43ed5eab17 8174996: jar leaves temporary file when exception occur in creating jar
sherman
parents: 43731
diff changeset
   342
                validateAndClose(tmpFile);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            } else if (uflag) {
44033
ab43ed5eab17 8174996: jar leaves temporary file when exception occur in creating jar
sherman
parents: 43731
diff changeset
   344
                File inputFile = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                if (fname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    inputFile = new File(fname);
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   347
                    tmpFile = createTempFileInSameDirectoryAs(inputFile);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    vflag = false;
40251
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   350
                    tmpFile = createTemporaryFile("tmpjar", ".jar");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                }
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   352
                expand();
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   353
                try (FileInputStream in = (fname != null) ? new FileInputStream(inputFile)
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   354
                        : new FileInputStream(FileDescriptor.in);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   355
                     FileOutputStream out = new FileOutputStream(tmpFile);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   356
                     InputStream manifest = (!Mflag && (mname != null)) ?
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   357
                            (new FileInputStream(mname)) : null;
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   358
                ) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   359
                    boolean updateOk = update(in, new BufferedOutputStream(out),
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   360
                        manifest, moduleInfos, null);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   361
                    if (ok) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   362
                        ok = updateOk;
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   363
                    }
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   364
                }
40251
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   365
                validateAndClose(tmpFile);
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   366
            } else if (tflag) {
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   367
                replaceFSC(filesMap);
26714
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
   368
                // For the "list table contents" action, access using the
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
   369
                // ZipFile class is always most efficient since only a
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
   370
                // "one-finger" scan through the central directory is required.
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   371
                String[] files = filesMapToFiles(filesMap);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                if (fname != null) {
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   373
                    list(fname, files);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                } else {
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   375
                    InputStream in = new FileInputStream(FileDescriptor.in);
26714
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
   376
                    try {
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   377
                        list(new BufferedInputStream(in), files);
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   378
                    } finally {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   379
                        in.close();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   380
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                }
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   382
            } else if (xflag) {
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   383
                replaceFSC(filesMap);
26714
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
   384
                // For the extract action, when extracting all the entries,
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
   385
                // access using the ZipInputStream class is most efficient,
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
   386
                // since only a single sequential scan through the zip file is
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
   387
                // required.  When using the ZipFile class, a "two-finger" scan
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
   388
                // is required, but this is likely to be more efficient when a
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
   389
                // partial extract is requested.  In case the zip file has
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
   390
                // "leading garbage", we fall back from the ZipInputStream
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
   391
                // implementation to the ZipFile implementation, since only the
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
   392
                // latter can handle it.
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   393
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   394
                String[] files = filesMapToFiles(filesMap);
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   395
                if (fname != null && files != null) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   396
                    extract(fname, files);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                } else {
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   398
                    InputStream in = (fname == null)
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   399
                        ? new FileInputStream(FileDescriptor.in)
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   400
                        : new FileInputStream(fname);
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   401
                    try {
26714
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
   402
                        if (!extract(new BufferedInputStream(in), files) && fname != null) {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
   403
                            extract(fname, files);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
   404
                        }
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   405
                    } finally {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   406
                        in.close();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   407
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            } else if (iflag) {
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   410
                String[] files = filesMap.get(BASE_VERSION);  // base entries only, can be null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                genIndex(rootjar, files);
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   412
            } else if (dflag) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   413
                boolean found;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   414
                if (fname != null) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
   415
                    try (ZipFile zf = new ZipFile(fname)) {
44267
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
   416
                        found = describeModule(zf);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
   417
                    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   418
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   419
                    try (FileInputStream fin = new FileInputStream(FileDescriptor.in)) {
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
   420
                        found = describeModuleFromStream(fin);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   421
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   422
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   423
                if (!found)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   424
                    error(getMsg("error.module.descriptor.not.found"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            fatalError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        } catch (Error ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            ee.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            ok = false;
44033
ab43ed5eab17 8174996: jar leaves temporary file when exception occur in creating jar
sherman
parents: 43731
diff changeset
   435
        } finally {
ab43ed5eab17 8174996: jar leaves temporary file when exception occur in creating jar
sherman
parents: 43731
diff changeset
   436
            if (tmpFile != null && tmpFile.exists())
ab43ed5eab17 8174996: jar leaves temporary file when exception occur in creating jar
sherman
parents: 43731
diff changeset
   437
                tmpFile.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        return ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
40251
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   444
    private void validateAndClose(File tmpfile) throws IOException {
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   445
        if (ok && isMultiRelease) {
47958
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
   446
            try (ZipFile zf = new ZipFile(tmpfile)) {
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
   447
                ok = Validator.validate(this, zf);
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   448
                if (!ok) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   449
                    error(formatMsg("error.validator.jarfile.invalid", fname));
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   450
                }
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   451
            } catch (IOException e) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   452
                error(formatMsg2("error.validator.jarfile.exception", fname, e.getMessage()));
40251
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   453
            }
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   454
        }
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   455
        Path path = tmpfile.toPath();
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   456
        try {
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   457
            if (ok) {
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   458
                if (fname != null) {
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   459
                    Files.move(path, Paths.get(fname), StandardCopyOption.REPLACE_EXISTING);
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   460
                } else {
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   461
                    Files.copy(path, new FileOutputStream(FileDescriptor.out));
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   462
                }
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   463
            }
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   464
        } finally {
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   465
            Files.deleteIfExists(path);
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   466
        }
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   467
    }
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   468
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   469
    private String[] filesMapToFiles(Map<Integer,String[]> filesMap) {
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   470
        if (filesMap.isEmpty()) return null;
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   471
        return filesMap.entrySet()
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   472
                .stream()
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   473
                .flatMap(this::filesToEntryNames)
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   474
                .toArray(String[]::new);
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   475
    }
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   476
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   477
    Stream<String> filesToEntryNames(Map.Entry<Integer,String[]> fileEntries) {
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   478
        int version = fileEntries.getKey();
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   479
        Set<String> cpaths = pathsMap.get(version);
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   480
        return Stream.of(fileEntries.getValue())
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   481
            .map(f -> toVersionedName(toEntryName(f, cpaths, false), version));
40251
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   482
    }
481b890e50a3 8158295: Add a multi-release jar validation mechanism to jar tool
sdrach
parents: 39313
diff changeset
   483
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   484
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   485
     * Parses command line arguments.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    boolean parseArgs(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        /* Preprocess and expand @file arguments */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            args = CommandLine.parse(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        } catch (FileNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            fatalError(formatMsg("error.cant.open", e.getMessage()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            fatalError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        /* parse flags */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        int count = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            String flags = args[0];
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   502
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   503
            // Note: flags.length == 2 can be treated as the short version of
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   504
            // the GNU option since the there cannot be any other options,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   505
            // excluding -C, as per the old way.
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   506
            if (flags.startsWith("--") ||
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   507
                (flags.startsWith("-") && flags.length() == 2)) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   508
                try {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   509
                    count = GNUStyleOptions.parseOptions(this, args);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   510
                } catch (GNUStyleOptions.BadArgs x) {
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   511
                    if (info == null) {
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   512
                        if (x.showUsage) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   513
                            usageError(x.getMessage());
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   514
                        } else {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   515
                            error(x.getMessage());
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   516
                        }
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   517
                        return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    }
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   519
                }
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   520
                if (info != null) {
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   521
                    info.accept(out);
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   522
                    return true;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   523
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   524
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   525
                // Legacy/compatibility options
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   526
                if (flags.startsWith("-")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   527
                    flags = flags.substring(1);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   528
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   529
                for (int i = 0; i < flags.length(); i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   530
                    switch (flags.charAt(i)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   531
                        case 'c':
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   532
                            if (xflag || tflag || uflag || iflag) {
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   533
                                usageError(getMsg("error.multiple.main.operations"));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   534
                                return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   535
                            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   536
                            cflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   537
                            break;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   538
                        case 'u':
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   539
                            if (cflag || xflag || tflag || iflag) {
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   540
                                usageError(getMsg("error.multiple.main.operations"));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   541
                                return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   542
                            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   543
                            uflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   544
                            break;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   545
                        case 'x':
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   546
                            if (cflag || uflag || tflag || iflag) {
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   547
                                usageError(getMsg("error.multiple.main.operations"));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   548
                                return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   549
                            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   550
                            xflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   551
                            break;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   552
                        case 't':
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   553
                            if (cflag || uflag || xflag || iflag) {
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   554
                                usageError(getMsg("error.multiple.main.operations"));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   555
                                return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   556
                            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   557
                            tflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   558
                            break;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   559
                        case 'M':
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   560
                            Mflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   561
                            break;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   562
                        case 'v':
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   563
                            vflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   564
                            break;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   565
                        case 'f':
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   566
                            fname = args[count++];
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   567
                            break;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   568
                        case 'm':
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   569
                            mname = args[count++];
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   570
                            break;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   571
                        case '0':
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   572
                            flag0 = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   573
                            break;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   574
                        case 'i':
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   575
                            if (cflag || uflag || xflag || tflag) {
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   576
                                usageError(getMsg("error.multiple.main.operations"));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   577
                                return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   578
                            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   579
                            // do not increase the counter, files will contain rootjar
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   580
                            rootjar = args[count++];
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   581
                            iflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   582
                            break;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   583
                        case 'n':
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   584
                            nflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   585
                            break;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   586
                        case 'e':
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   587
                            ename = args[count++];
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   588
                            break;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   589
                        case 'P':
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   590
                            pflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   591
                            break;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   592
                        default:
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   593
                            usageError(formatMsg("error.illegal.option",
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   594
                                       String.valueOf(flags.charAt(i))));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   595
                            return false;
5620
ed56433bf5d6 4690407: JAR tool: option -i can't be combined with other options
sherman
parents: 3288
diff changeset
   596
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        } catch (ArrayIndexOutOfBoundsException e) {
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   600
            usageError(getMsg("main.usage.summary"));
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   601
            return false;
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   602
        }
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   603
        if (!cflag && !tflag && !xflag && !uflag && !iflag && !dflag) {
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   604
            usageError(getMsg("error.bad.option"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   607
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        /* parse file arguments */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        int n = args.length - count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        if (n > 0) {
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   611
            int version = BASE_VERSION;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            String[] nameBuf = new String[n];
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   614
            pathsMap.put(version, new HashSet<>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                for (int i = count; i < args.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                    if (args[i].equals("-C")) {
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
   618
                        if (dflag) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
   619
                            // "--describe-module/-d" does not require file argument(s),
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
   620
                            // but does accept --release
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
   621
                            usageError(getMsg("error.bad.dflag"));
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
   622
                            return false;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
   623
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                        /* change the directory */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                        String dir = args[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                        dir = (dir.endsWith(File.separator) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                               dir : (dir + File.separator));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                        dir = dir.replace(File.separatorChar, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                        while (dir.indexOf("//") > -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                            dir = dir.replace("//", "/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                        }
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   632
                        pathsMap.get(version).add(dir.replace(File.separatorChar, '/'));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                        nameBuf[k++] = dir + args[++i];
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   634
                    } else if (args[i].startsWith("--release")) {
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   635
                        int v = BASE_VERSION;
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   636
                        try {
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   637
                            v = Integer.valueOf(args[++i]);
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   638
                        } catch (NumberFormatException x) {
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   639
                            error(formatMsg("error.release.value.notnumber", args[i]));
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   640
                            // this will fall into the next error, thus returning false
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   641
                        }
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   642
                        if (v < 9) {
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   643
                            usageError(formatMsg("error.release.value.toosmall", String.valueOf(v)));
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   644
                            return false;
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   645
                        }
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   646
                        // associate the files, if any, with the previous version number
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   647
                        if (k > 0) {
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   648
                            String[] files = new String[k];
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   649
                            System.arraycopy(nameBuf, 0, files, 0, k);
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   650
                            filesMap.put(version, files);
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   651
                            isMultiRelease = version > BASE_VERSION;
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   652
                        }
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   653
                        // reset the counters and start with the new version number
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   654
                        k = 0;
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   655
                        nameBuf = new String[n];
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   656
                        version = v;
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
   657
                        releaseValue = version;
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   658
                        pathsMap.put(version, new HashSet<>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                    } else {
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
   660
                        if (dflag) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
   661
                            // "--describe-module/-d" does not require file argument(s),
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
   662
                            // but does accept --release
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
   663
                            usageError(getMsg("error.bad.dflag"));
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
   664
                            return false;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
   665
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                        nameBuf[k++] = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            } catch (ArrayIndexOutOfBoundsException e) {
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   670
                usageError(getMsg("error.bad.file.arg"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            }
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   673
            // associate remaining files, if any, with a version
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   674
            if (k > 0) {
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   675
                String[] files = new String[k];
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   676
                System.arraycopy(nameBuf, 0, files, 0, k);
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   677
                filesMap.put(version, files);
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   678
                isMultiRelease = version > BASE_VERSION;
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   679
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        } else if (cflag && (mname == null)) {
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   681
            usageError(getMsg("error.bad.cflag"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        } else if (uflag) {
19409
d7c7b9d56631 8022921: Remove experimental Profile attribute
alanb
parents: 15682
diff changeset
   684
            if ((mname != null) || (ename != null)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                /* just want to update the manifest */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            } else {
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
   688
                usageError(getMsg("error.bad.uflag"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
   695
    /*
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
   696
     * Add the package of the given resource name if it's a .class
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
   697
     * or a resource in a named package.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
   698
     */
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   699
    void addPackageIfNamed(Set<String> packages, String name) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
   700
        if (name.startsWith(VERSIONS_DIR)) {
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   701
            // trim the version dir prefix
43731
bc7110b230c1 8165640: Enhance jar tool to allow module-info in versioned directories but not in base in modular multi-release jar files
sherman
parents: 43712
diff changeset
   702
            int i0 = VERSIONS_DIR_LENGTH;
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   703
            int i = name.indexOf('/', i0);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   704
            if (i <= 0) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   705
                warn(formatMsg("warn.release.unexpected.versioned.entry", name));
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   706
                return;
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   707
            }
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   708
            while (i0 < i) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   709
                char c = name.charAt(i0);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   710
                if (c < '0' || c > '9') {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   711
                    warn(formatMsg("warn.release.unexpected.versioned.entry", name));
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   712
                    return;
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   713
                }
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   714
                i0++;
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   715
            }
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   716
            name = name.substring(i + 1, name.length());
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
   717
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
   718
        String pn = toPackageName(name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
   719
        // add if this is a class or resource in a package
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43109
diff changeset
   720
        if (Checks.isPackageName(pn)) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
   721
            packages.add(pn);
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   722
        }
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   723
    }
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   724
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   725
    private String toEntryName(String name, Set<String> cpaths, boolean isDir) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   726
        name = name.replace(File.separatorChar, '/');
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   727
        if (isDir) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   728
            name = name.endsWith("/") ? name : name + "/";
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
   729
        }
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   730
        String matchPath = "";
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   731
        for (String path : cpaths) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   732
            if (name.startsWith(path) && path.length() > matchPath.length()) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   733
                matchPath = path;
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   734
            }
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   735
        }
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   736
        name = safeName(name.substring(matchPath.length()));
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   737
        // the old implementaton doesn't remove
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   738
        // "./" if it was led by "/" (?)
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   739
        if (name.startsWith("./")) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   740
            name = name.substring(2);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   741
        }
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   742
        return name;
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   743
    }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
   744
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   745
    private static String toVersionedName(String name, int version) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   746
        return version > BASE_VERSION
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   747
                ? VERSIONS_DIR + version + "/" + name : name;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   748
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   749
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   750
    private static String toPackageName(String path) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
   751
        int index = path.lastIndexOf('/');
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   752
        if (index != -1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   753
            return path.substring(0, index).replace('/', '.');
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   754
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   755
            return "";
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   756
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   757
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   758
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   759
    private void expand() throws IOException {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   760
        for (int version : filesMap.keySet()) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   761
            String[] files = filesMap.get(version);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   762
            expand(null, files, pathsMap.get(version), version);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
   763
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
   764
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
   765
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   766
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * Expands list of files to process into full list of all files that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * can be found by recursively descending directories.
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   769
     *
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   770
     * @param dir    parent directory
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
   771
     * @param files  list of files to expand
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   772
     * @param cpaths set of directories specified by -C option for the files
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   773
     * @throws IOException if an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     */
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   775
    private void expand(File dir, String[] files, Set<String> cpaths, int version)
38468
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
   776
        throws IOException
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
   777
    {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   778
        if (files == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            return;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   780
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        for (int i = 0; i < files.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            File f;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   783
            if (dir == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                f = new File(files[i]);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   785
            else
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                f = new File(dir, files[i]);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   787
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   788
            boolean isDir = f.isDirectory();
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   789
            String name = toEntryName(f.getPath(), cpaths, isDir);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   790
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   791
            if (version != BASE_VERSION) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   792
                if (name.startsWith(VERSIONS_DIR)) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   793
                    // the entry starts with VERSIONS_DIR and version != BASE_VERSION,
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   794
                    // which means the "[dirs|files]" in --release v [dirs|files]
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   795
                    // includes VERSIONS_DIR-ed entries --> warning and skip (?)
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
   796
                    error(formatMsg2("error.release.unexpected.versioned.entry",
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   797
                                     name, String.valueOf(version)));
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
   798
                    ok = false;
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   799
                    return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                }
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   801
                name = toVersionedName(name, version);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   802
            }
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   803
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   804
            if (f.isFile()) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   805
                Entry e = new Entry(f, name, false);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   806
                if (isModuleInfoEntry(name)) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   807
                    moduleInfos.putIfAbsent(name, Files.readAllBytes(f.toPath()));
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   808
                    if (uflag)
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   809
                        entryMap.put(name, e);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   810
                } else if (entries.add(e)) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   811
                    if (uflag)
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   812
                        entryMap.put(name, e);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   813
                }
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   814
            } else if (isDir) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   815
                Entry e = new Entry(f, name, true);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   816
                if (entries.add(e)) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   817
                    // utilize entryMap for the duplicate dir check even in
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   818
                    // case of cflag == true.
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   819
                    // dir name confilict/duplicate could happen with -C option.
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   820
                    // just remove the last "e" from the "entries" (zos will fail
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   821
                    // with "duplicated" entries), but continue expanding the
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   822
                    // sub tree
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   823
                    if (entryMap.containsKey(name)) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   824
                        entries.remove(e);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   825
                    } else {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   826
                        entryMap.put(name, e);
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   827
                    }
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   828
                    expand(f, f.list(), cpaths, version);
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   829
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                error(formatMsg("error.nosuch.fileordir", String.valueOf(f)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   837
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * Creates a new JAR file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     */
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   840
    void create(OutputStream out, Manifest manifest) throws IOException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    {
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   842
        try (ZipOutputStream zos = new JarOutputStream(out)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            if (flag0) {
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   844
                zos.setMethod(ZipOutputStream.STORED);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            }
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   846
            // TODO: check module-info attributes against manifest ??
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   847
            if (manifest != null) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   848
                if (vflag) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   849
                    output(getMsg("out.added.manifest"));
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   850
                }
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   851
                ZipEntry e = new ZipEntry(MANIFEST_DIR);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   852
                e.setTime(System.currentTimeMillis());
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   853
                e.setSize(0);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   854
                e.setCrc(0);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   855
                zos.putNextEntry(e);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   856
                e = new ZipEntry(MANIFEST_NAME);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   857
                e.setTime(System.currentTimeMillis());
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   858
                if (flag0) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   859
                    crc32Manifest(e, manifest);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   860
                }
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   861
                zos.putNextEntry(e);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   862
                manifest.write(zos);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   863
                zos.closeEntry();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   864
            }
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   865
            updateModuleInfo(moduleInfos, zos);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   866
            for (Entry entry : entries) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   867
                addFile(zos, entry);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   868
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
   869
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   872
    private char toUpperCaseASCII(char c) {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   873
        return (c < 'a' || c > 'z') ? c : (char) (c + 'A' - 'a');
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   874
    }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   875
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   876
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   877
     * Compares two strings for equality, ignoring case.  The second
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   878
     * argument must contain only upper-case ASCII characters.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   879
     * We don't want case comparison to be locale-dependent (else we
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   880
     * have the notorious "turkish i bug").
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   881
     */
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   882
    private boolean equalsIgnoreCase(String s, String upper) {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   883
        assert upper.toUpperCase(java.util.Locale.ENGLISH).equals(upper);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   884
        int len;
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   885
        if ((len = s.length()) != upper.length())
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   886
            return false;
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   887
        for (int i = 0; i < len; i++) {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   888
            char c1 = s.charAt(i);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   889
            char c2 = upper.charAt(i);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   890
            if (c1 != c2 && toUpperCaseASCII(c1) != c2)
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   891
                return false;
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   892
        }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   893
        return true;
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   894
    }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   895
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   896
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   897
     * Updates an existing jar file.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    boolean update(InputStream in, OutputStream out,
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   900
                   InputStream newManifest,
38468
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
   901
                   Map<String,byte[]> moduleInfos,
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   902
                   JarIndex jarIndex) throws IOException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        ZipInputStream zis = new ZipInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        ZipOutputStream zos = new JarOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        ZipEntry e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        boolean foundManifest = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        boolean updateOk = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   910
        // All actual entries added/updated/existing, in the jar file (excl manifest
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   911
        // and module-info.class ).
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   912
        Set<String> jentries = new HashSet<>();
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   913
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   914
        if (jarIndex != null) {
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   915
            addIndex(jarIndex, zos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        // put the old entries first, replace if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        while ((e = zis.getNextEntry()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            String name = e.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   922
            boolean isManifestEntry = equalsIgnoreCase(name, MANIFEST_NAME);
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   923
            boolean isModuleInfoEntry = isModuleInfoEntry(name);
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   924
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   925
            if ((jarIndex != null && equalsIgnoreCase(name, INDEX_NAME))
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   926
                || (Mflag && isManifestEntry)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            } else if (isManifestEntry && ((newManifest != null) ||
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   929
                        (ename != null) || isMultiRelease)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                foundManifest = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                if (newManifest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                    // Don't read from the newManifest InputStream, as we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                    // might need it below, and we can't re-read the same data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                    // twice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                    FileInputStream fis = new FileInputStream(mname);
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   936
                    boolean ambiguous = isAmbiguousMainClass(new Manifest(fis));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                    fis.close();
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   938
                    if (ambiguous) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                // Update the manifest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                Manifest old = new Manifest(zis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                if (newManifest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                    old.read(newManifest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                }
15682
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents: 14504
diff changeset
   947
                if (!updateManifest(old, zos)) {
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents: 14504
diff changeset
   948
                    return false;
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents: 14504
diff changeset
   949
                }
38468
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
   950
            } else if (moduleInfos != null && isModuleInfoEntry) {
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   951
                moduleInfos.putIfAbsent(name, zis.readAllBytes());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            } else {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
   953
                boolean isDir = e.isDirectory();
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   954
                if (!entryMap.containsKey(name)) { // copy the old stuff
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                    // do our own compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                    ZipEntry e2 = new ZipEntry(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                    e2.setMethod(e.getMethod());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                    e2.setTime(e.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                    e2.setComment(e.getComment());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                    e2.setExtra(e.getExtra());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                    if (e.getMethod() == ZipEntry.STORED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                        e2.setSize(e.getSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                        e2.setCrc(e.getCrc());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                    zos.putNextEntry(e2);
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   966
                    copy(zis, zos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                } else { // replace with the new files
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   968
                    Entry ent = entryMap.get(name);
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
   969
                    addFile(zos, ent);
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   970
                    entryMap.remove(name);
41221
eb0dc5ea2302 8165944: jar utility doesn't process more than one -C argument
sdrach
parents: 40251
diff changeset
   971
                    entries.remove(ent);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
   972
                    isDir = ent.isDir;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
   974
                if (!isDir) {
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   975
                    jentries.add(name);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
   976
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        // add the remaining new files
41221
eb0dc5ea2302 8165944: jar utility doesn't process more than one -C argument
sdrach
parents: 40251
diff changeset
   981
        for (Entry entry : entries) {
eb0dc5ea2302 8165944: jar utility doesn't process more than one -C argument
sdrach
parents: 40251
diff changeset
   982
            addFile(zos, entry);
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   983
            if (!entry.isDir) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   984
                jentries.add(entry.name);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
   985
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        if (!foundManifest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            if (newManifest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                Manifest m = new Manifest(newManifest);
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   990
                updateOk = !isAmbiguousMainClass(m);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                if (updateOk) {
15682
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents: 14504
diff changeset
   992
                    if (!updateManifest(m, zos)) {
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents: 14504
diff changeset
   993
                        updateOk = false;
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents: 14504
diff changeset
   994
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                }
19409
d7c7b9d56631 8022921: Remove experimental Profile attribute
alanb
parents: 15682
diff changeset
   996
            } else if (ename != null) {
15682
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents: 14504
diff changeset
   997
                if (!updateManifest(new Manifest(), zos)) {
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents: 14504
diff changeset
   998
                    updateOk = false;
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents: 14504
diff changeset
   999
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        }
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1002
        if (updateOk) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1003
            if (moduleInfos != null && !moduleInfos.isEmpty()) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1004
                Set<String> pkgs = new HashSet<>();
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1005
                jentries.forEach( je -> addPackageIfNamed(pkgs, je));
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1006
                addExtendedModuleAttributes(moduleInfos, pkgs);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1007
                updateOk = checkModuleInfo(moduleInfos.get(MODULE_INFO), jentries);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1008
                updateModuleInfo(moduleInfos, zos);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1009
                // TODO: check manifest main classes, etc
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1010
            } else if (moduleVersion != null || modulesToHash != null) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1011
                error(getMsg("error.module.options.without.info"));
38468
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  1012
                updateOk = false;
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  1013
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1014
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        zis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        zos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        return updateOk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    private void addIndex(JarIndex index, ZipOutputStream zos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    {
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1023
        ZipEntry e = new ZipEntry(INDEX_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        e.setTime(System.currentTimeMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        if (flag0) {
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1026
            CRC32OutputStream os = new CRC32OutputStream();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1027
            index.write(os);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1028
            os.updateEntry(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        zos.putNextEntry(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        index.write(zos);
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1032
        zos.closeEntry();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1035
    private void updateModuleInfo(Map<String,byte[]> moduleInfos, ZipOutputStream zos)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1036
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1037
    {
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1038
        String fmt = uflag ? "out.update.module-info": "out.added.module-info";
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1039
        for (Map.Entry<String,byte[]> mi : moduleInfos.entrySet()) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1040
            String name = mi.getKey();
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1041
            byte[] bytes = mi.getValue();
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1042
            ZipEntry e = new ZipEntry(name);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1043
            e.setTime(System.currentTimeMillis());
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1044
            if (flag0) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1045
                crc32ModuleInfo(e, bytes);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1046
            }
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1047
            zos.putNextEntry(e);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1048
            zos.write(bytes);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1049
            zos.closeEntry();
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1050
            if (vflag) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1051
                output(formatMsg(fmt, name));
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1052
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1053
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1054
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1055
15682
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents: 14504
diff changeset
  1056
    private boolean updateManifest(Manifest m, ZipOutputStream zos)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        addVersion(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        addCreatedBy(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        if (ename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            addMainClass(m, ename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        }
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
  1064
        if (isMultiRelease) {
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
  1065
            addMultiRelease(m);
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
  1066
        }
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1067
        ZipEntry e = new ZipEntry(MANIFEST_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        e.setTime(System.currentTimeMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        if (flag0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            crc32Manifest(e, m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        zos.putNextEntry(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        m.write(zos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        if (vflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
            output(getMsg("out.update.manifest"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        }
15682
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents: 14504
diff changeset
  1077
        return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
29914
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1080
    private static final boolean isWinDriveLetter(char c) {
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1081
        return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1082
    }
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1083
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1084
    private String safeName(String name) {
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1085
        if (!pflag) {
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1086
            int len = name.length();
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1087
            int i = name.lastIndexOf("../");
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1088
            if (i == -1) {
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1089
                i = 0;
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1090
            } else {
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1091
                i += 3; // strip any dot-dot components
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1092
            }
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1093
            if (File.separatorChar == '\\') {
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1094
                // the spec requests no drive letter. skip if
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1095
                // the entry name has one.
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1096
                while (i < len) {
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1097
                    int off = i;
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1098
                    if (i + 1 < len &&
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1099
                        name.charAt(i + 1) == ':' &&
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1100
                        isWinDriveLetter(name.charAt(i))) {
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1101
                        i += 2;
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1102
                    }
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1103
                    while (i < len && name.charAt(i) == '/') {
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1104
                        i++;
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1105
                    }
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1106
                    if (i == off) {
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1107
                        break;
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1108
                    }
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1109
                }
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1110
            } else {
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1111
                while (i < len && name.charAt(i) == '/') {
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1112
                    i++;
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1113
                }
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1114
            }
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1115
            if (i != 0) {
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1116
                name = name.substring(i);
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1117
            }
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1118
        }
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1119
        return name;
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1120
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    private void addVersion(Manifest m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        Attributes global = m.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        if (global.getValue(Attributes.Name.MANIFEST_VERSION) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            global.put(Attributes.Name.MANIFEST_VERSION, VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    private void addCreatedBy(Manifest m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        Attributes global = m.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        if (global.getValue(new Attributes.Name("Created-By")) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            String javaVendor = System.getProperty("java.vendor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            String jdkVersion = System.getProperty("java.version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            global.put(new Attributes.Name("Created-By"), jdkVersion + " (" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                        javaVendor + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    private void addMainClass(Manifest m, String mainApp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        Attributes global = m.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        // overrides any existing Main-Class attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        global.put(Attributes.Name.MAIN_CLASS, mainApp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
  1146
    private void addMultiRelease(Manifest m) {
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
  1147
        Attributes global = m.getMainAttributes();
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
  1148
        global.put(Attributes.Name.MULTI_RELEASE, "true");
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
  1149
    }
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
  1150
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1151
    private boolean isAmbiguousMainClass(Manifest m) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        if (ename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            Attributes global = m.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            if ((global.get(Attributes.Name.MAIN_CLASS) != null)) {
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
  1155
                usageError(getMsg("error.bad.eflag"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1162
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * Adds a new file entry to the ZIP output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     */
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
  1165
    void addFile(ZipOutputStream zos, Entry entry) throws IOException {
41221
eb0dc5ea2302 8165944: jar utility doesn't process more than one -C argument
sdrach
parents: 40251
diff changeset
  1166
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
  1167
        File file = entry.file;
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1168
        String name = entry.name;
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
  1169
        boolean isDir = entry.isDir;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        if (name.equals("") || name.equals(".") || name.equals(zname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            return;
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1173
        } else if ((name.equals(MANIFEST_DIR) || name.equals(MANIFEST_NAME))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                   && !Mflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            if (vflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                output(formatMsg("out.ignore.entry", name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            return;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1179
        } else if (name.equals(MODULE_INFO)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1180
            throw new Error("Unexpected module info: " + name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        long size = isDir ? 0 : file.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        if (vflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            out.print(formatMsg("out.adding", name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        ZipEntry e = new ZipEntry(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        e.setTime(file.lastModified());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        if (size == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            e.setMethod(ZipEntry.STORED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            e.setSize(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            e.setCrc(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        } else if (flag0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            crc32File(e, file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        zos.putNextEntry(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        if (!isDir) {
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1199
            copy(file, zos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        zos.closeEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        /* report how much compression occurred. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        if (vflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            size = e.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            long csize = e.getCompressedSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            out.print(formatMsg2("out.size", String.valueOf(size),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                        String.valueOf(csize)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            if (e.getMethod() == ZipEntry.DEFLATED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                long ratio = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                if (size != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                    ratio = ((size - csize) * 100) / size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                output(formatMsg("out.deflated", String.valueOf(ratio)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                output(getMsg("out.stored"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1220
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1221
     * A buffer for use only by copy(InputStream, OutputStream).
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1222
     * Not as clean as allocating a new buffer as needed by copy,
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1223
     * but significantly more efficient.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     */
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1225
    private byte[] copyBuf = new byte[8192];
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1226
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1227
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1228
     * Copies all bytes from the input stream to the output stream.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1229
     * Does not close or flush either stream.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1230
     *
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1231
     * @param from the input stream to read from
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1232
     * @param to the output stream to write to
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1233
     * @throws IOException if an I/O error occurs
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1234
     */
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1235
    private void copy(InputStream from, OutputStream to) throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1236
        int n;
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1237
        while ((n = from.read(copyBuf)) != -1)
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1238
            to.write(copyBuf, 0, n);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1241
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1242
     * Copies all bytes from the input file to the output stream.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1243
     * Does not close or flush the output stream.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1244
     *
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1245
     * @param from the input file to read from
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1246
     * @param to the output stream to write to
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1247
     * @throws IOException if an I/O error occurs
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1248
     */
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1249
    private void copy(File from, OutputStream to) throws IOException {
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1250
        try (InputStream in = new FileInputStream(from)) {
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1251
            copy(in, to);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1252
        }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1253
    }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1254
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1255
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1256
     * Copies all bytes from the input stream to the output file.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1257
     * Does not close the input stream.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1258
     *
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1259
     * @param from the input stream to read from
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1260
     * @param to the output file to write to
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1261
     * @throws IOException if an I/O error occurs
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1262
     */
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1263
    private void copy(InputStream from, File to) throws IOException {
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1264
        try (OutputStream out = new FileOutputStream(to)) {
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1265
            copy(from, out);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1266
        }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1267
    }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1268
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1269
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1270
     * Computes the crc32 of a module-info.class.  This is necessary when the
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1271
     * ZipOutputStream is in STORED mode.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1272
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1273
    private void crc32ModuleInfo(ZipEntry e, byte[] bytes) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1274
        CRC32OutputStream os = new CRC32OutputStream();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1275
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1276
        in.transferTo(os);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1277
        os.updateEntry(e);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1278
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1279
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1280
    /**
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1281
     * Computes the crc32 of a Manifest.  This is necessary when the
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1282
     * ZipOutputStream is in STORED mode.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1283
     */
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1284
    private void crc32Manifest(ZipEntry e, Manifest m) throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1285
        CRC32OutputStream os = new CRC32OutputStream();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1286
        m.write(os);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1287
        os.updateEntry(e);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1288
    }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1289
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1290
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1291
     * Computes the crc32 of a File.  This is necessary when the
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1292
     * ZipOutputStream is in STORED mode.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    private void crc32File(ZipEntry e, File f) throws IOException {
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1295
        CRC32OutputStream os = new CRC32OutputStream();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1296
        copy(f, os);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1297
        if (os.n != f.length()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            throw new JarException(formatMsg(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                        "error.incorrect.length", f.getPath()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        }
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1301
        os.updateEntry(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
  1304
    void replaceFSC(Map<Integer, String []> filesMap) {
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
  1305
        filesMap.keySet().forEach(version -> {
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
  1306
            String[] files = filesMap.get(version);
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
  1307
            if (files != null) {
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
  1308
                for (int i = 0; i < files.length; i++) {
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
  1309
                    files[i] = files[i].replace(File.separatorChar, '/');
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
  1310
                }
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1311
            }
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
  1312
        });
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1313
    }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1314
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1315
    @SuppressWarnings("serial")
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1316
    Set<ZipEntry> newDirSet() {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1317
        return new HashSet<ZipEntry>() {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1318
            public boolean add(ZipEntry e) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1319
                return ((e == null || useExtractionTime) ? false : super.add(e));
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1320
            }};
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1321
    }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1322
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1323
    void updateLastModifiedTime(Set<ZipEntry> zes) throws IOException {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1324
        for (ZipEntry ze : zes) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1325
            long lastModified = ze.getTime();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1326
            if (lastModified != -1) {
29914
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1327
                String name = safeName(ze.getName().replace(File.separatorChar, '/'));
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1328
                if (name.length() != 0) {
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1329
                    File f = new File(name.replace('/', File.separatorChar));
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1330
                    f.setLastModified(lastModified);
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1331
                }
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1332
            }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1333
        }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1334
    }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1335
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1336
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * Extracts specified entries from JAR file.
26714
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
  1338
     *
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
  1339
     * @return whether entries were found and successfully extracted
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
  1340
     * (indicating this was a zip file without "leading garbage")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     */
26714
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
  1342
    boolean extract(InputStream in, String files[]) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        ZipInputStream zis = new ZipInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        ZipEntry e;
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
  1345
        // Set of all directory entries specified in archive.  Disallows
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        // null entries.  Disallows all entries if using pre-6.0 behavior.
26714
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
  1347
        boolean entriesFound = false;
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1348
        Set<ZipEntry> dirs = newDirSet();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        while ((e = zis.getNextEntry()) != null) {
26714
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
  1350
            entriesFound = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            if (files == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                dirs.add(extractFile(zis, e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                String name = e.getName();
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1355
                for (String file : files) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                    if (name.startsWith(file)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                        dirs.add(extractFile(zis, e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        // Update timestamps of directories specified in archive with their
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        // timestamps as given in the archive.  We do this after extraction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        // instead of during, because creating a file in a directory changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        // that directory's timestamp.
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1368
        updateLastModifiedTime(dirs);
26714
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
  1369
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents: 25859
diff changeset
  1370
        return entriesFound;
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1371
    }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1372
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1373
    /**
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1374
     * Extracts specified entries from JAR file, via ZipFile.
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1375
     */
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1376
    void extract(String fname, String files[]) throws IOException {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1377
        ZipFile zf = new ZipFile(fname);
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1378
        Set<ZipEntry> dirs = newDirSet();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1379
        Enumeration<? extends ZipEntry> zes = zf.entries();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1380
        while (zes.hasMoreElements()) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1381
            ZipEntry e = zes.nextElement();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1382
            if (files == null) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1383
                dirs.add(extractFile(zf.getInputStream(e), e));
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1384
            } else {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1385
                String name = e.getName();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1386
                for (String file : files) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1387
                    if (name.startsWith(file)) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1388
                        dirs.add(extractFile(zf.getInputStream(e), e));
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1389
                        break;
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1390
                    }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1391
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        }
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1394
        zf.close();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1395
        updateLastModifiedTime(dirs);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1398
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * Extracts next entry from JAR file, creating directories as needed.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * the entry is for a directory which doesn't exist prior to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * invocation, returns that entry, otherwise returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     */
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1403
    ZipEntry extractFile(InputStream is, ZipEntry e) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        ZipEntry rc = null;
29914
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1405
        // The spec requres all slashes MUST be forward '/', it is possible
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1406
        // an offending zip/jar entry may uses the backwards slash in its
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1407
        // name. It might cause problem on Windows platform as it skips
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1408
        // our "safe" check for leading slahs and dot-dot. So replace them
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1409
        // with '/'.
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1410
        String name = safeName(e.getName().replace(File.separatorChar, '/'));
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1411
        if (name.length() == 0) {
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1412
            return rc;    // leading '/' or 'dot-dot' only path
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1413
        }
48393dc87f88 8064601: Improve jar file handling
sherman
parents: 29369
diff changeset
  1414
        File f = new File(name.replace('/', File.separatorChar));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        if (e.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
            if (f.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                if (!f.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
                    throw new IOException(formatMsg("error.create.dir",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
                        f.getPath()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                if (!f.mkdirs()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                    throw new IOException(formatMsg("error.create.dir",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                        f.getPath()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                    rc = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            if (vflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                output(formatMsg("out.create", name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            if (f.getParent() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                File d = new File(f.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                if (!d.exists() && !d.mkdirs() || !d.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                    throw new IOException(formatMsg(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                        "error.create.dir", d.getPath()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
            }
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1441
            try {
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1442
                copy(is, f);
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1443
            } finally {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1444
                if (is instanceof ZipInputStream)
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1445
                    ((ZipInputStream)is).closeEntry();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1446
                else
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1447
                    is.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
            if (vflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                if (e.getMethod() == ZipEntry.DEFLATED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                    output(formatMsg("out.inflated", name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                    output(formatMsg("out.extracted", name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        if (!useExtractionTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            long lastModified = e.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
            if (lastModified != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                f.setLastModified(lastModified);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        return rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1466
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * Lists contents of JAR file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
    void list(InputStream in, String files[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        ZipInputStream zis = new ZipInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        ZipEntry e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        while ((e = zis.getNextEntry()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
             * In the case of a compressed (deflated) entry, the entry size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
             * is stored immediately following the entry data and cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
             * determined until the entry is fully read. Therefore, we close
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
             * the entry first before printing out its attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
            zis.closeEntry();
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1480
            printEntry(e, files);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1484
    /**
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1485
     * Lists contents of JAR file, via ZipFile.
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1486
     */
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1487
    void list(String fname, String files[]) throws IOException {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1488
        ZipFile zf = new ZipFile(fname);
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1489
        Enumeration<? extends ZipEntry> zes = zf.entries();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1490
        while (zes.hasMoreElements()) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1491
            printEntry(zes.nextElement(), files);
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1492
        }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1493
        zf.close();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1494
    }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1495
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
    /**
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1497
     * Outputs the class index table to the INDEX.LIST file of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     * root jar file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
    void dumpIndex(String rootjar, JarIndex index) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        File jarFile = new File(rootjar);
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1502
        Path jarPath = jarFile.toPath();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1503
        Path tmpPath = createTempFileInSameDirectoryAs(jarFile).toPath();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1504
        try {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1505
            if (update(Files.newInputStream(jarPath),
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1506
                       Files.newOutputStream(tmpPath),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1507
                       null, null, index)) {
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1508
                try {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1509
                    Files.move(tmpPath, jarPath, REPLACE_EXISTING);
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1510
                } catch (IOException e) {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1511
                    throw new IOException(getMsg("error.write.file"), e);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1512
                }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1513
            }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1514
        } finally {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1515
            Files.deleteIfExists(tmpPath);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1519
    private HashSet<String> jarPaths = new HashSet<String>();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1520
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1521
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1522
     * Generates the transitive closure of the Class-Path attribute for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     * the specified jar file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     */
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1525
    List<String> getJarPath(String jar) throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1526
        List<String> files = new ArrayList<String>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        files.add(jar);
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1528
        jarPaths.add(jar);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        // take out the current path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        String path = jar.substring(0, Math.max(0, jar.lastIndexOf('/') + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        // class path attribute will give us jar file name with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        // '/' as separators, so we need to change them to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        // appropriate one before we open the jar file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
        JarFile rf = new JarFile(jar.replace('/', File.separatorChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
        if (rf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
            Manifest man = rf.getManifest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
            if (man != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                Attributes attr = man.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                    String value = attr.getValue(Attributes.Name.CLASS_PATH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                    if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                        StringTokenizer st = new StringTokenizer(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                        while (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                            String ajar = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                            if (!ajar.endsWith("/")) {  // it is a jar file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                                ajar = path.concat(ajar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                                /* check on cyclic dependency */
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1551
                                if (! jarPaths.contains(ajar)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                                    files.addAll(getJarPath(ajar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        rf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        return files;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
    /**
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1565
     * Generates class index file for the specified root jar file.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
    void genIndex(String rootjar, String[] files) throws IOException {
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1568
        List<String> jars = getJarPath(rootjar);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        int njars = jars.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        String[] jarfiles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
        if (njars == 1 && files != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
            // no class-path attribute defined in rootjar, will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
            // use command line specified list of jars
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
            for (int i = 0; i < files.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                jars.addAll(getJarPath(files[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            njars = jars.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        }
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1580
        jarfiles = jars.toArray(new String[njars]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        JarIndex index = new JarIndex(jarfiles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        dumpIndex(rootjar, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1585
    /**
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1586
     * Prints entry information, if requested.
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1587
     */
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1588
    void printEntry(ZipEntry e, String[] files) throws IOException {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1589
        if (files == null) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1590
            printEntry(e);
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1591
        } else {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1592
            String name = e.getName();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1593
            for (String file : files) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1594
                if (name.startsWith(file)) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1595
                    printEntry(e);
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1596
                    return;
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1597
                }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1598
            }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1599
        }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1600
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1602
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     * Prints entry information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
    void printEntry(ZipEntry e) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
        if (vflag) {
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1607
            StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
            String s = Long.toString(e.getSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
            for (int i = 6 - s.length(); i > 0; --i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                sb.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
            sb.append(s).append(' ').append(new Date(e.getTime()).toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
            sb.append(' ').append(e.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            output(sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
            output(e.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1620
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1621
     * Prints usage message.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
     */
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
  1623
    void usageError(String s) {
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 42345
diff changeset
  1624
        err.println(s);
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  1625
        err.println(getMsg("main.usage.summary.try"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1628
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     * A fatal exception has been caught.  No recovery possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
    void fatalError(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1635
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     * A fatal condition has been detected; message is "s".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     * No recovery possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
    void fatalError(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
        error(program + ": " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
     * Print an output message; like verbose output and the like
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
    protected void output(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        out.println(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
    /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19409
diff changeset
  1651
     * Print an error message; like something is broken
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     */
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38468
diff changeset
  1653
    void error(String s) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        err.println(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1657
    /**
41754
8b7c8d5e9a0d 8164805: Fail to create a MR modular JAR with a versioned entry of a concealed package
sdrach
parents: 41484
diff changeset
  1658
     * Print a warning message
8b7c8d5e9a0d 8164805: Fail to create a MR modular JAR with a versioned entry of a concealed package
sdrach
parents: 41484
diff changeset
  1659
     */
8b7c8d5e9a0d 8164805: Fail to create a MR modular JAR with a versioned entry of a concealed package
sdrach
parents: 41484
diff changeset
  1660
    void warn(String s) {
8b7c8d5e9a0d 8164805: Fail to create a MR modular JAR with a versioned entry of a concealed package
sdrach
parents: 41484
diff changeset
  1661
        err.println(s);
8b7c8d5e9a0d 8164805: Fail to create a MR modular JAR with a versioned entry of a concealed package
sdrach
parents: 41484
diff changeset
  1662
    }
8b7c8d5e9a0d 8164805: Fail to create a MR modular JAR with a versioned entry of a concealed package
sdrach
parents: 41484
diff changeset
  1663
8b7c8d5e9a0d 8164805: Fail to create a MR modular JAR with a versioned entry of a concealed package
sdrach
parents: 41484
diff changeset
  1664
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
     * Main routine to start program.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
    public static void main(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        Main jartool = new Main(System.out, System.err, "jar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        System.exit(jartool.run(args) ? 0 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1672
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1673
     * An OutputStream that doesn't send its output anywhere, (but could).
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1674
     * It's here to find the CRC32 of an input file, necessary for STORED
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1675
     * mode in ZIP.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1676
     */
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1677
    private static class CRC32OutputStream extends java.io.OutputStream {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1678
        final CRC32 crc = new CRC32();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1679
        long n = 0;
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1680
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1681
        CRC32OutputStream() {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1683
        public void write(int r) throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1684
            crc.update(r);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1685
            n++;
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1686
        }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1687
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1688
        public void write(byte[] b, int off, int len) throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1689
            crc.update(b, off, len);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1690
            n += len;
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1691
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1693
        /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1694
         * Updates a ZipEntry which describes the data read by this
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1695
         * output stream, in STORED mode.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1696
         */
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1697
        public void updateEntry(ZipEntry e) {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1698
            e.setMethod(ZipEntry.STORED);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1699
            e.setSize(n);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1700
            e.setCrc(crc.getValue());
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1701
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
    }
21348
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1703
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1704
    /**
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1705
     * Attempt to create temporary file in the system-provided temporary folder, if failed attempts
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1706
     * to create it in the same folder as the file in parameter (if any)
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1707
     */
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1708
    private File createTemporaryFile(String tmpbase, String suffix) {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1709
        File tmpfile = null;
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1710
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1711
        try {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1712
            tmpfile = File.createTempFile(tmpbase, suffix);
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1713
        } catch (IOException | SecurityException e) {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1714
            // Unable to create file due to permission violation or security exception
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1715
        }
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1716
        if (tmpfile == null) {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1717
            // Were unable to create temporary file, fall back to temporary file in the same folder
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1718
            if (fname != null) {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1719
                try {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1720
                    File tmpfolder = new File(fname).getAbsoluteFile().getParentFile();
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1721
                    tmpfile = File.createTempFile(fname, ".tmp" + suffix, tmpfolder);
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1722
                } catch (IOException ioe) {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1723
                    // Last option failed - fall gracefully
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1724
                    fatalError(ioe);
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1725
                }
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1726
            } else {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1727
                // No options left - we can not compress to stdout without access to the temporary folder
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1728
                fatalError(new IOException(getMsg("error.create.tempfile")));
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1729
            }
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1730
        }
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1731
        return tmpfile;
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents: 19409
diff changeset
  1732
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1733
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1734
    // Modular jar support
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1735
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1736
    /**
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1737
     * Associates a module descriptor's zip entry name along with its
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1738
     * bytes and an optional URI. Used when describing modules.
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1739
     */
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1740
    interface ModuleInfoEntry {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1741
       String name();
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1742
       Optional<String> uriString();
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1743
       InputStream bytes() throws IOException;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1744
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1745
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1746
    static class ZipFileModuleInfoEntry implements ModuleInfoEntry {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1747
        private final ZipFile zipFile;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1748
        private final ZipEntry entry;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1749
        ZipFileModuleInfoEntry(ZipFile zipFile, ZipEntry entry) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1750
            this.zipFile = zipFile;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1751
            this.entry = entry;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1752
        }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1753
        @Override public String name() { return entry.getName(); }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1754
        @Override public InputStream bytes() throws IOException {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1755
            return zipFile.getInputStream(entry);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1756
        }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1757
        /** Returns an optional containing the effective URI. */
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1758
        @Override public Optional<String> uriString() {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1759
            String uri = (Paths.get(zipFile.getName())).toUri().toString();
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1760
            uri = "jar:" + uri + "/!" + entry.getName();
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1761
            return Optional.of(uri);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1762
        }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1763
    }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1764
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1765
    static class StreamedModuleInfoEntry implements ModuleInfoEntry {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1766
        private final String name;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1767
        private final byte[] bytes;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1768
        StreamedModuleInfoEntry(String name, byte[] bytes) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1769
            this.name = name;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1770
            this.bytes = bytes;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1771
        }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1772
        @Override public String name() { return name; }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1773
        @Override public InputStream bytes() throws IOException {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1774
            return new ByteArrayInputStream(bytes);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1775
        }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1776
        /** Returns an empty optional. */
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1777
        @Override public Optional<String> uriString() {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1778
            return Optional.empty();  // no URI can be derived
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1779
        }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1780
    }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1781
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1782
    /** Describes a module from a given zip file. */
44267
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1783
    private boolean describeModule(ZipFile zipFile) throws IOException {
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1784
        ZipFileModuleInfoEntry[] infos = zipFile.stream()
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1785
                .filter(e -> isModuleInfoEntry(e.getName()))
47958
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  1786
                .sorted(ENTRY_COMPARATOR)
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1787
                .map(e -> new ZipFileModuleInfoEntry(zipFile, e))
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1788
                .toArray(ZipFileModuleInfoEntry[]::new);
44267
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1789
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1790
        if (infos.length == 0) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1791
            // No module descriptor found, derive and describe the automatic module
44267
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1792
            String fn = zipFile.getName();
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1793
            ModuleFinder mf = ModuleFinder.of(Paths.get(fn));
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1794
            try {
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1795
                Set<ModuleReference> mref = mf.findAll();
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1796
                if (mref.isEmpty()) {
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1797
                    output(formatMsg("error.unable.derive.automodule", fn));
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1798
                    return true;
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1799
                }
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1800
                ModuleDescriptor md = mref.iterator().next().descriptor();
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1801
                output(getMsg("out.automodule") + "\n");
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1802
                describeModule(md, null, null, "");
44267
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1803
            } catch (FindException e) {
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1804
                String msg = formatMsg("error.unable.derive.automodule", fn);
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1805
                Throwable t = e.getCause();
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1806
                if (t != null)
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1807
                    msg = msg + "\n" + t.getMessage();
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1808
                output(msg);
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1809
            }
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1810
        } else {
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1811
            return describeModuleFromEntries(infos);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1812
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1813
        return true;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1814
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1815
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1816
    private boolean describeModuleFromStream(FileInputStream fis)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1817
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1818
    {
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1819
        List<ModuleInfoEntry> infos = new LinkedList<>();
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1820
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1821
        try (BufferedInputStream bis = new BufferedInputStream(fis);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1822
             ZipInputStream zis = new ZipInputStream(bis)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1823
            ZipEntry e;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1824
            while ((e = zis.getNextEntry()) != null) {
43731
bc7110b230c1 8165640: Enhance jar tool to allow module-info in versioned directories but not in base in modular multi-release jar files
sherman
parents: 43712
diff changeset
  1825
                String ename = e.getName();
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1826
                if (isModuleInfoEntry(ename)) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1827
                    infos.add(new StreamedModuleInfoEntry(ename, zis.readAllBytes()));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1828
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1829
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1830
        }
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1831
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1832
        if (infos.size() == 0)
43731
bc7110b230c1 8165640: Enhance jar tool to allow module-info in versioned directories but not in base in modular multi-release jar files
sherman
parents: 43712
diff changeset
  1833
            return false;
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1834
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1835
        ModuleInfoEntry[] sorted = infos.stream()
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1836
                .sorted(Comparator.comparing(ModuleInfoEntry::name, ENTRYNAME_COMPARATOR))
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1837
                .toArray(ModuleInfoEntry[]::new);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1838
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1839
        return describeModuleFromEntries(sorted);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1840
    }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1841
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1842
    private boolean lessThanEqualReleaseValue(ModuleInfoEntry entry) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1843
        return intVersionFromEntry(entry) <= releaseValue ? true : false;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1844
    }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1845
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1846
    private static String versionFromEntryName(String name) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1847
        String s = name.substring(VERSIONS_DIR_LENGTH);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1848
        return s.substring(0, s.indexOf("/"));
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1849
    }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1850
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1851
    private static int intVersionFromEntry(ModuleInfoEntry entry) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1852
        String name = entry.name();
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1853
        if (!name.startsWith(VERSIONS_DIR))
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1854
            return BASE_VERSION;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1855
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1856
        String s = name.substring(VERSIONS_DIR_LENGTH);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1857
        s = s.substring(0, s.indexOf('/'));
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1858
        return Integer.valueOf(s);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1859
    }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1860
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1861
    /**
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1862
     * Describes a single module descriptor, determined by the specified
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1863
     * --release, if any, from the given ordered entries.
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1864
     * The given infos must be ordered as per ENTRY_COMPARATOR.
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1865
     */
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1866
    private boolean describeModuleFromEntries(ModuleInfoEntry[] infos)
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1867
        throws IOException
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1868
    {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1869
        assert infos.length > 0;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1870
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1871
        // Informative: output all non-root descriptors, if any
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1872
        String releases = Arrays.stream(infos)
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1873
                .filter(e -> !e.name().equals(MODULE_INFO))
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1874
                .map(ModuleInfoEntry::name)
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1875
                .map(Main::versionFromEntryName)
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1876
                .collect(joining(" "));
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1877
        if (!releases.equals(""))
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1878
            output("releases: " + releases + "\n");
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1879
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1880
        // Describe the operative descriptor for the specified --release, if any
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1881
        if (releaseValue != -1) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1882
            ModuleInfoEntry entry = null;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1883
            int i = 0;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1884
            while (i < infos.length && lessThanEqualReleaseValue(infos[i])) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1885
                entry = infos[i];
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1886
                i++;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1887
            }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1888
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1889
            if (entry == null) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1890
                output(formatMsg("error.no.operative.descriptor",
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1891
                                 String.valueOf(releaseValue)));
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1892
                return false;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1893
            }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1894
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1895
            String uriString = entry.uriString().orElse("");
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1896
            try (InputStream is = entry.bytes()) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1897
                describeModule(is, uriString);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1898
            }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1899
        } else {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1900
            // no specific --release specified, output the root, if any
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1901
            if (infos[0].name().equals(MODULE_INFO)) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1902
                String uriString = infos[0].uriString().orElse("");
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1903
                try (InputStream is = infos[0].bytes()) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1904
                    describeModule(is, uriString);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1905
                }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1906
            } else {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1907
                // no root, output message to specify --release
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1908
                output(getMsg("error.no.root.descriptor"));
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1909
            }
43731
bc7110b230c1 8165640: Enhance jar tool to allow module-info in versioned directories but not in base in modular multi-release jar files
sherman
parents: 43712
diff changeset
  1910
        }
bc7110b230c1 8165640: Enhance jar tool to allow module-info in versioned directories but not in base in modular multi-release jar files
sherman
parents: 43712
diff changeset
  1911
        return true;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1912
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1913
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
  1914
    static <T> String toString(Collection<T> set) {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  1915
        if (set.isEmpty()) { return ""; }
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1916
        return " " + set.stream().map(e -> e.toString().toLowerCase(Locale.ROOT))
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1917
                  .sorted().collect(joining(" "));
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  1918
    }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  1919
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1920
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1921
    private void describeModule(InputStream entryInputStream, String uriString)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1922
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1923
    {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
  1924
        ModuleInfo.Attributes attrs = ModuleInfo.read(entryInputStream, null);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
  1925
        ModuleDescriptor md = attrs.descriptor();
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44033
diff changeset
  1926
        ModuleTarget target = attrs.target();
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
  1927
        ModuleHashes hashes = attrs.recordedHashes();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
  1928
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1929
        describeModule(md, target, hashes, uriString);
44267
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1930
    }
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1931
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1932
    private void describeModule(ModuleDescriptor md,
44360
alanb
parents: 44267 44359
diff changeset
  1933
                                ModuleTarget target,
44267
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1934
                                ModuleHashes hashes,
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1935
                                String uriString)
44267
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1936
        throws IOException
00898a596f31 8176772: jar tool support to report automatic module names
chegar
parents: 44033
diff changeset
  1937
    {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1938
        StringBuilder sb = new StringBuilder();
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1939
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1940
        sb.append(md.toNameAndVersion());
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1941
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1942
        if (!uriString.equals(""))
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1943
            sb.append(" ").append(uriString);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1944
        if (md.isOpen())
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1945
            sb.append(" open");
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1946
        if (md.isAutomatic())
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1947
            sb.append(" automatic");
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1948
        sb.append("\n");
43731
bc7110b230c1 8165640: Enhance jar tool to allow module-info in versioned directories but not in base in modular multi-release jar files
sherman
parents: 43712
diff changeset
  1949
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1950
        // unqualified exports (sorted by package)
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1951
        md.exports().stream()
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1952
                .sorted(Comparator.comparing(Exports::source))
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1953
                .filter(e -> !e.isQualified())
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1954
                .forEach(e -> sb.append("exports ").append(e.source())
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1955
                                .append(toString(e.modifiers())).append("\n"));
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1956
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1957
        // dependences
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1958
        md.requires().stream().sorted()
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1959
                .forEach(r -> sb.append("requires ").append(r.name())
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1960
                                .append(toString(r.modifiers())).append("\n"));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1961
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1962
        // service use and provides
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1963
        md.uses().stream().sorted()
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1964
                .forEach(s -> sb.append("uses ").append(s).append("\n"));
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1965
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1966
        md.provides().stream()
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1967
                .sorted(Comparator.comparing(Provides::service))
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1968
                .forEach(p -> sb.append("provides ").append(p.service())
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1969
                                .append(" with")
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1970
                                .append(toString(p.providers()))
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1971
                                .append("\n"));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1972
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1973
        // qualified exports
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1974
        md.exports().stream()
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1975
                .sorted(Comparator.comparing(Exports::source))
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1976
                .filter(Exports::isQualified)
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1977
                .forEach(e -> sb.append("qualified exports ").append(e.source())
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1978
                                .append(" to").append(toString(e.targets()))
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1979
                                .append("\n"));
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  1980
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1981
        // open packages
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1982
        md.opens().stream()
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1983
                .sorted(Comparator.comparing(Opens::source))
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1984
                .filter(o -> !o.isQualified())
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1985
                .forEach(o -> sb.append("opens ").append(o.source())
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1986
                                 .append(toString(o.modifiers()))
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1987
                                 .append("\n"));
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  1988
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
  1989
        md.opens().stream()
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1990
                .sorted(Comparator.comparing(Opens::source))
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1991
                .filter(Opens::isQualified)
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1992
                .forEach(o -> sb.append("qualified opens ").append(o.source())
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1993
                                 .append(toString(o.modifiers()))
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1994
                                 .append(" to").append(toString(o.targets()))
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1995
                                 .append("\n"));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  1996
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1997
        // non-exported/non-open packages
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  1998
        Set<String> concealed = new TreeSet<>(md.packages());
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
  1999
        md.exports().stream().map(Exports::source).forEach(concealed::remove);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
  2000
        md.opens().stream().map(Opens::source).forEach(concealed::remove);
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  2001
        concealed.forEach(p -> sb.append("contains ").append(p).append("\n"));
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
  2002
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  2003
        md.mainClass().ifPresent(v -> sb.append("main-class ").append(v).append("\n"));
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2004
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44033
diff changeset
  2005
        if (target != null) {
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  2006
            String targetPlatform = target.targetPlatform();
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  2007
            if (!targetPlatform.isEmpty())
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  2008
                sb.append("platform ").append(targetPlatform).append("\n");
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 44033
diff changeset
  2009
       }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2010
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2011
       if (hashes != null) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2012
           hashes.names().stream().sorted().forEach(
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  2013
                   mod -> sb.append("hashes ").append(mod).append(" ")
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2014
                            .append(hashes.algorithm()).append(" ")
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  2015
                            .append(toHex(hashes.hashFor(mod)))
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44360
diff changeset
  2016
                            .append("\n"));
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
  2017
        }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2018
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2019
        output(sb.toString());
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2020
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2021
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
  2022
    private static String toHex(byte[] ba) {
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2023
        StringBuilder sb = new StringBuilder(ba.length << 1);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
  2024
        for (byte b: ba) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
  2025
            sb.append(String.format("%02x", b & 0xff));
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
  2026
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
  2027
        return sb.toString();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
  2028
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
  2029
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2030
    static String toBinaryName(String classname) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2031
        return (classname.replace('.', '/')) + ".class";
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2032
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2033
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2034
    private boolean checkModuleInfo(byte[] moduleInfoBytes, Set<String> entries)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2035
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2036
    {
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2037
        boolean ok = true;
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2038
        if (moduleInfoBytes != null) {  // no root module-info.class if null
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2039
            try {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2040
                // ModuleDescriptor.read() checks open/exported pkgs vs packages
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2041
                ModuleDescriptor md = ModuleDescriptor.read(ByteBuffer.wrap(moduleInfoBytes));
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2042
                // A module must have the implementation class of the services it 'provides'.
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2043
                if (md.provides().stream().map(Provides::providers).flatMap(List::stream)
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2044
                      .filter(p -> !entries.contains(toBinaryName(p)))
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2045
                      .peek(p -> fatalError(formatMsg("error.missing.provider", p)))
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2046
                      .count() != 0) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2047
                    ok = false;
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2048
                }
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2049
            } catch (InvalidModuleDescriptorException x) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2050
                fatalError(x.getMessage());
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2051
                ok = false;
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2052
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2053
        }
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2054
        return ok;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2055
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2056
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2057
    /**
38468
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2058
     * Adds extended modules attributes to the given module-info's.  The given
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2059
     * Map values are updated in-place. Returns false if an error occurs.
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2060
     */
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2061
    private void addExtendedModuleAttributes(Map<String,byte[]> moduleInfos,
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2062
                                                Set<String> packages)
38468
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2063
        throws IOException
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2064
    {
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2065
        for (Map.Entry<String,byte[]> e: moduleInfos.entrySet()) {
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2066
            ModuleDescriptor md = ModuleDescriptor.read(ByteBuffer.wrap(e.getValue()));
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2067
            e.setValue(extendedInfoBytes(md, e.getValue(), packages));
38468
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2068
        }
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2069
    }
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2070
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2071
    static boolean isModuleInfoEntry(String name) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2072
        // root or versioned module-info.class
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2073
        if (name.endsWith(MODULE_INFO)) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2074
            int end = name.length() - MODULE_INFO.length();
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2075
            if (end == 0)
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2076
                return true;
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2077
            if (name.startsWith(VERSIONS_DIR)) {
43731
bc7110b230c1 8165640: Enhance jar tool to allow module-info in versioned directories but not in base in modular multi-release jar files
sherman
parents: 43712
diff changeset
  2078
                int off = VERSIONS_DIR_LENGTH;
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2079
                if (off == end)      // meta-inf/versions/module-info.class
38468
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2080
                    return false;
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2081
                while (off < end - 1) {
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2082
                    char c = name.charAt(off++);
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2083
                    if (c < '0' || c > '9')
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2084
                        return false;
38468
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2085
                }
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2086
                return name.charAt(off) == '/';
38468
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2087
            }
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2088
        }
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2089
        return false;
38468
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2090
    }
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2091
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2092
    /**
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2093
     * Returns a byte array containing the given module-info.class plus any
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2094
     * extended attributes.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2095
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2096
     * If --module-version, --main-class, or other options were provided
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2097
     * then the corresponding class file attributes are added to the
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2098
     * module-info here.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2099
     */
43095
336dfda4ae89 8172432: jar cleanup/update for module and mrm jar
sherman
parents: 42703
diff changeset
  2100
    private byte[] extendedInfoBytes(ModuleDescriptor md,
38468
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2101
                                     byte[] miBytes,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
  2102
                                     Set<String> packages)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2103
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2104
    {
38468
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2105
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2106
        InputStream is = new ByteArrayInputStream(miBytes);
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2107
        ModuleInfoExtender extender = ModuleInfoExtender.newExtender(is);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2108
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
  2109
        // Add (or replace) the Packages attribute
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41754
diff changeset
  2110
        extender.packages(packages);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2111
38468
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2112
        // --main-class
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2113
        if (ename != null)
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2114
            extender.mainClass(ename);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2115
38468
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2116
        // --module-version
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2117
        if (moduleVersion != null)
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2118
            extender.version(moduleVersion);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2119
38468
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2120
        // --hash-modules
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2121
        if (modulesToHash != null) {
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2122
            String mn = md.name();
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2123
            Hasher hasher = new Hasher(md, fname);
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2124
            ModuleHashes moduleHashes = hasher.computeHashes(mn);
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2125
            if (moduleHashes != null) {
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2126
                extender.hashes(moduleHashes);
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2127
            } else {
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2128
                warn("warning: no module is recorded in hash in " + mn);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2129
            }
38468
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2130
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2131
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
  2132
        if (moduleResolution.value() != 0) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
  2133
            extender.moduleResolution(moduleResolution);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
  2134
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
  2135
38468
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2136
        extender.write(baos);
d459a0f8fe72 8156497: Add jar tool support for Multi-Release Modular JARs
chegar
parents: 38457
diff changeset
  2137
        return baos.toByteArray();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2138
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2139
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2140
    /**
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2141
     * Compute and record hashes
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2142
     */
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2143
    private class Hasher {
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2144
        final ModuleHashesBuilder hashesBuilder;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2145
        final ModuleFinder finder;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2146
        final Set<String> modules;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2147
        Hasher(ModuleDescriptor descriptor, String fname) throws IOException {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2148
            // Create a module finder that finds the modular JAR
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2149
            // being created/updated
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2150
            URI uri = Paths.get(fname).toUri();
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
  2151
            ModuleReference mref = new ModuleReference(descriptor, uri) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
  2152
                @Override
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
  2153
                public ModuleReader open() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
  2154
                    throw new UnsupportedOperationException("should not reach here");
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
  2155
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
  2156
            };
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2157
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2158
            // Compose a module finder with the module path and
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2159
            // the modular JAR being created or updated
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2160
            this.finder = ModuleFinder.compose(moduleFinder,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2161
                new ModuleFinder() {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2162
                    @Override
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2163
                    public Optional<ModuleReference> find(String name) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2164
                        if (descriptor.name().equals(name))
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2165
                            return Optional.of(mref);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2166
                        else
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2167
                            return Optional.empty();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2168
                    }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2169
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2170
                    @Override
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2171
                    public Set<ModuleReference> findAll() {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2172
                        return Collections.singleton(mref);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2173
                    }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2174
                });
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2175
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2176
            // Determine the modules that matches the pattern {@code modulesToHash}
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2177
            Set<String> roots = finder.findAll().stream()
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2178
                .map(ref -> ref.descriptor().name())
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2179
                .filter(mn -> modulesToHash.matcher(mn).find())
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2180
                .collect(Collectors.toSet());
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2181
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2182
            // use system module path unless it creates a modular JAR for
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2183
            // a module that is present in the system image e.g. upgradeable
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2184
            // module
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2185
            ModuleFinder system;
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2186
            String name = descriptor.name();
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2187
            if (name != null && ModuleFinder.ofSystem().find(name).isPresent()) {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2188
                system = ModuleFinder.of();
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2189
            } else {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2190
                system = ModuleFinder.ofSystem();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2191
            }
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2192
            // get a resolved module graph
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2193
            Configuration config =
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43109
diff changeset
  2194
                Configuration.empty().resolve(system, finder, roots);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2195
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2196
            // filter modules resolved from the system module finder
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2197
            this.modules = config.modules().stream()
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2198
                .map(ResolvedModule::name)
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2199
                .filter(mn -> roots.contains(mn) && !system.find(mn).isPresent())
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2200
                .collect(Collectors.toSet());
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2201
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2202
            this.hashesBuilder = new ModuleHashesBuilder(config, modules);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2203
        }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2204
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2205
        /**
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2206
         * Compute hashes of the specified module.
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2207
         *
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2208
         * It records the hashing modules that depend upon the specified
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2209
         * module directly or indirectly.
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2210
         */
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2211
        ModuleHashes computeHashes(String name) {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2212
            if (hashesBuilder == null)
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2213
                return null;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36745
diff changeset
  2214
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 43099
diff changeset
  2215
            return hashesBuilder.computeHashes(Set.of(name)).get(name);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2216
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29914
diff changeset
  2217
    }
47958
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2218
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2219
    // sort base entries before versioned entries, and sort entry classes with
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2220
    // nested classes so that the outter class appears before the associated
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2221
    // nested class
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2222
    static Comparator<String> ENTRYNAME_COMPARATOR = (s1, s2) ->  {
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2223
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2224
        if (s1.equals(s2)) return 0;
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2225
        boolean b1 = s1.startsWith(VERSIONS_DIR);
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2226
        boolean b2 = s2.startsWith(VERSIONS_DIR);
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2227
        if (b1 && !b2) return 1;
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2228
        if (!b1 && b2) return -1;
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2229
        int n = 0; // starting char for String compare
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2230
        if (b1 && b2) {
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2231
            // normally strings would be sorted so "10" goes before "9", but
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2232
            // version number strings need to be sorted numerically
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2233
            n = VERSIONS_DIR.length();   // skip the common prefix
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2234
            int i1 = s1.indexOf('/', n);
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2235
            int i2 = s2.indexOf('/', n);
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2236
            if (i1 == -1) throw new Validator.InvalidJarException(s1);
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2237
            if (i2 == -1) throw new Validator.InvalidJarException(s2);
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2238
            // shorter version numbers go first
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2239
            if (i1 != i2) return i1 - i2;
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2240
            // otherwise, handle equal length numbers below
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2241
        }
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2242
        int l1 = s1.length();
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2243
        int l2 = s2.length();
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2244
        int lim = Math.min(l1, l2);
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2245
        for (int k = n; k < lim; k++) {
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2246
            char c1 = s1.charAt(k);
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2247
            char c2 = s2.charAt(k);
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2248
            if (c1 != c2) {
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2249
                // change natural ordering so '.' comes before '$'
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2250
                // i.e. outer classes come before nested classes
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2251
                if (c1 == '$' && c2 == '.') return 1;
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2252
                if (c1 == '.' && c2 == '$') return -1;
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2253
                return c1 - c2;
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2254
            }
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2255
        }
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2256
        return l1 - l2;
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2257
    };
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2258
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2259
    static Comparator<ZipEntry> ENTRY_COMPARATOR =
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2260
        Comparator.comparing(ZipEntry::getName, ENTRYNAME_COMPARATOR);
d34958cb3163 8186087: jar tool fails to create a multi-release jar when validating nested classes
sherman
parents: 47216
diff changeset
  2261
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
}