From 02b50be7828496683e3619755aeffe391e829d8c Mon Sep 17 00:00:00 2001
From: comp500 <comp500@users.noreply.github.com>
Date: Sat, 10 Aug 2019 19:43:41 +0100
Subject: [PATCH] Ensure files are removed when they need to be

---
 .../infra/packwiz/installer/DownloadTask.java | 25 ++++++++++++++-----
 1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/src/main/java/link/infra/packwiz/installer/DownloadTask.java b/src/main/java/link/infra/packwiz/installer/DownloadTask.java
index b4ddc18..22e40a8 100644
--- a/src/main/java/link/infra/packwiz/installer/DownloadTask.java
+++ b/src/main/java/link/infra/packwiz/installer/DownloadTask.java
@@ -10,6 +10,7 @@ import okio.Buffer;
 import okio.Okio;
 import okio.Source;
 
+import java.io.IOException;
 import java.net.URI;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -101,8 +102,21 @@ class DownloadTask implements IOptionDetails {
 
 	public void download(String packFolder, URI indexUri) {
 		if (failure != null) return;
+
+		// Ensure it is removed
+		if (!cachedFile.optionValue || !correctSide()) {
+			if (cachedFile.cachedLocation == null) return;
+			try {
+				Files.deleteIfExists(Paths.get(packFolder, cachedFile.cachedLocation));
+			} catch (IOException e) {
+				// TODO: how much of a problem is this? use log4j/other log library to show warning?
+				e.printStackTrace();
+			}
+			cachedFile.cachedLocation = null;
+			return;
+		}
+
 		if (alreadyUpToDate) return;
-		if (!correctSide()) return;
 
 		Path destPath = Paths.get(packFolder, metadata.getDestURI().toString());
 
@@ -198,7 +212,7 @@ class DownloadTask implements IOptionDetails {
 
 	@Override
 	public boolean getOptionValue() {
-		return this.cachedFile.optionValue;
+		return cachedFile.optionValue;
 	}
 
 	@Override
@@ -210,12 +224,11 @@ class DownloadTask implements IOptionDetails {
 	}
 
 	public void setOptionValue(boolean value) {
-		// TODO: if this is false, ensure the file is deleted in the actual download stage (regardless of alreadyUpToDate?)
-		if (value && !this.cachedFile.optionValue) {
-			// Ensure that an update is done if it changes from false to true
+		if (value && !cachedFile.optionValue) {
+			// Ensure that an update is done if it changes from false to true, or from true to false
 			alreadyUpToDate = false;
 		}
-		this.cachedFile.optionValue = value;
+		cachedFile.optionValue = value;
 	}
 
 	public static List<DownloadTask> createTasksFromIndex(IndexFile index, String defaultFormat, UpdateManager.Options.Side downloadSide) {