#include <ne7ssh_sftp.h>
Classes | |
struct | sftpFile |
struct | sftpFileAttrs |
Public Member Functions | |
Ne7sshSftp (ne7ssh_session *_session, ne7ssh_channel *_channel) | |
~Ne7sshSftp () | |
bool | init () |
void | setTimeout (uint32 _timeout) |
uint32 | openFile (const char *filename, uint8 shortMode) |
uint32 | openDir (const char *dirname) |
bool | readFile (uint32 fileID, uint64 offset=0) |
bool | writeFile (uint32 fileID, const uint8 *data, uint32 len, uint64 offset=0) |
bool | closeFile (uint32 fileID) |
bool | getFileAttrs (Ne7SftpSubsystem::fileAttrs &attributes, const char *remoteFile, bool followSymLinks=true) |
bool | isFile (const char *remoteFile) |
bool | isDir (const char *remoteFile) |
bool | get (const char *remoteFile, FILE *localFile) |
bool | put (FILE *localFile, const char *remoteFile) |
bool | rm (const char *remoteFile) |
bool | mv (const char *oldFile, const char *newFile) |
bool | mkdir (const char *remoteDir) |
bool | rmdir (const char *remoteDir) |
const char * | ls (const char *remoteDir, bool longNames=false) |
bool | cd (const char *remoteDir) |
bool | chmod (const char *remoteFile, const char *mode) |
bool | chown (const char *remoteFile, uint32 uid, uint32 gid=0) |
Private Types | |
enum | writeMode { READ, OVERWRITE, APPEND } |
Private Member Functions | |
bool | handleData (Botan::SecureVector< Botan::byte > &packet) |
bool | handleVersion (Botan::SecureVector< Botan::byte > &packet) |
bool | handleStatus (Botan::SecureVector< Botan::byte > &packet) |
bool | addOpenHandle (Botan::SecureVector< Botan::byte > &packet) |
bool | handleSftpData (Botan::SecureVector< Botan::byte > &packet) |
bool | handleNames (Botan::SecureVector< Botan::byte > &packet) |
sftpFile * | getFileHandle (uint32 fileID) |
bool | receiveUntil (short _cmd, uint32 timeSec=0) |
bool | receiveWhile (short _cmd, uint32 timeSec=0) |
bool | processAttrs (Botan::SecureVector< Botan::byte > &packet) |
bool | getFileStats (const char *remoteFile, bool followSymLinks=true) |
bool | getFileAttrs (sftpFileAttrs &attributes, Botan::SecureVector< Botan::byte > &remoteFile, bool followSymLinks=true) |
bool | getFStat (uint32 fileID) |
uint64 | getFileSize (uint32 fileID) |
bool | receiveWindowAdjust () |
ne7ssh_string | getFullPath (const char *filename) |
bool | isType (const char *remoteFile, uint32 type) |
Private Attributes | |
ne7ssh_session * | session |
uint32 | timeout |
uint32 | seq |
uint8 | sftpCmd |
ne7ssh_string | commBuffer |
Botan::SecureVector< Botan::byte > | fileBuffer |
uint8 | lastError |
char * | currentPath |
sftpFileAttrs | attrs |
sftpFile ** | sftpFiles |
uint16 | sftpFilesCount |
Ne7sshSftp::Ne7sshSftp | ( | ne7ssh_session * | _session, | |
ne7ssh_channel * | _channel | |||
) |
Constructor.
_session | Pointer to connections session data. | |
_channel | Pointer to the ne7ssh_channel instance, taken from the new ne7ssh_connection instance. |
References ne7ssh_channel::getRecvWindow(), and ne7ssh_channel::getSendWindow().
Ne7sshSftp::~Ne7sshSftp | ( | ) |
Default destructor.
bool Ne7sshSftp::addOpenHandle | ( | Botan::SecureVector< Botan::byte > & | packet | ) | [private] |
Method to add a new file to sftpFiles variable from the HANDLE packet.
packet | HANDLE packet. |
References ne7ssh_string::getInt(), and ne7ssh_string::getString().
Referenced by handleData().
bool Ne7sshSftp::cd | ( | const char * | remoteDir | ) |
This method is used to change the current working directory.
remoteDir | Full or relative path to the new working directory on the remote server. |
References ne7ssh::errors(), getFullPath(), ne7ssh_session::getSendChannel(), ne7ssh_session::getSshChannel(), ne7ssh_string::length(), Ne7sshError::push(), receiveWhile(), and ne7ssh_string::value().
Referenced by Ne7SftpSubsystem::cd().
bool Ne7sshSftp::chmod | ( | const char * | remoteFile, | |
const char * | mode | |||
) |
This method is used for changing the permissions associated with a remote file.
remoteFile | Full or relative path to the remote file. | |
mode | Mode string. It can be wither a numerical mode expression such as "755" or an expression showing the modifications to be made, such as "ug+w". Mode string is the same as used by *nix chmod command. |
References ne7ssh::errors(), getFileAttrs(), getFullPath(), ne7ssh_session::getSendChannel(), ne7ssh_session::getSshChannel(), ne7ssh_string::length(), Ne7sshError::push(), receiveWhile(), and ne7ssh_string::value().
Referenced by Ne7SftpSubsystem::chmod().
bool Ne7sshSftp::chown | ( | const char * | remoteFile, | |
uint32 | uid, | |||
uint32 | gid = 0 | |||
) |
This method is used to change the owner of a remote file.
remoteFile | Full or relative path to the remote file. | |
uid | Numerical new owner user ID. | |
gid | Numerical new owner group ID. |
References ne7ssh::errors(), getFileAttrs(), getFullPath(), ne7ssh_session::getSendChannel(), ne7ssh_session::getSshChannel(), ne7ssh_string::length(), Ne7sshError::push(), receiveWhile(), and ne7ssh_string::value().
Referenced by Ne7SftpSubsystem::chown().
bool Ne7sshSftp::closeFile | ( | uint32 | fileID | ) |
Low level method used to close a file opened using openFile() method.
fileID | File ID returned by openFile() method. |
References ne7ssh::errors(), getFileHandle(), ne7ssh_session::getSendChannel(), ne7ssh_session::getSshChannel(), Ne7sshError::push(), and receiveUntil().
Referenced by Ne7SftpSubsystem::closeFile(), get(), ls(), and put().
bool Ne7sshSftp::get | ( | const char * | remoteFile, | |
FILE * | localFile | |||
) |
This method is used to retrieve a remote file and dump it into local file.
remoteFile | Full or relative path to the file on the remote side. | |
localFile | Pointer to the FILE structure. If the file being retrieved is binary, use "w+" attributes in fopen function. |
References closeFile(), ne7ssh::errors(), getFileSize(), ne7ssh_session::getSshChannel(), openFile(), Ne7sshError::push(), and readFile().
Referenced by Ne7SftpSubsystem::get().
bool Ne7sshSftp::getFileAttrs | ( | Ne7SftpSubsystem::fileAttrs & | attributes, | |
const char * | remoteFile, | |||
bool | followSymLinks = true | |||
) |
This method is used to retrieve remote file attributes and place them into fileAttrs structure.
attributes | Reference to fileAttrs structure where retrieved attributes should be placed. | |
remoteFile | Name of the remote file. | |
followSymLinks | If this variable is set to true, symbolic links will be followed. That is the default befavour. If this behavour is undesired, pass "false". |
References ne7ssh::errors(), getFileStats(), getFullPath(), ne7ssh_session::getSshChannel(), ne7ssh_string::length(), Ne7sshError::push(), and ne7ssh_string::value().
bool Ne7sshSftp::getFileAttrs | ( | sftpFileAttrs & | attributes, | |
Botan::SecureVector< Botan::byte > & | remoteFile, | |||
bool | followSymLinks = true | |||
) | [private] |
Gets attributes of a remote file and dumps them into sfptFileAtts structure.
attributes | reference to sftpFileAttrs structure where the result will be stored. | |
remoteFile | Full or relative path to a remote file. | |
followSymLinks | If set to true symbolic links will be followed. That is the default behavior. If following symbolic links is undesired set to "false". |
References ne7ssh::errors(), getFileStats(), ne7ssh_session::getSshChannel(), and Ne7sshError::push().
Referenced by chmod(), chown(), and Ne7SftpSubsystem::getFileAttrs().
Ne7sshSftp::sftpFile * Ne7sshSftp::getFileHandle | ( | uint32 | fileID | ) | [private] |
This method is used to get a pointer to currently open file stored in sftpFile structure.
fileID | File ID, received from fileOpen() method. |
References ne7ssh::errors(), ne7ssh_session::getSshChannel(), and Ne7sshError::push().
Referenced by closeFile(), getFStat(), ls(), readFile(), and writeFile().
uint64 Ne7sshSftp::getFileSize | ( | uint32 | fileID | ) | [private] |
This methods returnes the size of an open file.
fileID | File ID, returned by the openFile() method. |
References ne7ssh::errors(), getFStat(), ne7ssh_session::getSshChannel(), and Ne7sshError::push().
Referenced by get().
bool Ne7sshSftp::getFileStats | ( | const char * | remoteFile, | |
bool | followSymLinks = true | |||
) | [private] |
Low level method to request file attributes.
remoteFile | Full or relative path to a remote file. | |
followSymLinks | If set to true symbolic links will be followed. That is the default behavior. If following symbolic links is undesired set to "false". |
References ne7ssh::errors(), getFullPath(), ne7ssh_session::getSendChannel(), ne7ssh_session::getSshChannel(), ne7ssh_string::length(), Ne7sshError::push(), receiveWhile(), and ne7ssh_string::value().
Referenced by getFileAttrs(), and isType().
bool Ne7sshSftp::getFStat | ( | uint32 | fileID | ) | [private] |
Works like getFileStats() method, except that it operates on a handle of already opened file instead of path.
fileID | File ID, returned by the openFile() method. |
References ne7ssh::errors(), getFileHandle(), ne7ssh_session::getSendChannel(), ne7ssh_session::getSshChannel(), Ne7sshError::push(), and receiveWhile().
Referenced by getFileSize().
ne7ssh_string Ne7sshSftp::getFullPath | ( | const char * | filename | ) | [private] |
Returns full path to a file or directory.
filename | Relative path to a remote file or directory. |
Referenced by cd(), chmod(), chown(), getFileAttrs(), getFileStats(), isType(), mkdir(), mv(), openDir(), openFile(), rm(), and rmdir().
bool Ne7sshSftp::handleData | ( | Botan::SecureVector< Botan::byte > & | packet | ) | [private, virtual] |
Replacement for ne7ssh_channel handleData method. Processes SFTP specific packets.
packet | Reference to the newly received packet. |
Reimplemented from ne7ssh_channel.
References addOpenHandle(), ne7ssh_string::addVector(), ne7ssh_channel::adjustRecvWindow(), ne7ssh_string::clear(), ne7ssh::errors(), ne7ssh_string::getByte(), ne7ssh_string::getInt(), ne7ssh_session::getSshChannel(), ne7ssh_string::getString(), handleNames(), handleSftpData(), handleStatus(), handleVersion(), ne7ssh_string::length(), processAttrs(), Ne7sshError::push(), and ne7ssh_string::value().
bool Ne7sshSftp::handleNames | ( | Botan::SecureVector< Botan::byte > & | packet | ) | [private] |
Method to process NAME packets.
packet | NAME packet. |
References ne7ssh_string::addInt(), ne7ssh_string::addVectorField(), ne7ssh_string::getInt(), Ne7sshSftpPacket::getInt64(), ne7ssh_string::getString(), and ne7ssh_string::value().
Referenced by handleData().
bool Ne7sshSftp::handleSftpData | ( | Botan::SecureVector< Botan::byte > & | packet | ) | [private] |
Method to process DATA packets.
packet | DATA packet. |
References ne7ssh_string::clear(), ne7ssh::errors(), ne7ssh_string::getInt(), ne7ssh_session::getSshChannel(), ne7ssh_string::getString(), and Ne7sshError::push().
Referenced by handleData().
bool Ne7sshSftp::handleStatus | ( | Botan::SecureVector< Botan::byte > & | packet | ) | [private] |
Processes the STATUS packet received from the server.
packet | STATUS packet. |
References ne7ssh::errors(), ne7ssh_string::getInt(), ne7ssh_session::getSshChannel(), ne7ssh_string::getString(), and Ne7sshError::push().
Referenced by handleData().
bool Ne7sshSftp::handleVersion | ( | Botan::SecureVector< Botan::byte > & | packet | ) | [private] |
Processes the VERSION packet received from the server.
packet | VERSION packet. |
References ne7ssh::errors(), ne7ssh_string::getInt(), ne7ssh_session::getSshChannel(), and Ne7sshError::push().
Referenced by handleData().
bool Ne7sshSftp::init | ( | ) |
Initializes SFTP subsystem.
References ne7ssh_string::addChar(), ne7ssh_string::addInt(), ne7ssh_string::addString(), ne7ssh_string::clear(), ne7ssh_session::getSendChannel(), receiveUntil(), ne7ssh_transport::sendPacket(), and ne7ssh_string::value().
Referenced by ne7ssh_connection::startSftp().
bool Ne7sshSftp::isDir | ( | const char * | remoteFile | ) |
This method is used to determine if a remote inode is a directory.
remoteFile | Full or relative path to the remote file. |
References isType().
Referenced by Ne7SftpSubsystem::isDir().
bool Ne7sshSftp::isFile | ( | const char * | remoteFile | ) |
This method is used to determine if a remote inode is a regular file.
remoteFile | Full or relative path to the remote inode. |
References isType().
Referenced by Ne7SftpSubsystem::isFile().
bool Ne7sshSftp::isType | ( | const char * | remoteFile, | |
uint32 | type | |||
) | [private] |
Determines the type of a remote file.
remoteFile | Relative or full path to the remote file. | |
type | Type, taken from sys/stat.h. |
References ne7ssh::errors(), getFileStats(), getFullPath(), ne7ssh_session::getSshChannel(), ne7ssh_string::length(), Ne7sshError::push(), and ne7ssh_string::value().
const char * Ne7sshSftp::ls | ( | const char * | remoteDir, | |
bool | longNames = false | |||
) |
This methods is used retrieve a listing of a remote directory.
remoteDir | Full or relative path to a directory. | |
longNames | If set to "true" the returned string in addition to file strings will contain attributes for each file. |
References ne7ssh_string::addVector(), ne7ssh_string::clear(), closeFile(), ne7ssh::errors(), getFileHandle(), ne7ssh_session::getSendChannel(), ne7ssh_session::getSshChannel(), openDir(), Ne7sshError::push(), receiveWhile(), and ne7ssh_string::value().
Referenced by Ne7SftpSubsystem::ls().
bool Ne7sshSftp::mkdir | ( | const char * | remoteDir | ) |
This method is used to create a new directory.
remoteDir | Full or relative path to a new directory on the remote server. |
References ne7ssh::errors(), getFullPath(), ne7ssh_session::getSendChannel(), ne7ssh_session::getSshChannel(), ne7ssh_string::length(), Ne7sshError::push(), receiveWhile(), and ne7ssh_string::value().
Referenced by Ne7SftpSubsystem::mkdir().
bool Ne7sshSftp::mv | ( | const char * | oldFile, | |
const char * | newFile | |||
) |
This method is used to rename/move files.
oldFile | Full or relative path to an old file on the remote server. | |
newFile | Full or relative path to a new file on the remote side. |
References ne7ssh::errors(), getFullPath(), ne7ssh_session::getSendChannel(), ne7ssh_session::getSshChannel(), ne7ssh_string::length(), Ne7sshError::push(), receiveWhile(), and ne7ssh_string::value().
Referenced by Ne7SftpSubsystem::mv().
uint32 Ne7sshSftp::openDir | ( | const char * | dirname | ) |
Low level method used to open an inode containing file entries a.k.a directory.
dirname | Relative or full path to the inode. |
References ne7ssh::errors(), getFullPath(), ne7ssh_session::getSendChannel(), ne7ssh_session::getSshChannel(), ne7ssh_string::length(), Ne7sshError::push(), receiveUntil(), and ne7ssh_string::value().
Referenced by ls(), and Ne7SftpSubsystem::openDir().
uint32 Ne7sshSftp::openFile | ( | const char * | filename, | |
uint8 | shortMode | |||
) |
Low level method used to open a remote file.
filename | Relative or full path to the file. | |
shortMode | Mode to be used when opening the file. Can be one of the modes defined by writeMode class variable. |
References ne7ssh::errors(), getFullPath(), ne7ssh_session::getSendChannel(), ne7ssh_session::getSshChannel(), ne7ssh_string::length(), Ne7sshError::push(), receiveUntil(), and ne7ssh_string::value().
Referenced by get(), Ne7SftpSubsystem::openFile(), and put().
bool Ne7sshSftp::processAttrs | ( | Botan::SecureVector< Botan::byte > & | packet | ) | [private] |
Method to process ATTRS packet.
packet | ATTRS packet. |
References ne7ssh_string::getInt(), and Ne7sshSftpPacket::getInt64().
Referenced by handleData().
bool Ne7sshSftp::put | ( | FILE * | localFile, | |
const char * | remoteFile | |||
) |
This method is used to upload a file to a remote server.
localFile | Pointer to the FILE structure. If the file being retrieved is binary, use "w+" attributes in fopen function. | |
remoteFile | Full or relative path to the file on the remote side. |
References closeFile(), ne7ssh::errors(), ne7ssh_session::getSshChannel(), openFile(), Ne7sshError::push(), and writeFile().
Referenced by Ne7SftpSubsystem::put().
bool Ne7sshSftp::readFile | ( | uint32 | fileID, | |
uint64 | offset = 0 | |||
) |
Low level method used to read datablock up to the size of SFTP_MAX_MSG_SIZE from a file.
fileID | File ID retruned by openFile() method. | |
offset | Offset. 0 by default. |
References ne7ssh::errors(), getFileHandle(), ne7ssh_session::getSendChannel(), ne7ssh_session::getSshChannel(), Ne7sshError::push(), and receiveWhile().
Referenced by get(), and Ne7SftpSubsystem::readFile().
bool Ne7sshSftp::receiveUntil | ( | short | _cmd, | |
uint32 | timeSec = 0 | |||
) | [private] |
Receive packets until specific SFTP subsystem command is received.
_cmd | SFTP command to wait for. | |
timeSec | Timeout in seconds. |
References ne7ssh_string::clear(), ne7ssh_transport::getPacket(), ne7ssh_channel::handleReceived(), ne7ssh_string::length(), and ne7ssh_transport::waitForPacket().
Referenced by closeFile(), init(), openDir(), openFile(), and writeFile().
bool Ne7sshSftp::receiveWhile | ( | short | _cmd, | |
uint32 | timeSec = 0 | |||
) | [private] |
Receive packets while SFTP subsystem commands received matches specified command.
_cmd | Command to receive. | |
timeSec | Timeout in seconds. |
References ne7ssh_string::clear(), ne7ssh_transport::getPacket(), ne7ssh_channel::handleReceived(), ne7ssh_string::length(), and ne7ssh_transport::waitForPacket().
Referenced by cd(), chmod(), chown(), getFileStats(), getFStat(), ls(), mkdir(), mv(), readFile(), rm(), and rmdir().
bool Ne7sshSftp::receiveWindowAdjust | ( | ) | [private] |
This method is used to wait for an ADJUST_WINDOW packet, when the send window size is zero.
References ne7ssh::errors(), ne7ssh_transport::getPacket(), ne7ssh_session::getSshChannel(), ne7ssh_channel::handleReceived(), Ne7sshError::push(), and ne7ssh_transport::waitForPacket().
Referenced by writeFile().
bool Ne7sshSftp::rm | ( | const char * | remoteFile | ) |
This method is used to remove a file on a remote server.
remoteFile | Full or relative path to the file on the remote side. |
References ne7ssh::errors(), getFullPath(), ne7ssh_session::getSendChannel(), ne7ssh_session::getSshChannel(), ne7ssh_string::length(), Ne7sshError::push(), receiveWhile(), and ne7ssh_string::value().
Referenced by Ne7SftpSubsystem::rm().
bool Ne7sshSftp::rmdir | ( | const char * | remoteDir | ) |
This method is used to remove a remote directory.
remoteDir | Full or relative path to a directory to be removed. |
References ne7ssh::errors(), getFullPath(), ne7ssh_session::getSendChannel(), ne7ssh_session::getSshChannel(), ne7ssh_string::length(), Ne7sshError::push(), receiveWhile(), and ne7ssh_string::value().
Referenced by Ne7SftpSubsystem::rmdir().
void Ne7sshSftp::setTimeout | ( | uint32 | _timeout | ) | [inline] |
This method is used to set a timeout for all SFTP subsystem communications.
_timeout | Timeout in seconds. |
Referenced by Ne7SftpSubsystem::setTimeout().
bool Ne7sshSftp::writeFile | ( | uint32 | fileID, | |
const uint8 * | data, | |||
uint32 | len, | |||
uint64 | offset = 0 | |||
) |
Low level method used to write data-block up to the size of SFTP_MAX_MSG_SIZE to a remote file.
fileID | File ID returned by openFile() method. | |
data | Pointer to a buffer containing the data. | |
len | Length of the block. | |
offset | Offset in the remote file. If offset is passed EOF the space between EOF and offset will be filled by 0x0. Variable is set to 0 by default. |
References ne7ssh::errors(), getFileHandle(), ne7ssh_session::getSendChannel(), ne7ssh_session::getSshChannel(), Ne7sshError::push(), receiveUntil(), and receiveWindowAdjust().
Referenced by put(), and Ne7SftpSubsystem::writeFile().