Skip to content

Commit

Permalink
Fix reading file content
Browse files Browse the repository at this point in the history
  • Loading branch information
adangel committed Nov 3, 2023
1 parent 8f46c7b commit fbd03bc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,21 @@ SeekableByteChannel newByteChannel(String path, Set<? extends OpenOption> option
} else if (segments[0].equals("hosts")) {
snapshotById = findSnapshotByHostAndTime(segments[1], segments[2]);
firstIndex = 3;
} else {
throw new IllegalArgumentException("Unknown path: " + path);
}
String[] subPath = new String[segments.length - firstIndex];
System.arraycopy(segments, firstIndex, subPath, 0, subPath.length);
String subPathJoined = String.join("/", subPath);
if (subPathJoined.isEmpty()) {
throw new IllegalStateException(path + " is not a file");
}
String[] parentSubPath = new String[subPath.length - 1];
System.arraycopy(subPath, 0, parentSubPath, 0, parentSubPath.length);
String parentSubPathJoined = String.join("/", parentSubPath);
try {
Tree tree = repository.readTree(snapshotById.snapshot().tree());
Tree.Node node = findNodeInTree(tree, segments[firstIndex]);
List<Tree.Node> files = repository.listFiles(snapshotById.id(), "/" + parentSubPathJoined);
Tree.Node node = files.stream().filter(n -> n.name().equals(subPath[subPath.length - 1])).findFirst().get();
return createFromNode(node);
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.adangel.resticbrowser.filesystem;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertLinesMatch;

import java.io.IOException;
Expand Down Expand Up @@ -44,4 +45,11 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
List<String> expectedFiles = Files.readAllLines(Path.of("src/test/resources/repos/repo2.filelist"));
assertLinesMatch(expectedFiles, files);
}

@Test
void readFile() throws IOException {
Path path = fileSystem.getPath("/snapshots/2a9e949d33e1d696070a75e0be69dc6c1e9e89a2dd44d02a7b971a8d849c2f6c/home/johndoe/subdir1/a/file2.txt");
String content = Files.readString(path);
assertEquals("This is path subdir1/a/file2.txt\n\n", content);
}
}

0 comments on commit fbd03bc

Please sign in to comment.