From 780efe2c9fea0f42b561c4594bb68e6766ed38f8 Mon Sep 17 00:00:00 2001 From: comp500 Date: Tue, 2 Jul 2019 23:32:00 +0100 Subject: [PATCH] Remove files correctly, redownload when deleted --- .../packwiz/installer/UpdateManager.java | 33 +++++++++++++++++++ .../installer/metadata/ManifestFile.java | 1 + 2 files changed, 34 insertions(+) diff --git a/src/main/java/link/infra/packwiz/installer/UpdateManager.java b/src/main/java/link/infra/packwiz/installer/UpdateManager.java index ac3a6a4..e03a259 100644 --- a/src/main/java/link/infra/packwiz/installer/UpdateManager.java +++ b/src/main/java/link/infra/packwiz/installer/UpdateManager.java @@ -7,10 +7,14 @@ import java.io.IOException; import java.io.Writer; import java.net.URI; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; +import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.concurrent.Callable; import java.util.concurrent.CompletionService; import java.util.concurrent.ConcurrentLinkedQueue; @@ -207,6 +211,31 @@ public class UpdateManager { manifest.cachedFiles = new HashMap(); } + ui.submitProgress(new InstallProgress("Checking local files...")); + List invalidatedUris = new ArrayList<>(); + Iterator> it = manifest.cachedFiles.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry entry = it.next(); + if (entry.getValue().cachedLocation != null) { + Path filePath = Paths.get(opts.packFolder, entry.getValue().cachedLocation); + URI fileUri = entry.getKey(); + + if (!indexFile.files.stream().anyMatch(f -> f.file.equals(fileUri))) { + // File has been removed from the index + try { + Files.deleteIfExists(filePath); + } catch (IOException e) { + // TODO: should this be shown to the user in some way? + e.printStackTrace(); + } + it.remove(); + } else if (!Files.exists(filePath)) { + invalidatedUris.add(fileUri); + } + } + } + ui.submitProgress(new InstallProgress("Comparing new files...")); + // TODO: progress bar ConcurrentLinkedQueue exceptionQueue = new ConcurrentLinkedQueue(); List newFiles = indexFile.files.stream().map(f -> { @@ -215,6 +244,9 @@ public class UpdateManager { } return f; }).filter(f -> { + if (invalidatedUris.contains(f.file)) { + return true; + } ManifestFile.File cachedFile = manifest.cachedFiles.get(f.file); Hash newHash; try { @@ -343,6 +375,7 @@ public class UpdateManager { ret.err = e; } } + newCachedFile.cachedLocation = ret.file.getDestURI().toString(); manifest.cachedFiles.put(ret.file.file, newCachedFile); } // TODO: show errors properly? diff --git a/src/main/java/link/infra/packwiz/installer/metadata/ManifestFile.java b/src/main/java/link/infra/packwiz/installer/metadata/ManifestFile.java index ecf5c65..1107d03 100644 --- a/src/main/java/link/infra/packwiz/installer/metadata/ManifestFile.java +++ b/src/main/java/link/infra/packwiz/installer/metadata/ManifestFile.java @@ -16,5 +16,6 @@ public class ManifestFile { public boolean isOptional = false; public boolean optionValue = true; public Hash linkedFileHash = null; + public String cachedLocation = null; } } \ No newline at end of file