com.maverick.sshd.fs
Class AbstractFileSystem<T extends AbstractFile,K extends FileSystemMount>

java.lang.Object
  extended by com.maverick.sshd.fs.AbstractFileSystem<T,K>
All Implemented Interfaces:
FileSystem<K>

public class AbstractFileSystem<T extends AbstractFile,K extends FileSystemMount>
extends Object
implements FileSystem<K>

Author:
Lee David Painter

Field Summary
protected  AbstractFileFactory<T,K> fileFactory
           
protected  Map<String,com.maverick.sshd.fs.AbstractFileSystem.OpenDirectory> openDirectories
           
protected  Map<String,com.maverick.sshd.fs.AbstractFileSystem.OpenFile> openFiles
           
 
Fields inherited from interface com.maverick.sshd.sftp.FileSystem
AUTHORIZED_KEYS_STORE, OPEN_APPEND, OPEN_CREATE, OPEN_EXCLUSIVE, OPEN_READ, OPEN_TRUNCATE, OPEN_WRITE, SCP, SFTP, SSH
 
Constructor Summary
AbstractFileSystem(AbstractFileFactory<T,K> fileFactory, Connection con, String protocolInUse)
           
 
Method Summary
 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(String link, String target)
          Create a symbolic link.
 boolean fileExists(String path)
          Determine whether a file exists and return the result
 String getDefaultPath()
          Get the default path for the current session.
 SftpFileAttributes getFileAttributes(byte[] handle)
          Get the attributes for a given file handle.
 SftpFileAttributes getFileAttributes(String path)
          Get the attributes for a given file.
 AbstractFileFactory<T,K> getFileFactory()
           
 MountManager<K> getMountManager()
           
 String getRealPath(String path)
          Get the real path for a given path.
 boolean makeDirectory(String path, SftpFileAttributes attrs)
          Create a directory.
 byte[] openDirectory(String path)
          Open a directory for reading and allocate an open file handle.
 byte[] openFile(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(String path)
          Read the target location of a symbolic link.
 void removeDirectory(String path)
          Remove an empty directory from the file system.
 void removeFile(String path)
          Remove a file.
 void renameFile(String oldpath, String newpath)
          Rename a file.
 AbstractFile resolveFile(String path)
           
 void setFileAttributes(byte[] handle, SftpFileAttributes attrs)
          Set the open files attributes
 void setFileAttributes(String path, SftpFileAttributes attrs)
          Set the files attributes.
 boolean supportsMounting()
           
 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
 

Field Detail

openFiles

protected Map<String,com.maverick.sshd.fs.AbstractFileSystem.OpenFile> openFiles

openDirectories

protected Map<String,com.maverick.sshd.fs.AbstractFileSystem.OpenDirectory> openDirectories

fileFactory

protected AbstractFileFactory<T extends AbstractFile,K extends FileSystemMount> fileFactory
Constructor Detail

AbstractFileSystem

public AbstractFileSystem(AbstractFileFactory<T,K> fileFactory,
                          Connection con,
                          String protocolInUse)
Method Detail

resolveFile

public AbstractFile resolveFile(String path)
                         throws PermissionDeniedException,
                                IOException
Throws:
PermissionDeniedException
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<K extends FileSystemMount>

makeDirectory

public boolean makeDirectory(String path,
                             SftpFileAttributes attrs)
                      throws PermissionDeniedException,
                             FileNotFoundException,
                             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<K extends FileSystemMount>
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.
FileNotFoundException - if the parent directory does not exist.
IOException

getFileAttributes

public SftpFileAttributes getFileAttributes(byte[] handle)
                                     throws IOException,
                                            InvalidHandleException,
                                            PermissionDeniedException
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<K extends FileSystemMount>
Parameters:
handle - byte[]
Returns:
SftpFileAttributes
Throws:
IOException
InvalidHandleException
PermissionDeniedException

getFileAttributes

public SftpFileAttributes getFileAttributes(String path)
                                     throws IOException,
                                            FileNotFoundException,
                                            PermissionDeniedException
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<K extends FileSystemMount>
Parameters:
path - String the absolute path to the file
Returns:
SftpFileAttributes
Throws:
IOException
FileNotFoundException - if the file cannot be found
PermissionDeniedException - if the user does not have access to this file
See Also:
com.maverick.sftp.SftpFileAttributes

openDirectory

public byte[] openDirectory(String path)
                     throws PermissionDeniedException,
                            FileNotFoundException,
                            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<K extends FileSystemMount>
Parameters:
path - String
Returns:
byte[]
Throws:
PermissionDeniedException
FileNotFoundException
IOException

readDirectory

public SftpFile[] readDirectory(byte[] handle)
                         throws InvalidHandleException,
                                EOFException,
                                IOException,
                                PermissionDeniedException
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<K extends FileSystemMount>
Parameters:
handle - byte[]
Returns:
SftpFile[] an array of SftpFile objects.
Throws:
InvalidHandleException - the handle supplied is invalid.
EOFException - thrown once all the files contained with the directory have been read.
IOException - an error occurred
PermissionDeniedException

openFile

public byte[] openFile(String path,
                       com.maverick.util.UnsignedInteger32 flags,
                       SftpFileAttributes attrs)
                throws PermissionDeniedException,
                       FileNotFoundException,
                       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<K extends FileSystemMount>
Parameters:
path - String
flags - UnsignedInteger32
attrs - SftpFileAttributes
Returns:
byte[]
Throws:
PermissionDeniedException
FileNotFoundException
IOException

readFile

public int readFile(byte[] handle,
                    com.maverick.util.UnsignedInteger64 offset,
                    byte[] buf,
                    int start,
                    int numBytesToRead)
             throws InvalidHandleException,
                    EOFException,
                    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<K extends FileSystemMount>
Parameters:
handle - byte[]
offset - UnsignedInteger64
buf - byte[]
start - int
numBytesToRead - int
Returns:
int
Throws:
InvalidHandleException
EOFException
IOException

writeFile

public void writeFile(byte[] handle,
                      com.maverick.util.UnsignedInteger64 offset,
                      byte[] data,
                      int off,
                      int len)
               throws InvalidHandleException,
                      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<K extends FileSystemMount>
Parameters:
handle - byte[]
offset - UnsignedInteger64
data - byte[]
off - int
len - int
Throws:
InvalidHandleException
IOException

closeFile

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

Specified by:
closeFile in interface FileSystem<K extends FileSystemMount>
Parameters:
handle - byte[]
Throws:
InvalidHandleException
IOException

closeFile

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

removeFile

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

Specified by:
removeFile in interface FileSystem<K extends FileSystemMount>
Parameters:
path - String
Throws:
PermissionDeniedException
IOException
FileNotFoundException

renameFile

public void renameFile(String oldpath,
                       String newpath)
                throws PermissionDeniedException,
                       FileNotFoundException,
                       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<K extends FileSystemMount>
Parameters:
oldpath - String
newpath - String
Throws:
PermissionDeniedException
FileNotFoundException
IOException

getDefaultPath

public String getDefaultPath()
                      throws IOException,
                             PermissionDeniedException
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<K extends FileSystemMount>
Returns:
String the absolute path to the default path.
Throws:
FileNotFoundException - if the users default path does not exist.
IOException
PermissionDeniedException

removeDirectory

public void removeDirectory(String path)
                     throws PermissionDeniedException,
                            FileNotFoundException,
                            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<K extends FileSystemMount>
Parameters:
path - String
Throws:
PermissionDeniedException
FileNotFoundException
IOException

setFileAttributes

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

Specified by:
setFileAttributes in interface FileSystem<K extends FileSystemMount>
Parameters:
path - String
attrs - SftpFileAttributes
Throws:
PermissionDeniedException
IOException
FileNotFoundException

setFileAttributes

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

Specified by:
setFileAttributes in interface FileSystem<K extends FileSystemMount>
Parameters:
handle - byte[]
attrs - SftpFileAttributes
Throws:
PermissionDeniedException
IOException
InvalidHandleException

readSymbolicLink

public SftpFile readSymbolicLink(String path)
                          throws UnsupportedFileOperationException,
                                 FileNotFoundException,
                                 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<K extends FileSystemMount>
Parameters:
path - String
Returns:
SftpFile
Throws:
UnsupportedFileOperationException
FileNotFoundException
IOException
PermissionDeniedException

createSymbolicLink

public void createSymbolicLink(String link,
                               String target)
                        throws UnsupportedFileOperationException,
                               FileNotFoundException,
                               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<K extends FileSystemMount>
Parameters:
link - String
target - String
Throws:
UnsupportedFileOperationException
FileNotFoundException
IOException
PermissionDeniedException

fileExists

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

Specified by:
fileExists in interface FileSystem<K extends FileSystemMount>
Parameters:
path - String
Returns:
boolean true if the file exists, otherwise false.
Throws:
IOException
PermissionDeniedException

getRealPath

public String getRealPath(String path)
                   throws IOException,
                          FileNotFoundException,
                          PermissionDeniedException
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<K extends FileSystemMount>
Parameters:
path - String the path to canonicalize.
Returns:
String the absolute path to the file.
Throws:
IOException - if an unrecoverable error occurs.
FileNotFoundException - if the path cannot be found.
PermissionDeniedException

supportsMounting

public boolean supportsMounting()
Specified by:
supportsMounting in interface FileSystem<K extends FileSystemMount>

getMountManager

public MountManager<K> getMountManager()
                                                        throws IOException,
                                                               PermissionDeniedException
Specified by:
getMountManager in interface FileSystem<K extends FileSystemMount>
Throws:
IOException
PermissionDeniedException

getFileFactory

public AbstractFileFactory<T,K> getFileFactory()


Copyright © 2012. All Rights Reserved.