From 555132e09646f0381ca732ab48b8579ee0fb5aa3 Mon Sep 17 00:00:00 2001 From: Scott Frederick Date: Fri, 17 Apr 2020 17:33:28 -0500 Subject: [PATCH] Fix archive attributes in Gradle plugin This commit ensures that file permissions are set on entries that the Gradle plugin adds to an archive. It also reverts the constant date and time used for added entries to a previous value to ensure a time zone offset is not applied. See gh-20927 --- .../gradle/tasks/bundling/BootZipCopyAction.java | 13 +++++++------ .../boot/gradle/tasks/bundling/BootJarTests.java | 4 ++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java index 99bd28bfbb..b104e39312 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java @@ -22,9 +22,9 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; -import java.time.OffsetDateTime; -import java.time.ZoneOffset; +import java.util.Calendar; import java.util.Collection; +import java.util.GregorianCalendar; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -63,11 +63,12 @@ import org.springframework.util.StringUtils; * * @author Andy Wilkinson * @author Phillip Webb + * @author Scott Frederick */ class BootZipCopyAction implements CopyAction { - static final long CONSTANT_TIME_FOR_ZIP_ENTRIES = OffsetDateTime.of(1980, 2, 1, 0, 0, 0, 0, ZoneOffset.UTC) - .toInstant().toEpochMilli(); + static final long CONSTANT_TIME_FOR_ZIP_ENTRIES = new GregorianCalendar(1980, Calendar.FEBRUARY, 1, 0, 0, 0) + .getTimeInMillis(); private final File output; @@ -261,7 +262,7 @@ class BootZipCopyAction implements CopyAction { if (parentDirectory != null && this.writtenDirectories.add(parentDirectory)) { writeParentDirectoriesIfNecessary(parentDirectory, time); ZipArchiveEntry entry = new ZipArchiveEntry(parentDirectory + '/'); - entry.setUnixMode(UnixStat.DIR_FLAG); + entry.setUnixMode(UnixStat.DIR_FLAG | UnixStat.DEFAULT_DIR_PERM); entry.setTime(time); this.out.putArchiveEntry(entry); this.out.closeArchiveEntry(); @@ -372,7 +373,7 @@ class BootZipCopyAction implements CopyAction { ZipEntryCustomizer entryCustomizer) throws IOException { writeParentDirectoriesIfNecessary(name, CONSTANT_TIME_FOR_ZIP_ENTRIES); ZipArchiveEntry entry = new ZipArchiveEntry(name); - entry.setUnixMode(UnixStat.FILE_FLAG); + entry.setUnixMode(UnixStat.FILE_FLAG | UnixStat.DEFAULT_FILE_PERM); entry.setTime(CONSTANT_TIME_FOR_ZIP_ENTRIES); entryCustomizer.customize(entry); this.out.putArchiveEntry(entry); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootJarTests.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootJarTests.java index 60a8b841c7..4aa69f83eb 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootJarTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootJarTests.java @@ -101,10 +101,14 @@ class BootJarTests extends AbstractBootArchiveTests { assertThat(entryNames).contains("BOOT-INF/lib/first-library.jar", "BOOT-INF/lib/second-library.jar", "BOOT-INF/lib/third-library-SNAPSHOT.jar", "BOOT-INF/classes/com/example/Application.class", "BOOT-INF/classes/application.properties", "BOOT-INF/classes/static/test.css"); + ZipEntry layersIndexEntry = jarFile.getEntry("BOOT-INF/layers.idx"); + assertThat(layersIndexEntry.getTime()).isEqualTo(BootZipCopyAction.CONSTANT_TIME_FOR_ZIP_ENTRIES); List index = entryLines(jarFile, "BOOT-INF/layers.idx"); assertThat(getLayerNames(index)).containsExactly("dependencies", "spring-boot-loader", "snapshot-dependencies", "application"); String layerToolsJar = "BOOT-INF/lib/" + JarModeLibrary.LAYER_TOOLS.getName(); + ZipEntry layerToolsEntry = jarFile.getEntry(layerToolsJar); + assertThat(layerToolsEntry.getTime()).isEqualTo(BootZipCopyAction.CONSTANT_TIME_FOR_ZIP_ENTRIES); List expected = new ArrayList<>(); expected.add("- \"dependencies\":"); expected.add(" - \"BOOT-INF/lib/first-library.jar\"");