Search in sources :

Example 1 with SimpleFileVisitor

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

the class CopyNativeLibraries method copyNativeLibrary.

public static void copyNativeLibrary(final ProjectFilesystem filesystem, Path sourceDir, final Path destinationDir, ImmutableSet<TargetCpuType> cpuFilters, ImmutableList.Builder<Step> steps) {
    if (cpuFilters.isEmpty()) {
        steps.add(CopyStep.forDirectory(filesystem, sourceDir, destinationDir, CopyStep.DirectoryMode.CONTENTS_ONLY));
    } else {
        for (TargetCpuType cpuType : cpuFilters) {
            Optional<String> abiDirectoryComponent = getAbiDirectoryComponent(cpuType);
            Preconditions.checkState(abiDirectoryComponent.isPresent());
            final Path libSourceDir = sourceDir.resolve(abiDirectoryComponent.get());
            Path libDestinationDir = destinationDir.resolve(abiDirectoryComponent.get());
            final MkdirStep mkDirStep = new MkdirStep(filesystem, libDestinationDir);
            final CopyStep copyStep = CopyStep.forDirectory(filesystem, libSourceDir, libDestinationDir, CopyStep.DirectoryMode.CONTENTS_ONLY);
            steps.add(new Step() {

                @Override
                public StepExecutionResult execute(ExecutionContext context) {
                    // different cells --- this check works by coincidence.
                    if (!filesystem.exists(libSourceDir)) {
                        return StepExecutionResult.SUCCESS;
                    }
                    if (mkDirStep.execute(context).isSuccess() && copyStep.execute(context).isSuccess()) {
                        return StepExecutionResult.SUCCESS;
                    }
                    return StepExecutionResult.ERROR;
                }

                @Override
                public String getShortName() {
                    return "copy_native_libraries";
                }

                @Override
                public String getDescription(ExecutionContext context) {
                    ImmutableList.Builder<String> stringBuilder = ImmutableList.builder();
                    stringBuilder.add(String.format("[ -d %s ]", libSourceDir.toString()));
                    stringBuilder.add(mkDirStep.getDescription(context));
                    stringBuilder.add(copyStep.getDescription(context));
                    return Joiner.on(" && ").join(stringBuilder.build());
                }
            });
        }
    }
    // Rename native files named like "*-disguised-exe" to "lib*.so" so they will be unpacked
    // by the Android package installer.  Then they can be executed like normal binaries
    // on the device.
    steps.add(new AbstractExecutionStep("rename_native_executables") {

        @Override
        public StepExecutionResult execute(ExecutionContext context) {
            final ImmutableSet.Builder<Path> executablesBuilder = ImmutableSet.builder();
            try {
                filesystem.walkRelativeFileTree(destinationDir, new SimpleFileVisitor<Path>() {

                    @Override
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                        if (file.toString().endsWith("-disguised-exe")) {
                            executablesBuilder.add(file);
                        }
                        return FileVisitResult.CONTINUE;
                    }
                });
                for (Path exePath : executablesBuilder.build()) {
                    Path fakeSoPath = Paths.get(MorePaths.pathWithUnixSeparators(exePath).replaceAll("/([^/]+)-disguised-exe$", "/lib$1.so"));
                    filesystem.move(exePath, fakeSoPath);
                }
            } catch (IOException e) {
                context.logError(e, "Renaming native executables failed.");
                return StepExecutionResult.ERROR;
            }
            return StepExecutionResult.SUCCESS;
        }
    });
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) TargetCpuType(com.facebook.buck.android.NdkCxxPlatforms.TargetCpuType) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) MkdirStep(com.facebook.buck.step.fs.MkdirStep) Step(com.facebook.buck.step.Step) CopyStep(com.facebook.buck.step.fs.CopyStep) MkdirStep(com.facebook.buck.step.fs.MkdirStep) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) IOException(java.io.IOException) CopyStep(com.facebook.buck.step.fs.CopyStep) ExecutionContext(com.facebook.buck.step.ExecutionContext) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 2 with SimpleFileVisitor

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

the class AndroidResourceDescription method collectInputFiles.

