Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reproducable zips across timezones. #952

Merged
merged 4 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions src/main/java/net/fabricmc/loom/util/ZipReprocessorUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.util.Calendar;
import java.util.Comparator;
import java.util.GregorianCalendar;
Expand All @@ -42,11 +41,6 @@
import org.intellij.lang.annotations.MagicConstant;

public class ZipReprocessorUtil {
/**
* See {@link org.gradle.api.internal.file.archive.ZipCopyAction} about this.
*/
private static final long CONSTANT_TIME_FOR_ZIP_ENTRIES = new GregorianCalendar(1980, Calendar.FEBRUARY, 1, 0, 0, 0).getTimeInMillis();

private ZipReprocessorUtil() { }

private static final String MANIFEST_LOCATION = "META-INF/MANIFEST.MF";
Expand Down Expand Up @@ -183,9 +177,8 @@ private static void copyZipEntry(ZipOutputStream zipOutputStream, ZipEntry entry
}

private static void setConstantFileTime(ZipEntry entry) {
entry.setTime(ZipReprocessorUtil.CONSTANT_TIME_FOR_ZIP_ENTRIES);
entry.setLastModifiedTime(FileTime.fromMillis(ZipReprocessorUtil.CONSTANT_TIME_FOR_ZIP_ENTRIES));
entry.setLastAccessTime(FileTime.fromMillis(ZipReprocessorUtil.CONSTANT_TIME_FOR_ZIP_ENTRIES));
// See https://github.com/openjdk/jdk/blob/master/test/jdk/java/util/zip/ZipFile/ZipEntryTimeBounds.java
entry.setTime(new GregorianCalendar(1980, Calendar.JANUARY, 1, 0, 0, 0).getTimeInMillis());
}

@MagicConstant(valuesFromClass = ZipOutputStream.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ class ReproducibleBuildTest extends Specification implements GradleProjectTestTr

where:
version | modHash | sourceHash
DEFAULT_GRADLE | "174c9b52f4bc6d489548d11b42e853cf" | [
"5e6e56df303b4fbaaef372d6f143dbfc",
"92b6fbffd0bd14bf3c626750eb86c264"
DEFAULT_GRADLE | "97240b42385adfaa1952e9c4ea942f71" | [
"61438feb9bd548788bbc637637d202fc",
"185ad8396d89b726064682bf22572036"
]
PRE_RELEASE_GRADLE | "174c9b52f4bc6d489548d11b42e853cf" | [
"5e6e56df303b4fbaaef372d6f143dbfc",
"92b6fbffd0bd14bf3c626750eb86c264"
PRE_RELEASE_GRADLE | "97240b42385adfaa1952e9c4ea942f71" | [
"61438feb9bd548788bbc637637d202fc",
"185ad8396d89b726064682bf22572036"
]
}

Expand Down
18 changes: 17 additions & 1 deletion src/test/groovy/net/fabricmc/loom/test/unit/ZipUtilsTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package net.fabricmc.loom.test.unit

import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.time.ZoneId

import spock.lang.Specification

Expand Down Expand Up @@ -155,6 +156,9 @@ class ZipUtilsTest extends Specification {

def "append zip entry"() {
given:
def currentTimezone = TimeZone.getDefault()
TimeZone.setDefault(TimeZone.getTimeZone(ZoneId.of(timezone)))

// Create a reproducible input zip
def dir = Files.createTempDirectory("loom-zip-test")
def zip = Files.createTempFile("loom-zip-test", ".zip")
Expand All @@ -167,9 +171,21 @@ class ZipUtilsTest extends Specification {
// Add an entry to it
ZipReprocessorUtil.appendZipEntry(zip.toFile(), "fabric.mod.json", "Some text".getBytes(StandardCharsets.UTF_8))

// Reset the timezone back
TimeZone.setDefault(currentTimezone)

then:
ZipUtils.unpack(zip, "text.txt") == "hello world".bytes
ZipUtils.unpack(zip, "fabric.mod.json") == "Some text".bytes
Checksum.sha1Hex(zip) == "232ecda4c770bde8ba618e7a194a4f7b57928dc5"
Checksum.sha1Hex(zip) == "1b06cc0aaa65ab2b0d423fe33431ff5bd14bf9c8"

where:
timezone | _
"UTC" | _
"US/Central" | _
"Europe/London" | _
"Australia/Sydney" | _
"Etc/GMT-6" | _
"Etc/GMT+9" | _
}
}
Loading