From 36451c24439b6fc0a407b3aae12048125539f8d5 Mon Sep 17 00:00:00 2001 From: Keuin Date: Thu, 23 Apr 2020 21:33:07 +0800 Subject: 1.2.0-dev: - Optimized ZipUtil. - Added support for Asian characters (Chinese, Japanese, Korean) in backup name. - Add validate for backup name (in file name). --- .../com/keuin/kbackupfabric/KBCommandRegister.java | 4 ++-- .../java/com/keuin/kbackupfabric/KBCommands.java | 24 +++++++++++++++++----- .../com/keuin/kbackupfabric/util/PrintUtil.java | 2 +- .../java/com/keuin/kbackupfabric/util/ZipUtil.java | 2 +- 4 files changed, 23 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/main/java/com/keuin/kbackupfabric/KBCommandRegister.java b/src/main/java/com/keuin/kbackupfabric/KBCommandRegister.java index ec67a64..775dd62 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBCommandRegister.java +++ b/src/main/java/com/keuin/kbackupfabric/KBCommandRegister.java @@ -19,10 +19,10 @@ public final class KBCommandRegister { dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("list").requires(PermissionValidator::op).executes(KBCommands::list))); // register /kb backup [name] for performing backup. OP is required. - dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("backup").then(CommandManager.argument("backupName", StringArgumentType.string()).requires(PermissionValidator::op).executes(KBCommands::backup)).requires(PermissionValidator::op).executes(KBCommands::backupWithDefaultName))); + dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("backup").then(CommandManager.argument("backupName", StringArgumentType.greedyString()).requires(PermissionValidator::op).executes(KBCommands::backup)).requires(PermissionValidator::op).executes(KBCommands::backupWithDefaultName))); // register /kb restore for performing restore. OP is required. - dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("restore").then(CommandManager.argument("backupName", StringArgumentType.string()).suggests(BackupNameSuggestionProvider.getSuggestionProvider()).requires(PermissionValidator::op).executes(KBCommands::restore)).executes(KBCommands::list))); + dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("restore").then(CommandManager.argument("backupName", StringArgumentType.greedyString()).suggests(BackupNameSuggestionProvider.getSuggestionProvider()).requires(PermissionValidator::op).executes(KBCommands::restore)).executes(KBCommands::list))); // register /kb confirm for confirming the execution. OP is required. dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("confirm").requires(PermissionValidator::op).executes(KBCommands::confirm))); diff --git a/src/main/java/com/keuin/kbackupfabric/KBCommands.java b/src/main/java/com/keuin/kbackupfabric/KBCommands.java index b932c80..825046d 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBCommands.java +++ b/src/main/java/com/keuin/kbackupfabric/KBCommands.java @@ -75,7 +75,7 @@ public final class KBCommands { if (backupName.matches("[0-9]*")) { // Numeric param is not allowed backupName = String.format("a%s", backupName); - msgWarn(context, String.format("Pure numeric name is not allowed. Renamed to %s", backupName)); + msgWarn(context, String.format("Pure numeric name is not allowed. Renaming to %s", backupName)); } return doBackup(context, backupName); } @@ -123,13 +123,26 @@ public final class KBCommands { * @return stat code. */ public static int backupWithDefaultName(CommandContext context) { + return doBackup(context, "noname"); + } + + private static int doBackup(CommandContext context, String customName) { + // Real backup name (compatible with legacy backup): date_name, such as 2020-04-23_21-03-00_test //KBMain.backup("name") DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss"); String timeString = LocalDateTime.now().format(formatter); - return doBackup(context, timeString); - } + String backupName = timeString + "_" + customName; + + // Validate file name + final char[] ILLEGAL_CHARACTERS = {'/', '\n', '\r', '\t', '\0', '\f', '`', '?', '*', '\\', '<', '>', '|', '\"', ':'}; + for (char c : ILLEGAL_CHARACTERS) { + if (backupName.contains(String.valueOf(c))) { + msgErr(context, String.format("Name cannot contain special character \"%c\".", c)); + return FAILED; + } + } - private static int doBackup(CommandContext context, String backupName) { + // Do backup BackupMetadata metadata = new BackupMetadata(System.currentTimeMillis(), backupName); BackupWorker.invoke(context, backupName, metadata); return SUCCESS; @@ -149,13 +162,14 @@ public final class KBCommands { // do restore to backupName String backupName = restoreBackupNameToBeConfirmed; - PrintUtil.msgInfo(context, String.format("Restoring worlds to %s ...", backupName), true); + PrintUtil.msgInfo(context, String.format("Restoring to previous world %s ...", backupName), true); // Get server MinecraftServer server = context.getSource().getMinecraftServer(); String backupFileName = getBackupFileName(backupName); debug("Backup file name: " + backupFileName); File backupFile = new File(getBackupSaveDirectory(server), backupFileName); + PrintUtil.msgInfo(context, "Server will shutdown in a few seconds, depended on your world size and the disk speed, the restore progress may take seconds or minutes.", true); PrintUtil.msgInfo(context, "Please do not force the server stop, or the level would be broken.", true); PrintUtil.msgInfo(context, "After it shuts down, please restart the server manually.", true); diff --git a/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java b/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java index 4b93d17..de63037 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java +++ b/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java @@ -52,7 +52,7 @@ public final class PrintUtil { return message(context, messageText, broadcastToOps, errorStyle); } - public static CommandContext message(CommandContext context, String messageText, boolean broadcastToOps, Style style) { + private static CommandContext message(CommandContext context, String messageText, boolean broadcastToOps, Style style) { synchronized (syncMessage) { Text text = new LiteralText(messageText); text.setStyle(style); diff --git a/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java b/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java index 5297dda..e819afc 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java +++ b/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java @@ -172,7 +172,7 @@ public final class ZipUtil { BufferedInputStream bis; BufferedOutputStream bos; ZipFile zip = new ZipFile(zipFile); - Enumeration entries = (Enumeration) zip.entries(); + Enumeration entries = zip.entries(); // 循环对压缩包里的每一个文件进行解压 while (entries.hasMoreElements()) { entry = entries.nextElement(); -- cgit v1.2.3