use of org.mycore.datamodel.ifs.MCRFilesystemNode 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.MCRFilesystemNode 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.ifs.MCRFilesystemNode 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.MCRFilesystemNode in project mycore by MyCoRe-Org.
the class MCRFileSystemProvider method getFileStore.
/* (non-Javadoc)
* @see java.nio.file.spi.FileSystemProvider#getFileStore(java.nio.file.Path)
*/
@Override
public FileStore getFileStore(Path path) throws IOException {
MCRPath mcrPath = MCRFileSystemUtils.checkPathAbsolute(path);
if (mcrPath.getNameCount() > 0) {
MCRFilesystemNode node = resolvePath(mcrPath);
if (node instanceof MCRFile) {
MCRFile file = (MCRFile) node;
String storeID = file.getStoreID();
return MCRFileStore.getInstance(storeID);
}
}
return MCRFileStore.getInstance(MCRContentStoreFactory.getDefaultStore().getID());
}
use of org.mycore.datamodel.ifs.MCRFilesystemNode in project mycore by MyCoRe-Org.
the class MCRFileSystemUtils method getMCRFile.
static MCRFile getMCRFile(MCRDirectory baseDir, MCRPath relativePath, boolean create, boolean createNew) throws IOException {
MCRPath ifsPath = relativePath;
if (relativePath.isAbsolute()) {
if (baseDir.getOwnerID().equals(relativePath.getOwner())) {
ifsPath = baseDir.toPath().relativize(relativePath);
} else
throw new IOException(relativePath + " is absolute does not fit to " + baseDir.toPath());
}
Deque<MCRFilesystemNode> created = new LinkedList<>();
MCRFile file;
try {
file = (MCRFile) baseDir.getChildByPath(ifsPath.toString());
if (file != null && createNew) {
throw new FileAlreadyExistsException(baseDir.toPath().resolve(ifsPath).toString());
}
if (file == null & (create || createNew)) {
Path normalized = ifsPath.normalize();
MCRDirectory parent = baseDir;
int nameCount = normalized.getNameCount();
int directoryCount = nameCount - 1;
int i = 0;
while (i < directoryCount) {
String curName = normalized.getName(i).toString();
MCRDirectory curDir = (MCRDirectory) parent.getChild(curName);
if (curDir == null) {
curDir = new MCRDirectory(curName, parent);
created.addFirst(curDir);
}
i++;
parent = curDir;
}
String fileName = normalized.getFileName().toString();
file = new MCRFile(fileName, parent);
// false -> no event handler
file.setContentFrom(new ByteArrayInputStream(new byte[0]), false);
created.addFirst(file);
}
} catch (Exception e) {
if (create || createNew) {
LOGGER.error("Exception while getting MCRFile {}. Removing created filesystem nodes.", ifsPath);
while (created.peekFirst() != null) {
MCRFilesystemNode node = created.pollFirst();
try {
node.delete();
} catch (Exception de) {
LOGGER.fatal("Error while deleting file system node: {}", node.getName(), de);
}
}
}
throw e;
}
return file;
}
Aggregations