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 ++++++++++++++++++++++ 7 files changed, 144 insertions(+), 13 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 (limited to 'src/main/java/com') 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(); + } + +} -- cgit v1.2.3