Compare commits

..

2 Commits

Author SHA1 Message Date
comp500
34a86ffb7d Handle renamed files correctly, log invalidation 2019-07-03 22:49:08 +01:00
comp500
780efe2c9f Remove files correctly, redownload when deleted 2019-07-02 23:32:00 +01:00
2 changed files with 45 additions and 3 deletions

View File

@@ -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,32 @@ public class UpdateManager {
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)) {
System.out.println("File " + fileUri.toString() + " invalidated, marked for redownloading");
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 -> {
@@ -215,6 +245,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 {
@@ -270,9 +303,11 @@ public class UpdateManager {
} catch (Exception e) {}
}
Path destPath = Paths.get(opts.packFolder, f.getDestURI().toString());
// Don't update files marked with preserve if they already exist on disk
if (f.preserve) {
if (Files.exists(Paths.get(opts.packFolder, f.getDestURI().toString()))) {
if (Files.exists(destPath)) {
return dc;
}
}
@@ -294,14 +329,19 @@ public class UpdateManager {
Okio.buffer(fileSource).readAll(data);
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.createDirectories(destPath.getParent());
Files.copy(data.inputStream(), destPath, StandardCopyOption.REPLACE_EXISTING);
} 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!");
}
if (cachedFile != null && !destPath.equals(Paths.get(opts.packFolder, cachedFile.cachedLocation))) {
// Delete old file if location changes
Files.delete(Paths.get(opts.packFolder, cachedFile.cachedLocation));
}
return dc;
} catch (Exception e) {
@@ -343,6 +383,7 @@ public class UpdateManager {
ret.err = e;
}
}
newCachedFile.cachedLocation = ret.file.getDestURI().toString();
manifest.cachedFiles.put(ret.file.file, newCachedFile);
}
// TODO: show errors properly?

View File

@@ -16,5 +16,6 @@ public class ManifestFile {
public boolean isOptional = false;
public boolean optionValue = true;
public Hash linkedFileHash = null;
public String cachedLocation = null;
}
}