Search in sources :

Example 1 with MCRFile

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

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

the class MCRIFSCommands method moveFile.

private static String moveFile(String ifsId, MCRContentStore target) throws IOException {
    MCRFile sourceFile = MCRFile.getFile(Objects.requireNonNull(ifsId));
    sourceFile.moveTo(target);
    return sourceFile.getStorageID();
}
Also used : MCRFile(org.mycore.datamodel.ifs.MCRFile)

Example 3 with MCRFile

use of org.mycore.datamodel.ifs.MCRFile 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 4 with MCRFile

use of org.mycore.datamodel.ifs.MCRFile 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 5 with MCRFile

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

MCRFile (org.mycore.datamodel.ifs.MCRFile)16 MCRDirectory (org.mycore.datamodel.ifs.MCRDirectory)11 MCRFilesystemNode (org.mycore.datamodel.ifs.MCRFilesystemNode)11 MCRPath (org.mycore.datamodel.niofs.MCRPath)9 NoSuchFileException (java.nio.file.NoSuchFileException)7 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)5 NotDirectoryException (java.nio.file.NotDirectoryException)4 IOException (java.io.IOException)3 InvalidPathException (java.nio.file.InvalidPathException)3 Path (java.nio.file.Path)3 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)3 File (java.io.File)2 CopyOption (java.nio.file.CopyOption)2 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)2 LinkOption (java.nio.file.LinkOption)2 ProviderMismatchException (java.nio.file.ProviderMismatchException)2 StandardCopyOption (java.nio.file.StandardCopyOption)2 FileTime (java.nio.file.attribute.FileTime)2 HashMap (java.util.HashMap)2 CacheBuilder (com.google.common.cache.CacheBuilder)1