Skip to content

Commit

Permalink
Remove files correctly, redownload when deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
comp500 committed Jul 2, 2019
1 parent d164776 commit 780efe2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/main/java/link/infra/packwiz/installer/UpdateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
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;
Expand Down Expand Up @@ -207,6 +211,31 @@ protected void processIndex(URI indexUri, Hash indexHash, String hashFormat, Man
manifest.cachedFiles = new HashMap<URI, ManifestFile.File>();
}

ui.submitProgress(new InstallProgress("Checking local files..."));
List<URI> invalidatedUris = new ArrayList<>();
Iterator<Map.Entry<URI, ManifestFile.File>> it = manifest.cachedFiles.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<URI, ManifestFile.File> 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<Exception> exceptionQueue = new ConcurrentLinkedQueue<Exception>();
List<IndexFile.File> newFiles = indexFile.files.stream().map(f -> {
Expand All @@ -215,6 +244,9 @@ protected void processIndex(URI indexUri, Hash indexHash, String hashFormat, Man
}
return f;
}).filter(f -> {
if (invalidatedUris.contains(f.file)) {
return true;
}
ManifestFile.File cachedFile = manifest.cachedFiles.get(f.file);
Hash newHash;
try {
Expand Down Expand Up @@ -343,6 +375,7 @@ public DownloadCompletion call() {
ret.err = e;
}
}
newCachedFile.cachedLocation = ret.file.getDestURI().toString();
manifest.cachedFiles.put(ret.file.file, newCachedFile);
}
// TODO: show errors properly?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ public static class File {
public boolean isOptional = false;
public boolean optionValue = true;
public Hash linkedFileHash = null;
public String cachedLocation = null;
}
}

0 comments on commit 780efe2

Please sign in to comment.