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; package net.szum123321.textile_backup.core;
import net.szum123321.textile_backup.core.restore.RestoreContext;
import java.io.*; import java.io.*;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Map; 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 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 static final String DATA_FILENAME = "textile_status.data";
public boolean isValid(long decompressedHash) { public Optional<String> isValid(long hash, RestoreContext ctx) throws RuntimeException {
return decompressedHash == treeHash && brokenFiles.isEmpty(); 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 { 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.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask; import java.util.concurrent.FutureTask;
/** /**
@ -110,11 +109,11 @@ public class RestoreBackupRunnable implements Runnable {
log.info("Status: {}", status); log.info("Status: {}", status);
//TODO: check broken file array var state = status.isValid(hash, ctx);
boolean valid = status.isValid(hash);
if(valid || !config.get().errorErrorHandlingMode.verify()) { if(state.isEmpty() || !config.get().errorErrorHandlingMode.verify()) {
if(valid) log.info("Backup valid. Restoring"); if (state.isEmpty()) log.info("Backup valid. Restoring");
else log.info("Backup is damaged, but verification is disabled. Restoring"); else log.info("Backup is damaged, but verification is disabled [{}]. Restoring", state.get());
Utilities.deleteDirectory(worldFile); Utilities.deleteDirectory(worldFile);
Files.move(tmp, worldFile); Files.move(tmp, worldFile);
@ -124,9 +123,9 @@ public class RestoreBackupRunnable implements Runnable {
Files.delete(ctx.restoreableFile().getFile()); Files.delete(ctx.restoreableFile().getFile());
} }
} else { } 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); log.error("An exception occurred while trying to restore a backup!", e);
} finally { } finally {
//Regardless of what happened, we should still clean up //Regardless of what happened, we should still clean up