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);
}
}
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
}
}
}
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);
}
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;
}
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);
}
}
Aggregations