finalized verification code

This commit is contained in:
Szum123321 2022-11-29 22:34:47 +01:00
parent 5f1706eed3
commit 19cfb3cb27
2 changed files with 25 additions and 10 deletions

View File

@ -18,17 +18,33 @@
package net.szum123321.textile_backup.core;
import net.szum123321.textile_backup.core.restore.RestoreContext;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.Optional;
public record CompressionStatus(long treeHash, Map<Path, Exception> brokenFiles, LocalDateTime date, long startTimestamp, long finishTimestamp, String version) implements Serializable {
public static final String DATA_FILENAME = "textile_status.data";
public boolean isValid(long decompressedHash) {
return decompressedHash == treeHash && brokenFiles.isEmpty();
public Optional<String> isValid(long hash, RestoreContext ctx) throws RuntimeException {
if(hash != treeHash)
return Optional.of("Tree Hash mismatch!\n Expected: " + treeHash + ", got: " + hash);
if(!brokenFiles.isEmpty())
return Optional.of("Damaged files present! ^");
if(ctx.restoreableFile().getCreationTime() != date)
return Optional.of(
"Creation date mismatch!\n Expected: " +
date.format(DateTimeFormatter.ISO_DATE_TIME) + ", got: " +
ctx.restoreableFile().getCreationTime().format(DateTimeFormatter.ISO_DATE_TIME)
);
return Optional.empty();
}
public static CompressionStatus readFromFile(Path folder) throws IOException, ClassNotFoundException {

View File

@ -34,7 +34,6 @@ import net.szum123321.textile_backup.core.restore.decompressors.ZipDecompressor;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
@ -110,11 +109,11 @@ public class RestoreBackupRunnable implements Runnable {
log.info("Status: {}", status);
//TODO: check broken file array
boolean valid = status.isValid(hash);
if(valid || !config.get().errorErrorHandlingMode.verify()) {
if(valid) log.info("Backup valid. Restoring");
else log.info("Backup is damaged, but verification is disabled. Restoring");
var state = status.isValid(hash, ctx);
if(state.isEmpty() || !config.get().errorErrorHandlingMode.verify()) {
if (state.isEmpty()) log.info("Backup valid. Restoring");
else log.info("Backup is damaged, but verification is disabled [{}]. Restoring", state.get());
Utilities.deleteDirectory(worldFile);
Files.move(tmp, worldFile);
@ -124,9 +123,9 @@ public class RestoreBackupRunnable implements Runnable {
Files.delete(ctx.restoreableFile().getFile());
}
} else {
log.error("File tree hash mismatch! Got: {}, Expected {}. Aborting", hash, status.treeHash());
log.error(state.get());
}
} catch (ExecutionException | InterruptedException | ClassNotFoundException | IOException e) {
} catch (Exception e) {
log.error("An exception occurred while trying to restore a backup!", e);
} finally {
//Regardless of what happened, we should still clean up