diff options
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/util/backup')
16 files changed, 107 insertions, 274 deletions
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 d0c7500..c1aa5fe 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/BackupFilesystemUtil.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/BackupFilesystemUtil.java @@ -17,6 +17,7 @@ public final class BackupFilesystemUtil { private static final String backupSaveDirectoryName = "backups"; private static final String backupFileNamePrefix = "kbackup-"; + @Deprecated public static String getBackupFileNamePrefix() { return backupFileNamePrefix; } @@ -26,18 +27,18 @@ public final class BackupFilesystemUtil { return backupFileNamePrefix + backupName + ".zip"; } - @Deprecated - public static String getBackupName(String backupFileName) { - try { - if (backupFileName.matches(backupFileNamePrefix + ".+\\.zip")) - return backupFileName.substring(backupFileNamePrefix.length(), backupFileName.length() - 4); - } catch (IndexOutOfBoundsException ignored) { - } - return backupFileName; - } +// @Deprecated +// public static String getBackupName(String backupFileName) { +// try { +// if (backupFileName.matches(backupFileNamePrefix + ".+\\.zip")) +// return backupFileName.substring(backupFileNamePrefix.length(), backupFileName.length() - 4); +// } catch (IndexOutOfBoundsException ignored) { +// } +// return backupFileName; +// } - public static boolean isBackupNameValid(String backupName, MinecraftServer server) { - File backupFile = new File(getBackupSaveDirectory(server), getBackupFileName(backupName)); + public static boolean isBackupFileExists(String backupFileName, MinecraftServer server) { + File backupFile = new File(getBackupSaveDirectory(server), backupFileName); return backupFile.isFile(); } @@ -70,8 +71,8 @@ public final class BackupFilesystemUtil { return -1; } - public static String getFriendlyFileSizeString(long size) { - double fileSize = size * 1.0 / 1024 / 1024; // Default unit is MB + public static String getFriendlyFileSizeString(long sizeBytes) { + double fileSize = sizeBytes * 1.0 / 1024 / 1024; // Default unit is MB if (fileSize > 1000) //msgInfo(context, String.format("File size: %.2fGB", fileSize / 1024)); return String.format("%.2fGB", fileSize / 1024); diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/BackupType.java b/src/main/java/com/keuin/kbackupfabric/util/backup/BackupType.java deleted file mode 100644 index d02ce77..0000000 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/BackupType.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.keuin.kbackupfabric.util.backup; - -/** - * Representing the backup type. - * Should only be used in BackupFileNameBuilder and BackupFileNameFormatter - */ -@Deprecated -public enum BackupType { - - PRIMITIVE_ZIP_BACKUP("Primitive Zip Backup", "zip"), - OBJECT_TREE_BACKUP("Object Tree Backup", "incremental"); - - private final String friendlyName; // e.g. Primitive Zip Backup - private final String name; // e.g. zip - - BackupType(String friendlyName, String name) { - this.friendlyName = friendlyName; - this.name = name; - } - - /** - * Get name used in command. - * @return name (such as "zip", "incremental"). - */ - public String getName() { - return name; - } - - @Override - public String toString() { - return friendlyName; - } -} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/builder/BackupFileNameBuilder.java b/src/main/java/com/keuin/kbackupfabric/util/backup/builder/BackupFileNameBuilder.java deleted file mode 100644 index f57302c..0000000 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/builder/BackupFileNameBuilder.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.keuin.kbackupfabric.util.backup.builder; - - -import java.time.LocalDateTime; - -public interface BackupFileNameBuilder { - - static BackupFileNameBuilder primitiveZipBackup() { - return PrimitiveZipBackupFileNameBuilder.getInstance(); - } - - static BackupFileNameBuilder objectTreeBackup() { - return ObjectTreeBackupFileNameBuilder.getInstance(); - } - - /** - * Build a backup file name based on given information. - * @param time when the backup was created. - * @param backupName the custom name of this backup. Note that this should be a valid file name in current file system. - * @return the backup file name string. - */ - String build(LocalDateTime time, String backupName); - -} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/builder/ObjectTreeBackupFileNameBuilder.java b/src/main/java/com/keuin/kbackupfabric/util/backup/builder/ObjectTreeBackupFileNameBuilder.java deleted file mode 100644 index c3d8257..0000000 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/builder/ObjectTreeBackupFileNameBuilder.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.keuin.kbackupfabric.util.backup.builder; - -import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil; -import com.keuin.kbackupfabric.util.backup.BackupNameTimeFormatter; - -import java.time.LocalDateTime; - -public class ObjectTreeBackupFileNameBuilder implements BackupFileNameBuilder { - private static final ObjectTreeBackupFileNameBuilder instance = new ObjectTreeBackupFileNameBuilder(); - - public static ObjectTreeBackupFileNameBuilder getInstance() { - return instance; - } - - @Override - public String build(LocalDateTime time, String backupName) { - String timeString = BackupNameTimeFormatter.localDateTimeToString(time); - return String.format("%s%s_%s%s", BackupFilesystemUtil.getBackupFileNamePrefix(), timeString, backupName, ".json"); - } -} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/builder/PrimitiveZipBackupFileNameBuilder.java b/src/main/java/com/keuin/kbackupfabric/util/backup/builder/PrimitiveZipBackupFileNameBuilder.java deleted file mode 100644 index 6d8b5f5..0000000 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/builder/PrimitiveZipBackupFileNameBuilder.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.keuin.kbackupfabric.util.backup.builder; - -import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil; -import com.keuin.kbackupfabric.util.backup.BackupNameTimeFormatter; - -import java.time.LocalDateTime; - -public class PrimitiveZipBackupFileNameBuilder implements BackupFileNameBuilder { - - private static final PrimitiveZipBackupFileNameBuilder instance = new PrimitiveZipBackupFileNameBuilder(); - - public static PrimitiveZipBackupFileNameBuilder getInstance() { - return instance; - } - - @Override - public String build(LocalDateTime time, String backupName) { - String timeString = BackupNameTimeFormatter.localDateTimeToString(time); - return String.format("%s%s_%s%s", BackupFilesystemUtil.getBackupFileNamePrefix(), timeString, backupName, ".zip"); - } - -} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/BackupFileNameFormatter.java b/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/BackupFileNameFormatter.java deleted file mode 100644 index a437629..0000000 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/BackupFileNameFormatter.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.keuin.kbackupfabric.util.backup.formatter; - -import com.keuin.kbackupfabric.util.backup.BackupNameTimeFormatter; - -import java.time.LocalDateTime; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public interface BackupFileNameFormatter { - - BackupFileName format(String fileName); - - class BackupFileName { - public final LocalDateTime time; - public final String name; - - public BackupFileName(LocalDateTime time, String name) { - this.time = time; - this.name = name; - } - } - - static BackupFileNameFormatter objectTreeBackup() { - return ObjectTreeBackupFileNameFormatter.getInstance(); - } - - static BackupFileNameFormatter primitiveZipBackup() { - return PrimitiveZipBackupFileNameFormatter.getInstance(); - } - -} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/ObjectTreeBackupFileNameFormatter.java b/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/ObjectTreeBackupFileNameFormatter.java deleted file mode 100644 index 08805b2..0000000 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/ObjectTreeBackupFileNameFormatter.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.keuin.kbackupfabric.util.backup.formatter; - -import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil; -import com.keuin.kbackupfabric.util.backup.BackupNameTimeFormatter; -import org.spongepowered.asm.mixin.Overwrite; - -import java.time.LocalDateTime; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class ObjectTreeBackupFileNameFormatter implements BackupFileNameFormatter { - - private static final ObjectTreeBackupFileNameFormatter instance = new ObjectTreeBackupFileNameFormatter(); - - public static ObjectTreeBackupFileNameFormatter getInstance() { - return instance; - } - - @Override - public BackupFileNameFormatter.BackupFileName format(String fileName) { - LocalDateTime time = getTime(fileName); - String name = getBackupName(fileName); - return new BackupFileNameFormatter.BackupFileName(time,name); - } - - private LocalDateTime getTime(String fileName) { - Matcher matcher = Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2}").matcher(fileName); - if (matcher.find()) { - String timeString = matcher.group(0); - return BackupNameTimeFormatter.timeStringToLocalDateTime(timeString); - } - return null; - } - - private String getBackupName(String backupFileName) { - try { - if (backupFileName.matches(BackupFilesystemUtil.getBackupFileNamePrefix() + ".+\\.json")) - return backupFileName.substring(BackupFilesystemUtil.getBackupFileNamePrefix().length(), backupFileName.length() - 4); - } catch (IndexOutOfBoundsException ignored) { - } - return backupFileName; - } - -} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/PrimitiveZipBackupFileNameFormatter.java b/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/PrimitiveZipBackupFileNameFormatter.java deleted file mode 100644 index 2d50d17..0000000 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/PrimitiveZipBackupFileNameFormatter.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.keuin.kbackupfabric.util.backup.formatter; - -import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil; -import com.keuin.kbackupfabric.util.backup.BackupNameTimeFormatter; - -import java.time.LocalDateTime; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class PrimitiveZipBackupFileNameFormatter implements BackupFileNameFormatter { - - private static final PrimitiveZipBackupFileNameFormatter instance = new PrimitiveZipBackupFileNameFormatter(); - - public static PrimitiveZipBackupFileNameFormatter getInstance() { - return instance; - } - - @Override - public BackupFileNameFormatter.BackupFileName format(String fileName) { - LocalDateTime time = getTime(fileName); - String name = getBackupName(fileName); - return new BackupFileNameFormatter.BackupFileName(time,name); - } - - private LocalDateTime getTime(String fileName) { - Matcher matcher = Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2}").matcher(fileName); - if (matcher.find()) { - String timeString = matcher.group(0); - return BackupNameTimeFormatter.timeStringToLocalDateTime(timeString); - } - return null; - } - - private String getBackupName(String backupFileName) { - try { - if (backupFileName.matches(BackupFilesystemUtil.getBackupFileNamePrefix() + ".+\\.zip")) - return backupFileName.substring(BackupFilesystemUtil.getBackupFileNamePrefix().length(), backupFileName.length() - 4); - } catch (IndexOutOfBoundsException ignored) { - } - return backupFileName; - } - -} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionFactory.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionFactory.java index ea9edd9..ac87883 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionFactory.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionFactory.java @@ -1,6 +1,6 @@ package com.keuin.kbackupfabric.util.backup.incremental; -import com.keuin.kbackupfabric.util.backup.incremental.identifier.FileIdentifierFactory; +import com.keuin.kbackupfabric.util.backup.incremental.identifier.FileIdentifierProvider; import com.keuin.kbackupfabric.util.backup.incremental.identifier.ObjectIdentifier; import java.io.File; @@ -17,9 +17,9 @@ import java.util.*; */ public class ObjectCollectionFactory <T extends ObjectIdentifier> { - private final FileIdentifierFactory<T> identifierFactory; + private final FileIdentifierProvider<T> identifierFactory; - public ObjectCollectionFactory(FileIdentifierFactory<T> identifierFactory) { + public ObjectCollectionFactory(FileIdentifierProvider<T> identifierFactory) { this.identifierFactory = identifierFactory; } diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionSerializer.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionSerializer.java index cc77837..6f9b792 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionSerializer.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionSerializer.java @@ -3,6 +3,9 @@ package com.keuin.kbackupfabric.util.backup.incremental; import java.io.*; import java.util.Objects; +/** + * Serialize and deserialize ObjectCollection from/to the disk file. + */ public class ObjectCollectionSerializer { public static ObjectCollection fromFile(File file) throws IOException { Objects.requireNonNull(file); diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/FileIdentifierFactory.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/FileIdentifierProvider.java index 34ad9a0..9a03371 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/FileIdentifierFactory.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/FileIdentifierProvider.java @@ -3,6 +3,6 @@ package com.keuin.kbackupfabric.util.backup.incremental.identifier; import java.io.File; import java.io.IOException; -public interface FileIdentifierFactory<T extends ObjectIdentifier> { +public interface FileIdentifierProvider<T extends ObjectIdentifier> { T fromFile(File file) throws IOException; } diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256Identifier.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256Identifier.java index 9ecf2d3..cbf1bb9 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256Identifier.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256Identifier.java @@ -15,7 +15,7 @@ public class Sha256Identifier extends SingleHashIdentifier { private static final int SHA256_LENGTH = 32; private static final Sha256Identifier DUMMY = new Sha256Identifier(new byte[SHA256_LENGTH]); // only for using its hash method - private static final FileIdentifierFactory<Sha256Identifier> factory = Sha256Identifier::fromFile; + private static final FileIdentifierProvider<Sha256Identifier> factory = Sha256Identifier::fromFile; public static Sha256Identifier fromFile(File file) throws IOException { if (!Objects.requireNonNull(file).isFile()) { @@ -24,7 +24,7 @@ public class Sha256Identifier extends SingleHashIdentifier { return new Sha256Identifier(DUMMY.hash(file)); } - public static FileIdentifierFactory<Sha256Identifier> getFactory() { + public static FileIdentifierProvider<Sha256Identifier> getFactory() { return factory; } 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 new file mode 100644 index 0000000..83967b7 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/name/BackupFileNameEncoder.java @@ -0,0 +1,49 @@ +package com.keuin.kbackupfabric.util.backup.name; + +import java.time.LocalDateTime; + +/** + * Encode and decode backup file name for a specific backup type. + */ +public interface BackupFileNameEncoder { + + /** + * Construct full backup file name from custom name and creation time. + * @param customName the custom name. If the custom name contains invalid chars, an exception will be thrown. + * @param time the creation time. + * @return the file name. + */ + String encode(String customName, LocalDateTime time); + + /** + * Extract custom and backup time from backup file name. + * @param fileName the backup file name. + * @return the information. If the given file name is invalid, return null. + */ + BackupBasicInformation decode(String fileName); + + /** + * Check if the given string is a valid custom backup name. + * @param customName the custom backup name. + * @return if the name is valid. + */ + default boolean isValidCustomName(String customName) { + final char[] ILLEGAL_CHARACTERS = {'/', '\n', '\r', '\t', '\0', '\f', '`', '?', '*', '\\', '<', '>', '|', '\"', ':'}; + for (char c : ILLEGAL_CHARACTERS) { + if (customName.contains(String.valueOf(c))) { + return false; + } + } + return true; + } + + class BackupBasicInformation { + public final String customName; + public final LocalDateTime time; + + BackupBasicInformation(String customName, LocalDateTime time) { + this.customName = customName; + this.time = time; + } + } +} 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 new file mode 100644 index 0000000..39c2403 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/name/PrimitiveBackupFileNameEncoder.java @@ -0,0 +1,34 @@ +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 PrimitiveBackupFileNameEncoder implements BackupFileNameEncoder { + + private static final String backupFileNamePrefix = "kbackup"; + 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 + ".zip"; + } + + @Override + public 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})_(.+)\\.zip" + ); + Matcher matcher = pattern.matcher(fileName); + if (matcher.find()) { + String timeString = matcher.group(1); + String customName = matcher.group(2); + return new BackupBasicInformation(customName, LocalDateTime.parse(timeString, formatter)); + } + return null; + } +} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupMethodSuggestionProvider.java b/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupMethodSuggestionProvider.java deleted file mode 100644 index 320d9bf..0000000 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupMethodSuggestionProvider.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.keuin.kbackupfabric.util.backup.suggestion; - -import com.keuin.kbackupfabric.util.backup.BackupType; -import com.mojang.brigadier.suggestion.SuggestionProvider; -import com.mojang.brigadier.suggestion.Suggestions; -import com.mojang.brigadier.suggestion.SuggestionsBuilder; -import net.minecraft.server.command.ServerCommandSource; - -import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; -import java.util.Locale; -import java.util.concurrent.CompletableFuture; - -public class BackupMethodSuggestionProvider { - - private static final List<String> suggestions = Arrays.asList( - BackupType.OBJECT_TREE_BACKUP.getName(), - BackupType.PRIMITIVE_ZIP_BACKUP.getName() - ); // All backup methods - - public static SuggestionProvider<ServerCommandSource> getProvider() { - return (context, builder) -> getCompletableFuture(builder); - } - - private static CompletableFuture<Suggestions> getCompletableFuture(SuggestionsBuilder builder) { - String remaining = builder.getRemaining().toLowerCase(Locale.ROOT); - for (String string : suggestions) { // Iterate through the supplied list - if (string.toLowerCase(Locale.ROOT).startsWith(remaining)) { - builder.suggest(string); // Add every single entry to suggestions list. - } - } - return builder.buildFuture(); // Create the CompletableFuture containing all the suggestions - } - -} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupNameSuggestionProvider.java b/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupNameSuggestionProvider.java index f6f4056..01152c2 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupNameSuggestionProvider.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupNameSuggestionProvider.java @@ -1,6 +1,5 @@ package com.keuin.kbackupfabric.util.backup.suggestion; -import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil; import com.mojang.brigadier.suggestion.SuggestionProvider; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; @@ -38,7 +37,7 @@ public class BackupNameSuggestionProvider { if (files == null) return; for (File f : files) - candidateCacheList.add(BackupFilesystemUtil.getBackupName(f.getName())); + candidateCacheList.add(f.getName()); cacheUpdateTime = System.currentTimeMillis(); } catch (NullPointerException ignored) { } |