com.maverick.sshd.vfs
Class VirtualFileSystem

java.lang.Object
  extended by com.maverick.sshd.vfs.VirtualFileSystem
All Implemented Interfaces:
FileSystem

public class VirtualFileSystem
extends java.lang.Object
implements FileSystem

Provides a platform independent file system that uses standard java file operations. The virtual file system operates on a set of 'mounts' that map a virtual directory to a physical location on the server.

To configure the file system use the following code in your com.maverick.sshd.SshDaemon#configure(ConfigurationContext) implementation:

 context.setFileSystemProvider(VirtualFileSystem.class);
 

Mounts can be created by setting system properties and only one property is required "com.maverick.sshd.vfs.VFSRoot". This instructs the server where to look for the root of the file system "/". For example if you wanted to provide access to the root of a windows machine you could set the following property

 System.setProperty("com.maverick.sshd.vfs.VFSRoot", "C:\\");
 

Additional mounts can be set using "com.maverick.sshd.vfs.VFSMount.1" "com.maverick.sshd.vfs.VFSMount.2" and so forth. These mounts must start with a "/" and also have a descriptive name. For example to set the virtual path "/documents" you could use the following property:

 System.setProperty("com.maverick.sshd.vfs.VFSMount.1",
                "/documents=C:\\My Documents");
 

The setting of attributes is NOT supported within the virtual file system and by default users will have access to all files and folders. You can optionally implement your own VFSPermissionHandler that will enable you to restrict access to files based on user/group permissions. To configure the file system with your handler simply set the "com.maverick.sshd.vfs.VFSPermissionHandler" system property to the fully qualified class name of your class. This will be installed whenever an instance of the VirtualFileSystem is created.

The file system also maps the /home/USERNAME to the users home directory.

Author:
Lee David Painter

Field Summary
 
Fields inherited from interface com.maverick.sshd.platform.FileSystem
AUTHORIZED_KEYS_STORE, OPEN_APPEND, OPEN_CREATE, OPEN_EXCLUSIVE, OPEN_READ, OPEN_TRUNCATE, OPEN_WRITE, SCP, SFTP, SSH
 
Constructor Summary
VirtualFileSystem()
           
 
Method Summary
protected  void addMount(java.lang.String vfspath, java.lang.String nfspath)
           
 void closeFile(byte[] handle)
          Close an open file or directory.
 boolean closeFile(byte[] handle, boolean remove)
           
 void closeFilesystem()
          The filesystem instance is being closed and no further use of the instance is required.
 void createSymbolicLink(java.lang.String link, java.lang.String target)
          Create a symbolic link.
 boolean fileExists(java.lang.String path)
          Determine whether a file exists and return the result
 java.lang.String getDefaultPath()
          Get the default path for the current session.
 SftpFileAttributes getFileAttributes(byte[] handle)
          Get the attributes for a given file handle.
 SftpFileAttributes getFileAttributes(java.lang.String path)
          Get the attributes for a given file.
 java.lang.String getRealPath(java.lang.String path)
          Get the real path for a given path.
 void init(Connection con, java.lang.String protocolInUse)
           
 boolean makeDirectory(java.lang.String path)
          Create a directory.
 byte[] openDirectory(java.lang.String path)
          Open a directory for reading and allocate an open file handle.
 byte[] openFile(java.lang.String path, com.maverick.util.UnsignedInteger32 flags, SftpFileAttributes attrs)
          Open a file for reading/writing and allocate an open file handle.
 SftpFile[] readDirectory(byte[] handle)
           Read the contents of a directory.
 int readFile(byte[] handle, com.maverick.util.UnsignedInteger64 offset, byte[] buf, int start, int numBytesToRead)
          Read a block of data from an open file.
 SftpFile readSymbolicLink(java.lang.String path)
          Read the target location of a symbolic link.
 void removeDirectory(java.lang.String path)
          Remove an empty directory from the file system.
 void removeFile(java.lang.String path)
          Remove a file.
 void renameFile(java.lang.String oldpath, java.lang.String newpath)
          Rename a file.
 void setFileAttributes(byte[] handle, SftpFileAttributes attrs)
          Set the open files attributes
 void setFileAttributes(java.lang.String path, SftpFileAttributes attrs)
          Set the files attributes.
