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
pull/21007/head
Scott Frederick 5 years ago
parent dc56608651
commit 555132e096

@ -22,9 +22,9 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.time.OffsetDateTime; import java.util.Calendar;
import java.time.ZoneOffset;
import java.util.Collection; import java.util.Collection;
import java.util.GregorianCalendar;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -63,11 +63,12 @@ import org.springframework.util.StringUtils;
* *
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Phillip Webb * @author Phillip Webb
* @author Scott Frederick
*/ */
class BootZipCopyAction implements CopyAction { class BootZipCopyAction implements CopyAction {
static final long CONSTANT_TIME_FOR_ZIP_ENTRIES = OffsetDateTime.of(1980, 2, 1, 0, 0, 0, 0, ZoneOffset.UTC) static final long CONSTANT_TIME_FOR_ZIP_ENTRIES = new GregorianCalendar(1980, Calendar.FEBRUARY, 1, 0, 0, 0)
.toInstant().toEpochMilli(); .getTimeInMillis();
private final File output; private final File output;
@ -261,7 +262,7 @@ class BootZipCopyAction implements CopyAction {
if (parentDirectory != null && this.writtenDirectories.add(parentDirectory)) { if (parentDirectory != null && this.writtenDirectories.add(parentDirectory)) {
writeParentDirectoriesIfNecessary(parentDirectory, time); writeParentDirectoriesIfNecessary(parentDirectory, time);
ZipArchiveEntry entry = new ZipArchiveEntry(parentDirectory + '/'); ZipArchiveEntry entry = new ZipArchiveEntry(parentDirectory + '/');
entry.setUnixMode(UnixStat.DIR_FLAG); entry.setUnixMode(UnixStat.DIR_FLAG | UnixStat.DEFAULT_DIR_PERM);
entry.setTime(time); entry.setTime(time);
this.out.putArchiveEntry(entry); this.out.putArchiveEntry(entry);
this.out.closeArchiveEntry(); this.out.closeArchiveEntry();
@ -372,7 +373,7 @@ class BootZipCopyAction implements CopyAction {
ZipEntryCustomizer entryCustomizer) throws IOException { ZipEntryCustomizer entryCustomizer) throws IOException {
writeParentDirectoriesIfNecessary(name, CONSTANT_TIME_FOR_ZIP_ENTRIES); writeParentDirectoriesIfNecessary(name, CONSTANT_TIME_FOR_ZIP_ENTRIES);
ZipArchiveEntry entry = new ZipArchiveEntry(name); 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); entry.setTime(CONSTANT_TIME_FOR_ZIP_ENTRIES);
entryCustomizer.customize(entry); entryCustomizer.customize(entry);
this.out.putArchiveEntry(entry); this.out.putArchiveEntry(entry);

@ -101,10 +101,14 @@ class BootJarTests extends AbstractBootArchiveTests<TestBootJar> {
assertThat(entryNames).contains("BOOT-INF/lib/first-library.jar", "BOOT-INF/lib/second-library.jar", 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/lib/third-library-SNAPSHOT.jar", "BOOT-INF/classes/com/example/Application.class",
"BOOT-INF/classes/application.properties", "BOOT-INF/classes/static/test.css"); "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<String> index = entryLines(jarFile, "BOOT-INF/layers.idx"); List<String> index = entryLines(jarFile, "BOOT-INF/layers.idx");
assertThat(getLayerNames(index)).containsExactly("dependencies", "spring-boot-loader", assertThat(getLayerNames(index)).containsExactly("dependencies", "spring-boot-loader",
"snapshot-dependencies", "application"); "snapshot-dependencies", "application");
String layerToolsJar = "BOOT-INF/lib/" + JarModeLibrary.LAYER_TOOLS.getName(); 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<String> expected = new ArrayList<>(); List<String> expected = new ArrayList<>();
expected.add("- \"dependencies\":"); expected.add("- \"dependencies\":");
expected.add(" - \"BOOT-INF/lib/first-library.jar\""); expected.add(" - \"BOOT-INF/lib/first-library.jar\"");

Loading…
Cancel
Save