From 8b05c48d76bbb05c2b440e57df1d9b3323edc598 Mon Sep 17 00:00:00 2001 From: Keuin Date: Thu, 17 Dec 2020 00:21:13 +0800 Subject: Implementing incremental backup. (in progress) --- .../java/com/keuin/kbackupfabric/KBCommands.java | 13 +++++---- .../kbackupfabric/operation/BackupOperation.java | 8 ++---- .../util/backup/BackupFilesystemUtil.java | 2 +- .../builder/ObjectTreeBackupFileNameBuilder.java | 4 +-- .../builder/PrimitiveZipBackupFileNameBuilder.java | 4 +-- .../util/backup/inc/ObjectCollectionManager.java | 11 ++++++++ .../backup/inc/identifier/ObjectIdentifier.java | 9 ++++++ .../inc/identifier/SingleHashIdentifier.java | 32 ++++++++++++++++++++++ 8 files changed, 67 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/inc/ObjectCollectionManager.java create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/inc/identifier/ObjectIdentifier.java create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/inc/identifier/SingleHashIdentifier.java (limited to 'src/main/java/com/keuin') diff --git a/src/main/java/com/keuin/kbackupfabric/KBCommands.java b/src/main/java/com/keuin/kbackupfabric/KBCommands.java index bc04291..571333f 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBCommands.java +++ b/src/main/java/com/keuin/kbackupfabric/KBCommands.java @@ -8,21 +8,24 @@ import com.keuin.kbackupfabric.operation.abstracts.i.Invokable; import com.keuin.kbackupfabric.operation.backup.BackupMethod; import com.keuin.kbackupfabric.operation.backup.IncrementalBackupMethod; import com.keuin.kbackupfabric.operation.backup.PrimitiveBackupMethod; +import com.keuin.kbackupfabric.util.PrintUtil; import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil; +import com.keuin.kbackupfabric.util.backup.BackupNameTimeFormatter; import com.keuin.kbackupfabric.util.backup.BackupType; import com.keuin.kbackupfabric.util.backup.suggestion.BackupNameSuggestionProvider; -import com.keuin.kbackupfabric.util.backup.BackupNameTimeFormatter; -import com.keuin.kbackupfabric.util.PrintUtil; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.context.CommandContext; import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.ServerCommandSource; import java.io.File; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; -import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.*; import static com.keuin.kbackupfabric.util.PrintUtil.*; +import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.*; public final class KBCommands { @@ -298,7 +301,7 @@ public final class KBCommands { } else { ++i; } - msgInfo(context, String.format("The most recent backup: [%d] %s , size: %s", i, backupName, humanFileSize(prevBackupFile.length()))); + msgInfo(context, String.format("The most recent backup: [%d] %s , size: %s", i, backupName, getFriendlyFileSizeString(prevBackupFile.length()))); } catch (NullPointerException e) { msgInfo(context, "There are no backups available."); } catch (SecurityException ignored) { diff --git a/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java index d03c347..465b293 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java @@ -1,12 +1,8 @@ package com.keuin.kbackupfabric.operation; -import com.keuin.kbackupfabric.exception.ZipUtilException; -import com.keuin.kbackupfabric.metadata.BackupMetadata; import com.keuin.kbackupfabric.operation.abstracts.InvokableAsyncBlockingOperation; import com.keuin.kbackupfabric.operation.backup.BackupMethod; import com.keuin.kbackupfabric.util.PrintUtil; -import com.keuin.kbackupfabric.util.backup.builder.BackupFileNameBuilder; -import com.keuin.kbackupfabric.util.backup.formatter.BackupFileNameFormatter; import com.mojang.brigadier.context.CommandContext; import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.ServerCommandSource; @@ -17,8 +13,8 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; -import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.*; import static com.keuin.kbackupfabric.util.PrintUtil.msgInfo; +import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.*; public class BackupOperation extends InvokableAsyncBlockingOperation { @@ -64,7 +60,7 @@ public class BackupOperation extends InvokableAsyncBlockingOperation { long timeElapsedMillis = System.currentTimeMillis() - startTime; String msgText = String.format("Backup finished. Time elapsed: %.2fs.", timeElapsedMillis / 1000.0); File backupZipFile = new File(backupSaveDirectory, backupFileName); - msgText += String.format(" File size: %s.", humanFileSize(result.getBackupSizeBytes())); + msgText += String.format(" File size: %s.", getFriendlyFileSizeString(result.getBackupSizeBytes())); PrintUtil.msgInfo(context, msgText, true); } else { // failed 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 54c2f58..d0c7500 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/BackupFilesystemUtil.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/BackupFilesystemUtil.java @@ -70,7 +70,7 @@ public final class BackupFilesystemUtil { return -1; } - public static String humanFileSize(long size) { + public static String getFriendlyFileSizeString(long size) { double fileSize = size * 1.0 / 1024 / 1024; // Default unit is MB if (fileSize > 1000) //msgInfo(context, String.format("File size: %.2fGB", fileSize / 1024)); 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 index 3c15741..c3d8257 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/builder/ObjectTreeBackupFileNameBuilder.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/builder/ObjectTreeBackupFileNameBuilder.java @@ -8,13 +8,13 @@ import java.time.LocalDateTime; public class ObjectTreeBackupFileNameBuilder implements BackupFileNameBuilder { private static final ObjectTreeBackupFileNameBuilder instance = new ObjectTreeBackupFileNameBuilder(); - public static ObjectTreeBackupFileNameBuilder getInstance() { + 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"); + 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 index f910c37..6d8b5f5 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/builder/PrimitiveZipBackupFileNameBuilder.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/builder/PrimitiveZipBackupFileNameBuilder.java @@ -9,14 +9,14 @@ public class PrimitiveZipBackupFileNameBuilder implements BackupFileNameBuilder private static final PrimitiveZipBackupFileNameBuilder instance = new PrimitiveZipBackupFileNameBuilder(); - public static PrimitiveZipBackupFileNameBuilder getInstance() { + 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"); + return String.format("%s%s_%s%s", BackupFilesystemUtil.getBackupFileNamePrefix(), timeString, backupName, ".zip"); } } diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/inc/ObjectCollectionManager.java b/src/main/java/com/keuin/kbackupfabric/util/backup/inc/ObjectCollectionManager.java new file mode 100644 index 0000000..9560261 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/inc/ObjectCollectionManager.java @@ -0,0 +1,11 @@ +package com.keuin.kbackupfabric.util.backup.inc; + +/** + * Incremental backup is implemented as git-like file collection. + * Files are called `objects`, the collection contains all files distinguished by their + * identifiers. Usually, identifier is the combination of hash and other short information (such as size and another hash). + * The identifier should use hashes that are strong enough, to prevent possible collisions. + */ +public class ObjectCollectionManager { + +} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/inc/identifier/ObjectIdentifier.java b/src/main/java/com/keuin/kbackupfabric/util/backup/inc/identifier/ObjectIdentifier.java new file mode 100644 index 0000000..079f49d --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/inc/identifier/ObjectIdentifier.java @@ -0,0 +1,9 @@ +package com.keuin.kbackupfabric.util.backup.inc.identifier; + +/** + * The identifier distinguishing files in the object collection. + * It should be based on cryptographic hash function in order to prevent possible attacks to the backup system. + * All identifiers should be immutable and implement their own equals method. + */ +public interface ObjectIdentifier { +} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/inc/identifier/SingleHashIdentifier.java b/src/main/java/com/keuin/kbackupfabric/util/backup/inc/identifier/SingleHashIdentifier.java new file mode 100644 index 0000000..50b23bb --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/inc/identifier/SingleHashIdentifier.java @@ -0,0 +1,32 @@ +package com.keuin.kbackupfabric.util.backup.inc.identifier; + +import java.io.File; +import java.util.Arrays; + +/** + * A simple identifier based on a single hash function. + */ +public abstract class SingleHashIdentifier implements ObjectIdentifier { + + private final byte[] hash; + + protected SingleHashIdentifier(byte[] hash) { + this.hash = Arrays.copyOf(hash, hash.length); + } + + /** + * The hash function. + * + * @param file the file to be hashed. + * @return the hash bytes. + */ + protected abstract byte[] hash(File file); + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof SingleHashIdentifier)) { + return false; + } + return Arrays.equals(hash, ((SingleHashIdentifier) obj).hash); + } +} -- cgit v1.2.3 From afd26cdd12fef4bd2aafa2ac8d708e18d277a2fe Mon Sep 17 00:00:00 2001 From: Keuin Date: Tue, 12 Jan 2021 12:51:23 +0800 Subject: Implement object collection(not tested) and sha256(tested) --- .../com/keuin/kbackupfabric/util/BytesUtil.java | 16 ++++++ .../util/backup/inc/ObjectCollectionManager.java | 11 ---- .../backup/inc/identifier/ObjectIdentifier.java | 9 --- .../inc/identifier/SingleHashIdentifier.java | 32 ----------- .../util/backup/incremental/ObjectCollection.java | 45 +++++++++++++++ .../incremental/ObjectCollectionFactory.java | 48 ++++++++++++++++ .../identifier/FileIdentifierFactory.java | 8 +++ .../incremental/identifier/ObjectIdentifier.java | 10 ++++ .../incremental/identifier/Sha256Identifier.java | 67 ++++++++++++++++++++++ .../identifier/SingleHashIdentifier.java | 40 +++++++++++++ .../identifier/Sha256IdentifierTest.java | 23 ++++++++ src/test/sha256 | 1 + 12 files changed, 258 insertions(+), 52 deletions(-) create mode 100644 src/main/java/com/keuin/kbackupfabric/util/BytesUtil.java delete mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/inc/ObjectCollectionManager.java delete mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/inc/identifier/ObjectIdentifier.java delete mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/inc/identifier/SingleHashIdentifier.java create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollection.java create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionFactory.java create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/FileIdentifierFactory.java create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256Identifier.java create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java create mode 100644 src/test/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256IdentifierTest.java create mode 100644 src/test/sha256 (limited to 'src/main/java/com/keuin') diff --git a/src/main/java/com/keuin/kbackupfabric/util/BytesUtil.java b/src/main/java/com/keuin/kbackupfabric/util/BytesUtil.java new file mode 100644 index 0000000..6ded7b8 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/BytesUtil.java @@ -0,0 +1,16 @@ +package com.keuin.kbackupfabric.util; + +import java.nio.charset.StandardCharsets; + +public class BytesUtil { + private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII); + public static String bytesToHex(byte[] bytes) { + byte[] hexChars = new byte[bytes.length * 2]; + for (int j = 0; j < bytes.length; j++) { + int v = bytes[j] & 0xFF; + hexChars[j * 2] = HEX_ARRAY[v >>> 4]; + hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; + } + return new String(hexChars, StandardCharsets.UTF_8); + } +} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/inc/ObjectCollectionManager.java b/src/main/java/com/keuin/kbackupfabric/util/backup/inc/ObjectCollectionManager.java deleted file mode 100644 index 9560261..0000000 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/inc/ObjectCollectionManager.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.keuin.kbackupfabric.util.backup.inc; - -/** - * Incremental backup is implemented as git-like file collection. - * Files are called `objects`, the collection contains all files distinguished by their - * identifiers. Usually, identifier is the combination of hash and other short information (such as size and another hash). - * The identifier should use hashes that are strong enough, to prevent possible collisions. - */ -public class ObjectCollectionManager { - -} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/inc/identifier/ObjectIdentifier.java b/src/main/java/com/keuin/kbackupfabric/util/backup/inc/identifier/ObjectIdentifier.java deleted file mode 100644 index 079f49d..0000000 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/inc/identifier/ObjectIdentifier.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.keuin.kbackupfabric.util.backup.inc.identifier; - -/** - * The identifier distinguishing files in the object collection. - * It should be based on cryptographic hash function in order to prevent possible attacks to the backup system. - * All identifiers should be immutable and implement their own equals method. - */ -public interface ObjectIdentifier { -} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/inc/identifier/SingleHashIdentifier.java b/src/main/java/com/keuin/kbackupfabric/util/backup/inc/identifier/SingleHashIdentifier.java deleted file mode 100644 index 50b23bb..0000000 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/inc/identifier/SingleHashIdentifier.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.keuin.kbackupfabric.util.backup.inc.identifier; - -import java.io.File; -import java.util.Arrays; - -/** - * A simple identifier based on a single hash function. - */ -public abstract class SingleHashIdentifier implements ObjectIdentifier { - - private final byte[] hash; - - protected SingleHashIdentifier(byte[] hash) { - this.hash = Arrays.copyOf(hash, hash.length); - } - - /** - * The hash function. - * - * @param file the file to be hashed. - * @return the hash bytes. - */ - protected abstract byte[] hash(File file); - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof SingleHashIdentifier)) { - return false; - } - return Arrays.equals(hash, ((SingleHashIdentifier) obj).hash); - } -} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollection.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollection.java new file mode 100644 index 0000000..d5d766a --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollection.java @@ -0,0 +1,45 @@ +package com.keuin.kbackupfabric.util.backup.incremental; + +import com.keuin.kbackupfabric.util.backup.incremental.identifier.ObjectIdentifier; + +import java.util.Objects; +import java.util.Set; + +public class ObjectCollection { + private final String name; + private final Set elements; + private final Set subCollections; + + ObjectCollection(String name, Set elements, Set subCollections) { + this.name = Objects.requireNonNull(name); + this.elements = Objects.requireNonNull(elements); + this.subCollections = Objects.requireNonNull(subCollections); + } + + public String getName() { + return name; + } + + public Set getElements() { + return elements; + } + + public Set getSubCollections() { + return subCollections; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ObjectCollection that = (ObjectCollection) o; + return name.equals(that.name) && + elements.equals(that.elements) && + subCollections.equals(that.subCollections); + } + + @Override + public int hashCode() { + return Objects.hash(name, elements, subCollections); + } +} 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 new file mode 100644 index 0000000..8b974db --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionFactory.java @@ -0,0 +1,48 @@ +package com.keuin.kbackupfabric.util.backup.incremental; + +import com.keuin.kbackupfabric.util.backup.incremental.identifier.FileIdentifierFactory; +import com.keuin.kbackupfabric.util.backup.incremental.identifier.ObjectIdentifier; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Objects; +import java.util.Set; + +/** + * Incremental backup is implemented as git-like file collection. + * Files are called `objects`, the collection contains all files distinguished by their + * identifiers. Usually, identifier is the combination of hash and other short information (such as size and another hash). + * The identifier should use hashes that are strong enough, to prevent possible collisions. + */ +public class ObjectCollectionFactory { + private final FileIdentifierFactory identifierFactory; + + public ObjectCollectionFactory(FileIdentifierFactory identifierFactory) { + this.identifierFactory = identifierFactory; + } + + public ObjectCollection fromDirectory(File directory) throws IOException { + final Set subFiles = new HashSet<>(); + final Set subCollections = new HashSet<>(); + + if (!Objects.requireNonNull(directory).isDirectory()) + throw new IllegalArgumentException("given file is not a directory"); + + for (Iterator iter = Files.walk(directory.toPath()).iterator(); iter.hasNext();) { + Path path = iter.next(); + File file = path.toFile(); + if (file.isDirectory()) { + subCollections.add(fromDirectory(file)); + } else { + subFiles.add(identifierFactory.fromFile(file)); + } + } + + return new ObjectCollection(directory.getName(), subFiles, subCollections); + } + +} 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/FileIdentifierFactory.java new file mode 100644 index 0000000..34ad9a0 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/FileIdentifierFactory.java @@ -0,0 +1,8 @@ +package com.keuin.kbackupfabric.util.backup.incremental.identifier; + +import java.io.File; +import java.io.IOException; + +public interface FileIdentifierFactory { + T fromFile(File file) throws IOException; +} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java new file mode 100644 index 0000000..62798e1 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java @@ -0,0 +1,10 @@ +package com.keuin.kbackupfabric.util.backup.incremental.identifier; + +/** + * The identifier distinguishing files in the object collection. + * It should be based on cryptographic hash function in order to prevent possible attacks to the backup system. + * All identifiers should be immutable and implement their own equals method. + */ +public interface ObjectIdentifier { + String getIdentification(); +} 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 new file mode 100644 index 0000000..9f56b0e --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256Identifier.java @@ -0,0 +1,67 @@ +package com.keuin.kbackupfabric.util.backup.incremental.identifier; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Objects; + +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 factory = Sha256Identifier::fromFile; + + public static Sha256Identifier fromFile(File file) throws IOException { + if (!Objects.requireNonNull(file).isFile()) { + throw new IllegalArgumentException("file is not a file"); + } + return new Sha256Identifier(DUMMY.hash(file)); + } + + public static FileIdentifierFactory getFactory() { + return factory; + } + + protected Sha256Identifier(byte[] hash) { + super(hash); + Objects.requireNonNull(hash); + if (hash.length != SHA256_LENGTH) { + throw new IllegalStateException(String.format("SHA256 must be %d bytes", SHA256_LENGTH)); + } + } + + @Override + protected byte[] hash(File file) throws IOException { + try { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + + FileInputStream inputStream = new FileInputStream(file); + + // This does not work. I don't know why +// FileChannel channel = inputStream.getChannel(); +// ByteBuffer buffer = ByteBuffer.allocate(128); +// int readLength; +// while ((readLength = channel.read(buffer)) > 0) +// digest.update(buffer); + + // This also works, without warnings + byte[] readBuffer = new byte[1024 * 1024]; + int readLength; + while ((readLength = inputStream.read(readBuffer)) > 0) + digest.update(readBuffer,0, readLength); + + // The below lines also works, but the IDE will complain about the while loop +// DigestInputStream digestInputStream = new DigestInputStream(inputStream, digest); +// while(digestInputStream.read() > 0) +// ; + + return digest.digest(); + } catch (NoSuchAlgorithmException ignored) { + // this shouldn't happen + return new byte[SHA256_LENGTH]; + } + } + +} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java new file mode 100644 index 0000000..62ba47c --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java @@ -0,0 +1,40 @@ +package com.keuin.kbackupfabric.util.backup.incremental.identifier; + +import com.keuin.kbackupfabric.util.BytesUtil; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; + +/** + * A simple identifier based on a single hash function. + */ +public abstract class SingleHashIdentifier implements ObjectIdentifier { + + private final byte[] hash; + + protected SingleHashIdentifier(byte[] hash) { + this.hash = Arrays.copyOf(hash, hash.length); + } + + /** + * The hash function. + * + * @param file the file to be hashed. + * @return the hash bytes. + */ + protected abstract byte[] hash(File file) throws IOException; + + @Override + public String getIdentification() { + return BytesUtil.bytesToHex(hash); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof SingleHashIdentifier)) { + return false; + } + return Arrays.equals(hash, ((SingleHashIdentifier) obj).hash); + } +} diff --git a/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256IdentifierTest.java b/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256IdentifierTest.java new file mode 100644 index 0000000..2296e01 --- /dev/null +++ b/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256IdentifierTest.java @@ -0,0 +1,23 @@ +package com.keuin.kbackupfabric.util.backup.incremental.identifier; + +import org.junit.Test; + +import java.io.File; +import java.io.IOException; + +import static org.junit.Assert.assertEquals; + +public class Sha256IdentifierTest { + + @Test + public void fromFile() { + try { + Sha256Identifier sha256 = Sha256Identifier.fromFile(new File("./src/test/sha256")); + String str = sha256.getIdentification().toUpperCase(); + assertEquals("315F5BDB76D078C43B8AC0064E4A0164612B1FCE77C869345BFC94C75894EDD3", str); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} \ No newline at end of file diff --git a/src/test/sha256 b/src/test/sha256 new file mode 100644 index 0000000..5dd01c1 --- /dev/null +++ b/src/test/sha256 @@ -0,0 +1 @@ +Hello, world! \ No newline at end of file -- cgit v1.2.3 From 4a52f5e6ce06cb6717510c6a975d5490be627c98 Mon Sep 17 00:00:00 2001 From: Keuin Date: Tue, 12 Jan 2021 15:03:23 +0800 Subject: Add unit test for Sha256Identifier and ObjectCollection --- .../util/backup/incremental/ObjectCollection.java | 45 ++++++++---- .../incremental/ObjectCollectionFactory.java | 17 ++--- .../util/backup/incremental/ObjectElement.java | 59 +++++++++++++++ .../incremental/identifier/ObjectIdentifier.java | 1 + .../incremental/identifier/Sha256Identifier.java | 4 + .../identifier/SingleHashIdentifier.java | 1 + .../incremental/ObjectCollectionFactoryTest.java | 81 +++++++++++++++++++++ .../identifier/Sha256IdentifierTest.java | 4 +- src/test/sha256 | 1 - testfile/ObjectCollectionFactoryTest/1/11/111/a | Bin 0 -> 1024 bytes testfile/ObjectCollectionFactoryTest/1/11/111/b | Bin 0 -> 1024 bytes testfile/ObjectCollectionFactoryTest/1/a | Bin 0 -> 1024 bytes testfile/ObjectCollectionFactoryTest/1/b | Bin 0 -> 1024 bytes testfile/ObjectCollectionFactoryTest/a | Bin 0 -> 1024 bytes testfile/ObjectCollectionFactoryTest/b | Bin 0 -> 1024 bytes testfile/Sha256IdentifierTest | 1 + 16 files changed, 190 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectElement.java create mode 100644 src/test/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionFactoryTest.java delete mode 100644 src/test/sha256 create mode 100644 testfile/ObjectCollectionFactoryTest/1/11/111/a create mode 100644 testfile/ObjectCollectionFactoryTest/1/11/111/b create mode 100644 testfile/ObjectCollectionFactoryTest/1/a create mode 100644 testfile/ObjectCollectionFactoryTest/1/b create mode 100644 testfile/ObjectCollectionFactoryTest/a create mode 100644 testfile/ObjectCollectionFactoryTest/b create mode 100644 testfile/Sha256IdentifierTest (limited to 'src/main/java/com/keuin') diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollection.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollection.java index d5d766a..e0ef4f7 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollection.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollection.java @@ -1,31 +1,50 @@ package com.keuin.kbackupfabric.util.backup.incremental; -import com.keuin.kbackupfabric.util.backup.incremental.identifier.ObjectIdentifier; - -import java.util.Objects; -import java.util.Set; +import java.util.*; public class ObjectCollection { private final String name; - private final Set elements; - private final Set subCollections; + private final Map elements; + private final Map subCollections; - ObjectCollection(String name, Set elements, Set subCollections) { + ObjectCollection(String name, Set elements, Map subCollections) { this.name = Objects.requireNonNull(name); - this.elements = Objects.requireNonNull(elements); - this.subCollections = Objects.requireNonNull(subCollections); + this.elements = new HashMap<>(); + for (ObjectElement e : elements) { + Objects.requireNonNull(e); + if (this.elements.put(e.getName(), e) != null) { + throw new IllegalStateException("elements conflict with the same name"); + } + } + this.subCollections = new HashMap<>(Objects.requireNonNull(subCollections)); } public String getName() { return name; } - public Set getElements() { - return elements; + public Set getElementSet() { + return new HashSet<>(elements.values()); + } + + public Map getElementMap() { + return Collections.unmodifiableMap(elements); + } + + public ObjectElement getElement(String name) { + return elements.get(name); + } + + public Set getSubCollectionSet() { + return new HashSet<>(subCollections.values()); + } + + public Map getSubCollectionMap() { + return Collections.unmodifiableMap(subCollections); } - public Set getSubCollections() { - return subCollections; + public ObjectCollection getSubCollection(String name) { + return subCollections.get(name); } @Override 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 8b974db..be9e426 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 @@ -7,10 +7,7 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Objects; -import java.util.Set; +import java.util.*; /** * Incremental backup is implemented as git-like file collection. @@ -26,19 +23,21 @@ public class ObjectCollectionFactory { } public ObjectCollection fromDirectory(File directory) throws IOException { - final Set subFiles = new HashSet<>(); - final Set subCollections = new HashSet<>(); + final Set subFiles = new HashSet<>(); + final Map subCollections = new HashMap<>(); if (!Objects.requireNonNull(directory).isDirectory()) throw new IllegalArgumentException("given file is not a directory"); - for (Iterator iter = Files.walk(directory.toPath()).iterator(); iter.hasNext();) { + for (Iterator iter = Files.walk(directory.toPath(), 1).iterator(); iter.hasNext();) { Path path = iter.next(); + if (Files.isSameFile(path, directory.toPath())) + continue; File file = path.toFile(); if (file.isDirectory()) { - subCollections.add(fromDirectory(file)); + subCollections.put(file.getName(), fromDirectory(file)); } else { - subFiles.add(identifierFactory.fromFile(file)); + subFiles.add(new ObjectElement(file.getName(), identifierFactory.fromFile(file))); } } diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectElement.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectElement.java new file mode 100644 index 0000000..1232fb9 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectElement.java @@ -0,0 +1,59 @@ +package com.keuin.kbackupfabric.util.backup.incremental; + +import com.keuin.kbackupfabric.util.backup.incremental.identifier.ObjectIdentifier; + +import java.util.Objects; + +/** + * Representing a file in a ObjectCollection. + * Immutable. + */ +public class ObjectElement { + private final String name; + private final ObjectIdentifier identifier; + + public ObjectElement(String name, ObjectIdentifier identifier) { + Objects.requireNonNull(name); + Objects.requireNonNull(identifier); + this.name = name; + this.identifier = identifier; + } + + /** + * Get file name. + * @return the file name. + */ + public String getName() { + return name; + } + + /** + * Get file identifier, which is considered to be different between files with different contents. + * @return the identifier. + */ + public ObjectIdentifier getIdentifier() { + return identifier; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ObjectElement that = (ObjectElement) o; + return name.equals(that.name) && + identifier.equals(that.identifier); + } + + @Override + public int hashCode() { + return Objects.hash(name, identifier); + } + + @Override + public String toString() { + return "ObjectElement{" + + "name='" + name + '\'' + + ", identifier=" + identifier + + '}'; + } +} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java index 62798e1..6744616 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java @@ -4,6 +4,7 @@ package com.keuin.kbackupfabric.util.backup.incremental.identifier; * The identifier distinguishing files in the object collection. * It should be based on cryptographic hash function in order to prevent possible attacks to the backup system. * All identifiers should be immutable and implement their own equals method. + * Immutable. */ public interface ObjectIdentifier { String getIdentification(); 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 9f56b0e..64716ed 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 @@ -7,6 +7,10 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Objects; +/** + * Identifier based on sha256. + * Immutable. + */ public class Sha256Identifier extends SingleHashIdentifier { private static final int SHA256_LENGTH = 32; diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java index 62ba47c..3b96f79 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java @@ -8,6 +8,7 @@ import java.util.Arrays; /** * A simple identifier based on a single hash function. + * Immutable. */ public abstract class SingleHashIdentifier implements ObjectIdentifier { diff --git a/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionFactoryTest.java b/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionFactoryTest.java new file mode 100644 index 0000000..60a42cf --- /dev/null +++ b/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionFactoryTest.java @@ -0,0 +1,81 @@ +package com.keuin.kbackupfabric.util.backup.incremental; + +import com.keuin.kbackupfabric.util.backup.incremental.identifier.Sha256Identifier; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.util.*; + +import static org.junit.Assert.*; + +public class ObjectCollectionFactoryTest { + + private void validate(ObjectCollection collection, List subCollections, Map subElements) { + assertEquals(subCollections.size(), collection.getSubCollectionMap().size()); + assertEquals(subElements.size(), collection.getElementSet().size()); + for (Map.Entry c : collection.getSubCollectionMap().entrySet()) { + assertEquals(c.getKey(), c.getValue().getName()); + assertTrue(subCollections.contains(c.getKey())); + } + for (Map.Entry entry : collection.getElementMap().entrySet()) { +// assertTrue(subElements.contains(e.getIdentification())); + assertEquals(subElements.get(entry.getKey()), entry.getValue().getIdentifier().getIdentification()); + } + } + + @Test + public void fromDirectory() { + try { + ObjectCollectionFactory factory = + new ObjectCollectionFactory<>(Sha256Identifier.getFactory()); + ObjectCollection collection = + factory.fromDirectory(new File("./testfile/ObjectCollectionFactoryTest")); + + assertEquals("ObjectCollectionFactoryTest", collection.getName()); + assertEquals(3, collection.getSubCollectionMap().size()); + assertEquals(2, collection.getElementSet().size()); + + final Map elements = new HashMap<>(); + + // check root dir + elements.put("a", "261CA0D59FEE8FD169802BB8030A07CF23E5C1593FA81A16C6D0A8CF27DAA2ED"); + elements.put("b", "B3FED75012C4969DC63A50EBC4E745FF77E4A06E0B04720EF71EF033032EBAF7"); + validate(collection, Arrays.asList("1", "2", "3"), elements); + elements.clear(); + + // check `1` + elements.put("a", "E8620F35A5DB33B1257CC51245DDACDA8AF3E0D431A8A38473575E468BCBD0BD"); + elements.put("b", "19EE41585A674274891DE5A4B365DBAB9C49C576AB6F86CD515B683724D2DBBD"); + validate(collection.getSubCollectionMap().get("1"), Arrays.asList("11", "12"), elements); + elements.clear(); + + // check `2` + validate(collection.getSubCollectionMap().get("2"), Collections.emptyList(), Collections.emptyMap()); + + // check `3` + validate(collection.getSubCollectionMap().get("3"), Collections.emptyList(), Collections.emptyMap()); + + // check `11` + validate(collection.getSubCollectionMap().get("1").getSubCollectionMap().get("11"), Collections.singletonList("111"), Collections.emptyMap()); + + // check `111` + elements.put("a", "1EDBE882A757E1FAFCA77A9D3BE3FF5D2BB3E2037B238C865F1F957C431F43B4"); + elements.put("b", "30BA7CD8B4AD93A8B3826CD8D1518790924EEBB930EC04DF7DFB03A50B17D7BC"); + validate( + collection.getSubCollectionMap().get("1").getSubCollectionMap().get("11").getSubCollectionMap().get("111"), + Collections.emptyList(), + elements + ); + elements.clear(); + + // check `12` + validate(collection.getSubCollectionMap().get("1").getSubCollectionMap().get("12"), Collections.emptyList(), Collections.emptyMap()); + + + } catch (IOException e) { + e.printStackTrace(); + fail(); + } + } +} \ No newline at end of file diff --git a/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256IdentifierTest.java b/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256IdentifierTest.java index 2296e01..53e92e2 100644 --- a/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256IdentifierTest.java +++ b/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256IdentifierTest.java @@ -6,17 +6,19 @@ import java.io.File; import java.io.IOException; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; public class Sha256IdentifierTest { @Test public void fromFile() { try { - Sha256Identifier sha256 = Sha256Identifier.fromFile(new File("./src/test/sha256")); + Sha256Identifier sha256 = Sha256Identifier.fromFile(new File("./testfile/Sha256IdentifierTest")); String str = sha256.getIdentification().toUpperCase(); assertEquals("315F5BDB76D078C43B8AC0064E4A0164612B1FCE77C869345BFC94C75894EDD3", str); } catch (IOException e) { e.printStackTrace(); + fail(); } } diff --git a/src/test/sha256 b/src/test/sha256 deleted file mode 100644 index 5dd01c1..0000000 --- a/src/test/sha256 +++ /dev/null @@ -1 +0,0 @@ -Hello, world! \ No newline at end of file diff --git a/testfile/ObjectCollectionFactoryTest/1/11/111/a b/testfile/ObjectCollectionFactoryTest/1/11/111/a new file mode 100644 index 0000000..13a4f6c Binary files /dev/null and b/testfile/ObjectCollectionFactoryTest/1/11/111/a differ diff --git a/testfile/ObjectCollectionFactoryTest/1/11/111/b b/testfile/ObjectCollectionFactoryTest/1/11/111/b new file mode 100644 index 0000000..52755f5 Binary files /dev/null and b/testfile/ObjectCollectionFactoryTest/1/11/111/b differ diff --git a/testfile/ObjectCollectionFactoryTest/1/a b/testfile/ObjectCollectionFactoryTest/1/a new file mode 100644 index 0000000..4936a74 Binary files /dev/null and b/testfile/ObjectCollectionFactoryTest/1/a differ diff --git a/testfile/ObjectCollectionFactoryTest/1/b b/testfile/ObjectCollectionFactoryTest/1/b new file mode 100644 index 0000000..7c0897e Binary files /dev/null and b/testfile/ObjectCollectionFactoryTest/1/b differ diff --git a/testfile/ObjectCollectionFactoryTest/a b/testfile/ObjectCollectionFactoryTest/a new file mode 100644 index 0000000..210ecec Binary files /dev/null and b/testfile/ObjectCollectionFactoryTest/a differ diff --git a/testfile/ObjectCollectionFactoryTest/b b/testfile/ObjectCollectionFactoryTest/b new file mode 100644 index 0000000..344e596 Binary files /dev/null and b/testfile/ObjectCollectionFactoryTest/b differ diff --git a/testfile/Sha256IdentifierTest b/testfile/Sha256IdentifierTest new file mode 100644 index 0000000..5dd01c1 --- /dev/null +++ b/testfile/Sha256IdentifierTest @@ -0,0 +1 @@ +Hello, world! \ No newline at end of file -- cgit v1.2.3 From f4d926c1eb91749a4b0f89c7a35538821ff7e21b Mon Sep 17 00:00:00 2001 From: Keuin Date: Tue, 12 Jan 2021 16:54:03 +0800 Subject: Add serialization and deserialization for ObjectCollection --- .../util/backup/incremental/ObjectCollection.java | 3 +- .../incremental/ObjectCollectionFactory.java | 1 + .../incremental/ObjectCollectionSerializer.java | 30 +++++++ .../util/backup/incremental/ObjectElement.java | 3 +- .../incremental/identifier/ObjectIdentifier.java | 4 +- .../incremental/identifier/Sha256Identifier.java | 21 ++--- .../manager/IncrementalBackupStorageManager.java | 95 ++++++++++++++++++++++ .../incremental/ObjectCollectionFactoryTest.java | 1 - .../ObjectCollectionSerializerTest.java | 29 +++++++ 9 files changed, 173 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionSerializer.java create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/incremental/manager/IncrementalBackupStorageManager.java create mode 100644 src/test/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionSerializerTest.java (limited to 'src/main/java/com/keuin') diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollection.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollection.java index e0ef4f7..16d95e6 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollection.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollection.java @@ -1,8 +1,9 @@ package com.keuin.kbackupfabric.util.backup.incremental; +import java.io.Serializable; import java.util.*; -public class ObjectCollection { +public class ObjectCollection implements Serializable { private final String name; private final Map elements; private final Map subCollections; 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 be9e426..ea9edd9 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 @@ -16,6 +16,7 @@ import java.util.*; * The identifier should use hashes that are strong enough, to prevent possible collisions. */ public class ObjectCollectionFactory { + private final FileIdentifierFactory identifierFactory; public ObjectCollectionFactory(FileIdentifierFactory 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 new file mode 100644 index 0000000..cc77837 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionSerializer.java @@ -0,0 +1,30 @@ +package com.keuin.kbackupfabric.util.backup.incremental; + +import java.io.*; +import java.util.Objects; + +public class ObjectCollectionSerializer { + public static ObjectCollection fromFile(File file) throws IOException { + Objects.requireNonNull(file); + ObjectCollection collection; + try (FileInputStream fileInputStream = new FileInputStream(file)) { + try (ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)) { + collection = (ObjectCollection) objectInputStream.readObject(); + } catch (ClassNotFoundException ignored) { + // this should not happen + return null; + } + } + return collection; + } + + public static void toFile(ObjectCollection collection, File file) throws IOException { + Objects.requireNonNull(collection); + Objects.requireNonNull(file); + try (FileOutputStream fileOutputStream = new FileOutputStream(file)) { + try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) { + objectOutputStream.writeObject(collection); + } + } + } +} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectElement.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectElement.java index 1232fb9..cbb886e 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectElement.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectElement.java @@ -2,13 +2,14 @@ package com.keuin.kbackupfabric.util.backup.incremental; import com.keuin.kbackupfabric.util.backup.incremental.identifier.ObjectIdentifier; +import java.io.Serializable; import java.util.Objects; /** * Representing a file in a ObjectCollection. * Immutable. */ -public class ObjectElement { +public class ObjectElement implements Serializable { private final String name; private final ObjectIdentifier identifier; diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java index 6744616..aece07d 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java @@ -1,11 +1,13 @@ package com.keuin.kbackupfabric.util.backup.incremental.identifier; +import java.io.Serializable; + /** * The identifier distinguishing files in the object collection. * It should be based on cryptographic hash function in order to prevent possible attacks to the backup system. * All identifiers should be immutable and implement their own equals method. * Immutable. */ -public interface ObjectIdentifier { +public interface ObjectIdentifier extends Serializable { String getIdentification(); } 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 64716ed..9ecf2d3 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 @@ -41,27 +41,28 @@ public class Sha256Identifier extends SingleHashIdentifier { try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); - FileInputStream inputStream = new FileInputStream(file); - - // This does not work. I don't know why + try (FileInputStream inputStream = new FileInputStream(file)) { + // This does not work. I don't know why // FileChannel channel = inputStream.getChannel(); // ByteBuffer buffer = ByteBuffer.allocate(128); // int readLength; // while ((readLength = channel.read(buffer)) > 0) // digest.update(buffer); - // This also works, without warnings - byte[] readBuffer = new byte[1024 * 1024]; - int readLength; - while ((readLength = inputStream.read(readBuffer)) > 0) - digest.update(readBuffer,0, readLength); + // This also works, without warnings + byte[] readBuffer = new byte[1024 * 1024]; + int readLength; + while ((readLength = inputStream.read(readBuffer)) > 0) + digest.update(readBuffer, 0, readLength); - // The below lines also works, but the IDE will complain about the while loop + // The below lines also works, but the IDE will complain about the while loop // DigestInputStream digestInputStream = new DigestInputStream(inputStream, digest); // while(digestInputStream.read() > 0) // ; - return digest.digest(); + return digest.digest(); + } + } catch (NoSuchAlgorithmException ignored) { // this shouldn't happen return new byte[SHA256_LENGTH]; diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/manager/IncrementalBackupStorageManager.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/manager/IncrementalBackupStorageManager.java new file mode 100644 index 0000000..cd15499 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/manager/IncrementalBackupStorageManager.java @@ -0,0 +1,95 @@ +package com.keuin.kbackupfabric.util.backup.incremental.manager; + +import com.keuin.kbackupfabric.util.backup.incremental.ObjectCollection; +import com.keuin.kbackupfabric.util.backup.incremental.ObjectElement; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Map; +import java.util.Objects; + +public class IncrementalBackupStorageManager { + + private final Path backupStorageBase; + + public IncrementalBackupStorageManager(Path backupStorageBase) { + this.backupStorageBase = backupStorageBase; + } + + /** + * Add a object collection to storage base. + * @param collection the collection. + * @return objects copied to the base. + * @throws IOException I/O Error. + */ + public int addObjectCollection(ObjectCollection collection, File collectionBasePath) throws IOException { + Objects.requireNonNull(collection); + Objects.requireNonNull(collectionBasePath); + + int copyCount = 0; + + // copy sub files + for (Map.Entry entry : collection.getElementMap().entrySet()) { + File copyDestination = new File(backupStorageBase.toFile(), entry.getValue().getIdentifier().getIdentification()); + if (!baseContainsObject(entry.getValue())) { + // element does not exist. copy. + Files.copy(Paths.get(collectionBasePath.getAbsolutePath(), entry.getKey()), copyDestination.toPath()); + ++copyCount; + } + } + + //copy sub dirs recursively + for (Map.Entry entry : collection.getSubCollectionMap().entrySet()) { + File newBase = new File(collectionBasePath, entry.getKey()); + copyCount += addObjectCollection(entry.getValue(), newBase); + } + + return copyCount; + } + + /** + * Restore an object collection from the storage base. i.e., restore the save from backup storage. + * @param collection the collection to be restored. + * @param collectionBasePath save path of the collection. + * @return objects restored from the base. + * @throws IOException I/O Error. + */ + public int restoreObjectCollection(ObjectCollection collection, File collectionBasePath) throws IOException { + Objects.requireNonNull(collection); + Objects.requireNonNull(collectionBasePath); + + int copyCount = 0; + + // copy sub files + for (Map.Entry entry : collection.getElementMap().entrySet()) { + File copySource = new File(backupStorageBase.toFile(), entry.getValue().getIdentifier().getIdentification()); + if (!baseContainsObject(entry.getValue())) { + throw new IOException(String.format("File %s does not exist in the base.", copySource.getName())); + } + Files.copy(copySource.toPath(), Paths.get(collectionBasePath.getAbsolutePath(), entry.getKey())); + ++copyCount; + } + + //copy sub dirs recursively + for (Map.Entry entry : collection.getSubCollectionMap().entrySet()) { + File newBase = new File(collectionBasePath, entry.getKey()); + copyCount += restoreObjectCollection(entry.getValue(), newBase); + } + + return copyCount; + } + + /** + * Check if the backup base contains given element. + * @param objectElement the element. + * @return true or false. + */ + private boolean baseContainsObject(ObjectElement objectElement) { + // This may be extended to use more variants of hash functions and combinations of other attributes (such as file size) + return (new File(backupStorageBase.toFile(), objectElement.getIdentifier().getIdentification())).exists(); + } + +} diff --git a/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionFactoryTest.java b/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionFactoryTest.java index 60a42cf..b7e0aad 100644 --- a/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionFactoryTest.java +++ b/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionFactoryTest.java @@ -72,7 +72,6 @@ public class ObjectCollectionFactoryTest { // check `12` validate(collection.getSubCollectionMap().get("1").getSubCollectionMap().get("12"), Collections.emptyList(), Collections.emptyMap()); - } catch (IOException e) { e.printStackTrace(); fail(); diff --git a/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionSerializerTest.java b/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionSerializerTest.java new file mode 100644 index 0000000..0edfe01 --- /dev/null +++ b/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionSerializerTest.java @@ -0,0 +1,29 @@ +package com.keuin.kbackupfabric.util.backup.incremental; + +import com.keuin.kbackupfabric.util.backup.incremental.identifier.Sha256Identifier; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +import static org.junit.Assert.assertEquals; + +public class ObjectCollectionSerializerTest { + @Test + public void testSerializationConsistency() throws IOException { + ObjectCollectionFactory factory = + new ObjectCollectionFactory<>(Sha256Identifier.getFactory()); + ObjectCollection collection = + factory.fromDirectory(new File("./testfile/ObjectCollectionFactoryTest")); + File file = new File("./testfile/serialized"); + if (file.exists()) { + Files.delete(file.toPath()); + } + ObjectCollectionSerializer.toFile(collection, file); + ObjectCollection collection2 = ObjectCollectionSerializer.fromFile(file); + Files.delete(file.toPath()); + assertEquals(collection, collection2); + } + +} \ No newline at end of file -- cgit v1.2.3 From 81497ebee2cdb0dac3bcdc26c5d77d06bdb98b5d Mon Sep 17 00:00:00 2001 From: Keuin Date: Wed, 13 Jan 2021 12:51:15 +0800 Subject: Clean old incremental backup codes. --- README.md | 16 +-- gradle.properties | 6 +- .../java/com/keuin/kbackupfabric/KBCommands.java | 149 ++++++++++----------- .../keuin/kbackupfabric/KBCommandsRegister.java | 26 ++-- .../kbackupfabric/operation/BackupOperation.java | 19 ++- .../kbackupfabric/operation/DeleteOperation.java | 20 ++- .../kbackupfabric/operation/RestoreOperation.java | 30 +++-- .../operation/backup/BackupMethod.java | 26 +--- .../operation/backup/IncrementalBackupMethod.java | 62 --------- .../operation/backup/IncrementalBackupUtil.java | 90 ------------- .../operation/backup/PrimitiveBackupMethod.java | 45 +++---- .../operation/backup/feedback/BackupFeedback.java | 5 + .../backup/feedback/PrimitiveBackupFeedback.java | 29 ++++ .../util/backup/BackupFilesystemUtil.java | 27 ++-- .../kbackupfabric/util/backup/BackupType.java | 33 ----- .../util/backup/builder/BackupFileNameBuilder.java | 24 ---- .../builder/ObjectTreeBackupFileNameBuilder.java | 20 --- .../builder/PrimitiveZipBackupFileNameBuilder.java | 22 --- .../backup/formatter/BackupFileNameFormatter.java | 31 ----- .../ObjectTreeBackupFileNameFormatter.java | 44 ------ .../PrimitiveZipBackupFileNameFormatter.java | 43 ------ .../incremental/ObjectCollectionFactory.java | 6 +- .../incremental/ObjectCollectionSerializer.java | 3 + .../identifier/FileIdentifierFactory.java | 8 -- .../identifier/FileIdentifierProvider.java | 8 ++ .../incremental/identifier/Sha256Identifier.java | 4 +- .../util/backup/name/BackupFileNameEncoder.java | 49 +++++++ .../name/PrimitiveBackupFileNameEncoder.java | 34 +++++ .../suggestion/BackupMethodSuggestionProvider.java | 36 ----- .../suggestion/BackupNameSuggestionProvider.java | 3 +- src/test/java/IncrementalBackupUtilTest.java | 16 --- .../name/PrimitiveBackupFileNameEncoderTest.java | 21 +++ 32 files changed, 318 insertions(+), 637 deletions(-) delete mode 100644 src/main/java/com/keuin/kbackupfabric/operation/backup/IncrementalBackupMethod.java delete mode 100644 src/main/java/com/keuin/kbackupfabric/operation/backup/IncrementalBackupUtil.java create mode 100644 src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/BackupFeedback.java create mode 100644 src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/PrimitiveBackupFeedback.java delete mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/BackupType.java delete mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/builder/BackupFileNameBuilder.java delete mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/builder/ObjectTreeBackupFileNameBuilder.java delete mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/builder/PrimitiveZipBackupFileNameBuilder.java delete mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/formatter/BackupFileNameFormatter.java delete mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/formatter/ObjectTreeBackupFileNameFormatter.java delete mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/formatter/PrimitiveZipBackupFileNameFormatter.java delete mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/FileIdentifierFactory.java create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/FileIdentifierProvider.java create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/name/BackupFileNameEncoder.java create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/name/PrimitiveBackupFileNameEncoder.java delete mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupMethodSuggestionProvider.java delete mode 100644 src/test/java/IncrementalBackupUtilTest.java create mode 100644 src/test/java/com/keuin/kbackupfabric/util/backup/name/PrimitiveBackupFileNameEncoderTest.java (limited to 'src/main/java/com/keuin') diff --git a/README.md b/README.md index 2c34c2f..7152430 100644 --- a/README.md +++ b/README.md @@ -51,14 +51,8 @@ done ## 3. To-Do List: -- New version checker. -- Refactor code. -- More thorough test. -- Implement incremental backup. - + Restore: trace-back (recursively, then generate file dependence tree) - - Implement unZipRecursively (unzip a .zip.inc file recursively until reaches the root (i.e. the last full backup).) - + Backup: base-diff (select most recently backup as the base, then diff) - - Implement zipDiff (make a new zip with the latest backup as the base, store diff-table in zip comment (A:Add, M:Modification, D:Deletion)) -- Optimize help menu. (colored command help menu) -- Add op login hint in the next start after restoring. -- Implement incremental backup. +- Incremental backup +- Code refactor +- Op login hint in the next start after restoring +- Optimized help menu (colored command help menu) +- New version checker diff --git a/gradle.properties b/gradle.properties index a131c0a..4d7ec6c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,11 +4,11 @@ org.gradle.jvmargs=-Xmx1G # check these on https://fabricmc.net/use minecraft_version=1.14.4 yarn_mappings=1.14.4+build.18 -loader_version=0.10.8 +loader_version=0.11.0 # Mod Properties -mod_version=1.3.3 +mod_version=1.4.0 maven_group=com.keuin.kbackupfabric archives_base_name=kbackup-fabric # Dependencies # currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api -fabric_version=0.23.2+1.14 +fabric_version=0.28.4+1.14 diff --git a/src/main/java/com/keuin/kbackupfabric/KBCommands.java b/src/main/java/com/keuin/kbackupfabric/KBCommands.java index 571333f..ef4a401 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBCommands.java +++ b/src/main/java/com/keuin/kbackupfabric/KBCommands.java @@ -5,13 +5,9 @@ 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.BackupMethod; -import com.keuin.kbackupfabric.operation.backup.IncrementalBackupMethod; import com.keuin.kbackupfabric.operation.backup.PrimitiveBackupMethod; import com.keuin.kbackupfabric.util.PrintUtil; import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil; -import com.keuin.kbackupfabric.util.backup.BackupNameTimeFormatter; -import com.keuin.kbackupfabric.util.backup.BackupType; import com.keuin.kbackupfabric.util.backup.suggestion.BackupNameSuggestionProvider; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.context.CommandContext; @@ -33,10 +29,11 @@ public final class KBCommands { private static final int SUCCESS = 1; private static final int FAILED = -1; private static final String DEFAULT_BACKUP_NAME = "noname"; + private static boolean notifiedPreviousRestoration = false; //private static final Logger LOGGER = LogManager.getLogger(); - private static final List backupNameList = new ArrayList<>(); // index -> backupName + private static final List backupFileNameList = new ArrayList<>(); // index -> backupName private static Invokable pendingOperation = null; //private static BackupMethod activatedBackupMethod = new PrimitiveBackupMethod(); // The backup method we currently using @@ -65,8 +62,9 @@ public final class KBCommands { */ public static int kb(CommandContext context) { int statCode = list(context); - if (MetadataHolder.hasMetadata()) { + if (MetadataHolder.hasMetadata() && !notifiedPreviousRestoration) { // Output metadata info + notifiedPreviousRestoration = true; msgStress(context, "Restored from backup " + MetadataHolder.getMetadata().getBackupName()); } return statCode; @@ -84,19 +82,20 @@ public final class KBCommands { (dir, name) -> dir.isDirectory() && name.toLowerCase().endsWith(".zip") && name.toLowerCase().startsWith(getBackupFileNamePrefix()) ); - backupNameList.clear(); + 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, check /kb backup."); + msgInfo(context, "There are no available backups. To make a new backup, run /kb backup."); } int i = 0; for (File file : files) { ++i; - String backupName = getBackupName(file.getName()); - backupNameList.add(backupName); - msgInfo(context, String.format("[%d] %s, size: %.1fMB", i, backupName, file.length() * 1.0 / 1024 / 1024)); + 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."); @@ -112,13 +111,13 @@ public final class KBCommands { */ public static int primitiveBackup(CommandContext context) { //KBMain.backup("name") - String backupName = StringArgumentType.getString(context, "backupName"); - if (backupName.matches("[0-9]*")) { + String customBackupName = StringArgumentType.getString(context, "backupName"); + if (customBackupName.matches("[0-9]*")) { // Numeric param is not allowed - backupName = String.format("a%s", backupName); - msgWarn(context, String.format("Pure numeric name is not allowed. Renaming to %s", backupName)); + customBackupName = String.format("a%s", customBackupName); + msgWarn(context, String.format("Pure numeric name is not allowed. Renaming to %s", customBackupName)); } - return doBackup(context, backupName, PrimitiveBackupMethod.getInstance()); + return doBackup(context, customBackupName); } /** @@ -128,23 +127,23 @@ public final class KBCommands { * @return stat code. */ public static int primitiveBackupWithDefaultName(CommandContext context) { - return doBackup(context, DEFAULT_BACKUP_NAME, PrimitiveBackupMethod.getInstance()); + return doBackup(context, DEFAULT_BACKUP_NAME); } - public static int incrementalBackup(CommandContext context) { - //KBMain.backup("name") - String backupName = StringArgumentType.getString(context, "backupName"); - 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. Renaming to %s", backupName)); - } - return doBackup(context, backupName, IncrementalBackupMethod.getInstance()); - } - - public static int incrementalBackupWithDefaultName(CommandContext context) { - return doBackup(context, DEFAULT_BACKUP_NAME, IncrementalBackupMethod.getInstance()); - } +// public static int incrementalBackup(CommandContext context) { +// //KBMain.backup("name") +// String backupName = StringArgumentType.getString(context, "backupName"); +// 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. Renaming to %s", backupName)); +// } +// return doBackup(context, backupName, IncrementalBackupMethod.getInstance()); +// } +// +// public static int incrementalBackupWithDefaultName(CommandContext context) { +// return doBackup(context, DEFAULT_BACKUP_NAME, IncrementalBackupMethod.getInstance()); +// } /** * Delete an existing backup with context parameter backupName. @@ -155,14 +154,14 @@ public final class KBCommands { */ public static int delete(CommandContext context) { - String backupName = parseBackupName(context, StringArgumentType.getString(context, "backupName")); + String backupFileName = parseBackupFileName(context, StringArgumentType.getString(context, "backupName")); MinecraftServer server = context.getSource().getMinecraftServer(); - if (backupName == null) + if (backupFileName == null) return list(context); // Show the list and return // Validate backupName - if (!isBackupNameValid(backupName, server)) { + if (!isBackupFileExists(backupFileName, server)) { // Invalid backupName msgErr(context, "Invalid backup name! Please check your input. The list index number is also valid."); return FAILED; @@ -170,9 +169,9 @@ public final class KBCommands { // Update pending task //pendingOperation = AbstractConfirmableOperation.createDeleteOperation(context, backupName); - pendingOperation = new DeleteOperation(context, backupName); + pendingOperation = new DeleteOperation(context, backupFileName); - msgWarn(context, String.format("DELETION WARNING: The deletion is irreversible! You will lose the backup %s permanently. Use /kb confirm to start or /kb cancel to abort.", backupName), true); + msgWarn(context, String.format("DELETION WARNING: The deletion is irreversible! You will lose the backup %s permanently. Use /kb confirm to start or /kb cancel to abort.", backupFileName), true); return SUCCESS; } @@ -187,14 +186,14 @@ public final class KBCommands { public static int restore(CommandContext context) { //KBMain.restore("name") MinecraftServer server = context.getSource().getMinecraftServer(); - String backupName = parseBackupName(context, StringArgumentType.getString(context, "backupName")); - backupName = parseBackupName(context, backupName); + String backupFileName = parseBackupFileName(context, StringArgumentType.getString(context, "backupName")); + backupFileName = parseBackupFileName(context, backupFileName); - if (backupName == null) + if (backupFileName == null) return list(context); // Show the list and return // Validate backupName - if (!isBackupNameValid(backupName, server)) { + 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; @@ -205,22 +204,22 @@ public final class KBCommands { // Update pending task //pendingOperation = AbstractConfirmableOperation.createRestoreOperation(context, backupName); - File backupFile = new File(getBackupSaveDirectory(server), getBackupFileName(backupName)); - pendingOperation = new RestoreOperation(context, backupFile.getAbsolutePath(), getLevelPath(server), backupName); +// File backupFile = new File(getBackupSaveDirectory(server), getBackupFileName(backupName)); + pendingOperation = new RestoreOperation(context, getBackupSaveDirectory(server).getAbsolutePath(), getLevelPath(server), backupFileName); - 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.", backupName), true); + 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; } - private static int doBackup(CommandContext context, String customBackupName, BackupMethod backupMethod) { + private static int doBackup(CommandContext context, String customBackupName) { // 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; +// String backupName = BackupNameTimeFormatter.getTimeString() + "_" + customBackupName; // Validate file name final char[] ILLEGAL_CHARACTERS = {'/', '\n', '\r', '\t', '\0', '\f', '`', '?', '*', '\\', '<', '>', '|', '\"', ':'}; for (char c : ILLEGAL_CHARACTERS) { - if (backupName.contains(String.valueOf(c))) { + if (customBackupName.contains(String.valueOf(c))) { msgErr(context, String.format("Name cannot contain special character \"%c\".", c)); return FAILED; } @@ -229,7 +228,7 @@ public final class KBCommands { // Do backup PrintUtil.info("Invoking backup worker ..."); //BackupWorker.invoke(context, backupName, metadata); - BackupOperation operation = new BackupOperation(context, backupName, backupMethod); + BackupOperation operation = new BackupOperation(context, customBackupName, PrimitiveBackupMethod.getInstance()); if (operation.invoke()) { return SUCCESS; } else if (operation.isBlocked()) { @@ -293,15 +292,15 @@ public final class KBCommands { files.removeIf(f -> !f.getName().startsWith(BackupFilesystemUtil.getBackupFileNamePrefix())); files.sort((x, y) -> (int) (BackupFilesystemUtil.getBackupTimeFromBackupFileName(y.getName()) - BackupFilesystemUtil.getBackupTimeFromBackupFileName(x.getName()))); File prevBackupFile = files.get(0); - String backupName = getBackupName(prevBackupFile.getName()); - int i = backupNameList.indexOf(backupName); + String backupFileName = prevBackupFile.getName(); + int i = backupFileNameList.indexOf(backupFileName); if (i == -1) { - backupNameList.add(backupName); - i = backupNameList.size(); + backupFileNameList.add(backupFileName); + i = backupFileNameList.size(); } else { ++i; } - msgInfo(context, String.format("The most recent backup: [%d] %s , size: %s", i, backupName, getFriendlyFileSizeString(prevBackupFile.length()))); + msgInfo(context, String.format("The most recent backup: [%d] %s , size: %s", i, backupFileName, getFriendlyFileSizeString(prevBackupFile.length()))); } catch (NullPointerException e) { msgInfo(context, "There are no backups available."); } catch (SecurityException ignored) { @@ -311,35 +310,35 @@ public final class KBCommands { return SUCCESS; } - /** - * Select the backup method we use. - * @param context the context. - * @return stat code. - */ - public static int setMethod(CommandContext context) { - String desiredMethodName = StringArgumentType.getString(context, "backupMethod"); - List backupMethods = Arrays.asList(BackupType.PRIMITIVE_ZIP_BACKUP, BackupType.OBJECT_TREE_BACKUP); - for (BackupType method : backupMethods) { - if(method.getName().equals(desiredMethodName)) { - // Incremental backup -// activatedBackupMethod = - msgInfo(context, String.format("Backup method is set to: %s", desiredMethodName)); - return SUCCESS; - } - } - - return SUCCESS; - } - - - private static String parseBackupName(CommandContext context, String userInput) { +// /** +// * Select the backup method we use. +// * @param context the context. +// * @return stat code. +// */ +// public static int setMethod(CommandContext context) { +// String desiredMethodName = StringArgumentType.getString(context, "backupMethod"); +// List backupMethods = Arrays.asList(BackupType.PRIMITIVE_ZIP_BACKUP, BackupType.OBJECT_TREE_BACKUP); +// for (BackupType method : backupMethods) { +// if(method.getName().equals(desiredMethodName)) { +// // Incremental backup +//// activatedBackupMethod = +// msgInfo(context, String.format("Backup method is set to: %s", desiredMethodName)); +// return SUCCESS; +// } +// } +// +// return SUCCESS; +// } + + + private static String parseBackupFileName(CommandContext context, String userInput) { try { String backupName = StringArgumentType.getString(context, "backupName"); if (backupName.matches("[0-9]*")) { // If numeric input int index = Integer.parseInt(backupName) - 1; - return backupNameList.get(index); // Replace input number with real backup name. + 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/KBCommandsRegister.java b/src/main/java/com/keuin/kbackupfabric/KBCommandsRegister.java index 3376ac9..4154464 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBCommandsRegister.java +++ b/src/main/java/com/keuin/kbackupfabric/KBCommandsRegister.java @@ -1,8 +1,7 @@ package com.keuin.kbackupfabric; -import com.keuin.kbackupfabric.util.backup.suggestion.BackupMethodSuggestionProvider; -import com.keuin.kbackupfabric.util.backup.suggestion.BackupNameSuggestionProvider; import com.keuin.kbackupfabric.util.PermissionValidator; +import com.keuin.kbackupfabric.util.backup.suggestion.BackupNameSuggestionProvider; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.StringArgumentType; import net.minecraft.server.command.CommandManager; @@ -19,19 +18,12 @@ public final class KBCommandsRegister { // register /kb list for showing the backup list. OP is required. dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("list").requires(PermissionValidator::op).executes(KBCommands::list))); - // register /kb backup zip [name] as a alias - dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("backup").then( - CommandManager.literal("zip").then( - CommandManager.argument("backupName", StringArgumentType.greedyString()).requires(PermissionValidator::op).executes(KBCommands::primitiveBackup) - ).requires(PermissionValidator::op).executes(KBCommands::primitiveBackupWithDefaultName))) - ); - - // register /kb backup incremental [name] - dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("backup").then( - CommandManager.literal("incremental").then( - CommandManager.argument("backupName", StringArgumentType.greedyString()).requires(PermissionValidator::op).executes(KBCommands::incrementalBackup) - ).requires(PermissionValidator::op).executes(KBCommands::incrementalBackupWithDefaultName))) - ); +// // register /kb backup incremental [name] +// dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("backup").then( +// CommandManager.literal("incremental").then( +// CommandManager.argument("backupName", StringArgumentType.greedyString()).requires(PermissionValidator::op).executes(KBCommands::incrementalBackup) +// ).requires(PermissionValidator::op).executes(KBCommands::incrementalBackupWithDefaultName))) +// ); // register /kb backup [name] for performing backup. OP is required. dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("backup").then( @@ -53,7 +45,7 @@ public final class KBCommandsRegister { // register /kb prev for showing the latest backup. dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("prev").requires(PermissionValidator::op).executes(KBCommands::prev))); - // register /kb setMethod for selecting backup method (zip, incremental) - dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("setMethod").then(CommandManager.argument("backupMethod", StringArgumentType.string()).suggests(BackupMethodSuggestionProvider.getProvider()).requires(PermissionValidator::op).executes(KBCommands::setMethod)))); +// // register /kb setMethod for selecting backup method (zip, incremental) +// dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("setMethod").then(CommandManager.argument("backupMethod", StringArgumentType.string()).suggests(BackupMethodSuggestionProvider.getProvider()).requires(PermissionValidator::op).executes(KBCommands::setMethod)))); } } diff --git a/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java index 465b293..f8736c8 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java @@ -2,6 +2,7 @@ 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.util.PrintUtil; import com.mojang.brigadier.context.CommandContext; import net.minecraft.server.MinecraftServer; @@ -19,16 +20,16 @@ import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.*; public class BackupOperation extends InvokableAsyncBlockingOperation { private final CommandContext context; - private final String backupName; + private final String customBackupName; private final Map oldWorldsSavingDisabled = new HashMap<>(); private final BackupMethod backupMethod; private long startTime; - public BackupOperation(CommandContext context, String backupName, BackupMethod backupMethod) { + public BackupOperation(CommandContext context, String customBackupName, BackupMethod backupMethod) { super("BackupWorker"); this.context = context; - this.backupName = backupName; + this.customBackupName = customBackupName; this.backupMethod = backupMethod; } @@ -49,18 +50,16 @@ public class BackupOperation extends InvokableAsyncBlockingOperation { // Make zip String levelPath = getLevelPath(server); - String backupFileName = getBackupFileName(backupName); + String backupFileName = getBackupFileName(customBackupName); - BackupMethod.BackupResult result = backupMethod.backup(backupName,levelPath,backupSaveDirectory); + PrimitiveBackupFeedback result = backupMethod.backup(customBackupName,levelPath,backupSaveDirectory); if(result.isSuccess()) { - // Restore old autosave switch stat + // Restore old auto-save switch stat server.getWorlds().forEach(world -> world.savingDisabled = oldWorldsSavingDisabled.getOrDefault(world, true)); // Print finish message: time elapsed and file size long timeElapsedMillis = System.currentTimeMillis() - startTime; - String msgText = String.format("Backup finished. Time elapsed: %.2fs.", timeElapsedMillis / 1000.0); - File backupZipFile = new File(backupSaveDirectory, backupFileName); - msgText += String.format(" File size: %s.", getFriendlyFileSizeString(result.getBackupSizeBytes())); + String msgText = String.format("Backup finished. Time elapsed: %.2fs. ", timeElapsedMillis / 1000.0) + result.getFeedback(); PrintUtil.msgInfo(context, msgText, true); } else { // failed @@ -77,7 +76,7 @@ public class BackupOperation extends InvokableAsyncBlockingOperation { protected boolean sync() { //// Save world, save old autosave configs - PrintUtil.broadcast(String.format("Making backup %s, please wait ...", backupName)); + PrintUtil.broadcast(String.format("Making backup %s, please wait ...", customBackupName)); // Get server MinecraftServer server = context.getSource().getMinecraftServer(); diff --git a/src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java index 444ca9a..3ae09f5 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java @@ -1,8 +1,8 @@ package com.keuin.kbackupfabric.operation; import com.keuin.kbackupfabric.operation.abstracts.InvokableAsyncBlockingOperation; -import com.keuin.kbackupfabric.util.backup.suggestion.BackupNameSuggestionProvider; import com.keuin.kbackupfabric.util.PrintUtil; +import com.keuin.kbackupfabric.util.backup.suggestion.BackupNameSuggestionProvider; import com.mojang.brigadier.context.CommandContext; import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.ServerCommandSource; @@ -10,27 +10,26 @@ import net.minecraft.server.command.ServerCommandSource; import java.io.File; import java.io.IOException; -import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.getBackupFileName; -import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.getBackupSaveDirectory; import static com.keuin.kbackupfabric.util.PrintUtil.msgErr; import static com.keuin.kbackupfabric.util.PrintUtil.msgInfo; +import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.getBackupSaveDirectory; import static org.apache.commons.io.FileUtils.forceDelete; public class DeleteOperation extends InvokableAsyncBlockingOperation { //private static final Logger LOGGER = LogManager.getLogger(); - private final String backupName; + private final String backupFileName; private final CommandContext context; - public DeleteOperation(CommandContext context, String backupName) { + public DeleteOperation(CommandContext context, String backupFileName) { super("BackupDeletingWorker"); - this.backupName = backupName; + this.backupFileName = backupFileName; this.context = context; } @Override public String toString() { - return String.format("deletion of %s", backupName); + return String.format("deletion of %s", backupFileName); } @Override @@ -41,8 +40,7 @@ public class DeleteOperation extends InvokableAsyncBlockingOperation { private void delete() { MinecraftServer server = context.getSource().getMinecraftServer(); - String backupFileName = getBackupFileName(backupName); - PrintUtil.info("Deleting backup " + backupName); + PrintUtil.info("Deleting backup file " + this.backupFileName); File backupFile = new File(getBackupSaveDirectory(server), backupFileName); int tryCounter = 0; do { @@ -59,7 +57,7 @@ public class DeleteOperation extends InvokableAsyncBlockingOperation { } ++tryCounter; } while (backupFile.exists()); - PrintUtil.info("Deleted backup " + backupName); - msgInfo(context, "Deleted backup " + backupName); + PrintUtil.info("Successfully deleted backup file " + this.backupFileName); + msgInfo(context, "Successfully deleted backup file " + this.backupFileName); } } diff --git a/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java index 22397a1..b95c767 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java @@ -1,9 +1,9 @@ package com.keuin.kbackupfabric.operation; -import com.keuin.kbackupfabric.exception.ZipUtilException; 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.util.PrintUtil; -import com.keuin.kbackupfabric.util.ZipUtil; import com.mojang.brigadier.context.CommandContext; import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.ServerCommandSource; @@ -13,33 +13,33 @@ import java.io.IOException; import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.getBackupFileName; import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.getBackupSaveDirectory; -import static org.apache.commons.io.FileUtils.forceDelete; public class RestoreOperation extends InvokableBlockingOperation { //private static final Logger LOGGER = LogManager.getLogger(); - private final String backupName; + private final String backupFileName; private final Thread serverThread; - private final String backupFilePath; - private final String levelDirectory; + private final String backupSavePath; + private final String levelPath; private final CommandContext context; private final MinecraftServer server; + private final BackupMethod backupMethod = PrimitiveBackupMethod.getInstance(); - public RestoreOperation(CommandContext context, String backupFilePath, String levelDirectory, String backupName) { + public RestoreOperation(CommandContext context, String backupSavePath, String levelPath, String backupFileName) { server = context.getSource().getMinecraftServer(); - this.backupName = backupName; + this.backupFileName = backupFileName; this.serverThread = server.getThread(); - this.backupFilePath = backupFilePath; - this.levelDirectory = levelDirectory; + this.backupSavePath = backupSavePath; + this.levelPath = levelPath; this.context = context; } @Override protected boolean blockingContext() { // do restore to backupName - PrintUtil.broadcast(String.format("Restoring to previous world %s ...", backupName)); + PrintUtil.broadcast(String.format("Restoring to backup %s ...", backupFileName)); - String backupFileName = getBackupFileName(backupName); + String backupFileName = getBackupFileName(this.backupFileName); PrintUtil.debug("Backup file name: " + backupFileName); File backupFile = new File(getBackupSaveDirectory(server), backupFileName); @@ -67,7 +67,7 @@ public class RestoreOperation extends InvokableBlockingOperation { @Override public String toString() { - return String.format("restoration from %s", backupName); + return String.format("restoration from %s", backupFileName); } private class WorkerThread implements Runnable { @@ -94,12 +94,16 @@ public class RestoreOperation extends InvokableBlockingOperation { }while(--cnt > 0); //////////////////// + backupMethod.restore(backupFileName, levelPath, backupSavePath); //ServerRestartUtil.forkAndRestart(); System.exit(111); } catch (SecurityException e) { PrintUtil.error("An exception occurred while restoring: " + e.getMessage()); + } catch (IOException e) { + PrintUtil.error(e.toString()); + PrintUtil.error("Failed to restore."); } } } diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/BackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/BackupMethod.java index 4e9eb6c..b65a076 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/BackupMethod.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/BackupMethod.java @@ -1,7 +1,6 @@ package com.keuin.kbackupfabric.operation.backup; -import com.keuin.kbackupfabric.util.backup.builder.BackupFileNameBuilder; -import com.keuin.kbackupfabric.util.backup.formatter.BackupFileNameFormatter; +import com.keuin.kbackupfabric.operation.backup.feedback.PrimitiveBackupFeedback; import java.io.IOException; @@ -16,29 +15,8 @@ public interface BackupMethod { * @param backupName the backup name. * @return if the backup operation succeed. */ - BackupResult backup(String backupName, String levelPath, String backupSaveDirectory) throws IOException; + PrimitiveBackupFeedback backup(String backupName, String levelPath, String backupSaveDirectory) throws IOException; boolean restore(String backupName, String levelPath, String backupSaveDirectory) throws IOException; - BackupFileNameBuilder getBackupFileNameBuilder(); - - BackupFileNameFormatter getBackupFileNameFormatter(); - - class BackupResult { - private final boolean success; - private final long backupSizeBytes; - - public BackupResult(boolean success, long backupSizeBytes) { - this.success = success; - this.backupSizeBytes = backupSizeBytes; - } - - public boolean isSuccess() { - return success; - } - - public long getBackupSizeBytes() { - return backupSizeBytes; - } - } } diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/IncrementalBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/IncrementalBackupMethod.java deleted file mode 100644 index 4a87bb3..0000000 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/IncrementalBackupMethod.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.keuin.kbackupfabric.operation.backup; - -import com.google.gson.JsonObject; -import com.keuin.kbackupfabric.util.PrintUtil; -import com.keuin.kbackupfabric.util.backup.builder.BackupFileNameBuilder; -import com.keuin.kbackupfabric.util.backup.builder.ObjectTreeBackupFileNameBuilder; -import com.keuin.kbackupfabric.util.backup.formatter.BackupFileNameFormatter; -import com.keuin.kbackupfabric.util.backup.formatter.ObjectTreeBackupFileNameFormatter; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Paths; -import java.time.LocalDateTime; - -public class IncrementalBackupMethod implements BackupMethod { - - private static final IncrementalBackupMethod INSTANCE = new IncrementalBackupMethod(); - - public static IncrementalBackupMethod getInstance() { - return INSTANCE; - } - - @Override - public BackupResult backup(String backupName, String levelPath, String backupSaveDirectory) throws IOException { - /* - 1. Analyze the save directory, to get a json containing md5 values of all files. - 2. Copy new files which we do not have in our backup repository. - 3. Save the above json as a backup file. When restoring from this, - what we have to do is just copy all files back from the repository, - based on their md5 digests. - */ - - boolean success = true; - // Generate JSON - JsonObject hashJson = IncrementalBackupUtil.generateDirectoryJsonObject(levelPath); - // Copy files - long newFilesSizeBytes = IncrementalBackupUtil.saveNewFiles(backupSaveDirectory, levelPath, hashJson); - if(newFilesSizeBytes < 0) { - success = false; - PrintUtil.error("Failed to copy new files to object tree."); - } - // Save JSON tree - File jsonFile = new File(String.valueOf(Paths.get(backupSaveDirectory, BackupFileNameBuilder.objectTreeBackup().build(LocalDateTime.now(), backupName)))); - // TODO - return new BackupResult(success, newFilesSizeBytes); - } - - @Override - public boolean restore(String backupName, String levelPath, String backupSaveDirectory) throws IOException { - return false; - } - - @Override - public BackupFileNameBuilder getBackupFileNameBuilder() { - return null; - } - - @Override - public BackupFileNameFormatter getBackupFileNameFormatter() { - return null; - } -} diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/IncrementalBackupUtil.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/IncrementalBackupUtil.java deleted file mode 100644 index f90aef1..0000000 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/IncrementalBackupUtil.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.keuin.kbackupfabric.operation.backup; - -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.keuin.kbackupfabric.util.FilesystemUtil; -import org.apache.commons.codec.digest.DigestUtils; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.*; -import java.util.Map; - -public class IncrementalBackupUtil { - /** - * Generate a json object representing a directory and its all sub files and directories. - * @param path path to the directory. - * @return a json object. - */ - public static JsonObject generateDirectoryJsonObject(String path) throws IOException { - JsonObject json = new JsonObject(); - File directory = new File(path); - if (!(directory.isDirectory() && directory.exists())) - throw new IOException(String.format("Path %s is not a valid directory.", path)); - - // Iterate all sub files using BFS. - try (DirectoryStream directoryStream = Files.newDirectoryStream(Paths.get(path))) { - for (Path sub : directoryStream) { - if (sub.toFile().isFile()) { - // A sub file - // Just hash and add it as a string - try (InputStream is = Files.newInputStream(sub)) { - String md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(is); - json.addProperty(sub.getFileName().toString(), md5); - } - } else { - // A sub directory - // Search into - json.addProperty(String.valueOf(sub.getFileName()), sub.toString()); - } - } - } - - return json; - } - - /** - * Save new (or modified) files to target path, based on hash json. - * @param targetSavePath where we should save new files. - * @param sourcePath where new files come from. This path must be the base directory of given hash json. - * @param hashJson the json object obtained by calling generateDirectoryJsonObject method. - * @return total size of new files. If failed, will return -1. - */ - public static long saveNewFiles(String targetSavePath, String sourcePath, JsonObject hashJson) throws IOException { - long bytesCopied = 0; - for (Map.Entry entry : hashJson.entrySet()) { - String key = entry.getKey(); - JsonElement value = entry.getValue(); - if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isString()) { - // A sub file - // key is file name - // value is file md5 - String md5 = value.getAsJsonPrimitive().getAsString(); - File saveTarget = new File(targetSavePath, md5); - if (!saveTarget.exists()) { - // Target file does not exist. We have to copy this to the target. - File sourceFile = new File(sourcePath, key); - Files.copy(sourceFile.toPath(), saveTarget.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES); - try { - bytesCopied += sourceFile.length(); - } catch (SecurityException ignored) { - // failed to get the file size. Just ignore this. - } - } - } else if (value.isJsonObject()) { - // A sub directory - // key is directory name - // value is directory json object - // Go into - if(!value.isJsonObject()) - throw new IllegalArgumentException(String.format("Hash json contains illegal argument of a directory item: %s -> %s.", key, value)); - Path pathSource = Paths.get(sourcePath, key); - bytesCopied += saveNewFiles(targetSavePath, pathSource.toString(), value.getAsJsonObject()); - } else { - throw new IllegalArgumentException(String.format("Hash json contains illegal element: %s -> %s.", key, value)); - } - } - return bytesCopied; - } -} diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java index 854355d..9c065a6 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java @@ -2,11 +2,13 @@ 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.builder.BackupFileNameBuilder; -import com.keuin.kbackupfabric.util.backup.formatter.BackupFileNameFormatter; +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; @@ -23,27 +25,32 @@ public class PrimitiveBackupMethod implements BackupMethod { 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 BackupResult backup(String backupName, String levelPath, String backupSaveDirectory) throws IOException { - String backupFileName = BackupFileNameBuilder.primitiveZipBackup().build(LocalDateTime.now(),backupName); + 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(), backupName); - - PrintUtil.info(String.format("zip(srcPath=%s, destPath=%s)", levelPath, backupSaveDirectory)); + 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, backupSaveDirectory, backupFileName, backupMetadata); - + ZipUtil.makeBackupZip(levelPath, backupSavePath, backupFileName, backupMetadata); } catch (ZipUtilException exception) { PrintUtil.info("Infinite recursive of directory tree detected, backup was aborted."); - return new BackupResult(false, 0); + return new PrimitiveBackupFeedback(false, 0); } // Get backup file size and return - return new BackupResult(true, FilesystemUtil.getFileSizeBytes(backupSaveDirectory, backupFileName)); + return new PrimitiveBackupFeedback(true, FilesystemUtil.getFileSizeBytes(backupSavePath, backupFileName)); } @Override - public boolean restore(String backupName, String levelDirectory, String backupSaveDirectory) throws IOException { + 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); @@ -73,10 +80,10 @@ public class PrimitiveBackupMethod implements BackupMethod { // TODO: Refactor this to the concrete BackupMethod. // Decompress archive PrintUtil.info("Decompressing archived level ..."); - ZipUtil.unzip(Paths.get(backupSaveDirectory, backupName).toString(), levelDirectory, false); + 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 visit the project manual at: https://github.com/keuin/KBackup-Fabric/blob/master/README.md"); + 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); @@ -85,14 +92,4 @@ public class PrimitiveBackupMethod implements BackupMethod { return true; } - - @Override - public BackupFileNameBuilder getBackupFileNameBuilder() { - return BackupFileNameBuilder.primitiveZipBackup(); - } - - @Override - public BackupFileNameFormatter getBackupFileNameFormatter() { - return BackupFileNameFormatter.primitiveZipBackup(); - } } 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 new file mode 100644 index 0000000..92a9f39 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/BackupFeedback.java @@ -0,0 +1,5 @@ +package com.keuin.kbackupfabric.operation.backup.feedback; + +public interface BackupFeedback { + String getFeedback(); +} 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 new file mode 100644 index 0000000..6d7a15b --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/PrimitiveBackupFeedback.java @@ -0,0 +1,29 @@ +package com.keuin.kbackupfabric.operation.backup.feedback; + +import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.getFriendlyFileSizeString; + +public class PrimitiveBackupFeedback implements BackupFeedback { + private final boolean success; + private final long backupSizeBytes; + + public PrimitiveBackupFeedback(boolean success, long backupSizeBytes) { + this.success = success; + this.backupSizeBytes = backupSizeBytes; + } + + public boolean isSuccess() { + return success; + } + + public long getBackupSizeBytes() { + return backupSizeBytes; + } + + @Override + public String getFeedback() { + if (success && backupSizeBytes >= 0) + return String.format(" File size: %s.", getFriendlyFileSizeString(backupSizeBytes)); + else + return ""; + } +} 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 { - private final FileIdentifierFactory identifierFactory; + private final FileIdentifierProvider identifierFactory; - public ObjectCollectionFactory(FileIdentifierFactory identifierFactory) { + public ObjectCollectionFactory(FileIdentifierProvider 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/FileIdentifierFactory.java deleted file mode 100644 index 34ad9a0..0000000 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/FileIdentifierFactory.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.keuin.kbackupfabric.util.backup.incremental.identifier; - -import java.io.File; -import java.io.IOException; - -public interface FileIdentifierFactory { - T fromFile(File file) throws IOException; -} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/FileIdentifierProvider.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/FileIdentifierProvider.java new file mode 100644 index 0000000..9a03371 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/FileIdentifierProvider.java @@ -0,0 +1,8 @@ +package com.keuin.kbackupfabric.util.backup.incremental.identifier; + +import java.io.File; +import java.io.IOException; + +public interface FileIdentifierProvider { + 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 factory = Sha256Identifier::fromFile; + private static final FileIdentifierProvider 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 getFactory() { + public static FileIdentifierProvider 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 suggestions = Arrays.asList( - BackupType.OBJECT_TREE_BACKUP.getName(), - BackupType.PRIMITIVE_ZIP_BACKUP.getName() - ); // All backup methods - - public static SuggestionProvider getProvider() { - return (context, builder) -> getCompletableFuture(builder); - } - - private static CompletableFuture 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) { } diff --git a/src/test/java/IncrementalBackupUtilTest.java b/src/test/java/IncrementalBackupUtilTest.java deleted file mode 100644 index b050a91..0000000 --- a/src/test/java/IncrementalBackupUtilTest.java +++ /dev/null @@ -1,16 +0,0 @@ -import com.keuin.kbackupfabric.operation.backup.IncrementalBackupUtil; - -import java.io.IOException; - -public class IncrementalBackupUtilTest { - - @org.junit.Test - public void generateDirectoryJsonObject() { - try { - System.out.println(IncrementalBackupUtil.generateDirectoryJsonObject("D:\\1")); - } catch (IOException exception) { - exception.printStackTrace(); - } - - } -} \ No newline at end of file diff --git a/src/test/java/com/keuin/kbackupfabric/util/backup/name/PrimitiveBackupFileNameEncoderTest.java b/src/test/java/com/keuin/kbackupfabric/util/backup/name/PrimitiveBackupFileNameEncoderTest.java new file mode 100644 index 0000000..4823575 --- /dev/null +++ b/src/test/java/com/keuin/kbackupfabric/util/backup/name/PrimitiveBackupFileNameEncoderTest.java @@ -0,0 +1,21 @@ +package com.keuin.kbackupfabric.util.backup.name; + +import org.junit.Test; + +import java.time.LocalDateTime; +import java.time.ZoneOffset; + +import static org.junit.Assert.assertEquals; + +public class PrimitiveBackupFileNameEncoderTest { + + @Test + public void testConsistency() { + LocalDateTime time = LocalDateTime.ofEpochSecond(System.currentTimeMillis()/1000, 0, ZoneOffset.UTC); + String name = "Test Na_me"; + PrimitiveBackupFileNameEncoder encoder = new PrimitiveBackupFileNameEncoder(); + BackupFileNameEncoder.BackupBasicInformation information = encoder.decode(encoder.encode(name, time)); + assertEquals(time, information.time); + assertEquals(name, information.customName); + } +} \ No newline at end of file -- cgit v1.2.3 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/keuin') 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 From 1c6d68ee303ad3d4b1df5c3d433bf972256f0c8e Mon Sep 17 00:00:00 2001 From: Keuin Date: Wed, 13 Jan 2021 13:23:31 +0800 Subject: Refactor for a better code quality --- src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java | 4 ++-- .../keuin/kbackupfabric/operation/backup/method/BackupMethod.java | 5 +++-- .../operation/backup/method/IncrementalBackupMethod.java | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) (limited to 'src/main/java/com/keuin') diff --git a/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java index 3183cf0..0ff2c40 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java @@ -52,8 +52,8 @@ public class BackupOperation extends InvokableAsyncBlockingOperation { String levelPath = getLevelPath(server); String backupFileName = getBackupFileName(customBackupName); - BackupFeedback result = backupMethod.backup(customBackupName,levelPath,backupSaveDirectory); - if(result.isSuccess()) { + 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/backup/method/BackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/BackupMethod.java index 25a5952..7afbabd 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/BackupMethod.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/BackupMethod.java @@ -12,10 +12,11 @@ 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. + * + * @param customBackupName the custom backup name. * @return if the backup operation succeed. */ - BackupFeedback backup(String backupName, String levelPath, String backupSaveDirectory) throws IOException; + BackupFeedback backup(String customBackupName, 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 index 1dacc44..25e5731 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/IncrementalBackupMethod.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/IncrementalBackupMethod.java @@ -6,7 +6,7 @@ import java.io.IOException; public class IncrementalBackupMethod implements BackupMethod { @Override - public IncrementalBackupFeedback backup(String backupName, String levelPath, String backupSaveDirectory) throws IOException { + public IncrementalBackupFeedback backup(String customBackupName, String levelPath, String backupSaveDirectory) throws IOException { return null; } -- cgit v1.2.3 From 2bc659d8f95a97d0514491e48ed9c66828a4e308 Mon Sep 17 00:00:00 2001 From: Keuin Date: Wed, 13 Jan 2021 14:32:08 +0800 Subject: BackupMethod now becomes stateful --- .../java/com/keuin/kbackupfabric/KBCommands.java | 19 +++-- .../kbackupfabric/operation/BackupOperation.java | 22 ++--- .../kbackupfabric/operation/RestoreOperation.java | 19 ++--- .../operation/backup/method/BackupMethod.java | 23 ----- .../backup/method/ConfiguredBackupMethod.java | 23 +++++ .../method/ConfiguredIncrementalBackupMethod.java | 52 ++++++++++++ .../method/ConfiguredPrimitiveBackupMethod.java | 98 ++++++++++++++++++++++ .../backup/method/IncrementalBackupMethod.java | 17 ---- .../backup/method/PrimitiveBackupMethod.java | 95 --------------------- .../incremental/ObjectCollectionFactory.java | 2 +- .../name/IncrementalBackupFileNameEncoder.java | 4 + 11 files changed, 206 insertions(+), 168 deletions(-) delete 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/ConfiguredBackupMethod.java create mode 100644 src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java create mode 100644 src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java delete mode 100644 src/main/java/com/keuin/kbackupfabric/operation/backup/method/IncrementalBackupMethod.java delete mode 100644 src/main/java/com/keuin/kbackupfabric/operation/backup/method/PrimitiveBackupMethod.java (limited to 'src/main/java/com/keuin') diff --git a/src/main/java/com/keuin/kbackupfabric/KBCommands.java b/src/main/java/com/keuin/kbackupfabric/KBCommands.java index 0e268ce..8f0e451 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBCommands.java +++ b/src/main/java/com/keuin/kbackupfabric/KBCommands.java @@ -5,7 +5,7 @@ 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.method.PrimitiveBackupMethod; +import com.keuin.kbackupfabric.operation.backup.method.ConfiguredPrimitiveBackupMethod; import com.keuin.kbackupfabric.util.PrintUtil; import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil; import com.keuin.kbackupfabric.util.backup.name.PrimitiveBackupFileNameEncoder; @@ -16,6 +16,7 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.ServerCommandSource; import java.io.File; +import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -227,10 +228,18 @@ public final class KBCommands { } } - // Do backup - PrintUtil.info("Invoking backup worker ..."); - //BackupWorker.invoke(context, backupName, metadata); - BackupOperation operation = new BackupOperation(context, customBackupName, PrimitiveBackupMethod.getInstance()); + PrintUtil.info("Start backup..."); + + // configure backup method + MinecraftServer server = context.getSource().getMinecraftServer(); + ConfiguredPrimitiveBackupMethod method = new ConfiguredPrimitiveBackupMethod( + new PrimitiveBackupFileNameEncoder().encode(customBackupName, LocalDateTime.now()), + getLevelPath(server), + getBackupSaveDirectory(server).getAbsolutePath() + ); + + // dispatch to operation worker + BackupOperation operation = new BackupOperation(context, method); if (operation.invoke()) { return SUCCESS; } else if (operation.isBlocked()) { diff --git a/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java index 0ff2c40..645facd 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java @@ -2,7 +2,7 @@ package com.keuin.kbackupfabric.operation; import com.keuin.kbackupfabric.operation.abstracts.InvokableAsyncBlockingOperation; import com.keuin.kbackupfabric.operation.backup.feedback.BackupFeedback; -import com.keuin.kbackupfabric.operation.backup.method.BackupMethod; +import com.keuin.kbackupfabric.operation.backup.method.ConfiguredBackupMethod; import com.keuin.kbackupfabric.util.PrintUtil; import com.mojang.brigadier.context.CommandContext; import net.minecraft.server.MinecraftServer; @@ -15,22 +15,20 @@ import java.util.HashMap; import java.util.Map; import static com.keuin.kbackupfabric.util.PrintUtil.msgInfo; -import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.*; +import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.getBackupSaveDirectory; public class BackupOperation extends InvokableAsyncBlockingOperation { private final CommandContext context; - private final String customBackupName; private final Map oldWorldsSavingDisabled = new HashMap<>(); - private final BackupMethod backupMethod; + private final ConfiguredBackupMethod configuredBackupMethod; private long startTime; - public BackupOperation(CommandContext context, String customBackupName, BackupMethod backupMethod) { + public BackupOperation(CommandContext context, ConfiguredBackupMethod configuredBackupMethod) { super("BackupWorker"); this.context = context; - this.customBackupName = customBackupName; - this.backupMethod = backupMethod; + this.configuredBackupMethod = configuredBackupMethod; } @Override @@ -49,10 +47,8 @@ public class BackupOperation extends InvokableAsyncBlockingOperation { } // Make zip - String levelPath = getLevelPath(server); - String backupFileName = getBackupFileName(customBackupName); - BackupFeedback result = backupMethod.backup(customBackupName, levelPath, backupSaveDirectory); + BackupFeedback result = configuredBackupMethod.backup(); if (result.isSuccess()) { // Restore old auto-save switch stat server.getWorlds().forEach(world -> world.savingDisabled = oldWorldsSavingDisabled.getOrDefault(world, true)); @@ -74,14 +70,14 @@ public class BackupOperation extends InvokableAsyncBlockingOperation { @Override protected boolean sync() { - //// Save world, save old autosave configs + //// Save world, save old auto-save configs - PrintUtil.broadcast(String.format("Making backup %s, please wait ...", customBackupName)); + PrintUtil.broadcast("Making backup, please wait ..."); // Get server MinecraftServer server = context.getSource().getMinecraftServer(); - // Save old autosave switch stat temporally + // Save old auto-save switch state for restoration after finished oldWorldsSavingDisabled.clear(); server.getWorlds().forEach(world -> { oldWorldsSavingDisabled.put(world, world.savingDisabled); diff --git a/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java index 02b76f1..011d2e2 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java @@ -1,37 +1,30 @@ package com.keuin.kbackupfabric.operation; import com.keuin.kbackupfabric.operation.abstracts.InvokableBlockingOperation; -import com.keuin.kbackupfabric.operation.backup.method.BackupMethod; -import com.keuin.kbackupfabric.operation.backup.method.PrimitiveBackupMethod; +import com.keuin.kbackupfabric.operation.backup.method.ConfiguredBackupMethod; +import com.keuin.kbackupfabric.operation.backup.method.ConfiguredPrimitiveBackupMethod; import com.keuin.kbackupfabric.util.PrintUtil; import com.mojang.brigadier.context.CommandContext; import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.ServerCommandSource; -import java.io.File; import java.io.IOException; -import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.getBackupFileName; -import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.getBackupSaveDirectory; - public class RestoreOperation extends InvokableBlockingOperation { //private static final Logger LOGGER = LogManager.getLogger(); private final String backupFileName; private final Thread serverThread; - private final String backupSavePath; - private final String levelPath; private final CommandContext context; private final MinecraftServer server; - private final BackupMethod backupMethod = PrimitiveBackupMethod.getInstance(); + private final ConfiguredBackupMethod configuredBackupMethod; public RestoreOperation(CommandContext context, String backupSavePath, String levelPath, String backupFileName) { server = context.getSource().getMinecraftServer(); this.backupFileName = backupFileName; this.serverThread = server.getThread(); - this.backupSavePath = backupSavePath; - this.levelPath = levelPath; this.context = context; + this.configuredBackupMethod = new ConfiguredPrimitiveBackupMethod(backupFileName, levelPath, backupSavePath); } @Override @@ -39,9 +32,7 @@ public class RestoreOperation extends InvokableBlockingOperation { // do restore to backupName PrintUtil.broadcast(String.format("Restoring to backup %s ...", backupFileName)); - String backupFileName = getBackupFileName(this.backupFileName); PrintUtil.debug("Backup file name: " + backupFileName); - File backupFile = new File(getBackupSaveDirectory(server), backupFileName); PrintUtil.msgInfo(context, "Server will shutdown in a few seconds, depending on world size and disk speed, the progress may take from seconds to minutes.", true); PrintUtil.msgInfo(context, "Please do not force the server stop, or the level would be broken.", true); @@ -94,7 +85,7 @@ public class RestoreOperation extends InvokableBlockingOperation { }while(--cnt > 0); //////////////////// - if (backupMethod.restore(backupFileName, levelPath, backupSavePath)) { + if (configuredBackupMethod.restore()) { //ServerRestartUtil.forkAndRestart(); System.exit(111); } else { 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 deleted file mode 100644 index 7afbabd..0000000 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/BackupMethod.java +++ /dev/null @@ -1,23 +0,0 @@ -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 customBackupName the custom backup name. - * @return if the backup operation succeed. - */ - BackupFeedback backup(String customBackupName, 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/ConfiguredBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredBackupMethod.java new file mode 100644 index 0000000..b1b8d90 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredBackupMethod.java @@ -0,0 +1,23 @@ +package com.keuin.kbackupfabric.operation.backup.method; + +import com.keuin.kbackupfabric.operation.backup.feedback.BackupFeedback; + +import java.io.IOException; + +/** + * Provide specific backup method, which has been configured with proper settings, + * such as saving directory and level path. + */ +public interface ConfiguredBackupMethod { + + /** + * Perform a backup with given method. The backup will be saved as the given name. + * Note: real file name depends on the backup type. + * + * @return backup result. + */ + BackupFeedback backup() throws IOException; + + boolean restore() throws IOException; + +} diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java new file mode 100644 index 0000000..0201d18 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java @@ -0,0 +1,52 @@ +package com.keuin.kbackupfabric.operation.backup.method; + +import com.keuin.kbackupfabric.operation.backup.feedback.IncrementalBackupFeedback; +import com.keuin.kbackupfabric.util.backup.incremental.ObjectCollection; +import com.keuin.kbackupfabric.util.backup.incremental.ObjectCollectionFactory; +import com.keuin.kbackupfabric.util.backup.incremental.ObjectCollectionSerializer; +import com.keuin.kbackupfabric.util.backup.incremental.identifier.Sha256Identifier; +import com.keuin.kbackupfabric.util.backup.incremental.manager.IncrementalBackupStorageManager; +import com.keuin.kbackupfabric.util.backup.name.IncrementalBackupFileNameEncoder; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; +import java.time.LocalDateTime; + +public class ConfiguredIncrementalBackupMethod implements ConfiguredBackupMethod { + + private final String backupFileName; + private final String levelPath; + private final String backupSavePath; + + public ConfiguredIncrementalBackupMethod(String backupFileName, String levelPath, String backupSavePath) { + this.backupFileName = backupFileName; + this.levelPath = levelPath; + this.backupSavePath = backupSavePath; + } + + @Override + public IncrementalBackupFeedback backup() throws IOException { + String customBackupName = new IncrementalBackupFileNameEncoder().decode(backupFileName).customName; + String backupIndexFileName = new IncrementalBackupFileNameEncoder().encode(customBackupName, LocalDateTime.now()); + File levelPathFile = new File(levelPath); + + // construct incremental backup index + ObjectCollection collection = new ObjectCollectionFactory<>(Sha256Identifier.getFactory()) + .fromDirectory(levelPathFile); + + // update storage + IncrementalBackupStorageManager storageManager = new IncrementalBackupStorageManager(Paths.get(backupSavePath)); + int filesAdded = storageManager.addObjectCollection(collection, levelPathFile); + + // save index file + ObjectCollectionSerializer.toFile(collection, new File(backupSavePath, backupIndexFileName)); + + return new IncrementalBackupFeedback(filesAdded >= 0, filesAdded); + } + + @Override + public boolean restore() throws IOException { + return false; + } +} diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java new file mode 100644 index 0000000..c3013e9 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java @@ -0,0 +1,98 @@ +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 ConfiguredPrimitiveBackupMethod implements ConfiguredBackupMethod { + + private final String backupFileName; + private final String levelPath; + private final String backupSavePath; + + public ConfiguredPrimitiveBackupMethod(String backupFileName, String levelPath, String backupSavePath) { + this.backupFileName = backupFileName; + this.levelPath = levelPath; + this.backupSavePath = backupSavePath; + } + + @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() throws IOException { + try { + String customBackupName = new PrimitiveBackupFileNameEncoder().decode(backupFileName).customName; + 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() throws IOException { + // Delete old level + PrintUtil.info("Server stopped. Deleting old level ..."); + File levelDirFile = new File(levelPath); + 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(backupSavePath, backupFileName).toString(), levelPath, 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/method/IncrementalBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/IncrementalBackupMethod.java deleted file mode 100644 index 25e5731..0000000 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/IncrementalBackupMethod.java +++ /dev/null @@ -1,17 +0,0 @@ -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 customBackupName, 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 deleted file mode 100644 index 1f15346..0000000 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/PrimitiveBackupMethod.java +++ /dev/null @@ -1,95 +0,0 @@ -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/incremental/ObjectCollectionFactory.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionFactory.java index ac87883..627cb5c 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 @@ -15,7 +15,7 @@ import java.util.*; * identifiers. Usually, identifier is the combination of hash and other short information (such as size and another hash). * The identifier should use hashes that are strong enough, to prevent possible collisions. */ -public class ObjectCollectionFactory { +public class ObjectCollectionFactory { private final FileIdentifierProvider identifierFactory; 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 index ae54930..a0d4128 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/name/IncrementalBackupFileNameEncoder.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/name/IncrementalBackupFileNameEncoder.java @@ -9,6 +9,10 @@ public class IncrementalBackupFileNameEncoder implements BackupFileNameEncoder { private static final String backupFileNamePrefix = "incremental-"; private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss"); + // TODO: make this private and use singleton pattern + public IncrementalBackupFileNameEncoder() { + } + @Override public String encode(String customName, LocalDateTime time) { if (!isValidCustomName(customName)) -- cgit v1.2.3 From e0c6a21fe9bfb01237fd145064f0af309879a9fb Mon Sep 17 00:00:00 2001 From: Keuin Date: Wed, 13 Jan 2021 17:47:20 +0800 Subject: Incremental backup now works (tested, but not thoroughly) --- .../java/com/keuin/kbackupfabric/KBCommands.java | 44 +++++++++++++-- .../keuin/kbackupfabric/KBCommandsRegister.java | 12 ++-- .../kbackupfabric/operation/BackupOperation.java | 11 +--- .../kbackupfabric/operation/RestoreOperation.java | 34 ++++++----- .../backup/method/ConfiguredBackupMethod.java | 14 +++++ .../method/ConfiguredIncrementalBackupMethod.java | 66 +++++++++++++++++----- .../method/ConfiguredPrimitiveBackupMethod.java | 42 ++++++-------- .../keuin/kbackupfabric/util/FilesystemUtil.java | 38 ++++++++++++- .../util/backup/BackupFilesystemUtil.java | 9 ++- .../incremental/ObjectCollectionFactory.java | 15 +++-- .../manager/IncrementalBackupStorageManager.java | 43 +++++++++++++- .../name/IncrementalBackupFileNameEncoder.java | 2 +- 12 files changed, 248 insertions(+), 82 deletions(-) (limited to 'src/main/java/com/keuin') diff --git a/src/main/java/com/keuin/kbackupfabric/KBCommands.java b/src/main/java/com/keuin/kbackupfabric/KBCommands.java index 8f0e451..847fe7c 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBCommands.java +++ b/src/main/java/com/keuin/kbackupfabric/KBCommands.java @@ -5,9 +5,12 @@ 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.method.ConfiguredBackupMethod; +import com.keuin.kbackupfabric.operation.backup.method.ConfiguredIncrementalBackupMethod; import com.keuin.kbackupfabric.operation.backup.method.ConfiguredPrimitiveBackupMethod; import com.keuin.kbackupfabric.util.PrintUtil; import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil; +import com.keuin.kbackupfabric.util.backup.name.IncrementalBackupFileNameEncoder; import com.keuin.kbackupfabric.util.backup.name.PrimitiveBackupFileNameEncoder; import com.keuin.kbackupfabric.util.backup.suggestion.BackupNameSuggestionProvider; import com.mojang.brigadier.arguments.StringArgumentType; @@ -120,7 +123,7 @@ public final class KBCommands { customBackupName = String.format("a%s", customBackupName); msgWarn(context, String.format("Pure numeric name is not allowed. Renaming to %s", customBackupName)); } - return doBackup(context, customBackupName); + return doBackup(context, customBackupName, false); } /** @@ -130,9 +133,24 @@ public final class KBCommands { * @return stat code. */ public static int primitiveBackupWithDefaultName(CommandContext context) { - return doBackup(context, DEFAULT_BACKUP_NAME); + return doBackup(context, DEFAULT_BACKUP_NAME, false); } + public static int incrementalBackup(CommandContext context) { + String customBackupName = StringArgumentType.getString(context, "backupName"); + if (customBackupName.matches("[0-9]*")) { + // Numeric param is not allowed + customBackupName = String.format("a%s", customBackupName); + msgWarn(context, String.format("Pure numeric name is not allowed. Renaming to %s", customBackupName)); + } + return doBackup(context, customBackupName, true); + } + + public static int incrementalBackupWithDefaultName(CommandContext context) { + return doBackup(context, DEFAULT_BACKUP_NAME, true); + } + + // public static int incrementalBackup(CommandContext context) { // //KBMain.backup("name") // String backupName = StringArgumentType.getString(context, "backupName"); @@ -208,13 +226,24 @@ public final class KBCommands { // Update pending task //pendingOperation = AbstractConfirmableOperation.createRestoreOperation(context, backupName); // File backupFile = new File(getBackupSaveDirectory(server), getBackupFileName(backupName)); - pendingOperation = new RestoreOperation(context, getBackupSaveDirectory(server).getAbsolutePath(), getLevelPath(server), 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); 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; } - private static int doBackup(CommandContext context, String customBackupName) { + private static int doBackup(CommandContext 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") // String backupName = BackupNameTimeFormatter.getTimeString() + "_" + customBackupName; @@ -232,10 +261,15 @@ public final class KBCommands { // configure backup method MinecraftServer server = context.getSource().getMinecraftServer(); - ConfiguredPrimitiveBackupMethod method = new ConfiguredPrimitiveBackupMethod( + 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 diff --git a/src/main/java/com/keuin/kbackupfabric/KBCommandsRegister.java b/src/main/java/com/keuin/kbackupfabric/KBCommandsRegister.java index 4154464..d9ace66 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBCommandsRegister.java +++ b/src/main/java/com/keuin/kbackupfabric/KBCommandsRegister.java @@ -18,18 +18,16 @@ public final class KBCommandsRegister { // register /kb list for showing the backup list. OP is required. dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("list").requires(PermissionValidator::op).executes(KBCommands::list))); -// // register /kb backup incremental [name] -// dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("backup").then( -// CommandManager.literal("incremental").then( -// CommandManager.argument("backupName", StringArgumentType.greedyString()).requires(PermissionValidator::op).executes(KBCommands::incrementalBackup) -// ).requires(PermissionValidator::op).executes(KBCommands::incrementalBackupWithDefaultName))) -// ); - // register /kb backup [name] for performing backup. OP is required. dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("backup").then( CommandManager.argument("backupName", StringArgumentType.greedyString()).requires(PermissionValidator::op).executes(KBCommands::primitiveBackup) ).requires(PermissionValidator::op).executes(KBCommands::primitiveBackupWithDefaultName))); + // register /kb incbak [name] for performing incremental backup. OP is required. + dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("incbak").then( + CommandManager.argument("backupName", StringArgumentType.greedyString()).requires(PermissionValidator::op).executes(KBCommands::incrementalBackup) + ).requires(PermissionValidator::op).executes(KBCommands::incrementalBackupWithDefaultName))); + // register /kb restore for performing restore. OP is required. dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("restore").then(CommandManager.argument("backupName", StringArgumentType.greedyString()).suggests(BackupNameSuggestionProvider.getProvider()).requires(PermissionValidator::op).executes(KBCommands::restore)).executes(KBCommands::list))); diff --git a/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java index 645facd..b38921d 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java @@ -9,13 +9,11 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.world.World; -import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import static com.keuin.kbackupfabric.util.PrintUtil.msgInfo; -import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.getBackupSaveDirectory; public class BackupOperation extends InvokableAsyncBlockingOperation { @@ -39,15 +37,12 @@ public class BackupOperation extends InvokableAsyncBlockingOperation { //// Do our main backup logic // Create backup saving directory - File backupSaveDirectoryFile = getBackupSaveDirectory(server); - backupSaveDirectory = backupSaveDirectoryFile.getName(); - if (!backupSaveDirectoryFile.isDirectory() && !backupSaveDirectoryFile.mkdir()) { - msgInfo(context, String.format("Failed to create backup saving directory: %s. Failed to backup.", backupSaveDirectory)); + if (!configuredBackupMethod.touch()) { + PrintUtil.msgErr(context, "Failed to create backup save directory. Cannot backup."); return; } - // Make zip - + // Backup BackupFeedback result = configuredBackupMethod.backup(); if (result.isSuccess()) { // Restore old auto-save switch stat diff --git a/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java index 011d2e2..b870746 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java @@ -2,37 +2,35 @@ package com.keuin.kbackupfabric.operation; import com.keuin.kbackupfabric.operation.abstracts.InvokableBlockingOperation; import com.keuin.kbackupfabric.operation.backup.method.ConfiguredBackupMethod; -import com.keuin.kbackupfabric.operation.backup.method.ConfiguredPrimitiveBackupMethod; import com.keuin.kbackupfabric.util.PrintUtil; import com.mojang.brigadier.context.CommandContext; import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.ServerCommandSource; import java.io.IOException; +import java.util.Objects; public class RestoreOperation extends InvokableBlockingOperation { //private static final Logger LOGGER = LogManager.getLogger(); - private final String backupFileName; private final Thread serverThread; private final CommandContext context; private final MinecraftServer server; private final ConfiguredBackupMethod configuredBackupMethod; - public RestoreOperation(CommandContext context, String backupSavePath, String levelPath, String backupFileName) { - server = context.getSource().getMinecraftServer(); - this.backupFileName = backupFileName; - this.serverThread = server.getThread(); - this.context = context; - this.configuredBackupMethod = new ConfiguredPrimitiveBackupMethod(backupFileName, levelPath, backupSavePath); + public RestoreOperation(CommandContext context, ConfiguredBackupMethod configuredBackupMethod) { + server = Objects.requireNonNull(context.getSource().getMinecraftServer()); + this.serverThread = Objects.requireNonNull(server.getThread()); + this.context = Objects.requireNonNull(context); + this.configuredBackupMethod = Objects.requireNonNull(configuredBackupMethod); } @Override protected boolean blockingContext() { // do restore to backupName - PrintUtil.broadcast(String.format("Restoring to backup %s ...", backupFileName)); + PrintUtil.broadcast(String.format("Restoring to backup %s ...", configuredBackupMethod.getBackupFileName())); - PrintUtil.debug("Backup file name: " + backupFileName); + PrintUtil.debug("Backup file name: " + configuredBackupMethod.getBackupFileName()); PrintUtil.msgInfo(context, "Server will shutdown in a few seconds, depending on world size and disk speed, the progress may take from seconds to minutes.", true); PrintUtil.msgInfo(context, "Please do not force the server stop, or the level would be broken.", true); @@ -58,7 +56,7 @@ public class RestoreOperation extends InvokableBlockingOperation { @Override public String toString() { - return String.format("restoration from %s", backupFileName); + return String.format("restoration from %s", configuredBackupMethod.getBackupFileName()); } private class WorkerThread implements Runnable { @@ -71,7 +69,7 @@ public class RestoreOperation extends InvokableBlockingOperation { while (serverThread.isAlive()) { try { serverThread.join(); - } catch (InterruptedException ignored) { + } catch (InterruptedException | RuntimeException ignored) { } } @@ -85,7 +83,15 @@ public class RestoreOperation extends InvokableBlockingOperation { }while(--cnt > 0); //////////////////// + long startTime = System.currentTimeMillis(); if (configuredBackupMethod.restore()) { + 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"); //ServerRestartUtil.forkAndRestart(); System.exit(111); } else { @@ -94,9 +100,11 @@ public class RestoreOperation extends InvokableBlockingOperation { } catch (SecurityException e) { PrintUtil.error("An exception occurred while restoring: " + e.getMessage()); + e.printStackTrace(); } catch (IOException e) { PrintUtil.error(e.toString()); - PrintUtil.error("Failed to restore."); + PrintUtil.error("Failed to restore due to an unhandled I/O exception."); + e.printStackTrace(); } System.exit(0); // all failed restoration will eventually go here } diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredBackupMethod.java index b1b8d90..bb80c80 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredBackupMethod.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredBackupMethod.java @@ -20,4 +20,18 @@ public interface ConfiguredBackupMethod { boolean restore() throws IOException; + /** + * Create backup save directory and do some essential initialization before the backup process. + * + * @return false if failed, then the backup process won't proceed. + */ + boolean touch(); + + /** + * Get the used backup file name. + * + * @return the file name. + */ + String getBackupFileName(); + } diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java index 0201d18..b5d2463 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java @@ -1,52 +1,92 @@ package com.keuin.kbackupfabric.operation.backup.method; import com.keuin.kbackupfabric.operation.backup.feedback.IncrementalBackupFeedback; +import com.keuin.kbackupfabric.util.FilesystemUtil; +import com.keuin.kbackupfabric.util.PrintUtil; import com.keuin.kbackupfabric.util.backup.incremental.ObjectCollection; import com.keuin.kbackupfabric.util.backup.incremental.ObjectCollectionFactory; import com.keuin.kbackupfabric.util.backup.incremental.ObjectCollectionSerializer; import com.keuin.kbackupfabric.util.backup.incremental.identifier.Sha256Identifier; import com.keuin.kbackupfabric.util.backup.incremental.manager.IncrementalBackupStorageManager; -import com.keuin.kbackupfabric.util.backup.name.IncrementalBackupFileNameEncoder; import java.io.File; import java.io.IOException; import java.nio.file.Paths; -import java.time.LocalDateTime; +import java.util.Arrays; +import java.util.HashSet; public class ConfiguredIncrementalBackupMethod implements ConfiguredBackupMethod { - private final String backupFileName; + private final String backupIndexFileName; private final String levelPath; - private final String backupSavePath; + private final String backupIndexFileSaveDirectory; + private final String backupBaseDirectory; - public ConfiguredIncrementalBackupMethod(String backupFileName, String levelPath, String backupSavePath) { - this.backupFileName = backupFileName; + public ConfiguredIncrementalBackupMethod(String backupIndexFileName, String levelPath, String backupIndexFileSaveDirectory, String backupBaseDirectory) { + this.backupIndexFileName = backupIndexFileName; this.levelPath = levelPath; - this.backupSavePath = backupSavePath; + this.backupIndexFileSaveDirectory = backupIndexFileSaveDirectory; + this.backupBaseDirectory = backupBaseDirectory; } @Override public IncrementalBackupFeedback backup() throws IOException { - String customBackupName = new IncrementalBackupFileNameEncoder().decode(backupFileName).customName; - String backupIndexFileName = new IncrementalBackupFileNameEncoder().encode(customBackupName, LocalDateTime.now()); File levelPathFile = new File(levelPath); // construct incremental backup index + PrintUtil.info("Hashing files..."); ObjectCollection collection = new ObjectCollectionFactory<>(Sha256Identifier.getFactory()) - .fromDirectory(levelPathFile); + .fromDirectory(levelPathFile, new HashSet<>(Arrays.asList("session.lock", "kbackup_metadata"))); // update storage - IncrementalBackupStorageManager storageManager = new IncrementalBackupStorageManager(Paths.get(backupSavePath)); + PrintUtil.info("Copying files..."); + IncrementalBackupStorageManager storageManager = new IncrementalBackupStorageManager(Paths.get(backupBaseDirectory)); int filesAdded = storageManager.addObjectCollection(collection, levelPathFile); // save index file - ObjectCollectionSerializer.toFile(collection, new File(backupSavePath, backupIndexFileName)); + PrintUtil.info("Saving index file..."); + ObjectCollectionSerializer.toFile(collection, new File(backupIndexFileSaveDirectory, backupIndexFileName)); + // return result + PrintUtil.info("Incremental backup finished."); return new IncrementalBackupFeedback(filesAdded >= 0, filesAdded); } @Override public boolean restore() throws IOException { - return false; + // load collection + PrintUtil.info("Loading file list..."); + ObjectCollection collection = ObjectCollectionSerializer.fromFile( + new File(backupIndexFileSaveDirectory, backupIndexFileName) + ); + + // delete old level + File levelPathFile = new File(levelPath); + PrintUtil.info("Deleting old level..."); + if (!FilesystemUtil.forceDeleteDirectory(levelPathFile)) { + PrintUtil.info("Failed to delete old level!"); + return false; + } + + // restore file + PrintUtil.info("Copying files..."); + IncrementalBackupStorageManager storageManager = new IncrementalBackupStorageManager(Paths.get(backupBaseDirectory)); + int restoreObjectCount = storageManager.restoreObjectCollection(collection, levelPathFile); + + PrintUtil.info(String.format("%d file(s) restored.", restoreObjectCount)); + return true; + } + + @Override + public boolean touch() { + File baseDirectoryFile = new File(backupBaseDirectory); + return baseDirectoryFile.isDirectory() || baseDirectoryFile.mkdir(); + } + + @Override + public String getBackupFileName() { + return backupIndexFileName; } + + } diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java index c3013e9..1c3c9f6 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java @@ -15,8 +15,6 @@ import java.io.IOException; import java.nio.file.Paths; import java.time.LocalDateTime; -import static org.apache.commons.io.FileUtils.forceDelete; - public class ConfiguredPrimitiveBackupMethod implements ConfiguredBackupMethod { private final String backupFileName; @@ -56,37 +54,17 @@ public class ConfiguredPrimitiveBackupMethod implements ConfiguredBackupMethod { public boolean restore() throws IOException { // Delete old level PrintUtil.info("Server stopped. Deleting old level ..."); - File levelDirFile = new File(levelPath); - 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())); + if (!FilesystemUtil.forceDeleteDirectory(new File(levelPath))) { + PrintUtil.info("Failed to delete old level!"); return false; } + // TODO: Refactor this to the concrete BackupMethod. // Decompress archive PrintUtil.info("Decompressing archived level ..."); ZipUtil.unzip(Paths.get(backupSavePath, backupFileName).toString(), levelPath, 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); @@ -95,4 +73,16 @@ public class ConfiguredPrimitiveBackupMethod implements ConfiguredBackupMethod { return true; } + + @Override + public boolean touch() { + File backupSaveDirectoryFile = new File(backupSavePath); + return backupSaveDirectoryFile.isDirectory() || backupSaveDirectoryFile.mkdir(); + } + + @Override + public String getBackupFileName() { + return backupFileName; + } + } diff --git a/src/main/java/com/keuin/kbackupfabric/util/FilesystemUtil.java b/src/main/java/com/keuin/kbackupfabric/util/FilesystemUtil.java index 7f74725..f245cff 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/FilesystemUtil.java +++ b/src/main/java/com/keuin/kbackupfabric/util/FilesystemUtil.java @@ -1,6 +1,9 @@ package com.keuin.kbackupfabric.util; import java.io.File; +import java.io.IOException; + +import static org.apache.commons.io.FileUtils.forceDelete; public class FilesystemUtil { @@ -22,12 +25,43 @@ public class FilesystemUtil { public static long getFileSizeBytes(String filePath) { long fileSize = -1; - try{ + try { File backupZipFile = new File(filePath); fileSize = backupZipFile.length(); - } catch (SecurityException ignored){ + } catch (SecurityException ignored) { } return fileSize; } + public static boolean forceDeleteDirectory(File levelDirFile) throws IOException { + int failedCounter = 0; + final int MAX_RETRY_TIMES = 20; + IOException exception = null; + while (failedCounter < MAX_RETRY_TIMES) { + System.gc(); + if (!levelDirFile.delete() && levelDirFile.exists()) { + System.gc(); + try { + forceDelete(levelDirFile); // Try to force delete. + } catch (IOException e) { + exception = e; + } + } + if (!levelDirFile.exists()) + break; + ++failedCounter; + try { + Thread.sleep(500); + } catch (InterruptedException ignored) { + } + } + if (exception != null) + throw exception; + if (levelDirFile.exists()) { + PrintUtil.error(String.format("Cannot restore: failed to delete old level %s .", levelDirFile.getName())); + return false; + } + return true; + } + } 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 c1aa5fe..837b638 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/BackupFilesystemUtil.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/BackupFilesystemUtil.java @@ -14,7 +14,8 @@ import java.util.regex.Pattern; */ public final class BackupFilesystemUtil { - private static final String backupSaveDirectoryName = "backups"; + private static final String BACKUP_SAVE_DIRECTORY_NAME = "backups"; + private static final String INCREMENTAL_BASE_DIRECTORY_NAME = "incremental"; private static final String backupFileNamePrefix = "kbackup-"; @Deprecated @@ -43,7 +44,11 @@ public final class BackupFilesystemUtil { } public static File getBackupSaveDirectory(MinecraftServer server) { - return new File(server.getRunDirectory(), backupSaveDirectoryName); + return new File(server.getRunDirectory(), BACKUP_SAVE_DIRECTORY_NAME); + } + + public static File getIncrementalBackupBaseDirectory(MinecraftServer server) { + return new File(server.getRunDirectory(), INCREMENTAL_BASE_DIRECTORY_NAME); } public static String getLevelPath(MinecraftServer server) { 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 627cb5c..2f3761c 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,5 +1,6 @@ package com.keuin.kbackupfabric.util.backup.incremental; +import com.keuin.kbackupfabric.util.PrintUtil; import com.keuin.kbackupfabric.util.backup.incremental.identifier.FileIdentifierProvider; import com.keuin.kbackupfabric.util.backup.incremental.identifier.ObjectIdentifier; @@ -23,26 +24,32 @@ public class ObjectCollectionFactory { this.identifierFactory = identifierFactory; } - public ObjectCollection fromDirectory(File directory) throws IOException { + public ObjectCollection fromDirectory(File directory, Set ignoredFiles) throws IOException { final Set subFiles = new HashSet<>(); final Map subCollections = new HashMap<>(); if (!Objects.requireNonNull(directory).isDirectory()) throw new IllegalArgumentException("given file is not a directory"); - for (Iterator iter = Files.walk(directory.toPath(), 1).iterator(); iter.hasNext();) { + for (Iterator iter = Files.walk(directory.toPath(), 1).iterator(); iter.hasNext(); ) { Path path = iter.next(); if (Files.isSameFile(path, directory.toPath())) continue; File file = path.toFile(); if (file.isDirectory()) { - subCollections.put(file.getName(), fromDirectory(file)); - } else { + subCollections.put(file.getName(), fromDirectory(file, ignoredFiles)); + } else if (!ignoredFiles.contains(file.getName())) { subFiles.add(new ObjectElement(file.getName(), identifierFactory.fromFile(file))); + } else { + PrintUtil.info(String.format("Skipping file %s.", file.getName())); } } return new ObjectCollection(directory.getName(), subFiles, subCollections); } + public ObjectCollection fromDirectory(File directory) throws IOException { + return fromDirectory(directory, Collections.emptySet()); + } + } diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/manager/IncrementalBackupStorageManager.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/manager/IncrementalBackupStorageManager.java index cd15499..a4271c6 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/manager/IncrementalBackupStorageManager.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/manager/IncrementalBackupStorageManager.java @@ -1,9 +1,11 @@ package com.keuin.kbackupfabric.util.backup.incremental.manager; +import com.keuin.kbackupfabric.util.PrintUtil; import com.keuin.kbackupfabric.util.backup.incremental.ObjectCollection; import com.keuin.kbackupfabric.util.backup.incremental.ObjectElement; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -11,6 +13,8 @@ import java.nio.file.Paths; import java.util.Map; import java.util.Objects; +import static org.apache.commons.io.FileUtils.forceDelete; + public class IncrementalBackupStorageManager { private final Path backupStorageBase; @@ -63,13 +67,50 @@ public class IncrementalBackupStorageManager { int copyCount = 0; + // touch directory + if (!collectionBasePath.exists()) { + int retryCounter = 0; + boolean success = false; + while (retryCounter++ < 5) { + if (collectionBasePath.mkdirs()) { + success = true; + break; + } + } + if (!success) { + throw new IOException("Failed to create directory " + collectionBasePath.getAbsolutePath()); + } + } + // copy sub files for (Map.Entry entry : collection.getElementMap().entrySet()) { File copySource = new File(backupStorageBase.toFile(), entry.getValue().getIdentifier().getIdentification()); + File copyTarget = new File(collectionBasePath.getAbsolutePath(), entry.getKey()); + if (!baseContainsObject(entry.getValue())) { throw new IOException(String.format("File %s does not exist in the base.", copySource.getName())); } - Files.copy(copySource.toPath(), Paths.get(collectionBasePath.getAbsolutePath(), entry.getKey())); + if (copyTarget.exists()) { + boolean successDeleting = false; + for (int i = 0; i < 5; ++i) { + try { + forceDelete(copyTarget); + successDeleting = true; + break; + } catch (FileNotFoundException ignored) { + break; + } catch (IOException e) { + PrintUtil.error(String.format("Failed to delete file %s, retry.", copyTarget.getName())); + } + } + if (!successDeleting) { + String msg = String.format("Failed to delete file %s.", copyTarget.getName()); + PrintUtil.error(msg); + throw new IOException(msg); + } + } + + Files.copy(copySource.toPath(), copyTarget.toPath()); ++copyCount; } 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 index a0d4128..3c35201 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/name/IncrementalBackupFileNameEncoder.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/name/IncrementalBackupFileNameEncoder.java @@ -6,7 +6,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; public class IncrementalBackupFileNameEncoder implements BackupFileNameEncoder { - private static final String backupFileNamePrefix = "incremental-"; + private static final String backupFileNamePrefix = "incremental"; private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss"); // TODO: make this private and use singleton pattern -- cgit v1.2.3 From 5410762b8272feca0680f7a48f895714b91e3df7 Mon Sep 17 00:00:00 2001 From: Keuin Date: Wed, 13 Jan 2021 19:39:55 +0800 Subject: Add a more comprehensive test for incremental backup --- .gitignore | 1 + .../manager/IncrementalBackupStorageManager.java | 4 + .../ConfiguredIncrementalBackupMethodTest.java | 193 +++++++++++++++++++++ 3 files changed, 198 insertions(+) create mode 100644 src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java (limited to 'src/main/java/com/keuin') diff --git a/.gitignore b/.gitignore index 550b373..ed36e39 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,4 @@ bin/ # fabric run/ +logs/ \ No newline at end of file diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/manager/IncrementalBackupStorageManager.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/manager/IncrementalBackupStorageManager.java index a4271c6..6c359c6 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/manager/IncrementalBackupStorageManager.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/manager/IncrementalBackupStorageManager.java @@ -30,6 +30,10 @@ public class IncrementalBackupStorageManager { * @throws IOException I/O Error. */ public int addObjectCollection(ObjectCollection collection, File collectionBasePath) throws IOException { + if (!backupStorageBase.toFile().isDirectory()) { + if (!backupStorageBase.toFile().mkdirs()) + throw new IOException("Backup storage base directory does not exist, and failed to create it."); + } Objects.requireNonNull(collection); Objects.requireNonNull(collectionBasePath); diff --git a/src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java b/src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java new file mode 100644 index 0000000..7e4f267 --- /dev/null +++ b/src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java @@ -0,0 +1,193 @@ +package com.keuin.kbackupfabric.operation.backup.method; + +import org.apache.commons.codec.digest.DigestUtils; +import org.junit.Test; + +import java.io.*; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; +import java.util.function.Function; + +import static org.apache.commons.io.FileUtils.forceDelete; +import static org.junit.Assert.*; + +public class ConfiguredIncrementalBackupMethodTest { + + private final String testTempPath = "R:\\"; + private final String sourceDirectoryName = "source"; + private final String destDirectoryName = "destination"; + private final String indexFileName = "index"; + + private final double directoryFactor = 0.4; + private final double fileFactor = 0.1; + private final int maxRandomFileSizeBytes = 1024 * 1024; + private final Function scaleDecayFunc = (x) -> x - 1; + + @Test + public void iterationTest() throws IOException { + int a = 100; + for (int i = 0; i < a; ++i) { + performTest(Math.min(i + 1, 10)); + System.out.println("Round " + i + " passed."); + } + } + + private void performTest(int scale) throws IOException { + + // init source and destination + final Path sourcePath = Paths.get(testTempPath, sourceDirectoryName); + final Path destPath = Paths.get(testTempPath, destDirectoryName); + if (new File(sourcePath.toString()).exists()) { + forceDelete(new File(sourcePath.toString())); + if (!new File(sourcePath.toString()).mkdirs()) + fail(); + } + if (new File(destPath.toString()).exists()) { + forceDelete(new File(destPath.toString())); + if (!new File(destPath.toString()).mkdirs()) + fail(); + } + if (new File(testTempPath, indexFileName).exists()) { + if (!new File(testTempPath, indexFileName).delete()) + fail(); + } + + // initialize src + createRandomDirectoryTree(sourcePath.toString(), scale); + + String hash1 = calcMD5HashForDir(sourcePath.toFile(), true); + + // copy src to dest + ConfiguredIncrementalBackupMethod method = new ConfiguredIncrementalBackupMethod( + indexFileName, + sourcePath.toString(), + testTempPath, + destPath.toString() + ); + method.backup(); + + // delete src + forceDelete(sourcePath.toFile()); + assertFalse(sourcePath.toFile().isDirectory()); + + // restore src + if (!method.restore()) + fail(); + + String hash2 = calcMD5HashForDir(sourcePath.toFile(), true); + + assertEquals(hash1, hash2); + } + + private void createRandomDirectoryTree(String path, int scale) throws IOException { + if (scale <= 0) { + if (Math.random() < 0.5) + if (!new File(path).mkdirs() && !new File(path).exists()) + throw new IOException("Failed to create directory " + path); + return; + } + if (!new File(path).isDirectory() && !new File(path).mkdirs()) + throw new IOException("Failed to create directory " + path); + + int subFileCount = (int) Math.round(Math.random() * 10 * scale * fileFactor); + for (int i = 0; i < subFileCount; i++) { + String subFile = null; + while (subFile == null || new File(path, subFile).exists()) + subFile = getRandomString((int) (Math.random() * 16 + 1)); + createRandomFile(new File(path, subFile), maxRandomFileSizeBytes); + } + + + int subDirCount = (int) Math.round(Math.random() * 10 * scale * directoryFactor); + for (int i = 0; i < subDirCount; i++) { + String subDir = null; + while (subDir == null || new File(path, subDir).exists()) + subDir = getRandomString((int) (Math.random() * 16 + 1)); + createRandomDirectoryTree(new File(path, subDir).getAbsolutePath(), scaleDecayFunc.apply(scale)); + } + } + + private static void createRandomFile(File file, int maxSizeBytes) throws IOException { + if (!file.createNewFile()) + throw new IOException("Failed to create file " + file.getAbsolutePath()); + try (FileOutputStream fileOutputStream = new FileOutputStream(file)) { + int fileBytes = (int) (maxSizeBytes * Math.random() + 1); + Random random = new Random(); + final int chunkSize = 1024 * 4; + byte[] randomChunk = new byte[chunkSize]; + for (int i = 0; i < fileBytes / chunkSize; i++) { + random.nextBytes(randomChunk); + fileOutputStream.write(randomChunk); + } + if (fileBytes % chunkSize != 0) { + randomChunk = new byte[fileBytes % chunkSize]; + random.nextBytes(randomChunk); + fileOutputStream.write(randomChunk); + } + } + } + + private static String getRandomString(int length) { + String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + Random random = new Random(); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < length; i++) { + int number = random.nextInt(62); + sb.append(str.charAt(number)); + } + return sb.toString(); + } + + public String calcMD5HashForDir(File dirToHash, boolean includeHiddenFiles) { + + assert (dirToHash.isDirectory()); + Vector fileStreams = new Vector<>(); + + System.out.println("Found files for hashing:"); + collectInputStreams(dirToHash, fileStreams, includeHiddenFiles); + + SequenceInputStream seqStream = + new SequenceInputStream(fileStreams.elements()); + + try { + String md5Hash = DigestUtils.md5Hex(seqStream); + seqStream.close(); + return md5Hash; + } catch (IOException e) { + throw new RuntimeException("Error reading files to hash in " + + dirToHash.getAbsolutePath(), e); + } + + } + + private void collectInputStreams(File dir, + List foundStreams, + boolean includeHiddenFiles) { + + File[] fileList = dir.listFiles(); + Arrays.sort(fileList, // Need in reproducible order + new Comparator() { + public int compare(File f1, File f2) { + return f1.getName().compareTo(f2.getName()); + } + }); + + for (File f : fileList) { + if (!includeHiddenFiles && f.getName().startsWith(".")) { + // Skip it + } else if (f.isDirectory()) { + collectInputStreams(f, foundStreams, includeHiddenFiles); + } else { + try { + System.out.println("\t" + f.getAbsolutePath()); + foundStreams.add(new FileInputStream(f)); + } catch (FileNotFoundException e) { + throw new AssertionError(e.getMessage() + + ": file should never not be found!"); + } + } + } + + } +} \ No newline at end of file -- cgit v1.2.3 From 6baece29cccc906651331cbf8a90a06f8cee045b Mon Sep 17 00:00:00 2001 From: Keuin Date: Thu, 14 Jan 2021 13:00:52 +0800 Subject: Fix a minor naming bug. Code refactor. Improve test. --- .../java/com/keuin/kbackupfabric/KBCommands.java | 4 +- .../com/keuin/kbackupfabric/util/BytesUtil.java | 17 ++++++- .../util/backup/BackupFilesystemUtil.java | 4 -- .../identifier/FileIdentifierProvider.java | 7 +++ .../incremental/identifier/Sha256Identifier.java | 20 ++++++++- .../identifier/SingleHashIdentifier.java | 16 ++++++- .../identifier/StorageObjectLoader.java | 25 +++++++++++ .../manager/IncrementalBackupStorageManager.java | 52 +++++++++++++++++++++- .../util/backup/name/BackupFileNameEncoder.java | 8 +++- .../name/IncrementalBackupFileNameEncoder.java | 2 +- .../name/PrimitiveBackupFileNameEncoder.java | 2 +- .../backup/provider/AvailableBackupProvider.java | 8 ++++ .../provider/IncrementalBackupInformation.java | 13 ++++++ .../provider/PrimitiveBackupInformation.java | 14 ++++++ .../ConfiguredIncrementalBackupMethodTest.java | 34 +++++++++++--- .../incremental/ObjectCollectionFactoryTest.java | 14 +++--- .../identifier/Sha256IdentifierTest.java | 2 +- .../name/IncrementalBackupFileNameEncoderTest.java | 38 ++++++++++++++++ .../name/PrimitiveBackupFileNameEncoderTest.java | 33 +++++++++++++- 19 files changed, 281 insertions(+), 32 deletions(-) create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/StorageObjectLoader.java create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/provider/AvailableBackupProvider.java create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/provider/IncrementalBackupInformation.java create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/provider/PrimitiveBackupInformation.java create mode 100644 src/test/java/com/keuin/kbackupfabric/util/backup/name/IncrementalBackupFileNameEncoderTest.java (limited to 'src/main/java/com/keuin') diff --git a/src/main/java/com/keuin/kbackupfabric/KBCommands.java b/src/main/java/com/keuin/kbackupfabric/KBCommands.java index 847fe7c..98f6369 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBCommands.java +++ b/src/main/java/com/keuin/kbackupfabric/KBCommands.java @@ -84,7 +84,9 @@ public final class KBCommands { public static int list(CommandContext context) { MinecraftServer server = context.getSource().getMinecraftServer(); File[] files = getBackupSaveDirectory(server).listFiles( - (dir, name) -> dir.isDirectory() && name.toLowerCase().endsWith(".zip") && name.toLowerCase().startsWith(getBackupFileNamePrefix()) + (dir, name) -> dir.isDirectory() && + (name.toLowerCase().endsWith(".zip") && name.toLowerCase().startsWith(getBackupFileNamePrefix()) + || name.toLowerCase().endsWith(".kbi")) ); synchronized (backupFileNameList) { diff --git a/src/main/java/com/keuin/kbackupfabric/util/BytesUtil.java b/src/main/java/com/keuin/kbackupfabric/util/BytesUtil.java index 6ded7b8..c33c028 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/BytesUtil.java +++ b/src/main/java/com/keuin/kbackupfabric/util/BytesUtil.java @@ -4,6 +4,7 @@ import java.nio.charset.StandardCharsets; public class BytesUtil { private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII); + public static String bytesToHex(byte[] bytes) { byte[] hexChars = new byte[bytes.length * 2]; for (int j = 0; j < bytes.length; j++) { @@ -11,6 +12,20 @@ public class BytesUtil { hexChars[j * 2] = HEX_ARRAY[v >>> 4]; hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; } - return new String(hexChars, StandardCharsets.UTF_8); + return new String(hexChars, StandardCharsets.UTF_8).toUpperCase(); + } + + public static byte[] hexToBytes(String s) { + int len = s.length(); + if (len % 2 != 0) + throw new IllegalArgumentException("Invalid hex string."); + byte[] b = new byte[len / 2]; + int index, v; + for (int i = 0; i < b.length; i++) { + index = i * 2; + v = Integer.parseInt(s.substring(index, index + 2), 16); + b[i] = (byte) v; + } + return b; } } 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 837b638..7106ad2 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/BackupFilesystemUtil.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/BackupFilesystemUtil.java @@ -23,10 +23,6 @@ public final class BackupFilesystemUtil { return backupFileNamePrefix; } - @Deprecated - public static String getBackupFileName(String backupName) { - return backupFileNamePrefix + backupName + ".zip"; - } // @Deprecated // public static String getBackupName(String backupFileName) { diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/FileIdentifierProvider.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/FileIdentifierProvider.java index 9a03371..3fbe284 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/FileIdentifierProvider.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/FileIdentifierProvider.java @@ -4,5 +4,12 @@ import java.io.File; import java.io.IOException; public interface FileIdentifierProvider { + /** + * Generate file identifier from a random file. The file is not necessarily in the object base. + * + * @param file the file. + * @return the file identifier. + * @throws IOException when an I/O error occurs. + */ 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 cbf1bb9..c1c87e1 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 @@ -1,5 +1,7 @@ package com.keuin.kbackupfabric.util.backup.incremental.identifier; +import com.keuin.kbackupfabric.util.BytesUtil; + import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -16,20 +18,34 @@ 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 FileIdentifierProvider factory = Sha256Identifier::fromFile; + private static final String marker = "S2"; public static Sha256Identifier fromFile(File file) throws IOException { - if (!Objects.requireNonNull(file).isFile()) { + if (!file.isFile()) { throw new IllegalArgumentException("file is not a file"); } return new Sha256Identifier(DUMMY.hash(file)); } + /** + * Load sha-256 from a named file. Only used in StorageObjectLoader. + * + * @param fileName the file name. + * @return identifier. + */ + static Sha256Identifier fromFileName(String fileName) { + if (!fileName.matches(marker + "-[0-9A-Fa-f]{32}")) + return null; + String hexString = fileName.substring(marker.length() + 1); + return new Sha256Identifier(BytesUtil.hexToBytes(hexString)); + } + public static FileIdentifierProvider getFactory() { return factory; } protected Sha256Identifier(byte[] hash) { - super(hash); + super(hash, marker); Objects.requireNonNull(hash); if (hash.length != SHA256_LENGTH) { throw new IllegalStateException(String.format("SHA256 must be %d bytes", SHA256_LENGTH)); diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java index 3b96f79..0f62f2b 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java @@ -5,6 +5,7 @@ import com.keuin.kbackupfabric.util.BytesUtil; import java.io.File; import java.io.IOException; import java.util.Arrays; +import java.util.Objects; /** * A simple identifier based on a single hash function. @@ -13,9 +14,13 @@ import java.util.Arrays; public abstract class SingleHashIdentifier implements ObjectIdentifier { private final byte[] hash; + private final String type; - protected SingleHashIdentifier(byte[] hash) { + protected SingleHashIdentifier(byte[] hash, String type) { + Objects.requireNonNull(hash); + Objects.requireNonNull(type); this.hash = Arrays.copyOf(hash, hash.length); + this.type = type; } /** @@ -28,7 +33,7 @@ public abstract class SingleHashIdentifier implements ObjectIdentifier { @Override public String getIdentification() { - return BytesUtil.bytesToHex(hash); + return type + "-" + BytesUtil.bytesToHex(hash); } @Override @@ -38,4 +43,11 @@ public abstract class SingleHashIdentifier implements ObjectIdentifier { } return Arrays.equals(hash, ((SingleHashIdentifier) obj).hash); } + + @Override + public int hashCode() { + int result = Objects.hash(type); + result = 31 * result + Arrays.hashCode(hash); + return result; + } } diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/StorageObjectLoader.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/StorageObjectLoader.java new file mode 100644 index 0000000..96bc295 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/StorageObjectLoader.java @@ -0,0 +1,25 @@ +package com.keuin.kbackupfabric.util.backup.incremental.identifier; + +import java.io.File; +import java.util.Objects; + +public class StorageObjectLoader { + /** + * Get identifier from storage file. + * + * @param file storage file. + * @return identifier. If failed, return null. + */ + public static ObjectIdentifier asIdentifier(File file) { + Objects.requireNonNull(file); + String fileName = file.getName(); + ObjectIdentifier identifier; + + identifier = Sha256Identifier.fromFileName(fileName); + if (identifier != null) + return identifier; + + // Add more identifiers. + return null; + } +} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/manager/IncrementalBackupStorageManager.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/manager/IncrementalBackupStorageManager.java index 6c359c6..6fd339b 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/manager/IncrementalBackupStorageManager.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/manager/IncrementalBackupStorageManager.java @@ -3,6 +3,8 @@ package com.keuin.kbackupfabric.util.backup.incremental.manager; import com.keuin.kbackupfabric.util.PrintUtil; import com.keuin.kbackupfabric.util.backup.incremental.ObjectCollection; import com.keuin.kbackupfabric.util.backup.incremental.ObjectElement; +import com.keuin.kbackupfabric.util.backup.incremental.identifier.ObjectIdentifier; +import com.keuin.kbackupfabric.util.backup.incremental.identifier.StorageObjectLoader; import java.io.File; import java.io.FileNotFoundException; @@ -10,14 +12,15 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Map; -import java.util.Objects; +import java.util.*; import static org.apache.commons.io.FileUtils.forceDelete; public class IncrementalBackupStorageManager { private final Path backupStorageBase; + private final Map map = new HashMap<>(); + private boolean loaded = false; public IncrementalBackupStorageManager(Path backupStorageBase) { this.backupStorageBase = backupStorageBase; @@ -127,8 +130,31 @@ public class IncrementalBackupStorageManager { return copyCount; } + public int cleanUnusedObjects(Iterable collectionIterable) { + // construct object list in memory + Set objects = new HashSet<>(); +// backupStorageBase + + for (ObjectCollection collection : collectionIterable) { + for (ObjectElement ele : collection.getElementMap().values()) { + + } + } + throw new RuntimeException("not impl"); + } + + /** + * Check all objects, return unused ones. + * + * @return the unused ones. + */ + private Map markUnusedObjects() { + throw new RuntimeException("not impl"); + } + /** * Check if the backup base contains given element. + * * @param objectElement the element. * @return true or false. */ @@ -137,4 +163,26 @@ public class IncrementalBackupStorageManager { return (new File(backupStorageBase.toFile(), objectElement.getIdentifier().getIdentification())).exists(); } + private void lazyLoadStorage() throws IOException { + if (!loaded) { + loadStorage(); + loaded = true; + } + } + + private synchronized void loadStorage() throws IOException { + map.clear(); + Files.walk(backupStorageBase, 1).forEach(path -> { + File file = path.toFile(); + ObjectIdentifier identifier = StorageObjectLoader.asIdentifier(file); + if (identifier == null) { + map.clear(); + throw new IllegalStateException(String.format( + "Bad storage object %s: cannot recognize identifier.", file.getName() + )); + } + map.put(identifier, file); + }); + } + } 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 972403b..8ebc7ff 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 @@ -18,13 +18,19 @@ public interface BackupFileNameEncoder { /** * 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); + default boolean isValidFileName(String fileName) { + return decode(fileName) != null; + } + /** * Check if the given string is a valid custom backup name. + * * @param customName the custom backup name. * @return if the name is valid. */ @@ -45,7 +51,7 @@ public interface BackupFileNameEncoder { private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm.ss"); - BackupBasicInformation(String customName, LocalDateTime time) { + protected BackupBasicInformation(String customName, LocalDateTime time) { this.customName = customName; this.time = time; } 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 index 3c35201..926f47c 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/name/IncrementalBackupFileNameEncoder.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/name/IncrementalBackupFileNameEncoder.java @@ -24,7 +24,7 @@ public class IncrementalBackupFileNameEncoder implements BackupFileNameEncoder { @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" + "^" + 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()) { 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 bcba114..ef15ae7 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 @@ -20,7 +20,7 @@ public class PrimitiveBackupFileNameEncoder implements BackupFileNameEncoder { @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" + "^" + 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()) { diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/provider/AvailableBackupProvider.java b/src/main/java/com/keuin/kbackupfabric/util/backup/provider/AvailableBackupProvider.java new file mode 100644 index 0000000..caa0e84 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/provider/AvailableBackupProvider.java @@ -0,0 +1,8 @@ +package com.keuin.kbackupfabric.util.backup.provider; + +/** + * List all backup in disk. Provide their basic information as soon as possible. + */ +public class AvailableBackupProvider { + // TODO: remove obsolete impl. in command user interface. Use this instead. +} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/provider/IncrementalBackupInformation.java b/src/main/java/com/keuin/kbackupfabric/util/backup/provider/IncrementalBackupInformation.java new file mode 100644 index 0000000..861d210 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/provider/IncrementalBackupInformation.java @@ -0,0 +1,13 @@ +package com.keuin.kbackupfabric.util.backup.provider; + +import com.keuin.kbackupfabric.util.backup.name.BackupFileNameEncoder; + +import java.time.LocalDateTime; + +public class IncrementalBackupInformation extends BackupFileNameEncoder.BackupBasicInformation { + // TODO: show total size for incremental backup + + public IncrementalBackupInformation(String customName, LocalDateTime time) { + super(customName, time); + } +} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/provider/PrimitiveBackupInformation.java b/src/main/java/com/keuin/kbackupfabric/util/backup/provider/PrimitiveBackupInformation.java new file mode 100644 index 0000000..d3d2db8 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/provider/PrimitiveBackupInformation.java @@ -0,0 +1,14 @@ +package com.keuin.kbackupfabric.util.backup.provider; + +import com.keuin.kbackupfabric.util.backup.name.BackupFileNameEncoder; + +import java.time.LocalDateTime; + +public class PrimitiveBackupInformation extends BackupFileNameEncoder.BackupBasicInformation { + public final long sizeBytes; + + public PrimitiveBackupInformation(String customName, LocalDateTime time, long sizeBytes) { + super(customName, time); + this.sizeBytes = sizeBytes; + } +} diff --git a/src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java b/src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java index 7e4f267..58e1711 100644 --- a/src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java +++ b/src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java @@ -4,6 +4,7 @@ import org.apache.commons.codec.digest.DigestUtils; import org.junit.Test; import java.io.*; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; @@ -19,14 +20,14 @@ public class ConfiguredIncrementalBackupMethodTest { private final String destDirectoryName = "destination"; private final String indexFileName = "index"; - private final double directoryFactor = 0.4; - private final double fileFactor = 0.1; - private final int maxRandomFileSizeBytes = 1024 * 1024; + private final double directoryFactor = 0.03; + private final double fileFactor = 0.05; + private final int maxRandomFileSizeBytes = 1024 * 1024 * 16; private final Function scaleDecayFunc = (x) -> x - 1; @Test public void iterationTest() throws IOException { - int a = 100; + int a = 12; for (int i = 0; i < a; ++i) { performTest(Math.min(i + 1, 10)); System.out.println("Round " + i + " passed."); @@ -75,9 +76,28 @@ public class ConfiguredIncrementalBackupMethodTest { if (!method.restore()) fail(); + boolean fake = scale % 2 != 0; + + int[] success = new int[1]; + if (fake) { + Files.walk(sourcePath).filter(path -> path.toFile().isFile()).limit(3).forEach(path -> { + if (!path.toFile().delete()) + fail(); + success[0]++; + }); + if (success[0] == 0) + fake = false; + } + + if (fake) + System.out.println("Fake: deleted " + success[0] + " file(s)."); + String hash2 = calcMD5HashForDir(sourcePath.toFile(), true); - assertEquals(hash1, hash2); + if (!fake) + assertEquals(hash1, hash2); + else + assertNotEquals(hash1, hash2); } private void createRandomDirectoryTree(String path, int scale) throws IOException { @@ -94,7 +114,7 @@ public class ConfiguredIncrementalBackupMethodTest { for (int i = 0; i < subFileCount; i++) { String subFile = null; while (subFile == null || new File(path, subFile).exists()) - subFile = getRandomString((int) (Math.random() * 16 + 1)); + subFile = getRandomString((int) (Math.random() * 16 + 5)); createRandomFile(new File(path, subFile), maxRandomFileSizeBytes); } @@ -103,7 +123,7 @@ public class ConfiguredIncrementalBackupMethodTest { for (int i = 0; i < subDirCount; i++) { String subDir = null; while (subDir == null || new File(path, subDir).exists()) - subDir = getRandomString((int) (Math.random() * 16 + 1)); + subDir = getRandomString((int) (Math.random() * 16 + 5)); createRandomDirectoryTree(new File(path, subDir).getAbsolutePath(), scaleDecayFunc.apply(scale)); } } diff --git a/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionFactoryTest.java b/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionFactoryTest.java index b7e0aad..3f722a4 100644 --- a/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionFactoryTest.java +++ b/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionFactoryTest.java @@ -20,7 +20,7 @@ public class ObjectCollectionFactoryTest { } for (Map.Entry entry : collection.getElementMap().entrySet()) { // assertTrue(subElements.contains(e.getIdentification())); - assertEquals(subElements.get(entry.getKey()), entry.getValue().getIdentifier().getIdentification()); + assertEquals(subElements.get(entry.getKey()).toUpperCase(), entry.getValue().getIdentifier().getIdentification().toUpperCase()); } } @@ -39,14 +39,14 @@ public class ObjectCollectionFactoryTest { final Map elements = new HashMap<>(); // check root dir - elements.put("a", "261CA0D59FEE8FD169802BB8030A07CF23E5C1593FA81A16C6D0A8CF27DAA2ED"); - elements.put("b", "B3FED75012C4969DC63A50EBC4E745FF77E4A06E0B04720EF71EF033032EBAF7"); + elements.put("a", "S2-261CA0D59FEE8FD169802BB8030A07CF23E5C1593FA81A16C6D0A8CF27DAA2ED"); + elements.put("b", "S2-B3FED75012C4969DC63A50EBC4E745FF77E4A06E0B04720EF71EF033032EBAF7"); validate(collection, Arrays.asList("1", "2", "3"), elements); elements.clear(); // check `1` - elements.put("a", "E8620F35A5DB33B1257CC51245DDACDA8AF3E0D431A8A38473575E468BCBD0BD"); - elements.put("b", "19EE41585A674274891DE5A4B365DBAB9C49C576AB6F86CD515B683724D2DBBD"); + elements.put("a", "S2-E8620F35A5DB33B1257CC51245DDACDA8AF3E0D431A8A38473575E468BCBD0BD"); + elements.put("b", "S2-19EE41585A674274891DE5A4B365DBAB9C49C576AB6F86CD515B683724D2DBBD"); validate(collection.getSubCollectionMap().get("1"), Arrays.asList("11", "12"), elements); elements.clear(); @@ -60,8 +60,8 @@ public class ObjectCollectionFactoryTest { validate(collection.getSubCollectionMap().get("1").getSubCollectionMap().get("11"), Collections.singletonList("111"), Collections.emptyMap()); // check `111` - elements.put("a", "1EDBE882A757E1FAFCA77A9D3BE3FF5D2BB3E2037B238C865F1F957C431F43B4"); - elements.put("b", "30BA7CD8B4AD93A8B3826CD8D1518790924EEBB930EC04DF7DFB03A50B17D7BC"); + elements.put("a", "S2-1EDBE882A757E1FAFCA77A9D3BE3FF5D2BB3E2037B238C865F1F957C431F43B4"); + elements.put("b", "S2-30BA7CD8B4AD93A8B3826CD8D1518790924EEBB930EC04DF7DFB03A50B17D7BC"); validate( collection.getSubCollectionMap().get("1").getSubCollectionMap().get("11").getSubCollectionMap().get("111"), Collections.emptyList(), diff --git a/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256IdentifierTest.java b/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256IdentifierTest.java index 53e92e2..f799a95 100644 --- a/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256IdentifierTest.java +++ b/src/test/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256IdentifierTest.java @@ -15,7 +15,7 @@ public class Sha256IdentifierTest { try { Sha256Identifier sha256 = Sha256Identifier.fromFile(new File("./testfile/Sha256IdentifierTest")); String str = sha256.getIdentification().toUpperCase(); - assertEquals("315F5BDB76D078C43B8AC0064E4A0164612B1FCE77C869345BFC94C75894EDD3", str); + assertEquals("S2-315F5BDB76D078C43B8AC0064E4A0164612B1FCE77C869345BFC94C75894EDD3", str); } catch (IOException e) { e.printStackTrace(); fail(); diff --git a/src/test/java/com/keuin/kbackupfabric/util/backup/name/IncrementalBackupFileNameEncoderTest.java b/src/test/java/com/keuin/kbackupfabric/util/backup/name/IncrementalBackupFileNameEncoderTest.java new file mode 100644 index 0000000..e5fedd7 --- /dev/null +++ b/src/test/java/com/keuin/kbackupfabric/util/backup/name/IncrementalBackupFileNameEncoderTest.java @@ -0,0 +1,38 @@ +package com.keuin.kbackupfabric.util.backup.name; + +import org.junit.Test; + +import java.time.LocalDateTime; + +import static org.junit.Assert.*; + +public class IncrementalBackupFileNameEncoderTest { + @Test + public void testEncode() { + LocalDateTime time = LocalDateTime.of(1, 1, 1, 1, 1, 1); + String customName = "name"; + IncrementalBackupFileNameEncoder encoder = new IncrementalBackupFileNameEncoder(); + assertEquals("incremental-0001-01-01_01-01-01_name.kbi", encoder.encode(customName, time)); + } + + @Test + public void testDecode() { + LocalDateTime time = LocalDateTime.of(1, 1, 1, 1, 1, 1); + String customName = "name"; + IncrementalBackupFileNameEncoder encoder = new IncrementalBackupFileNameEncoder(); + BackupFileNameEncoder.BackupBasicInformation information = encoder.decode("incremental-0001-01-01_01-01-01_name.kbi"); + assertEquals(time, information.time); + assertEquals(customName, information.customName); + } + + @Test + public void isValid() { + IncrementalBackupFileNameEncoder encoder = new IncrementalBackupFileNameEncoder(); + assertTrue(encoder.isValidFileName("incremental-0001-01-01_01-01-01_name.kbi")); + assertTrue(encoder.isValidFileName("incremental-0001-01-01_01-01-01_0001-01-01_01-01-01_name.kbi")); + assertFalse(encoder.isValidFileName("incremental-0001-01-01_01-01-01incremental-0001-01-01_01-01-01_name.kbi")); + assertFalse(encoder.isValidFileName("incremental-0001-01-01_01-01-01_name")); + assertFalse(encoder.isValidFileName("incremental-0001-01-01_01-01-01_name.zip")); + assertFalse(encoder.isValidFileName("somefile")); + } +} \ No newline at end of file diff --git a/src/test/java/com/keuin/kbackupfabric/util/backup/name/PrimitiveBackupFileNameEncoderTest.java b/src/test/java/com/keuin/kbackupfabric/util/backup/name/PrimitiveBackupFileNameEncoderTest.java index 4823575..abc19d5 100644 --- a/src/test/java/com/keuin/kbackupfabric/util/backup/name/PrimitiveBackupFileNameEncoderTest.java +++ b/src/test/java/com/keuin/kbackupfabric/util/backup/name/PrimitiveBackupFileNameEncoderTest.java @@ -5,17 +5,46 @@ import org.junit.Test; import java.time.LocalDateTime; import java.time.ZoneOffset; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.*; public class PrimitiveBackupFileNameEncoderTest { @Test public void testConsistency() { - LocalDateTime time = LocalDateTime.ofEpochSecond(System.currentTimeMillis()/1000, 0, ZoneOffset.UTC); + LocalDateTime time = LocalDateTime.ofEpochSecond(System.currentTimeMillis() / 1000, 0, ZoneOffset.UTC); String name = "Test Na_me"; PrimitiveBackupFileNameEncoder encoder = new PrimitiveBackupFileNameEncoder(); BackupFileNameEncoder.BackupBasicInformation information = encoder.decode(encoder.encode(name, time)); assertEquals(time, information.time); assertEquals(name, information.customName); } + + @Test + public void testEncode() { + LocalDateTime time = LocalDateTime.of(1, 1, 1, 1, 1, 1); + String customName = "name"; + PrimitiveBackupFileNameEncoder encoder = new PrimitiveBackupFileNameEncoder(); + assertEquals("kbackup-0001-01-01_01-01-01_name.zip", encoder.encode(customName, time)); + } + + @Test + public void testDecode() { + LocalDateTime time = LocalDateTime.of(1, 1, 1, 1, 1, 1); + String customName = "name"; + PrimitiveBackupFileNameEncoder encoder = new PrimitiveBackupFileNameEncoder(); + BackupFileNameEncoder.BackupBasicInformation information = encoder.decode("kbackup-0001-01-01_01-01-01_name.zip"); + assertEquals(time, information.time); + assertEquals(customName, information.customName); + } + + @Test + public void isValid() { + PrimitiveBackupFileNameEncoder encoder = new PrimitiveBackupFileNameEncoder(); + assertTrue(encoder.isValidFileName("kbackup-0001-01-01_01-01-01_name.zip")); + assertTrue(encoder.isValidFileName("kbackup-0001-01-01_01-01-01_0001-01-01_01-01-01_name.zip")); + assertFalse(encoder.isValidFileName("kbackup-0001-01-01_01-01-01kbackup-0001-01-01_01-01-01_name.zip")); + assertFalse(encoder.isValidFileName("kbackup-0001-01-01_01-01-01_name")); + assertFalse(encoder.isValidFileName("kbackup-0001-01-01_01-01-01_name.kbi")); + assertFalse(encoder.isValidFileName("somefile")); + } } \ No newline at end of file -- cgit v1.2.3 From 98274aeaa26f96529dcc76d8b64692fcc6627410 Mon Sep 17 00:00:00 2001 From: Keuin Date: Thu, 14 Jan 2021 13:20:41 +0800 Subject: Fix Intellij fake warning --- .../java/com/keuin/kbackupfabric/exception/ZipUtilException.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'src/main/java/com/keuin') diff --git a/src/main/java/com/keuin/kbackupfabric/exception/ZipUtilException.java b/src/main/java/com/keuin/kbackupfabric/exception/ZipUtilException.java index c23ce26..88b1d18 100644 --- a/src/main/java/com/keuin/kbackupfabric/exception/ZipUtilException.java +++ b/src/main/java/com/keuin/kbackupfabric/exception/ZipUtilException.java @@ -1,10 +1,7 @@ package com.keuin.kbackupfabric.exception; public class ZipUtilException extends Exception { - public ZipUtilException() { - } - - public ZipUtilException(String gripe) { - super(gripe); + public ZipUtilException(String string) { + super(string); } } -- cgit v1.2.3 From a28bd300a49328d04dff0a071e7cfca47cc1ed6c Mon Sep 17 00:00:00 2001 From: Keuin Date: Wed, 20 Jan 2021 17:41:35 +0800 Subject: 1.4.1: will not backup `session.lock` file. Improve test case compatibility to suit more general compile environments. --- gradle.properties | 2 +- .../java/com/keuin/kbackupfabric/util/ZipUtil.java | 20 +++++++++++++------- .../ConfiguredIncrementalBackupMethodTest.java | 4 ++-- 3 files changed, 16 insertions(+), 10 deletions(-) (limited to 'src/main/java/com/keuin') diff --git a/gradle.properties b/gradle.properties index 4d7ec6c..ccc429a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ minecraft_version=1.14.4 yarn_mappings=1.14.4+build.18 loader_version=0.11.0 # Mod Properties -mod_version=1.4.0 +mod_version=1.4.1 maven_group=com.keuin.kbackupfabric archives_base_name=kbackup-fabric # Dependencies 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 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/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java b/src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java index 58e1711..30f2d44 100644 --- a/src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java +++ b/src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java @@ -15,12 +15,12 @@ import static org.junit.Assert.*; public class ConfiguredIncrementalBackupMethodTest { - private final String testTempPath = "R:\\"; + private final String testTempPath = (new File("R:\\").isDirectory()) ? "R:\\" : ".\\testfile\\ConfiguredIncrementalBackupMethodTest"; private final String sourceDirectoryName = "source"; private final String destDirectoryName = "destination"; private final String indexFileName = "index"; - private final double directoryFactor = 0.03; + private final double directoryFactor = 0.02; private final double fileFactor = 0.05; private final int maxRandomFileSizeBytes = 1024 * 1024 * 16; private final Function scaleDecayFunc = (x) -> x - 1; -- cgit v1.2.3 From 51c2d5d8e63b9db9245d0a1c76f8a94c4cf7c31d Mon Sep 17 00:00:00 2001 From: Keuin Date: Wed, 20 Jan 2021 18:15:35 +0800 Subject: 1.4.2: BugFix: The plugin makes a blank backup if the world (is created in client-side and) is not named as the same with the folder name (by default it is `world`, the backup will fail silently if the world name is not equal to the folder name). --- gradle.properties | 2 +- .../java/com/keuin/kbackupfabric/KBCommands.java | 125 +++++++++++---------- .../util/backup/BackupFilesystemUtil.java | 12 +- 3 files changed, 79 insertions(+), 60 deletions(-) (limited to 'src/main/java/com/keuin') diff --git a/gradle.properties b/gradle.properties index ccc429a..ef7c112 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ minecraft_version=1.14.4 yarn_mappings=1.14.4+build.18 loader_version=0.11.0 # Mod Properties -mod_version=1.4.1 +mod_version=1.4.2 maven_group=com.keuin.kbackupfabric archives_base_name=kbackup-fabric # Dependencies 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 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 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/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 { -- cgit v1.2.3