Fix URI parsing, hash checking, NPEs, create dirs

This commit is contained in:
comp500 2019-06-24 02:09:19 +01:00
parent 040bb955ec
commit d986b39aa5
No known key found for this signature in database
GPG Key ID: 214C822FFEC586B5
5 changed files with 67 additions and 7 deletions

View File

@ -9,6 +9,7 @@ import java.net.URI;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService; import java.util.concurrent.CompletionService;
@ -197,9 +198,18 @@ public class UpdateManager {
// TODO: throw exception // TODO: throw exception
} }
if (manifest.cachedFiles == null) {
manifest.cachedFiles = new HashMap<URI, ManifestFile.File>();
}
// TODO: progress bar // TODO: progress bar
ConcurrentLinkedQueue<Exception> exceptionQueue = new ConcurrentLinkedQueue<Exception>(); ConcurrentLinkedQueue<Exception> exceptionQueue = new ConcurrentLinkedQueue<Exception>();
List<IndexFile.File> newFiles = indexFile.files.stream().filter(f -> { List<IndexFile.File> newFiles = indexFile.files.stream().map(f -> {
if (f.hashFormat == null || f.hashFormat.length() == 0) {
f.hashFormat = indexFile.hashFormat;
}
return f;
}).filter(f -> {
ManifestFile.File cachedFile = manifest.cachedFiles.get(f.file); ManifestFile.File cachedFile = manifest.cachedFiles.get(f.file);
Object newHash; Object newHash;
try { try {
@ -208,7 +218,7 @@ public class UpdateManager {
exceptionQueue.add(e); exceptionQueue.add(e);
return false; return false;
} }
return cachedFile == null || newHash.equals(cachedFile.hash); return cachedFile == null || !newHash.equals(cachedFile.hash);
}).parallel().map(f -> { }).parallel().map(f -> {
try { try {
f.downloadMeta(indexFile, indexUri); f.downloadMeta(indexFile, indexUri);
@ -244,7 +254,7 @@ public class UpdateManager {
DownloadCompletion dc = new DownloadCompletion(); DownloadCompletion dc = new DownloadCompletion();
dc.file = f; dc.file = f;
if (cachedFile.linkedFileHash != null && f.linkedFile != null) { if (cachedFile != null && cachedFile.linkedFileHash != null && f.linkedFile != null) {
try { try {
if (cachedFile.linkedFileHash.equals(f.linkedFile.getHash())) { if (cachedFile.linkedFileHash.equals(f.linkedFile.getHash())) {
// Do nothing, the file didn't change // Do nothing, the file didn't change
@ -268,8 +278,12 @@ public class UpdateManager {
hash = f.getHash(); hash = f.getHash();
} }
if (fileSource.hashIsEqual(hash)) { if (fileSource.hashIsEqual(hash)) {
Files.createDirectories(Paths.get(opts.packFolder, f.getDestURI().toString()).getParent());
Files.copy(data.inputStream(), Paths.get(opts.packFolder, f.getDestURI().toString()), StandardCopyOption.REPLACE_EXISTING); Files.copy(data.inputStream(), Paths.get(opts.packFolder, f.getDestURI().toString()), StandardCopyOption.REPLACE_EXISTING);
} else { } else {
System.out.println("Invalid hash for " + f.getDestURI().toString());
System.out.println("Calculated: " + fileSource.getHash());
System.out.println("Expected: " + hash);
dc.err = new Exception("Hash invalid!"); dc.err = new Exception("Hash invalid!");
} }
@ -313,13 +327,22 @@ public class UpdateManager {
ret.err = e; ret.err = e;
} }
} }
manifest.cachedFiles.put(ret.file.file, newCachedFile);
} }
// TODO: show errors properly? // TODO: show errors properly?
String progress; String progress;
if (ret != null && ret.file != null) { if (ret != null) {
progress = "Downloaded " + ret.file.getName(); if (ret.err != null) {
} else if (ret.err != null) { if (ret.file != null) {
progress = "Failed to download: " + ret.err.getMessage(); progress = "Failed to download " + ret.file.getName() + ": " + ret.err.getMessage();
} else {
progress = "Failed to download: " + ret.err.getMessage();
}
} else if (ret.file != null) {
progress = "Downloaded " + ret.file.getName();
} else {
progress = "Failed to download, unknown reason";
}
} else { } else {
progress = "Failed to download, unknown reason"; progress = "Failed to download, unknown reason";
} }

View File

@ -4,6 +4,7 @@ import java.net.URI;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.List; import java.util.List;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import com.moandjiezana.toml.Toml; import com.moandjiezana.toml.Toml;
@ -19,6 +20,7 @@ public class IndexFile {
public List<File> files; public List<File> files;
public static class File { public static class File {
@JsonAdapter(SpaceSafeURIParser.class)
public URI file; public URI file;
@SerializedName("hash-format") @SerializedName("hash-format")
public String hashFormat; public String hashFormat;

View File

@ -3,6 +3,7 @@ package link.infra.packwiz.installer.metadata;
import java.net.URI; import java.net.URI;
import java.util.Map; import java.util.Map;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import link.infra.packwiz.installer.UpdateManager.Options.Side; import link.infra.packwiz.installer.UpdateManager.Options.Side;
@ -17,6 +18,7 @@ public class ModFile {
public Download download; public Download download;
public static class Download { public static class Download {
@JsonAdapter(SpaceSafeURIParser.class)
public URI url; public URI url;
@SerializedName("hash-format") @SerializedName("hash-format")
public String hashFormat; public String hashFormat;

View File

@ -3,6 +3,7 @@ package link.infra.packwiz.installer.metadata;
import java.net.URI; import java.net.URI;
import java.util.Map; import java.util.Map;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
public class PackFile { public class PackFile {
@ -10,6 +11,7 @@ public class PackFile {
public IndexFileLoc index; public IndexFileLoc index;
public static class IndexFileLoc { public static class IndexFileLoc {
@JsonAdapter(SpaceSafeURIParser.class)
public URI file; public URI file;
@SerializedName("hash-format") @SerializedName("hash-format")
public String hashFormat; public String hashFormat;

View File

@ -0,0 +1,31 @@
package link.infra.packwiz.installer.metadata;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URISyntaxException;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
/**
* This class encodes spaces before parsing the URI, so the URI can actually be
* parsed.
*/
class SpaceSafeURIParser implements JsonDeserializer<URI> {
@Override
public URI deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
String uriString = json.getAsString().replace(" ", "%20");
try {
return new URI(uriString);
} catch (URISyntaxException e) {
throw new JsonParseException("Failed to parse URI", e);
}
}
// TODO: replace this with a better solution?
}