Copy downloaded HMCL to .hmcl only after it's verified

This commit is contained in:
yushijinhun
2018-08-01 12:32:37 +08:00
parent 38f27d134d
commit b78beb3305
2 changed files with 23 additions and 9 deletions

View File

@@ -19,11 +19,10 @@ package org.jackhuang.hmcl.upgrade;
import static org.jackhuang.hmcl.util.Logging.LOG; import static org.jackhuang.hmcl.util.Logging.LOG;
import java.io.IOException; import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Optional; import java.util.Optional;
import java.util.jar.Attributes; import java.util.jar.Attributes;
import java.util.jar.JarFile; import java.util.jar.JarFile;
@@ -63,12 +62,21 @@ final class LocalRepository {
/** /**
* Creates a task that downloads the given version to local repository. * Creates a task that downloads the given version to local repository.
*/ */
public static FileDownloadTask downloadFromRemote(RemoteVersion version) { public static FileDownloadTask downloadFromRemote(RemoteVersion version) throws IOException {
try { Path stage = Files.createTempFile("hmcl-update-", ".jar");
return new FileDownloadTask(new URL(version.getUrl()), localStorage.toFile(), version.getIntegrityCheck()); return new FileDownloadTask(new URL(version.getUrl()), stage.toFile(), version.getIntegrityCheck()) {
} catch (MalformedURLException e) { @Override
throw new UncheckedIOException(e); public void execute() throws Exception {
} try {
super.execute();
IntegrityChecker.requireVerifiedJar(stage);
Files.createDirectories(localStorage.getParent());
Files.copy(stage, localStorage, StandardCopyOption.REPLACE_EXISTING);
} finally {
Files.deleteIfExists(stage);
}
}
};
} }
/** /**

View File

@@ -162,7 +162,13 @@ public final class UpdateHandler {
public static void updateFrom(RemoteVersion version) { public static void updateFrom(RemoteVersion version) {
checkFxUserThread(); checkFxUserThread();
Task task = LocalRepository.downloadFromRemote(version); Task task;
try {
task = LocalRepository.downloadFromRemote(version);
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to create upgrade download task", e);
return;
}
TaskExecutor executor = task.executor(); TaskExecutor executor = task.executor();
Region dialog = Controllers.taskDialog(executor, i18n("message.downloading"), "", null); Region dialog = Controllers.taskDialog(executor, i18n("message.downloading"), "", null);
thread(() -> { thread(() -> {