From c040d05bd86430926c4e219136c39aa705d4e724 Mon Sep 17 00:00:00 2001 From: Szum123321 Date: Sun, 27 Nov 2022 13:58:45 +0100 Subject: [PATCH] Added toString method to CompressionStatus, version bump to 3.0.0-a, few small tweaks --- gradle.properties | 4 +-- .../textile_backup/TextileBackup.java | 2 +- .../core/CompressionStatus.java | 32 ++++++++++++++++++- .../core/NoSpaceLeftOnDeviceException.java | 2 +- .../core/create/MakeBackupRunnable.java | 28 ++++++---------- .../core/restore/RestoreBackupRunnable.java | 12 ++++--- 6 files changed, 53 insertions(+), 27 deletions(-) diff --git a/gradle.properties b/gradle.properties index 3ed2600..993b3d3 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ yarn_mappings=1.19.2+build.28 loader_version=0.14.10 #Fabric api -fabric_version=0.64.0+1.19.2 +fabric_version=0.67.1+1.19.2 #Cloth Config cloth_version=8.2.88 @@ -21,6 +21,6 @@ lazydfu_version=v0.1.3 pgzip_commit_hash=af5f5c297e735f3f2df7aa4eb0e19a5810b8aff6 # Mod Properties -mod_version = 2.5.0 +mod_version = 3.0.0-a maven_group = net.szum123321 archives_base_name = textile_backup \ No newline at end of file diff --git a/src/main/java/net/szum123321/textile_backup/TextileBackup.java b/src/main/java/net/szum123321/textile_backup/TextileBackup.java index 60d7e11..99c0833 100644 --- a/src/main/java/net/szum123321/textile_backup/TextileBackup.java +++ b/src/main/java/net/szum123321/textile_backup/TextileBackup.java @@ -63,7 +63,7 @@ public class TextileBackup implements ModInitializer { Globals.INSTANCE.updateTMPFSFlag(server); }); - //Wait 60s for already submited backups to finish. After that kill the bastards and run the one last if required + //Wait 60s for already submitted backups to finish. After that kill the bastards and run the one last if required ServerLifecycleEvents.SERVER_STOPPED.register(server -> { Globals.INSTANCE.shutdownQueueExecutor(60000); diff --git a/src/main/java/net/szum123321/textile_backup/core/CompressionStatus.java b/src/main/java/net/szum123321/textile_backup/core/CompressionStatus.java index 1e9803f..58cb8e2 100644 --- a/src/main/java/net/szum123321/textile_backup/core/CompressionStatus.java +++ b/src/main/java/net/szum123321/textile_backup/core/CompressionStatus.java @@ -22,11 +22,14 @@ 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; public record CompressionStatus(long treeHash, LocalDateTime date, long startTimestamp, long finishTimestamp, Map brokenFiles) implements Serializable { public static final String DATA_FILENAME = "textile_status.data"; - public boolean isValid(long decompressedHash) { return true; } + public boolean isValid(long decompressedHash) { + return decompressedHash == treeHash; + } public static CompressionStatus readFromFile(Path folder) throws IOException, ClassNotFoundException { try(InputStream i = Files.newInputStream(folder.resolve(DATA_FILENAME)); @@ -43,4 +46,31 @@ public record CompressionStatus(long treeHash, LocalDateTime date, long startTim } } + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("Hash: ") + .append(treeHash) + .append(", Date: ") + .append(date.format(DateTimeFormatter.ISO_DATE_TIME)) + .append(", start time stamp: ").append(startTimestamp) + .append(", finish time stamp: ").append(finishTimestamp); + + builder.append(", broken files: "); + if(brokenFiles.isEmpty()) builder.append("[]"); + else { + builder.append("[\n"); + for(Path i: brokenFiles.keySet()) { + builder.append(i.toString()) + .append(":"); + + ByteArrayOutputStream o = new ByteArrayOutputStream(); + brokenFiles.get(i).printStackTrace(new PrintStream(o)); + builder.append(o).append("\n"); + } + builder.append("]"); + } + + return builder.toString(); + } } diff --git a/src/main/java/net/szum123321/textile_backup/core/NoSpaceLeftOnDeviceException.java b/src/main/java/net/szum123321/textile_backup/core/NoSpaceLeftOnDeviceException.java index 3f71e4b..044cdb9 100644 --- a/src/main/java/net/szum123321/textile_backup/core/NoSpaceLeftOnDeviceException.java +++ b/src/main/java/net/szum123321/textile_backup/core/NoSpaceLeftOnDeviceException.java @@ -25,6 +25,6 @@ import java.io.IOException; */ public class NoSpaceLeftOnDeviceException extends IOException { public NoSpaceLeftOnDeviceException(Throwable cause) { - super(cause); + super("The underlying filesystem has ran out of available space.\nSee: https://github.com/Szum123321/textile_backup/wiki/ZIP-Problems", cause); } } diff --git a/src/main/java/net/szum123321/textile_backup/core/create/MakeBackupRunnable.java b/src/main/java/net/szum123321/textile_backup/core/create/MakeBackupRunnable.java index fd3472f..e550a0a 100644 --- a/src/main/java/net/szum123321/textile_backup/core/create/MakeBackupRunnable.java +++ b/src/main/java/net/szum123321/textile_backup/core/create/MakeBackupRunnable.java @@ -51,11 +51,10 @@ public class MakeBackupRunnable implements Callable { public MakeBackupRunnable(BackupContext context) { this.context = context; } + @Override public Void call() throws IOException, ExecutionException, InterruptedException { - Path outFile = Utilities - .getBackupRootPath(Utilities.getLevelName(context.server())) - .resolve(getFileName()); + Path outFile = Utilities.getBackupRootPath(Utilities.getLevelName(context.server())).resolve(getFileName()); log.trace("Outfile is: {}", outFile); @@ -77,11 +76,9 @@ public class MakeBackupRunnable implements Callable { int coreCount; - if(config.get().compressionCoreCountLimit <= 0) { - coreCount = Runtime.getRuntime().availableProcessors(); - } else { + if (config.get().compressionCoreCountLimit <= 0) coreCount = Runtime.getRuntime().availableProcessors(); + else coreCount = Math.min(config.get().compressionCoreCountLimit, Runtime.getRuntime().availableProcessors()); - } log.trace("Running compression on {} threads. Available cores: {}", coreCount, Runtime.getRuntime().availableProcessors()); @@ -107,11 +104,8 @@ public class MakeBackupRunnable implements Callable { Globals.INSTANCE.getQueueExecutor().submit(new Cleanup(context.commandSource(), Utilities.getLevelName(context.server()))); - if(config.get().broadcastBackupDone) { - Utilities.notifyPlayers( - context.server(), - "Done!" - ); + if (config.get().broadcastBackupDone) { + Utilities.notifyPlayers(context.server(), "Done!"); } else { log.sendInfoAL(context, "Done!"); } @@ -119,7 +113,7 @@ public class MakeBackupRunnable implements Callable { //ExecutorService swallows exception, so I need to catch everything log.error("An exception occurred when trying to create new backup file!", e); - if(ConfigHelper.INSTANCE.get().errorErrorHandlingMode.isStrict()) { + if (ConfigHelper.INSTANCE.get().errorErrorHandlingMode.isStrict()) { try { Files.delete(outFile); } catch (IOException ex) { @@ -127,7 +121,7 @@ public class MakeBackupRunnable implements Callable { } } - if(context.initiator() == ActionInitiator.Player) + if (context.initiator() == ActionInitiator.Player) log.sendError(context, "An exception occurred when trying to create new backup file!"); throw e; @@ -139,9 +133,7 @@ public class MakeBackupRunnable implements Callable { return null; } - private String getFileName(){ - return Utilities.getDateTimeFormatter().format(context.startDate()) + - (context.comment() != null ? "#" + context.comment().replaceAll("[\\\\/:*?\"<>|#]", "") : "") + - config.get().format.getCompleteString(); + private String getFileName() { + return Utilities.getDateTimeFormatter().format(context.startDate()) + (context.comment() != null ? "#" + context.comment().replaceAll("[\\\\/:*?\"<>|#]", "") : "") + config.get().format.getCompleteString(); } } diff --git a/src/main/java/net/szum123321/textile_backup/core/restore/RestoreBackupRunnable.java b/src/main/java/net/szum123321/textile_backup/core/restore/RestoreBackupRunnable.java index 5821bbc..230c27c 100644 --- a/src/main/java/net/szum123321/textile_backup/core/restore/RestoreBackupRunnable.java +++ b/src/main/java/net/szum123321/textile_backup/core/restore/RestoreBackupRunnable.java @@ -55,7 +55,8 @@ public class RestoreBackupRunnable implements Runnable { ctx.server().stop(false); - Path worldFile = Utilities.getWorldFolder(ctx.server()), tmp; + Path worldFile = Utilities.getWorldFolder(ctx.server()), + tmp; try { tmp = Files.createTempDirectory( @@ -99,8 +100,11 @@ public class RestoreBackupRunnable implements Runnable { //locks until the backup is finished waitForShutdown.get(); - if(status.isValid(hash) || !config.get().errorErrorHandlingMode.verify()) { - if(status.isValid(hash)) log.info("Backup valid. Restoring"); + log.info("Status: {}", status); + + 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"); Utilities.deleteDirectory(worldFile); @@ -116,7 +120,7 @@ public class RestoreBackupRunnable implements Runnable { } catch (ExecutionException | InterruptedException | ClassNotFoundException | IOException e) { log.error("An exception occurred while trying to restore a backup!", e); } finally { - //Regardless of what happended, we shiuld still clean up + //Regardless of what happened, we should still clean up if(Files.exists(tmp)) { try { Utilities.deleteDirectory(tmp);