diff options
Diffstat (limited to 'src/main/java/com')
3 files changed, 91 insertions, 66 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/KBCommands.java b/src/main/java/com/keuin/kbackupfabric/KBCommands.java index 98f6369..1b52b6b 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBCommands.java +++ b/src/main/java/com/keuin/kbackupfabric/KBCommands.java @@ -19,6 +19,7 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.ServerCommandSource; import java.io.File; +import java.io.IOException; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Arrays; @@ -207,79 +208,89 @@ public final class KBCommands { * @return stat code. */ public static int restore(CommandContext<ServerCommandSource> context) { - //KBMain.restore("name") - MinecraftServer server = context.getSource().getMinecraftServer(); - String backupFileName = parseBackupFileName(context, StringArgumentType.getString(context, "backupName")); - backupFileName = parseBackupFileName(context, backupFileName); + try { + //KBMain.restore("name") + MinecraftServer server = context.getSource().getMinecraftServer(); + String backupFileName = parseBackupFileName(context, StringArgumentType.getString(context, "backupName")); + backupFileName = parseBackupFileName(context, backupFileName); - if (backupFileName == null) - return list(context); // Show the list and return + if (backupFileName == null) + return list(context); // Show the list and return - // Validate backupName - if (!isBackupFileExists(backupFileName, server)) { - // Invalid backupName - msgErr(context, "Invalid backup name! Please check your input. The list index number is also valid.", false); - return FAILED; - } + // Validate backupName + if (!isBackupFileExists(backupFileName, server)) { + // Invalid backupName + msgErr(context, "Invalid backup name! Please check your input. The list index number is also valid.", false); + return FAILED; + } - // Detect backup type + // Detect backup type - // Update pending task - //pendingOperation = AbstractConfirmableOperation.createRestoreOperation(context, backupName); + // Update pending task + //pendingOperation = AbstractConfirmableOperation.createRestoreOperation(context, backupName); // File backupFile = new File(getBackupSaveDirectory(server), getBackupFileName(backupName)); - // TODO: improve this - ConfiguredBackupMethod method = backupFileName.endsWith(".zip") ? - new ConfiguredPrimitiveBackupMethod( - backupFileName, getLevelPath(server), getBackupSaveDirectory(server).getAbsolutePath() - ) : new ConfiguredIncrementalBackupMethod( - backupFileName, getLevelPath(server), - getBackupSaveDirectory(server).getAbsolutePath(), - getIncrementalBackupBaseDirectory(server).getAbsolutePath() - ); - // String backupSavePath, String levelPath, String backupFileName + // TODO: improve this + ConfiguredBackupMethod method = backupFileName.endsWith(".zip") ? + new ConfiguredPrimitiveBackupMethod( + backupFileName, getLevelPath(server), getBackupSaveDirectory(server).getAbsolutePath() + ) : new ConfiguredIncrementalBackupMethod( + backupFileName, getLevelPath(server), + getBackupSaveDirectory(server).getAbsolutePath(), + getIncrementalBackupBaseDirectory(server).getAbsolutePath() + ); + // String backupSavePath, String levelPath, String backupFileName // getBackupSaveDirectory(server).getAbsolutePath(), getLevelPath(server), backupFileName - pendingOperation = new RestoreOperation(context, method); + pendingOperation = new RestoreOperation(context, method); - msgWarn(context, String.format("RESET WARNING: You will LOSE YOUR CURRENT WORLD PERMANENTLY! The worlds will be replaced with backup %s . Use /kb confirm to start or /kb cancel to abort.", backupFileName), true); - return SUCCESS; + msgWarn(context, String.format("RESET WARNING: You will LOSE YOUR CURRENT WORLD PERMANENTLY! The worlds will be replaced with backup %s . Use /kb confirm to start or /kb cancel to abort.", backupFileName), true); + return SUCCESS; + } catch (IOException e) { + msgErr(context, String.format("An I/O exception occurred while making backup: %s", e)); + } + return FAILED; } private static int doBackup(CommandContext<ServerCommandSource> context, String customBackupName, boolean incremental) { - // Real backup name (compatible with legacy backup): date_name, such as 2020-04-23_21-03-00_test - //KBMain.backup("name") + try { + // Real backup name (compatible with legacy backup): date_name, such as 2020-04-23_21-03-00_test + //KBMain.backup("name") // String backupName = BackupNameTimeFormatter.getTimeString() + "_" + customBackupName; - // Validate file name - final char[] ILLEGAL_CHARACTERS = {'/', '\n', '\r', '\t', '\0', '\f', '`', '?', '*', '\\', '<', '>', '|', '\"', ':'}; - for (char c : ILLEGAL_CHARACTERS) { - if (customBackupName.contains(String.valueOf(c))) { - msgErr(context, String.format("Name cannot contain special character \"%c\".", c)); - return FAILED; + // Validate file name + final char[] ILLEGAL_CHARACTERS = {'/', '\n', '\r', '\t', '\0', '\f', '`', '?', '*', '\\', '<', '>', '|', '\"', ':'}; + for (char c : ILLEGAL_CHARACTERS) { + if (customBackupName.contains(String.valueOf(c))) { + msgErr(context, String.format("Name cannot contain special character \"%c\".", c)); + return FAILED; + } } - } - PrintUtil.info("Start backup..."); + PrintUtil.info("Start backup..."); - // configure backup method - MinecraftServer server = context.getSource().getMinecraftServer(); - ConfiguredBackupMethod method = !incremental ? new ConfiguredPrimitiveBackupMethod( - new PrimitiveBackupFileNameEncoder().encode(customBackupName, LocalDateTime.now()), - getLevelPath(server), - getBackupSaveDirectory(server).getAbsolutePath() - ) : new ConfiguredIncrementalBackupMethod( - new IncrementalBackupFileNameEncoder().encode(customBackupName, LocalDateTime.now()), - getLevelPath(server), - getBackupSaveDirectory(server).getAbsolutePath(), - getIncrementalBackupBaseDirectory(server).getAbsolutePath() - ); - - // dispatch to operation worker - BackupOperation operation = new BackupOperation(context, method); - if (operation.invoke()) { - return SUCCESS; - } else if (operation.isBlocked()) { - msgWarn(context, "Another task is running, cannot issue new backup at once."); + // configure backup method + MinecraftServer server = context.getSource().getMinecraftServer(); + ConfiguredBackupMethod method = !incremental ? new ConfiguredPrimitiveBackupMethod( + new PrimitiveBackupFileNameEncoder().encode(customBackupName, LocalDateTime.now()), + getLevelPath(server), + getBackupSaveDirectory(server).getAbsolutePath() + ) : new ConfiguredIncrementalBackupMethod( + new IncrementalBackupFileNameEncoder().encode(customBackupName, LocalDateTime.now()), + getLevelPath(server), + getBackupSaveDirectory(server).getAbsolutePath(), + getIncrementalBackupBaseDirectory(server).getAbsolutePath() + ); + + // dispatch to operation worker + BackupOperation operation = new BackupOperation(context, method); + if (operation.invoke()) { + return SUCCESS; + } else if (operation.isBlocked()) { + msgWarn(context, "Another task is running, cannot issue new backup at once."); + return FAILED; + } + } catch (IOException e) { + msgErr(context, String.format("An I/O exception occurred while making backup: %s", e)); } return FAILED; } diff --git a/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java b/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java index c670cf1..0fa9d77 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java +++ b/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java @@ -4,7 +4,10 @@ import com.keuin.kbackupfabric.exception.ZipUtilException; import com.keuin.kbackupfabric.metadata.BackupMetadata; import java.io.*; +import java.util.Collections; import java.util.Enumeration; +import java.util.Optional; +import java.util.Set; import java.util.zip.*; public final class ZipUtil { @@ -15,9 +18,10 @@ public final class ZipUtil { * @param srcRootDir 压缩文件夹根目录的子路径 * @param file 当前递归压缩的文件或目录对象 * @param zipOutputStream 压缩文件存储对象 + * @param filesSkipping 被忽略的文件 * @throws IOException IO Error */ - private static void zip(String srcRootDir, File file, ZipOutputStream zipOutputStream) throws IOException { + private static void zip(String srcRootDir, File file, ZipOutputStream zipOutputStream, Set<String> filesSkipping) throws IOException { if (file == null) { return; } @@ -26,7 +30,8 @@ public final class ZipUtil { return; // Reject // 如果是文件,则直接压缩该文件 - if (file.isFile()) { + boolean skipping = Optional.ofNullable(filesSkipping).orElse(Collections.emptySet()).contains(file.getName()); + if (file.isFile() && !skipping) { int count, bufferLen = 1024; byte[] data = new byte[bufferLen]; @@ -36,6 +41,8 @@ public final class ZipUtil { if (index != -1) { subPath = subPath.substring(srcRootDir.length() + File.separator.length()); } + + // 写入压缩包 ZipEntry entry = new ZipEntry(subPath); zipOutputStream.putNextEntry(entry); BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file)); @@ -44,14 +51,13 @@ public final class ZipUtil { } inputStream.close(); zipOutputStream.closeEntry(); - } - // 如果是目录,则压缩整个目录 - else { + } else { + // 如果是目录,则压缩整个目录 // 压缩目录中的文件或子目录 File[] childFileList = file.listFiles(); if (childFileList != null) { for (File value : childFileList) - zip(srcRootDir, value, zipOutputStream); + zip(srcRootDir, value, zipOutputStream, filesSkipping); } } } @@ -127,7 +133,7 @@ public final class ZipUtil { } } //调用递归压缩方法进行目录或文件压缩 - zip(srcRootDir, srcFile, zipOutputStream); + zip(srcRootDir, srcFile, zipOutputStream, Collections.singleton("session.lock")); zipOutputStream.flush(); } finally { try { diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/BackupFilesystemUtil.java b/src/main/java/com/keuin/kbackupfabric/util/backup/BackupFilesystemUtil.java index 7106ad2..28ede70 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/BackupFilesystemUtil.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/BackupFilesystemUtil.java @@ -2,10 +2,13 @@ package com.keuin.kbackupfabric.util.backup; import com.keuin.kbackupfabric.util.ReflectionUtils; import net.minecraft.server.MinecraftServer; +import net.minecraft.server.dedicated.MinecraftDedicatedServer; import net.minecraft.server.world.ThreadedAnvilChunkStorage; import net.minecraft.world.World; import java.io.File; +import java.io.IOException; +import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -47,8 +50,13 @@ public final class BackupFilesystemUtil { return new File(server.getRunDirectory(), INCREMENTAL_BASE_DIRECTORY_NAME); } - public static String getLevelPath(MinecraftServer server) { - return (new File(server.getRunDirectory(), server.getLevelName())).getAbsolutePath(); + public static String getLevelPath(MinecraftServer server) throws IOException { + if (!(server instanceof MinecraftDedicatedServer)) + throw new IllegalStateException("This plugin is server-side only."); + String path = (new File(server.getRunDirectory().getCanonicalPath(), ((MinecraftDedicatedServer) server).getLevelName())).getAbsolutePath(); + Logger.getLogger("getLevelPath").info(String.format("Level path: %s", path)); + assert (new File(path)).exists(); + return path; } public static String getWorldDirectoryName(World world) throws NoSuchFieldException, IllegalAccessException { |