Contents · Forum Home · StreamSec Tools 4.1.5.363 documentation

StreamSec.SFTP.Transport

Feature notes: SFTP implementation notes

Classes, interfaces and records

interfaceiSFTPTransport

(no public members)

interfaceiSFTPTransport

inherits from iBaseInterface

function GetServerNewLine: string;
getter for ServerNewLine
function GetServerSupported: tSupportedStructure;
getter for ServerSupported
procedure SetServerNewLine(const Value: string);
setter for ServerNewLine (normally internal wiring)
procedure SetServerSupported(const Value: tSupportedStructure);
setter for ServerSupported; app assigns the object to be filled
function NewFileAttributes: iFileAttributes;
Creates an empty iFileAttributes matching the negotiated protocol version (tFileAttributes3 for v3, tFileAttributes5 for v5). Call this to build an attributes object to pass to OpenFile/MakeDir/SetStat, or to receive decoded attributes; the version choice follows the connected SFTP session.
property ServerNewLine: string read GetServerNewLine write SetServerNewLine;
Newline byte sequence the peer advertised via the SFTP 'newline' extension, used when translating line endings for text-mode transfers. Filled on the client from the server's SSH_FXP_VERSION reply during Connect; empty if the server sent no such extension.
property ServerSupported: tSupportedStructure read GetServerSupported write SetServerSupported;
The v5 'supported' capabilities the server advertised (max read size, access/attribute masks, open flags, extension names). Assign a tSupportedStructure before connecting and the client decodes the server's extension into it during Connect; leaving it nil ignores that extension.

interfaceiFileAttributesList

Read-only in-memory snapshot of a directory listing: parallel arrays of file names and their attributes. An application uses this when it wants the whole listing as an indexable collection instead of handling each entry through the per-file OnReadDir event (see tFileAttributesList, which builds one).

function GetItemCount: Integer;
getter for ItemCount
function GetFileNames(aIndex: Integer): string;
getter for FileNames[]
function GetAttributes(aIndex: Integer): iFileAttributes;
getter for Attributes[]
property ItemCount: Integer read GetItemCount;
Number of directory entries captured in this listing snapshot.
property FileNames[aIndex: Integer]: string read GetFileNames;
Name of the aIndex-th entry (0..ItemCount-1) in the collected listing.
property Attributes[aIndex: Integer]: iFileAttributes read GetAttributes;
Attributes of the aIndex-th entry, paired with FileNames at the same index.

classtFileAttributesList

inherits from TInterfacedObject

implements iFileAttributesList

constructor Create;
constructs an empty listing ready to be populated
destructor Destroy; override;
releases the collected names and attribute references
procedure AddFile(const aTransport : iSFTPTransport; const aFileName : string; const aAttributes: iFileAttributes);
Appends one (name, attributes) pair to the list and, when OnReadDir is set, fires that event for the entry. Its signature matches tSFTPReadDirEvent, so an application can wire this method directly as the transport's OnReadDir to accumulate a whole ReadDir listing here while still observing each file.
property OnReadDir: tSFTPReadDirEvent read fOnReadDir write SetOnReadDir;
Optional per-entry callback fired by AddFile as each file is appended.

interfaceiSFTPClientTransport

inherits from iSFTPTransport

