Search in sources :

Example 6 with DirectoryNotEmptyException

use of java.nio.file.DirectoryNotEmptyException 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 7 with DirectoryNotEmptyException

use of java.nio.file.DirectoryNotEmptyException 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 8 with DirectoryNotEmptyException

use of java.nio.file.DirectoryNotEmptyException in project mycore by MyCoRe-Org.

the class MCRIFSFileSystem method removeRoot.

@Override
public void removeRoot(String owner) throws FileSystemException {
    MCRPath rootPath = getPath(owner, "", this);
    MCRDirectory rootDirectory = MCRDirectory.getRootDirectory(owner);
    if (rootDirectory == null) {
        throw new NoSuchFileException(rootPath.toString());
    }
    if (rootDirectory.isDeleted()) {
        return;
    }
    if (rootDirectory.hasChildren()) {
        throw new DirectoryNotEmptyException(rootPath.toString());
    }
    try {
        rootDirectory.delete();
    } catch (RuntimeException e) {
        LogManager.getLogger(getClass()).warn("Catched run time exception while removing root directory.", e);
        throw new FileSystemException(rootPath.toString(), null, e.getMessage());
    }
    LogManager.getLogger(getClass()).info("Removed root directory: {}", rootPath);
}
Also used : FileSystemException(java.nio.file.FileSystemException) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) NoSuchFileException(java.nio.file.NoSuchFileException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 9 with DirectoryNotEmptyException

use of java.nio.file.DirectoryNotEmptyException in project crate by crate.

the class StreamInput method readException.

@SuppressWarnings("unchecked")
public <T extends Exception> T readException() throws IOException {
    if (readBoolean()) {
        int key = readVInt();
        switch(key) {
            case 0:
                final int ord = readVInt();
                if (ord == 59) {
                    final ElasticsearchException ex = new ElasticsearchException(this);
                    final boolean isExecutorShutdown = readBoolean();
                    return (T) new EsRejectedExecutionException(ex.getMessage(), isExecutorShutdown);
                }
                return (T) ElasticsearchException.readException(this, ord);
            case 1:
                String msg1 = readOptionalString();
                String resource1 = readOptionalString();
                return (T) readStackTrace(new CorruptIndexException(msg1, resource1, readException()), this);
            case 2:
                String resource2 = readOptionalString();
                int version2 = readInt();
                int minVersion2 = readInt();
                int maxVersion2 = readInt();
                return (T) readStackTrace(new IndexFormatTooNewException(resource2, version2, minVersion2, maxVersion2), this);
            case 3:
                String resource3 = readOptionalString();
                if (readBoolean()) {
                    int version3 = readInt();
                    int minVersion3 = readInt();
                    int maxVersion3 = readInt();
                    return (T) readStackTrace(new IndexFormatTooOldException(resource3, version3, minVersion3, maxVersion3), this);
                } else {
                    String version3 = readOptionalString();
                    return (T) readStackTrace(new IndexFormatTooOldException(resource3, version3), this);
                }
            case 4:
                return (T) readStackTrace(new NullPointerException(readOptionalString()), this);
            case 5:
                return (T) readStackTrace(new NumberFormatException(readOptionalString()), this);
            case 6:
                return (T) readStackTrace(new IllegalArgumentException(readOptionalString(), readException()), this);
            case 7:
                return (T) readStackTrace(new AlreadyClosedException(readOptionalString(), readException()), this);
            case 8:
                return (T) readStackTrace(new EOFException(readOptionalString()), this);
            case 9:
                return (T) readStackTrace(new SecurityException(readOptionalString(), readException()), this);
            case 10:
                return (T) readStackTrace(new StringIndexOutOfBoundsException(readOptionalString()), this);
            case 11:
                return (T) readStackTrace(new ArrayIndexOutOfBoundsException(readOptionalString()), this);
            case 12:
                return (T) readStackTrace(new FileNotFoundException(readOptionalString()), this);
            case 13:
                final int subclass = readVInt();
                final String file = readOptionalString();
                final String other = readOptionalString();
                final String reason = readOptionalString();
                // skip the msg - it's composed from file, other and reason
                readOptionalString();
                final Exception exception;
                switch(subclass) {
                    case 0:
                        exception = new NoSuchFileException(file, other, reason);
                        break;
                    case 1:
                        exception = new NotDirectoryException(file);
                        break;
                    case 2:
                        exception = new DirectoryNotEmptyException(file);
                        break;
                    case 3:
                        exception = new AtomicMoveNotSupportedException(file, other, reason);
                        break;
                    case 4:
                        exception = new FileAlreadyExistsException(file, other, reason);
                        break;
                    case 5:
                        exception = new AccessDeniedException(file, other, reason);
                        break;
                    case 6:
                        exception = new FileSystemLoopException(file);
                        break;
                    case 7:
                        exception = new FileSystemException(file, other, reason);
                        break;
                    default:
                        throw new IllegalStateException("unknown FileSystemException with index " + subclass);
                }
                return (T) readStackTrace(exception, this);
            case 14:
                return (T) readStackTrace(new IllegalStateException(readOptionalString(), readException()), this);
            case 15:
                return (T) readStackTrace(new LockObtainFailedException(readOptionalString(), readException()), this);
            case 16:
                return (T) readStackTrace(new InterruptedException(readOptionalString()), this);
            case 17:
                return (T) readStackTrace(new IOException(readOptionalString(), readException()), this);
            case 18:
                final boolean isExecutorShutdown = readBoolean();
                return (T) readStackTrace(new EsRejectedExecutionException(readOptionalString(), isExecutorShutdown), this);
            case 19:
                return (T) readStackTrace(new UncheckedIOException(readOptionalString(), readException()), this);
            default:
                throw new IOException("no such exception for id: " + key);
        }
    }
    return null;
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) AccessDeniedException(java.nio.file.AccessDeniedException) FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) UncheckedIOException(java.io.UncheckedIOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) NotDirectoryException(java.nio.file.NotDirectoryException) FileSystemException(java.nio.file.FileSystemException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) EOFException(java.io.EOFException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) GeoPoint(org.elasticsearch.common.geo.GeoPoint) ElasticsearchException(org.elasticsearch.ElasticsearchException) NoSuchFileException(java.nio.file.NoSuchFileException) IndexFormatTooNewException(org.apache.lucene.index.IndexFormatTooNewException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) FileSystemException(java.nio.file.FileSystemException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) UncheckedIOException(java.io.UncheckedIOException) AccessDeniedException(java.nio.file.AccessDeniedException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) FileSystemLoopException(java.nio.file.FileSystemLoopException) NotDirectoryException(java.nio.file.NotDirectoryException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) IndexFormatTooNewException(org.apache.lucene.index.IndexFormatTooNewException) FileSystemLoopException(java.nio.file.FileSystemLoopException)