protected  java.lang.String translateCanonicalPath(java.lang.String path, java.lang.String securemount)
           
 java.lang.String translateNFSPath(java.lang.String nfspath)
           
 java.lang.String translateVFSPath(java.lang.String vfspath)
           
 void writeFile(byte[] handle, com.maverick.util.UnsignedInteger64 offset, byte[] data, int off, int len)
          Write a block of data to an open file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

VirtualFileSystem

public VirtualFileSystem()
Method Detail

init

public void init(Connection con,
                 java.lang.String protocolInUse)
          throws java.io.IOException
Throws:
java.io.IOException

addMount

protected void addMount(java.lang.String vfspath,
                        java.lang.String nfspath)
                 throws java.io.IOException
Throws:
java.io.IOException

closeFilesystem

public void closeFilesystem()
Description copied from interface: FileSystem
The filesystem instance is being closed and no further use of the instance is required.

Specified by:
closeFilesystem in interface FileSystem

translateNFSPath

public java.lang.String translateNFSPath(java.lang.String nfspath)
                                  throws java.io.IOException,
                                         java.io.FileNotFoundException
Throws:
java.io.IOException
java.io.FileNotFoundException

translateVFSPath

public java.lang.String translateVFSPath(java.lang.String vfspath)
                                  throws java.io.IOException,
                                         java.io.FileNotFoundException
Throws:
java.io.IOException
java.io.FileNotFoundException

translateCanonicalPath

protected java.lang.String translateCanonicalPath(java.lang.String path,
                                                  java.lang.String securemount)
                                           throws java.io.FileNotFoundException,
                                                  java.io.IOException
Throws:
java.io.FileNotFoundException
java.io.IOException

makeDirectory

public boolean makeDirectory(java.lang.String path)
                      throws PermissionDeniedException,
                             java.io.FileNotFoundException,
                             java.io.IOException
Description copied from interface: FileSystem
Create a directory. A false value should be returned if the file or directory with the specified path already exists.

Specified by:
makeDirectory in interface FileSystem
Parameters:
path - String the absolute path to the new directory.
Returns:
boolean true if the directory was created, false if the directory already exists.
Throws:
PermissionDeniedException - if the user cannot perform this operation.
java.io.FileNotFoundException - if the parent directory does not exist.
java.io.IOException

getFileAttributes

public SftpFileAttributes getFileAttributes(byte[] handle)
                                     throws java.io.IOException,
                                            InvalidHandleException
Description copied from interface: FileSystem
Get the attributes for a given file handle. This requires that you build a file attributes object based upon all the information that you can determine from the file whose open handle has been provided.

Specified by:
getFileAttributes in interface FileSystem
Parameters:
handle - byte[]
Returns:
SftpFileAttributes
Throws:
java.io.IOException
InvalidHandleException

getFileAttributes

public SftpFileAttributes getFileAttributes(java.lang.String path)
                                     throws java.io.IOException,
                                            java.io.FileNotFoundException
Description copied from interface: FileSystem
Get the attributes for a given file. This requires that you build a file attributes object based upon all the information that you can determine.

Specified by:
getFileAttributes in interface FileSystem
Parameters:
path - String the absolute path to the file
Returns:
SftpFileAttributes
Throws:
java.io.IOException
java.io.FileNotFoundException - if the file cannot be found
See Also:
com.maverick.sftp.SftpFileAttributes

openDirectory

public byte[] openDirectory(java.lang.String path)
                     throws PermissionDeniedException,
                            java.io.FileNotFoundException,
                            java.io.IOException
Description copied from interface: FileSystem
Open a directory for reading and allocate an open file handle. Open file handles can be whatever you require but the length of the handle string MUST NOT exceed 256 data bytes. Subsequent calls to readDirectory and closeFile will provide the handle returned from this method.

Specified by:
openDirectory in interface FileSystem
Parameters:
path - String
Returns:
byte[]
Throws:
PermissionDeniedException
java.io.FileNotFoundException
java.io.IOException

readDirectory

