Search in sources :

Example 46 with FileVisitResult

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

the class DirectoryCleaner method computeDirSizeBytesRecursively.

private static long computeDirSizeBytesRecursively(Path directoryPath) throws IOException {
    final AtomicLong totalSizeBytes = new AtomicLong(0);
    Files.walkFileTree(directoryPath, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            totalSizeBytes.addAndGet(attrs.size());
            return FileVisitResult.CONTINUE;
        }
    });
    return totalSizeBytes.get();
}
Also used : Path(java.nio.file.Path) AtomicLong(java.util.concurrent.atomic.AtomicLong) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 47 with FileVisitResult

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

the class ProjectGenerator method addCoreDataModelBuildPhase.

private void addCoreDataModelBuildPhase(PBXGroup targetGroup, Iterable<AppleWrapperResourceArg> dataModels) throws IOException {
    for (final AppleWrapperResourceArg dataModel : dataModels) {
        // Core data models go in the resources group also.
        PBXGroup resourcesGroup = targetGroup.getOrCreateChildGroupByName("Resources");
        if (CoreDataModelDescription.isVersionedDataModel(dataModel)) {
            // It's safe to do I/O here to figure out the current version because we're returning all
            // the versions and the file pointing to the current version from
            // getInputsToCompareToOutput(), so the rule will be correctly detected as stale if any of
            // them change.
            final String currentVersionFileName = ".xccurrentversion";
            final String currentVersionKey = "_XCCurrentVersionName";
            final XCVersionGroup versionGroup = resourcesGroup.getOrCreateChildVersionGroupsBySourceTreePath(new SourceTreePath(PBXReference.SourceTree.SOURCE_ROOT, pathRelativizer.outputDirToRootRelative(dataModel.path), Optional.empty()));
            projectFilesystem.walkRelativeFileTree(dataModel.path, new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
                    if (dir.equals(dataModel.path)) {
                        return FileVisitResult.CONTINUE;
                    }
                    versionGroup.getOrCreateFileReferenceBySourceTreePath(new SourceTreePath(PBXReference.SourceTree.SOURCE_ROOT, pathRelativizer.outputDirToRootRelative(dir), Optional.empty()));
                    return FileVisitResult.SKIP_SUBTREE;
                }
            });
            Path currentVersionPath = dataModel.path.resolve(currentVersionFileName);
            try (InputStream in = projectFilesystem.newFileInputStream(currentVersionPath)) {
                NSObject rootObject;
                try {
                    rootObject = PropertyListParser.parse(in);
                } catch (IOException e) {
                    throw e;
                } catch (Exception e) {
                    rootObject = null;
                }
                if (!(rootObject instanceof NSDictionary)) {
                    throw new HumanReadableException("Malformed %s file.", currentVersionFileName);
                }
                NSDictionary rootDictionary = (NSDictionary) rootObject;
                NSObject currentVersionName = rootDictionary.objectForKey(currentVersionKey);
                if (!(currentVersionName instanceof NSString)) {
                    throw new HumanReadableException("Malformed %s file.", currentVersionFileName);
                }
                PBXFileReference ref = versionGroup.getOrCreateFileReferenceBySourceTreePath(new SourceTreePath(PBXReference.SourceTree.SOURCE_ROOT, pathRelativizer.outputDirToRootRelative(dataModel.path.resolve(currentVersionName.toString())), Optional.empty()));
                versionGroup.setCurrentVersion(Optional.of(ref));
            } catch (NoSuchFileException e) {
                if (versionGroup.getChildren().size() == 1) {
                    versionGroup.setCurrentVersion(Optional.of(Iterables.get(versionGroup.getChildren(), 0)));
                }
            }
        } else {
            resourcesGroup.getOrCreateFileReferenceBySourceTreePath(new SourceTreePath(PBXReference.SourceTree.SOURCE_ROOT, pathRelativizer.outputDirToRootRelative(dataModel.path), Optional.empty()));
        }
    }
}
Also used : SourceTreePath(com.facebook.buck.apple.xcode.xcodeproj.SourceTreePath) SourceTreePath(com.facebook.buck.apple.xcode.xcodeproj.SourceTreePath) Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) NSObject(com.dd.plist.NSObject) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NSDictionary(com.dd.plist.NSDictionary) NoSuchFileException(java.nio.file.NoSuchFileException) FileVisitResult(java.nio.file.FileVisitResult) NSString(com.dd.plist.NSString) IOException(java.io.IOException) NSString(com.dd.plist.NSString) MacroException(com.facebook.buck.model.MacroException) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) IOException(java.io.IOException) NoSuchFileException(java.nio.file.NoSuchFileException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) HumanReadableException(com.facebook.buck.util.HumanReadableException) XCVersionGroup(com.facebook.buck.apple.xcode.xcodeproj.XCVersionGroup) HumanReadableException(com.facebook.buck.util.HumanReadableException) PBXGroup(com.facebook.buck.apple.xcode.xcodeproj.PBXGroup) AppleWrapperResourceArg(com.facebook.buck.apple.AppleWrapperResourceArg) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) PBXFileReference(com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference)

Example 48 with FileVisitResult

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

