Search in sources :

Example 6 with FileVisitResult

use of java.nio.file.FileVisitResult in project buck by facebook.

the class NdkLibraryDescription method findSources.

@VisibleForTesting
protected ImmutableSortedSet<SourcePath> findSources(final ProjectFilesystem filesystem, final Path buildRulePath) {
    final ImmutableSortedSet.Builder<SourcePath> srcs = ImmutableSortedSet.naturalOrder();
    try {
        final Path rootDirectory = filesystem.resolve(buildRulePath);
        Files.walkFileTree(rootDirectory, EnumSet.of(FileVisitOption.FOLLOW_LINKS), /* maxDepth */
        Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (EXTENSIONS_REGEX.matcher(file.toString()).matches()) {
                    srcs.add(new PathSourcePath(filesystem, buildRulePath.resolve(rootDirectory.relativize(file))));
                }
                return super.visitFile(file, attrs);
            }
        });
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return srcs.build();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 7 with FileVisitResult

use of java.nio.file.FileVisitResult in project buck by facebook.

the class IjProjectTemplateDataPreparer method createExcludes.

public ImmutableSet<IjFolder> createExcludes(final IjModule module) throws IOException {
    final ImmutableSet.Builder<IjFolder> excludesBuilder = ImmutableSet.builder();
    final Path moduleBasePath = module.getModuleBasePath();
    projectFilesystem.walkRelativeFileTree(moduleBasePath, new FileVisitor<Path>() {

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            // When we create excludes for that module.
            if (filesystemTraversalBoundaryPaths.contains(dir) && !moduleBasePath.equals(dir)) {
                return FileVisitResult.SKIP_SUBTREE;
            }
            if (isRootAndroidResourceDirectory(module, dir)) {
                return FileVisitResult.SKIP_SUBTREE;
            }
            if (!referencedFolderPaths.contains(dir)) {
                excludesBuilder.add(new ExcludeFolder(dir));
                return FileVisitResult.SKIP_SUBTREE;
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            return FileVisitResult.CONTINUE;
        }
    });
    return excludesBuilder.build();
}
Also used : Path(java.nio.file.Path) ImmutableSet(com.google.common.collect.ImmutableSet) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 8 with FileVisitResult

use of java.nio.file.FileVisitResult in project buck by facebook.

the class ProjectFilesystemTest method testWalkFileTreeWhenProjectRootIsWorkingDir.

@Test
public void testWalkFileTreeWhenProjectRootIsWorkingDir() throws IOException {
    ProjectFilesystem projectFilesystem = new ProjectFilesystem(Paths.get(".").toAbsolutePath());
    final ImmutableList.Builder<String> fileNames = ImmutableList.builder();
    Path pathRelativeToProjectRoot = Paths.get("test/com/facebook/buck/io/testdata/directory_traversal_ignore_paths");
    projectFilesystem.walkRelativeFileTree(pathRelativeToProjectRoot, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
            fileNames.add(file.getFileName().toString());
            return FileVisitResult.CONTINUE;
        }
    });
    assertThat(fileNames.build(), containsInAnyOrder("file", "a_file", "b_file", "b_c_file", "b_d_file"));
}
Also used : Path(java.nio.file.Path) ImmutableList(com.google.common.collect.ImmutableList) FileVisitResult(java.nio.file.FileVisitResult) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Test(org.junit.Test)

Example 9 with FileVisitResult

use of java.nio.file.FileVisitResult in project buck by facebook.

the class FakeProjectFilesystem method walkRelativeFileTree.

/**
   * TODO(natthu): (1) Also traverse the directories. (2) Do not ignore return value of
   * {@code fileVisitor}.
   */
@Override
public final void walkRelativeFileTree(Path path, EnumSet<FileVisitOption> visitOptions, FileVisitor<Path> fileVisitor) throws IOException {
    if (!isDirectory(path)) {
        fileVisitor.visitFile(path, DEFAULT_FILE_ATTRIBUTES);
        return;
    }
    ImmutableCollection<Path> ents = getDirectoryContents(path);
    for (Path ent : ents) {
        if (!isDirectory(ent)) {
            FileVisitResult result = fileVisitor.visitFile(ent, DEFAULT_FILE_ATTRIBUTES);
            if (result == FileVisitResult.SKIP_SIBLINGS) {
                return;
            }
        } else {
            FileVisitResult result = fileVisitor.preVisitDirectory(ent, DEFAULT_DIR_ATTRIBUTES);
            if (result == FileVisitResult.SKIP_SIBLINGS) {
                return;
            }
            if (result != FileVisitResult.SKIP_SUBTREE) {
                walkRelativeFileTree(ent, fileVisitor);
                fileVisitor.postVisitDirectory(ent, null);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) FileVisitResult(java.nio.file.FileVisitResult)

Example 10 with FileVisitResult

use of java.nio.file.FileVisitResult in project elasticsearch-jdbc by jprante.

the class NodeTestUtils method deleteFiles.

private static void deleteFiles() throws IOException {
    Path directory = Paths.get(System.getProperty("path.home") + "/data");
    Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            Files.delete(file);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            Files.delete(dir);
            return FileVisitResult.CONTINUE;
        }
    });
}
Also used : Path(java.nio.file.Path) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

FileVisitResult (java.nio.file.FileVisitResult)74 Path (java.nio.file.Path)67 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)66 IOException (java.io.IOException)65 File (java.io.File)10 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)8 InputStream (java.io.InputStream)4 HashSet (java.util.HashSet)4 JarEntry (java.util.jar.JarEntry)4 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 FileOutputStream (java.io.FileOutputStream)3 JarOutputStream (java.util.jar.JarOutputStream)3 PathSourcePath (com.facebook.buck.rules.PathSourcePath)2 SourcePath (com.facebook.buck.rules.SourcePath)2 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)2 FileSystemException (io.vertx.core.file.FileSystemException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileInputStream (java.io.FileInputStream)2