Szum123321 2022-11-22 15:09:21 +01:00
parent c816c70a6b
commit 85452efbca
3 changed files with 43 additions and 14 deletions

View File

@ -5,7 +5,8 @@ import java.nio.file.Path;
import java.time.LocalDateTime; import java.time.LocalDateTime;
public record CompressionStatus(long[] treeHash, LocalDateTime date, long startTimestamp, long finishTimestamp, boolean ok, Path[] brokenFiles) implements Serializable { public record CompressionStatus(long[] treeHash, LocalDateTime date, long startTimestamp, long finishTimestamp, boolean ok, Path[] brokenFiles) implements Serializable {
public static final String DATA_FILENAME = "textile_status.data";
public boolean isValid(long decompressedHash) { return true; }
public static class Builder { public static class Builder {
public synchronized void update(Path path, long hash, Exception error) { throw new RuntimeException("UNIMPLEMENTED!"); } public synchronized void update(Path path, long hash, Exception error) { throw new RuntimeException("UNIMPLEMENTED!"); }
public synchronized void update(Path path, Exception error) { throw new RuntimeException("UNIMPLEMENTED!"); } public synchronized void update(Path path, Exception error) { throw new RuntimeException("UNIMPLEMENTED!"); }

View File

@ -110,16 +110,14 @@ public abstract class AbstractCompressor {
} }
private record StatusFileInputSupplier(byte[] data, int len) implements InputSupplier { private record StatusFileInputSupplier(byte[] data, int len) implements InputSupplier {
private final static String NAME = "textile_status.data";
@Override @Override
public InputStream getInputStream() { return new ByteArrayInputStream(data, 0, len); } public InputStream getInputStream() { return new ByteArrayInputStream(data, 0, len); }
@Override @Override
public Path getPath() { return Path.of(NAME); } public Path getPath() { return Path.of(CompressionStatus.DATA_FILENAME); }
@Override @Override
public String getName() { return NAME; } public String getName() { return CompressionStatus.DATA_FILENAME; }
@Override @Override
public InputStream get() { return new ByteArrayInputStream(data, 0, len); } public InputStream get() { return new ByteArrayInputStream(data, 0, len); }

View File

@ -24,6 +24,7 @@ import net.szum123321.textile_backup.TextileLogger;
import net.szum123321.textile_backup.config.ConfigHelper; import net.szum123321.textile_backup.config.ConfigHelper;
import net.szum123321.textile_backup.config.ConfigPOJO; import net.szum123321.textile_backup.config.ConfigPOJO;
import net.szum123321.textile_backup.core.ActionInitiator; import net.szum123321.textile_backup.core.ActionInitiator;
import net.szum123321.textile_backup.core.CompressionStatus;
import net.szum123321.textile_backup.core.LivingServer; import net.szum123321.textile_backup.core.LivingServer;
import net.szum123321.textile_backup.core.Utilities; import net.szum123321.textile_backup.core.Utilities;
import net.szum123321.textile_backup.core.create.BackupContext; import net.szum123321.textile_backup.core.create.BackupContext;
@ -32,6 +33,8 @@ import net.szum123321.textile_backup.core.restore.decompressors.GenericTarDecomp
import net.szum123321.textile_backup.core.restore.decompressors.ZipDecompressor; import net.szum123321.textile_backup.core.restore.decompressors.ZipDecompressor;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@ -66,13 +69,22 @@ public class RestoreBackupRunnable implements Runnable {
).run(); ).run();
} }
Path worldFile = Utilities.getWorldFolder(ctx.server()); Path worldFile = Utilities.getWorldFolder(ctx.server()), tmp = null;
try { try {
Path tmp = Files.createTempDirectory( tmp = Files.createTempDirectory(
worldFile.getParent(), ctx.server().getRunDirectory().toPath(),
ctx.restoreableFile().getFile().getFileName().toString()); ctx.restoreableFile().getFile().getFileName().toString());
} catch (IOException e) {
throw new RuntimeException(e);
}
if(tmp == null) {
//TODO: log error!
return;
}
try {
log.info("Starting decompression..."); log.info("Starting decompression...");
if (ctx.restoreableFile().getArchiveFormat() == ConfigPOJO.ArchiveFormat.ZIP) if (ctx.restoreableFile().getArchiveFormat() == ConfigPOJO.ArchiveFormat.ZIP)
@ -80,6 +92,16 @@ public class RestoreBackupRunnable implements Runnable {
else else
GenericTarDecompressor.decompress(ctx.restoreableFile().getFile(), tmp); GenericTarDecompressor.decompress(ctx.restoreableFile().getFile(), tmp);
CompressionStatus status = null;
try (InputStream in = Files.newInputStream(tmp.resolve(CompressionStatus.DATA_FILENAME))) {
ObjectInputStream objectInputStream = new ObjectInputStream(in);
status = (CompressionStatus)objectInputStream.readObject();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
if(status.isValid(0)) {
log.info("Deleting old world..."); log.info("Deleting old world...");
Utilities.deleteDirectory(worldFile); Utilities.deleteDirectory(worldFile);
@ -87,11 +109,19 @@ public class RestoreBackupRunnable implements Runnable {
if (config.get().deleteOldBackupAfterRestore) { if (config.get().deleteOldBackupAfterRestore) {
log.info("Deleting old backup"); log.info("Deleting old backup");
Files.delete(ctx.restoreableFile().getFile()); Files.delete(ctx.restoreableFile().getFile());
} }
}
} catch (IOException e) { } catch (IOException 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 {
if(Files.exists(tmp)) {
try {
Utilities.deleteDirectory(tmp);
} catch (IOException e) {
//TODO: Log error!
}
}
} }
//in case we're playing on client //in case we're playing on client