@VisibleForTesting
ImmutableSortedMap<Path, SourcePath> collectInputFiles(final ProjectFilesystem filesystem, Path inputDir) {
    final ImmutableSortedMap.Builder<Path, SourcePath> paths = ImmutableSortedMap.naturalOrder();
    // We ignore the same files that mini-aapt and aapt ignore.
    FileVisitor<Path> fileVisitor = new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attr) throws IOException {
            String dirName = dir.getFileName().toString();
            // Special case: directory starting with '_' as per aapt.
            if (dirName.charAt(0) == '_' || !isPossibleResourceName(dirName)) {
                return FileVisitResult.SKIP_SUBTREE;
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attr) throws IOException {
            String filename = file.getFileName().toString();
            if (isPossibleResourceName(filename)) {
                paths.put(MorePaths.relativize(inputDir, file), new PathSourcePath(filesystem, file));
            }
            return FileVisitResult.CONTINUE;
        }
    };
    try {
        filesystem.walkRelativeFileTree(inputDir, fileVisitor);
    } catch (IOException e) {
        throw new HumanReadableException(e, "Error while searching for android resources in directory %s.", inputDir);
    }
    return paths.build();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) HumanReadableException(com.facebook.buck.util.HumanReadableException) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) PathSourcePath(com.facebook.buck.rules.PathSourcePath) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with SimpleFileVisitor

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

the class AndroidBinary method getStepsForNativeAssets.