public SftpFile[] readDirectory(byte[] handle)
                         throws InvalidHandleException,
                                java.io.EOFException,
                                java.io.IOException
Description copied from interface: FileSystem

Read the contents of a directory. Each call to this method should return one or more file objects with full file attributes for each file. The client will call this method repeatedly until an EOFException is thrown indicating that there are no more files to read from the directory. If there are no files to list then the EOFException should be thrown upon first call.

IMPORTANT: Each SftpFile object should be initialized with the relative path of the file AND NOT the absolute path.

Specified by:
readDirectory in interface FileSystem
Parameters:
handle - byte[]
Returns:
SftpFile[] an array of SftpFile objects.
Throws:
InvalidHandleException - the handle supplied is invalid.
java.io.EOFException - thrown once all the files contained with the directory have been read.
java.io.IOException - an error occurred

openFile

public byte[] openFile(java.lang.String path,
                       com.maverick.util.UnsignedInteger32 flags,
                       SftpFileAttributes attrs)
                throws PermissionDeniedException,
                       java.io.FileNotFoundException,
                       java.io.IOException
Description copied from interface: FileSystem
Open a file for reading/writing and allocate an open file handle. Open file handles can be whatever you require but the length of the handle string MUST NOT exceed 256 data bytes. Subsequent calls to readDirectory and closeFile will provide the handle returned from this method. The method specifes one or more file open flags which determine which mode the file should be opened in.

Specified by:
openFile in interface FileSystem
Parameters:
path - String
flags - UnsignedInteger32
attrs - SftpFileAttributes
Returns:
byte[]
Throws:
PermissionDeniedException
java.io.FileNotFoundException
java.io.IOException

readFile

public int readFile(byte[] handle,
                    com.maverick.util.UnsignedInteger64 offset,
                    byte[] buf,
                    int start,
                    int numBytesToRead)
             throws InvalidHandleException,
                    java.io.EOFException,
                    java.io.IOException
Description copied from interface: FileSystem
Read a block of data from an open file. You should read from the file offset provided and write the data into the buffer array from the start position and return no more data than is requested by numBytesToRead; return the actual number of bytes read in the return value and if the file offset specified is passed EOF you should throw an EOFException.

Specified by:
readFile in interface FileSystem
Parameters:
handle - byte[]
offset - UnsignedInteger64
buf - byte[]
start - int
numBytesToRead - int
Returns:
int
Throws:
InvalidHandleException
java.io.EOFException
java.io.IOException

writeFile

public void writeFile(byte[] handle,
                      com.maverick.util.UnsignedInteger64 offset,
                      byte[] data,
                      int off,
                      int len)
               throws InvalidHandleException,
                      java.io.IOException
Description copied from interface: FileSystem
Write a block of data to an open file. You should write the data from the offset for as many bytes as are supplied.

Specified by:
writeFile in interface FileSystem
Parameters:
handle - byte[]
offset - UnsignedInteger64
data - byte[]
off - int
len - int
Throws:
InvalidHandleException
java.io.IOException

closeFile

public void closeFile(byte[] handle)
               throws InvalidHandleException,
                      java.io.IOException
Description copied from interface: FileSystem
Close an open file or directory.

Specified by:
closeFile in interface FileSystem
Parameters:
handle - byte[]
Throws:
InvalidHandleException
java.io.IOException

closeFile

public boolean closeFile(byte[] handle,
                         boolean remove)
                  throws InvalidHandleException,
                         java.io.IOException
Throws:
InvalidHandleException
java.io.IOException

removeFile

public void removeFile(java.lang.String path)
                throws PermissionDeniedException,
                       java.io.IOException,
                       java.io.FileNotFoundException
Description copied from interface: FileSystem
Remove a file.

Specified by:
removeFile in interface FileSystem
Parameters:
path - String
Throws:
PermissionDeniedException
java.io.IOException
java.io.FileNotFoundException

renameFile

public void renameFile(java.lang.String oldpath,
                       java.lang.String newpath)
                throws PermissionDeniedException,
                       java.io.FileNotFoundException,
                       java.io.IOException
Description copied from interface: FileSystem
Rename a file. It is an error if there already exists a file with the name specified by newpath.

