use of java.nio.file.FileVisitOption in project java-mapollage by trixon.
the class Operation method generateFileList.
private boolean generateFileList() throws IOException {
mListener.onOperationLog("");
mListener.onOperationLog(Dict.GENERATING_FILELIST.toString());
PathMatcher pathMatcher = mProfileSource.getPathMatcher();
EnumSet<FileVisitOption> fileVisitOptions;
if (mProfileSource.isFollowLinks()) {
fileVisitOptions = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
} else {
fileVisitOptions = EnumSet.noneOf(FileVisitOption.class);
}
File file = mProfileSource.getDir();
if (file.isDirectory()) {
FileVisitor fileVisitor = new FileVisitor(pathMatcher, mFiles, file, this);
try {
if (mProfileSource.isRecursive()) {
Files.walkFileTree(file.toPath(), fileVisitOptions, Integer.MAX_VALUE, fileVisitor);
} else {
Files.walkFileTree(file.toPath(), fileVisitOptions, 1, fileVisitor);
}
if (fileVisitor.isInterrupted()) {
return false;
}
} catch (IOException ex) {
throw new IOException(String.format("E000 %s", file.getAbsolutePath()));
}
} else if (file.isFile() && pathMatcher.matches(file.toPath().getFileName())) {
mFiles.add(file);
}
if (mFiles.isEmpty()) {
mListener.onOperationFinished(Dict.FILELIST_EMPTY.toString(), 0);
} else {
Collections.sort(mFiles);
}
return true;
}
use of java.nio.file.FileVisitOption in project j2objc by google.
the class Files2Test method test_walkFileTree_negativeDepth.
@Test
public void test_walkFileTree_negativeDepth() throws IOException {
Path rootDir = filesSetup.getPathInTestDir("root");
Path dir1 = filesSetup.getPathInTestDir("root/dir1");
Files.createDirectories(dir1);
Map<Object, VisitOption> dirMap = new HashMap<>();
Set<FileVisitOption> option = new HashSet<>();
option.add(FileVisitOption.FOLLOW_LINKS);
try {
Files.walkFileTree(rootDir, option, -1, new Files2Test.TestFileVisitor(dirMap));
fail();
} catch (IllegalArgumentException expected) {
}
}
use of java.nio.file.FileVisitOption in project j2objc by google.
the class Files2Test method test_walkFileTree$Path$Set$int$FileVisitor_symbolicLinkFollow.
@Test
public void test_walkFileTree$Path$Set$int$FileVisitor_symbolicLinkFollow() throws IOException, InterruptedException {
// Directory structure.
// root
// ├── dir1
// │ └── dir2 ─ dir3-file1 - file3
// │
// └── file2
//
// With follow link it should be able to traverse to dir3 and file1 when started from file2.
// Directory setup.
Path rootDir = filesSetup.getPathInTestDir("root");
Path dir1 = filesSetup.getPathInTestDir("root/dir1");
Path dir2 = filesSetup.getPathInTestDir("root/dir1/dir2");
Path dir3 = filesSetup.getPathInTestDir("root/dir1/dir2/dir3");
Path file1 = filesSetup.getPathInTestDir("root/dir1/dir2/dir3/file1");
Path file2 = filesSetup.getPathInTestDir("root/file2");
Files.createDirectories(dir3);
Files.createFile(file1);
Files.createSymbolicLink(file2, dir2.toAbsolutePath());
assertTrue(Files.isSymbolicLink(file2));
Map<Object, VisitOption> dirMap = new HashMap<>();
Map<Object, VisitOption> expectedDirMap = new HashMap<>();
Set<FileVisitOption> option = new HashSet<>();
option.add(FileVisitOption.FOLLOW_LINKS);
Files.walkFileTree(file2, option, 50, new TestFileVisitor(dirMap, option));
expectedDirMap.put(file1.getFileName(), VisitOption.VISIT_FILE);
expectedDirMap.put(file2.getFileName(), VisitOption.POST_VISIT_DIRECTORY);
expectedDirMap.put(dir3.getFileName(), VisitOption.POST_VISIT_DIRECTORY);
assertEquals(expectedDirMap, dirMap);
}
use of java.nio.file.FileVisitOption in project j2objc by google.
the class Files2Test method test_walkFileTree_maximumDepth.
@Test
public void test_walkFileTree_maximumDepth() throws IOException {
// Directory structure.
// root
// ├── dir1
// │ ├── dir2
// │ │ ├── dir3
// │ │ └── file5
// │ ├── dir4
// │ └── file3
// ├── dir5
// └── file1
//
// depth will be 2. file5, dir3 is not reachable.
// Directory Setup.
Path rootDir = filesSetup.getPathInTestDir("root");
Path dir1 = filesSetup.getPathInTestDir("root/dir1");
Path dir2 = filesSetup.getPathInTestDir("root/dir1/dir2");
Path dir3 = filesSetup.getPathInTestDir("root/dir1/dir2/dir3");
Path dir4 = filesSetup.getPathInTestDir("root/dir1/dir4");
Path dir5 = filesSetup.getPathInTestDir("root/dir5");
Path file1 = filesSetup.getPathInTestDir("root/file1");
Path file3 = filesSetup.getPathInTestDir("root/dir1/file3");
Path file5 = filesSetup.getPathInTestDir("root/dir1/dir2/file5");
Files.createDirectories(dir3);
Files.createDirectories(dir4);
Files.createDirectories(dir5);
Files.createFile(file1);
Files.createFile(file3);
Files.createFile(file5);
Map<Object, VisitOption> dirMap = new HashMap<>();
Map<Object, VisitOption> expectedDirMap = new HashMap<>();
Set<FileVisitOption> option = new HashSet<>();
option.add(FileVisitOption.FOLLOW_LINKS);
Files.walkFileTree(rootDir, option, 2, new Files2Test.TestFileVisitor(dirMap));
assertTrue(Files.isDirectory(dir4));
expectedDirMap.put(rootDir.getFileName(), VisitOption.POST_VISIT_DIRECTORY);
expectedDirMap.put(dir1.getFileName(), VisitOption.POST_VISIT_DIRECTORY);
// Both of the directories are at maximum depth, therefore, will be treated as simple file.
expectedDirMap.put(dir2.getFileName(), VisitOption.VISIT_FILE);
expectedDirMap.put(dir4.getFileName(), VisitOption.VISIT_FILE);
expectedDirMap.put(dir5.getFileName(), VisitOption.POST_VISIT_DIRECTORY);
expectedDirMap.put(file1.getFileName(), VisitOption.VISIT_FILE);
expectedDirMap.put(file3.getFileName(), VisitOption.VISIT_FILE);
assertEquals(expectedDirMap, dirMap);
}
use of java.nio.file.FileVisitOption in project j2objc by google.
the class Files2Test method test_walkFileTree$Path$FileVisitor_FileSystemLoopException.
@Test
public void test_walkFileTree$Path$FileVisitor_FileSystemLoopException() throws IOException {
// Directory structure.
// .
// ├── DATA_FILE
// └── root
// └── dir1
// └── file1
//
// file1 is symlink to dir1
// Directory Setup.
Path rootDir = filesSetup.getPathInTestDir("root");
Path dir1 = filesSetup.getPathInTestDir("root/dir1");
Path file1 = filesSetup.getPathInTestDir("root/dir1/file1");
Files.createDirectories(dir1);
Files.createSymbolicLink(file1, dir1.toAbsolutePath());
assertEquals(dir1.getFileName(), Files.readSymbolicLink(file1).getFileName());
Map<Object, VisitOption> dirMap = new HashMap<>();
Set<FileVisitOption> option = new HashSet<>();
option.add(FileVisitOption.FOLLOW_LINKS);
try {
Files.walkFileTree(rootDir, option, Integer.MAX_VALUE, new Files2Test.TestFileVisitor(dirMap));
fail();
} catch (FileSystemLoopException expected) {
}
}
Aggregations