Search in sources :

Example 1 with FileAttributeView

use of java.nio.file.attribute.FileAttributeView in project cryptofs by cryptomator.

the class CryptoFileSystemImpl method copyAttributes.

private void copyAttributes(Path src, Path dst) throws IOException {
    Set<Class<? extends FileAttributeView>> supportedAttributeViewTypes = fileStore.supportedFileAttributeViewTypes();
    if (supportedAttributeViewTypes.contains(BasicFileAttributeView.class)) {
        BasicFileAttributes srcAttrs = Files.readAttributes(src, BasicFileAttributes.class);
        BasicFileAttributeView dstAttrView = Files.getFileAttributeView(dst, BasicFileAttributeView.class);
        dstAttrView.setTimes(srcAttrs.lastModifiedTime(), srcAttrs.lastAccessTime(), srcAttrs.creationTime());
    }
    if (supportedAttributeViewTypes.contains(FileOwnerAttributeView.class)) {
        FileOwnerAttributeView srcAttrView = Files.getFileAttributeView(src, FileOwnerAttributeView.class);
        FileOwnerAttributeView dstAttrView = Files.getFileAttributeView(dst, FileOwnerAttributeView.class);
        dstAttrView.setOwner(srcAttrView.getOwner());
    }
    if (supportedAttributeViewTypes.contains(PosixFileAttributeView.class)) {
        PosixFileAttributes srcAttrs = Files.readAttributes(src, PosixFileAttributes.class);
        PosixFileAttributeView dstAttrView = Files.getFileAttributeView(dst, PosixFileAttributeView.class);
        dstAttrView.setGroup(srcAttrs.group());
        dstAttrView.setPermissions(srcAttrs.permissions());
    }
    if (supportedAttributeViewTypes.contains(DosFileAttributeView.class)) {
        DosFileAttributes srcAttrs = Files.readAttributes(src, DosFileAttributes.class);
        DosFileAttributeView dstAttrView = Files.getFileAttributeView(dst, DosFileAttributeView.class);
        dstAttrView.setArchive(srcAttrs.isArchive());
        dstAttrView.setHidden(srcAttrs.isHidden());
        dstAttrView.setReadOnly(srcAttrs.isReadOnly());
        dstAttrView.setSystem(srcAttrs.isSystem());
    }
}
Also used : BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) FileOwnerAttributeView(java.nio.file.attribute.FileOwnerAttributeView) DosFileAttributeView(java.nio.file.attribute.DosFileAttributeView) DosFileAttributes(java.nio.file.attribute.DosFileAttributes) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) DosFileAttributeView(java.nio.file.attribute.DosFileAttributeView) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) FileAttributeView(java.nio.file.attribute.FileAttributeView) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 2 with FileAttributeView

use of java.nio.file.attribute.FileAttributeView in project cryptofs by cryptomator.

the class CryptoFileSystemProviderTest method testGetFileAttributeViewDelegatesToFileSystem.

@Test
public void testGetFileAttributeViewDelegatesToFileSystem() {
    FileAttributeView view = mock(FileAttributeView.class);
    LinkOption option = LinkOption.NOFOLLOW_LINKS;
    when(cryptoFileSystem.getFileAttributeView(cryptoPath, FileAttributeView.class, option)).thenReturn(view);
    FileAttributeView result = inTest.getFileAttributeView(cryptoPath, FileAttributeView.class, option);
    assertThat(result, is(view));
}
Also used : LinkOption(java.nio.file.LinkOption) FileAttributeView(java.nio.file.attribute.FileAttributeView) Test(org.junit.Test)

Example 3 with FileAttributeView

use of java.nio.file.attribute.FileAttributeView in project cryptofs by cryptomator.

the class CryptoFileAttributeByNameProvider method setAttribute.

@SuppressWarnings({ "unchecked", "rawtypes" })
public void setAttribute(Path path, String attributeName, Object value) throws IOException {
    String normalizedAttributeName = normalizedAttributeName(attributeName);
    AttributeSetter setter = SETTERS.get(normalizedAttributeName);
    if (setter == null) {
        throw new IllegalArgumentException("Unrecognized attribute name: " + attributeName);
    }
    FileAttributeView view = cryptoFileAttributeViewProvider.getAttributeView(path, setter.type());
    setter.set(view, value);
}
Also used : PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView) DosFileAttributeView(java.nio.file.attribute.DosFileAttributeView) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) FileAttributeView(java.nio.file.attribute.FileAttributeView)

Example 4 with FileAttributeView

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

the class AttributeService method createInheritedViews.

private void createInheritedViews(FileLookup lookup, AttributeProvider provider, Map<String, FileAttributeView> inheritedViews) {
    for (String inherited : provider.inherits()) {
        if (!inheritedViews.containsKey(inherited)) {
            AttributeProvider inheritedProvider = providersByName.get(inherited);
            FileAttributeView inheritedView = getFileAttributeView(lookup, inheritedProvider.viewType(), inheritedViews);
            inheritedViews.put(inherited, inheritedView);
        }
    }
}
Also used : FileAttributeView(java.nio.file.attribute.FileAttributeView)

