package org.eclipse.core.internal.filesystem.local.nio;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.*;
import java.nio.file.attribute.DosFileAttributeView;
import java.nio.file.attribute.DosFileAttributes;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileInfo;
import org.eclipse.core.filesystem.provider.FileInfo;
import org.eclipse.core.internal.filesystem.local.NativeHandler;
public class DosHandler extends NativeHandler {
private static final int ATTRIBUTES = EFS.ATTRIBUTE_SYMLINK | EFS.ATTRIBUTE_LINK_TARGET
| EFS.ATTRIBUTE_ARCHIVE | EFS.ATTRIBUTE_READ_ONLY | EFS.ATTRIBUTE_HIDDEN;
@Override
public FileInfo fetchFileInfo(String fileName) {
FileInfo info = new FileInfo();
try {
Path path = Paths.get(fileName);
Path fileNamePath = path.toRealPath(LinkOption.NOFOLLOW_LINKS).getFileName();
String canonicalName = fileNamePath == null ? "" : fileNamePath.toString();
info.setName(canonicalName);
DosFileAttributes attrs = Files.readAttributes(path, DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
info.setExists(true);
info.setLastModified(attrs.lastModifiedTime().toMillis());
info.setLength(attrs.size());
info.setAttribute(EFS.ATTRIBUTE_ARCHIVE, attrs.isArchive());
info.setAttribute(EFS.ATTRIBUTE_READ_ONLY, attrs.isReadOnly());
info.setAttribute(EFS.ATTRIBUTE_HIDDEN, attrs.isHidden());
if (attrs.isSymbolicLink()) {
info.setDirectory(isDirectoryLink(attrs));
info.setAttribute(EFS.ATTRIBUTE_SYMLINK, true);
info.setStringAttribute(EFS.ATTRIBUTE_LINK_TARGET, Files.readSymbolicLink(path).toString());
} else {
info.setDirectory(attrs.isDirectory());
}
} catch (NoSuchFileException e) {
} catch (IOException e) {
info.setError(IFileInfo.IO_ERROR);
}
return info;
}
private boolean isDirectoryLink(DosFileAttributes attrs) {
try {
Method method = attrs.getClass().getDeclaredMethod("isDirectoryLink");
method.setAccessible(true);
return (Boolean) method.invoke(attrs);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
return false;
}
}
@Override
public int getSupportedAttributes() {
return ATTRIBUTES;
}
@Override
public boolean putFileInfo(String fileName, IFileInfo info, int options) {
Path path = Paths.get(fileName);
DosFileAttributeView view = Files.getFileAttributeView(path, DosFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
try {
view.setArchive(info.getAttribute(EFS.ATTRIBUTE_ARCHIVE));
view.setReadOnly(info.getAttribute(EFS.ATTRIBUTE_READ_ONLY));
view.setHidden(info.getAttribute(EFS.ATTRIBUTE_HIDDEN));
} catch (IOException e) {
return false;
}
return true;
}
}