Search in sources :

Example 6 with MCRDirectory

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

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

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

Example 9 with MCRDirectory

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

the class MCRFileSystemProvider method move.

/* (non-Javadoc)
     * @see java.nio.file.spi.FileSystemProvider#move(java.nio.file.Path, java.nio.file.Path, java.nio.file.CopyOption[])
     */
@Override
public void move(Path source, Path target, CopyOption... options) throws IOException {
    HashSet<CopyOption> copyOptions = Sets.newHashSet(options);
    if (copyOptions.contains(StandardCopyOption.ATOMIC_MOVE)) {
        throw new AtomicMoveNotSupportedException(source.toString(), target.toString(), "ATOMIC_MOVE not supported yet");
    }
    if (Files.isDirectory(source)) {
        MCRPath src = MCRFileSystemUtils.checkPathAbsolute(source);
        MCRDirectory srcRootDirectory = getRootDirectory(src);
        if (srcRootDirectory.hasChildren()) {
            throw new IOException("Directory is not empty");
        }
    }
    copy(source, target, options);
    delete(source);
}
Also used : CopyOption(java.nio.file.CopyOption) StandardCopyOption(java.nio.file.StandardCopyOption) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 10 with MCRDirectory

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

the class MCRFileSystemUtils method getMCRFile.

static MCRFile getMCRFile(MCRPath ifsPath, boolean create, boolean createNew) throws IOException {
    if (!ifsPath.isAbsolute()) {
        throw new IllegalArgumentException("'path' needs to be absolute.");
    }
    MCRFile file;
    MCRDirectory root = null;
    boolean rootCreated = false;
    try {
        try {
            root = MCRFileSystemProvider.getRootDirectory(ifsPath);
        } catch (NoSuchFileException e) {
            if (create || createNew) {
                root = new MCRDirectory(ifsPath.getOwner());
                rootCreated = true;
            } else {
                throw e;
            }
        }
        MCRPath relativePath = root.toPath().relativize(ifsPath);
        file = getMCRFile(root, relativePath, create, createNew);
    } catch (Exception e) {
        if (rootCreated) {
            LOGGER.error("Exception while getting MCRFile {}. Removing created filesystem nodes.", ifsPath);
            try {
                root.delete();
            } catch (Exception de) {
                LOGGER.fatal("Error while deleting file system node: {}", root.getName(), de);
            }
        }
        throw e;
    }
    return file;
}
Also used : MCRFile(org.mycore.datamodel.ifs.MCRFile) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) NoSuchFileException(java.nio.file.NoSuchFileException) MCRPath(org.mycore.datamodel.niofs.MCRPath) NoSuchFileException(java.nio.file.NoSuchFileException) ProviderMismatchException(java.nio.file.ProviderMismatchException) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) InvalidPathException(java.nio.file.InvalidPathException)

Aggregations

MCRDirectory (org.mycore.datamodel.ifs.MCRDirectory)21 MCRFilesystemNode (org.mycore.datamodel.ifs.MCRFilesystemNode)13 MCRPath (org.mycore.datamodel.niofs.MCRPath)13 MCRFile (org.mycore.datamodel.ifs.MCRFile)10 NoSuchFileException (java.nio.file.NoSuchFileException)9 IOException (java.io.IOException)7 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)5 NotDirectoryException (java.nio.file.NotDirectoryException)4 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)3 Path (java.nio.file.Path)3 CopyOption (java.nio.file.CopyOption)2 FileSystemException (java.nio.file.FileSystemException)2 InvalidPathException (java.nio.file.InvalidPathException)2 ProviderMismatchException (java.nio.file.ProviderMismatchException)2 StandardCopyOption (java.nio.file.StandardCopyOption)2 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)2 GregorianCalendar (java.util.GregorianCalendar)2 MCRException (org.mycore.common.MCRException)2 MCRPersistenceException (org.mycore.common.MCRPersistenceException)2 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)2