package de.tudarmstadt.informatik.hostage.protocol.cifs; //import org.alfresco.config.ConfigElement; import org.alfresco.jlan.server.NetworkServer; import org.alfresco.jlan.server.SrvSession; import org.alfresco.jlan.server.core.DeviceContext; import org.alfresco.jlan.server.core.DeviceContextException; import org.alfresco.jlan.server.filesys.DiskDeviceContext; import org.alfresco.jlan.server.filesys.DiskInterface; import org.alfresco.jlan.server.filesys.FileAttribute; import org.alfresco.jlan.server.filesys.FileExistsException; import org.alfresco.jlan.server.filesys.FileInfo; import org.alfresco.jlan.server.filesys.FileName; import org.alfresco.jlan.server.filesys.FileOpenParams; import org.alfresco.jlan.server.filesys.FileStatus; import org.alfresco.jlan.server.filesys.FileSystem; import org.alfresco.jlan.server.filesys.NetworkFile; import org.alfresco.jlan.server.filesys.SearchContext; import org.alfresco.jlan.server.filesys.TreeConnection; import org.alfresco.jlan.smb.server.disk.JavaNetworkFile; import org.springframework.extensions.config.ConfigElement; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; public class PseudoCIFSDiskDriver implements DiskInterface { private TreeConnection SMBTree; private static class FileNode { public String name; public int type; public int parent; public FileNode(String name, int type, int parent){ this.name = name; this.type = type; this.parent = parent; } } private FileNode[] filesystem = new FileNode[]{ new FileNode("/", FileAttribute.Directory, 0), new FileNode("/testDir", FileAttribute.Directory, "/".hashCode()), new FileNode("/testDir2", FileAttribute.Directory, "/".hashCode()), new FileNode("/muhahaha.txt", 0, "/".hashCode()) }; private static class PseudoNetworkFile extends NetworkFile { public PseudoNetworkFile(String name){ super(name); setFileSize(12); setModifyDate(System.currentTimeMillis()); setCreationDate(System.currentTimeMillis()); } @Override public void openFile(boolean createFlag) throws IOException { setClosed(false); } @Override public int readFile(byte[] buf, int len, int pos, long fileOff) throws IOException { return 0; } @Override public void writeFile(byte[] buf, int len, int pos, long fileOff) throws IOException { } @Override public long seekFile(long pos, int typ) throws IOException { return 0; } @Override public void flushFile() throws IOException { } @Override public void truncateFile(long siz) throws IOException { } @Override public void closeFile() throws IOException { setClosed(true); } } @Override public void closeFile(SrvSession srvSession, TreeConnection treeConnection, NetworkFile networkFile) throws IOException { networkFile.closeFile(); } @Override public void createDirectory(SrvSession srvSession, TreeConnection treeConnection, FileOpenParams fileOpenParams) throws IOException { } @Override public NetworkFile createFile(SrvSession srvSession, TreeConnection treeConnection, FileOpenParams fileOpenParams) throws IOException { throw new FileExistsException(); } @Override public void deleteDirectory(SrvSession srvSession, TreeConnection treeConnection, String s) throws IOException { } @Override public void deleteFile(SrvSession srvSession, TreeConnection treeConnection, String s) throws IOException { } @Override public int fileExists(SrvSession srvSession, TreeConnection treeConnection, String s) { DeviceContext ctx = treeConnection.getContext(); String filename = FileName.buildPath(ctx.getDeviceName(), s, null, File.separatorChar); FileNode fileNode = getFileNode(filename); if(fileNode == null) return FileStatus.NotExist; if(fileNode.type == FileAttribute.Directory){ return FileStatus.DirectoryExists; } else { return FileStatus.FileExists; } } @Override public void flushFile(SrvSession srvSession, TreeConnection treeConnection, NetworkFile networkFile) throws IOException { } private FileNode getFileNode(String name){ for(FileNode node: this.filesystem){ if(node.name.equals(name)) return node; } return null; } @Override public FileInfo getFileInformation(SrvSession srvSession, TreeConnection treeConnection, String name) throws IOException { //String path = FileName.buildPath(treeConnection.getContext().getDeviceName(), "/", "jlan", java.io.File.separatorChar); //File file = new File(treeConnection.getContext().getDeviceName(), "jlan"); //int fattr = FileAttribute.ReadOnly; String path = FileName.buildPath(treeConnection.getContext().getDeviceName(), name, null, java.io.File.separatorChar); FileNode fileNode = getFileNode(path); if(fileNode == null){ return null; } System.out.println("PATH::::"+path); int fattr = fileNode.type + FileAttribute.ReadOnly; FileInfo finfo = new FileInfo(path, 0, fattr); long fdate = System.currentTimeMillis(); finfo.setModifyDateTime(fdate); finfo.setCreationDateTime(fdate - 1000); finfo.setChangeDateTime(fdate); finfo.setDirectoryId(fileNode.parent); finfo.setFileId(fileNode.name.hashCode()); return finfo; } @Override public boolean isReadOnly(SrvSession srvSession, DeviceContext deviceContext) throws IOException { return true; } @Override public NetworkFile openFile(SrvSession srvSession, TreeConnection treeConnection, FileOpenParams fileOpenParams) throws IOException { System.out.println("openFile method::::"+srvSession + " - " + treeConnection + " - " + fileOpenParams); String fname = FileName.buildPath(treeConnection.getContext().getDeviceName(), fileOpenParams.getPath(), null, java.io.File.separatorChar); FileNode fileNode = getFileNode(fname); if(fileNode == null) return null; String[] path = fileNode.name.split("" + java.io.File.separatorChar); NetworkFile netFile = new PseudoNetworkFile(path.length == 0 ? "/" : path[path.length - 1]); netFile.setFullName(fileNode.name); netFile.setDirectoryId(fileNode.parent); netFile.setFileId(fileNode.name.hashCode()); netFile.setAttributes(fileNode.type); netFile.setGrantedAccess(NetworkFile.READONLY); netFile.setAttributes(FileAttribute.ReadOnly); return netFile; } @Override public int readFile(SrvSession srvSession, TreeConnection treeConnection, NetworkFile networkFile, byte[] bytes, int i, int i2, long l) throws IOException { System.out.println("readFile method::::"+srvSession + " - " + treeConnection + " - "+networkFile); return 0; } @Override public void renameFile(SrvSession srvSession, TreeConnection treeConnection, String s, String s2) throws IOException { } @Override public long seekFile(SrvSession srvSession, TreeConnection treeConnection, NetworkFile networkFile, long l, int i) throws IOException { System.out.println("seekFile method::::"+srvSession + " - " + treeConnection + " - "+networkFile); return 0; } @Override public void setFileInformation(SrvSession srvSession, TreeConnection treeConnection, String s, FileInfo fileInfo) throws IOException { } @Override public SearchContext startSearch(SrvSession srvSession, TreeConnection treeConnection, String s, int i) throws FileNotFoundException { System.out.println("Start search!!!"); throw new FileNotFoundException(); } @Override public void truncateFile(SrvSession srvSession, TreeConnection treeConnection, NetworkFile networkFile, long l) throws IOException { } @Override public int writeFile(SrvSession srvSession, TreeConnection treeConnection, NetworkFile networkFile, byte[] bytes, int i, int i2, long l) throws IOException { return 0; } @Override public DeviceContext createContext(String s, ConfigElement configElement) throws DeviceContextException { DiskDeviceContext ctx = new DiskDeviceContext("/"); ctx.setFilesystemAttributes(FileSystem.CasePreservedNames + FileSystem.UnicodeOnDisk); ctx.setAvailable(true); return ctx; } @Override public void treeOpened(SrvSession srvSession, TreeConnection treeConnection) { System.out.println("treeOpened method::::"+srvSession + " - " + treeConnection); System.out.println("treeOpened method::::"+treeConnection.getContext().getDeviceName()); } @Override public void treeClosed(SrvSession srvSession, TreeConnection treeConnection) { } }