Example 5 with FileAttributeView

use of java.nio.file.attribute.FileAttributeView in project structr by structr.

the class StructrSSHFileSystem method provider.

@Override
public FileSystemProvider provider() {
    logger.info("x");
    return new FileSystemProvider() {

        @Override
        public OutputStream newOutputStream(Path path, OpenOption... options) throws IOException {
            logger.info("x");
            OutputStream os = null;
            File actualFile = (File) ((StructrSSHFile) path).getActualFile();
            try (final Tx tx = StructrApp.getInstance(securityContext).tx()) {
                if (actualFile == null) {
                    actualFile = (File) create(path);
                }
                if (actualFile != null) {
                    os = ((File) actualFile).getOutputStream();
                }
                tx.success();
            } catch (FrameworkException fex) {
                logger.warn("", fex);
                throw new IOException(fex);
            }
            return os;
        }

        @Override
        public InputStream newInputStream(Path path, OpenOption... options) throws IOException {
            // Remote file => file node in Structr
            logger.info("x");
            InputStream inputStream = null;
            try (final Tx tx = StructrApp.getInstance(securityContext).tx()) {
                final File fileNode = (File) ((StructrSSHFile) path).getActualFile();
                inputStream = fileNode.getInputStream();
                tx.success();
            } catch (FrameworkException fex) {
                logger.warn("", fex);
                throw new IOException(fex);
            }
            return inputStream;
        }

        @Override
        public String getScheme() {
            logger.info("Method not implemented yet");
            return null;
        }

        @Override
        public FileSystem newFileSystem(URI uri, Map<String, ?> env) throws IOException {
            logger.info("Method not implemented yet");
            return null;
        }

        @Override
        public FileSystem getFileSystem(URI uri) {
            logger.info("Method not implemented yet");
            return null;
        }

        @Override
        public Path getPath(URI uri) {
            logger.info("Method not implemented yet");
            return null;
        }

        @Override
        public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
            logger.info("x");
            SeekableByteChannel channel = null;
            final File fileNode = (File) ((StructrSSHFile) path).getActualFile();
            if (fileNode != null) {
                try (Tx tx = StructrApp.getInstance(securityContext).tx()) {
                    final Path filePath = fileNode.getFileOnDisk().toPath();
                    channel = Files.newByteChannel(filePath);
                    tx.success();
                } catch (FrameworkException fex) {
                    logger.error("", fex);
                    throw new IOException(fex);
                }
            }
            return channel;
        }

        @Override
        public DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter) throws IOException {
            logger.info("x");
            return new DirectoryStream() {

                boolean closed = false;

                @Override
                public Iterator iterator() {
                    if (!closed) {
                        final App app = StructrApp.getInstance(securityContext);
                        final List<StructrSSHFile> files = new LinkedList<>();
                        final StructrSSHFile thisDir = (StructrSSHFile) dir;
                        try (final Tx tx = app.tx()) {
                            for (final Folder child : thisDir.getFolders()) {
                                files.add(new StructrSSHFile(thisDir, child.getName(), child));
                            }
                            for (final File child : thisDir.getFiles()) {
                                files.add(new StructrSSHFile(thisDir, child.getName(), child));
                            }
                            tx.success();
                        } catch (FrameworkException fex) {
                            logger.warn("", fex);
                        }
                        return files.iterator();
                    }
                    return Collections.emptyIterator();
                }

                @Override
                public void close() throws IOException {
                    closed = true;
                }
            };
        }

        @Override
        public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException {
            logger.info("x");
            final StructrSSHFile parent = (StructrSSHFile) dir.getParent();
            final App app = StructrApp.getInstance(securityContext);
            final String name = dir.getFileName().toString();
            try (final Tx tx = app.tx()) {
                final Folder folder = app.create(Folder.class, new NodeAttribute(AbstractNode.name, name), new NodeAttribute(StructrApp.key(AbstractFile.class, "parent"), parent != null ? parent.getActualFile() : null));
                ((StructrSSHFile) dir).setActualFile(folder);
                tx.success();
            } catch (FrameworkException fex) {
                logger.warn("", fex);
                throw new IOException(fex);
            }
        }

        @Override
        public void delete(Path path) throws IOException {
            logger.info("Method not implemented yet");
        }

        @Override
        public void copy(Path source, Path target, CopyOption... options) throws IOException {
            logger.info("Method not implemented yet");
        }

        @Override
        public void move(Path source, Path target, CopyOption... options) throws IOException {
            logger.info("Method not implemented yet");
        }

        @Override
        public boolean isSameFile(Path path, Path path2) throws IOException {
            logger.info("x");
            return path != null && path.equals(path);
        }

        @Override
        public boolean isHidden(Path path) throws IOException {
            logger.info("Method not implemented yet");
            return false;
        }

        @Override
        public FileStore getFileStore(Path path) throws IOException {
            logger.info("Method not implemented yet");
            return null;
        }

        @Override
        public void checkAccess(Path path, AccessMode... modes) throws IOException {
            logger.info("Checking access", new Object[] { path, modes });
        }

        @Override
        public <V extends FileAttributeView> V getFileAttributeView(final Path path, final Class<V> type, final LinkOption... options) {
            logger.info("x");
            return (V) new PosixFileAttributeView() {

                @Override
                public String name() {
                    return "posix";
                }

                @Override
                public PosixFileAttributes readAttributes() throws IOException {
                    return new StructrPosixFileAttributes((StructrSSHFile) path);
                }

                @Override
                public void setPermissions(Set<PosixFilePermission> set) throws IOException {
                    logger.info("Method not implemented yet");
                }

                @Override
                public void setGroup(GroupPrincipal gp) throws IOException {
                    logger.info("Method not implemented yet");
                }

                @Override
                public void setTimes(FileTime ft, FileTime ft1, FileTime ft2) throws IOException {
                    logger.info("Method not implemented yet");
                }

                @Override
                public UserPrincipal getOwner() throws IOException {
                    logger.info("Method not implemented yet");
                    return null;
                }

                @Override
                public void setOwner(UserPrincipal up) throws IOException {
                    logger.info("Method not implemented yet");
                }
            };
        }

        @Override
        public <A extends BasicFileAttributes> A readAttributes(Path path, Class<A> type, LinkOption... options) throws IOException {
            logger.info("x");
            if (path != null) {
                if (path instanceof StructrSSHFile) {
                    final StructrSSHFile sshFile = (StructrSSHFile) path;
                    if (sshFile.getActualFile() == null) {
                        throw new NoSuchFileException("SSH file doesn't exist");
                    }
                    BasicFileAttributes attrs = new StructrPosixFileAttributes((StructrSSHFile) path);
                    return (A) attrs;
                }
            }
            throw new IOException("Unable to read attributes: Path is null");
        }

        @Override
        public Map<String, Object> readAttributes(Path path, String attributes, LinkOption... options) throws IOException {
            return Collections.EMPTY_MAP;
        }

        @Override
        public void setAttribute(Path path, String attribute, Object value, LinkOption... options) throws IOException {
            logger.info("Method not implemented yet");
            ;
        }

        private AbstractFile create(final Path path) throws IOException {
            logger.info("x");
            final StructrSSHFile parent = (StructrSSHFile) path.getParent();
            AbstractFile newFile = null;
            final App app = StructrApp.getInstance(securityContext);
            try (final Tx tx = app.tx()) {
                final String fileName = path.getFileName().toString();
                final Folder parentFolder = (Folder) parent.getActualFile();
                newFile = app.create(File.class, new NodeAttribute(AbstractNode.name, fileName), new NodeAttribute(StructrApp.key(AbstractFile.class, "parent"), parentFolder));
                tx.success();
            } catch (FrameworkException fex) {
                logger.warn("", fex);
                throw new IOException(fex);
            }
            return newFile;
        }
    };
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) HashSet(java.util.HashSet) Set(java.util.Set) CopyOption(java.nio.file.CopyOption) AbstractFile(org.structr.web.entity.AbstractFile) OutputStream(java.io.OutputStream) DirectoryStream(java.nio.file.DirectoryStream) NoSuchFileException(java.nio.file.NoSuchFileException) FileTime(java.nio.file.attribute.FileTime) Folder(org.structr.web.entity.Folder) URI(java.net.URI) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Path(java.nio.file.Path) NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) LinkOption(java.nio.file.LinkOption) InputStream(java.io.InputStream) IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) LinkedList(java.util.LinkedList) UserPrincipal(java.nio.file.attribute.UserPrincipal) SeekableByteChannel(java.nio.channels.SeekableByteChannel) OpenOption(java.nio.file.OpenOption) GroupPrincipal(java.nio.file.attribute.GroupPrincipal) FileSystemProvider(java.nio.file.spi.FileSystemProvider) AccessMode(java.nio.file.AccessMode) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File) Map(java.util.Map) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView) FileAttributeView(java.nio.file.attribute.FileAttributeView) FileAttribute(java.nio.file.attribute.FileAttribute)

Aggregations

FileAttributeView (java.nio.file.attribute.FileAttributeView)5 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)3 LinkOption (java.nio.file.LinkOption)2 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)2 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)2 DosFileAttributeView (java.nio.file.attribute.DosFileAttributeView)2 PosixFileAttributes (java.nio.file.attribute.PosixFileAttributes)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 SeekableByteChannel (java.nio.channels.SeekableByteChannel)1 AccessMode (java.nio.file.AccessMode)1 CopyOption (java.nio.file.CopyOption)1 DirectoryStream (java.nio.file.DirectoryStream)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 OpenOption (java.nio.file.OpenOption)1 Path (java.nio.file.Path)1 DosFileAttributes (java.nio.file.attribute.DosFileAttributes)1 FileAttribute (java.nio.file.attribute.FileAttribute)1