Search in sources :

Example 21 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath 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 22 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath 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)

Example 23 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRFileSystemProvider method getParentDirectory.

private static MCRDirectory getParentDirectory(MCRPath mcrPath) throws NoSuchFileException, NotDirectoryException {
    if (mcrPath.getNameCount() == 0) {
        throw new IllegalArgumentException("Root component has no parent: " + mcrPath);
    }
    MCRDirectory rootDirectory = getRootDirectory(mcrPath);
    if (mcrPath.getNameCount() == 1) {
        return rootDirectory;
    }
    MCRPath parentPath = mcrPath.getParent();
    MCRFilesystemNode parentNode = rootDirectory.getChildByPath(getAbsolutePathFromRootComponent(parentPath).toString());
    if (parentNode == null) {
        throw new NoSuchFileException(rootDirectory.toPath().toString(), getAbsolutePathFromRootComponent(mcrPath).toString(), "Parent directory does not exists.");
    }
    if (parentNode instanceof MCRFile) {
        throw new NotDirectoryException(parentNode.toPath().toString());
    }
    MCRDirectory parentDir = (MCRDirectory) parentNode;
    // warm-up cache
    parentDir.getChildren();
    return parentDir;
}
Also used : MCRFile(org.mycore.datamodel.ifs.MCRFile) NotDirectoryException(java.nio.file.NotDirectoryException) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) NoSuchFileException(java.nio.file.NoSuchFileException) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 24 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRFileSystemProvider method readAttributes.

/* (non-Javadoc)
     * @see java.nio.file.spi.FileSystemProvider#readAttributes(java.nio.file.Path, java.lang.Class, java.nio.file.LinkOption[])
     */
@SuppressWarnings("unchecked")
@Override
public <A extends BasicFileAttributes> A readAttributes(Path path, Class<A> type, LinkOption... options) throws IOException {
    MCRPath mcrPath = MCRFileSystemUtils.checkPathAbsolute(path);
    MCRFilesystemNode node = resolvePath(mcrPath);
    // must support BasicFileAttributeView
    if (type == BasicFileAttributes.class || type == MCRFileAttributes.class) {
        return (A) MCRBasicFileAttributeViewImpl.readAttributes(node);
    }
    return null;
}
Also used : MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) MCRFileAttributes(org.mycore.datamodel.niofs.MCRFileAttributes) MCRPath(org.mycore.datamodel.niofs.MCRPath) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 25 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRFileSystemProvider method checkAccess.

/* (non-Javadoc)
     * @see java.nio.file.spi.FileSystemProvider#checkAccess(java.nio.file.Path, java.nio.file.AccessMode[])
     */
@Override
public void checkAccess(Path path, AccessMode... modes) throws IOException {
    MCRPath mcrPath = MCRFileSystemUtils.checkPathAbsolute(path);
    MCRFilesystemNode node = resolvePath(mcrPath);
    if (node == null) {
        throw new NoSuchFileException(mcrPath.toString());
    }
    if (node instanceof MCRDirectory) {
        checkDirectory((MCRDirectory) node, modes);
    } else {
        checkFile((MCRFile) node, modes);
    }
}
Also used : MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) NoSuchFileException(java.nio.file.NoSuchFileException) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Aggregations

MCRPath (org.mycore.datamodel.niofs.MCRPath)96 IOException (java.io.IOException)49 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)26 Path (java.nio.file.Path)25 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)22 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)22 Document (org.jdom2.Document)15 JDOMException (org.jdom2.JDOMException)15 MCRPersistenceException (org.mycore.common.MCRPersistenceException)14 MCRException (org.mycore.common.MCRException)13 MCRDirectory (org.mycore.datamodel.ifs.MCRDirectory)13 MCRAccessException (org.mycore.access.MCRAccessException)12 Files (java.nio.file.Files)11 Collectors (java.util.stream.Collectors)11 LogManager (org.apache.logging.log4j.LogManager)11 Logger (org.apache.logging.log4j.Logger)11 FileVisitResult (java.nio.file.FileVisitResult)10 NoSuchFileException (java.nio.file.NoSuchFileException)10 Date (java.util.Date)10 List (java.util.List)10