function GetProtocolVersion: Integer;
getter for ProtocolVersion
function GetOnReadDir: tSFTPReadDirEvent;
getter for OnReadDir
procedure SetOnReadDir(const Value: tSFTPReadDirEvent);
setter for OnReadDir
function OpenFile(const aFileName: string; const aAccess : tACEMask; const aFlags : tFileOpenFlags5; const aAttrs : iFileAttributes5 = nil) : tSFTPHandle; overload;
Opens (and optionally creates) a remote file using the SFTP v5+ model: aAccess is the ACE-style access mask, aFlags the v5 open disposition (create/truncate/exclusive), and aAttrs optionally seeds attributes when a new file is created. Returns an opaque handle for ReadFile/WriteFile/ CloseHandle. Use this overload against a version-5 server.
function OpenFile(const aFileName: string; const aFlags : tFileOpenFlags3; const aAttrs : iFileAttributes3 = nil) : tSFTPHandle; overload;
Version-3 variant of OpenFile: aFlags is the legacy pflags open bitmask (read/write/append/creat/trunc/excl). Use this overload when connected to a protocol-version-3 server.
function OpenDir(const aPath: string): tSFTPHandle;
Opens a directory for enumeration and returns a handle to feed to ReadDir.
function CloseHandle(const aHandle: tSFTPHandle): Boolean;
Releases a file or directory handle obtained from OpenFile/OpenDir. Returns True on an SSH_FX_OK status. Call once you are done reading or writing.
function ReadFile(const aHandle: tSFTPHandle; const aOffset: Int64; aLength: Integer) : tBytes; overload;
Reads up to aLength bytes starting at aOffset and returns them as a byte array; the result is shorter than requested near end-of-file and nil at EOF.
procedure ReadFile(const aHandle: tSFTPHandle; const aOffset: Int64; aLength: Integer; aDest : TStream); overload;
Streaming variant of ReadFile: writes the bytes returned for one READ straight into aDest, avoiding an intermediate buffer when downloading; at EOF it writes nothing.
function ReadDir(const aHandle: tSFTPHandle): Boolean;
Fetches the next batch of directory entries for an OpenDir handle, firing OnReadDir once per entry. Returns True while entries remain and False at the end (SSH_FX_EOF); an application calls it in a loop until it returns False.
function WriteFile(const aHandle : tSFTPHandle; const aBuf : tBytes; aBufOffset : Integer; aLength : Integer; const aFileOffset: Int64) : Boolean; overload;
Writes aLength bytes taken from aBuf at aBufOffset to the open file at absolute position aFileOffset. Returns True on an SSH_FX_OK status.
function WriteFile(const aHandle : tSFTPHandle; aSource : TStream; const aLength : Integer) : Boolean; overload;
Writes aLength bytes read from aSource (at the stream's current position) to the open file at that same stream position; used when uploading from a stream.
function RemoveFile(const aFileName: string): Boolean;
Deletes a remote file. Returns True on SSH_FX_OK.
function Rename(const aOldPath: string; const aNewPath: string; const aFlags : tRenameFlags = []) : Boolean;
Renames or moves aOldPath to aNewPath. aFlags (atomic/overwrite/native) is only transmitted to version-4+ servers and ignored on version 3.
function MakeDir(const aPath: string; const aAttr: iFileAttributes) : Boolean;
Creates a remote directory, optionally seeding it with aAttr (permissions, owner). Pass nil for default attributes.
function RemoveDir(const aPath: string): Boolean;
Removes a remote directory (which the server requires to be empty).
function Stat(const aPath : string; const aFlags: tValidFileAttrFlags5 = []) : iFileAttributes;
Retrieves the attributes of a path, following a final symbolic link. On version-4+ servers aFlags selects which attribute groups to return; it is ignored on version 3.
function LStat(const aPath : string; const aFlags: tValidFileAttrFlags5 = []) : iFileAttributes;
Like Stat but does NOT follow a final symbolic link, so it reports on the link itself.
function FStat(const aHandle: tSFTPHandle; const aFlags : tValidFileAttrFlags5 = []) : iFileAttributes;
Retrieves the attributes of an already-open handle rather than a path.
function SetStat(const aPath : string; const aAttrs: iFileAttributes) : Boolean;
Applies the given attributes (permissions, times, size, owner) to a path.
function FSetStat(const aHandle: tSFTPHandle; const aAttrs : iFileAttributes) : Boolean;
Applies attributes to an already-open handle rather than a path.
function ReadLink(const aPath: string): string;
Returns the target path a symbolic link points to.
function SymLink(const aLinkPath : string; const aTargetPath: string) : Boolean;
Creates a symbolic link at aLinkPath that points to aTargetPath.
function RealPath(const aPath: string): string;
Canonicalizes aPath into an absolute path as the server sees it; the usual way to resolve "." to the login/home directory after connecting.
function MD5Hash(const aFileName: string; const aLocal : TStream; const aLength : Int64) : Boolean;
Uses the server's md5-hash extension to compare a remote file against the local stream aLocal over aLength bytes, returning True when the digests match; an application uses it to decide whether a transfer can be skipped.
function MD5HashHandle(const aHandle: tSFTPHandle; const aLocal : TStream; const aLength: Int64) : Boolean;
Same digest comparison as MD5Hash but keyed by an already-open handle (md5-hash-handle extension) instead of a file name.
property ProtocolVersion: Integer read GetProtocolVersion;
The SFTP protocol version negotiated during Connect (3 or 5).
property OnReadDir: tSFTPReadDirEvent read GetOnReadDir write SetOnReadDir;
Event fired once for each entry decoded while a ReadDir batch is processed.

interfaceiResponse

Internal wiring: one outstanding SFTP request-reply pairing, tracked by the transport so an incoming reply packet can be matched back to the request that is waiting for it (by request id). Applications call the high-level iSFTPClientTransport methods and rarely touch this directly.

function GetPacketType: Byte;
getter for PacketType
function GetRequestID: UInt32;
getter for RequestID
function GetIsDecoded: Boolean;
getter for IsDecoded
function GetBuf: tBytes;
getter for Buf
function GetOffset: Integer;
getter for Offset
function GetLen: Integer;
getter for Len
function Decode(aCode: Byte; const aBuf : tBytes; aOffs: Integer; aLen : Integer) : Boolean;
Attempts to claim a freshly received packet for this pending request: succeeds (returns True) and captures aBuf/offset/length only when the packet's leading request id equals RequestID and this response has not already been decoded. Driven by the transport's reply dispatch loop.
property PacketType: Byte read GetPacketType;
The SSH_FXP_* type byte of the matched reply (e.g. SSH_FXP_STATUS, SSH_FXP_DATA); valid once IsDecoded is True.
property RequestID: UInt32 read GetRequestID;
The request id this response object is waiting for.
property IsDecoded: Boolean read GetIsDecoded;
True once a matching reply has been captured; the transport spins on this while awaiting the answer to a synchronous operation.
property Buf: tBytes read GetBuf;
The raw reply packet bytes captured when the request id matched.
property Offset: Integer read GetOffset;
Offset into Buf where the reply payload begins (just past the request id).
property Len: Integer read GetLen;
Remaining payload length in Buf from Offset onward.

classtResponse

inherits from TInterfacedObject

implements iResponse

Default iResponse implementation: one instance per outstanding request, registered in the transport's thread-safe response list and matched to an incoming reply by request id. Created internally via the transport, not by application code.

constructor Create(aList: TThreadList; const aRequestID: UInt32);
Creates a pending response for aRequestID and registers itself in the shared list aList so the transport's dispatch loop can later match a reply to it.
destructor Destroy; override;
Unregisters from the list if the response was never matched (still pending).
function Decode(aCode: Byte; const aBuf : tBytes; aOffs: Integer; aLen : Integer) : Boolean;
Claims a received packet for this request when its leading id equals RequestID and it has not yet been decoded; see iResponse.Decode.
property PacketType: Byte read fPacketType write SetPacketType;
The SSH_FXP_* type byte of the matched reply; set when the reply is decoded.
property RequestID: UInt32 read fRequestID;
The request id this response waits on.
property IsDecoded: Boolean read fIsDecoded;
True once a matching reply has been captured.
property Buf: tBytes read fBuf;
The raw reply packet bytes captured on decode.
property Offset: Integer read fOffs;
Offset into Buf where the reply payload begins (past the request id).
property Len: Integer read fLen;
Remaining payload length in Buf from Offset.

classeSFTPClientError

inherits from Exception

Base class for SFTP client-side failures raised by this unit, e.g. an unexpected/unknown packet type, an unsupported protocol version, or the SSH channel closing mid-operation. Catch this to handle any SFTP client error.

(no public members)

classeSFTPStatusError

inherits from eSFTPClientError

Raised when the server answers with an SSH_FXP_STATUS whose code is neither OK nor EOF (a real error such as no-such-file or permission-denied); carries the numeric SSH_FX_* code plus the server's message and language tag.

constructor Create(aErrorCode : UInt32; const aErrorMessage: string; const aLangTag : AnsiString);
Builds the exception from the decoded status fields.
property ErrorCode: UInt32 read fErrorCode;
The SSH_FX_* status code the server returned.
property ErrorMessage: string read fErrorMessage;
The server-supplied human-readable message (present on v4+; empty on v3).
property LangTag: AnsiString read fLangTag;
RFC 3066 language tag describing ErrorMessage (present on v4+).

classeSFTPUnknownPrinciple

inherits from eSFTPStatusError

Specialization raised for the SSH_FX_UNKNOWN_PRINCIPLE status (a SetStat owner or group name that the server could not map); additionally exposes the offending principal name(s).

constructor Create(aErrorCode : UInt32; const aErrorMessage: string; const aLangTag : AnsiString; const aNames : string);
Builds the exception, adding the unmapped principal name(s) to the base status fields.
property Names: string read fNames;
Comma-separated list of the principal name(s) the server did not recognize.

classtSFTPTransport

constructor Create(const aChannel : iSSHWindowedChannel; aAsServer: Boolean);
Wraps an already-open SSH windowed channel as an SFTP transport. Pass aAsServer False for a client that will issue requests (the fully implemented role), True to act as the server side. Call Connect afterwards to launch the "sftp" subsystem and negotiate the protocol version.
destructor Destroy; override;
Releases the outstanding-response tracking list; does not close the channel.

Types (enumerations, sets, aliases)

typetSFTPHandle

Opaque server-issued file/directory handle returned by OpenFile/OpenDir and passed back to ReadFile/WriteFile/ReadDir/FStat/CloseHandle; treat as a blob.

  tSFTPHandle = tBytes;

proctSFTPReadDirEvent

Callback fired once per directory entry during ReadDir, delivering the entry's file name and decoded attributes; wire it to OnReadDir to observe each file.

  tSFTPReadDirEvent = procedure (const aTransport : iSFTPTransport;
                                 const aFileName  : string;
                                 const aAttributes: iFileAttributes) of object;

settByteSet

Set of SSH_FXP_* packet-type bytes; used internally to whitelist which reply packet types ReadPacket will accept from the peer.

  tByteSet = set of Byte;