1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.jmod;
  27 
  28 import java.io.BufferedInputStream;
  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 import java.nio.file.Files;
  32 import java.nio.file.Path;
  33 import java.util.Arrays;
  34 import java.util.HashMap;
  35 import java.util.Map;
  36 import java.util.function.Function;
  37 import java.util.stream.Collectors;
  38 import java.util.stream.Stream;
  39 import java.util.zip.ZipEntry;
  40 import java.util.zip.ZipFile;
  41 
  42 /**
  43  * Helper class to read JMOD file
  44  */
  45 public class JmodFile implements AutoCloseable {
  46     // jmod magic number and version number
  47     public static final int JMOD_MAJOR_VERSION = 0x01;
  48     public static final int JMOD_MINOR_VERSION = 0x00;
  49     public static final byte[] JMOD_MAGIC_NUMBER = {
  50         0x4A, 0x4D, /* JM */
  51         JMOD_MAJOR_VERSION, JMOD_MINOR_VERSION, /* version 1.0 */
  52     };
  53 
  54     public static void checkMagic(Path file) throws IOException {
  55         try (InputStream in = Files.newInputStream(file);
  56              BufferedInputStream bis = new BufferedInputStream(in)) {
  57             // validate the header
  58             byte[] magic = new byte[4];
  59             bis.read(magic);
  60             if (magic[0] != JMOD_MAGIC_NUMBER[0] ||
  61                 magic[1] != JMOD_MAGIC_NUMBER[1]) {
  62                 throw new IOException("Invalid jmod file: " + file.toString());
  63             }
  64             if (magic[2] > JMOD_MAJOR_VERSION ||
  65                 (magic[2] == JMOD_MAJOR_VERSION && magic[3] > JMOD_MINOR_VERSION)) {
  66                 throw new IOException("Unsupported jmod version: " +
  67                     magic[2] + "." + magic[3] + " in " + file.toString());
  68             }
  69         }
  70     }
  71 
  72     /**
  73      * JMOD sections
  74      */
  75     public static enum Section {
  76         CLASSES("classes"),
  77         CONFIG("conf"),
  78         HEADER_FILES("include"),
  79         LEGAL_NOTICES("legal"),
  80         MAN_PAGES("man"),
  81         NATIVE_LIBS("native"),
  82         NATIVE_CMDS("bin");
  83 
  84         private final String jmodDir;
  85         private Section(String jmodDir) {
  86             this.jmodDir = jmodDir;
  87         }
  88 
  89         /**
  90          * Returns the directory name in the JMOD file corresponding to
  91          * this section
  92          */
  93         public String jmodDir() { return jmodDir; }
  94     }
  95 
  96     /**
  97      * JMOD file entry.
  98      *
  99      * Each entry corresponds to a ZipEntry whose name is:
 100      *   Section::jmodDir + '/' + name
 101      */
 102     public static class Entry {
 103         private final ZipEntry zipEntry;
 104         private final Section section;
 105         private final String name;
 106 
 107         private Entry(ZipEntry e) {
 108             String name = e.getName();
 109             int i = name.indexOf('/');
 110             if (i <= 1) {
 111                 throw new RuntimeException("invalid jmod entry: " + name);
 112             }
 113 
 114             this.zipEntry = e;
 115             this.section = section(name.substring(0, i));
 116             this.name = name.substring(i+1);
 117         }
 118 
 119         /**
 120          * Returns the section of this entry.
 121          */
 122         public Section section() {
 123             return section;
 124         }
 125 
 126         /**
 127          * Returns the name of this entry.
 128          */
 129         public String name() {
 130             return name;
 131         }
 132 
 133         /**
 134          * Returns the size of this entry.
 135          */
 136         public long size() {
 137             return zipEntry.getSize();
 138         }
 139 
 140         public ZipEntry zipEntry() {
 141             return zipEntry;
 142         }
 143 
 144         @Override
 145         public String toString() {
 146             return section.jmodDir() + "/" + name;
 147         }
 148 
 149         /*
 150          * A map from the jmodDir name to Section
 151          */
 152         static final Map<String, Section> NAME_TO_SECTION =
 153             Arrays.stream(Section.values())
 154                   .collect(Collectors.toMap(Section::jmodDir, Function.identity()));
 155 
 156         static Section section(String name) {
 157             if (!NAME_TO_SECTION.containsKey(name)) {
 158                 throw new IllegalArgumentException("invalid section: " + name);
 159 
 160             }
 161             return NAME_TO_SECTION.get(name);
 162         }
 163 
 164     }
 165 
 166     private final Path file;
 167     private final ZipFile zipfile;
 168 
 169     /**
 170      * Constructs a {@code JmodFile} from a given path.
 171      */
 172     public JmodFile(Path file) throws IOException {
 173         checkMagic(file);
 174         this.file = file;
 175         this.zipfile = new ZipFile(file.toFile());
 176     }
 177 
 178     /**
 179      * Returns the {@code Entry} for a resource in a JMOD file section
 180      * or {@code null} if not found.
 181      */
 182     public Entry getEntry(Section section, String name) {
 183         String entry = section.jmodDir() + "/" + name;
 184         ZipEntry ze = zipfile.getEntry(entry);
 185         return (ze != null) ? new Entry(ze) : null;
 186     }
 187 
 188     /**
 189      * Opens an {@code InputStream} for reading the named entry of the given
 190      * section in this jmod file.
 191      *
 192      * @throws IOException if the named entry is not found, or I/O error
 193      *         occurs when reading it
 194      */
 195     public InputStream getInputStream(Section section, String name)
 196         throws IOException
 197     {
 198         String entry = section.jmodDir() + "/" + name;
 199         ZipEntry e = zipfile.getEntry(entry);
 200         if (e == null) {
 201             throw new IOException(name + " not found: " + file);
 202         }
 203         return zipfile.getInputStream(e);
 204     }
 205 
 206     /**
 207      * Opens an {@code InputStream} for reading an entry in the JMOD file.
 208      *
 209      * @throws IOException if an I/O error occurs
 210      */
 211     public InputStream getInputStream(Entry entry) throws IOException {
 212         return zipfile.getInputStream(entry.zipEntry());
 213     }
 214 
 215     /**
 216      * Returns a stream of non-directory entries in this jmod file.
 217      */
 218     public Stream<Entry> stream() {
 219         return zipfile.stream()
 220                       .filter(e -> !e.isDirectory())
 221                       .map(Entry::new);
 222     }
 223 
 224     @Override
 225     public void close() throws IOException {
 226         if (zipfile != null) {
 227             zipfile.close();
 228         }
 229     }
 230 }