Method ListDirectoryAsync
ListDirectoryAsync(string, CancellationToken)
Lists a directory, without . and ... Entries arrive in batches as the server sends them, in the server's order;
names that cannot be file names in one directory (empty, or with a /) are left out. Up to 8 batches are asked for at once
(see the remarks). To list only names that match a pattern, or a whole tree, use
ListDirectoryAsync(string, SftpListOptions, CancellationToken).
public IAsyncEnumerable<SftpFileInfo> ListDirectoryAsync(string path, CancellationToken cancellationToken = default)
Parameters
pathstringcancellationTokenCancellationToken
Returns
Remarks
Names are UTF-8 in SFTP version 3 as OpenSSH uses it; a name that is not valid UTF-8 (a legacy encoding on the server) is listed with U+FFFD in place of the bytes it cannot decode, so FullPath cannot open it. A listing stops with an error (ProtocolError) after 1,000,000 entries or 1,000 empty batches in a row, so a hostile server cannot make it run forever.
The batches are pipelined: two READDIR requests go out at once, and twice as many after each batch, up to 8, so a large directory costs a round trip per 8 batches rather than per batch, and a small one (a batch and the end) a single round trip. The server handles the requests on one handle in the order they were sent (the protocol requires it), and each batch is taken in the order of its request, however the replies arrive, so the entries come in the same order as one request at a time would give them. Requests still in flight at the end of the listing are answered and dropped before the handle is closed.
ListDirectoryAsync(string, SftpListOptions, CancellationToken)
Lists a directory with options: only the names that match Pattern, and with
Recursive everything below it as well, depth first: a subdirectory's whole subtree follows the
subdirectory's own entry, before the entries the server listed after it. Each directory is listed as
ListDirectoryAsync(string, CancellationToken) lists it (pipelined, without . and ..) and nothing is
sorted: within a directory the entries come in the server's order, which is often the file system's and need not be
alphabetical, so a file may come before or after a sibling directory's subtree. The order is the same every time the server
returns the same listings; sort the results when you need a fixed one.
public IAsyncEnumerable<SftpFileInfo> ListDirectoryAsync(string path, SftpListOptions options, CancellationToken cancellationToken = default)
Parameters
pathstringThe directory to list.
optionsSftpListOptionsThe pattern, and whether and how to descend into subdirectories.
cancellationTokenCancellationTokenCancels the listing.
Returns
Remarks
A recursive listing reads up to MaxConcurrentDirectories subdirectories ahead while the caller works through the entries already listed, each subdirectory read whole (its handle closed) before its entries are returned. At most that many listings read ahead are kept until the walk reaches them, so the memory it holds is those listings and the ones of the directories it is inside, however wide the tree. It goes no deeper than MaxDepth. Symbolic links are listed but not entered unless FollowSymbolicLinks is set. A subdirectory that cannot be listed (permission denied, or deleted meanwhile) ends the listing with its SftpException when the listing reaches it; with SkipUnreadableDirectories, one the server refuses with PermissionDenied is left out instead, and the listing goes on with the entries after it.
Stopping early (leaving an await foreach) or cancelling stops the listings read ahead and waits for them to close their
handles.
Exceptions
- SftpException
The directory, or a subdirectory of a recursive listing, cannot be listed.