private void getStepsForNativeAssets(SourcePathResolver resolver, ImmutableList.Builder<Step> steps, Optional<ImmutableCollection<SourcePath>> nativeLibDirs, final Path libSubdirectory, final String metadataFilename, final APKModule module) {
    steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), libSubdirectory));
    // Filter, rename and copy the ndk libraries marked as assets.
    if (nativeLibDirs.isPresent()) {
        for (SourcePath nativeLibDir : nativeLibDirs.get()) {
            CopyNativeLibraries.copyNativeLibrary(getProjectFilesystem(), resolver.getAbsolutePath(nativeLibDir), libSubdirectory, cpuFilters, steps);
        }
    }
    // Input asset libraries are sorted in descending filesize order.
    final ImmutableSortedSet.Builder<Path> inputAssetLibrariesBuilder = ImmutableSortedSet.orderedBy((libPath1, libPath2) -> {
        try {
            ProjectFilesystem filesystem = getProjectFilesystem();
            int filesizeResult = -Long.compare(filesystem.getFileSize(libPath1), filesystem.getFileSize(libPath2));
            int pathnameResult = libPath1.compareTo(libPath2);
            return filesizeResult != 0 ? filesizeResult : pathnameResult;
        } catch (IOException e) {
            return 0;
        }
    });
    if (packageAssetLibraries || !module.isRootModule()) {
        if (enhancementResult.getCopyNativeLibraries().isPresent() && enhancementResult.getCopyNativeLibraries().get().containsKey(module)) {
            // Copy in cxx libraries marked as assets. Filtering and renaming was already done
            // in CopyNativeLibraries.getBuildSteps().
            Path cxxNativeLibsSrc = enhancementResult.getCopyNativeLibraries().get().get(module).getPathToNativeLibsAssetsDir();
            steps.add(CopyStep.forDirectory(getProjectFilesystem(), cxxNativeLibsSrc, libSubdirectory, CopyStep.DirectoryMode.CONTENTS_ONLY));
        }
        steps.add(// Step that populates a list of libraries and writes a metadata.txt to decompress.
        new AbstractExecutionStep("write_metadata_for_asset_libraries_" + module.getName()) {

            @Override
            public StepExecutionResult execute(ExecutionContext context) {
                ProjectFilesystem filesystem = getProjectFilesystem();
                try {
                    // Walk file tree to find libraries
                    filesystem.walkRelativeFileTree(libSubdirectory, new SimpleFileVisitor<Path>() {

                        @Override
                        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                            if (!file.toString().endsWith(".so")) {
                                throw new IOException("unexpected file in lib directory");
                            }
                            inputAssetLibrariesBuilder.add(file);
                            return FileVisitResult.CONTINUE;
                        }
                    });
                    // Write a metadata
                    ImmutableList.Builder<String> metadataLines = ImmutableList.builder();
                    Path metadataOutput = libSubdirectory.resolve(metadataFilename);
                    for (Path libPath : inputAssetLibrariesBuilder.build()) {
                        // Should return something like x86/libfoo.so
                        Path relativeLibPath = libSubdirectory.relativize(libPath);
                        long filesize = filesystem.getFileSize(libPath);
                        String desiredOutput = relativeLibPath.toString();
                        String checksum = filesystem.computeSha256(libPath);
                        metadataLines.add(desiredOutput + ' ' + filesize + ' ' + checksum);
                    }
                    ImmutableList<String> metadata = metadataLines.build();
                    if (!metadata.isEmpty()) {
                        filesystem.writeLinesToPath(metadata, metadataOutput);
                    }
                } catch (IOException e) {
                    context.logError(e, "Writing metadata for asset libraries failed.");
                    return StepExecutionResult.ERROR;
                }
                return StepExecutionResult.SUCCESS;
            }
        });
    }
    if (compressAssetLibraries || !module.isRootModule()) {
        final ImmutableList.Builder<Path> outputAssetLibrariesBuilder = ImmutableList.builder();
        steps.add(new AbstractExecutionStep("rename_asset_libraries_as_temp_files_" + module.getName()) {

            @Override
            public StepExecutionResult execute(ExecutionContext context) {
                try {
                    ProjectFilesystem filesystem = getProjectFilesystem();
                    for (Path libPath : inputAssetLibrariesBuilder.build()) {
                        Path tempPath = libPath.resolveSibling(libPath.getFileName() + "~");
                        filesystem.move(libPath, tempPath);
                        outputAssetLibrariesBuilder.add(tempPath);
                    }
                    return StepExecutionResult.SUCCESS;
                } catch (IOException e) {
                    context.logError(e, "Renaming asset libraries failed");
                    return StepExecutionResult.ERROR;
                }
            }
        });
        // Concat and xz compress.
        Path libOutputBlob = libSubdirectory.resolve("libraries.blob");
        steps.add(new ConcatStep(getProjectFilesystem(), outputAssetLibrariesBuilder, libOutputBlob));
        int compressionLevel = xzCompressionLevel.orElse(XzStep.DEFAULT_COMPRESSION_LEVEL).intValue();
        steps.add(new XzStep(getProjectFilesystem(), libOutputBlob, libSubdirectory.resolve(SOLID_COMPRESSED_ASSET_LIBRARY_FILENAME), compressionLevel));
    }
}
Also used : Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) ImmutableList(com.google.common.collect.ImmutableList) XzStep(com.facebook.buck.step.fs.XzStep) IOException(java.io.IOException) SourcePath(com.facebook.buck.rules.SourcePath) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) ExecutionContext(com.facebook.buck.step.ExecutionContext) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 4 with SimpleFileVisitor

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

the class ZipStep method execute.

