< prev index next >

src/jdk.jlink/share/classes/jdk/tools/jmod/JmodTask.java

Print this page
rev 58246 : 8240333: jmod incorrectly updates .jar and .jmod files during hashing
   1 /*
   2  * Copyright (c) 2015, 2018, 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


 970         }
 971 
 972         private void updateModularJar(Path target, Path tempTarget,
 973                                       ModuleHashes moduleHashes)
 974             throws IOException
 975         {
 976             try (JarFile jf = new JarFile(target.toFile());
 977                  OutputStream out = Files.newOutputStream(tempTarget);
 978                  JarOutputStream jos = new JarOutputStream(out))
 979             {
 980                 jf.stream().forEach(e -> {
 981                     try (InputStream in = jf.getInputStream(e)) {
 982                         if (e.getName().equals(MODULE_INFO)) {
 983                             // what about module-info.class in versioned entries?
 984                             ZipEntry ze = new ZipEntry(e.getName());
 985                             ze.setTime(System.currentTimeMillis());
 986                             jos.putNextEntry(ze);
 987                             recordHashes(in, jos, moduleHashes);
 988                             jos.closeEntry();
 989                         } else {








 990                             jos.putNextEntry(e);
 991                             jos.write(in.readAllBytes());
 992                             jos.closeEntry();
 993                         }
 994                     } catch (IOException x) {
 995                         throw new UncheckedIOException(x);
 996                     }
 997                 });
 998             }
 999         }
1000 
1001         private void updateJmodFile(Path target, Path tempTarget,
1002                                     ModuleHashes moduleHashes)
1003             throws IOException
1004         {
1005 
1006             try (JmodFile jf = new JmodFile(target);
1007                  JmodOutputStream jos = JmodOutputStream.newOutputStream(tempTarget))
1008             {
1009                 jf.stream().forEach(e -> {


   1 /*
   2  * Copyright (c) 2015, 2020, 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


 970         }
 971 
 972         private void updateModularJar(Path target, Path tempTarget,
 973                                       ModuleHashes moduleHashes)
 974             throws IOException
 975         {
 976             try (JarFile jf = new JarFile(target.toFile());
 977                  OutputStream out = Files.newOutputStream(tempTarget);
 978                  JarOutputStream jos = new JarOutputStream(out))
 979             {
 980                 jf.stream().forEach(e -> {
 981                     try (InputStream in = jf.getInputStream(e)) {
 982                         if (e.getName().equals(MODULE_INFO)) {
 983                             // what about module-info.class in versioned entries?
 984                             ZipEntry ze = new ZipEntry(e.getName());
 985                             ze.setTime(System.currentTimeMillis());
 986                             jos.putNextEntry(ze);
 987                             recordHashes(in, jos, moduleHashes);
 988                             jos.closeEntry();
 989                         } else {
 990                             // We can't know which exact Deflater and which
 991                             // compression level was used to compress the entry.
 992                             // Setting "compressedSize" to "-1" prevents an error
 993                             // in ZipOutputStream.closeEntry() if the newly
 994                             // delfated entry will have another size than the
 995                             // original compressed entry. See:
 996                             // ZipOutputStream.putNextEntry()/closeEntry()
 997                             e.setCompressedSize(-1);
 998                             jos.putNextEntry(e);
 999                             jos.write(in.readAllBytes());
1000                             jos.closeEntry();
1001                         }
1002                     } catch (IOException x) {
1003                         throw new UncheckedIOException(x);
1004                     }
1005                 });
1006             }
1007         }
1008 
1009         private void updateJmodFile(Path target, Path tempTarget,
1010                                     ModuleHashes moduleHashes)
1011             throws IOException
1012         {
1013 
1014             try (JmodFile jf = new JmodFile(target);
1015                  JmodOutputStream jos = JmodOutputStream.newOutputStream(tempTarget))
1016             {
1017                 jf.stream().forEach(e -> {


< prev index next >