Example 10 with DirectoryNotEmptyException

use of java.nio.file.DirectoryNotEmptyException in project elasticsearch by elastic.

the class StreamOutput method writeException.

public void writeException(Throwable throwable) throws IOException {
    if (throwable == null) {
        writeBoolean(false);
    } else {
        writeBoolean(true);
        boolean writeCause = true;
        boolean writeMessage = true;
        if (throwable instanceof CorruptIndexException) {
            writeVInt(1);
            writeOptionalString(((CorruptIndexException) throwable).getOriginalMessage());
            writeOptionalString(((CorruptIndexException) throwable).getResourceDescription());
            writeMessage = false;
        } else if (throwable instanceof IndexFormatTooNewException) {
            writeVInt(2);
            writeOptionalString(((IndexFormatTooNewException) throwable).getResourceDescription());
            writeInt(((IndexFormatTooNewException) throwable).getVersion());
            writeInt(((IndexFormatTooNewException) throwable).getMinVersion());
            writeInt(((IndexFormatTooNewException) throwable).getMaxVersion());
            writeMessage = false;
            writeCause = false;
        } else if (throwable instanceof IndexFormatTooOldException) {
            writeVInt(3);
            IndexFormatTooOldException t = (IndexFormatTooOldException) throwable;
            writeOptionalString(t.getResourceDescription());
            if (t.getVersion() == null) {
                writeBoolean(false);
                writeOptionalString(t.getReason());
            } else {
                writeBoolean(true);
                writeInt(t.getVersion());
                writeInt(t.getMinVersion());
                writeInt(t.getMaxVersion());
            }
            writeMessage = false;
            writeCause = false;
        } else if (throwable instanceof NullPointerException) {
            writeVInt(4);
            writeCause = false;
        } else if (throwable instanceof NumberFormatException) {
            writeVInt(5);
            writeCause = false;
        } else if (throwable instanceof IllegalArgumentException) {
            writeVInt(6);
        } else if (throwable instanceof AlreadyClosedException) {
            writeVInt(7);
        } else if (throwable instanceof EOFException) {
            writeVInt(8);
            writeCause = false;
        } else if (throwable instanceof SecurityException) {
            writeVInt(9);
        } else if (throwable instanceof StringIndexOutOfBoundsException) {
            writeVInt(10);
            writeCause = false;
        } else if (throwable instanceof ArrayIndexOutOfBoundsException) {
            writeVInt(11);
            writeCause = false;
        } else if (throwable instanceof FileNotFoundException) {
            writeVInt(12);
            writeCause = false;
        } else if (throwable instanceof FileSystemException) {
            writeVInt(13);
            if (throwable instanceof NoSuchFileException) {
                writeVInt(0);
            } else if (throwable instanceof NotDirectoryException) {
                writeVInt(1);
            } else if (throwable instanceof DirectoryNotEmptyException) {
                writeVInt(2);
            } else if (throwable instanceof AtomicMoveNotSupportedException) {
                writeVInt(3);
            } else if (throwable instanceof FileAlreadyExistsException) {
                writeVInt(4);
            } else if (throwable instanceof AccessDeniedException) {
                writeVInt(5);
            } else if (throwable instanceof FileSystemLoopException) {
                writeVInt(6);
            } else {
                writeVInt(7);
            }
            writeOptionalString(((FileSystemException) throwable).getFile());
            writeOptionalString(((FileSystemException) throwable).getOtherFile());
            writeOptionalString(((FileSystemException) throwable).getReason());
            writeCause = false;
        } else if (throwable instanceof IllegalStateException) {
            writeVInt(14);
        } else if (throwable instanceof LockObtainFailedException) {
            writeVInt(15);
        } else if (throwable instanceof InterruptedException) {
            writeVInt(16);
            writeCause = false;
        } else if (throwable instanceof IOException) {
            writeVInt(17);
        } else {
            ElasticsearchException ex;
            if (throwable instanceof ElasticsearchException && ElasticsearchException.isRegistered(throwable.getClass(), version)) {
                ex = (ElasticsearchException) throwable;
            } else {
                ex = new NotSerializableExceptionWrapper(throwable);
            }
            writeVInt(0);
            writeVInt(ElasticsearchException.getId(ex.getClass()));
            ex.writeTo(this);
            return;
        }
        if (writeMessage) {
            writeOptionalString(throwable.getMessage());
        }
        if (writeCause) {
            writeException(throwable.getCause());
        }
        ElasticsearchException.writeStackTraces(throwable, this);
    }
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) AccessDeniedException(java.nio.file.AccessDeniedException) FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) ElasticsearchException(org.elasticsearch.ElasticsearchException) FileSystemException(java.nio.file.FileSystemException) NotDirectoryException(java.nio.file.NotDirectoryException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) EOFException(java.io.EOFException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) IOException(java.io.IOException) IndexFormatTooNewException(org.apache.lucene.index.IndexFormatTooNewException) FileSystemLoopException(java.nio.file.FileSystemLoopException)

Aggregations

DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)21 NoSuchFileException (java.nio.file.NoSuchFileException)12 IOException (java.io.IOException)10 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)8 Path (java.nio.file.Path)8 AccessDeniedException (java.nio.file.AccessDeniedException)6 AtomicMoveNotSupportedException (java.nio.file.AtomicMoveNotSupportedException)6 NotDirectoryException (java.nio.file.NotDirectoryException)6 FileSystemException (java.nio.file.FileSystemException)5 FileNotFoundException (java.io.FileNotFoundException)4 FileSystemLoopException (java.nio.file.FileSystemLoopException)4 EOFException (java.io.EOFException)3 File (java.io.File)3 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)3 IndexFormatTooNewException (org.apache.lucene.index.IndexFormatTooNewException)3 IndexFormatTooOldException (org.apache.lucene.index.IndexFormatTooOldException)3 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)3 LockObtainFailedException (org.apache.lucene.store.LockObtainFailedException)3 MCRDirectory (org.mycore.datamodel.ifs.MCRDirectory)3 MCRPath (org.mycore.datamodel.niofs.MCRPath)3