Search in sources :

Example 1 with MCRFilesystemNode

use of org.mycore.datamodel.ifs.MCRFilesystemNode in project mycore by MyCoRe-Org.

the class MCRIFSCommands method fixDirectorySize.

private static long fixDirectorySize(MCRDirectory directory) {
    long directorySize = 0;
    for (MCRFilesystemNode child : directory.getChildren()) {
        if (child instanceof MCRDirectory) {
            directorySize += fixDirectorySize((MCRDirectory) child);
        } else if (child instanceof MCRFile) {
            MCRFile file = (MCRFile) child;
            directorySize += file.getSize();
        }
    }
    /*
            There is no setSize method on MCRFileSystemNode and there should not be one.
            But in this repair command we need to set the size, so we use reflection.
         */
    try {
        Field privateLongField = MCRFilesystemNode.class.getDeclaredField(MCRFILESYSTEMNODE_SIZE_FIELD_NAME);
        privateLongField.setAccessible(true);
        privateLongField.set(directory, directorySize);
    } catch (NoSuchFieldException e) {
        String message = MessageFormat.format("There is no field named {0} in MCRFileSystemNode!", MCRFILESYSTEMNODE_SIZE_FIELD_NAME);
        throw new MCRException(message, e);
    } catch (IllegalAccessException e) {
        String message = MessageFormat.format("Could not acces filed {0} in {1}!", MCRFILESYSTEMNODE_SIZE_FIELD_NAME, directory.toString());
        throw new MCRException(message, e);
    }
    // now call touch with the old date of MCRFSN to apply the changes to the DB
    GregorianCalendar lastModified = directory.getLastModified();
    FileTime LastModifiedFileTime = FileTime.fromMillis(lastModified.getTimeInMillis());
    try {
        Method touchMethod = MCRFilesystemNode.class.getDeclaredMethod(MCRFILESYSTEMNODE_TOUCH_METHOD_NAME, FileTime.class, boolean.class);
        touchMethod.setAccessible(true);
        touchMethod.invoke(directory, LastModifiedFileTime, false);
    } catch (NoSuchMethodException e) {
        throw new MCRException(MessageFormat.format("There is no {0}-method..", MCRFILESYSTEMNODE_TOUCH_METHOD_NAME));
    } catch (InvocationTargetException | IllegalAccessException e) {
        throw new MCRException(MessageFormat.format("Error while calling {0}-method..", MCRFILESYSTEMNODE_TOUCH_METHOD_NAME));
    }
    LOGGER.info(MessageFormat.format("Changed size of directory {0} to {1} Bytes", directory.getName(), directorySize));
    return directorySize;
}
Also used : MCRFile(org.mycore.datamodel.ifs.MCRFile) MCRException(org.mycore.common.MCRException) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) GregorianCalendar(java.util.GregorianCalendar) FileTime(java.nio.file.attribute.FileTime) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory)

Example 2 with MCRFilesystemNode

use of org.mycore.datamodel.ifs.MCRFilesystemNode in project mycore by MyCoRe-Org.

the class MCRBasicFileAttributeViewImpl method setTimes.

@Override
public void setTimes(FileTime lastModifiedTime, FileTime lastAccessTime, FileTime createTime) throws IOException {
    MCRFilesystemNode node = resolveNode();
    if (node instanceof MCRFile) {
        MCRFile file = (MCRFile) node;
        file.adjustMetadata(lastModifiedTime, file.getMD5(), file.getSize());
        Files.getFileAttributeView(file.getLocalFile().toPath(), BasicFileAttributeView.class).setTimes(lastModifiedTime, lastAccessTime, createTime);
    } else if (node instanceof MCRDirectory) {
        LOGGER.warn("Setting times on directories is not supported: {}", node.toPath());
    }
}
Also used : BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) MCRFile(org.mycore.datamodel.ifs.MCRFile) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory)

Example 3 with MCRFilesystemNode

use of org.mycore.datamodel.ifs.MCRFilesystemNode in project mycore by MyCoRe-Org.

the class MCRDirectoryStream method newDirectoryStream.

@Override
public SecureDirectoryStream<Path> newDirectoryStream(Path path, LinkOption... options) throws IOException {
    checkClosed();
    MCRPath mcrPath = checkFileSystem(path);
    if (mcrPath.isAbsolute()) {
        return (SecureDirectoryStream<Path>) Files.newDirectoryStream(mcrPath);
    }
    MCRFilesystemNode childByPath = dir.getChildByPath(mcrPath.toString());
    if (childByPath == null || childByPath instanceof MCRFile) {
        throw new NoSuchFileException(dir.toString(), path.toString(), "Does not exist or is a file.");
    }
    return new MCRDirectoryStream((MCRDirectory) childByPath, MCRPath.toMCRPath(path.resolve(mcrPath)));
}
Also used : MCRFile(org.mycore.datamodel.ifs.MCRFile) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) NoSuchFileException(java.nio.file.NoSuchFileException) MCRPath(org.mycore.datamodel.niofs.MCRPath) SecureDirectoryStream(java.nio.file.SecureDirectoryStream)

Example 4 with MCRFilesystemNode

use of org.mycore.datamodel.ifs.MCRFilesystemNode in project mycore by MyCoRe-Org.

the class MCRFileSystemProvider method delete.

/* (non-Javadoc)
     * @see java.nio.file.spi.FileSystemProvider#delete(java.nio.file.Path)
     */
@Override
public void delete(Path path) throws IOException {
    MCRPath mcrPath = MCRFileSystemUtils.checkPathAbsolute(path);
    MCRFilesystemNode child = resolvePath(mcrPath);
    if (child instanceof MCRDirectory) {
        if (((MCRDirectory) child).hasChildren()) {
            throw new DirectoryNotEmptyException(mcrPath.toString());
        }
    }
    try {
        child.delete();
    } catch (RuntimeException e) {
        throw new IOException("Could not delete: " + mcrPath, e);
    }
}
Also used : MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 5 with MCRFilesystemNode

use of org.mycore.datamodel.ifs.MCRFilesystemNode 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

MCRFilesystemNode (org.mycore.datamodel.ifs.MCRFilesystemNode)19 MCRDirectory (org.mycore.datamodel.ifs.MCRDirectory)12 MCRPath (org.mycore.datamodel.niofs.MCRPath)11 MCRFile (org.mycore.datamodel.ifs.MCRFile)10 NoSuchFileException (java.nio.file.NoSuchFileException)9 IOException (java.io.IOException)5 NotDirectoryException (java.nio.file.NotDirectoryException)5 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)3 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)2 Path (java.nio.file.Path)2 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)2 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 CopyOption (java.nio.file.CopyOption)1 InvalidPathException (java.nio.file.InvalidPathException)1 LinkOption (java.nio.file.LinkOption)1