the class ArchiveStep method getAllInputs.

private ImmutableList<String> getAllInputs() throws IOException {
    ImmutableList.Builder<String> allInputs = ImmutableList.builder();
    // found from a recursive search.
    for (Path input : inputs) {
        if (filesystem.isDirectory(input)) {
            // We make sure to sort the files we find under the directories so that we get
            // deterministic output.
            final Set<String> dirFiles = new TreeSet<>();
            filesystem.walkFileTree(filesystem.resolve(input), new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                    dirFiles.add(file.toString());
                    return FileVisitResult.CONTINUE;
                }
            });
            allInputs.addAll(dirFiles);
        } else {
            allInputs.add(input.toString());
        }
    }
    return allInputs.build();
}
Also used : Path(java.nio.file.Path) ImmutableList(com.google.common.collect.ImmutableList) TreeSet(java.util.TreeSet) FileVisitResult(java.nio.file.FileVisitResult) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 49 with FileVisitResult

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

the class JarDirectoryStepHelper method addFilesInDirectoryToJar.

/**
   * @param directory that must not contain symlinks with loops.
   * @param jar is the file being written.
   */
private static void addFilesInDirectoryToJar(final ProjectFilesystem filesystem, final Path directory, CustomZipOutputStream jar, final Set<String> alreadyAddedEntries, final Iterable<Pattern> blacklist, final JavacEventSink eventSink) throws IOException {
    // Since filesystem traversals can be non-deterministic, sort the entries we find into
    // a tree map before writing them out.
    final Map<String, Pair<JarEntry, Optional<Path>>> entries = Maps.newTreeMap();
    filesystem.walkFileTree(directory, EnumSet.of(FileVisitOption.FOLLOW_LINKS), new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            String relativePath = MorePaths.pathWithUnixSeparators(MorePaths.relativize(directory, file));
            // Skip re-reading the manifest
            if (JarFile.MANIFEST_NAME.equals(relativePath)) {
                return FileVisitResult.CONTINUE;
            }
            // Check if the entry belongs to the blacklist and it should be excluded from the Jar.
            if (shouldEntryBeRemovedFromJar(eventSink, relativePath, blacklist)) {
                return FileVisitResult.CONTINUE;
            }
            JarEntry entry = new JarEntry(relativePath);
            String entryName = entry.getName();
            // We want deterministic JARs, so avoid mtimes.
            entry.setTime(ZipConstants.getFakeTime());
            // those repeatedly would be lame, so don't do that.
            if (!isDuplicateAllowed(entryName) && !alreadyAddedEntries.add(entryName)) {
                if (!entryName.endsWith("/")) {
                    eventSink.reportEvent(determineSeverity(entry), "Duplicate found when adding directory to jar: %s", relativePath);
                }
                return FileVisitResult.CONTINUE;
            }
            entries.put(entry.getName(), new Pair<>(entry, Optional.of(file)));
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            String relativePath = MorePaths.pathWithUnixSeparators(MorePaths.relativize(directory, dir));
            if (relativePath.isEmpty()) {
                // root of the tree. Skip.
                return FileVisitResult.CONTINUE;
            }
            String entryName = relativePath.replace('\\', '/') + "/";
            if (alreadyAddedEntries.contains(entryName)) {
                return FileVisitResult.CONTINUE;
            }
            JarEntry entry = new JarEntry(entryName);
            // We want deterministic JARs, so avoid mtimes.
            entry.setTime(ZipConstants.getFakeTime());
            entries.put(entry.getName(), new Pair<>(entry, Optional.empty()));
            return FileVisitResult.CONTINUE;
        }
    });
    // Write the entries out using the iteration order of the tree map above.
    for (Pair<JarEntry, Optional<Path>> entry : entries.values()) {
        jar.putNextEntry(entry.getFirst());
        if (entry.getSecond().isPresent()) {
            Files.copy(entry.getSecond().get(), jar);
        }
        jar.closeEntry();
    }
}
Also used : Path(java.nio.file.Path) Optional(java.util.Optional) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Pair(com.facebook.buck.model.Pair)

Example 50 with FileVisitResult

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

the class ProvisioningProfileCopyStepTest method setUp.

@Before
public void setUp() throws IOException {
    testdataDir = TestDataHelper.getTestDataDirectory(this).resolve("provisioning_profiles");
    projectFilesystem = new FakeProjectFilesystem(testdataDir);
    Files.walkFileTree(testdataDir, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            projectFilesystem.writeBytesToPath(Files.readAllBytes(file), projectFilesystem.resolve(file));
            return FileVisitResult.CONTINUE;
        }
    });
    tempOutputDir = tmp.getRoot();
    outputFile = tempOutputDir.resolve("embedded.mobileprovision");
    xcentFile = Paths.get("test.xcent");
    dryRunResultFile = Paths.get("test_dry_run_results.plist");
    executionContext = TestExecutionContext.newInstance();
    codeSignIdentityStore = CodeSignIdentityStore.fromIdentities(ImmutableList.of());
    entitlementsFile = testdataDir.resolve("Entitlements.plist");
}
Also used : Path(java.nio.file.Path) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Before(org.junit.Before)

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