Specified by:
renameFile in interface FileSystem
Parameters:
oldpath - String
newpath - String
Throws:
PermissionDeniedException
java.io.FileNotFoundException
java.io.IOException

getDefaultPath

public java.lang.String getDefaultPath()
Description copied from interface: FileSystem
Get the default path for the current session. The default path is normally the users home directory.

Specified by:
getDefaultPath in interface FileSystem
Returns:
String the absolute path to the default path.

removeDirectory

public void removeDirectory(java.lang.String path)
                     throws PermissionDeniedException,
                            java.io.FileNotFoundException,
                            java.io.IOException
Description copied from interface: FileSystem
Remove an empty directory from the file system. This method should fail if the directory has any file system objects as children.

Specified by:
removeDirectory in interface FileSystem
Parameters:
path - String
Throws:
PermissionDeniedException
java.io.FileNotFoundException
java.io.IOException

setFileAttributes

public void setFileAttributes(java.lang.String path,
                              SftpFileAttributes attrs)
                       throws PermissionDeniedException,
                              java.io.IOException,
                              java.io.FileNotFoundException
Description copied from interface: FileSystem
Set the files attributes.

Specified by:
setFileAttributes in interface FileSystem
Parameters:
path - String
attrs - SftpFileAttributes
Throws:
PermissionDeniedException
java.io.IOException
java.io.FileNotFoundException

setFileAttributes

public void setFileAttributes(byte[] handle,
                              SftpFileAttributes attrs)
                       throws PermissionDeniedException,
                              java.io.IOException,
                              InvalidHandleException
Description copied from interface: FileSystem
Set the open files attributes

Specified by:
setFileAttributes in interface FileSystem
Parameters:
handle - byte[]
attrs - SftpFileAttributes
Throws:
PermissionDeniedException
java.io.IOException
InvalidHandleException

readSymbolicLink

public SftpFile readSymbolicLink(java.lang.String path)
                          throws UnsupportedFileOperationException,
                                 java.io.FileNotFoundException,
                                 java.io.IOException,
                                 PermissionDeniedException
Description copied from interface: FileSystem
Read the target location of a symbolic link. If the file system does not support links then an UnsupportedOperationException should be thrown. Otherwise the full path to the target of the link should be returned.

Specified by:
readSymbolicLink in interface FileSystem
Parameters:
path - String
Returns:
SftpFile
Throws:
UnsupportedFileOperationException
java.io.FileNotFoundException
java.io.IOException
PermissionDeniedException

createSymbolicLink

public void createSymbolicLink(java.lang.String link,
                               java.lang.String target)
                        throws UnsupportedFileOperationException,
                               java.io.FileNotFoundException,
                               java.io.IOException,
                               PermissionDeniedException
Description copied from interface: FileSystem
Create a symbolic link. If the file system does not support the creation of links then an UnsupportedOperationException should be thrown.

Specified by:
createSymbolicLink in interface FileSystem
Parameters:
link - String
target - String
Throws:
UnsupportedFileOperationException
java.io.FileNotFoundException
java.io.IOException
PermissionDeniedException

fileExists

public boolean fileExists(java.lang.String path)
                   throws java.io.IOException
Description copied from interface: FileSystem
Determine whether a file exists and return the result

Specified by:
fileExists in interface FileSystem
Parameters:
path - String
Returns:
boolean true if the file exists, otherwise false.
Throws:
java.io.IOException

getRealPath

public java.lang.String getRealPath(java.lang.String path)
                             throws java.io.IOException,
                                    java.io.FileNotFoundException
Description copied from interface: FileSystem
Get the real path for a given path. This requires that the path be processed and a full path be returned, for example '/usr/../var' would be returned as '/var'. This should also expand relative paths using the users home directory as a base.

Specified by:
getRealPath in interface FileSystem
Parameters:
path - String the path to canonicalize.
Returns:
String the absolute path to the file.
Throws:
java.io.IOException - if an unrecoverable error occurs.
java.io.FileNotFoundException - if the path cannot be found.


Copyright © 2003-2008 SSHTools LTD. All Rights Reserved.