Simplify pack validation and merge response mixins
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package online.astradev.packs;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -16,9 +19,6 @@ import java.util.HexFormat;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
/** Archives the shared content directory, keeping the current and one previous ZIP. */
|
||||
public final class PackBuilder {
|
||||
public record Pack(Path path, String sha1, int files, long bytes) {}
|
||||
@@ -71,19 +71,29 @@ public final class PackBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
private int writeArchive(Path contents, Path target, String resourceVersion) throws IOException {
|
||||
// Shared with server-side exporters (AstraPlus). A build must see one complete asset generation.
|
||||
String lock = "AstraPacks:contents:" + contents.toAbsolutePath().normalize().toString().toLowerCase(java.util.Locale.ROOT);
|
||||
private int writeArchive(Path contents, Path target, String resourceVersion)
|
||||
throws IOException {
|
||||
// Shared with server-side exporters (AstraPlus). A build must see one complete asset
|
||||
// generation.
|
||||
String lock =
|
||||
"AstraPacks:contents:"
|
||||
+ contents.toAbsolutePath()
|
||||
.normalize()
|
||||
.toString()
|
||||
.toLowerCase(java.util.Locale.ROOT);
|
||||
synchronized (lock.intern()) {
|
||||
return writeLockedArchive(contents, target, resourceVersion);
|
||||
}
|
||||
}
|
||||
|
||||
private int writeLockedArchive(Path contents, Path target, String resourceVersion) throws IOException {
|
||||
private int writeLockedArchive(Path contents, Path target, String resourceVersion)
|
||||
throws IOException {
|
||||
int files = 0;
|
||||
long totalBytes = 0;
|
||||
try (var paths = Files.walk(contents);
|
||||
var zip = new ZipOutputStream(new BufferedOutputStream(Files.newOutputStream(target)))) {
|
||||
var zip =
|
||||
new ZipOutputStream(
|
||||
new BufferedOutputStream(Files.newOutputStream(target)))) {
|
||||
for (Path file : paths.sorted().toList()) {
|
||||
if (Files.isSymbolicLink(file)) {
|
||||
throw new IOException("Symbolic links are not supported: " + file);
|
||||
@@ -105,8 +115,10 @@ public final class PackBuilder {
|
||||
ZipEntry entry = new ZipEntry(name);
|
||||
entry.setTime(0);
|
||||
zip.putNextEntry(entry);
|
||||
long limit = Math.min(config.build.maxFileMiB * 1024L * 1024L,
|
||||
config.build.maxTotalMiB * 1024L * 1024L - totalBytes);
|
||||
long limit =
|
||||
Math.min(
|
||||
config.build.maxFileMiB * 1024L * 1024L,
|
||||
config.build.maxTotalMiB * 1024L * 1024L - totalBytes);
|
||||
totalBytes += copyResource(file, zip, limit);
|
||||
zip.closeEntry();
|
||||
}
|
||||
@@ -119,7 +131,8 @@ public final class PackBuilder {
|
||||
return files + 1;
|
||||
}
|
||||
|
||||
private static long copyResource(Path file, OutputStream output, long limit) throws IOException {
|
||||
private static long copyResource(Path file, OutputStream output, long limit)
|
||||
throws IOException {
|
||||
byte[] buffer = new byte[8192];
|
||||
long size = 0;
|
||||
try (var input = Files.newInputStream(file)) {
|
||||
@@ -168,14 +181,14 @@ public final class PackBuilder {
|
||||
try (var paths = Files.list(output)) {
|
||||
for (Path path : paths.toList()) {
|
||||
String name = path.getFileName().toString();
|
||||
if (name.matches("[a-f0-9]{40}\\.zip") && !name.equals(pack.sha1() + ".zip")
|
||||
&& (previous == null || !name.equals(previous + ".zip"))
|
||||
&& Files.isRegularFile(path, LinkOption.NOFOLLOW_LINKS)) {
|
||||
Files.delete(path);
|
||||
}
|
||||
if (!name.matches("[a-f0-9]{40}\\.zip")) continue;
|
||||
if (name.equals(pack.sha1() + ".zip")) continue;
|
||||
if (previous != null && name.equals(previous + ".zip")) continue;
|
||||
if (Files.isRegularFile(path, LinkOption.NOFOLLOW_LINKS)) Files.delete(path);
|
||||
}
|
||||
}
|
||||
PackFiles.atomicWrite(manifest, PackConfig.JSON.toJson(info).getBytes(StandardCharsets.UTF_8));
|
||||
PackFiles.atomicWrite(
|
||||
manifest, PackConfig.JSON.toJson(info).getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
public static Pack latest(Path root, PackConfig config) throws IOException {
|
||||
@@ -192,11 +205,16 @@ public final class PackBuilder {
|
||||
private static JsonObject readManifest(Path path) throws IOException {
|
||||
try {
|
||||
JsonObject info = JsonParser.parseString(Files.readString(path)).getAsJsonObject();
|
||||
if (!info.get("sha1").getAsString().matches("[a-f0-9]{40}")
|
||||
|| (info.has("previousSha1") && !info.get("previousSha1").getAsString().matches("[a-f0-9]{40}"))
|
||||
|| info.get("files").getAsInt() < 1) {
|
||||
throw new IOException("Invalid resource pack manifest: " + path);
|
||||
String current = info.get("sha1").getAsString();
|
||||
if (!current.matches("[a-f0-9]{40}"))
|
||||
throw new IOException("Invalid current pack hash: " + path);
|
||||
if (info.has("previousSha1")) {
|
||||
String previous = info.get("previousSha1").getAsString();
|
||||
if (!previous.matches("[a-f0-9]{40}"))
|
||||
throw new IOException("Invalid previous pack hash: " + path);
|
||||
}
|
||||
if (info.get("files").getAsInt() < 1)
|
||||
throw new IOException("Empty resource pack manifest: " + path);
|
||||
return info;
|
||||
} catch (RuntimeException e) {
|
||||
throw new IOException("Invalid resource pack manifest: " + path, e);
|
||||
|
||||
Reference in New Issue
Block a user