Search in sources :

Example 11 with FileVisitOption

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;
}
Also used : PathMatcher(java.nio.file.PathMatcher) FileVisitOption(java.nio.file.FileVisitOption) IOException(java.io.IOException) File(java.io.File)

Example 12 with FileVisitOption

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) {
    }
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) FileVisitOption(java.nio.file.FileVisitOption) FileVisitOption(java.nio.file.FileVisitOption) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 13 with FileVisitOption

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);
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) FileVisitOption(java.nio.file.FileVisitOption) FileVisitOption(java.nio.file.FileVisitOption) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 14 with FileVisitOption

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);
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) FileVisitOption(java.nio.file.FileVisitOption) FileVisitOption(java.nio.file.FileVisitOption) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 15 with FileVisitOption

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) {
    }
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) FileVisitOption(java.nio.file.FileVisitOption) FileSystemLoopException(java.nio.file.FileSystemLoopException) FileVisitOption(java.nio.file.FileVisitOption) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

FileVisitOption (java.nio.file.FileVisitOption)21 Path (java.nio.file.Path)18 IOException (java.io.IOException)12 FileVisitResult (java.nio.file.FileVisitResult)6 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)6 File (java.io.File)5 HashSet (java.util.HashSet)5 HashMap (java.util.HashMap)4 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)4 Test (org.junit.Test)4 FileWriter (java.io.FileWriter)2 PrintWriter (java.io.PrintWriter)2 FileSystem (java.nio.file.FileSystem)2 PathMatcher (java.nio.file.PathMatcher)2 ArrayList (java.util.ArrayList)2 LoggerContext (org.apache.logging.log4j.core.LoggerContext)2 AbstractConfiguration (org.apache.logging.log4j.core.config.AbstractConfiguration)2 CompositeConfiguration (org.apache.logging.log4j.core.config.composite.CompositeConfiguration)2 PropertiesConfiguration (org.apache.logging.log4j.core.config.properties.PropertiesConfiguration)2 PropertiesConfigurationFactory (org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory)2