| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625 | #nullable enableusing System;using System.Collections.Generic;using System.Diagnostics;using System.Diagnostics.CodeAnalysis;using System.Globalization;using System.IO;using System.Net;using System.Runtime.CompilerServices;using System.Runtime.ExceptionServices;using System.Text;using System.Threading;using System.Threading.Tasks;using Renci.SshNet.Abstractions;using Renci.SshNet.Common;using Renci.SshNet.Sftp;namespace Renci.SshNet{    /// <summary>    /// Implementation of the SSH File Transfer Protocol (SFTP) over SSH.    /// </summary>    public class SftpClient : BaseClient, ISftpClient    {        private static readonly Encoding Utf8NoBOM = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);        /// <summary>        /// Holds the <see cref="ISftpSession"/> instance that is used to communicate to the        /// SFTP server.        /// </summary>        private ISftpSession? _sftpSession;        /// <summary>        /// Holds the operation timeout.        /// </summary>        private int _operationTimeout;        /// <summary>        /// Holds the size of the buffer.        /// </summary>        private uint _bufferSize;        /// <summary>        /// Gets or sets the operation timeout.        /// </summary>        /// <value>        /// The timeout to wait until an operation completes. The default value is negative        /// one (-1) milliseconds, which indicates an infinite timeout period.        /// </value>        /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> represents a value that is less than -1 or greater than <see cref="int.MaxValue"/> milliseconds.</exception>        public TimeSpan OperationTimeout        {            get            {                return TimeSpan.FromMilliseconds(_operationTimeout);            }            set            {                _operationTimeout = value.AsTimeout(nameof(OperationTimeout));                if (_sftpSession is { } sftpSession)                {                    sftpSession.OperationTimeout = _operationTimeout;                }            }        }        /// <summary>        /// Gets or sets the maximum size of the buffer in bytes.        /// </summary>        /// <value>        /// The size of the buffer. The default buffer size is 32768 bytes (32 KB).        /// </value>        /// <remarks>        /// <para>        /// For write operations, this limits the size of the payload for        /// individual SSH_FXP_WRITE messages. The actual size is always        /// capped at the maximum packet size supported by the peer        /// (minus the size of protocol fields).        /// </para>        /// <para>        /// For read operations, this controls the size of the payload which        /// is requested from the peer in a SSH_FXP_READ message. The peer        /// will send the requested number of bytes in a SSH_FXP_DATA message,        /// possibly split over multiple SSH_MSG_CHANNEL_DATA messages.        /// </para>        /// <para>        /// To optimize the size of the SSH packets sent by the peer,        /// the actual requested size will take into account the size of the        /// SSH_FXP_DATA protocol fields.        /// </para>        /// <para>        /// The size of the each individual SSH_FXP_DATA message is limited to the        /// local maximum packet size of the channel, which is set to <c>64 KB</c>        /// for SSH.NET. However, the peer can limit this even further.        /// </para>        /// </remarks>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public uint BufferSize        {            get            {                CheckDisposed();                return _bufferSize;            }            set            {                CheckDisposed();                _bufferSize = value;            }        }        /// <summary>        /// Gets a value indicating whether this client is connected to the server and        /// the SFTP session is open.        /// </summary>        /// <value>        /// <see langword="true"/> if this client is connected and the SFTP session is open; otherwise, <see langword="false"/>.        /// </value>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public override bool IsConnected        {            get            {                return base.IsConnected && _sftpSession?.IsOpen == true;            }        }        /// <summary>        /// Gets remote working directory.        /// </summary>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public string WorkingDirectory        {            get            {                CheckDisposed();                if (_sftpSession is null)                {                    throw new SshConnectionException("Client not connected.");                }                return _sftpSession.WorkingDirectory;            }        }        /// <summary>        /// Gets sftp protocol version.        /// </summary>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public int ProtocolVersion        {            get            {                CheckDisposed();                if (_sftpSession is null)                {                    throw new SshConnectionException("Client not connected.");                }                return (int)_sftpSession.ProtocolVersion;            }        }        /// <summary>        /// Gets the current SFTP session.        /// </summary>        /// <value>        /// The current SFTP session.        /// </value>        internal ISftpSession? SftpSession        {            get { return _sftpSession; }        }        #region Constructors        /// <summary>        /// Initializes a new instance of the <see cref="SftpClient"/> class.        /// </summary>        /// <param name="connectionInfo">The connection info.</param>        /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <see langword="null"/>.</exception>        public SftpClient(ConnectionInfo connectionInfo)            : this(connectionInfo, ownsConnectionInfo: false)        {        }        /// <summary>        /// Initializes a new instance of the <see cref="SftpClient"/> class.        /// </summary>        /// <param name="host">Connection host.</param>        /// <param name="port">Connection port.</param>        /// <param name="username">Authentication username.</param>        /// <param name="password">Authentication password.</param>        /// <exception cref="ArgumentNullException"><paramref name="password"/> is <see langword="null"/>.</exception>        /// <exception cref="ArgumentException"><paramref name="host"/> is invalid. <para>-or-</para> <paramref name="username"/> is <see langword="null"/> or contains only whitespace characters.</exception>        /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="IPEndPoint.MinPort"/> and <see cref="IPEndPoint.MaxPort"/>.</exception>        [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope", Justification = "Disposed in Dispose(bool) method.")]        public SftpClient(string host, int port, string username, string password)            : this(new PasswordConnectionInfo(host, port, username, password), ownsConnectionInfo: true)        {        }        /// <summary>        /// Initializes a new instance of the <see cref="SftpClient"/> class.        /// </summary>        /// <param name="host">Connection host.</param>        /// <param name="username">Authentication username.</param>        /// <param name="password">Authentication password.</param>        /// <exception cref="ArgumentNullException"><paramref name="password"/> is <see langword="null"/>.</exception>        /// <exception cref="ArgumentException"><paramref name="host"/> is invalid. <para>-or-</para> <paramref name="username"/> is <see langword="null"/> contains only whitespace characters.</exception>        public SftpClient(string host, string username, string password)            : this(host, ConnectionInfo.DefaultPort, username, password)        {        }        /// <summary>        /// Initializes a new instance of the <see cref="SftpClient"/> class.        /// </summary>        /// <param name="host">Connection host.</param>        /// <param name="port">Connection port.</param>        /// <param name="username">Authentication username.</param>        /// <param name="keyFiles">Authentication private key file(s) .</param>        /// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is <see langword="null"/>.</exception>        /// <exception cref="ArgumentException"><paramref name="host"/> is invalid. <para>-or-</para> <paramref name="username"/> is <see langword="null"/> or contains only whitespace characters.</exception>        /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="IPEndPoint.MinPort"/> and <see cref="IPEndPoint.MaxPort"/>.</exception>        [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope", Justification = "Disposed in Dispose(bool) method.")]        public SftpClient(string host, int port, string username, params IPrivateKeySource[] keyFiles)            : this(new PrivateKeyConnectionInfo(host, port, username, keyFiles), ownsConnectionInfo: true)        {        }        /// <summary>        /// Initializes a new instance of the <see cref="SftpClient"/> class.        /// </summary>        /// <param name="host">Connection host.</param>        /// <param name="username">Authentication username.</param>        /// <param name="keyFiles">Authentication private key file(s) .</param>        /// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is <see langword="null"/>.</exception>        /// <exception cref="ArgumentException"><paramref name="host"/> is invalid. <para>-or-</para> <paramref name="username"/> is <see langword="null"/> or contains only whitespace characters.</exception>        public SftpClient(string host, string username, params IPrivateKeySource[] keyFiles)            : this(host, ConnectionInfo.DefaultPort, username, keyFiles)        {        }        /// <summary>        /// Initializes a new instance of the <see cref="SftpClient"/> class.        /// </summary>        /// <param name="connectionInfo">The connection info.</param>        /// <param name="ownsConnectionInfo">Specified whether this instance owns the connection info.</param>        /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <see langword="null"/>.</exception>        /// <remarks>        /// If <paramref name="ownsConnectionInfo"/> is <see langword="true"/>, the connection info will be disposed when this        /// instance is disposed.        /// </remarks>        private SftpClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo)            : this(connectionInfo, ownsConnectionInfo, new ServiceFactory())        {        }        /// <summary>        /// Initializes a new instance of the <see cref="SftpClient"/> class.        /// </summary>        /// <param name="connectionInfo">The connection info.</param>        /// <param name="ownsConnectionInfo">Specified whether this instance owns the connection info.</param>        /// <param name="serviceFactory">The factory to use for creating new services.</param>        /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <see langword="null"/>.</exception>        /// <exception cref="ArgumentNullException"><paramref name="serviceFactory"/> is <see langword="null"/>.</exception>        /// <remarks>        /// If <paramref name="ownsConnectionInfo"/> is <see langword="true"/>, the connection info will be disposed when this        /// instance is disposed.        /// </remarks>        internal SftpClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory)            : base(connectionInfo, ownsConnectionInfo, serviceFactory)        {            _operationTimeout = Timeout.Infinite;            _bufferSize = 1024 * 32;        }        #endregion Constructors        /// <summary>        /// Changes remote directory to path.        /// </summary>        /// <param name="path">New directory path.</param>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPermissionDeniedException">Permission to change directory denied by remote host. <para>-or-</para> A SSH command was denied by the server.</exception>        /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>        /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public void ChangeDirectory(string path)        {            CheckDisposed();            ThrowHelper.ThrowIfNull(path);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            _sftpSession.ChangeDirectory(path);        }        /// <summary>        /// Asynchronously requests to change the current working directory to the specified path.        /// </summary>        /// <param name="path">The new working directory.</param>        /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>        /// <returns>A <see cref="Task"/> that tracks the asynchronous change working directory request.</returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPermissionDeniedException">Permission to change directory denied by remote host. <para>-or-</para> A SSH command was denied by the server.</exception>        /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>        /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public Task ChangeDirectoryAsync(string path, CancellationToken cancellationToken = default)        {            CheckDisposed();            ThrowHelper.ThrowIfNull(path);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            cancellationToken.ThrowIfCancellationRequested();            return _sftpSession.ChangeDirectoryAsync(path, cancellationToken);        }        /// <summary>        /// Changes permissions of file(s) to specified mode.        /// </summary>        /// <param name="path">File(s) path, may match multiple files.</param>        /// <param name="mode">The mode.</param>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPermissionDeniedException">Permission to change permission on the path(s) was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>        /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>        /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public void ChangePermissions(string path, short mode)        {            var file = Get(path);            file.SetPermissions(mode);        }        /// <summary>        /// Creates remote directory specified by path.        /// </summary>        /// <param name="path">Directory path to create.</param>        /// <exception cref="ArgumentException"><paramref name="path"/> is <see langword="null"/> or contains only whitespace characters.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPermissionDeniedException">Permission to create the directory was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>        /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public void CreateDirectory(string path)        {            CheckDisposed();            ThrowHelper.ThrowIfNullOrWhiteSpace(path);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            var fullPath = _sftpSession.GetCanonicalPath(path);            _sftpSession.RequestMkDir(fullPath);        }        /// <summary>        /// Asynchronously requests to create a remote directory specified by path.        /// </summary>        /// <param name="path">Directory path to create.</param>        /// <param name="cancellationToken">The <see cref="CancellationToken"/> to observe.</param>        /// <returns>A <see cref="Task"/> that represents the asynchronous create directory operation.</returns>        /// <exception cref="ArgumentException"><paramref name="path"/> is <see langword="null"/> or contains only whitespace characters.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPermissionDeniedException">Permission to create the directory was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>        /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public async Task CreateDirectoryAsync(string path, CancellationToken cancellationToken = default)        {            CheckDisposed();            ThrowHelper.ThrowIfNullOrWhiteSpace(path);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            var fullPath = await _sftpSession.GetCanonicalPathAsync(path, cancellationToken).ConfigureAwait(false);            await _sftpSession.RequestMkDirAsync(fullPath, cancellationToken).ConfigureAwait(false);        }        /// <summary>        /// Deletes remote directory specified by path.        /// </summary>        /// <param name="path">Directory to be deleted path.</param>        /// <exception cref="ArgumentException"><paramref name="path"/> is <see langword="null"/> or contains only whitespace characters.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>        /// <exception cref="SftpPermissionDeniedException">Permission to delete the directory was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>        /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public void DeleteDirectory(string path)        {            CheckDisposed();            ThrowHelper.ThrowIfNullOrWhiteSpace(path);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            var fullPath = _sftpSession.GetCanonicalPath(path);            _sftpSession.RequestRmDir(fullPath);        }        /// <inheritdoc />        public async Task DeleteDirectoryAsync(string path, CancellationToken cancellationToken = default)        {            CheckDisposed();            ThrowHelper.ThrowIfNullOrWhiteSpace(path);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            cancellationToken.ThrowIfCancellationRequested();            var fullPath = await _sftpSession.GetCanonicalPathAsync(path, cancellationToken).ConfigureAwait(false);            await _sftpSession.RequestRmDirAsync(fullPath, cancellationToken).ConfigureAwait(false);        }        /// <summary>        /// Deletes remote file specified by path.        /// </summary>        /// <param name="path">File to be deleted path.</param>        /// <exception cref="ArgumentException"><paramref name="path"/> is <see langword="null"/> or contains only whitespace characters.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>        /// <exception cref="SftpPermissionDeniedException">Permission to delete the file was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>        /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public void DeleteFile(string path)        {            CheckDisposed();            ThrowHelper.ThrowIfNullOrWhiteSpace(path);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            var fullPath = _sftpSession.GetCanonicalPath(path);            _sftpSession.RequestRemove(fullPath);        }        /// <inheritdoc />        public async Task DeleteFileAsync(string path, CancellationToken cancellationToken)        {            CheckDisposed();            ThrowHelper.ThrowIfNullOrWhiteSpace(path);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            cancellationToken.ThrowIfCancellationRequested();            var fullPath = await _sftpSession.GetCanonicalPathAsync(path, cancellationToken).ConfigureAwait(false);            await _sftpSession.RequestRemoveAsync(fullPath, cancellationToken).ConfigureAwait(false);        }        /// <summary>        /// Renames remote file from old path to new path.        /// </summary>        /// <param name="oldPath">Path to the old file location.</param>        /// <param name="newPath">Path to the new file location.</param>        /// <exception cref="ArgumentNullException"><paramref name="oldPath"/> is <see langword="null"/>. <para>-or-</para> or <paramref name="newPath"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPermissionDeniedException">Permission to rename the file was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>        /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public void RenameFile(string oldPath, string newPath)        {            RenameFile(oldPath, newPath, isPosix: false);        }        /// <summary>        /// Renames remote file from old path to new path.        /// </summary>        /// <param name="oldPath">Path to the old file location.</param>        /// <param name="newPath">Path to the new file location.</param>        /// <param name="isPosix">if set to <see langword="true"/> then perform a posix rename.</param>        /// <exception cref="ArgumentNullException"><paramref name="oldPath" /> is <see langword="null"/>. <para>-or-</para> or <paramref name="newPath" /> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPermissionDeniedException">Permission to rename the file was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>        /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public void RenameFile(string oldPath, string newPath, bool isPosix)        {            CheckDisposed();            ThrowHelper.ThrowIfNull(oldPath);            ThrowHelper.ThrowIfNull(newPath);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            var oldFullPath = _sftpSession.GetCanonicalPath(oldPath);            var newFullPath = _sftpSession.GetCanonicalPath(newPath);            if (isPosix)            {                _sftpSession.RequestPosixRename(oldFullPath, newFullPath);            }            else            {                _sftpSession.RequestRename(oldFullPath, newFullPath);            }        }        /// <summary>        /// Asynchronously renames remote file from old path to new path.        /// </summary>        /// <param name="oldPath">Path to the old file location.</param>        /// <param name="newPath">Path to the new file location.</param>        /// <param name="cancellationToken">The <see cref="CancellationToken"/> to observe.</param>        /// <returns>A <see cref="Task"/> that represents the asynchronous rename operation.</returns>        /// <exception cref="ArgumentNullException"><paramref name="oldPath"/> is <see langword="null"/>. <para>-or-</para> or <paramref name="newPath"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPermissionDeniedException">Permission to rename the file was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>        /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public async Task RenameFileAsync(string oldPath, string newPath, CancellationToken cancellationToken)        {            CheckDisposed();            ThrowHelper.ThrowIfNull(oldPath);            ThrowHelper.ThrowIfNull(newPath);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            cancellationToken.ThrowIfCancellationRequested();            var oldFullPath = await _sftpSession.GetCanonicalPathAsync(oldPath, cancellationToken).ConfigureAwait(false);            var newFullPath = await _sftpSession.GetCanonicalPathAsync(newPath, cancellationToken).ConfigureAwait(false);            await _sftpSession.RequestRenameAsync(oldFullPath, newFullPath, cancellationToken).ConfigureAwait(false);        }        /// <summary>        /// Creates a symbolic link from old path to new path.        /// </summary>        /// <param name="path">The old path.</param>        /// <param name="linkPath">The new path.</param>        /// <exception cref="ArgumentException"><paramref name="path"/> is <see langword="null"/>. <para>-or-</para> <paramref name="linkPath"/> is <see langword="null"/> or contains only whitespace characters.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPermissionDeniedException">Permission to create the symbolic link was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>        /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public void SymbolicLink(string path, string linkPath)        {            CheckDisposed();            ThrowHelper.ThrowIfNullOrWhiteSpace(path);            ThrowHelper.ThrowIfNullOrWhiteSpace(linkPath);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            var fullPath = _sftpSession.GetCanonicalPath(path);            var linkFullPath = _sftpSession.GetCanonicalPath(linkPath);            _sftpSession.RequestSymLink(fullPath, linkFullPath);        }        /// <summary>        /// Retrieves list of files in remote directory.        /// </summary>        /// <param name="path">The path.</param>        /// <param name="listCallback">The list callback.</param>        /// <returns>        /// A list of files.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPermissionDeniedException">Permission to list the contents of the directory was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>        /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public IEnumerable<ISftpFile> ListDirectory(string path, Action<int>? listCallback = null)        {            CheckDisposed();            return InternalListDirectory(path, asyncResult: null, listCallback);        }        /// <summary>        /// Asynchronously enumerates the files in remote directory.        /// </summary>        /// <param name="path">The path.</param>        /// <param name="cancellationToken">The <see cref="CancellationToken"/> to observe.</param>        /// <returns>        /// An <see cref="IAsyncEnumerable{T}"/> of <see cref="ISftpFile"/> that represents the asynchronous enumeration operation.        /// The enumeration contains an async stream of <see cref="ISftpFile"/> for the files in the directory specified by <paramref name="path" />.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPermissionDeniedException">Permission to list the contents of the directory was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>        /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public async IAsyncEnumerable<ISftpFile> ListDirectoryAsync(string path, [EnumeratorCancellation] CancellationToken cancellationToken)        {            CheckDisposed();            ThrowHelper.ThrowIfNull(path);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            cancellationToken.ThrowIfCancellationRequested();            var fullPath = await _sftpSession.GetCanonicalPathAsync(path, cancellationToken).ConfigureAwait(false);            var handle = await _sftpSession.RequestOpenDirAsync(fullPath, cancellationToken).ConfigureAwait(false);            try            {                var basePath = (fullPath[fullPath.Length - 1] == '/') ?                    fullPath :                    fullPath + '/';                while (true)                {                    var files = await _sftpSession.RequestReadDirAsync(handle, cancellationToken).ConfigureAwait(false);                    if (files is null)                    {                        break;                    }                    foreach (var file in files)                    {                        yield return new SftpFile(_sftpSession, basePath + file.Key, file.Value);                    }                }            }            finally            {                await _sftpSession.RequestCloseAsync(handle, cancellationToken).ConfigureAwait(false);            }        }        /// <summary>        /// Begins an asynchronous operation of retrieving list of files in remote directory.        /// </summary>        /// <param name="path">The path.</param>        /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>        /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests.</param>        /// <param name="listCallback">The list callback.</param>        /// <returns>        /// An <see cref="IAsyncResult" /> that references the asynchronous operation.        /// </returns>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public IAsyncResult BeginListDirectory(string path, AsyncCallback? asyncCallback, object? state, Action<int>? listCallback = null)        {            CheckDisposed();            var asyncResult = new SftpListDirectoryAsyncResult(asyncCallback, state);            ThreadAbstraction.ExecuteThread(() =>            {                try                {                    var result = InternalListDirectory(path, asyncResult, listCallback);                    asyncResult.SetAsCompleted(result, completedSynchronously: false);                }                catch (Exception exp)                {                    asyncResult.SetAsCompleted(exp, completedSynchronously: false);                }            });            return asyncResult;        }        /// <summary>        /// Ends an asynchronous operation of retrieving list of files in remote directory.        /// </summary>        /// <param name="asyncResult">The pending asynchronous SFTP request.</param>        /// <returns>        /// A list of files.        /// </returns>        /// <exception cref="ArgumentException">The <see cref="IAsyncResult"/> object did not come from the corresponding async method on this type.<para>-or-</para><see cref="EndListDirectory(IAsyncResult)"/> was called multiple times with the same <see cref="IAsyncResult"/>.</exception>        public IEnumerable<ISftpFile> EndListDirectory(IAsyncResult asyncResult)        {            if (asyncResult is not SftpListDirectoryAsyncResult ar || ar.EndInvokeCalled)            {                throw new ArgumentException("Either the IAsyncResult object did not come from the corresponding async method on this type, or EndExecute was called multiple times with the same IAsyncResult.");            }            // Wait for operation to complete, then return result or throw exception            return ar.EndInvoke();        }        /// <summary>        /// Gets reference to remote file or directory.        /// </summary>        /// <param name="path">The path.</param>        /// <returns>        /// A reference to <see cref="ISftpFile"/> file object.        /// </returns>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>        /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null"/>.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public ISftpFile Get(string path)        {            CheckDisposed();            ThrowHelper.ThrowIfNull(path);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            var fullPath = _sftpSession.GetCanonicalPath(path);            var attributes = _sftpSession.RequestLStat(fullPath);            return new SftpFile(_sftpSession, fullPath, attributes);        }        /// <summary>        /// Gets reference to remote file or directory.        /// </summary>        /// <param name="path">The path.</param>        /// <param name="cancellationToken">The <see cref="CancellationToken"/> to observe.</param>        /// <returns>        /// A <see cref="Task{ISftpFile}"/> that represents the get operation.        /// The task result contains the reference to <see cref="ISftpFile"/> file object.        /// </returns>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>        /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null"/>.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public async Task<ISftpFile> GetAsync(string path, CancellationToken cancellationToken)        {            CheckDisposed();            ThrowHelper.ThrowIfNull(path);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            cancellationToken.ThrowIfCancellationRequested();            var fullPath = await _sftpSession.GetCanonicalPathAsync(path, cancellationToken).ConfigureAwait(false);            var attributes = await _sftpSession.RequestLStatAsync(fullPath, cancellationToken).ConfigureAwait(false);            return new SftpFile(_sftpSession, fullPath, attributes);        }        /// <summary>        /// Checks whether file or directory exists.        /// </summary>        /// <param name="path">The path.</param>        /// <returns>        /// <see langword="true"/> if directory or file exists; otherwise <see langword="false"/>.        /// </returns>        /// <exception cref="ArgumentException"><paramref name="path"/> is <see langword="null"/> or contains only whitespace characters.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPermissionDeniedException">Permission to perform the operation was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>        /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public bool Exists(string path)        {            CheckDisposed();            ThrowHelper.ThrowIfNullOrWhiteSpace(path);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            var fullPath = _sftpSession.GetCanonicalPath(path);            /*             * Using SSH_FXP_REALPATH is not an alternative as the SFTP specification has not always             * been clear on how the server should respond when the specified path is not present on             * the server:             *             * SSH 1 to 4:             * No mention of how the server should respond if the path is not present on the server.             *             * SSH 5:             * The server SHOULD fail the request if the path is not present on the server.             *             * SSH 6:             * Draft 06: The server SHOULD fail the request if the path is not present on the server.             * Draft 07 to 13: The server MUST NOT fail the request if the path does not exist.             *             * Note that SSH 6 (draft 06 and forward) allows for more control options, but we             * currently only support up to v3.             */            try            {                _ = _sftpSession.RequestLStat(fullPath);                return true;            }            catch (SftpPathNotFoundException)            {                return false;            }        }        /// <summary>        /// Checks whether file or directory exists.        /// </summary>        /// <param name="path">The path.</param>        /// <param name="cancellationToken">The <see cref="CancellationToken"/> to observe.</param>        /// <returns>        /// A <see cref="Task{T}"/> that represents the exists operation.        /// The task result contains <see langword="true"/> if directory or file exists; otherwise <see langword="false"/>.        /// </returns>        /// <exception cref="ArgumentException"><paramref name="path"/> is <see langword="null"/> or contains only whitespace characters.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPermissionDeniedException">Permission to perform the operation was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>        /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public async Task<bool> ExistsAsync(string path, CancellationToken cancellationToken = default)        {            CheckDisposed();            ThrowHelper.ThrowIfNullOrWhiteSpace(path);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            cancellationToken.ThrowIfCancellationRequested();            var fullPath = await _sftpSession.GetCanonicalPathAsync(path, cancellationToken).ConfigureAwait(false);            /*             * Using SSH_FXP_REALPATH is not an alternative as the SFTP specification has not always             * been clear on how the server should respond when the specified path is not present on             * the server:             *             * SSH 1 to 4:             * No mention of how the server should respond if the path is not present on the server.             *             * SSH 5:             * The server SHOULD fail the request if the path is not present on the server.             *             * SSH 6:             * Draft 06: The server SHOULD fail the request if the path is not present on the server.             * Draft 07 to 13: The server MUST NOT fail the request if the path does not exist.             *             * Note that SSH 6 (draft 06 and forward) allows for more control options, but we             * currently only support up to v3.             */            try            {                _ = await _sftpSession.RequestLStatAsync(fullPath, cancellationToken).ConfigureAwait(false);                return true;            }            catch (SftpPathNotFoundException)            {                return false;            }        }        /// <inheritdoc />        public void DownloadFile(string path, Stream output, Action<ulong>? downloadCallback = null)        {            CheckDisposed();            InternalDownloadFile(path, output, asyncResult: null, downloadCallback);        }        /// <inheritdoc />        public Task DownloadFileAsync(string path, Stream output, CancellationToken cancellationToken = default)        {            CheckDisposed();            return InternalDownloadFileAsync(path, output, cancellationToken);        }        /// <summary>        /// Begins an asynchronous file downloading into the stream.        /// </summary>        /// <param name="path">The path.</param>        /// <param name="output">The output.</param>        /// <returns>        /// An <see cref="IAsyncResult" /> that references the asynchronous operation.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="output" /> is <see langword="null"/>.</exception>        /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whitespace characters.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPermissionDeniedException">Permission to perform the operation was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>        /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        /// <remarks>        /// Method calls made by this method to <paramref name="output" />, may under certain conditions result in exceptions thrown by the stream.        /// </remarks>        public IAsyncResult BeginDownloadFile(string path, Stream output)        {            return BeginDownloadFile(path, output, asyncCallback: null, state: null);        }        /// <summary>        /// Begins an asynchronous file downloading into the stream.        /// </summary>        /// <param name="path">The path.</param>        /// <param name="output">The output.</param>        /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>        /// <returns>        /// An <see cref="IAsyncResult" /> that references the asynchronous operation.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="output" /> is <see langword="null"/>.</exception>        /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whitespace characters.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPermissionDeniedException">Permission to perform the operation was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>        /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        /// <remarks>        /// Method calls made by this method to <paramref name="output" />, may under certain conditions result in exceptions thrown by the stream.        /// </remarks>        public IAsyncResult BeginDownloadFile(string path, Stream output, AsyncCallback? asyncCallback)        {            return BeginDownloadFile(path, output, asyncCallback, state: null);        }        /// <summary>        /// Begins an asynchronous file downloading into the stream.        /// </summary>        /// <param name="path">The path.</param>        /// <param name="output">The output.</param>        /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>        /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests.</param>        /// <param name="downloadCallback">The download callback.</param>        /// <returns>        /// An <see cref="IAsyncResult" /> that references the asynchronous operation.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="output" /> is <see langword="null"/>.</exception>        /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whitespace characters.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        /// <remarks>        /// Method calls made by this method to <paramref name="output" />, may under certain conditions result in exceptions thrown by the stream.        /// </remarks>        public IAsyncResult BeginDownloadFile(string path, Stream output, AsyncCallback? asyncCallback, object? state, Action<ulong>? downloadCallback = null)        {            CheckDisposed();            ThrowHelper.ThrowIfNullOrWhiteSpace(path);            ThrowHelper.ThrowIfNull(output);            var asyncResult = new SftpDownloadAsyncResult(asyncCallback, state);            ThreadAbstraction.ExecuteThread(() =>            {                try                {                    InternalDownloadFile(path, output, asyncResult, downloadCallback);                    asyncResult.SetAsCompleted(exception: null, completedSynchronously: false);                }                catch (Exception exp)                {                    asyncResult.SetAsCompleted(exp, completedSynchronously: false);                }            });            return asyncResult;        }        /// <summary>        /// Ends an asynchronous file downloading into the stream.        /// </summary>        /// <param name="asyncResult">The pending asynchronous SFTP request.</param>        /// <exception cref="ArgumentException">The <see cref="IAsyncResult"/> object did not come from the corresponding async method on this type.<para>-or-</para><see cref="EndDownloadFile(IAsyncResult)"/> was called multiple times with the same <see cref="IAsyncResult"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPermissionDeniedException">Permission to perform the operation was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>        /// <exception cref="SftpPathNotFoundException">The path was not found on the remote host.</exception>        /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>        public void EndDownloadFile(IAsyncResult asyncResult)        {            if (asyncResult is not SftpDownloadAsyncResult ar || ar.EndInvokeCalled)            {                throw new ArgumentException("Either the IAsyncResult object did not come from the corresponding async method on this type, or EndExecute was called multiple times with the same IAsyncResult.");            }            // Wait for operation to complete, then return result or throw exception            ar.EndInvoke();        }        /// <inheritdoc/>        public void UploadFile(Stream input, string path, Action<ulong>? uploadCallback = null)        {            UploadFile(input, path, canOverride: true, uploadCallback);        }        /// <inheritdoc/>        public void UploadFile(Stream input, string path, bool canOverride, Action<ulong>? uploadCallback = null)        {            CheckDisposed();            var flags = Flags.Write | Flags.Truncate;            if (canOverride)            {                flags |= Flags.CreateNewOrOpen;            }            else            {                flags |= Flags.CreateNew;            }            InternalUploadFile(input, path, flags, asyncResult: null, uploadCallback);        }        /// <inheritdoc />        public Task UploadFileAsync(Stream input, string path, CancellationToken cancellationToken = default)        {            CheckDisposed();            return InternalUploadFileAsync(input, path, cancellationToken);        }        /// <summary>        /// Begins an asynchronous uploading the stream into remote file.        /// </summary>        /// <param name="input">Data input stream.</param>        /// <param name="path">Remote file path.</param>        /// <returns>        /// An <see cref="IAsyncResult" /> that references the asynchronous operation.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="input" /> is <see langword="null"/>.</exception>        /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whitespace characters.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPermissionDeniedException">Permission to list the contents of the directory was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>        /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        /// <remarks>        /// <para>        /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in exceptions thrown by the stream.        /// </para>        /// <para>        /// If the remote file already exists, it is overwritten and truncated.        /// </para>        /// </remarks>        public IAsyncResult BeginUploadFile(Stream input, string path)        {            return BeginUploadFile(input, path, canOverride: true, asyncCallback: null, state: null);        }        /// <summary>        /// Begins an asynchronous uploading the stream into remote file.        /// </summary>        /// <param name="input">Data input stream.</param>        /// <param name="path">Remote file path.</param>        /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>        /// <returns>        /// An <see cref="IAsyncResult" /> that references the asynchronous operation.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="input" /> is <see langword="null"/>.</exception>        /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whitespace characters.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPermissionDeniedException">Permission to list the contents of the directory was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>        /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        /// <remarks>        /// <para>        /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in exceptions thrown by the stream.        /// </para>        /// <para>        /// If the remote file already exists, it is overwritten and truncated.        /// </para>        /// </remarks>        public IAsyncResult BeginUploadFile(Stream input, string path, AsyncCallback? asyncCallback)        {            return BeginUploadFile(input, path, canOverride: true, asyncCallback, state: null);        }        /// <summary>        /// Begins an asynchronous uploading the stream into remote file.        /// </summary>        /// <param name="input">Data input stream.</param>        /// <param name="path">Remote file path.</param>        /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>        /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests.</param>        /// <param name="uploadCallback">The upload callback.</param>        /// <returns>        /// An <see cref="IAsyncResult" /> that references the asynchronous operation.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="input" /> is <see langword="null"/>.</exception>        /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whitespace characters.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPermissionDeniedException">Permission to list the contents of the directory was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>        /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        /// <remarks>        /// <para>        /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in exceptions thrown by the stream.        /// </para>        /// <para>        /// If the remote file already exists, it is overwritten and truncated.        /// </para>        /// </remarks>        public IAsyncResult BeginUploadFile(Stream input, string path, AsyncCallback? asyncCallback, object? state, Action<ulong>? uploadCallback = null)        {            return BeginUploadFile(input, path, canOverride: true, asyncCallback, state, uploadCallback);        }        /// <summary>        /// Begins an asynchronous uploading the stream into remote file.        /// </summary>        /// <param name="input">Data input stream.</param>        /// <param name="path">Remote file path.</param>        /// <param name="canOverride">Specified whether an existing file can be overwritten.</param>        /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>        /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests.</param>        /// <param name="uploadCallback">The upload callback.</param>        /// <returns>        /// An <see cref="IAsyncResult" /> that references the asynchronous operation.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="input" /> is <see langword="null"/>.</exception>        /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whitespace characters.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        /// <remarks>        /// <para>        /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in exceptions thrown by the stream.        /// </para>        /// <para>        /// When <paramref name="path"/> refers to an existing file, set <paramref name="canOverride"/> to <see langword="true"/> to overwrite and truncate that file.        /// If <paramref name="canOverride"/> is <see langword="false"/>, the upload will fail and <see cref="EndUploadFile(IAsyncResult)"/> will throw an        /// <see cref="SshException"/>.        /// </para>        /// </remarks>        public IAsyncResult BeginUploadFile(Stream input, string path, bool canOverride, AsyncCallback? asyncCallback, object? state, Action<ulong>? uploadCallback = null)        {            CheckDisposed();            ThrowHelper.ThrowIfNull(input);            ThrowHelper.ThrowIfNullOrWhiteSpace(path);            var flags = Flags.Write | Flags.Truncate;            if (canOverride)            {                flags |= Flags.CreateNewOrOpen;            }            else            {                flags |= Flags.CreateNew;            }            var asyncResult = new SftpUploadAsyncResult(asyncCallback, state);            ThreadAbstraction.ExecuteThread(() =>            {                try                {                    InternalUploadFile(input, path, flags, asyncResult, uploadCallback);                    asyncResult.SetAsCompleted(exception: null, completedSynchronously: false);                }                catch (Exception exp)                {                    asyncResult.SetAsCompleted(exception: exp, completedSynchronously: false);                }            });            return asyncResult;        }        /// <summary>        /// Ends an asynchronous uploading the stream into remote file.        /// </summary>        /// <param name="asyncResult">The pending asynchronous SFTP request.</param>        /// <exception cref="ArgumentException">The <see cref="IAsyncResult"/> object did not come from the corresponding async method on this type.<para>-or-</para><see cref="EndUploadFile(IAsyncResult)"/> was called multiple times with the same <see cref="IAsyncResult"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException">The directory of the file was not found on the remote host.</exception>        /// <exception cref="SftpPermissionDeniedException">Permission to upload the file was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>        /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>        public void EndUploadFile(IAsyncResult asyncResult)        {            if (asyncResult is not SftpUploadAsyncResult ar || ar.EndInvokeCalled)            {                throw new ArgumentException("Either the IAsyncResult object did not come from the corresponding async method on this type, or EndExecute was called multiple times with the same IAsyncResult.");            }            // Wait for operation to complete, then return result or throw exception            ar.EndInvoke();        }        /// <summary>        /// Gets status using statvfs@openssh.com request.        /// </summary>        /// <param name="path">The path.</param>        /// <returns>        /// A <see cref="SftpFileSystemInformation"/> instance that contains file status information.        /// </returns>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null"/>.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public SftpFileSystemInformation GetStatus(string path)        {            CheckDisposed();            ThrowHelper.ThrowIfNull(path);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            var fullPath = _sftpSession.GetCanonicalPath(path);            return _sftpSession.RequestStatVfs(fullPath);        }        /// <summary>        /// Asynchronously gets status using statvfs@openssh.com request.        /// </summary>        /// <param name="path">The path.</param>        /// <param name="cancellationToken">The <see cref="CancellationToken"/> to observe.</param>        /// <returns>        /// A <see cref="Task{SftpFileSystemInformation}"/> that represents the status operation.        /// The task result contains the <see cref="SftpFileSystemInformation"/> instance that contains file status information.        /// </returns>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null"/>.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public async Task<SftpFileSystemInformation> GetStatusAsync(string path, CancellationToken cancellationToken)        {            CheckDisposed();            ThrowHelper.ThrowIfNull(path);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            cancellationToken.ThrowIfCancellationRequested();            var fullPath = await _sftpSession.GetCanonicalPathAsync(path, cancellationToken).ConfigureAwait(false);            return await _sftpSession.RequestStatVfsAsync(fullPath, cancellationToken).ConfigureAwait(false);        }        #region File Methods        /// <summary>        /// Appends lines to a file, creating the file if it does not already exist.        /// </summary>        /// <param name="path">The file to append the lines to. The file is created if it does not already exist.</param>        /// <param name="contents">The lines to append to the file.</param>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>. <para>-or-</para> <paramref name="contents"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        /// <remarks>        /// The characters are written to the file using UTF-8 encoding without a byte-order mark (BOM).        /// </remarks>        public void AppendAllLines(string path, IEnumerable<string> contents)        {            CheckDisposed();            ThrowHelper.ThrowIfNull(contents);            using (var stream = AppendText(path))            {                foreach (var line in contents)                {                    stream.WriteLine(line);                }            }        }        /// <summary>        /// Appends lines to a file by using a specified encoding, creating the file if it does not already exist.        /// </summary>        /// <param name="path">The file to append the lines to. The file is created if it does not already exist.</param>        /// <param name="contents">The lines to append to the file.</param>        /// <param name="encoding">The character encoding to use.</param>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>. <para>-or-</para> <paramref name="contents"/> is <see langword="null"/>. <para>-or-</para> <paramref name="encoding"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public void AppendAllLines(string path, IEnumerable<string> contents, Encoding encoding)        {            CheckDisposed();            ThrowHelper.ThrowIfNull(contents);            using (var stream = AppendText(path, encoding))            {                foreach (var line in contents)                {                    stream.WriteLine(line);                }            }        }        /// <summary>        /// Appends the specified string to the file, creating the file if it does not already exist.        /// </summary>        /// <param name="path">The file to append the specified string to.</param>        /// <param name="contents">The string to append to the file.</param>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>. <para>-or-</para> <paramref name="contents"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        /// <remarks>        /// The characters are written to the file using UTF-8 encoding without a Byte-Order Mark (BOM).        /// </remarks>        public void AppendAllText(string path, string contents)        {            using (var stream = AppendText(path))            {                stream.Write(contents);            }        }        /// <summary>        /// Appends the specified string to the file, creating the file if it does not already exist.        /// </summary>        /// <param name="path">The file to append the specified string to.</param>        /// <param name="contents">The string to append to the file.</param>        /// <param name="encoding">The character encoding to use.</param>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>. <para>-or-</para> <paramref name="contents"/> is <see langword="null"/>. <para>-or-</para> <paramref name="encoding"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public void AppendAllText(string path, string contents, Encoding encoding)        {            using (var stream = AppendText(path, encoding))            {                stream.Write(contents);            }        }        /// <summary>        /// Creates a <see cref="StreamWriter"/> that appends UTF-8 encoded text to the specified file,        /// creating the file if it does not already exist.        /// </summary>        /// <param name="path">The path to the file to append to.</param>        /// <returns>        /// A <see cref="StreamWriter"/> that appends text to a file using UTF-8 encoding without a        /// Byte-Order Mark (BOM).        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public StreamWriter AppendText(string path)        {            return AppendText(path, Utf8NoBOM);        }        /// <summary>        /// Creates a <see cref="StreamWriter"/> that appends text to a file using the specified        /// encoding, creating the file if it does not already exist.        /// </summary>        /// <param name="path">The path to the file to append to.</param>        /// <param name="encoding">The character encoding to use.</param>        /// <returns>        /// A <see cref="StreamWriter"/> that appends text to a file using the specified encoding.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>. <para>-or-</para> <paramref name="encoding"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public StreamWriter AppendText(string path, Encoding encoding)        {            CheckDisposed();            ThrowHelper.ThrowIfNull(encoding);            return new StreamWriter(new SftpFileStream(_sftpSession, path, FileMode.Append, FileAccess.Write, (int)_bufferSize), encoding);        }        /// <summary>        /// Creates or overwrites a file in the specified path.        /// </summary>        /// <param name="path">The path and name of the file to create.</param>        /// <returns>        /// A <see cref="SftpFileStream"/> that provides read/write access to the file specified in path.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        /// <remarks>        /// If the target file already exists, it is first truncated to zero bytes.        /// </remarks>        public SftpFileStream Create(string path)        {            CheckDisposed();            return new SftpFileStream(_sftpSession, path, FileMode.Create, FileAccess.ReadWrite, (int)_bufferSize);        }        /// <summary>        /// Creates or overwrites the specified file.        /// </summary>        /// <param name="path">The path and name of the file to create.</param>        /// <param name="bufferSize">The maximum number of bytes buffered for reads and writes to the file.</param>        /// <returns>        /// A <see cref="SftpFileStream"/> that provides read/write access to the file specified in path.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        /// <remarks>        /// If the target file already exists, it is first truncated to zero bytes.        /// </remarks>        public SftpFileStream Create(string path, int bufferSize)        {            CheckDisposed();            return new SftpFileStream(_sftpSession, path, FileMode.Create, FileAccess.ReadWrite, bufferSize);        }        /// <summary>        /// Creates or opens a file for writing UTF-8 encoded text.        /// </summary>        /// <param name="path">The file to be opened for writing.</param>        /// <returns>        /// A <see cref="StreamWriter"/> that writes text to a file using UTF-8 encoding without        /// a Byte-Order Mark (BOM).        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        /// <remarks>        /// <para>        /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes.        /// </para>        /// <para>        /// If the target file does not exist, it is created.        /// </para>        /// </remarks>        public StreamWriter CreateText(string path)        {            return CreateText(path, Utf8NoBOM);        }        /// <summary>        /// Creates or opens a file for writing text using the specified encoding.        /// </summary>        /// <param name="path">The file to be opened for writing.</param>        /// <param name="encoding">The character encoding to use.</param>        /// <returns>        /// A <see cref="StreamWriter"/> that writes to a file using the specified encoding.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        /// <remarks>        /// <para>        /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes.        /// </para>        /// <para>        /// If the target file does not exist, it is created.        /// </para>        /// </remarks>        public StreamWriter CreateText(string path, Encoding encoding)        {            CheckDisposed();            return new StreamWriter(OpenWrite(path), encoding);        }        /// <summary>        /// Deletes the specified file or directory.        /// </summary>        /// <param name="path">The name of the file or directory to be deleted. Wildcard characters are not supported.</param>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public void Delete(string path)        {            var file = Get(path);            file.Delete();        }        /// <inheritdoc />        public async Task DeleteAsync(string path, CancellationToken cancellationToken = default)        {            var file = await GetAsync(path, cancellationToken).ConfigureAwait(false);            await file.DeleteAsync(cancellationToken).ConfigureAwait(false);        }        /// <summary>        /// Returns the date and time the specified file or directory was last accessed.        /// </summary>        /// <param name="path">The file or directory for which to obtain access date and time information.</param>        /// <returns>        /// A <see cref="DateTime"/> structure set to the date and time that the specified file or directory was last accessed.        /// This value is expressed in local time.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public DateTime GetLastAccessTime(string path)        {            var file = Get(path);            return file.LastAccessTime;        }        /// <summary>        /// Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last accessed.        /// </summary>        /// <param name="path">The file or directory for which to obtain access date and time information.</param>        /// <returns>        /// A <see cref="DateTime"/> structure set to the date and time that the specified file or directory was last accessed.        /// This value is expressed in UTC time.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public DateTime GetLastAccessTimeUtc(string path)        {            var lastAccessTime = GetLastAccessTime(path);            return lastAccessTime.ToUniversalTime();        }        /// <summary>        /// Returns the date and time the specified file or directory was last written to.        /// </summary>        /// <param name="path">The file or directory for which to obtain write date and time information.</param>        /// <returns>        /// A <see cref="DateTime"/> structure set to the date and time that the specified file or directory was last written to.        /// This value is expressed in local time.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public DateTime GetLastWriteTime(string path)        {            var file = Get(path);            return file.LastWriteTime;        }        /// <summary>        /// Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last written to.        /// </summary>        /// <param name="path">The file or directory for which to obtain write date and time information.</param>        /// <returns>        /// A <see cref="DateTime"/> structure set to the date and time that the specified file or directory was last written to.        /// This value is expressed in UTC time.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public DateTime GetLastWriteTimeUtc(string path)        {            var lastWriteTime = GetLastWriteTime(path);            return lastWriteTime.ToUniversalTime();        }        /// <summary>        /// Opens a <see cref="SftpFileStream"/> on the specified path with read/write access.        /// </summary>        /// <param name="path">The file to open.</param>        /// <param name="mode">A <see cref="FileMode"/> value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.</param>        /// <returns>        /// An unshared <see cref="SftpFileStream"/> that provides access to the specified file, with the specified mode and read/write access.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public SftpFileStream Open(string path, FileMode mode)        {            return Open(path, mode, FileAccess.ReadWrite);        }        /// <summary>        /// Opens a <see cref="SftpFileStream"/> on the specified path, with the specified mode and access.        /// </summary>        /// <param name="path">The file to open.</param>        /// <param name="mode">A <see cref="FileMode"/> value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.</param>        /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the file.</param>        /// <returns>        /// An unshared <see cref="SftpFileStream"/> that provides access to the specified file, with the specified mode and access.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public SftpFileStream Open(string path, FileMode mode, FileAccess access)        {            CheckDisposed();            return new SftpFileStream(_sftpSession, path, mode, access, (int)_bufferSize);        }        /// <summary>        /// Asynchronously opens a <see cref="SftpFileStream"/> on the specified path, with the specified mode and access.        /// </summary>        /// <param name="path">The file to open.</param>        /// <param name="mode">A <see cref="FileMode"/> value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.</param>        /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the file.</param>        /// <param name="cancellationToken">The <see cref="CancellationToken"/> to observe.</param>        /// <returns>        /// A <see cref="Task{SftpFileStream}"/> that represents the asynchronous open operation.        /// The task result contains the <see cref="SftpFileStream"/> that provides access to the specified file, with the specified mode and access.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public Task<SftpFileStream> OpenAsync(string path, FileMode mode, FileAccess access, CancellationToken cancellationToken)        {            CheckDisposed();            ThrowHelper.ThrowIfNull(path);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            cancellationToken.ThrowIfCancellationRequested();            return SftpFileStream.OpenAsync(_sftpSession, path, mode, access, (int)_bufferSize, cancellationToken);        }        /// <summary>        /// Opens an existing file for reading.        /// </summary>        /// <param name="path">The file to be opened for reading.</param>        /// <returns>        /// A read-only <see cref="SftpFileStream"/> on the specified path.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public SftpFileStream OpenRead(string path)        {            return Open(path, FileMode.Open, FileAccess.Read);        }        /// <summary>        /// Opens an existing UTF-8 encoded text file for reading.        /// </summary>        /// <param name="path">The file to be opened for reading.</param>        /// <returns>        /// A <see cref="StreamReader"/> on the specified path.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public StreamReader OpenText(string path)        {            return new StreamReader(OpenRead(path), Encoding.UTF8);        }        /// <summary>        /// Opens a file for writing.        /// </summary>        /// <param name="path">The file to be opened for writing.</param>        /// <returns>        /// An unshared <see cref="SftpFileStream"/> object on the specified path with <see cref="FileAccess.Write"/> access.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        /// <remarks>        /// If the file does not exist, it is created.        /// </remarks>        public SftpFileStream OpenWrite(string path)        {            CheckDisposed();            return new SftpFileStream(_sftpSession, path, FileMode.OpenOrCreate, FileAccess.Write, (int)_bufferSize);        }        /// <summary>        /// Opens a binary file, reads the contents of the file into a byte array, and closes the file.        /// </summary>        /// <param name="path">The file to open for reading.</param>        /// <returns>        /// A byte array containing the contents of the file.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public byte[] ReadAllBytes(string path)        {            using (var stream = OpenRead(path))            {                var buffer = new byte[stream.Length];                _ = stream.Read(buffer, 0, buffer.Length);                return buffer;            }        }        /// <summary>        /// Opens a text file, reads all lines of the file using UTF-8 encoding, and closes the file.        /// </summary>        /// <param name="path">The file to open for reading.</param>        /// <returns>        /// A string array containing all lines of the file.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public string[] ReadAllLines(string path)        {            return ReadAllLines(path, Encoding.UTF8);        }        /// <summary>        /// Opens a file, reads all lines of the file with the specified encoding, and closes the file.        /// </summary>        /// <param name="path">The file to open for reading.</param>        /// <param name="encoding">The encoding applied to the contents of the file.</param>        /// <returns>        /// A string array containing all lines of the file.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public string[] ReadAllLines(string path, Encoding encoding)        {            /*             * We use the default buffer size for StreamReader - which is 1024 bytes - and the configured buffer size             * for the SftpFileStream. We may want to revisit this later.             */            var lines = new List<string>();            using (var stream = new StreamReader(OpenRead(path), encoding))            {                string? line;                while ((line = stream.ReadLine()) != null)                {                    lines.Add(line);                }            }            return lines.ToArray();        }        /// <summary>        /// Opens a text file, reads all lines of the file with the UTF-8 encoding, and closes the file.        /// </summary>        /// <param name="path">The file to open for reading.</param>        /// <returns>        /// A string containing all lines of the file.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public string ReadAllText(string path)        {            return ReadAllText(path, Encoding.UTF8);        }        /// <summary>        /// Opens a file, reads all lines of the file with the specified encoding, and closes the file.        /// </summary>        /// <param name="path">The file to open for reading.</param>        /// <param name="encoding">The encoding applied to the contents of the file.</param>        /// <returns>        /// A string containing all lines of the file.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public string ReadAllText(string path, Encoding encoding)        {            /*             * We use the default buffer size for StreamReader - which is 1024 bytes - and the configured buffer size             * for the SftpFileStream. We may want to revisit this later.             */            using (var stream = new StreamReader(OpenRead(path), encoding))            {                return stream.ReadToEnd();            }        }        /// <summary>        /// Reads the lines of a file with the UTF-8 encoding.        /// </summary>        /// <param name="path">The file to read.</param>        /// <returns>        /// The lines of the file.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public IEnumerable<string> ReadLines(string path)        {            return ReadAllLines(path);        }        /// <summary>        /// Read the lines of a file that has a specified encoding.        /// </summary>        /// <param name="path">The file to read.</param>        /// <param name="encoding">The encoding that is applied to the contents of the file.</param>        /// <returns>        /// The lines of the file.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public IEnumerable<string> ReadLines(string path, Encoding encoding)        {            return ReadAllLines(path, encoding);        }        /// <summary>        /// Sets the date and time the specified file was last accessed.        /// </summary>        /// <param name="path">The file for which to set the access date and time information.</param>        /// <param name="lastAccessTime">A <see cref="DateTime"/> containing the value to set for the last access date and time of path. This value is expressed in local time.</param>        public void SetLastAccessTime(string path, DateTime lastAccessTime)        {            var attributes = GetAttributes(path);            attributes.LastAccessTime = lastAccessTime;            SetAttributes(path, attributes);        }        /// <summary>        /// Sets the date and time, in coordinated universal time (UTC), that the specified file was last accessed.        /// </summary>        /// <param name="path">The file for which to set the access date and time information.</param>        /// <param name="lastAccessTimeUtc">A <see cref="DateTime"/> containing the value to set for the last access date and time of path. This value is expressed in UTC time.</param>        public void SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc)        {            var attributes = GetAttributes(path);            attributes.LastAccessTimeUtc = lastAccessTimeUtc;            SetAttributes(path, attributes);        }        /// <summary>        /// Sets the date and time that the specified file was last written to.        /// </summary>        /// <param name="path">The file for which to set the date and time information.</param>        /// <param name="lastWriteTime">A <see cref="DateTime"/> containing the value to set for the last write date and time of path. This value is expressed in local time.</param>        public void SetLastWriteTime(string path, DateTime lastWriteTime)        {            var attributes = GetAttributes(path);            attributes.LastWriteTime = lastWriteTime;            SetAttributes(path, attributes);        }        /// <summary>        /// Sets the date and time, in coordinated universal time (UTC), that the specified file was last written to.        /// </summary>        /// <param name="path">The file for which to set the date and time information.</param>        /// <param name="lastWriteTimeUtc">A <see cref="DateTime"/> containing the value to set for the last write date and time of path. This value is expressed in UTC time.</param>        public void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc)        {            var attributes = GetAttributes(path);            attributes.LastWriteTimeUtc = lastWriteTimeUtc;            SetAttributes(path, attributes);        }        /// <summary>        /// Writes the specified byte array to the specified file, and closes the file.        /// </summary>        /// <param name="path">The file to write to.</param>        /// <param name="bytes">The bytes to write to the file.</param>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        /// <remarks>        /// <para>        /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes.        /// </para>        /// <para>        /// If the target file does not exist, it is created.        /// </para>        /// </remarks>        public void WriteAllBytes(string path, byte[] bytes)        {            using (var stream = OpenWrite(path))            {                stream.Write(bytes, 0, bytes.Length);            }        }        /// <summary>        /// Writes a collection of strings to the file using the UTF-8 encoding, and closes the file.        /// </summary>        /// <param name="path">The file to write to.</param>        /// <param name="contents">The lines to write to the file.</param>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        /// <remarks>        /// <para>        /// The characters are written to the file using UTF-8 encoding without a Byte-Order Mark (BOM).        /// </para>        /// <para>        /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes.        /// </para>        /// <para>        /// If the target file does not exist, it is created.        /// </para>        /// </remarks>        public void WriteAllLines(string path, IEnumerable<string> contents)        {            WriteAllLines(path, contents, Utf8NoBOM);        }        /// <summary>        /// Write the specified string array to the file using the UTF-8 encoding, and closes the file.        /// </summary>        /// <param name="path">The file to write to.</param>        /// <param name="contents">The string array to write to the file.</param>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        /// <remarks>        /// <para>        /// The characters are written to the file using UTF-8 encoding without a Byte-Order Mark (BOM).        /// </para>        /// <para>        /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes.        /// </para>        /// <para>        /// If the target file does not exist, it is created.        /// </para>        /// </remarks>        public void WriteAllLines(string path, string[] contents)        {            WriteAllLines(path, contents, Utf8NoBOM);        }        /// <summary>        /// Writes a collection of strings to the file using the specified encoding, and closes the file.        /// </summary>        /// <param name="path">The file to write to.</param>        /// <param name="contents">The lines to write to the file.</param>        /// <param name="encoding">The character encoding to use.</param>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        /// <remarks>        /// <para>        /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes.        /// </para>        /// <para>        /// If the target file does not exist, it is created.        /// </para>        /// </remarks>        public void WriteAllLines(string path, IEnumerable<string> contents, Encoding encoding)        {            using (var stream = CreateText(path, encoding))            {                foreach (var line in contents)                {                    stream.WriteLine(line);                }            }        }        /// <summary>        /// Writes the specified string array to the file by using the specified encoding, and closes the file.        /// </summary>        /// <param name="path">The file to write to.</param>        /// <param name="contents">The string array to write to the file.</param>        /// <param name="encoding">An <see cref="Encoding"/> object that represents the character encoding applied to the string array.</param>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        /// <remarks>        /// <para>        /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes.        /// </para>        /// <para>        /// If the target file does not exist, it is created.        /// </para>        /// </remarks>        public void WriteAllLines(string path, string[] contents, Encoding encoding)        {            using (var stream = CreateText(path, encoding))            {                foreach (var line in contents)                {                    stream.WriteLine(line);                }            }        }        /// <summary>        /// Writes the specified string to the file using the UTF-8 encoding, and closes the file.        /// </summary>        /// <param name="path">The file to write to.</param>        /// <param name="contents">The string to write to the file.</param>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        /// <remarks>        /// <para>        /// The characters are written to the file using UTF-8 encoding without a Byte-Order Mark (BOM).        /// </para>        /// <para>        /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes.        /// </para>        /// <para>        /// If the target file does not exist, it is created.        /// </para>        /// </remarks>        public void WriteAllText(string path, string contents)        {            using (var stream = CreateText(path))            {                stream.Write(contents);            }        }        /// <summary>        /// Writes the specified string to the file using the specified encoding, and closes the file.        /// </summary>        /// <param name="path">The file to write to.</param>        /// <param name="contents">The string to write to the file.</param>        /// <param name="encoding">The encoding to apply to the string.</param>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        /// <remarks>        /// <para>        /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes.        /// </para>        /// <para>        /// If the target file does not exist, it is created.        /// </para>        /// </remarks>        public void WriteAllText(string path, string contents, Encoding encoding)        {            using (var stream = CreateText(path, encoding))            {                stream.Write(contents);            }        }        /// <summary>        /// Gets the <see cref="SftpFileAttributes"/> of the file on the path.        /// </summary>        /// <param name="path">The path to the file.</param>        /// <returns>        /// The <see cref="SftpFileAttributes"/> of the file on the path.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public SftpFileAttributes GetAttributes(string path)        {            CheckDisposed();            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            var fullPath = _sftpSession.GetCanonicalPath(path);            return _sftpSession.RequestLStat(fullPath);        }        /// <summary>        /// Sets the specified <see cref="SftpFileAttributes"/> of the file on the specified path.        /// </summary>        /// <param name="path">The path to the file.</param>        /// <param name="fileAttributes">The desired <see cref="SftpFileAttributes"/>.</param>        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client is not connected.</exception>        /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>        public void SetAttributes(string path, SftpFileAttributes fileAttributes)        {            CheckDisposed();            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            var fullPath = _sftpSession.GetCanonicalPath(path);            _sftpSession.RequestSetStat(fullPath, fileAttributes);        }        #endregion // File Methods        #region SynchronizeDirectories        /// <summary>        /// Synchronizes the directories.        /// </summary>        /// <param name="sourcePath">The source path.</param>        /// <param name="destinationPath">The destination path.</param>        /// <param name="searchPattern">The search pattern.</param>        /// <returns>        /// A list of uploaded files.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="sourcePath"/> is <see langword="null"/>.</exception>        /// <exception cref="ArgumentException"><paramref name="destinationPath"/> is <see langword="null"/> or contains only whitespace.</exception>        /// <exception cref="SftpPathNotFoundException"><paramref name="destinationPath"/> was not found on the remote host.</exception>        /// <exception cref="SshException">If a problem occurs while copying the file.</exception>        public IEnumerable<FileInfo> SynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern)        {            ThrowHelper.ThrowIfNull(sourcePath);            ThrowHelper.ThrowIfNullOrWhiteSpace(destinationPath);            return InternalSynchronizeDirectories(sourcePath, destinationPath, searchPattern, asynchResult: null);        }        /// <summary>        /// Begins the synchronize directories.        /// </summary>        /// <param name="sourcePath">The source path.</param>        /// <param name="destinationPath">The destination path.</param>        /// <param name="searchPattern">The search pattern.</param>        /// <param name="asyncCallback">The async callback.</param>        /// <param name="state">The state.</param>        /// <returns>        /// An <see cref="IAsyncResult" /> that represents the asynchronous directory synchronization.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="sourcePath"/> or <paramref name="searchPattern"/> is <see langword="null"/>.</exception>        /// <exception cref="ArgumentException"><paramref name="destinationPath"/> is <see langword="null"/> or contains only whitespace.</exception>        /// <exception cref="SshException">If a problem occurs while copying the file.</exception>        public IAsyncResult BeginSynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern, AsyncCallback? asyncCallback, object? state)        {            ThrowHelper.ThrowIfNull(sourcePath);            ThrowHelper.ThrowIfNullOrWhiteSpace(destinationPath);            ThrowHelper.ThrowIfNull(searchPattern);            var asyncResult = new SftpSynchronizeDirectoriesAsyncResult(asyncCallback, state);            ThreadAbstraction.ExecuteThread(() =>                {                    try                    {                        var result = InternalSynchronizeDirectories(sourcePath, destinationPath, searchPattern, asyncResult);                        asyncResult.SetAsCompleted(result, completedSynchronously: false);                    }                    catch (Exception exp)                    {                        asyncResult.SetAsCompleted(exp, completedSynchronously: false);                    }                });            return asyncResult;        }        /// <summary>        /// Ends the synchronize directories.        /// </summary>        /// <param name="asyncResult">The async result.</param>        /// <returns>        /// A list of uploaded files.        /// </returns>        /// <exception cref="ArgumentException">The <see cref="IAsyncResult"/> object did not come from the corresponding async method on this type.<para>-or-</para><see cref="EndSynchronizeDirectories(IAsyncResult)"/> was called multiple times with the same <see cref="IAsyncResult"/>.</exception>        /// <exception cref="SftpPathNotFoundException">The destination path was not found on the remote host.</exception>        public IEnumerable<FileInfo> EndSynchronizeDirectories(IAsyncResult asyncResult)        {            if (asyncResult is not SftpSynchronizeDirectoriesAsyncResult ar || ar.EndInvokeCalled)            {                throw new ArgumentException("Either the IAsyncResult object did not come from the corresponding async method on this type, or EndExecute was called multiple times with the same IAsyncResult.");            }            // Wait for operation to complete, then return result or throw exception            return ar.EndInvoke();        }        private List<FileInfo> InternalSynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern, SftpSynchronizeDirectoriesAsyncResult? asynchResult)        {            if (!Directory.Exists(sourcePath))            {                throw new FileNotFoundException(string.Format("Source directory not found: {0}", sourcePath));            }            var uploadedFiles = new List<FileInfo>();            var sourceDirectory = new DirectoryInfo(sourcePath);            using (var sourceFiles = sourceDirectory.EnumerateFiles(searchPattern).GetEnumerator())            {                if (!sourceFiles.MoveNext())                {                    return uploadedFiles;                }                #region Existing Files at The Destination                var destFiles = InternalListDirectory(destinationPath, asyncResult: null, listCallback: null);                var destDict = new Dictionary<string, ISftpFile>();                foreach (var destFile in destFiles)                {                    if (destFile.IsDirectory)                    {                        continue;                    }                    destDict.Add(destFile.Name, destFile);                }                #endregion                #region Upload the difference                const Flags uploadFlag = Flags.Write | Flags.Truncate | Flags.CreateNewOrOpen;                do                {                    var localFile = sourceFiles.Current;                    if (localFile is null)                    {                        continue;                    }                    var isDifferent = true;                    if (destDict.TryGetValue(localFile.Name, out var remoteFile))                    {                        // File exists at the destination, use filesize to detect if there's a difference                        isDifferent = localFile.Length != remoteFile.Length;                    }                    if (isDifferent)                    {                        var remoteFileName = string.Format(CultureInfo.InvariantCulture, @"{0}/{1}", destinationPath, localFile.Name);                        try                        {#pragma warning disable CA2000 // Dispose objects before losing scope; false positive                            using (var file = File.OpenRead(localFile.FullName))#pragma warning restore CA2000 // Dispose objects before losing scope; false positive                            {                                InternalUploadFile(file, remoteFileName, uploadFlag, asyncResult: null, uploadCallback: null);                            }                            uploadedFiles.Add(localFile);                            asynchResult?.Update(uploadedFiles.Count);                        }                        catch (Exception ex)                        {                            throw new SshException($"Failed to upload {localFile.FullName} to {remoteFileName}", ex);                        }                    }                }                while (sourceFiles.MoveNext());            }            #endregion            return uploadedFiles;        }        #endregion        /// <summary>        /// Internals the list directory.        /// </summary>        /// <param name="path">The path.</param>        /// <param name="asyncResult">An <see cref="IAsyncResult"/> that references the asynchronous request.</param>        /// <param name="listCallback">The list callback.</param>        /// <returns>        /// A list of files in the specfied directory.        /// </returns>        /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null"/>.</exception>        /// <exception cref="SshConnectionException">Client not connected.</exception>        private List<ISftpFile> InternalListDirectory(string path, SftpListDirectoryAsyncResult? asyncResult, Action<int>? listCallback)        {            ThrowHelper.ThrowIfNull(path);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            var fullPath = _sftpSession.GetCanonicalPath(path);            var handle = _sftpSession.RequestOpenDir(fullPath);            var basePath = fullPath;#if NET            if (!basePath.EndsWith('/'))#else            if (!basePath.EndsWith("/", StringComparison.Ordinal))#endif            {                basePath = string.Format("{0}/", fullPath);            }            var result = new List<ISftpFile>();            var files = _sftpSession.RequestReadDir(handle);            while (files is not null)            {                foreach (var f in files)                {                    result.Add(new SftpFile(_sftpSession,                                            string.Format(CultureInfo.InvariantCulture, "{0}{1}", basePath, f.Key),                                            f.Value));                }                asyncResult?.Update(result.Count);                // Call callback to report number of files read                if (listCallback is not null)                {                    // Execute callback on different thread                    ThreadAbstraction.ExecuteThread(() => listCallback(result.Count));                }                files = _sftpSession.RequestReadDir(handle);            }            _sftpSession.RequestClose(handle);            return result;        }        /// <summary>        /// Internals the download file.        /// </summary>        /// <param name="path">The path.</param>        /// <param name="output">The output.</param>        /// <param name="asyncResult">An <see cref="IAsyncResult"/> that references the asynchronous request.</param>        /// <param name="downloadCallback">The download callback.</param>        /// <exception cref="ArgumentNullException"><paramref name="output" /> is <see langword="null"/>.</exception>        /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains whitespace.</exception>        /// <exception cref="SshConnectionException">Client not connected.</exception>        private void InternalDownloadFile(string path, Stream output, SftpDownloadAsyncResult? asyncResult, Action<ulong>? downloadCallback)        {            ThrowHelper.ThrowIfNull(output);            ThrowHelper.ThrowIfNullOrWhiteSpace(path);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            var fullPath = _sftpSession.GetCanonicalPath(path);            using (var fileReader = ServiceFactory.CreateSftpFileReader(fullPath, _sftpSession, _bufferSize))            {                var totalBytesRead = 0UL;                while (true)                {                    // Cancel download                    if (asyncResult is not null && asyncResult.IsDownloadCanceled)                    {                        break;                    }                    var data = fileReader.Read();                    if (data.Length == 0)                    {                        break;                    }                    output.Write(data, 0, data.Length);                    totalBytesRead += (ulong)data.Length;                    asyncResult?.Update(totalBytesRead);                    if (downloadCallback is not null)                    {                        // Copy offset to ensure it's not modified between now and execution of callback                        var downloadOffset = totalBytesRead;                        // Execute callback on different thread                        ThreadAbstraction.ExecuteThread(() => { downloadCallback(downloadOffset); });                    }                }            }        }        private async Task InternalDownloadFileAsync(string path, Stream output, CancellationToken cancellationToken)        {            ThrowHelper.ThrowIfNull(output);            ThrowHelper.ThrowIfNullOrWhiteSpace(path);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            cancellationToken.ThrowIfCancellationRequested();            var fullPath = await _sftpSession.GetCanonicalPathAsync(path, cancellationToken).ConfigureAwait(false);            var openStreamTask = SftpFileStream.OpenAsync(_sftpSession, fullPath, FileMode.Open, FileAccess.Read, (int)_bufferSize, cancellationToken);            using (var input = await openStreamTask.ConfigureAwait(false))            {                await input.CopyToAsync(output, 81920, cancellationToken).ConfigureAwait(false);            }        }        /// <summary>        /// Internals the upload file.        /// </summary>        /// <param name="input">The input.</param>        /// <param name="path">The path.</param>        /// <param name="flags">The flags.</param>        /// <param name="asyncResult">An <see cref="IAsyncResult"/> that references the asynchronous request.</param>        /// <param name="uploadCallback">The upload callback.</param>        /// <exception cref="ArgumentNullException"><paramref name="input" /> is <see langword="null"/>.</exception>        /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains whitespace.</exception>        /// <exception cref="SshConnectionException">Client not connected.</exception>        private void InternalUploadFile(Stream input, string path, Flags flags, SftpUploadAsyncResult? asyncResult, Action<ulong>? uploadCallback)        {            ThrowHelper.ThrowIfNull(input);            ThrowHelper.ThrowIfNullOrWhiteSpace(path);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            var fullPath = _sftpSession.GetCanonicalPath(path);            var handle = _sftpSession.RequestOpen(fullPath, flags);            ulong offset = 0;            // create buffer of optimal length            var buffer = new byte[_sftpSession.CalculateOptimalWriteLength(_bufferSize, handle)];            int bytesRead;            var expectedResponses = 0;            // We will send out all the write requests without waiting for each response.            // Afterwards, we may wait on this handle until all responses are received            // or an error has occured.            using var mres = new ManualResetEventSlim(initialState: false);            ExceptionDispatchInfo? exception = null;            while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)            {                if (asyncResult is not null && asyncResult.IsUploadCanceled)                {                    break;                }                exception?.Throw();                var writtenBytes = offset + (ulong)bytesRead;                _ = Interlocked.Increment(ref expectedResponses);                mres.Reset();                _sftpSession.RequestWrite(handle, offset, buffer, offset: 0, bytesRead, wait: null, s =>                {                    var setHandle = false;                    try                    {                        if (Sftp.SftpSession.GetSftpException(s) is Exception ex)                        {                            exception = ExceptionDispatchInfo.Capture(ex);                        }                        if (exception is not null)                        {                            setHandle = true;                            return;                        }                        Debug.Assert(s.StatusCode == StatusCodes.Ok);                        asyncResult?.Update(writtenBytes);                        // Call callback to report number of bytes written                        if (uploadCallback is not null)                        {                            // Execute callback on different thread                            ThreadAbstraction.ExecuteThread(() => uploadCallback(writtenBytes));                        }                    }                    finally                    {                        if (Interlocked.Decrement(ref expectedResponses) == 0 || setHandle)                        {                            mres.Set();                        }                    }                });                offset += (ulong)bytesRead;            }            // Make sure the read of exception cannot be executed ahead of            // the read of expectedResponses so that we do not miss an            // exception.            if (Volatile.Read(ref expectedResponses) != 0)            {                _sftpSession.WaitOnHandle(mres.WaitHandle, _operationTimeout);            }            exception?.Throw();            _sftpSession.RequestClose(handle);        }        private async Task InternalUploadFileAsync(Stream input, string path, CancellationToken cancellationToken)        {            ThrowHelper.ThrowIfNull(input);            ThrowHelper.ThrowIfNullOrWhiteSpace(path);            if (_sftpSession is null)            {                throw new SshConnectionException("Client not connected.");            }            cancellationToken.ThrowIfCancellationRequested();            var fullPath = await _sftpSession.GetCanonicalPathAsync(path, cancellationToken).ConfigureAwait(false);            var openStreamTask = SftpFileStream.OpenAsync(_sftpSession, fullPath, FileMode.Create, FileAccess.Write, (int)_bufferSize, cancellationToken);            using (var output = await openStreamTask.ConfigureAwait(false))            {                await input.CopyToAsync(output, 81920, cancellationToken).ConfigureAwait(false);            }        }        /// <summary>        /// Called when client is connected to the server.        /// </summary>        protected override void OnConnected()        {            base.OnConnected();            _sftpSession?.Dispose();            _sftpSession = CreateAndConnectToSftpSession();        }        /// <summary>        /// Called when client is disconnecting from the server.        /// </summary>        protected override void OnDisconnecting()        {            base.OnDisconnecting();            // disconnect, dispose and dereference the SFTP session since we create a new SFTP session            // on each connect            var sftpSession = _sftpSession;            if (sftpSession is not null)            {                _sftpSession = null;                sftpSession.Dispose();            }        }        /// <summary>        /// Releases unmanaged and - optionally - managed resources.        /// </summary>        /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>        protected override void Dispose(bool disposing)        {            base.Dispose(disposing);            if (disposing)            {                var sftpSession = _sftpSession;                if (sftpSession is not null)                {                    _sftpSession = null;                    sftpSession.Dispose();                }            }        }        private ISftpSession CreateAndConnectToSftpSession()        {            var sftpSession = ServiceFactory.CreateSftpSession(Session,                                                               _operationTimeout,                                                               ConnectionInfo.Encoding,                                                               ServiceFactory.CreateSftpResponseFactory());            try            {                sftpSession.Connect();                return sftpSession;            }            catch            {                sftpSession.Dispose();                throw;            }        }    }}
 |