@Override
public StepExecutionResult execute(ExecutionContext context) {
    if (filesystem.exists(pathToZipFile)) {
        context.postEvent(ConsoleEvent.severe("Attempting to overwrite an existing zip: %s", pathToZipFile));
        return StepExecutionResult.ERROR;
    }
    // Since filesystem traversals can be non-deterministic, sort the entries we find into
    // a tree map before writing them out.
    final Map<String, Pair<CustomZipEntry, Optional<Path>>> entries = Maps.newTreeMap();
    FileVisitor<Path> pathFileVisitor = new SimpleFileVisitor<Path>() {

        private boolean isSkipFile(Path file) {
            return !paths.isEmpty() && !paths.contains(file);
        }

        private String getEntryName(Path path) {
            Path relativePath = junkPaths ? path.getFileName() : baseDir.relativize(path);
            return MorePaths.pathWithUnixSeparators(relativePath);
        }

        private CustomZipEntry getZipEntry(String entryName, final Path path, BasicFileAttributes attr) throws IOException {
            boolean isDirectory = filesystem.isDirectory(path);
            if (isDirectory) {
                entryName += "/";
            }
            CustomZipEntry entry = new CustomZipEntry(entryName);
            // We want deterministic ZIPs, so avoid mtimes.
            entry.setFakeTime();
            entry.setCompressionLevel(isDirectory ? ZipCompressionLevel.MIN_COMPRESSION_LEVEL.getValue() : compressionLevel.getValue());
            // If we're using STORED files, we must manually set the CRC, size, and compressed size.
            if (entry.getMethod() == ZipEntry.STORED && !isDirectory) {
                entry.setSize(attr.size());
                entry.setCompressedSize(attr.size());
                entry.setCrc(new ByteSource() {

                    @Override
                    public InputStream openStream() throws IOException {
                        return filesystem.newFileInputStream(path);
                    }
                }.hash(Hashing.crc32()).padToLong());
            }
            long externalAttributes = filesystem.getFileAttributesForZipEntry(path);
            LOG.verbose("Setting mode for entry %s path %s to 0x%08X", entryName, path, externalAttributes);
            entry.setExternalAttributes(externalAttributes);
            return entry;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (!isSkipFile(file)) {
                CustomZipEntry entry = getZipEntry(getEntryName(file), file, attrs);
                entries.put(entry.getName(), new Pair<>(entry, Optional.of(file)));
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            if (!dir.equals(baseDir) && !isSkipFile(dir)) {
                CustomZipEntry entry = getZipEntry(getEntryName(dir), dir, attrs);
                entries.put(entry.getName(), new Pair<>(entry, Optional.empty()));
            }
            return FileVisitResult.CONTINUE;
        }
    };
    try (BufferedOutputStream baseOut = new BufferedOutputStream(filesystem.newFileOutputStream(pathToZipFile));
        CustomZipOutputStream out = ZipOutputStreams.newOutputStream(baseOut, THROW_EXCEPTION)) {
        filesystem.walkRelativeFileTree(baseDir, pathFileVisitor);
        // Write the entries out using the iteration order of the tree map above.
        for (Pair<CustomZipEntry, Optional<Path>> entry : entries.values()) {
            out.putNextEntry(entry.getFirst());
            if (entry.getSecond().isPresent()) {
                try (InputStream input = filesystem.newFileInputStream(entry.getSecond().get())) {
                    ByteStreams.copy(input, out);
                }
            }
            out.closeEntry();
        }
    } catch (IOException e) {
        context.logError(e, "Error creating zip file %s", pathToZipFile);
        return StepExecutionResult.ERROR;
    }
    return StepExecutionResult.SUCCESS;
}
Also used : Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) Optional(java.util.Optional) InputStream(java.io.InputStream) IOException(java.io.IOException) ByteSource(com.google.common.io.ByteSource) BufferedOutputStream(java.io.BufferedOutputStream) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Pair(com.facebook.buck.model.Pair)

Example 5 with SimpleFileVisitor

use of java.nio.file.SimpleFileVisitor 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

Path (java.nio.file.Path)11 SimpleFileVisitor (java.nio.file.SimpleFileVisitor)11 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)10 IOException (java.io.IOException)8 SourcePath (com.facebook.buck.rules.SourcePath)3 AbstractExecutionStep (com.facebook.buck.step.AbstractExecutionStep)2 ExecutionContext (com.facebook.buck.step.ExecutionContext)2 StepExecutionResult (com.facebook.buck.step.StepExecutionResult)2 MakeCleanDirectoryStep (com.facebook.buck.step.fs.MakeCleanDirectoryStep)2 InputStream (java.io.InputStream)2 FileVisitResult (java.nio.file.FileVisitResult)2 TargetCpuType (com.facebook.buck.android.NdkCxxPlatforms.TargetCpuType)1 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)1 Pair (com.facebook.buck.model.Pair)1 ExplicitBuildTargetSourcePath (com.facebook.buck.rules.ExplicitBuildTargetSourcePath)1 PathSourcePath (com.facebook.buck.rules.PathSourcePath)1 Step (com.facebook.buck.step.Step)1 CopyStep (com.facebook.buck.step.fs.CopyStep)1 MkdirStep (com.facebook.buck.step.fs.MkdirStep)1 XzStep (com.facebook.buck.step.fs.XzStep)1