use of org.mycore.datamodel.ifs.MCRFilesystemNode in project mycore by MyCoRe-Org.
the class MCRIFSCommands method fixDirectorySize.
private static long fixDirectorySize(MCRDirectory directory) {
long directorySize = 0;
for (MCRFilesystemNode child : directory.getChildren()) {
if (child instanceof MCRDirectory) {
directorySize += fixDirectorySize((MCRDirectory) child);
} else if (child instanceof MCRFile) {
MCRFile file = (MCRFile) child;
directorySize += file.getSize();
}
}
/*
There is no setSize method on MCRFileSystemNode and there should not be one.
But in this repair command we need to set the size, so we use reflection.
*/
try {
Field privateLongField = MCRFilesystemNode.class.getDeclaredField(MCRFILESYSTEMNODE_SIZE_FIELD_NAME);
privateLongField.setAccessible(true);
privateLongField.set(directory, directorySize);
} catch (NoSuchFieldException e) {
String message = MessageFormat.format("There is no field named {0} in MCRFileSystemNode!", MCRFILESYSTEMNODE_SIZE_FIELD_NAME);
throw new MCRException(message, e);
} catch (IllegalAccessException e) {
String message = MessageFormat.format("Could not acces filed {0} in {1}!", MCRFILESYSTEMNODE_SIZE_FIELD_NAME, directory.toString());
throw new MCRException(message, e);
}
// now call touch with the old date of MCRFSN to apply the changes to the DB
GregorianCalendar lastModified = directory.getLastModified();
FileTime LastModifiedFileTime = FileTime.fromMillis(lastModified.getTimeInMillis());
try {
Method touchMethod = MCRFilesystemNode.class.getDeclaredMethod(MCRFILESYSTEMNODE_TOUCH_METHOD_NAME, FileTime.class, boolean.class);
touchMethod.setAccessible(true);
touchMethod.invoke(directory, LastModifiedFileTime, false);
} catch (NoSuchMethodException e) {
throw new MCRException(MessageFormat.format("There is no {0}-method..", MCRFILESYSTEMNODE_TOUCH_METHOD_NAME));
} catch (InvocationTargetException | IllegalAccessException e) {
throw new MCRException(MessageFormat.format("Error while calling {0}-method..", MCRFILESYSTEMNODE_TOUCH_METHOD_NAME));
}
LOGGER.info(MessageFormat.format("Changed size of directory {0} to {1} Bytes", directory.getName(), directorySize));
return directorySize;
}
use of org.mycore.datamodel.ifs.MCRFilesystemNode in project mycore by MyCoRe-Org.
the class MCRBasicFileAttributeViewImpl method setTimes.
@Override
public void setTimes(FileTime lastModifiedTime, FileTime lastAccessTime, FileTime createTime) throws IOException {
MCRFilesystemNode node = resolveNode();
if (node instanceof MCRFile) {
MCRFile file = (MCRFile) node;
file.adjustMetadata(lastModifiedTime, file.getMD5(), file.getSize());
Files.getFileAttributeView(file.getLocalFile().toPath(), BasicFileAttributeView.class).setTimes(lastModifiedTime, lastAccessTime, createTime);
} else if (node instanceof MCRDirectory) {
LOGGER.warn("Setting times on directories is not supported: {}", node.toPath());
}
}
use of org.mycore.datamodel.ifs.MCRFilesystemNode in project mycore by MyCoRe-Org.
the class MCRDirectoryStream method newDirectoryStream.
@Override
public SecureDirectoryStream<Path> newDirectoryStream(Path path, LinkOption... options) throws IOException {
checkClosed();
MCRPath mcrPath = checkFileSystem(path);
if (mcrPath.isAbsolute()) {
return (SecureDirectoryStream<Path>) Files.newDirectoryStream(mcrPath);
}
MCRFilesystemNode childByPath = dir.getChildByPath(mcrPath.toString());
if (childByPath == null || childByPath instanceof MCRFile) {
throw new NoSuchFileException(dir.toString(), path.toString(), "Does not exist or is a file.");
}
return new MCRDirectoryStream((MCRDirectory) childByPath, MCRPath.toMCRPath(path.resolve(mcrPath)));
}
use of org.mycore.datamodel.ifs.MCRFilesystemNode 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.ifs.MCRFilesystemNode 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
}
}
}
Aggregations