From 3f2658ed9683bbd2cbdb89df25d04e2acac4bb30 Mon Sep 17 00:00:00 2001 From: Szum123321 Date: Sat, 5 Nov 2022 13:31:44 +0100 Subject: [PATCH] Cleanup is now implements Callable --- .../commands/create/CleanupCommand.java | 2 +- .../textile_backup/core/Cleanup.java | 19 ++++++++++++++----- .../core/create/MakeBackupRunnable.java | 2 +- .../core/restore/RestoreBackupRunnable.java | 1 - 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/szum123321/textile_backup/commands/create/CleanupCommand.java b/src/main/java/net/szum123321/textile_backup/commands/create/CleanupCommand.java index 05dafce..c0567ee 100644 --- a/src/main/java/net/szum123321/textile_backup/commands/create/CleanupCommand.java +++ b/src/main/java/net/szum123321/textile_backup/commands/create/CleanupCommand.java @@ -38,7 +38,7 @@ public class CleanupCommand { log.sendInfo( source, "Deleted: {} files.", - Cleanup.executeFileLimit(source, Utilities.getLevelName(source.getServer())) + new Cleanup(source, Utilities.getLevelName(source.getServer())).call() ); return 1; diff --git a/src/main/java/net/szum123321/textile_backup/core/Cleanup.java b/src/main/java/net/szum123321/textile_backup/core/Cleanup.java index 0894ce4..56fd97d 100644 --- a/src/main/java/net/szum123321/textile_backup/core/Cleanup.java +++ b/src/main/java/net/szum123321/textile_backup/core/Cleanup.java @@ -31,16 +31,25 @@ import java.nio.file.Path; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.util.Objects; +import java.util.concurrent.Callable; import java.util.stream.Stream; /** * Utility used for removing old backups */ -public class Cleanup { +public class Cleanup implements Callable { private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME); private final static ConfigHelper config = ConfigHelper.INSTANCE; - public static int executeFileLimit(ServerCommandSource ctx, String worldName) { + private final ServerCommandSource ctx; + private final String worldName; + + public Cleanup(ServerCommandSource ctx, String worldName) { + this.ctx = ctx; + this.worldName = worldName; + } + + public Integer call() { Path root = Utilities.getBackupRootPath(worldName); int deletedFiles = 0; @@ -86,7 +95,7 @@ public class Cleanup { return deletedFiles; } - private static long[] count(Path root) { + private long[] count(Path root) { long n = 0, size = 0; try(Stream stream = Files.list(root)) { @@ -108,13 +117,13 @@ public class Cleanup { return new long[]{n, size}; } - private static boolean isEmpty(Path root) { + private boolean isEmpty(Path root) { if (!Files.isDirectory(root)) return false; return RestoreableFile.applyOnFiles(root, false, e -> {}, s -> s.findFirst().isEmpty()); } //1 -> ok, 0 -> err - private static boolean deleteFile(Path f, ServerCommandSource ctx) { + private boolean deleteFile(Path f, ServerCommandSource ctx) { if(Globals.INSTANCE.getLockedFile().filter(p -> p == f).isPresent()) return false; try { Files.delete(f); 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 e184aab..13bda1b 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 @@ -104,7 +104,7 @@ public class MakeBackupRunnable implements Runnable { case TAR -> new AbstractTarArchiver().createArchive(world, outFile, context, coreCount); } - Cleanup.executeFileLimit(context.commandSource(), Utilities.getLevelName(context.server())); + new Cleanup(context.commandSource(), Utilities.getLevelName(context.server())).call(); if(config.get().broadcastBackupDone) { Utilities.notifyPlayers( 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 be798b4..f2dd22d 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 @@ -83,7 +83,6 @@ public class RestoreBackupRunnable implements Runnable { log.info("Deleting old world..."); Utilities.deleteDirectory(worldFile); - Files.move(tmp, worldFile); if (config.get().deleteOldBackupAfterRestore) {