From c82e1ee286e89f6e36e8920282d348ce8cfae9cf Mon Sep 17 00:00:00 2001 From: Keuin Date: Wed, 13 Jan 2021 13:22:19 +0800 Subject: Refactor for a better code quality --- .../java/com/keuin/kbackupfabric/KBCommands.java | 67 +++++++++------ .../kbackupfabric/operation/BackupOperation.java | 6 +- .../kbackupfabric/operation/RestoreOperation.java | 15 ++-- .../operation/backup/BackupMethod.java | 22 ----- .../operation/backup/PrimitiveBackupMethod.java | 95 ---------------------- .../operation/backup/feedback/BackupFeedback.java | 1 + .../backup/feedback/IncrementalBackupFeedback.java | 28 +++++++ .../backup/feedback/PrimitiveBackupFeedback.java | 3 +- .../operation/backup/method/BackupMethod.java | 22 +++++ .../backup/method/IncrementalBackupMethod.java | 17 ++++ .../backup/method/PrimitiveBackupMethod.java | 95 ++++++++++++++++++++++ .../util/backup/name/BackupFileNameEncoder.java | 9 ++ .../name/IncrementalBackupFileNameEncoder.java | 33 ++++++++ .../name/PrimitiveBackupFileNameEncoder.java | 1 - 14 files changed, 262 insertions(+), 152 deletions(-) delete mode 100644 src/main/java/com/keuin/kbackupfabric/operation/backup/BackupMethod.java delete mode 100644 src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java create mode 100644 src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/IncrementalBackupFeedback.java create mode 100644 src/main/java/com/keuin/kbackupfabric/operation/backup/method/BackupMethod.java create mode 100644 src/main/java/com/keuin/kbackupfabric/operation/backup/method/IncrementalBackupMethod.java create mode 100644 src/main/java/com/keuin/kbackupfabric/operation/backup/method/PrimitiveBackupMethod.java create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/name/IncrementalBackupFileNameEncoder.java (limited to 'src/main/java/com') diff --git a/src/main/java/com/keuin/kbackupfabric/KBCommands.java b/src/main/java/com/keuin/kbackupfabric/KBCommands.java index ef4a401..0e268ce 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBCommands.java +++ b/src/main/java/com/keuin/kbackupfabric/KBCommands.java @@ -5,9 +5,10 @@ import com.keuin.kbackupfabric.operation.BackupOperation; import com.keuin.kbackupfabric.operation.DeleteOperation; import com.keuin.kbackupfabric.operation.RestoreOperation; import com.keuin.kbackupfabric.operation.abstracts.i.Invokable; -import com.keuin.kbackupfabric.operation.backup.PrimitiveBackupMethod; +import com.keuin.kbackupfabric.operation.backup.method.PrimitiveBackupMethod; import com.keuin.kbackupfabric.util.PrintUtil; import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil; +import com.keuin.kbackupfabric.util.backup.name.PrimitiveBackupFileNameEncoder; import com.keuin.kbackupfabric.util.backup.suggestion.BackupNameSuggestionProvider; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.context.CommandContext; @@ -82,23 +83,24 @@ public final class KBCommands { (dir, name) -> dir.isDirectory() && name.toLowerCase().endsWith(".zip") && name.toLowerCase().startsWith(getBackupFileNamePrefix()) ); - backupFileNameList.clear(); - if (files != null) { - if (files.length != 0) { - msgInfo(context, "Available backups: (file is not checked, manipulation may affect this plugin)"); + synchronized (backupFileNameList) { + backupFileNameList.clear(); + if (files != null) { + if (files.length != 0) { + msgInfo(context, "Available backups: (file is not checked, manipulation may affect this plugin)"); + } else { + msgInfo(context, "There are no available backups. To make a new backup, run /kb backup."); + } + int i = 0; + for (File file : files) { + ++i; + String backupFileName = file.getName(); + msgInfo(context, String.format("[%d] %s", i, getPrimitiveBackupInformationString(backupFileName, file.length()))); + backupFileNameList.add(backupFileName); + } } else { - msgInfo(context, "There are no available backups. To make a new backup, run /kb backup."); + msgErr(context, "Error: failed to list files in backup folder."); } - int i = 0; - for (File file : files) { - ++i; - String backupFileName = file.getName(); - String sizeString = getFriendlyFileSizeString(file.length()); - msgInfo(context, String.format("[%d] %s, size: %s", i, backupFileName, sizeString)); - backupFileNameList.add(backupFileName); - } - } else { - msgErr(context, "Error: failed to list files in backup folder."); } return SUCCESS; } @@ -293,14 +295,21 @@ public final class KBCommands { files.sort((x, y) -> (int) (BackupFilesystemUtil.getBackupTimeFromBackupFileName(y.getName()) - BackupFilesystemUtil.getBackupTimeFromBackupFileName(x.getName()))); File prevBackupFile = files.get(0); String backupFileName = prevBackupFile.getName(); - int i = backupFileNameList.indexOf(backupFileName); - if (i == -1) { - backupFileNameList.add(backupFileName); - i = backupFileNameList.size(); - } else { - ++i; + int i; + synchronized (backupFileNameList) { + i = backupFileNameList.indexOf(backupFileName); + if (i == -1) { + backupFileNameList.add(backupFileName); + i = backupFileNameList.size(); + } else { + ++i; + } } - msgInfo(context, String.format("The most recent backup: [%d] %s , size: %s", i, backupFileName, getFriendlyFileSizeString(prevBackupFile.length()))); + msgInfo(context, String.format( + "The most recent backup: [%d] %s", + i, + getPrimitiveBackupInformationString(backupFileName, prevBackupFile.length()) + )); } catch (NullPointerException e) { msgInfo(context, "There are no backups available."); } catch (SecurityException ignored) { @@ -310,6 +319,14 @@ public final class KBCommands { return SUCCESS; } + private static String getPrimitiveBackupInformationString(String backupFileName, long backupFileSizeBytes) { + return String.format( + "%s , size: %s", + new PrimitiveBackupFileNameEncoder().decode(backupFileName), + getFriendlyFileSizeString(backupFileSizeBytes) + ); + } + // /** // * Select the backup method we use. // * @param context the context. @@ -338,7 +355,9 @@ public final class KBCommands { if (backupName.matches("[0-9]*")) { // If numeric input int index = Integer.parseInt(backupName) - 1; - return backupFileNameList.get(index); // Replace input number with real backup file name. + synchronized (backupFileNameList) { + return backupFileNameList.get(index); // Replace input number with real backup file name. + } } } catch (NumberFormatException | IndexOutOfBoundsException ignored) { } diff --git a/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java index f8736c8..3183cf0 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java @@ -1,8 +1,8 @@ package com.keuin.kbackupfabric.operation; import com.keuin.kbackupfabric.operation.abstracts.InvokableAsyncBlockingOperation; -import com.keuin.kbackupfabric.operation.backup.BackupMethod; -import com.keuin.kbackupfabric.operation.backup.feedback.PrimitiveBackupFeedback; +import com.keuin.kbackupfabric.operation.backup.feedback.BackupFeedback; +import com.keuin.kbackupfabric.operation.backup.method.BackupMethod; import com.keuin.kbackupfabric.util.PrintUtil; import com.mojang.brigadier.context.CommandContext; import net.minecraft.server.MinecraftServer; @@ -52,7 +52,7 @@ public class BackupOperation extends InvokableAsyncBlockingOperation { String levelPath = getLevelPath(server); String backupFileName = getBackupFileName(customBackupName); - PrimitiveBackupFeedback result = backupMethod.backup(customBackupName,levelPath,backupSaveDirectory); + BackupFeedback result = backupMethod.backup(customBackupName,levelPath,backupSaveDirectory); if(result.isSuccess()) { // Restore old auto-save switch stat server.getWorlds().forEach(world -> world.savingDisabled = oldWorldsSavingDisabled.getOrDefault(world, true)); diff --git a/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java index b95c767..02b76f1 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java @@ -1,8 +1,8 @@ package com.keuin.kbackupfabric.operation; import com.keuin.kbackupfabric.operation.abstracts.InvokableBlockingOperation; -import com.keuin.kbackupfabric.operation.backup.BackupMethod; -import com.keuin.kbackupfabric.operation.backup.PrimitiveBackupMethod; +import com.keuin.kbackupfabric.operation.backup.method.BackupMethod; +import com.keuin.kbackupfabric.operation.backup.method.PrimitiveBackupMethod; import com.keuin.kbackupfabric.util.PrintUtil; import com.mojang.brigadier.context.CommandContext; import net.minecraft.server.MinecraftServer; @@ -94,10 +94,12 @@ public class RestoreOperation extends InvokableBlockingOperation { }while(--cnt > 0); //////////////////// - backupMethod.restore(backupFileName, levelPath, backupSavePath); - - //ServerRestartUtil.forkAndRestart(); - System.exit(111); + if (backupMethod.restore(backupFileName, levelPath, backupSavePath)) { + //ServerRestartUtil.forkAndRestart(); + System.exit(111); + } else { + PrintUtil.error("Failed to restore! server will not restart automatically."); + } } catch (SecurityException e) { PrintUtil.error("An exception occurred while restoring: " + e.getMessage()); @@ -105,6 +107,7 @@ public class RestoreOperation extends InvokableBlockingOperation { PrintUtil.error(e.toString()); PrintUtil.error("Failed to restore."); } + System.exit(0); // all failed restoration will eventually go here } } } diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/BackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/BackupMethod.java deleted file mode 100644 index b65a076..0000000 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/BackupMethod.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.keuin.kbackupfabric.operation.backup; - -import com.keuin.kbackupfabric.operation.backup.feedback.PrimitiveBackupFeedback; - -import java.io.IOException; - -/** - * Provide specific backup method, which is implemented statelessly. - */ -public interface BackupMethod { - - /** - * Perform a backup with given method. The backup will be saved as the given name. - * Note: real file name depends on the backup type. - * @param backupName the backup name. - * @return if the backup operation succeed. - */ - PrimitiveBackupFeedback backup(String backupName, String levelPath, String backupSaveDirectory) throws IOException; - - boolean restore(String backupName, String levelPath, String backupSaveDirectory) throws IOException; - -} diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java deleted file mode 100644 index 9c065a6..0000000 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java +++ /dev/null @@ -1,95 +0,0 @@ -package com.keuin.kbackupfabric.operation.backup; - -import com.keuin.kbackupfabric.exception.ZipUtilException; -import com.keuin.kbackupfabric.metadata.BackupMetadata; -import com.keuin.kbackupfabric.operation.backup.feedback.PrimitiveBackupFeedback; -import com.keuin.kbackupfabric.util.FilesystemUtil; -import com.keuin.kbackupfabric.util.PrintUtil; -import com.keuin.kbackupfabric.util.ZipUtil; -import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil; -import com.keuin.kbackupfabric.util.backup.BackupNameTimeFormatter; -import com.keuin.kbackupfabric.util.backup.name.PrimitiveBackupFileNameEncoder; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Paths; -import java.time.LocalDateTime; - -import static org.apache.commons.io.FileUtils.forceDelete; - -public class PrimitiveBackupMethod implements BackupMethod { - - private static final PrimitiveBackupMethod INSTANCE = new PrimitiveBackupMethod(); - - public static PrimitiveBackupMethod getInstance() { - return INSTANCE; - } - - @Deprecated - private String getBackupFileName(LocalDateTime time, String backupName) { - String timeString = BackupNameTimeFormatter.localDateTimeToString(time); - return String.format("%s%s_%s%s", BackupFilesystemUtil.getBackupFileNamePrefix(), timeString, backupName, ".zip"); - } - - @Override - public PrimitiveBackupFeedback backup(String customBackupName, String levelPath, String backupSavePath) throws IOException { -// String backupFileName = getBackupFileName(LocalDateTime.now(),backupName); - String backupFileName = new PrimitiveBackupFileNameEncoder().encode(customBackupName, LocalDateTime.now()); - try { - BackupMetadata backupMetadata = new BackupMetadata(System.currentTimeMillis(), customBackupName); - PrintUtil.info(String.format("zip(srcPath=%s, destPath=%s)", levelPath, backupSavePath)); - PrintUtil.info("Compressing level ..."); - ZipUtil.makeBackupZip(levelPath, backupSavePath, backupFileName, backupMetadata); - } catch (ZipUtilException exception) { - PrintUtil.info("Infinite recursive of directory tree detected, backup was aborted."); - return new PrimitiveBackupFeedback(false, 0); - } - - // Get backup file size and return - return new PrimitiveBackupFeedback(true, FilesystemUtil.getFileSizeBytes(backupSavePath, backupFileName)); - } - - @Override - public boolean restore(String backupFileName, String levelDirectory, String backupSaveDirectory) throws IOException { - // Delete old level - PrintUtil.info("Server stopped. Deleting old level ..."); - File levelDirFile = new File(levelDirectory); - long startTime = System.currentTimeMillis(); - - int failedCounter = 0; - final int MAX_RETRY_TIMES = 20; - while (failedCounter < MAX_RETRY_TIMES) { - System.gc(); - if (!levelDirFile.delete() && levelDirFile.exists()) { - System.gc(); - forceDelete(levelDirFile); // Try to force delete. - } - if (!levelDirFile.exists()) - break; - ++failedCounter; - try { - Thread.sleep(500); - } catch (InterruptedException ignored) { - } - } - if (levelDirFile.exists()) { - PrintUtil.error(String.format("Cannot restore: failed to delete old level %s .", levelDirFile.getName())); - return false; - } - - // TODO: Refactor this to the concrete BackupMethod. - // Decompress archive - PrintUtil.info("Decompressing archived level ..."); - ZipUtil.unzip(Paths.get(backupSaveDirectory, backupFileName).toString(), levelDirectory, false); - long endTime = System.currentTimeMillis(); - PrintUtil.info(String.format("Restore complete! (%.2fs) Please restart the server manually.", (endTime - startTime) / 1000.0)); - PrintUtil.info("If you want to restart automatically after restoring, please check the manual at: https://github.com/keuin/KBackup-Fabric/blob/master/README.md"); - -// try { -// Thread.sleep(1000); -// } catch (InterruptedException ignored) { -// } - - return true; - } -} diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/BackupFeedback.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/BackupFeedback.java index 92a9f39..0fe0766 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/BackupFeedback.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/BackupFeedback.java @@ -2,4 +2,5 @@ package com.keuin.kbackupfabric.operation.backup.feedback; public interface BackupFeedback { String getFeedback(); + boolean isSuccess(); } diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/IncrementalBackupFeedback.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/IncrementalBackupFeedback.java new file mode 100644 index 0000000..f39fde6 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/IncrementalBackupFeedback.java @@ -0,0 +1,28 @@ +package com.keuin.kbackupfabric.operation.backup.feedback; + +public class IncrementalBackupFeedback implements BackupFeedback { + private final boolean success; + private final int newFilesAdded; + + public IncrementalBackupFeedback(boolean success, int newFilesAdded) { + this.success = success; + this.newFilesAdded = newFilesAdded; + } + + @Override + public boolean isSuccess() { + return success; + } + + public long getNewFilesAdded() { + return newFilesAdded; + } + + @Override + public String getFeedback() { + if (success && newFilesAdded >= 0) + return String.format("File(s) added: %d.", newFilesAdded); + else + return ""; + } +} diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/PrimitiveBackupFeedback.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/PrimitiveBackupFeedback.java index 6d7a15b..3bcd012 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/PrimitiveBackupFeedback.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/PrimitiveBackupFeedback.java @@ -11,6 +11,7 @@ public class PrimitiveBackupFeedback implements BackupFeedback { this.backupSizeBytes = backupSizeBytes; } + @Override public boolean isSuccess() { return success; } @@ -22,7 +23,7 @@ public class PrimitiveBackupFeedback implements BackupFeedback { @Override public String getFeedback() { if (success && backupSizeBytes >= 0) - return String.format(" File size: %s.", getFriendlyFileSizeString(backupSizeBytes)); + return String.format("File size: %s.", getFriendlyFileSizeString(backupSizeBytes)); else return ""; } diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/BackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/BackupMethod.java new file mode 100644 index 0000000..25a5952 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/BackupMethod.java @@ -0,0 +1,22 @@ +package com.keuin.kbackupfabric.operation.backup.method; + +import com.keuin.kbackupfabric.operation.backup.feedback.BackupFeedback; + +import java.io.IOException; + +/** + * Provide specific backup method, which is implemented statelessly. + */ +public interface BackupMethod { + + /** + * Perform a backup with given method. The backup will be saved as the given name. + * Note: real file name depends on the backup type. + * @param backupName the backup name. + * @return if the backup operation succeed. + */ + BackupFeedback backup(String backupName, String levelPath, String backupSaveDirectory) throws IOException; + + boolean restore(String backupName, String levelPath, String backupSaveDirectory) throws IOException; + +} diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/IncrementalBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/IncrementalBackupMethod.java new file mode 100644 index 0000000..1dacc44 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/IncrementalBackupMethod.java @@ -0,0 +1,17 @@ +package com.keuin.kbackupfabric.operation.backup.method; + +import com.keuin.kbackupfabric.operation.backup.feedback.IncrementalBackupFeedback; + +import java.io.IOException; + +public class IncrementalBackupMethod implements BackupMethod { + @Override + public IncrementalBackupFeedback backup(String backupName, String levelPath, String backupSaveDirectory) throws IOException { + return null; + } + + @Override + public boolean restore(String backupName, String levelPath, String backupSaveDirectory) throws IOException { + return false; + } +} diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/PrimitiveBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/PrimitiveBackupMethod.java new file mode 100644 index 0000000..1f15346 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/PrimitiveBackupMethod.java @@ -0,0 +1,95 @@ +package com.keuin.kbackupfabric.operation.backup.method; + +import com.keuin.kbackupfabric.exception.ZipUtilException; +import com.keuin.kbackupfabric.metadata.BackupMetadata; +import com.keuin.kbackupfabric.operation.backup.feedback.PrimitiveBackupFeedback; +import com.keuin.kbackupfabric.util.FilesystemUtil; +import com.keuin.kbackupfabric.util.PrintUtil; +import com.keuin.kbackupfabric.util.ZipUtil; +import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil; +import com.keuin.kbackupfabric.util.backup.BackupNameTimeFormatter; +import com.keuin.kbackupfabric.util.backup.name.PrimitiveBackupFileNameEncoder; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; +import java.time.LocalDateTime; + +import static org.apache.commons.io.FileUtils.forceDelete; + +public class PrimitiveBackupMethod implements BackupMethod { + + private static final PrimitiveBackupMethod INSTANCE = new PrimitiveBackupMethod(); + + public static PrimitiveBackupMethod getInstance() { + return INSTANCE; + } + + @Deprecated + private String getBackupFileName(LocalDateTime time, String backupName) { + String timeString = BackupNameTimeFormatter.localDateTimeToString(time); + return String.format("%s%s_%s%s", BackupFilesystemUtil.getBackupFileNamePrefix(), timeString, backupName, ".zip"); + } + + @Override + public PrimitiveBackupFeedback backup(String customBackupName, String levelPath, String backupSavePath) throws IOException { +// String backupFileName = getBackupFileName(LocalDateTime.now(),backupName); + String backupFileName = new PrimitiveBackupFileNameEncoder().encode(customBackupName, LocalDateTime.now()); + try { + BackupMetadata backupMetadata = new BackupMetadata(System.currentTimeMillis(), customBackupName); + PrintUtil.info(String.format("zip(srcPath=%s, destPath=%s)", levelPath, backupSavePath)); + PrintUtil.info("Compressing level ..."); + ZipUtil.makeBackupZip(levelPath, backupSavePath, backupFileName, backupMetadata); + } catch (ZipUtilException exception) { + PrintUtil.info("Infinite recursive of directory tree detected, backup was aborted."); + return new PrimitiveBackupFeedback(false, 0); + } + + // Get backup file size and return + return new PrimitiveBackupFeedback(true, FilesystemUtil.getFileSizeBytes(backupSavePath, backupFileName)); + } + + @Override + public boolean restore(String backupFileName, String levelDirectory, String backupSaveDirectory) throws IOException { + // Delete old level + PrintUtil.info("Server stopped. Deleting old level ..."); + File levelDirFile = new File(levelDirectory); + long startTime = System.currentTimeMillis(); + + int failedCounter = 0; + final int MAX_RETRY_TIMES = 20; + while (failedCounter < MAX_RETRY_TIMES) { + System.gc(); + if (!levelDirFile.delete() && levelDirFile.exists()) { + System.gc(); + forceDelete(levelDirFile); // Try to force delete. + } + if (!levelDirFile.exists()) + break; + ++failedCounter; + try { + Thread.sleep(500); + } catch (InterruptedException ignored) { + } + } + if (levelDirFile.exists()) { + PrintUtil.error(String.format("Cannot restore: failed to delete old level %s .", levelDirFile.getName())); + return false; + } + + // TODO: Refactor this to the concrete BackupMethod. + // Decompress archive + PrintUtil.info("Decompressing archived level ..."); + ZipUtil.unzip(Paths.get(backupSaveDirectory, backupFileName).toString(), levelDirectory, false); + long endTime = System.currentTimeMillis(); + PrintUtil.info(String.format("Restore complete! (%.2fs) Please restart the server manually.", (endTime - startTime) / 1000.0)); + PrintUtil.info("If you want to restart automatically after restoring, please check the manual at: https://github.com/keuin/KBackup-Fabric/blob/master/README.md"); + +// try { +// Thread.sleep(1000); +// } catch (InterruptedException ignored) { +// } + + return true; + } +} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/name/BackupFileNameEncoder.java b/src/main/java/com/keuin/kbackupfabric/util/backup/name/BackupFileNameEncoder.java index 83967b7..972403b 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/name/BackupFileNameEncoder.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/name/BackupFileNameEncoder.java @@ -1,6 +1,7 @@ package com.keuin.kbackupfabric.util.backup.name; import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; /** * Encode and decode backup file name for a specific backup type. @@ -38,12 +39,20 @@ public interface BackupFileNameEncoder { } class BackupBasicInformation { + public final String customName; public final LocalDateTime time; + private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm.ss"); + BackupBasicInformation(String customName, LocalDateTime time) { this.customName = customName; this.time = time; } + + @Override + public String toString() { + return String.format("%s, %s", customName, time.format(formatter)); + } } } diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/name/IncrementalBackupFileNameEncoder.java b/src/main/java/com/keuin/kbackupfabric/util/backup/name/IncrementalBackupFileNameEncoder.java new file mode 100644 index 0000000..ae54930 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/name/IncrementalBackupFileNameEncoder.java @@ -0,0 +1,33 @@ +package com.keuin.kbackupfabric.util.backup.name; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class IncrementalBackupFileNameEncoder implements BackupFileNameEncoder { + private static final String backupFileNamePrefix = "incremental-"; + private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss"); + + @Override + public String encode(String customName, LocalDateTime time) { + if (!isValidCustomName(customName)) + throw new IllegalArgumentException("Invalid custom name"); + String timeString = time.format(formatter); + return backupFileNamePrefix + "-" + timeString + "_" + customName + ".kbi"; + } + + @Override + public BackupFileNameEncoder.BackupBasicInformation decode(String fileName) { + Pattern pattern = Pattern.compile( + backupFileNamePrefix + "-" + "([0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2})_(.+)\\.kbi" + ); + Matcher matcher = pattern.matcher(fileName); + if (matcher.find()) { + String timeString = matcher.group(1); + String customName = matcher.group(2); + return new BackupFileNameEncoder.BackupBasicInformation(customName, LocalDateTime.parse(timeString, formatter)); + } + return null; + } +} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/name/PrimitiveBackupFileNameEncoder.java b/src/main/java/com/keuin/kbackupfabric/util/backup/name/PrimitiveBackupFileNameEncoder.java index 39c2403..bcba114 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/name/PrimitiveBackupFileNameEncoder.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/name/PrimitiveBackupFileNameEncoder.java @@ -6,7 +6,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; public class PrimitiveBackupFileNameEncoder implements BackupFileNameEncoder { - private static final String backupFileNamePrefix = "kbackup"; private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss"); -- cgit v1.2.3