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