Cleanup is now implements Callable

This commit is contained in:
Szum123321 2022-11-05 13:31:44 +01:00
parent fe25b1eec5
commit 3f2658ed96
4 changed files with 16 additions and 8 deletions

View File

@ -38,7 +38,7 @@ public class CleanupCommand {
log.sendInfo( log.sendInfo(
source, source,
"Deleted: {} files.", "Deleted: {} files.",
Cleanup.executeFileLimit(source, Utilities.getLevelName(source.getServer())) new Cleanup(source, Utilities.getLevelName(source.getServer())).call()
); );
return 1; return 1;

View File

@ -31,16 +31,25 @@ import java.nio.file.Path;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.stream.Stream; import java.util.stream.Stream;
/** /**
* Utility used for removing old backups * Utility used for removing old backups
*/ */
public class Cleanup { public class Cleanup implements Callable<Integer> {
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME); private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
private final static ConfigHelper config = ConfigHelper.INSTANCE; 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); Path root = Utilities.getBackupRootPath(worldName);
int deletedFiles = 0; int deletedFiles = 0;
@ -86,7 +95,7 @@ public class Cleanup {
return deletedFiles; return deletedFiles;
} }
private static long[] count(Path root) { private long[] count(Path root) {
long n = 0, size = 0; long n = 0, size = 0;
try(Stream<Path> stream = Files.list(root)) { try(Stream<Path> stream = Files.list(root)) {
@ -108,13 +117,13 @@ public class Cleanup {
return new long[]{n, size}; return new long[]{n, size};
} }
private static boolean isEmpty(Path root) { private boolean isEmpty(Path root) {
if (!Files.isDirectory(root)) return false; if (!Files.isDirectory(root)) return false;
return RestoreableFile.applyOnFiles(root, false, e -> {}, s -> s.findFirst().isEmpty()); return RestoreableFile.applyOnFiles(root, false, e -> {}, s -> s.findFirst().isEmpty());
} }
//1 -> ok, 0 -> err //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; if(Globals.INSTANCE.getLockedFile().filter(p -> p == f).isPresent()) return false;
try { try {
Files.delete(f); Files.delete(f);

View File

@ -104,7 +104,7 @@ public class MakeBackupRunnable implements Runnable {
case TAR -> new AbstractTarArchiver().createArchive(world, outFile, context, coreCount); 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) { if(config.get().broadcastBackupDone) {
Utilities.notifyPlayers( Utilities.notifyPlayers(

View File

@ -83,7 +83,6 @@ public class RestoreBackupRunnable implements Runnable {
log.info("Deleting old world..."); log.info("Deleting old world...");
Utilities.deleteDirectory(worldFile); Utilities.deleteDirectory(worldFile);
Files.move(tmp, worldFile); Files.move(tmp, worldFile);
if (config.get().deleteOldBackupAfterRestore) { if (config.get().deleteOldBackupAfterRestore) {