Search in sources :

Example 76 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project buck by facebook.

the class ProjectWorkspace method setUp.

/**
   * This will copy the template directory, renaming files named {@code foo.fixture} to {@code foo}
   * in the process. Files whose names end in {@code .expected} will not be copied.
   */
@SuppressWarnings("PMD.EmptyCatchBlock")
public ProjectWorkspace setUp() throws IOException {
    MoreFiles.copyRecursively(templatePath, destPath, BUILD_FILE_RENAME);
    // Stamp the buck-out directory if it exists and isn't stamped already
    try (OutputStream outputStream = new BufferedOutputStream(Channels.newOutputStream(Files.newByteChannel(destPath.resolve(BuckConstant.getBuckOutputPath().resolve(".currentversion")), ImmutableSet.<OpenOption>of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE))))) {
        outputStream.write(BuckVersion.getVersion().getBytes(Charsets.UTF_8));
    } catch (FileAlreadyExistsException | NoSuchFileException e) {
    // If the current version file already exists we don't need to create it
    // If buck-out doesn't exist we don't need to stamp it
    }
    if (Platform.detect() == Platform.WINDOWS) {
        // Hack for symlinks on Windows.
        SimpleFileVisitor<Path> copyDirVisitor = new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                // On NTFS length of path must be greater than 0 and less than 4096.
                if (attrs.size() > 0 && attrs.size() <= 4096) {
                    String linkTo = new String(Files.readAllBytes(path), UTF_8);
                    Path linkToFile;
                    try {
                        linkToFile = templatePath.resolve(linkTo);
                    } catch (InvalidPathException e) {
                        // link.
                        return FileVisitResult.CONTINUE;
                    }
                    if (Files.isRegularFile(linkToFile)) {
                        Files.copy(linkToFile, path, StandardCopyOption.REPLACE_EXISTING);
                    } else if (Files.isDirectory(linkToFile)) {
                        Files.delete(path);
                        MoreFiles.copyRecursively(linkToFile, path);
                    }
                }
                return FileVisitResult.CONTINUE;
            }
        };
        Files.walkFileTree(destPath, copyDirVisitor);
    }
    if (!Files.exists(getPath(".buckconfig.local"))) {
        manageLocalConfigs = true;
        // Disable the directory cache by default.  Tests that want to enable it can call
        // `enableDirCache` on this object.  Only do this if a .buckconfig.local file does not already
        // exist, however (we assume the test knows what it is doing at that point).
        addBuckConfigLocalOption("cache", "mode", "");
        // Limit the number of threads by default to prevent multiple integration tests running at the
        // same time from creating a quadratic number of threads. Tests can disable this using
        // `disableThreadLimitOverride`.
        addBuckConfigLocalOption("build", "threads", "2");
    }
    isSetUp = true;
    return this;
}
Also used : Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) NoSuchFileException(java.nio.file.NoSuchFileException) BufferedOutputStream(java.io.BufferedOutputStream) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) InvalidPathException(java.nio.file.InvalidPathException)

Example 77 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project buck by facebook.

the class FakeProjectFilesystemTest method testReadFileAttributes.

@Test
public void testReadFileAttributes() throws IOException {
    FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    filesystem.touch(Paths.get("foo"));
    filesystem.mkdirs(Paths.get("bar"));
    filesystem.touch(Paths.get("bar/aaaa"));
    BasicFileAttributes attrs;
    attrs = filesystem.readAttributes(Paths.get("foo"), BasicFileAttributes.class);
    assertFalse(attrs.isDirectory());
    assertTrue(attrs.isRegularFile());
    attrs = filesystem.readAttributes(Paths.get("bar"), BasicFileAttributes.class);
    assertTrue(attrs.isDirectory());
    assertFalse(attrs.isRegularFile());
    attrs = filesystem.readAttributes(Paths.get("bar/aaaa"), BasicFileAttributes.class);
    assertFalse(attrs.isDirectory());
    assertTrue(attrs.isRegularFile());
}
Also used : BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Test(org.junit.Test)

Example 78 with BasicFileAttributes

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

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

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

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