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