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