# HG changeset patch # User simonis # Date 1583232348 -3600 # Tue Mar 03 11:45:48 2020 +0100 # Node ID 00cdeefd66a1f21f815ea27feb49e088ee5d46e5 # Parent 1bc5f223df657bf25485c45cac5d57e892894075 8240333: jmod incorrectly updates .jar and .jmod files during hashing diff --git a/src/jdk.jlink/share/classes/jdk/tools/jmod/JmodOutputStream.java b/src/jdk.jlink/share/classes/jdk/tools/jmod/JmodOutputStream.java --- a/src/jdk.jlink/share/classes/jdk/tools/jmod/JmodOutputStream.java +++ b/src/jdk.jlink/share/classes/jdk/tools/jmod/JmodOutputStream.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -92,7 +92,22 @@ * Writes the given entry to the given input stream. */ public void writeEntry(InputStream in, Entry e) throws IOException { - zos.putNextEntry(e.zipEntry()); + ZipEntry e1 = e.zipEntry(); + // We can't know which exact Deflater and which + // compression level was used to compress the entry. + // Only preserve attributes which won't change by + // inflating and deflating the entry. See: + // sun.tools.jar.Main.update() + ZipEntry e2 = new ZipEntry(e1.getName()); + e2.setMethod(e1.getMethod()); + e2.setTime(e1.getTime()); + e2.setComment(e1.getComment()); + e2.setExtra(e1.getExtra()); + if (e1.getMethod() == ZipEntry.STORED) { + e2.setSize(e1.getSize()); + e2.setCrc(e1.getCrc()); + } + zos.putNextEntry(e2); zos.write(in.readAllBytes()); zos.closeEntry(); } diff --git a/src/jdk.jlink/share/classes/jdk/tools/jmod/JmodTask.java b/src/jdk.jlink/share/classes/jdk/tools/jmod/JmodTask.java --- a/src/jdk.jlink/share/classes/jdk/tools/jmod/JmodTask.java +++ b/src/jdk.jlink/share/classes/jdk/tools/jmod/JmodTask.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -987,6 +987,14 @@ recordHashes(in, jos, moduleHashes); jos.closeEntry(); } else { + // We can't know which exact Deflater and which + // compression level was used to compress the entry. + // Setting "compressedSize" to "-1" prevents an error + // in ZipOutputStream.closeEntry() if the newly + // delfated entry will have another size than the + // original compressed entry. See: + // ZipOutputStream.putNextEntry()/closeEntry() + e.setCompressedSize(-1); jos.putNextEntry(e); jos.write(in.readAllBytes()); jos.closeEntry();