Search in sources :

Example 1 with LinkOption

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

the class DefaultJavaLibraryTest method testRuleKeyIsOrderInsensitiveForSourcesAndResources.

@Test
public void testRuleKeyIsOrderInsensitiveForSourcesAndResources() throws Exception {
    // Note that these filenames were deliberately chosen to have identical hashes to maximize
    // the chance of order-sensitivity when being inserted into a HashMap.  Just using
    // {foo,bar}.{java,txt} resulted in a passing test even for the old broken code.
    ProjectFilesystem filesystem = new AllExistingProjectFilesystem() {

        @Override
        public boolean isDirectory(Path path, LinkOption... linkOptionsk) {
            return false;
        }
    };
    BuildRuleResolver resolver1 = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathRuleFinder ruleFinder1 = new SourcePathRuleFinder(resolver1);
    SourcePathResolver pathResolver1 = new SourcePathResolver(ruleFinder1);
    DefaultJavaLibrary rule1 = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//lib:lib")).addSrc(Paths.get("agifhbkjdec.java")).addSrc(Paths.get("bdeafhkgcji.java")).addSrc(Paths.get("bdehgaifjkc.java")).addSrc(Paths.get("cfiabkjehgd.java")).addResource(new FakeSourcePath("becgkaifhjd.txt")).addResource(new FakeSourcePath("bkhajdifcge.txt")).addResource(new FakeSourcePath("cabfghjekid.txt")).addResource(new FakeSourcePath("chkdbafijge.txt")).build(resolver1, filesystem);
    BuildRuleResolver resolver2 = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathRuleFinder ruleFinder2 = new SourcePathRuleFinder(resolver2);
    SourcePathResolver pathResolver2 = new SourcePathResolver(ruleFinder2);
    DefaultJavaLibrary rule2 = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//lib:lib")).addSrc(Paths.get("cfiabkjehgd.java")).addSrc(Paths.get("bdehgaifjkc.java")).addSrc(Paths.get("bdeafhkgcji.java")).addSrc(Paths.get("agifhbkjdec.java")).addResource(new FakeSourcePath("chkdbafijge.txt")).addResource(new FakeSourcePath("cabfghjekid.txt")).addResource(new FakeSourcePath("bkhajdifcge.txt")).addResource(new FakeSourcePath("becgkaifhjd.txt")).build(resolver2, filesystem);
    ImmutableMap.Builder<String, String> fileHashes = ImmutableMap.builder();
    for (String filename : ImmutableList.of("agifhbkjdec.java", "bdeafhkgcji.java", "bdehgaifjkc.java", "cfiabkjehgd.java", "becgkaifhjd.txt", "bkhajdifcge.txt", "cabfghjekid.txt", "chkdbafijge.txt")) {
        fileHashes.put(filename, Hashing.sha1().hashString(filename, Charsets.UTF_8).toString());
    }
    DefaultRuleKeyFactory ruleKeyFactory = new DefaultRuleKeyFactory(0, FakeFileHashCache.createFromStrings(fileHashes.build()), pathResolver1, ruleFinder1);
    DefaultRuleKeyFactory ruleKeyFactory2 = new DefaultRuleKeyFactory(0, FakeFileHashCache.createFromStrings(fileHashes.build()), pathResolver2, ruleFinder2);
    RuleKey key1 = ruleKeyFactory.build(rule1);
    RuleKey key2 = ruleKeyFactory2.build(rule2);
    assertEquals(key1, key2);
}
Also used : Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) LinkOption(java.nio.file.LinkOption) RuleKey(com.facebook.buck.rules.RuleKey) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) ImmutableMap(com.google.common.collect.ImmutableMap) AllExistingProjectFilesystem(com.facebook.buck.testutil.AllExistingProjectFilesystem) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) AllExistingProjectFilesystem(com.facebook.buck.testutil.AllExistingProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 2 with LinkOption

use of java.nio.file.LinkOption in project graal by oracle.

the class IOHelper method copy.

static void copy(final Path source, final Path target, final FileSystem sourceFileSystem, final FileSystem targetFileSystem, CopyOption... options) throws IOException {
    if (source.equals(target)) {
        return;
    }
    final Path sourceReal = sourceFileSystem.toRealPath(source, LinkOption.NOFOLLOW_LINKS);
    final Path targetReal = targetFileSystem.toRealPath(target, LinkOption.NOFOLLOW_LINKS);
    if (sourceReal.equals(targetReal)) {
        return;
    }
    final Set<LinkOption> linkOptions = new HashSet<>();
    final Set<StandardCopyOption> copyOptions = EnumSet.noneOf(StandardCopyOption.class);
    for (CopyOption option : options) {
        if (option instanceof StandardCopyOption) {
            copyOptions.add((StandardCopyOption) option);
        } else if (option instanceof LinkOption) {
            linkOptions.add((LinkOption) option);
        }
    }
    if (copyOptions.contains(StandardCopyOption.ATOMIC_MOVE)) {
        throw new AtomicMoveNotSupportedException(source.getFileName().toString(), target.getFileName().toString(), "Atomic move not supported");
    }
    final Map<String, Object> sourceAttributes = sourceFileSystem.readAttributes(sourceReal, "basic:isSymbolicLink,isDirectory,lastModifiedTime,lastAccessTime,creationTime", linkOptions.toArray(new LinkOption[linkOptions.size()]));
    if ((Boolean) sourceAttributes.getOrDefault("isSymbolicLink", false)) {
        throw new IOException("Copying of symbolic links is not supported.");
    }
    if (copyOptions.contains(StandardCopyOption.REPLACE_EXISTING)) {
        try {
            targetFileSystem.delete(targetReal);
        } catch (NoSuchFileException notFound) {
        // Does not exist - nothing to delete
        }
    } else {
        boolean exists;
        try {
            targetFileSystem.checkAccess(targetReal, EnumSet.noneOf(AccessMode.class));
            exists = true;
        } catch (IOException ioe) {
            exists = false;
        }
        if (exists) {
            throw new FileAlreadyExistsException(target.toString());
        }
    }
    if ((Boolean) sourceAttributes.getOrDefault("isDirectory", false)) {
        targetFileSystem.createDirectory(targetReal);
    } else {
        final Set<StandardOpenOption> readOptions = EnumSet.of(StandardOpenOption.READ);
        final Set<StandardOpenOption> writeOptions = EnumSet.of(StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW);
        try (final SeekableByteChannel sourceChannel = sourceFileSystem.newByteChannel(sourceReal, readOptions);
            final SeekableByteChannel targetChannel = targetFileSystem.newByteChannel(targetReal, writeOptions)) {
            final ByteBuffer buffer = ByteBuffer.allocateDirect(1 << 16);
            while (sourceChannel.read(buffer) != -1) {
                buffer.flip();
                while (buffer.hasRemaining()) {
                    targetChannel.write(buffer);
                }
                buffer.clear();
            }
        }
    }
    if (copyOptions.contains(StandardCopyOption.COPY_ATTRIBUTES)) {
        String[] basicMutableAttributes = { "lastModifiedTime", "lastAccessTime", "creationTime" };
        try {
            for (String key : basicMutableAttributes) {
                final Object value = sourceAttributes.get(key);
                if (value != null) {
                    targetFileSystem.setAttribute(targetReal, key, value);
                }
            }
        } catch (Throwable rootCause) {
            try {
                targetFileSystem.delete(targetReal);
            } catch (Throwable suppressed) {
                rootCause.addSuppressed(suppressed);
            }
            throw rootCause;
        }
    }
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) StandardCopyOption(java.nio.file.StandardCopyOption) CopyOption(java.nio.file.CopyOption) LinkOption(java.nio.file.LinkOption) StandardOpenOption(java.nio.file.StandardOpenOption) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) SeekableByteChannel(java.nio.channels.SeekableByteChannel) StandardCopyOption(java.nio.file.StandardCopyOption) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) AccessMode(java.nio.file.AccessMode) HashSet(java.util.HashSet)

