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) --- .../util/backup/incremental/ObjectCollection.java | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollection.java (limited to 'src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollection.java') 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); + } +} -- 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/kbackupfabric/util/backup/incremental/ObjectCollection.java') 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/kbackupfabric/util/backup/incremental/ObjectCollection.java') 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