CkSFtp C++ Class Reference (Visual C++)
CkSFtp
SFTP implementation for file transfer and remote file management over SSH.
Properties
void get_AccumulateBuffer(CkByteData &data);
Contains the bytes downloaded from a remote file via the AccumulateBytes method call. Each call to AccumulateBytes appends to this buffer. To clear this buffer, call the ClearAccumulateBuffer method.
void get_ClientIdentifier(CkString &str); void put_ClientIdentifier(const char *newVal);
The client-identifier string to be used when connecting to an SSH/SFTP server. Defaults to "SSH-2.0-PuTTY_Local:_Jun_27_2008_16:28:58". (This string is used to mimic PuTTY because some servers are known to disconnect from clients with unknown client identifiers.)
int get_ConnectTimeoutMs(void); void put_ConnectTimeoutMs(int newVal);
Maximum number of milliseconds to wait when connecting to an SSH server.
int get_DisconnectCode(void);
If the SSH/SFTP server sent a DISCONNECT message when closing the connection, this property contains the "reason code" as specified in RFC 4253:
Symbolic name reason code
------------- -----------
SSH_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT 1
SSH_DISCONNECT_PROTOCOL_ERROR 2
SSH_DISCONNECT_KEY_EXCHANGE_FAILED 3
SSH_DISCONNECT_RESERVED 4
SSH_DISCONNECT_MAC_ERROR 5
SSH_DISCONNECT_COMPRESSION_ERROR 6
SSH_DISCONNECT_SERVICE_NOT_AVAILABLE 7
SSH_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED 8
SSH_DISCONNECT_HOST_KEY_NOT_VERIFIABLE 9
SSH_DISCONNECT_CONNECTION_LOST 10
SSH_DISCONNECT_BY_APPLICATION 11
SSH_DISCONNECT_TOO_MANY_CONNECTIONS 12
SSH_DISCONNECT_AUTH_CANCELLED_BY_USER 13
SSH_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE 14
SSH_DISCONNECT_ILLEGAL_USER_NAME 15
void get_DisconnectReason(CkString &str);
If the SSH/SFTP server sent a DISCONNECT message when closing the connection, this property contains a descriptive string for the "reason code" as specified in RFC 4253.
bool get_EnableCache(void); void put_EnableCache(bool newVal);
Controls whether the component keeps an internal file size & attribute cache. The cache affects the following methods: GetFileSize32, GetFileSize64, GetFileSizeStr, GetFileCreateTime, GetFileLastAccess, GetFileLastModified, GetFileOwner, GetFileGroup, and GetFilePermissions.
The file attribute cache exists to minimize communications with the SFTP server. If the cache is enabled, then a request for any single attribute will cause all of the attributes to be cached. A subsequent request for a different attribute of the same file will be fulfilled from cache, eliminating the need for a message to be sent to the SFTP server.
Note: Caching only occurs when filenames are used, not handles.
CkSFtpProgress *get_EventCallbackObject(void) const; void put_EventCallbackObject(CkSFtpProgress *progress);
To be documented soon.
void get_FilenameCharset(CkString &str); void put_FilenameCharset(const char *newVal);
Automatically set during the InitializeSftp method if the server sends a filename-charset name-value extension. Otherwise, may be set directly to the name of a charset, such as "utf-8", "iso-8859-1", "windows-1252", etc. ("ansi" is also a valid choice.) Incoming filenames are interpreted as utf-8 if no FilenameCharset is set. Outgoing filenames are sent using utf-8 by default. Otherwise, incoming and outgoing filenames use the charset specified by this property.
bool get_ForceV3(void); void put_ForceV3(bool newVal);
If set to true, forces the client to choose version 3 of the SFTP protocol, even if the server supports a higher version. The default value of this property is false.
int get_HeartbeatMs(void); void put_HeartbeatMs(int newVal);
This is the number of milliseconds between each AbortCheck event callback. The AbortCheck callback allows an application to abort any SFTP operation prior to completion. If HeartbeatMs is 0 (the default), no AbortCheck event callbacks will fire.
void get_HostKeyFingerprint(CkString &str);
Set after connecting to an SSH/SFTP server. The format of the fingerprint looks like this: "ssh-rsa 1024 68:ff:d1:4e:6c:ff:d7:b0:d6:58:73:85:07:bc:2e:d5"
void get_HttpProxyAuthMethod(CkString &str); void put_HttpProxyAuthMethod(const char *newVal);
If an HTTP proxy requiring authentication is to be used, set this property to the HTTP proxy authentication method name. Valid choices are "LOGIN" or "NTLM".
void get_HttpProxyHostname(CkString &str); void put_HttpProxyHostname(const char *newVal);
If an HTTP proxy is to be used, set this property to the HTTP proxy hostname or IPv4 address (in dotted decimal notation).
void get_HttpProxyPassword(CkString &str); void put_HttpProxyPassword(const char *newVal);
If an HTTP proxy requiring authentication is to be used, set this property to the HTTP proxy password.
int get_HttpProxyPort(void); void put_HttpProxyPort(int newVal);
If an HTTP proxy is to be used, set this property to the HTTP proxy port number. (Two commonly used HTTP proxy ports are 8080 and 3128.)
void get_HttpProxyUsername(CkString &str); void put_HttpProxyUsername(const char *newVal);
If an HTTP proxy requiring authentication is to be used, set this property to the HTTP proxy login name.
int get_IdleTimeoutMs(void); void put_IdleTimeoutMs(int newVal);
Causes SFTP operations to fail when progress for sending or receiving data halts for more than this number of milliseconds. Setting IdleTimeoutMs = 0 (the default) allows the application to wait indefinitely.
int get_InitializeFailCode(void);
The InitializeSftp method call opens a channel for the SFTP session. If the request to open a channel fails, this property contains a code that identifies the reason for failure. The reason codes are defined in RFC 4254 and are reproduced here:
Symbolic name reason code
------------- -----------
SSH_OPEN_ADMINISTRATIVELY_PROHIBITED 1
SSH_OPEN_CONNECT_FAILED 2
SSH_OPEN_UNKNOWN_CHANNEL_TYPE 3
SSH_OPEN_RESOURCE_SHORTAGE 4
void get_InitializeFailReason(CkString &str);
The InitializeSftp method call opens a channel for the SFTP session. If the request to open a channel fails, this property contains a description of the reason for failure. (It contains descriptive text matching the InitializeFailCode property.)
bool get_IsConnected(void);
Returns true if connected to the SSH server. Note: This does not mean authentication has happened or InitializeSftp has already succeeded. It only means that the connection has been established by calling Connect.
bool get_KeepSessionLog(void); void put_KeepSessionLog(bool newVal);
Controls whether communications to/from the SFTP server are saved to the SessionLog property. The default value is false. If this property is set to true, the contents of the SessionLog property will continuously grow as SFTP activity transpires. The purpose of the KeepSessionLog / SessionLog properties is to help in debugging any future problems that may arise.
void LastErrorHtml(CkString &str);
Error information in HTML format for the last method called.
void LastErrorText(CkString &str);
Error information in plain-text format for the last method called.
void LastErrorXml(CkString &str);
Error information in XML format for the last method called.
int get_MaxPacketSize(void); void put_MaxPacketSize(int newVal);
The maximum packet length to be used in the underlying SSH transport protocol. The default value is 32768. (This should generally be left unchanged.)
bool get_PasswordChangeRequested(void);
To be documented soon...
bool get_PreserveDate(void); void put_PreserveDate(bool newVal);
If true, then the file last-modified and create date/time will be preserved for downloaded files. The default value is false.
int get_ProtocolVersion(void);
The negotiated SFTP protocol version, which should be a value between 3 and 6 inclusive. When the InitializeSftp method is called, the Chilkat SFTP client sends it's highest supported protocol version to the server, and the server sends it's SFTP protocol version in response. The negotiated protocol (i.e. the protocol version used for the session) is the lower of the two numbers. If the SFTP server's protocol version is lower than 6, some file date/attributes are not supported because they are not supported by the earlier protocol version. These exceptions are noted throughout the reference documentation.
void get_SessionLog(CkString &str);
Contains a log of the messages sent to/from the SFTP server. To enable session logging, set the KeepSessionLog property = true. Note: This property is not a filename -- it is a string property that contains the session log data.
void get_SocksHostname(CkString &str); void put_SocksHostname(const char *newVal);
The SOCKS4/SOCKS5 hostname or IPv4 address (in dotted decimal notation). This property is only used if the SocksVersion property is set to 4 or 5).
void get_SocksPassword(CkString &str); void put_SocksPassword(const char *newVal);
The SOCKS5 password (if required). The SOCKS4 protocol does not include the use of a password, so this does not apply to SOCKS4.
int get_SocksPort(void); void put_SocksPort(int newVal);
The SOCKS4/SOCKS5 proxy port. The default value is 1080.
This property only applies if a SOCKS proxy is used (if the SocksVersion property is set to 4 or 5).
void get_SocksUsername(CkString &str); void put_SocksUsername(const char *newVal);
The SOCKS4/SOCKS5 proxy username. This property is only used if the SocksVersion property is set to 4 or 5).
int get_SocksVersion(void); void put_SocksVersion(int newVal);
SocksVersion
May be set to one of the following integer values:
0 - No SOCKS proxy is used. This is the default.
4 - Connect via a SOCKS4 proxy.
5 - Connect via a SOCKS5 proxy.
bool get_TcpNoDelay(void); void put_TcpNoDelay(bool newVal);
To be documented soon...
bool get_UtcMode(void); void put_UtcMode(bool newVal);
If true, then date/times are returned as UTC times. If false (the default) then date/times are returned as local times.
bool get_Utf8(void) const; void put_Utf8(bool b);
To be documented soon...
bool get_VerboseLogging(void); void put_VerboseLogging(bool newVal);
If set to true, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false.
void get_Version(CkString &str);
The version of the SFTP component, such as "2.3.0".
Methods
int AccumulateBytes(const char * handle, int maxBytes);
Downloads bytes from an open file and appends them to the AccumulateBuffer. The handle is a file handle returned by the OpenFile method. The maxBytes is the maximum number of bytes to read. If the end-of-file is reached prior to reading the number of requested bytes, then fewer bytes may be returned.
Returns the number of bytes downloaded and appended to AccumulateBuffer. Returns -1 on error.
void Add64(const char * n1, const char * n2, CkString & outStr);
Convenience method for 64-bit addition. Allows for two 64-bit integers to be passed as decimal strings and returns the sum in a decimal sting. Returns true for success, false for failure.
bool AuthenticatePk(const char * username, CkSshKey & privateKey);
Authenticates with the SSH server using public-key authentication. The corresponding public key must have been installed on the SSH server for the username. Authentication will succeed if the matching privateKey is provided.
Important: When reporting problems, please send the full contents of the LastErrorText property to support@chilkatsoft.com. Returns true for success, false for failure.
bool AuthenticatePk(const char * username, const char * privateKey, CkSshKey & privateKey);
Authenticates with the SSH server using public-key authentication. The corresponding public key must have been installed on the SSH server for the username. Authentication will succeed if the matching privateKey is provided.
Important: When reporting problems, please send the full contents of the LastErrorText property to support@chilkatsoft.com. Returns true for success, false for failure.
bool AuthenticatePw(const char * login, const char * password);
Authenticates with the SSH server using a login and password.
An SFTP session always begins by first calling Connect to connect to the SSH server, then calling either AuthenticatePw or AuthenticatePk to login, and finally calling InitializeSftp.
Important: When reporting problems, please send the full contents of the LastErrorText property to support@chilkatsoft.com. Returns true for success, false for failure.
bool AuthenticatePwPk(const char * username, const char * password, CkSshKey & privateKey);
Authentication for SSH servers that require both a password and private key. (Most SSH servers are configured to require one or the other, but not both.)
Important: When reporting problems, please send the full contents of the LastErrorText property to support@chilkatsoft.com. Returns true for success, false for failure.
void ClearAccumulateBuffer();
Clears the contents of the AccumulateBuffer property.
void ClearCache();
Clears the internal file attribute cache. (Please refer to the EnableCache property for more information about the file attribute cache.)
void ClearSessionLog();
Clears the contents of the SessionLog property.
bool CloseHandle(const char * handle);
Closes a file on the SSH/SFTP server. handle is a file handle returned from a previous call to OpenFile. Returns true for success, false for failure.
bool Connect(const char * hostname, int port);
Connects to an SSH/SFTP server. The hostname may be a hostname or an IP address (example: 192.168.1.10). The port is typically 22, which is the standard port for SSH servers.
An SFTP session always begins by first calling Connect to connect to the SSH server, then calling either AuthenticatePw or AuthenticatePk to login, and finally calling InitializeSftp.
Important: When reporting problems, please send the full contents of the LastErrorText property to support@chilkatsoft.com. Returns true for success, false for failure.
bool CopyFileAttr(const char * localFilename, const char * remoteFilename, bool isHandle);
Sets the date/time and other attributes of a remote file to be equal to that of a local file.
The attributes copied depend on the SFTP version of the server:
SFTP v3 (and below)
Last-Modified Date/Time
Last-Access Date/Time
SFTP v4, v5
Last-Modified Date/Time
Last-Access Date/Time
Create Date/Time
SFTP v6 (and above)
Last-Modified Date/Time
Last-Access Date/Time
Create Date/Time
Read-Only Flag
Hidden Flag
Archive Flag
Compressed Flag
Encrypted Flag
Notes:
(1) The Last-Access date/time may or may not be set. Chilkat has found that the Last-Access time is set to the current date/time, which is probably a result of the operating system setting it based on when the SFTP server is touching the file.
(2) At the time of this writing, it is unknown whether the compressed/encryption settings for a local file are transferred to the remote file. For example, does the remote file become compressed and/or encrypted just by setting the flags? It may depend on the SFTP server and/or the remote filesystem.
(3) Dates/times are sent in GMT. SFTP servers should convert GMT times to local time zones.Returns true for success, false for failure.
bool CreateDir(const char * path);
Creates a directory on the SFTP server. Returns true for success, false for failure.
void Disconnect();
Disconnects from the SSH server.
bool DownloadFile(const char * handle, const char * toFilename);
Downloads a file from the SSH server to the local filesystem. There are no limitations on file size and the data is streamed from SSH server to the local file. handle is a file handle returned by a previous call to OpenFile. Returns true for success, false for failure.
bool DownloadFileByName(const char * remoteFilePath, const char * localFilePath);
Simplified method for downloading files. Returns true for success, false for failure. Understanding SFTP Absolute FilePaths
bool Eof(const char * handle);
Returns true if the last read operation for a handle reached the end of file. Otherwise returns false.
If an invalid handle is passed, a value of true is returned.
bool GetFileCreateTime(const char * filenameOrHandle, bool bFollowLinks, bool bIsHandle, SYSTEMTIME & sysTime);
Returns the create date/time for a file. filenameOrHandle may be a remote filepath or an open handle string as returned by OpenFile. If filenameOrHandle is a handle, then bIsHandle must be set to true, otherwise it should be false. If bFollowLinks is true, then symbolic links will be followed on the server.
Note: Servers running the SFTP v3 protocol or lower do not have the ability to return a file's creation date/time.
bool GetFileGroup(const char * filenameOrHandle, bool bFollowLinks, bool bIsHandle, CkString & outStr);
Returns the group of a file. filenameOrHandle may be a remote filepath or an open handle string as returned by OpenFile. If filenameOrHandle is a handle, then bIsHandle must be set to true, otherwise it should be false. If bFollowLinks is true, then symbolic links will be followed on the server.
Note: Servers running the SFTP v3 protocol or lower do not have the ability to return a file's group name. Instead, the decimal GID of the file is returned. Returns true for success, false for failure.
bool GetFileLastAccess(const char * filenameOrHandle, bool bFollowLinks, bool bIsHandle, SYSTEMTIME & sysTime);
Returns the last-access date/time for a file. filenameOrHandle may be a remote filepath or an open handle string as returned by OpenFile. If filenameOrHandle is a handle, then bIsHandle must be set to true, otherwise it should be false. If bFollowLinks is true, then symbolic links will be followed on the server.
bool GetFileLastModified(const char * filenameOrHandle, bool bFollowLinks, bool bIsHandle, SYSTEMTIME & sysTime);
Returns the last-modified date/time for a file. filenameOrHandle may be a remote filepath or an open handle string as returned by OpenFile. If filenameOrHandle is a handle, then bIsHandle must be set to true, otherwise it should be false. If bFollowLinks is true, then symbolic links will be followed on the server.
bool GetFileOwner(const char * filenameOrHandle, bool bFollowLinks, bool bIsHandle, CkString & outStr);
Returns the owner of a file. filenameOrHandle may be a remote filepath or an open handle string as returned by OpenFile. If filenameOrHandle is a handle, then bIsHandle must be set to true, otherwise it should be false. If bFollowLinks is true, then symbolic links will be followed on the server.
Note: Servers running the SFTP v3 protocol or lower do not have the ability to return a file's owner name. Instead, the decimal UID of the file is returned. Returns true for success, false for failure.
int GetFilePermissions(const char * filenameOrHandle, bool bFollowLinks, bool bIsHandle);
Returns the access permisssions flags of a file. filenameOrHandle may be a remote filepath or an open handle string as returned by OpenFile. If filenameOrHandle is a handle, then bIsHandle must be set to true, otherwise it should be false. If bFollowLinks is true, then symbolic links will be followed on the server.
unsigned long GetFileSize32(const char * filenameOrHandle, bool bFollowLinks, bool bIsHandle);
Returns the size in bytes of a file on the SSH server. If the file size exceeds what can be represented in 32-bits, a value of -1 is returned. filenameOrHandle may be a remote filepath or an open handle string as returned by OpenFile. If filenameOrHandle is a handle, then bIsHandle must be set to true, otherwise it should be false. If bFollowLinks is true, then symbolic links will be followed on the server.
__int64 GetFileSize64(const char * filenameOrHandle, bool bFollowLinks, bool bIsHandle);
Returns a 64-bit integer containing the size (in bytes) of a file on the SSH server. filenameOrHandle may be a remote filepath or an open handle string as returned by OpenFile. If filenameOrHandle is a handle, then bIsHandle must be set to true, otherwise it should be false. If bFollowLinks is true, then symbolic links will be followed on the server.
bool GetFileSizeStr(const char * filenameOrHandle, bool bFollowLinks, bool bIsHandle, CkString & outStr);
Returns the size in bytes (in decimal string form) of a file on the SSH server. filenameOrHandle may be a remote filepath or an open handle string as returned by OpenFile. If filenameOrHandle is a handle, then bIsHandle must be set to true, otherwise it should be false. If bFollowLinks is true, then symbolic links will be followed on the server.
Note: This method exists for environments that do not have 64-bit integer support. The Add64 method is provided for 64-bit addition, and other methods such as ReadFileBytes64s allow for 64-bit values to be passed as strings. Returns true for success, false for failure.
bool InitializeSftp();
Intializes the SFTP subsystem. This should be called after connecting and authenticating. An SFTP session always begins by first calling Connect to connect to the SSH server, then calling either AuthenticatePw or AuthenticatePk to login, and finally calling InitializeSftp.
Important: When reporting problems, please send the full contents of the LastErrorText property to support@chilkatsoft.com.
If this method fails, the reason may be present in the InitializeFailCode and InitializeFailReason properties (assuming the failure occurred when trying to open the SFTP session channel). Returns true for success, false for failure. Error Explained: Received FAILURE response to subsystem request.
bool LastReadFailed(const char * handle);
Returns true if the last read on the specified handle failed. Otherwise returns false.
int LastReadNumBytes(const char * handle);
Returns the number of bytes received by the last read on a specified channel.
bool OpenDir(const char * path, CkString & outStr);
Opens a directory for reading. To get a directory listing, first open the directory by calling this method, then call ReadDir to read the directory, and finally call CloseHandle to close the directory.
The SFTP protocol represents file names as strings. File names are
assumed to use the slash ('/') character as a directory separator.
File names starting with a slash are "absolute", and are relative to
the root of the file system. Names starting with any other character
are relative to the user's default directory (home directory). Note
that identifying the user is assumed to take place outside of this
protocol.
Servers SHOULD interpret a path name component ".." as
referring to the parent directory, and "." as referring to the
current directory.
An empty path name is valid, and it refers to the user's default
directory (usually the user's home directory). Returns true for success, false for failure. Understanding SFTP Absolute FilePaths
bool OpenFile(const char * remotePath, const char * access, const char * createDisposition, CkString & outStr);
Opens or creates a file on the remote system. Returns a handle which may be passed to methods for reading and/or writing the file.
access should be one of the following strings: "readOnly", "writeOnly", or "readWrite". createDisposition is a comma-separated list of keywords to provide more control over how the file is opened or created. One of the following keywords must be present: "createNew", "createTruncate", "openExisting", "openOrCreate", or "truncateExisting". All other keywords are optional. The list of keywords and their meanings are shown here:
createNew
A new file is created; if the file already exists the method fails.
createTruncate
A new file is created; if the file already exists, it is opened and truncated.
openExisting
An existing file is opened. If the file does not exist the method fails.
openOrCreate
If the file exists, it is opened. If the file does not exist,
it is created.
truncateExisting
An existing file is opened and truncated. If the file does not
exist the method fails.
appendData
Data is always written at the end of the file.
Data is not required to be appended atomically. This means that
if multiple writers attempt to append data simultaneously, data
from the first may be lost.
appendDataAtomic
Data is always written at the end of the file.
Data MUST be written atomically so that there is no chance that
multiple appenders can collide and result in data being lost.
textMode
Indicates that the server should treat the file as text and
convert it to the canonical newline convention in use.
When a file is opened with this flag, data is always appended to the end of the file.
Servers MUST process multiple, parallel reads and writes correctly
in this mode.
blockRead
The server MUST guarantee that no other handle has been opened
with read access, and that no other handle will be
opened with read access until the client closes the
handle. (This MUST apply both to other clients and to other
processes on the server.) In a nutshell, this opens the file
in non-sharing mode.
blockWrite
The server MUST guarantee that no other handle has been opened
with write access, and that no other
handle will be opened with write
access until the client closes the handle. (This MUST apply both
to other clients and to other processes on the server.)
In a nutshell, this opens the file
in non-sharing mode.
blockDelete
The server MUST guarantee that the
file itself is not deleted in any other way until the client
closes the handle. No other client or process is allowed to open the file with delete access.
blockAdvisory
If set, the above "block" modes are advisory. In
advisory mode, only other accesses that specify a "block" mode need
be considered when determining whether the "block" can be granted,
and the server need not prevent I/O operations that violate the
block mode.
The server MAY perform mandatory locking even if the
blockAdvisory flag is set.
noFollow
If the final component of the path is a symlink, then the open
MUST fail.
deleteOnClose
The file should be deleted when the last handle to it is closed.
(The last handle may not be an sftp-handle.) This MAY be emulated
by a server if the OS doesn't support it by deleting the file when
this handle is closed.
accessAuditAlarmInfo
The client wishes the server to enable any privileges or extra
capabilities that the user may have in to allow the reading and
writing of AUDIT or ALARM access control entries.
accessBackup
The client wishes the server to enable any privileges or extra
capabilities that the user may have in order to bypass normal
access checks for the purpose of backing up or restoring files.
backupStream
This flag indicates that the client wishes to read or write a
backup stream. A backup stream is a system dependent structured
data stream that encodes all the information that must be
preserved in order to restore the file from backup medium.
The only well defined use for backup stream data read in this
fashion is to write it to the same server to a file also opened
using the backupStream flag. However, if the server has a well
defined backup stream format, there may be other uses for this
data outside the scope of this protocol.
IMPORANT: If remotePath is a filename with no path, such as "test.txt", and the server responds with a "Folder not found" error, then try prepending "./" to the remotePath. For example, instead of passing "test.txt", try "./test.txt". Returns true for success, false for failure. Understanding SFTP Absolute FilePaths SFTP Where Did My Upload Go?
CkSFtpDir * ReadDir(const char * handle);
Reads the contents of a directory and returns the directory listing (as an object). The handle returned by OpenDir should be passed to this method.
bool ReadFileBytes(const char * handle, int numBytes, CkByteData & outBytes);
Reads file data from a remote file on the SSH server. The handle is a file handle returned by the OpenFile method. The numBytes is the maximum number of bytes to read. If the end-of-file is reached prior to reading the number of requested bytes, then fewer bytes may be returned.
To read an entire file, one may call ReadFileBytes repeatedly until Eof(handle) returns true. Returns false on failure. Returns true if succesful.
bool ReadFileBytes32(const char * handle, int offset, int numBytes, CkByteData & outBytes);
Reads file data from a remote file on the SSH server. The handle is a file handle returned by the OpenFile method. The offset is measured in bytes relative to the beginning of the file. (64-bit offsets are supported via the ReadFileBytes64 and ReadFileBytes64s methods.) The offset is ignored if the "textMode" flag was specified during the OpenFile. The numBytes is the maximum number of bytes to read. If the end-of-file is reached prior to reading the number of requested bytes, then fewer bytes may be returned. Returns false on failure. Returns true if succesful.
bool ReadFileBytes64(const char * handle, __int64 offset, int numBytes, CkByteData & outBytes);
Reads file data from a remote file on the SSH server. The handle is a file handle returned by the OpenFile method. The offset is a 64-bit integer measured in bytes relative to the beginning of the file. The offset is ignored if the "textMode" flag was specified during the OpenFile. The numBytes is the maximum number of bytes to read. If the end-of-file is reached prior to reading the number of requested bytes, then fewer bytes may be returned. Returns false on failure. Returns true if succesful.
bool ReadFileBytes64s(const char * handle, const char * offset, int numBytes, CkByteData & outBytes);
(This method exists for systems that do not support 64-bit integers. The 64-bit integer offset is passed as a decimal string instead.)
Reads file data from a remote file on the SSH server. The handle is a file handle returned by the OpenFile method. The offset is a 64-bit integer represented as a decimal string. It represents an offset in bytes from the beginning of the file. The offset is ignored if the "textMode" flag was specified during the OpenFile. The numBytes is the maximum number of bytes to read. If the end-of-file is reached prior to reading the number of requested bytes, then fewer bytes may be returned. Returns false on failure. Returns true if succesful.
bool ReadFileText(const char * handle, int numBytes, const char * charset, CkString & outStr);
This method is identical to ReadFileBytes except for one thing: The bytes are interpreted according to the specified charset (i.e. the character encoding) and returned as a string. A list of supported charset values may be found on this page: Supported Charsets.
Note: If the charset is an encoding where a single character might be represented in multiple bytes (such as utf-8, Shift_JIS, etc.) then there is a risk that the very last character may be partially read. This is because the method specifies the number of bytes to read, not the number of characters. This is never a problem with character encodings that use a single byte per character, such as all of the iso-8859-* encodings, or the Windows-* encodings.
To read an entire file, one may call ReadFileText repeatedly until Eof(handle) returns true. Returns true for success, false for failure.
bool ReadFileText32(const char * handle, int offset, int numBytes, const char * charset, CkString & outStr);
This method is identical to ReadFileBytes32 except for one thing: The bytes are interpreted according to the specified charset (i.e. the character encoding) and returned as a string. A list of supported charset values may be found on this page: Supported Charsets.
Note: If the charset is an encoding where a single character might be represented in multiple bytes (such as utf-8, Shift_JIS, etc.) then there is a risk that the very last character may be partially read. This is because the method specifies the number of bytes to read, not the number of characters. This is never a problem with character encodings that use a single byte per character, such as all of the iso-8859-* encodings, or the Windows-* encodings. Returns true for success, false for failure.
bool ReadFileText64(const char * handle, __int64 offset, int numBytes, const char * charset, CkString & outStr);
This method is identical to ReadFileBytes64 except for one thing: The bytes are interpreted according to the specified charset (i.e. the character encoding) and returned as a string. A list of supported charset values may be found on this page: Supported Charsets.
Note: If the charset is an encoding where a single character might be represented in multiple bytes (such as utf-8, Shift_JIS, etc.) then there is a risk that the very last character may be partially read. This is because the method specifies the number of bytes to read, not the number of characters. This is never a problem with character encodings that use a single byte per character, such as all of the iso-8859-* encodings, or the Windows-* encodings. Returns true for success, false for failure.
bool ReadFileText64s(const char * handle, const char * offset, int numBytes, const char * charset, CkString & outStr);
This method is identical to ReadFileBytes64s except for one thing: The bytes are interpreted according to the specified charset (i.e. the character encoding) and returned as a string. A list of supported charset values may be found on this page: Supported Charsets.
Note: If the charset is an encoding where a single character might be represented in multiple bytes (such as utf-8, Shift_JIS, etc.) then there is a risk that the very last character may be partially read. This is because the method specifies the number of bytes to read, not the number of characters. This is never a problem with character encodings that use a single byte per character, such as all of the iso-8859-* encodings, or the Windows-* encodings. Returns true for success, false for failure.
bool RealPath(const char * originalPath, const char * composePath, CkString & outStr);
This method can be used to have the server
canonicalize any given path name to an absolute path. This is useful
for converting path names containing ".." components or relative
pathnames without a leading slash into absolute paths. The absolute path is returned by this method.
originalPath is the first component of the path which the client wishes resolved
into a absolute canonical path. This may be the entire path.
The composePath is a path which the client wishes the server to compose with the
original path to form the new path. This field is optional and may be set to a zero-length string.
The server will take the originalPath and apply the composePath
as a modification to it. composePath may be relative to originalPath or may be an absolute path, in which case originalPath will
be discarded. The composePath may be zero length.
Note: Servers running SFTP v4 and below do not support composePath. Returns true for success, false for failure.
bool RemoveDir(const char * path);
Deletes a directory on the remote server. Most (if not all) SFTP servers require that the directorybe empty of files before it may be deleted. Returns true for success, false for failure.
bool RemoveFile(const char * filename);
Deletes a file on the SFTP server. Returns true for success, false for failure. Understanding SFTP Absolute FilePaths
bool RenameFileOrDir(const char * oldPath, const char * newPath);
Renames a file or directory on the SFTP server. Returns true for success, false for failure.
bool ResumeDownloadFileByName(const char * remoteFilePath, const char * localFilePath);
Resumes an SFTP download. The size of the localFilePath is checked and the download begins at the appropriate position in the remoteFilePath. If localFilePath is empty or non-existent, then this method is identical to DownloadFileByName. If the localFilePath is already fully downloaded, then no additional data is downloaded and the method will return true. Returns true for success, false for failure. SFTP Resume Download Example
bool ResumeUploadFileByName(const char * remoteFilePath, const char * localFilePath);
Resumes a file upload to the SFTP/SSH server. The size of the remoteFilePath is first checked to determine the starting offset for the upload. If remoteFilePath is empty or does not exist, this method is equivalent to UploadFileByName. If remoteFilePath is already fully uploaded (i.e. it's size is equal to localFilePath), then no additional bytes are uploaded and true is returned. Returns true for success, false for failure. SFTP Resume Upload Example
bool SaveLastError(const char * filename);
Saves the last error information to an XML formatted file.
bool SetCreateTime(const char * pathOrHandle, bool isHandle, SYSTEMTIME & createDateTime);
Sets the create date/time for a file on the server. The pathOrHandle may be a filepath or the handle of a currently open file. isHandle should be set to true if the pathOrHandle is a handle, otherwise set isHandle to false.
Note: Servers running version 3 or lower of the SFTP protocol do not support setting the create date/time. Returns true for success, false for failure.
bool SetLastAccessTime(const char * pathOrHandle, bool isHandle, SYSTEMTIME & accessDateTime);
Sets the last-access date/time for a file on the server. The pathOrHandle may be a filepath or the handle of a currently open file. isHandle should be set to true if the pathOrHandle is a handle, otherwise set isHandle to false. Returns true for success, false for failure.
bool SetLastModifiedTime(const char * pathOrHandle, bool isHandle, SYSTEMTIME & modifiedDateTime);
Sets the last-modified date/time for a file on the server. The pathOrHandle may be a filepath or the handle of a currently open file. isHandle should be set to true if the pathOrHandle is a handle, otherwise set isHandle to false. Returns true for success, false for failure.
bool SetOwnerAndGroup(const char * pathOrHandle, bool isHandle, const char * owner, const char * group);
Sets the owner and group for a file on the server. The pathOrHandle may be a filepath or the handle of a currently open file. isHandle should be set to true if the pathOrHandle is a handle, otherwise set isHandle to false.
Note: Servers running version 3 or lower of the SFTP protocol do not support setting the owner and group. Returns true for success, false for failure.
bool SetPermissions(const char * pathOrHandle, bool isHandle, int permissions);
Sets the permissions for a file on the server. The pathOrHandle may be a filepath or the handle of a currently open file. isHandle should be set to true if the pathOrHandle is a handle, otherwise set isHandle to false. Returns true for success, false for failure.
bool UnlockComponent(const char * unlockCode);
Unlocks the component. This must be called once prior to calling any other method. A fully-functional 30-day trial is automatically started when an arbitrary string is passed to this method. For example, passing "Hello", or "abc123" will unlock the component for the 1st thirty days after the initial install.
A permanent unlock code for SFTP should contain the substring "SSH" because SFTP is the Secure File Transfer protocol over SSH. It is a sub-system of the SSH protocol. It is not the FTP protocol. If the Chilkat FTP2 component/library should be used for the FTP protocol. Returns true for success, false for failure. Diagnosing UnlockComponent Problems UnlockComponent LastErrorText shows exact string passed to it. Verify UnlockComponent Success w/ Permanent Unlock Code LastErrorText Standard Information How UnlockComponent Works
bool UploadFile(const char * handle, const char * fromFilename);
Uploads a file from the local filesystem to the SFTP server. handle is a handle of a currently open file (obtained by calling the OpenFile method). Returns true for success, false for failure.
bool UploadFileByName(const char * remoteFilePath, const char * localFilePath);
Simplified method for uploading a file to the SFTP/SSH server. Returns true for success, false for failure. SFTP Simplified Upload Example
bool WriteFileBytes(const char * handle, CkByteData & byteData);
Appends byte data to an open file. The handle is a file handle returned by the OpenFile method. Returns true for success, false for failure.
bool WriteFileBytes32(const char * handle, int offset, CkByteData & data);
Writes data to an open file at a specific offset from the beginning of the file. The handle is a file handle returned by the OpenFile method. The offset is an offset from the beginning of the file. Returns true for success, false for failure.
bool WriteFileBytes64(const char * handle, __int64 offset64, CkByteData & data);
Writes data to an open file at a specific offset from the beginning of the file. The handle is a file handle returned by the OpenFile method. The offset64 is an offset from the beginning of the file. Returns true for success, false for failure.
bool WriteFileBytes64s(const char * handle, const char * offset64, CkByteData & data);
Writes data to an open file at a specific offset from the beginning of the file. The handle is a file handle returned by the OpenFile method. The offset64 is an offset (in decimal string format) from the beginning of the file. Returns true for success, false for failure.
bool WriteFileText(const char * handle, const char * charset, const char * textData);
Appends character data to an open file. The handle is a file handle returned by the OpenFile method. charset is a character encoding and is typically set to values such as "ansi", "utf-8", "windows-1252", etc.
A list of supported character encodings is found on this page: Supported Charsets.
Note: It is necessary to specify the character encoding because in many programming languages, strings are represented as Unicode (2 bytes/char) and in most cases one does not wish to write Unicode chars to a text file (although it is possible by setting charset = "Unicode"). Returns true for success, false for failure.
bool WriteFileText32(const char * handle, int offset32, const char * charset, const char * textData);
Writes character data to an open file at a specific offset from the beginning of the file. The handle is a file handle returned by the OpenFile method. charset is a character encoding and is typically set to values such as "ansi", "utf-8", "windows-1252", etc.
A list of supported character encodings is found on this page: Supported Charsets. Returns true for success, false for failure.
bool WriteFileText64(const char * handle, __int64 offset64, const char * charset, const char * textData);
Writes character data to an open file at a specific offset from the beginning of the file. The handle is a file handle returned by the OpenFile method. charset is a character encoding and is typically set to values such as "ansi", "utf-8", "windows-1252", etc.
A list of supported character encodings is found on this page: Supported Charsets. Returns true for success, false for failure.
bool WriteFileText64s(const char * handle, const char * offset64, const char * charset, const char * textData);
Writes character data to an open file at a specific offset from the beginning of the file. The handle is a file handle returned by the OpenFile method. The offset64 is an offset (in decimal string format) from the beginning of the file. charset is a character encoding and is typically set to values such as "ansi", "utf-8", "windows-1252", etc.
A list of supported character encodings is found on this page: Supported Charsets. Returns true for success, false for failure.
const char * add64(const char * n1, const char * n2);
Convenience method for 64-bit addition. Allows for two 64-bit integers to be passed as decimal strings and returns the sum in a decimal sting.
const char * clientIdentifier();
The client-identifier string to be used when connecting to an SSH/SFTP server. Defaults to "SSH-2.0-PuTTY_Local:_Jun_27_2008_16:28:58". (This string is used to mimic PuTTY because some servers are known to disconnect from clients with unknown client identifiers.)
const char * disconnectReason();
If the SSH/SFTP server sent a DISCONNECT message when closing the connection, this property contains a descriptive string for the "reason code" as specified in RFC 4253.
const char * filenameCharset();
Automatically set during the InitializeSftp method if the server sends a filename-charset name-value extension. Otherwise, may be set directly to the name of a charset, such as "utf-8", "iso-8859-1", "windows-1252", etc. ("ansi" is also a valid choice.) Incoming filenames are interpreted as utf-8 if no FilenameCharset is set. Outgoing filenames are sent using utf-8 by default. Otherwise, incoming and outgoing filenames use the charset specified by this property.
const char * getFileGroup(const char * filenameOrHandle, bool bFollowLinks, bool bIsHandle);
Returns the group of a file. filenameOrHandle may be a remote filepath or an open handle string as returned by OpenFile. If filenameOrHandle is a handle, then bIsHandle must be set to true, otherwise it should be false. If bFollowLinks is true, then symbolic links will be followed on the server.
Note: Servers running the SFTP v3 protocol or lower do not have the ability to return a file's group name. Instead, the decimal GID of the file is returned.
const char * getFileOwner(const char * filenameOrHandle, bool bFollowLinks, bool bIsHandle);
Returns the owner of a file. filenameOrHandle may be a remote filepath or an open handle string as returned by OpenFile. If filenameOrHandle is a handle, then bIsHandle must be set to true, otherwise it should be false. If bFollowLinks is true, then symbolic links will be followed on the server.
Note: Servers running the SFTP v3 protocol or lower do not have the ability to return a file's owner name. Instead, the decimal UID of the file is returned.
const char * getFileSizeStr(const char * filenameOrHandle, bool bFollowLinks, bool bIsHandle);
Returns the size in bytes (in decimal string form) of a file on the SSH server. filenameOrHandle may be a remote filepath or an open handle string as returned by OpenFile. If filenameOrHandle is a handle, then bIsHandle must be set to true, otherwise it should be false. If bFollowLinks is true, then symbolic links will be followed on the server.
Note: This method exists for environments that do not have 64-bit integer support. The Add64 method is provided for 64-bit addition, and other methods such as ReadFileBytes64s allow for 64-bit values to be passed as strings.
const char * hostKeyFingerprint();
Set after connecting to an SSH/SFTP server. The format of the fingerprint looks like this: "ssh-rsa 1024 68:ff:d1:4e:6c:ff:d7:b0:d6:58:73:85:07:bc:2e:d5"
const char * httpProxyAuthMethod();
If an HTTP proxy requiring authentication is to be used, set this property to the HTTP proxy authentication method name. Valid choices are "LOGIN" or "NTLM".
const char * httpProxyHostname();
If an HTTP proxy is to be used, set this property to the HTTP proxy hostname or IPv4 address (in dotted decimal notation).
const char * httpProxyPassword();
If an HTTP proxy requiring authentication is to be used, set this property to the HTTP proxy password.
const char * httpProxyUsername();
If an HTTP proxy requiring authentication is to be used, set this property to the HTTP proxy login name.
const char * initializeFailReason();
The InitializeSftp method call opens a channel for the SFTP session. If the request to open a channel fails, this property contains a description of the reason for failure. (It contains descriptive text matching the InitializeFailCode property.)
const char * lastErrorHtml();
Error information in HTML format for the last method called.
const char * lastErrorText();
Error information in plain-text format for the last method called.
const char * lastErrorXml();
Error information in XML format for the last method called.
const char * openDir(const char * path);
Opens a directory for reading. To get a directory listing, first open the directory by calling this method, then call ReadDir to read the directory, and finally call CloseHandle to close the directory.
The SFTP protocol represents file names as strings. File names are
assumed to use the slash ('/') character as a directory separator.
File names starting with a slash are "absolute", and are relative to
the root of the file system. Names starting with any other character
are relative to the user's default directory (home directory). Note
that identifying the user is assumed to take place outside of this
protocol.
Servers SHOULD interpret a path name component ".." as
referring to the parent directory, and "." as referring to the
current directory.
An empty path name is valid, and it refers to the user's default
directory (usually the user's home directory). Understanding SFTP Absolute FilePaths
const char * openFile(const char * remotePath, const char * access, const char * createDisposition);
Opens or creates a file on the remote system. Returns a handle which may be passed to methods for reading and/or writing the file.
access should be one of the following strings: "readOnly", "writeOnly", or "readWrite". createDisposition is a comma-separated list of keywords to provide more control over how the file is opened or created. One of the following keywords must be present: "createNew", "createTruncate", "openExisting", "openOrCreate", or "truncateExisting". All other keywords are optional. The list of keywords and their meanings are shown here:
createNew
A new file is created; if the file already exists the method fails.
createTruncate
A new file is created; if the file already exists, it is opened and truncated.
openExisting
An existing file is opened. If the file does not exist the method fails.
openOrCreate
If the file exists, it is opened. If the file does not exist,
it is created.
truncateExisting
An existing file is opened and truncated. If the file does not
exist the method fails.
appendData
Data is always written at the end of the file.
Data is not required to be appended atomically. This means that
if multiple writers attempt to append data simultaneously, data
from the first may be lost.
appendDataAtomic
Data is always written at the end of the file.
Data MUST be written atomically so that there is no chance that
multiple appenders can collide and result in data being lost.
textMode
Indicates that the server should treat the file as text and
convert it to the canonical newline convention in use.
When a file is opened with this flag, data is always appended to the end of the file.
Servers MUST process multiple, parallel reads and writes correctly
in this mode.
blockRead
The server MUST guarantee that no other handle has been opened
with read access, and that no other handle will be
opened with read access until the client closes the
handle. (This MUST apply both to other clients and to other
processes on the server.) In a nutshell, this opens the file
in non-sharing mode.
blockWrite
The server MUST guarantee that no other handle has been opened
with write access, and that no other
handle will be opened with write
access until the client closes the handle. (This MUST apply both
to other clients and to other processes on the server.)
In a nutshell, this opens the file
in non-sharing mode.
blockDelete
The server MUST guarantee that the
file itself is not deleted in any other way until the client
closes the handle. No other client or process is allowed to open the file with delete access.
blockAdvisory
If set, the above "block" modes are advisory. In
advisory mode, only other accesses that specify a "block" mode need
be considered when determining whether the "block" can be granted,
and the server need not prevent I/O operations that violate the
block mode.
The server MAY perform mandatory locking even if the
blockAdvisory flag is set.
noFollow
If the final component of the path is a symlink, then the open
MUST fail.
deleteOnClose
The file should be deleted when the last handle to it is closed.
(The last handle may not be an sftp-handle.) This MAY be emulated
by a server if the OS doesn't support it by deleting the file when
this handle is closed.
accessAuditAlarmInfo
The client wishes the server to enable any privileges or extra
capabilities that the user may have in to allow the reading and
writing of AUDIT or ALARM access control entries.
accessBackup
The client wishes the server to enable any privileges or extra
capabilities that the user may have in order to bypass normal
access checks for the purpose of backing up or restoring files.
backupStream
This flag indicates that the client wishes to read or write a
backup stream. A backup stream is a system dependent structured
data stream that encodes all the information that must be
preserved in order to restore the file from backup medium.
The only well defined use for backup stream data read in this
fashion is to write it to the same server to a file also opened
using the backupStream flag. However, if the server has a well
defined backup stream format, there may be other uses for this
data outside the scope of this protocol.
IMPORANT: If remotePath is a filename with no path, such as "test.txt", and the server responds with a "Folder not found" error, then try prepending "./" to the remotePath. For example, instead of passing "test.txt", try "./test.txt". Understanding SFTP Absolute FilePaths SFTP Where Did My Upload Go?
const char * readFileText(const char * handle, int numBytes, const char * charset);
This method is identical to ReadFileBytes except for one thing: The bytes are interpreted according to the specified charset (i.e. the character encoding) and returned as a string. A list of supported charset values may be found on this page: Supported Charsets.
Note: If the charset is an encoding where a single character might be represented in multiple bytes (such as utf-8, Shift_JIS, etc.) then there is a risk that the very last character may be partially read. This is because the method specifies the number of bytes to read, not the number of characters. This is never a problem with character encodings that use a single byte per character, such as all of the iso-8859-* encodings, or the Windows-* encodings.
To read an entire file, one may call ReadFileText repeatedly until Eof(handle) returns true.
const char * readFileText32(const char * handle, int offset, int numBytes, const char * charset);
This method is identical to ReadFileBytes32 except for one thing: The bytes are interpreted according to the specified charset (i.e. the character encoding) and returned as a string. A list of supported charset values may be found on this page: Supported Charsets.
Note: If the charset is an encoding where a single character might be represented in multiple bytes (such as utf-8, Shift_JIS, etc.) then there is a risk that the very last character may be partially read. This is because the method specifies the number of bytes to read, not the number of characters. This is never a problem with character encodings that use a single byte per character, such as all of the iso-8859-* encodings, or the Windows-* encodings.
const char * readFileText64(const char * handle, __int64 offset, int numBytes, const char * charset);
This method is identical to ReadFileBytes64 except for one thing: The bytes are interpreted according to the specified charset (i.e. the character encoding) and returned as a string. A list of supported charset values may be found on this page: Supported Charsets.
Note: If the charset is an encoding where a single character might be represented in multiple bytes (such as utf-8, Shift_JIS, etc.) then there is a risk that the very last character may be partially read. This is because the method specifies the number of bytes to read, not the number of characters. This is never a problem with character encodings that use a single byte per character, such as all of the iso-8859-* encodings, or the Windows-* encodings.
const char * readFileText64s(const char * handle, const char * offset, int numBytes, const char * charset);
This method is identical to ReadFileBytes64s except for one thing: The bytes are interpreted according to the specified charset (i.e. the character encoding) and returned as a string. A list of supported charset values may be found on this page: Supported Charsets.
Note: If the charset is an encoding where a single character might be represented in multiple bytes (such as utf-8, Shift_JIS, etc.) then there is a risk that the very last character may be partially read. This is because the method specifies the number of bytes to read, not the number of characters. This is never a problem with character encodings that use a single byte per character, such as all of the iso-8859-* encodings, or the Windows-* encodings.
const char * realPath(const char * originalPath, const char * composePath);
This method can be used to have the server
canonicalize any given path name to an absolute path. This is useful
for converting path names containing ".." components or relative
pathnames without a leading slash into absolute paths. The absolute path is returned by this method.
originalPath is the first component of the path which the client wishes resolved
into a absolute canonical path. This may be the entire path.
The composePath is a path which the client wishes the server to compose with the
original path to form the new path. This field is optional and may be set to a zero-length string.
The server will take the originalPath and apply the composePath
as a modification to it. composePath may be relative to originalPath or may be an absolute path, in which case originalPath will
be discarded. The composePath may be zero length.
Note: Servers running SFTP v4 and below do not support composePath.
const char * sessionLog();
Contains a log of the messages sent to/from the SFTP server. To enable session logging, set the KeepSessionLog property = true. Note: This property is not a filename -- it is a string property that contains the session log data.
const char * socksHostname();
The SOCKS4/SOCKS5 hostname or IPv4 address (in dotted decimal notation). This property is only used if the SocksVersion property is set to 4 or 5).
const char * socksPassword();
The SOCKS5 password (if required). The SOCKS4 protocol does not include the use of a password, so this does not apply to SOCKS4.
const char * socksUsername();
The SOCKS4/SOCKS5 proxy username. This property is only used if the SocksVersion property is set to 4 or 5).
const char * version();
The version of the SFTP component, such as "2.3.0".
|