# Ne7sshSftp Class Reference

`#include <ne7ssh_sftp.h>`

Inheritance diagram for Ne7sshSftp:

[legend]
Collaboration diagram for Ne7sshSftp:

[legend]

List of all members.

## 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

## Detailed Description

Author:
  Andrew Useckas <andrew@netsieben.com>

## Constructor & Destructor Documentation

Ne7sshSftp::Ne7sshSftp | ( | ne7ssh_session * | _session,
|  | ne7ssh_channel * | _channel |
| ) |  |  |

Constructor.

Parameters:

  | _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.

## Member Function Documentation

bool Ne7sshSftp::addOpenHandle | ( | Botan::SecureVector< Botan::byte > & | packet | ) | `[private]`

Method to add a new file to sftpFiles variable from the HANDLE packet.

Parameters:

  | packet | HANDLE packet.

Returns:
  True if processing successful, otherwise false.

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.

Parameters:

  | remoteDir | Full or relative path to the new working directory on the remote server.

Returns:
  True if change of directory succedded. False on any error.

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.

Parameters:

  | 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.

Returns:
  True if the new permissions are succesfully applied to the remote file. False on any error.

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.

Parameters:

  | remoteFile | Full or relative path to the remote file.
  | uid | Numerical new owner user ID.
  | gid | Numerical new owner group ID.

Returns:
  True if the change of ownership succeeds. False on any error.

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.

Parameters:

  | fileID | File ID returned by openFile() method.

Returns:
  True on success. False on any error.

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.

Parameters:

  | 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.

Returns:
  True if getting the file is succeeds. False on any error.

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.

Parameters:

  | 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".

Returns:
  True if the attributes successfully retrieved. Otherwise false is returned.

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.

Parameters:

  | 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".

Returns:
  True if the attributes successfully received. False on any error.

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.

Parameters:

  | fileID | File ID, received from fileOpen() method.

Returns:
  Returns a pointer to sftpFile structyure containing opened file or directory. If file specified by fileID has not been opened, NULL is returned.

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.

Parameters:

  | fileID | File ID, returned by the openFile() method.

Returns:
  The size of the remote file. False on any error.

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.

Parameters:

  | 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".

Returns:
  True if the request succeeds. False on any error.

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.

Parameters:

  | fileID | File ID, returned by the openFile() method.

Returns:
  True if the request succeeds. False on any error.

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.

Parameters:

  | filename | Relative path to a remote file or directory.

Returns:
  ne7ssh_string class containing full path to the remote file or directory. The class will contain an empty string on error.

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.

Parameters:

  | packet | Reference to the newly received packet.

Returns:
  True if data successfully processed. False on any error.

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.

Parameters:

  | packet | NAME packet.

Returns:
  True if processing successful, otherwise false.

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.

Parameters:

  | packet | DATA packet.

Returns:
  True if processing successful, otherwise false.

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.

Parameters:

  | packet | STATUS packet.

Returns:
  True if processing successful, otherwise false.

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.

Parameters:

  | packet | VERSION packet.

Returns:
  True if processing successful, otherwise false.

References ne7ssh::errors(), ne7ssh_string::getInt(), ne7ssh_session::getSshChannel(), and Ne7sshError::push().

Referenced by handleData().

bool Ne7sshSftp::init | ( |  | ) |

Initializes SFTP subsystem.

Returns:
  True if the subsystem successfully initialized. False on any error.

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.

Parameters:

  | remoteFile | Full or relative path to the remote file.

Returns:
  True if the remote inode is a directory. Otherwise false.

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.

Parameters:

  | remoteFile | Full or relative path to the remote inode.

Returns:
  True if the remote inode is a regular file. Otherwise false.

References isType().

Referenced by Ne7SftpSubsystem::isFile().

bool Ne7sshSftp::isType | ( | const char * | remoteFile,
|  | uint32 | type |
| ) |  |  | `[private]`

Determines the type of a remote file.

Parameters:

  | remoteFile | Relative or full path to the remote file.
  | type | Type, taken from sys/stat.h.

Returns:
  True if file is of specified type. Otherwise false.

References ne7ssh::errors(), getFileStats(), getFullPath(), ne7ssh_session::getSshChannel(), ne7ssh_string::length(), Ne7sshError::push(), and ne7ssh_string::value().

Referenced by isDir(), and isFile().

const char * Ne7sshSftp::ls | ( | const char * | remoteDir,
|  | bool | longNames = `false` |
| ) |  |  |

This methods is used retrieve a listing of a remote directory.

Parameters:

  | 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.

Returns:
  A pointer to a string containing the directory listing.

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.

Parameters:

  | remoteDir | Full or relative path to a new directory on the remote server.

Returns:
  True if the directory successfully created. False on any error.

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.

Parameters:

  | 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.

Returns:
  True if renaming successfull. False on any error.

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.

Parameters:

  | dirname | Relative or full path to the inode.

Returns:
  Newly opened file ID or 0 if the inode could not be opened.

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.

Parameters:

  | 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.

Returns:
  Newly opened file ID or 0 if file could not be opened.

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.

Parameters:

  | packet | ATTRS packet.

Returns:
  True if processing successful, otherwise false.

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.

Parameters:

  | 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.

Returns:
  True if putting the file succeeds. False on any error.

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.

Parameters:

  | fileID | File ID retruned by openFile() method.
  | offset | Offset. 0 by default.

Returns:
  True if file content successfully read and placed in the buffer. Otherwise false.

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.

Parameters:

  | _cmd | SFTP command to wait for.
  | timeSec | Timeout in seconds.

Returns:
  True if the command specified has been received. Otherwise false.

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.

Parameters:

  | _cmd | Command to receive.
  | timeSec | Timeout in seconds.

Returns:
  True if all expected data received. Otherwise false.

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.

Returns:
  True if the ADJUST_WINDOW packet has been received, otherwise false.

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.

Parameters:

  | remoteFile | Full or relative path to the file on the remote side.

Returns:
  True if remove succeeds. False on any error.

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.

Parameters:

  | remoteDir | Full or relative path to a directory to be removed.

Returns:
  True if the directory successfully removed. False on any error.

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.

Parameters:

  | _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.

Parameters:

  | 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.

Returns:
  True if file contect successfully written. False on any error.

References ne7ssh::errors(), getFileHandle(), ne7ssh_session::getSendChannel(), ne7ssh_session::getSshChannel(), Ne7sshError::push(), receiveUntil(), and receiveWindowAdjust().

Referenced by put(), and Ne7SftpSubsystem::writeFile().

The documentation for this class was generated from the following files:

- ne7ssh_sftp.h
- ne7ssh_sftp.cpp