From 1df50093bd76315905a9aae880470e81b5e1d8f0 Mon Sep 17 00:00:00 2001 From: Keuin Date: Sun, 24 Jan 2021 21:15:24 +0800 Subject: If incremental backup failed, unfinished copy will be fully reverted. --- .../incremental/ObjectCollectionSerializer.java | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java') diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java index f663f20..fa411a0 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java @@ -1,6 +1,11 @@ package com.keuin.kbackupfabric.backup.incremental; +import org.jetbrains.annotations.NotNull; + import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Iterator; import java.util.Objects; /** @@ -39,4 +44,40 @@ public class ObjectCollectionSerializer { } } } + + public static Iterable fromDirectory(File directory) throws IOException { + + if (!directory.isDirectory()) { + throw new IllegalArgumentException("Given directory is invalid."); + } + return new Iterable() { + private final Iterator iter = new Iterator() { + private final Iterator i = Files.walk(directory.toPath(), 1).filter(p -> { + File f = p.toFile(); + return f.isFile() && f.getName().endsWith(".kbi"); + }).iterator(); + + @Override + public boolean hasNext() { + return i.hasNext(); + } + + @Override + public ObjectCollection2 next() { + try { + return fromFile(i.next().toFile()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + }; + + @NotNull + @Override + public Iterator iterator() { + return iter; + } + }; + + } } -- cgit v1.2.3