diff options
author | Keuin <[email protected]> | 2020-04-24 12:21:47 +0800 |
---|---|---|
committer | keuin <[email protected]> | 2020-04-24 12:21:47 +0800 |
commit | 5749fc65909f0ee934d0410406265387157575a1 (patch) | |
tree | 111efa8934bae8761372bb7a13227abb75e3dc5a /src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java | |
parent | b692a73e039fbdb404ef1219785660dc9eb60e60 (diff) |
Refactored pending task using OCP design pattern.
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java')
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java new file mode 100644 index 0000000..1e28324 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java @@ -0,0 +1,59 @@ +package com.keuin.kbackupfabric.operation; + +import com.mojang.brigadier.context.CommandContext; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.command.ServerCommandSource; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.File; +import java.io.IOException; + +import static com.keuin.kbackupfabric.util.BackupFilesystemUtil.getBackupFileName; +import static com.keuin.kbackupfabric.util.BackupFilesystemUtil.getBackupSaveDirectory; +import static com.keuin.kbackupfabric.util.PrintUtil.msgErr; +import static com.keuin.kbackupfabric.util.PrintUtil.msgInfo; +import static org.apache.commons.io.FileUtils.forceDelete; + +class DeleteOperation extends Confirmable { + + private static final Logger LOGGER = LogManager.getLogger(); + private final String backupName; + private final CommandContext<ServerCommandSource> context; + + DeleteOperation(CommandContext<ServerCommandSource> context, String backupName) { + this.backupName = backupName; + this.context = context; + } + + @Override + public boolean confirm() { + MinecraftServer server = context.getSource().getMinecraftServer(); + String backupFileName = getBackupFileName(backupName); + LOGGER.info("Deleting backup " + backupName); + File backupFile = new File(getBackupSaveDirectory(server), backupFileName); + int tryCounter = 0; + do { + if (tryCounter == 5) { + String msg = "Failed to delete file " + backupFileName; + LOGGER.error(msg); + msgErr(context, msg); + return false; + } + try { + if (!backupFile.delete()) + forceDelete(backupFile); + } catch (SecurityException | NullPointerException | IOException ignored) { + } + ++tryCounter; + } while (backupFile.exists()); + LOGGER.info("Deleted backup " + backupName); + msgInfo(context, "Deleted backup " + backupName); + return true; + } + + @Override + public String toString() { + return String.format("deletion of %s", backupName); + } +} |