Search in sources :

Example 86 with BasicFileAttributes

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

the class ProvisioningProfileStore method fromSearchPath.

public static ProvisioningProfileStore fromSearchPath(final ProcessExecutor executor, final ImmutableList<String> readCommand, final Path searchPath) {
    LOG.debug("Provisioning profile search path: " + searchPath);
    return new ProvisioningProfileStore(Suppliers.memoize(() -> {
        final ImmutableList.Builder<ProvisioningProfileMetadata> profilesBuilder = ImmutableList.builder();
        try {
            Files.walkFileTree(searchPath.toAbsolutePath(), new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    if (file.toString().endsWith(".mobileprovision")) {
                        try {
                            ProvisioningProfileMetadata profile = ProvisioningProfileMetadata.fromProvisioningProfilePath(executor, readCommand, file);
                            profilesBuilder.add(profile);
                        } catch (IOException | IllegalArgumentException e) {
                            LOG.error(e, "Ignoring invalid or malformed .mobileprovision file");
                        } catch (InterruptedException e) {
                            throw new IOException(e);
                        }
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (IOException e) {
            if (e.getCause() instanceof InterruptedException) {
                LOG.error(e, "Interrupted while searching for mobileprovision files");
            } else {
                LOG.error(e, "Error while searching for mobileprovision files");
            }
        }
        return profilesBuilder.build();
    }));
}
Also used : Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 87 with BasicFileAttributes

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

the class MoreFiles method copyRecursively.

public static void copyRecursively(final Path fromPath, final Path toPath, final Function<Path, Path> transform, final Function<Path, Boolean> filter) throws IOException {
    // Adapted from http://codingjunkie.net/java-7-copy-move/.
    SimpleFileVisitor<Path> copyDirVisitor = new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            Path targetPath = toPath.resolve(fromPath.relativize(dir));
            if (!Files.exists(targetPath)) {
                Files.createDirectory(targetPath);
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (!filter.apply(file)) {
                return FileVisitResult.CONTINUE;
            }
            Path destPath = toPath.resolve(fromPath.relativize(file));
            Path transformedDestPath = transform.apply(destPath);
            if (transformedDestPath != null) {
                if (Files.isSymbolicLink(file)) {
                    Files.deleteIfExists(transformedDestPath);
                    Files.createSymbolicLink(transformedDestPath, Files.readSymbolicLink(file));
                } else {
                    Files.copy(file, transformedDestPath, StandardCopyOption.REPLACE_EXISTING);
                }
            }
            return FileVisitResult.CONTINUE;
        }
    };
    Files.walkFileTree(fromPath, copyDirVisitor);
}
Also used : Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 88 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project jimfs by google.

the class JimfsUnixLikeFileSystemTest method testCopy_withCopyAttributes.

@Test
public void testCopy_withCopyAttributes() throws IOException {
    Path foo = path("/foo");
    Files.createFile(foo);
    Files.getFileAttributeView(foo, BasicFileAttributeView.class).setTimes(FileTime.fromMillis(100), FileTime.fromMillis(1000), FileTime.fromMillis(10000));
    assertThat(Files.getAttribute(foo, "lastModifiedTime")).isEqualTo(FileTime.fromMillis(100));
    UserPrincipal zero = fs.getUserPrincipalLookupService().lookupPrincipalByName("zero");
    Files.setAttribute(foo, "owner:owner", zero);
    Path bar = path("/bar");
    Files.copy(foo, bar, COPY_ATTRIBUTES);
    BasicFileAttributes attributes = Files.readAttributes(bar, BasicFileAttributes.class);
    assertThat(attributes.lastModifiedTime()).isEqualTo(FileTime.fromMillis(100));
    assertThat(attributes.lastAccessTime()).isEqualTo(FileTime.fromMillis(1000));
    assertThat(attributes.creationTime()).isEqualTo(FileTime.fromMillis(10000));
    assertThat(Files.getAttribute(bar, "owner:owner")).isEqualTo(zero);
    Path baz = path("/baz");
    Files.copy(foo, baz);
    // test that attributes are not copied when COPY_ATTRIBUTES is not specified
    attributes = Files.readAttributes(baz, BasicFileAttributes.class);
    assertThat(attributes.lastModifiedTime()).isNotEqualTo(FileTime.fromMillis(100));
    assertThat(attributes.lastAccessTime()).isNotEqualTo(FileTime.fromMillis(1000));
    assertThat(attributes.creationTime()).isNotEqualTo(FileTime.fromMillis(10000));
    assertThat(Files.getAttribute(baz, "owner:owner")).isNotEqualTo(zero);
}
Also used : Path(java.nio.file.Path) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) UserPrincipal(java.nio.file.attribute.UserPrincipal) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Test(org.junit.Test)

Example 89 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project jimfs by google.

the class BasicAttributeProviderTest method testAttributes.

@Test
public void testAttributes() {
    BasicFileAttributes attrs = provider.readAttributes(file);
    assertThat(attrs.fileKey()).isEqualTo(0);
    assertThat(attrs.isDirectory()).isTrue();
    assertThat(attrs.isRegularFile()).isFalse();
    assertThat(attrs.creationTime()).isNotNull();
}
Also used : BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Test(org.junit.Test)

Example 90 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project jimfs by google.

the class BasicAttributeProviderTest method testView.

@Test
public void testView() throws IOException {
    BasicFileAttributeView view = provider.view(fileLookup(), NO_INHERITED_VIEWS);
    assertThat(view).isNotNull();
    assertThat(view.name()).isEqualTo("basic");
    BasicFileAttributes attrs = view.readAttributes();
    assertThat(attrs.fileKey()).isEqualTo(0);
    FileTime time = attrs.creationTime();
    assertThat(attrs.lastAccessTime()).isEqualTo(time);
    assertThat(attrs.lastModifiedTime()).isEqualTo(time);
    view.setTimes(null, null, null);
    attrs = view.readAttributes();
    assertThat(attrs.creationTime()).isEqualTo(time);
    assertThat(attrs.lastAccessTime()).isEqualTo(time);
    assertThat(attrs.lastModifiedTime()).isEqualTo(time);
    view.setTimes(FileTime.fromMillis(0L), null, null);
    attrs = view.readAttributes();
    assertThat(attrs.creationTime()).isEqualTo(time);
    assertThat(attrs.lastAccessTime()).isEqualTo(time);
    assertThat(attrs.lastModifiedTime()).isEqualTo(FileTime.fromMillis(0L));
}
Also used : BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) FileTime(java.nio.file.attribute.FileTime) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Test(org.junit.Test)

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