mirror of
https://github.com/cryptomator/cryptomator.git
synced 2026-09-21 07:24:37 +00:00
Replaced DirectoryWalker by FileSystemVisitor
This commit is contained in:
+12
-9
@@ -8,6 +8,8 @@
|
||||
*******************************************************************************/
|
||||
package org.cryptomator.crypto.fs;
|
||||
|
||||
import static org.cryptomator.filesystem.FileSystemVisitor.fileSystemVisitor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.ByteBuffer;
|
||||
@@ -175,15 +177,16 @@ public class CryptoFileSystemTest {
|
||||
*/
|
||||
private static int countDataFolders(Folder dataRoot) {
|
||||
final AtomicInteger num = new AtomicInteger();
|
||||
DirectoryWalker.walk(dataRoot, 0, 2, (node) -> {
|
||||
if (node instanceof Folder) {
|
||||
final Folder nodeParent = node.parent().get();
|
||||
final Folder nodeParentParent = nodeParent.parent().orElse(null);
|
||||
if (nodeParentParent != null && nodeParentParent.equals(dataRoot)) {
|
||||
num.incrementAndGet();
|
||||
}
|
||||
}
|
||||
});
|
||||
fileSystemVisitor() //
|
||||
.afterFolder(folder -> {
|
||||
final Folder parent = folder.parent().get();
|
||||
final Folder parentOfParent = parent.parent().orElse(null);
|
||||
if (parentOfParent != null && parentOfParent.equals(dataRoot)) {
|
||||
num.incrementAndGet();
|
||||
}
|
||||
}) //
|
||||
.withMaxDepth(2) //
|
||||
.visit(dataRoot);
|
||||
return num.get();
|
||||
}
|
||||
|
||||
|
||||
+16
-19
@@ -8,9 +8,8 @@
|
||||
*******************************************************************************/
|
||||
package org.cryptomator.crypto.fs;
|
||||
|
||||
import java.util.Optional;
|
||||
import static org.cryptomator.filesystem.FileSystemVisitor.fileSystemVisitor;
|
||||
|
||||
import org.cryptomator.filesystem.File;
|
||||
import org.cryptomator.filesystem.Folder;
|
||||
|
||||
public final class DirectoryPrinter {
|
||||
@@ -18,23 +17,21 @@ public final class DirectoryPrinter {
|
||||
private DirectoryPrinter() {
|
||||
}
|
||||
|
||||
public static String print(Folder folder) {
|
||||
StringBuilder sb = new StringBuilder(folder.name()).append('\n');
|
||||
|
||||
DirectoryWalker.walk(folder, (node) -> {
|
||||
Optional<? extends Folder> parent = node.parent();
|
||||
while (parent.isPresent()) {
|
||||
sb.append(" ");
|
||||
parent = parent.get().parent();
|
||||
}
|
||||
if (node instanceof Folder) {
|
||||
sb.append(node.name()).append('/').append('\n');
|
||||
} else if (node instanceof File) {
|
||||
sb.append(node.name()).append('\n');
|
||||
}
|
||||
});
|
||||
|
||||
return sb.toString();
|
||||
public static String print(Folder rootFolder) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
StringBuilder identation = new StringBuilder();
|
||||
fileSystemVisitor() //
|
||||
.beforeFolder(folder -> {
|
||||
result.append(identation).append(folder.name()).append("/\n");
|
||||
identation.append(" ");
|
||||
}) //
|
||||
.afterFolder(folder -> {
|
||||
identation.delete(identation.length() - 2, identation.length());
|
||||
}).forEachFile(file -> {
|
||||
result.append(identation).append(file.name()).append('\n');
|
||||
}) //
|
||||
.visit(rootFolder);
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2015 Sebastian Stenzel and others.
|
||||
* This file is licensed under the terms of the MIT license.
|
||||
* See the LICENSE.txt file for more info.
|
||||
*
|
||||
* Contributors:
|
||||
* Sebastian Stenzel - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.cryptomator.crypto.fs;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.cryptomator.filesystem.Folder;
|
||||
import org.cryptomator.filesystem.Node;
|
||||
|
||||
final class DirectoryWalker {
|
||||
|
||||
private DirectoryWalker() {
|
||||
}
|
||||
|
||||
public static void walk(Folder folder, Consumer<Node> visitor) {
|
||||
walk(folder, 0, Integer.MAX_VALUE, visitor);
|
||||
}
|
||||
|
||||
public static void walk(Folder folder, int depth, int maxDepth, Consumer<Node> visitor) {
|
||||
folder.files().forEach(visitor);
|
||||
if (depth == maxDepth) {
|
||||
return;
|
||||
} else {
|
||||
folder.folders().forEach(childFolder -> {
|
||||
visitor.accept(childFolder);
|
||||
walk(childFolder, depth + 1, maxDepth, visitor);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+7
-4
@@ -1,5 +1,7 @@
|
||||
package org.cryptomator.crypto.fs;
|
||||
|
||||
import static org.cryptomator.filesystem.FileSystemVisitor.fileSystemVisitor;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Arrays;
|
||||
|
||||
@@ -43,10 +45,11 @@ public class EncryptAndShortenIntegrationTest {
|
||||
// names:
|
||||
// LOG.debug("Unlimited filename length:\n" +
|
||||
// DirectoryPrinter.print(shorteningFs));
|
||||
DirectoryWalker.walk(shorteningFs, node -> {
|
||||
Assert.assertFalse(node.name().endsWith(".lng"));
|
||||
});
|
||||
|
||||
fileSystemVisitor() //
|
||||
.forEachNode(node -> {
|
||||
Assert.assertFalse(node.name().endsWith(".lng"));
|
||||
}) //
|
||||
.visit(shorteningFs);
|
||||
// on the third (cleartext layer) we have cleartext names on the root
|
||||
// level:
|
||||
// LOG.debug("Cleartext files:\n" + DirectoryPrinter.print(fs));
|
||||
|
||||
Reference in New Issue
Block a user