Example 3 with LinkOption

use of java.nio.file.LinkOption in project cryptofs by cryptomator.

the class CryptoFileSystemProviderTest method testSetAttributeDelegatesToFileSystem.

@Test
public void testSetAttributeDelegatesToFileSystem() throws IOException {
    LinkOption option = LinkOption.NOFOLLOW_LINKS;
    String attribute = "foo";
    String value = "bar";
    inTest.setAttribute(cryptoPath, attribute, value, option);
    verify(cryptoFileSystem).setAttribute(cryptoPath, attribute, value, option);
}
Also used : LinkOption(java.nio.file.LinkOption) Test(org.junit.Test)

Example 4 with LinkOption

use of java.nio.file.LinkOption in project spring-boot by spring-projects.

the class ExtractCommandTests method timeAttributes.

private void timeAttributes(File file) {
    try {
        BasicFileAttributes basicAttributes = Files.getFileAttributeView(file.toPath(), BasicFileAttributeView.class, new LinkOption[0]).readAttributes();
        assertThat(basicAttributes.lastModifiedTime().to(TimeUnit.SECONDS)).isEqualTo(LAST_MODIFIED_TIME.to(TimeUnit.SECONDS));
        assertThat(basicAttributes.creationTime().to(TimeUnit.SECONDS)).satisfiesAnyOf((creationTime) -> assertThat(creationTime).isEqualTo(CREATION_TIME.to(TimeUnit.SECONDS)), // On macOS (at least) the creation time is the last modified time
        (creationTime) -> assertThat(creationTime).isEqualTo(LAST_MODIFIED_TIME.to(TimeUnit.SECONDS)));
        assertThat(basicAttributes.lastAccessTime().to(TimeUnit.SECONDS)).isEqualTo(LAST_ACCESS_TIME.to(TimeUnit.SECONDS));
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) LinkOption(java.nio.file.LinkOption) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 5 with LinkOption

use of java.nio.file.LinkOption in project mycore by MyCoRe-Org.

the class MCRFileSystemProvider method copy.

/* (non-Javadoc)
     * @see java.nio.file.spi.FileSystemProvider#copy(java.nio.file.Path, java.nio.file.Path, java.nio.file.CopyOption[])
     */
@Override
public void copy(Path source, Path target, CopyOption... options) throws IOException {
    if (isSameFile(source, target)) {
        // that was easy
        return;
    }
    checkCopyOptions(options);
    HashSet<CopyOption> copyOptions = Sets.newHashSet(options);
    boolean createNew = !copyOptions.contains(StandardCopyOption.REPLACE_EXISTING);
    MCRPath src = MCRFileSystemUtils.checkPathAbsolute(source);
    MCRPath tgt = MCRFileSystemUtils.checkPathAbsolute(target);
    MCRFilesystemNode srcNode = resolvePath(src);
    // checkParent of target;
    if (tgt.getNameCount() == 0 && srcNode instanceof MCRDirectory) {
        MCRDirectory tgtDir = MCRDirectory.getRootDirectory(tgt.getOwner());
        if (tgtDir != null) {
            if (tgtDir.hasChildren() && copyOptions.contains(StandardCopyOption.REPLACE_EXISTING)) {
                throw new DirectoryNotEmptyException(tgt.toString());
            }
        } else {
            // TODO: handle StandardCopyOption.COPY_ATTRIBUTES
            tgtDir = new MCRDirectory(tgt.getOwner());
        }
        // created new root component
        return;
    }
    MCRDirectory tgtParentDir = (MCRDirectory) resolvePath(tgt.getParent());
    if (srcNode instanceof MCRFile) {
        MCRFile srcFile = (MCRFile) srcNode;
        MCRFile targetFile = MCRFileSystemUtils.getMCRFile(tgt, true, createNew);
        targetFile.setContentFrom(srcFile.getContentAsInputStream());
        if (copyOptions.contains(StandardCopyOption.COPY_ATTRIBUTES)) {
            @SuppressWarnings("unchecked") MCRMD5AttributeView<String> srcAttrView = Files.getFileAttributeView(src, MCRMD5AttributeView.class, (LinkOption[]) null);
            File targetLocalFile = targetFile.getLocalFile();
            BasicFileAttributeView targetBasicFileAttributeView = Files.getFileAttributeView(targetLocalFile.toPath(), BasicFileAttributeView.class);
            MCRFileAttributes<String> srcAttr = srcAttrView.readAllAttributes();
            targetFile.adjustMetadata(srcAttr.lastModifiedTime(), srcFile.getMD5(), srcFile.getSize());
            targetBasicFileAttributeView.setTimes(srcAttr.lastModifiedTime(), srcAttr.lastAccessTime(), srcAttr.creationTime());
        }
    } else if (srcNode instanceof MCRDirectory) {
        MCRFilesystemNode child = tgtParentDir.getChild(tgt.getFileName().toString());
        if (child != null) {
            if (!copyOptions.contains(StandardCopyOption.REPLACE_EXISTING)) {
                throw new FileAlreadyExistsException(tgtParentDir.toString(), tgt.getFileName().toString(), null);
            }
            if (child instanceof MCRFile) {
                throw new NotDirectoryException(tgt.toString());
            }
            MCRDirectory tgtDir = (MCRDirectory) child;
            if (tgtDir.hasChildren() && copyOptions.contains(StandardCopyOption.REPLACE_EXISTING)) {
                throw new DirectoryNotEmptyException(tgt.toString());
            }
        // TODO: handle StandardCopyOption.COPY_ATTRIBUTES
        } else {
            // simply create directory
            @SuppressWarnings("unused") MCRDirectory tgtDir = new MCRDirectory(tgt.getFileName().toString(), tgtParentDir);
        // TODO: handle StandardCopyOption.COPY_ATTRIBUTES
        }
    }
}
Also used : MCRFile(org.mycore.datamodel.ifs.MCRFile) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) CopyOption(java.nio.file.CopyOption) StandardCopyOption(java.nio.file.StandardCopyOption) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) LinkOption(java.nio.file.LinkOption) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) NotDirectoryException(java.nio.file.NotDirectoryException) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRFile(org.mycore.datamodel.ifs.MCRFile) File(java.io.File)

Aggregations

LinkOption (java.nio.file.LinkOption)11 Test (org.junit.Test)6 Path (java.nio.file.Path)4 IOException (java.io.IOException)3 CopyOption (java.nio.file.CopyOption)3 StandardCopyOption (java.nio.file.StandardCopyOption)3 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)3 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)2 DefaultBuildTargetSourcePath (com.facebook.buck.rules.DefaultBuildTargetSourcePath)2 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)2 PathSourcePath (com.facebook.buck.rules.PathSourcePath)2 SourcePath (com.facebook.buck.rules.SourcePath)2 AllExistingProjectFilesystem (com.facebook.buck.testutil.AllExistingProjectFilesystem)2 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)2 SeekableByteChannel (java.nio.channels.SeekableByteChannel)2 AccessMode (java.nio.file.AccessMode)2 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)2 NoSuchFileException (java.nio.file.NoSuchFileException)2 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)2 FileAttributeView (java.nio.file.attribute.FileAttributeView)2