Search in sources :

Example 16 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes 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 17 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes 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)

Example 18 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project neo4j by neo4j.

the class FileVisitorsDecoratorsTest method shouldDelegatePreVisitDirectory.

@Test
public void shouldDelegatePreVisitDirectory() throws IOException {
    Path dir = Paths.get("some-dir");
    BasicFileAttributes attrs = mock(BasicFileAttributes.class);
    decorator.preVisitDirectory(dir, attrs);
    verify(wrapped).preVisitDirectory(dir, attrs);
}
Also used : Path(java.nio.file.Path) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Test(org.junit.Test)

Example 19 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project dex2jar by pxb1988.

the class WebApp method main.

/**
     * @param args
     * @throws IOException
     */
public static void main(String[] args) throws IOException {
    if (args.length < 2) {
        System.out.println("webapp pathToWebApp config [ignoreJarConfig]");
        return;
    }
    File webApp = new File(args[0]);
    File config = new File(args[1]);
    Path jarIgnore = args.length > 2 ? new File(args[2]).toPath() : null;
    Path clz = new File(webApp, "WEB-INF/classes").toPath();
    Path tmpClz = new File(webApp, "WEB-INF/tmp-classes").toPath();
    final InvocationWeaver ro = (InvocationWeaver) new InvocationWeaver().withConfig(config.toPath());
    Files.deleteIfExists(tmpClz);
    copyDirectory(clz, tmpClz);
    System.out.println("InvocationWeaver from [" + tmpClz + "] to [" + clz + "]");
    ro.wave(tmpClz, clz);
    Files.deleteIfExists(tmpClz);
    final File lib = new File(webApp, "WEB-INF/lib");
    Path tmpLib = new File(webApp, "WEB-INF/Nlib").toPath();
    final Set<String> ignores = new HashSet<String>();
    if (jarIgnore != null && Files.exists(jarIgnore)) {
        ignores.addAll(Files.readAllLines(jarIgnore, StandardCharsets.UTF_8));
    } else {
        System.out.println("ignoreJarConfig ignored");
    }
    Files.deleteIfExists(tmpLib);
    copyDirectory(lib.toPath(), tmpLib);
    Files.walkFileTree(tmpLib, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (file.getFileName().toString().endsWith(".jar")) {
                final String s = file.getFileName().toString();
                boolean ignore = false;
                for (String i : ignores) {
                    if (s.startsWith(i)) {
                        ignore = true;
                        break;
                    }
                }
                if (!ignore) {
                    Path nJar = new File(lib, s).toPath();
                    System.out.println("InvocationWeaver from [" + file + "] to [" + nJar + "]");
                    ro.wave(file, nJar);
                }
            }
            return super.visitFile(file, attrs);
        }
    });
    Files.deleteIfExists(tmpLib);
}
Also used : Path(java.nio.file.Path) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) HashSet(java.util.HashSet)

Example 20 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project robovm by robovm.

the class AfcClient method upload.

/**
     * Uploads a local file or directory to the device.
     * 
     * @param localFile the file or directory to upload.
     * @param targetPath the path of the directory on the device where to place 
     *                   the uploaded files.
     * @param callback callback which will receive progress and status updates.
     *                 If <code>null</code> no progress will be reported.
     */
public void upload(File localFile, final String targetPath, final UploadProgressCallback callback) throws IOException {
    makeDirectory(targetPath);
    final Path root = localFile.toPath().getParent();
    // 64k seems to be a good buffer size. If smaller we will not get
    // acceptable write speeds.
    final byte[] buffer = new byte[64 * 1024];
    class FileCounterVisitor extends SimpleFileVisitor<Path> {

        int count;

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            count++;
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            count++;
            return FileVisitResult.CONTINUE;
        }
    }
    FileCounterVisitor visitor = new FileCounterVisitor();
    if (callback != null) {
        Files.walkFileTree(localFile.toPath(), visitor);
    }
    try {
        final int fileCount = visitor.count;
        Files.walkFileTree(localFile.toPath(), new SimpleFileVisitor<Path>() {

            int filesUploaded = 0;

            private void reportProgress(Path path) {
                if (callback != null) {
                    callback.progress(path.toFile(), 100 * filesUploaded / fileCount);
                }
                filesUploaded++;
            }

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                reportProgress(dir);
                String deviceDir = toAbsoluteDevicePath(targetPath, root.relativize(dir));
                makeDirectory(deviceDir);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                reportProgress(file);
                String deviceFile = toAbsoluteDevicePath(targetPath, root.relativize(file));
                if (Files.isSymbolicLink(file)) {
                    Path linkTargetPath = Files.readSymbolicLink(file);
                    makeLink(AfcLinkType.AFC_SYMLINK, toRelativeDevicePath(linkTargetPath), deviceFile);
                } else if (Files.isRegularFile(file, LinkOption.NOFOLLOW_LINKS)) {
                    long fd = fileOpen(deviceFile, AfcFileMode.AFC_FOPEN_WRONLY);
                    try (InputStream is = Files.newInputStream(file)) {
                        int n = 0;
                        while ((n = is.read(buffer)) != -1) {
                            fileWrite(fd, buffer, 0, n);
                        }
                    } finally {
                        fileClose(fd);
                    }
                }
                return FileVisitResult.CONTINUE;
            }
        });
        if (callback != null) {
            callback.success();
        }
    } catch (IOException e) {
        if (callback != null) {
            callback.error(e.getMessage());
        }
        throw e;
    } catch (LibIMobileDeviceException e) {
        if (callback != null) {
            callback.error(e.getMessage());
        }
        throw e;
    }
}
Also used : Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)119 Path (java.nio.file.Path)93 IOException (java.io.IOException)87 FileVisitResult (java.nio.file.FileVisitResult)66 File (java.io.File)18 Test (org.junit.Test)13 SimpleFileVisitor (java.nio.file.SimpleFileVisitor)11 ArrayList (java.util.ArrayList)11 HashSet (java.util.HashSet)8 FileNotFoundException (java.io.FileNotFoundException)7 InputStream (java.io.InputStream)6 HashMap (java.util.HashMap)6 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)5 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)5 SourcePath (com.facebook.buck.rules.SourcePath)4 ImmutableList (com.google.common.collect.ImmutableList)4 OutputStream (java.io.OutputStream)4 URI (java.net.URI)4 FileSystem (java.nio.file.FileSystem)4 FileVisitor (java.nio.file.FileVisitor)4