SftpClient.cs 122 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Globalization;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Runtime.CompilerServices;
  11. using System.Runtime.ExceptionServices;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using Renci.SshNet.Abstractions;
  16. using Renci.SshNet.Common;
  17. using Renci.SshNet.Sftp;
  18. namespace Renci.SshNet
  19. {
  20. /// <summary>
  21. /// Implementation of the SSH File Transfer Protocol (SFTP) over SSH.
  22. /// </summary>
  23. public class SftpClient : BaseClient, ISftpClient
  24. {
  25. private static readonly Encoding Utf8NoBOM = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
  26. /// <summary>
  27. /// Holds the <see cref="ISftpSession"/> instance that is used to communicate to the
  28. /// SFTP server.
  29. /// </summary>
  30. private ISftpSession? _sftpSession;
  31. /// <summary>
  32. /// Holds the operation timeout.
  33. /// </summary>
  34. private int _operationTimeout;
  35. /// <summary>
  36. /// Holds the size of the buffer.
  37. /// </summary>
  38. private uint _bufferSize;
  39. /// <summary>
  40. /// Gets or sets the operation timeout.
  41. /// </summary>
  42. /// <value>
  43. /// The timeout to wait until an operation completes. The default value is negative
  44. /// one (-1) milliseconds, which indicates an infinite timeout period.
  45. /// </value>
  46. /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> represents a value that is less than -1 or greater than <see cref="int.MaxValue"/> milliseconds.</exception>
  47. public TimeSpan OperationTimeout
  48. {
  49. get
  50. {
  51. return TimeSpan.FromMilliseconds(_operationTimeout);
  52. }
  53. set
  54. {
  55. _operationTimeout = value.AsTimeout(nameof(OperationTimeout));
  56. if (_sftpSession is { } sftpSession)
  57. {
  58. sftpSession.OperationTimeout = _operationTimeout;
  59. }
  60. }
  61. }
  62. /// <summary>
  63. /// Gets or sets the maximum size of the buffer in bytes.
  64. /// </summary>
  65. /// <value>
  66. /// The size of the buffer. The default buffer size is 32768 bytes (32 KB).
  67. /// </value>
  68. /// <remarks>
  69. /// <para>
  70. /// For write operations, this limits the size of the payload for
  71. /// individual SSH_FXP_WRITE messages. The actual size is always
  72. /// capped at the maximum packet size supported by the peer
  73. /// (minus the size of protocol fields).
  74. /// </para>
  75. /// <para>
  76. /// For read operations, this controls the size of the payload which
  77. /// is requested from the peer in a SSH_FXP_READ message. The peer
  78. /// will send the requested number of bytes in a SSH_FXP_DATA message,
  79. /// possibly split over multiple SSH_MSG_CHANNEL_DATA messages.
  80. /// </para>
  81. /// <para>
  82. /// To optimize the size of the SSH packets sent by the peer,
  83. /// the actual requested size will take into account the size of the
  84. /// SSH_FXP_DATA protocol fields.
  85. /// </para>
  86. /// <para>
  87. /// The size of the each individual SSH_FXP_DATA message is limited to the
  88. /// local maximum packet size of the channel, which is set to <c>64 KB</c>
  89. /// for SSH.NET. However, the peer can limit this even further.
  90. /// </para>
  91. /// </remarks>
  92. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  93. public uint BufferSize
  94. {
  95. get
  96. {
  97. CheckDisposed();
  98. return _bufferSize;
  99. }
  100. set
  101. {
  102. CheckDisposed();
  103. _bufferSize = value;
  104. }
  105. }
  106. /// <summary>
  107. /// Gets a value indicating whether this client is connected to the server and
  108. /// the SFTP session is open.
  109. /// </summary>
  110. /// <value>
  111. /// <see langword="true"/> if this client is connected and the SFTP session is open; otherwise, <see langword="false"/>.
  112. /// </value>
  113. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  114. public override bool IsConnected
  115. {
  116. get
  117. {
  118. return base.IsConnected && _sftpSession?.IsOpen == true;
  119. }
  120. }
  121. /// <summary>
  122. /// Gets remote working directory.
  123. /// </summary>
  124. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  125. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  126. public string WorkingDirectory
  127. {
  128. get
  129. {
  130. CheckDisposed();
  131. if (_sftpSession is null)
  132. {
  133. throw new SshConnectionException("Client not connected.");
  134. }
  135. return _sftpSession.WorkingDirectory;
  136. }
  137. }
  138. /// <summary>
  139. /// Gets sftp protocol version.
  140. /// </summary>
  141. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  142. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  143. public int ProtocolVersion
  144. {
  145. get
  146. {
  147. CheckDisposed();
  148. if (_sftpSession is null)
  149. {
  150. throw new SshConnectionException("Client not connected.");
  151. }
  152. return (int)_sftpSession.ProtocolVersion;
  153. }
  154. }
  155. /// <summary>
  156. /// Gets the current SFTP session.
  157. /// </summary>
  158. /// <value>
  159. /// The current SFTP session.
  160. /// </value>
  161. internal ISftpSession? SftpSession
  162. {
  163. get { return _sftpSession; }
  164. }
  165. #region Constructors
  166. /// <summary>
  167. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  168. /// </summary>
  169. /// <param name="connectionInfo">The connection info.</param>
  170. /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <see langword="null"/>.</exception>
  171. public SftpClient(ConnectionInfo connectionInfo)
  172. : this(connectionInfo, ownsConnectionInfo: false)
  173. {
  174. }
  175. /// <summary>
  176. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  177. /// </summary>
  178. /// <param name="host">Connection host.</param>
  179. /// <param name="port">Connection port.</param>
  180. /// <param name="username">Authentication username.</param>
  181. /// <param name="password">Authentication password.</param>
  182. /// <exception cref="ArgumentNullException"><paramref name="password"/> is <see langword="null"/>.</exception>
  183. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid. <para>-or-</para> <paramref name="username"/> is <see langword="null"/> or contains only whitespace characters.</exception>
  184. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="IPEndPoint.MinPort"/> and <see cref="IPEndPoint.MaxPort"/>.</exception>
  185. [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope", Justification = "Disposed in Dispose(bool) method.")]
  186. public SftpClient(string host, int port, string username, string password)
  187. : this(new PasswordConnectionInfo(host, port, username, password), ownsConnectionInfo: true)
  188. {
  189. }
  190. /// <summary>
  191. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  192. /// </summary>
  193. /// <param name="host">Connection host.</param>
  194. /// <param name="username">Authentication username.</param>
  195. /// <param name="password">Authentication password.</param>
  196. /// <exception cref="ArgumentNullException"><paramref name="password"/> is <see langword="null"/>.</exception>
  197. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid. <para>-or-</para> <paramref name="username"/> is <see langword="null"/> contains only whitespace characters.</exception>
  198. public SftpClient(string host, string username, string password)
  199. : this(host, ConnectionInfo.DefaultPort, username, password)
  200. {
  201. }
  202. /// <summary>
  203. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  204. /// </summary>
  205. /// <param name="host">Connection host.</param>
  206. /// <param name="port">Connection port.</param>
  207. /// <param name="username">Authentication username.</param>
  208. /// <param name="keyFiles">Authentication private key file(s) .</param>
  209. /// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is <see langword="null"/>.</exception>
  210. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid. <para>-or-</para> <paramref name="username"/> is <see langword="null"/> or contains only whitespace characters.</exception>
  211. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="IPEndPoint.MinPort"/> and <see cref="IPEndPoint.MaxPort"/>.</exception>
  212. [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope", Justification = "Disposed in Dispose(bool) method.")]
  213. public SftpClient(string host, int port, string username, params IPrivateKeySource[] keyFiles)
  214. : this(new PrivateKeyConnectionInfo(host, port, username, keyFiles), ownsConnectionInfo: true)
  215. {
  216. }
  217. /// <summary>
  218. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  219. /// </summary>
  220. /// <param name="host">Connection host.</param>
  221. /// <param name="username">Authentication username.</param>
  222. /// <param name="keyFiles">Authentication private key file(s) .</param>
  223. /// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is <see langword="null"/>.</exception>
  224. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid. <para>-or-</para> <paramref name="username"/> is <see langword="null"/> or contains only whitespace characters.</exception>
  225. public SftpClient(string host, string username, params IPrivateKeySource[] keyFiles)
  226. : this(host, ConnectionInfo.DefaultPort, username, keyFiles)
  227. {
  228. }
  229. /// <summary>
  230. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  231. /// </summary>
  232. /// <param name="connectionInfo">The connection info.</param>
  233. /// <param name="ownsConnectionInfo">Specified whether this instance owns the connection info.</param>
  234. /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <see langword="null"/>.</exception>
  235. /// <remarks>
  236. /// If <paramref name="ownsConnectionInfo"/> is <see langword="true"/>, the connection info will be disposed when this
  237. /// instance is disposed.
  238. /// </remarks>
  239. private SftpClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo)
  240. : this(connectionInfo, ownsConnectionInfo, new ServiceFactory())
  241. {
  242. }
  243. /// <summary>
  244. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  245. /// </summary>
  246. /// <param name="connectionInfo">The connection info.</param>
  247. /// <param name="ownsConnectionInfo">Specified whether this instance owns the connection info.</param>
  248. /// <param name="serviceFactory">The factory to use for creating new services.</param>
  249. /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <see langword="null"/>.</exception>
  250. /// <exception cref="ArgumentNullException"><paramref name="serviceFactory"/> is <see langword="null"/>.</exception>
  251. /// <remarks>
  252. /// If <paramref name="ownsConnectionInfo"/> is <see langword="true"/>, the connection info will be disposed when this
  253. /// instance is disposed.
  254. /// </remarks>
  255. internal SftpClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory)
  256. : base(connectionInfo, ownsConnectionInfo, serviceFactory)
  257. {
  258. _operationTimeout = Timeout.Infinite;
  259. _bufferSize = 1024 * 32;
  260. }
  261. #endregion Constructors
  262. /// <summary>
  263. /// Changes remote directory to path.
  264. /// </summary>
  265. /// <param name="path">New directory path.</param>
  266. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  267. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  268. /// <exception cref="SftpPermissionDeniedException">Permission to change directory denied by remote host. <para>-or-</para> A SSH command was denied by the server.</exception>
  269. /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>
  270. /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>
  271. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  272. public void ChangeDirectory(string path)
  273. {
  274. CheckDisposed();
  275. ThrowHelper.ThrowIfNull(path);
  276. if (_sftpSession is null)
  277. {
  278. throw new SshConnectionException("Client not connected.");
  279. }
  280. _sftpSession.ChangeDirectory(path);
  281. }
  282. /// <summary>
  283. /// Asynchronously requests to change the current working directory to the specified path.
  284. /// </summary>
  285. /// <param name="path">The new working directory.</param>
  286. /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
  287. /// <returns>A <see cref="Task"/> that tracks the asynchronous change working directory request.</returns>
  288. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  289. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  290. /// <exception cref="SftpPermissionDeniedException">Permission to change directory denied by remote host. <para>-or-</para> A SSH command was denied by the server.</exception>
  291. /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>
  292. /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>
  293. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  294. public Task ChangeDirectoryAsync(string path, CancellationToken cancellationToken = default)
  295. {
  296. CheckDisposed();
  297. ThrowHelper.ThrowIfNull(path);
  298. if (_sftpSession is null)
  299. {
  300. throw new SshConnectionException("Client not connected.");
  301. }
  302. cancellationToken.ThrowIfCancellationRequested();
  303. return _sftpSession.ChangeDirectoryAsync(path, cancellationToken);
  304. }
  305. /// <summary>
  306. /// Changes permissions of file(s) to specified mode.
  307. /// </summary>
  308. /// <param name="path">File(s) path, may match multiple files.</param>
  309. /// <param name="mode">The mode.</param>
  310. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  311. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  312. /// <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>
  313. /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>
  314. /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>
  315. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  316. public void ChangePermissions(string path, short mode)
  317. {
  318. var file = Get(path);
  319. file.SetPermissions(mode);
  320. }
  321. /// <summary>
  322. /// Creates remote directory specified by path.
  323. /// </summary>
  324. /// <param name="path">Directory path to create.</param>
  325. /// <exception cref="ArgumentException"><paramref name="path"/> is <see langword="null"/> or contains only whitespace characters.</exception>
  326. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  327. /// <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>
  328. /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>
  329. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  330. public void CreateDirectory(string path)
  331. {
  332. CheckDisposed();
  333. ThrowHelper.ThrowIfNullOrWhiteSpace(path);
  334. if (_sftpSession is null)
  335. {
  336. throw new SshConnectionException("Client not connected.");
  337. }
  338. var fullPath = _sftpSession.GetCanonicalPath(path);
  339. _sftpSession.RequestMkDir(fullPath);
  340. }
  341. /// <summary>
  342. /// Asynchronously requests to create a remote directory specified by path.
  343. /// </summary>
  344. /// <param name="path">Directory path to create.</param>
  345. /// <param name="cancellationToken">The <see cref="CancellationToken"/> to observe.</param>
  346. /// <returns>A <see cref="Task"/> that represents the asynchronous create directory operation.</returns>
  347. /// <exception cref="ArgumentException"><paramref name="path"/> is <see langword="null"/> or contains only whitespace characters.</exception>
  348. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  349. /// <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>
  350. /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>
  351. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  352. public async Task CreateDirectoryAsync(string path, CancellationToken cancellationToken = default)
  353. {
  354. CheckDisposed();
  355. ThrowHelper.ThrowIfNullOrWhiteSpace(path);
  356. if (_sftpSession is null)
  357. {
  358. throw new SshConnectionException("Client not connected.");
  359. }
  360. var fullPath = await _sftpSession.GetCanonicalPathAsync(path, cancellationToken).ConfigureAwait(false);
  361. await _sftpSession.RequestMkDirAsync(fullPath, cancellationToken).ConfigureAwait(false);
  362. }
  363. /// <summary>
  364. /// Deletes remote directory specified by path.
  365. /// </summary>
  366. /// <param name="path">Directory to be deleted path.</param>
  367. /// <exception cref="ArgumentException"><paramref name="path"/> is <see langword="null"/> or contains only whitespace characters.</exception>
  368. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  369. /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>
  370. /// <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>
  371. /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>
  372. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  373. public void DeleteDirectory(string path)
  374. {
  375. CheckDisposed();
  376. ThrowHelper.ThrowIfNullOrWhiteSpace(path);
  377. if (_sftpSession is null)
  378. {
  379. throw new SshConnectionException("Client not connected.");
  380. }
  381. var fullPath = _sftpSession.GetCanonicalPath(path);
  382. _sftpSession.RequestRmDir(fullPath);
  383. }
  384. /// <inheritdoc />
  385. public async Task DeleteDirectoryAsync(string path, CancellationToken cancellationToken = default)
  386. {
  387. CheckDisposed();
  388. ThrowHelper.ThrowIfNullOrWhiteSpace(path);
  389. if (_sftpSession is null)
  390. {
  391. throw new SshConnectionException("Client not connected.");
  392. }
  393. cancellationToken.ThrowIfCancellationRequested();
  394. var fullPath = await _sftpSession.GetCanonicalPathAsync(path, cancellationToken).ConfigureAwait(false);
  395. await _sftpSession.RequestRmDirAsync(fullPath, cancellationToken).ConfigureAwait(false);
  396. }
  397. /// <summary>
  398. /// Deletes remote file specified by path.
  399. /// </summary>
  400. /// <param name="path">File to be deleted path.</param>
  401. /// <exception cref="ArgumentException"><paramref name="path"/> is <see langword="null"/> or contains only whitespace characters.</exception>
  402. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  403. /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>
  404. /// <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>
  405. /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>
  406. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  407. public void DeleteFile(string path)
  408. {
  409. CheckDisposed();
  410. ThrowHelper.ThrowIfNullOrWhiteSpace(path);
  411. if (_sftpSession is null)
  412. {
  413. throw new SshConnectionException("Client not connected.");
  414. }
  415. var fullPath = _sftpSession.GetCanonicalPath(path);
  416. _sftpSession.RequestRemove(fullPath);
  417. }
  418. /// <inheritdoc />
  419. public async Task DeleteFileAsync(string path, CancellationToken cancellationToken)
  420. {
  421. CheckDisposed();
  422. ThrowHelper.ThrowIfNullOrWhiteSpace(path);
  423. if (_sftpSession is null)
  424. {
  425. throw new SshConnectionException("Client not connected.");
  426. }
  427. cancellationToken.ThrowIfCancellationRequested();
  428. var fullPath = await _sftpSession.GetCanonicalPathAsync(path, cancellationToken).ConfigureAwait(false);
  429. await _sftpSession.RequestRemoveAsync(fullPath, cancellationToken).ConfigureAwait(false);
  430. }
  431. /// <summary>
  432. /// Renames remote file from old path to new path.
  433. /// </summary>
  434. /// <param name="oldPath">Path to the old file location.</param>
  435. /// <param name="newPath">Path to the new file location.</param>
  436. /// <exception cref="ArgumentNullException"><paramref name="oldPath"/> is <see langword="null"/>. <para>-or-</para> or <paramref name="newPath"/> is <see langword="null"/>.</exception>
  437. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  438. /// <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>
  439. /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>
  440. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  441. public void RenameFile(string oldPath, string newPath)
  442. {
  443. RenameFile(oldPath, newPath, isPosix: false);
  444. }
  445. /// <summary>
  446. /// Renames remote file from old path to new path.
  447. /// </summary>
  448. /// <param name="oldPath">Path to the old file location.</param>
  449. /// <param name="newPath">Path to the new file location.</param>
  450. /// <param name="isPosix">if set to <see langword="true"/> then perform a posix rename.</param>
  451. /// <exception cref="ArgumentNullException"><paramref name="oldPath" /> is <see langword="null"/>. <para>-or-</para> or <paramref name="newPath" /> is <see langword="null"/>.</exception>
  452. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  453. /// <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>
  454. /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>
  455. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  456. public void RenameFile(string oldPath, string newPath, bool isPosix)
  457. {
  458. CheckDisposed();
  459. ThrowHelper.ThrowIfNull(oldPath);
  460. ThrowHelper.ThrowIfNull(newPath);
  461. if (_sftpSession is null)
  462. {
  463. throw new SshConnectionException("Client not connected.");
  464. }
  465. var oldFullPath = _sftpSession.GetCanonicalPath(oldPath);
  466. var newFullPath = _sftpSession.GetCanonicalPath(newPath);
  467. if (isPosix)
  468. {
  469. _sftpSession.RequestPosixRename(oldFullPath, newFullPath);
  470. }
  471. else
  472. {
  473. _sftpSession.RequestRename(oldFullPath, newFullPath);
  474. }
  475. }
  476. /// <summary>
  477. /// Asynchronously renames remote file from old path to new path.
  478. /// </summary>
  479. /// <param name="oldPath">Path to the old file location.</param>
  480. /// <param name="newPath">Path to the new file location.</param>
  481. /// <param name="cancellationToken">The <see cref="CancellationToken"/> to observe.</param>
  482. /// <returns>A <see cref="Task"/> that represents the asynchronous rename operation.</returns>
  483. /// <exception cref="ArgumentNullException"><paramref name="oldPath"/> is <see langword="null"/>. <para>-or-</para> or <paramref name="newPath"/> is <see langword="null"/>.</exception>
  484. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  485. /// <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>
  486. /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>
  487. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  488. public async Task RenameFileAsync(string oldPath, string newPath, CancellationToken cancellationToken)
  489. {
  490. CheckDisposed();
  491. ThrowHelper.ThrowIfNull(oldPath);
  492. ThrowHelper.ThrowIfNull(newPath);
  493. if (_sftpSession is null)
  494. {
  495. throw new SshConnectionException("Client not connected.");
  496. }
  497. cancellationToken.ThrowIfCancellationRequested();
  498. var oldFullPath = await _sftpSession.GetCanonicalPathAsync(oldPath, cancellationToken).ConfigureAwait(false);
  499. var newFullPath = await _sftpSession.GetCanonicalPathAsync(newPath, cancellationToken).ConfigureAwait(false);
  500. await _sftpSession.RequestRenameAsync(oldFullPath, newFullPath, cancellationToken).ConfigureAwait(false);
  501. }
  502. /// <summary>
  503. /// Creates a symbolic link from old path to new path.
  504. /// </summary>
  505. /// <param name="path">The old path.</param>
  506. /// <param name="linkPath">The new path.</param>
  507. /// <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>
  508. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  509. /// <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>
  510. /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>
  511. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  512. public void SymbolicLink(string path, string linkPath)
  513. {
  514. CheckDisposed();
  515. ThrowHelper.ThrowIfNullOrWhiteSpace(path);
  516. ThrowHelper.ThrowIfNullOrWhiteSpace(linkPath);
  517. if (_sftpSession is null)
  518. {
  519. throw new SshConnectionException("Client not connected.");
  520. }
  521. var fullPath = _sftpSession.GetCanonicalPath(path);
  522. var linkFullPath = _sftpSession.GetCanonicalPath(linkPath);
  523. _sftpSession.RequestSymLink(fullPath, linkFullPath);
  524. }
  525. /// <summary>
  526. /// Retrieves list of files in remote directory.
  527. /// </summary>
  528. /// <param name="path">The path.</param>
  529. /// <param name="listCallback">The list callback.</param>
  530. /// <returns>
  531. /// A list of files.
  532. /// </returns>
  533. /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null"/>.</exception>
  534. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  535. /// <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>
  536. /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>
  537. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  538. public IEnumerable<ISftpFile> ListDirectory(string path, Action<int>? listCallback = null)
  539. {
  540. CheckDisposed();
  541. return InternalListDirectory(path, asyncResult: null, listCallback);
  542. }
  543. /// <summary>
  544. /// Asynchronously enumerates the files in remote directory.
  545. /// </summary>
  546. /// <param name="path">The path.</param>
  547. /// <param name="cancellationToken">The <see cref="CancellationToken"/> to observe.</param>
  548. /// <returns>
  549. /// An <see cref="IAsyncEnumerable{T}"/> of <see cref="ISftpFile"/> that represents the asynchronous enumeration operation.
  550. /// The enumeration contains an async stream of <see cref="ISftpFile"/> for the files in the directory specified by <paramref name="path" />.
  551. /// </returns>
  552. /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null"/>.</exception>
  553. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  554. /// <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>
  555. /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>
  556. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  557. public async IAsyncEnumerable<ISftpFile> ListDirectoryAsync(string path, [EnumeratorCancellation] CancellationToken cancellationToken)
  558. {
  559. CheckDisposed();
  560. ThrowHelper.ThrowIfNull(path);
  561. if (_sftpSession is null)
  562. {
  563. throw new SshConnectionException("Client not connected.");
  564. }
  565. cancellationToken.ThrowIfCancellationRequested();
  566. var fullPath = await _sftpSession.GetCanonicalPathAsync(path, cancellationToken).ConfigureAwait(false);
  567. var handle = await _sftpSession.RequestOpenDirAsync(fullPath, cancellationToken).ConfigureAwait(false);
  568. try
  569. {
  570. var basePath = (fullPath[fullPath.Length - 1] == '/') ?
  571. fullPath :
  572. fullPath + '/';
  573. while (true)
  574. {
  575. var files = await _sftpSession.RequestReadDirAsync(handle, cancellationToken).ConfigureAwait(false);
  576. if (files is null)
  577. {
  578. break;
  579. }
  580. foreach (var file in files)
  581. {
  582. yield return new SftpFile(_sftpSession, basePath + file.Key, file.Value);
  583. }
  584. }
  585. }
  586. finally
  587. {
  588. await _sftpSession.RequestCloseAsync(handle, cancellationToken).ConfigureAwait(false);
  589. }
  590. }
  591. /// <summary>
  592. /// Begins an asynchronous operation of retrieving list of files in remote directory.
  593. /// </summary>
  594. /// <param name="path">The path.</param>
  595. /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>
  596. /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests.</param>
  597. /// <param name="listCallback">The list callback.</param>
  598. /// <returns>
  599. /// An <see cref="IAsyncResult" /> that references the asynchronous operation.
  600. /// </returns>
  601. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  602. public IAsyncResult BeginListDirectory(string path, AsyncCallback? asyncCallback, object? state, Action<int>? listCallback = null)
  603. {
  604. CheckDisposed();
  605. var asyncResult = new SftpListDirectoryAsyncResult(asyncCallback, state);
  606. ThreadAbstraction.ExecuteThread(() =>
  607. {
  608. try
  609. {
  610. var result = InternalListDirectory(path, asyncResult, listCallback);
  611. asyncResult.SetAsCompleted(result, completedSynchronously: false);
  612. }
  613. catch (Exception exp)
  614. {
  615. asyncResult.SetAsCompleted(exp, completedSynchronously: false);
  616. }
  617. });
  618. return asyncResult;
  619. }
  620. /// <summary>
  621. /// Ends an asynchronous operation of retrieving list of files in remote directory.
  622. /// </summary>
  623. /// <param name="asyncResult">The pending asynchronous SFTP request.</param>
  624. /// <returns>
  625. /// A list of files.
  626. /// </returns>
  627. /// <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>
  628. public IEnumerable<ISftpFile> EndListDirectory(IAsyncResult asyncResult)
  629. {
  630. if (asyncResult is not SftpListDirectoryAsyncResult ar || ar.EndInvokeCalled)
  631. {
  632. 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.");
  633. }
  634. // Wait for operation to complete, then return result or throw exception
  635. return ar.EndInvoke();
  636. }
  637. /// <summary>
  638. /// Gets reference to remote file or directory.
  639. /// </summary>
  640. /// <param name="path">The path.</param>
  641. /// <returns>
  642. /// A reference to <see cref="ISftpFile"/> file object.
  643. /// </returns>
  644. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  645. /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>
  646. /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null"/>.</exception>
  647. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  648. public ISftpFile Get(string path)
  649. {
  650. CheckDisposed();
  651. ThrowHelper.ThrowIfNull(path);
  652. if (_sftpSession is null)
  653. {
  654. throw new SshConnectionException("Client not connected.");
  655. }
  656. var fullPath = _sftpSession.GetCanonicalPath(path);
  657. var attributes = _sftpSession.RequestLStat(fullPath);
  658. return new SftpFile(_sftpSession, fullPath, attributes);
  659. }
  660. /// <summary>
  661. /// Gets reference to remote file or directory.
  662. /// </summary>
  663. /// <param name="path">The path.</param>
  664. /// <param name="cancellationToken">The <see cref="CancellationToken"/> to observe.</param>
  665. /// <returns>
  666. /// A <see cref="Task{ISftpFile}"/> that represents the get operation.
  667. /// The task result contains the reference to <see cref="ISftpFile"/> file object.
  668. /// </returns>
  669. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  670. /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>
  671. /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null"/>.</exception>
  672. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  673. public async Task<ISftpFile> GetAsync(string path, CancellationToken cancellationToken)
  674. {
  675. CheckDisposed();
  676. ThrowHelper.ThrowIfNull(path);
  677. if (_sftpSession is null)
  678. {
  679. throw new SshConnectionException("Client not connected.");
  680. }
  681. cancellationToken.ThrowIfCancellationRequested();
  682. var fullPath = await _sftpSession.GetCanonicalPathAsync(path, cancellationToken).ConfigureAwait(false);
  683. var attributes = await _sftpSession.RequestLStatAsync(fullPath, cancellationToken).ConfigureAwait(false);
  684. return new SftpFile(_sftpSession, fullPath, attributes);
  685. }
  686. /// <summary>
  687. /// Checks whether file or directory exists.
  688. /// </summary>
  689. /// <param name="path">The path.</param>
  690. /// <returns>
  691. /// <see langword="true"/> if directory or file exists; otherwise <see langword="false"/>.
  692. /// </returns>
  693. /// <exception cref="ArgumentException"><paramref name="path"/> is <see langword="null"/> or contains only whitespace characters.</exception>
  694. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  695. /// <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>
  696. /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>
  697. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  698. public bool Exists(string path)
  699. {
  700. CheckDisposed();
  701. ThrowHelper.ThrowIfNullOrWhiteSpace(path);
  702. if (_sftpSession is null)
  703. {
  704. throw new SshConnectionException("Client not connected.");
  705. }
  706. var fullPath = _sftpSession.GetCanonicalPath(path);
  707. /*
  708. * Using SSH_FXP_REALPATH is not an alternative as the SFTP specification has not always
  709. * been clear on how the server should respond when the specified path is not present on
  710. * the server:
  711. *
  712. * SSH 1 to 4:
  713. * No mention of how the server should respond if the path is not present on the server.
  714. *
  715. * SSH 5:
  716. * The server SHOULD fail the request if the path is not present on the server.
  717. *
  718. * SSH 6:
  719. * Draft 06: The server SHOULD fail the request if the path is not present on the server.
  720. * Draft 07 to 13: The server MUST NOT fail the request if the path does not exist.
  721. *
  722. * Note that SSH 6 (draft 06 and forward) allows for more control options, but we
  723. * currently only support up to v3.
  724. */
  725. try
  726. {
  727. _ = _sftpSession.RequestLStat(fullPath);
  728. return true;
  729. }
  730. catch (SftpPathNotFoundException)
  731. {
  732. return false;
  733. }
  734. }
  735. /// <summary>
  736. /// Checks whether file or directory exists.
  737. /// </summary>
  738. /// <param name="path">The path.</param>
  739. /// <param name="cancellationToken">The <see cref="CancellationToken"/> to observe.</param>
  740. /// <returns>
  741. /// A <see cref="Task{T}"/> that represents the exists operation.
  742. /// The task result contains <see langword="true"/> if directory or file exists; otherwise <see langword="false"/>.
  743. /// </returns>
  744. /// <exception cref="ArgumentException"><paramref name="path"/> is <see langword="null"/> or contains only whitespace characters.</exception>
  745. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  746. /// <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>
  747. /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the remote host.</exception>
  748. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  749. public async Task<bool> ExistsAsync(string path, CancellationToken cancellationToken = default)
  750. {
  751. CheckDisposed();
  752. ThrowHelper.ThrowIfNullOrWhiteSpace(path);
  753. if (_sftpSession is null)
  754. {
  755. throw new SshConnectionException("Client not connected.");
  756. }
  757. cancellationToken.ThrowIfCancellationRequested();
  758. var fullPath = await _sftpSession.GetCanonicalPathAsync(path, cancellationToken).ConfigureAwait(false);
  759. /*
  760. * Using SSH_FXP_REALPATH is not an alternative as the SFTP specification has not always
  761. * been clear on how the server should respond when the specified path is not present on
  762. * the server:
  763. *
  764. * SSH 1 to 4:
  765. * No mention of how the server should respond if the path is not present on the server.
  766. *
  767. * SSH 5:
  768. * The server SHOULD fail the request if the path is not present on the server.
  769. *
  770. * SSH 6:
  771. * Draft 06: The server SHOULD fail the request if the path is not present on the server.
  772. * Draft 07 to 13: The server MUST NOT fail the request if the path does not exist.
  773. *
  774. * Note that SSH 6 (draft 06 and forward) allows for more control options, but we
  775. * currently only support up to v3.
  776. */
  777. try
  778. {
  779. _ = await _sftpSession.RequestLStatAsync(fullPath, cancellationToken).ConfigureAwait(false);
  780. return true;
  781. }
  782. catch (SftpPathNotFoundException)
  783. {
  784. return false;
  785. }
  786. }
  787. /// <inheritdoc />
  788. public void DownloadFile(string path, Stream output, Action<ulong>? downloadCallback = null)
  789. {
  790. CheckDisposed();
  791. InternalDownloadFile(path, output, asyncResult: null, downloadCallback);
  792. }
  793. /// <inheritdoc />
  794. public Task DownloadFileAsync(string path, Stream output, CancellationToken cancellationToken = default)
  795. {
  796. CheckDisposed();
  797. return InternalDownloadFileAsync(path, output, cancellationToken);
  798. }
  799. /// <summary>
  800. /// Begins an asynchronous file downloading into the stream.
  801. /// </summary>
  802. /// <param name="path">The path.</param>
  803. /// <param name="output">The output.</param>
  804. /// <returns>
  805. /// An <see cref="IAsyncResult" /> that references the asynchronous operation.
  806. /// </returns>
  807. /// <exception cref="ArgumentNullException"><paramref name="output" /> is <see langword="null"/>.</exception>
  808. /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whitespace characters.</exception>
  809. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  810. /// <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>
  811. /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>
  812. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  813. /// <remarks>
  814. /// Method calls made by this method to <paramref name="output" />, may under certain conditions result in exceptions thrown by the stream.
  815. /// </remarks>
  816. public IAsyncResult BeginDownloadFile(string path, Stream output)
  817. {
  818. return BeginDownloadFile(path, output, asyncCallback: null, state: null);
  819. }
  820. /// <summary>
  821. /// Begins an asynchronous file downloading into the stream.
  822. /// </summary>
  823. /// <param name="path">The path.</param>
  824. /// <param name="output">The output.</param>
  825. /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>
  826. /// <returns>
  827. /// An <see cref="IAsyncResult" /> that references the asynchronous operation.
  828. /// </returns>
  829. /// <exception cref="ArgumentNullException"><paramref name="output" /> is <see langword="null"/>.</exception>
  830. /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whitespace characters.</exception>
  831. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  832. /// <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>
  833. /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>
  834. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  835. /// <remarks>
  836. /// Method calls made by this method to <paramref name="output" />, may under certain conditions result in exceptions thrown by the stream.
  837. /// </remarks>
  838. public IAsyncResult BeginDownloadFile(string path, Stream output, AsyncCallback? asyncCallback)
  839. {
  840. return BeginDownloadFile(path, output, asyncCallback, state: null);
  841. }
  842. /// <summary>
  843. /// Begins an asynchronous file downloading into the stream.
  844. /// </summary>
  845. /// <param name="path">The path.</param>
  846. /// <param name="output">The output.</param>
  847. /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>
  848. /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests.</param>
  849. /// <param name="downloadCallback">The download callback.</param>
  850. /// <returns>
  851. /// An <see cref="IAsyncResult" /> that references the asynchronous operation.
  852. /// </returns>
  853. /// <exception cref="ArgumentNullException"><paramref name="output" /> is <see langword="null"/>.</exception>
  854. /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whitespace characters.</exception>
  855. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  856. /// <remarks>
  857. /// Method calls made by this method to <paramref name="output" />, may under certain conditions result in exceptions thrown by the stream.
  858. /// </remarks>
  859. public IAsyncResult BeginDownloadFile(string path, Stream output, AsyncCallback? asyncCallback, object? state, Action<ulong>? downloadCallback = null)
  860. {
  861. CheckDisposed();
  862. ThrowHelper.ThrowIfNullOrWhiteSpace(path);
  863. ThrowHelper.ThrowIfNull(output);
  864. var asyncResult = new SftpDownloadAsyncResult(asyncCallback, state);
  865. ThreadAbstraction.ExecuteThread(() =>
  866. {
  867. try
  868. {
  869. InternalDownloadFile(path, output, asyncResult, downloadCallback);
  870. asyncResult.SetAsCompleted(exception: null, completedSynchronously: false);
  871. }
  872. catch (Exception exp)
  873. {
  874. asyncResult.SetAsCompleted(exp, completedSynchronously: false);
  875. }
  876. });
  877. return asyncResult;
  878. }
  879. /// <summary>
  880. /// Ends an asynchronous file downloading into the stream.
  881. /// </summary>
  882. /// <param name="asyncResult">The pending asynchronous SFTP request.</param>
  883. /// <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>
  884. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  885. /// <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>
  886. /// <exception cref="SftpPathNotFoundException">The path was not found on the remote host.</exception>
  887. /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>
  888. public void EndDownloadFile(IAsyncResult asyncResult)
  889. {
  890. if (asyncResult is not SftpDownloadAsyncResult ar || ar.EndInvokeCalled)
  891. {
  892. 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.");
  893. }
  894. // Wait for operation to complete, then return result or throw exception
  895. ar.EndInvoke();
  896. }
  897. /// <inheritdoc/>
  898. public void UploadFile(Stream input, string path, Action<ulong>? uploadCallback = null)
  899. {
  900. UploadFile(input, path, canOverride: true, uploadCallback);
  901. }
  902. /// <inheritdoc/>
  903. public void UploadFile(Stream input, string path, bool canOverride, Action<ulong>? uploadCallback = null)
  904. {
  905. ThrowHelper.ThrowIfNull(input);
  906. ThrowHelper.ThrowIfNullOrWhiteSpace(path);
  907. CheckDisposed();
  908. var flags = Flags.Write | Flags.Truncate;
  909. if (canOverride)
  910. {
  911. flags |= Flags.CreateNewOrOpen;
  912. }
  913. else
  914. {
  915. flags |= Flags.CreateNew;
  916. }
  917. InternalUploadFile(
  918. input,
  919. path,
  920. flags,
  921. asyncResult: null,
  922. uploadCallback,
  923. isAsync: false,
  924. default).GetAwaiter().GetResult();
  925. }
  926. /// <inheritdoc />
  927. public Task UploadFileAsync(Stream input, string path, CancellationToken cancellationToken = default)
  928. {
  929. ThrowHelper.ThrowIfNull(input);
  930. ThrowHelper.ThrowIfNullOrWhiteSpace(path);
  931. CheckDisposed();
  932. return InternalUploadFile(
  933. input,
  934. path,
  935. Flags.Write | Flags.Truncate | Flags.CreateNewOrOpen,
  936. asyncResult: null,
  937. uploadCallback: null,
  938. isAsync: true,
  939. cancellationToken);
  940. }
  941. /// <summary>
  942. /// Begins an asynchronous uploading the stream into remote file.
  943. /// </summary>
  944. /// <param name="input">Data input stream.</param>
  945. /// <param name="path">Remote file path.</param>
  946. /// <returns>
  947. /// An <see cref="IAsyncResult" /> that references the asynchronous operation.
  948. /// </returns>
  949. /// <exception cref="ArgumentNullException"><paramref name="input" /> is <see langword="null"/>.</exception>
  950. /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whitespace characters.</exception>
  951. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  952. /// <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>
  953. /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>
  954. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  955. /// <remarks>
  956. /// <para>
  957. /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in exceptions thrown by the stream.
  958. /// </para>
  959. /// <para>
  960. /// If the remote file already exists, it is overwritten and truncated.
  961. /// </para>
  962. /// </remarks>
  963. public IAsyncResult BeginUploadFile(Stream input, string path)
  964. {
  965. return BeginUploadFile(input, path, canOverride: true, asyncCallback: null, state: null);
  966. }
  967. /// <summary>
  968. /// Begins an asynchronous uploading the stream into remote file.
  969. /// </summary>
  970. /// <param name="input">Data input stream.</param>
  971. /// <param name="path">Remote file path.</param>
  972. /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>
  973. /// <returns>
  974. /// An <see cref="IAsyncResult" /> that references the asynchronous operation.
  975. /// </returns>
  976. /// <exception cref="ArgumentNullException"><paramref name="input" /> is <see langword="null"/>.</exception>
  977. /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whitespace characters.</exception>
  978. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  979. /// <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>
  980. /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>
  981. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  982. /// <remarks>
  983. /// <para>
  984. /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in exceptions thrown by the stream.
  985. /// </para>
  986. /// <para>
  987. /// If the remote file already exists, it is overwritten and truncated.
  988. /// </para>
  989. /// </remarks>
  990. public IAsyncResult BeginUploadFile(Stream input, string path, AsyncCallback? asyncCallback)
  991. {
  992. return BeginUploadFile(input, path, canOverride: true, asyncCallback, state: null);
  993. }
  994. /// <summary>
  995. /// Begins an asynchronous uploading the stream into remote file.
  996. /// </summary>
  997. /// <param name="input">Data input stream.</param>
  998. /// <param name="path">Remote file path.</param>
  999. /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>
  1000. /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests.</param>
  1001. /// <param name="uploadCallback">The upload callback.</param>
  1002. /// <returns>
  1003. /// An <see cref="IAsyncResult" /> that references the asynchronous operation.
  1004. /// </returns>
  1005. /// <exception cref="ArgumentNullException"><paramref name="input" /> is <see langword="null"/>.</exception>
  1006. /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whitespace characters.</exception>
  1007. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1008. /// <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>
  1009. /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>
  1010. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1011. /// <remarks>
  1012. /// <para>
  1013. /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in exceptions thrown by the stream.
  1014. /// </para>
  1015. /// <para>
  1016. /// If the remote file already exists, it is overwritten and truncated.
  1017. /// </para>
  1018. /// </remarks>
  1019. public IAsyncResult BeginUploadFile(Stream input, string path, AsyncCallback? asyncCallback, object? state, Action<ulong>? uploadCallback = null)
  1020. {
  1021. return BeginUploadFile(input, path, canOverride: true, asyncCallback, state, uploadCallback);
  1022. }
  1023. /// <summary>
  1024. /// Begins an asynchronous uploading the stream into remote file.
  1025. /// </summary>
  1026. /// <param name="input">Data input stream.</param>
  1027. /// <param name="path">Remote file path.</param>
  1028. /// <param name="canOverride">Specified whether an existing file can be overwritten.</param>
  1029. /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>
  1030. /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests.</param>
  1031. /// <param name="uploadCallback">The upload callback.</param>
  1032. /// <returns>
  1033. /// An <see cref="IAsyncResult" /> that references the asynchronous operation.
  1034. /// </returns>
  1035. /// <exception cref="ArgumentNullException"><paramref name="input" /> is <see langword="null"/>.</exception>
  1036. /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whitespace characters.</exception>
  1037. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1038. /// <remarks>
  1039. /// <para>
  1040. /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in exceptions thrown by the stream.
  1041. /// </para>
  1042. /// <para>
  1043. /// When <paramref name="path"/> refers to an existing file, set <paramref name="canOverride"/> to <see langword="true"/> to overwrite and truncate that file.
  1044. /// If <paramref name="canOverride"/> is <see langword="false"/>, the upload will fail and <see cref="EndUploadFile(IAsyncResult)"/> will throw an
  1045. /// <see cref="SshException"/>.
  1046. /// </para>
  1047. /// </remarks>
  1048. public IAsyncResult BeginUploadFile(Stream input, string path, bool canOverride, AsyncCallback? asyncCallback, object? state, Action<ulong>? uploadCallback = null)
  1049. {
  1050. ThrowHelper.ThrowIfNull(input);
  1051. ThrowHelper.ThrowIfNullOrWhiteSpace(path);
  1052. CheckDisposed();
  1053. var flags = Flags.Write | Flags.Truncate;
  1054. if (canOverride)
  1055. {
  1056. flags |= Flags.CreateNewOrOpen;
  1057. }
  1058. else
  1059. {
  1060. flags |= Flags.CreateNew;
  1061. }
  1062. var asyncResult = new SftpUploadAsyncResult(asyncCallback, state);
  1063. _ = DoUploadAndSetResult();
  1064. async Task DoUploadAndSetResult()
  1065. {
  1066. try
  1067. {
  1068. await InternalUploadFile(
  1069. input,
  1070. path,
  1071. flags,
  1072. asyncResult,
  1073. uploadCallback,
  1074. isAsync: true,
  1075. CancellationToken.None).ConfigureAwait(false);
  1076. asyncResult.SetAsCompleted(exception: null, completedSynchronously: false);
  1077. }
  1078. catch (Exception exp)
  1079. {
  1080. asyncResult.SetAsCompleted(exp, completedSynchronously: false);
  1081. }
  1082. }
  1083. return asyncResult;
  1084. }
  1085. /// <summary>
  1086. /// Ends an asynchronous uploading the stream into remote file.
  1087. /// </summary>
  1088. /// <param name="asyncResult">The pending asynchronous SFTP request.</param>
  1089. /// <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>
  1090. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1091. /// <exception cref="SftpPathNotFoundException">The directory of the file was not found on the remote host.</exception>
  1092. /// <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>
  1093. /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>
  1094. public void EndUploadFile(IAsyncResult asyncResult)
  1095. {
  1096. if (asyncResult is not SftpUploadAsyncResult ar || ar.EndInvokeCalled)
  1097. {
  1098. 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.");
  1099. }
  1100. // Wait for operation to complete, then return result or throw exception
  1101. ar.EndInvoke();
  1102. }
  1103. /// <summary>
  1104. /// Gets status using statvfs@openssh.com request.
  1105. /// </summary>
  1106. /// <param name="path">The path.</param>
  1107. /// <returns>
  1108. /// A <see cref="SftpFileSystemInformation"/> instance that contains file status information.
  1109. /// </returns>
  1110. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1111. /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null"/>.</exception>
  1112. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1113. public SftpFileSystemInformation GetStatus(string path)
  1114. {
  1115. CheckDisposed();
  1116. ThrowHelper.ThrowIfNull(path);
  1117. if (_sftpSession is null)
  1118. {
  1119. throw new SshConnectionException("Client not connected.");
  1120. }
  1121. var fullPath = _sftpSession.GetCanonicalPath(path);
  1122. return _sftpSession.RequestStatVfs(fullPath);
  1123. }
  1124. /// <summary>
  1125. /// Asynchronously gets status using statvfs@openssh.com request.
  1126. /// </summary>
  1127. /// <param name="path">The path.</param>
  1128. /// <param name="cancellationToken">The <see cref="CancellationToken"/> to observe.</param>
  1129. /// <returns>
  1130. /// A <see cref="Task{SftpFileSystemInformation}"/> that represents the status operation.
  1131. /// The task result contains the <see cref="SftpFileSystemInformation"/> instance that contains file status information.
  1132. /// </returns>
  1133. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1134. /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null"/>.</exception>
  1135. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1136. public async Task<SftpFileSystemInformation> GetStatusAsync(string path, CancellationToken cancellationToken)
  1137. {
  1138. CheckDisposed();
  1139. ThrowHelper.ThrowIfNull(path);
  1140. if (_sftpSession is null)
  1141. {
  1142. throw new SshConnectionException("Client not connected.");
  1143. }
  1144. cancellationToken.ThrowIfCancellationRequested();
  1145. var fullPath = await _sftpSession.GetCanonicalPathAsync(path, cancellationToken).ConfigureAwait(false);
  1146. return await _sftpSession.RequestStatVfsAsync(fullPath, cancellationToken).ConfigureAwait(false);
  1147. }
  1148. #region File Methods
  1149. /// <summary>
  1150. /// Appends lines to a file, creating the file if it does not already exist.
  1151. /// </summary>
  1152. /// <param name="path">The file to append the lines to. The file is created if it does not already exist.</param>
  1153. /// <param name="contents">The lines to append to the file.</param>
  1154. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>. <para>-or-</para> <paramref name="contents"/> is <see langword="null"/>.</exception>
  1155. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1156. /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>
  1157. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1158. /// <remarks>
  1159. /// The characters are written to the file using UTF-8 encoding without a byte-order mark (BOM).
  1160. /// </remarks>
  1161. public void AppendAllLines(string path, IEnumerable<string> contents)
  1162. {
  1163. CheckDisposed();
  1164. ThrowHelper.ThrowIfNull(contents);
  1165. using (var stream = AppendText(path))
  1166. {
  1167. foreach (var line in contents)
  1168. {
  1169. stream.WriteLine(line);
  1170. }
  1171. }
  1172. }
  1173. /// <summary>
  1174. /// Appends lines to a file by using a specified encoding, creating the file if it does not already exist.
  1175. /// </summary>
  1176. /// <param name="path">The file to append the lines to. The file is created if it does not already exist.</param>
  1177. /// <param name="contents">The lines to append to the file.</param>
  1178. /// <param name="encoding">The character encoding to use.</param>
  1179. /// <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>
  1180. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1181. /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>
  1182. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1183. public void AppendAllLines(string path, IEnumerable<string> contents, Encoding encoding)
  1184. {
  1185. CheckDisposed();
  1186. ThrowHelper.ThrowIfNull(contents);
  1187. using (var stream = AppendText(path, encoding))
  1188. {
  1189. foreach (var line in contents)
  1190. {
  1191. stream.WriteLine(line);
  1192. }
  1193. }
  1194. }
  1195. /// <summary>
  1196. /// Appends the specified string to the file, creating the file if it does not already exist.
  1197. /// </summary>
  1198. /// <param name="path">The file to append the specified string to.</param>
  1199. /// <param name="contents">The string to append to the file.</param>
  1200. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>. <para>-or-</para> <paramref name="contents"/> is <see langword="null"/>.</exception>
  1201. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1202. /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>
  1203. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1204. /// <remarks>
  1205. /// The characters are written to the file using UTF-8 encoding without a Byte-Order Mark (BOM).
  1206. /// </remarks>
  1207. public void AppendAllText(string path, string contents)
  1208. {
  1209. using (var stream = AppendText(path))
  1210. {
  1211. stream.Write(contents);
  1212. }
  1213. }
  1214. /// <summary>
  1215. /// Appends the specified string to the file, creating the file if it does not already exist.
  1216. /// </summary>
  1217. /// <param name="path">The file to append the specified string to.</param>
  1218. /// <param name="contents">The string to append to the file.</param>
  1219. /// <param name="encoding">The character encoding to use.</param>
  1220. /// <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>
  1221. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1222. /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>
  1223. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1224. public void AppendAllText(string path, string contents, Encoding encoding)
  1225. {
  1226. using (var stream = AppendText(path, encoding))
  1227. {
  1228. stream.Write(contents);
  1229. }
  1230. }
  1231. /// <summary>
  1232. /// Creates a <see cref="StreamWriter"/> that appends UTF-8 encoded text to the specified file,
  1233. /// creating the file if it does not already exist.
  1234. /// </summary>
  1235. /// <param name="path">The path to the file to append to.</param>
  1236. /// <returns>
  1237. /// A <see cref="StreamWriter"/> that appends text to a file using UTF-8 encoding without a
  1238. /// Byte-Order Mark (BOM).
  1239. /// </returns>
  1240. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  1241. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1242. /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>
  1243. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1244. public StreamWriter AppendText(string path)
  1245. {
  1246. return AppendText(path, Utf8NoBOM);
  1247. }
  1248. /// <summary>
  1249. /// Creates a <see cref="StreamWriter"/> that appends text to a file using the specified
  1250. /// encoding, creating the file if it does not already exist.
  1251. /// </summary>
  1252. /// <param name="path">The path to the file to append to.</param>
  1253. /// <param name="encoding">The character encoding to use.</param>
  1254. /// <returns>
  1255. /// A <see cref="StreamWriter"/> that appends text to a file using the specified encoding.
  1256. /// </returns>
  1257. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>. <para>-or-</para> <paramref name="encoding"/> is <see langword="null"/>.</exception>
  1258. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1259. /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>
  1260. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1261. public StreamWriter AppendText(string path, Encoding encoding)
  1262. {
  1263. CheckDisposed();
  1264. ThrowHelper.ThrowIfNull(encoding);
  1265. return new StreamWriter(Open(path, FileMode.Append, FileAccess.Write), encoding);
  1266. }
  1267. /// <summary>
  1268. /// Creates or overwrites a file in the specified path.
  1269. /// </summary>
  1270. /// <param name="path">The path and name of the file to create.</param>
  1271. /// <returns>
  1272. /// A <see cref="SftpFileStream"/> that provides read/write access to the file specified in path.
  1273. /// </returns>
  1274. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  1275. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1276. /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>
  1277. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1278. /// <remarks>
  1279. /// If the target file already exists, it is first truncated to zero bytes.
  1280. /// </remarks>
  1281. public SftpFileStream Create(string path)
  1282. {
  1283. return Create(path, (int)_bufferSize);
  1284. }
  1285. /// <summary>
  1286. /// Creates or overwrites the specified file.
  1287. /// </summary>
  1288. /// <param name="path">The path and name of the file to create.</param>
  1289. /// <param name="bufferSize">The maximum number of bytes buffered for reads and writes to the file.</param>
  1290. /// <returns>
  1291. /// A <see cref="SftpFileStream"/> that provides read/write access to the file specified in path.
  1292. /// </returns>
  1293. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  1294. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1295. /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>
  1296. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1297. /// <remarks>
  1298. /// If the target file already exists, it is first truncated to zero bytes.
  1299. /// </remarks>
  1300. public SftpFileStream Create(string path, int bufferSize)
  1301. {
  1302. CheckDisposed();
  1303. return SftpFileStream.Open(_sftpSession, path, FileMode.Create, FileAccess.ReadWrite, bufferSize);
  1304. }
  1305. /// <inheritdoc/>
  1306. public StreamWriter CreateText(string path)
  1307. {
  1308. return CreateText(path, Utf8NoBOM);
  1309. }
  1310. /// <inheritdoc/>
  1311. public StreamWriter CreateText(string path, Encoding encoding)
  1312. {
  1313. CheckDisposed();
  1314. return new StreamWriter(Open(path, FileMode.Create, FileAccess.Write), encoding);
  1315. }
  1316. /// <summary>
  1317. /// Deletes the specified file or directory.
  1318. /// </summary>
  1319. /// <param name="path">The name of the file or directory to be deleted. Wildcard characters are not supported.</param>
  1320. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  1321. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1322. /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>
  1323. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1324. public void Delete(string path)
  1325. {
  1326. var file = Get(path);
  1327. file.Delete();
  1328. }
  1329. /// <inheritdoc />
  1330. public async Task DeleteAsync(string path, CancellationToken cancellationToken = default)
  1331. {
  1332. var file = await GetAsync(path, cancellationToken).ConfigureAwait(false);
  1333. await file.DeleteAsync(cancellationToken).ConfigureAwait(false);
  1334. }
  1335. /// <summary>
  1336. /// Returns the date and time the specified file or directory was last accessed.
  1337. /// </summary>
  1338. /// <param name="path">The file or directory for which to obtain access date and time information.</param>
  1339. /// <returns>
  1340. /// A <see cref="DateTime"/> structure set to the date and time that the specified file or directory was last accessed.
  1341. /// This value is expressed in local time.
  1342. /// </returns>
  1343. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  1344. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1345. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1346. public DateTime GetLastAccessTime(string path)
  1347. {
  1348. var file = Get(path);
  1349. return file.LastAccessTime;
  1350. }
  1351. /// <summary>
  1352. /// Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last accessed.
  1353. /// </summary>
  1354. /// <param name="path">The file or directory for which to obtain access date and time information.</param>
  1355. /// <returns>
  1356. /// A <see cref="DateTime"/> structure set to the date and time that the specified file or directory was last accessed.
  1357. /// This value is expressed in UTC time.
  1358. /// </returns>
  1359. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  1360. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1361. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1362. public DateTime GetLastAccessTimeUtc(string path)
  1363. {
  1364. var lastAccessTime = GetLastAccessTime(path);
  1365. return lastAccessTime.ToUniversalTime();
  1366. }
  1367. /// <summary>
  1368. /// Returns the date and time the specified file or directory was last written to.
  1369. /// </summary>
  1370. /// <param name="path">The file or directory for which to obtain write date and time information.</param>
  1371. /// <returns>
  1372. /// A <see cref="DateTime"/> structure set to the date and time that the specified file or directory was last written to.
  1373. /// This value is expressed in local time.
  1374. /// </returns>
  1375. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  1376. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1377. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1378. public DateTime GetLastWriteTime(string path)
  1379. {
  1380. var file = Get(path);
  1381. return file.LastWriteTime;
  1382. }
  1383. /// <summary>
  1384. /// Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last written to.
  1385. /// </summary>
  1386. /// <param name="path">The file or directory for which to obtain write date and time information.</param>
  1387. /// <returns>
  1388. /// A <see cref="DateTime"/> structure set to the date and time that the specified file or directory was last written to.
  1389. /// This value is expressed in UTC time.
  1390. /// </returns>
  1391. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  1392. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1393. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1394. public DateTime GetLastWriteTimeUtc(string path)
  1395. {
  1396. var lastWriteTime = GetLastWriteTime(path);
  1397. return lastWriteTime.ToUniversalTime();
  1398. }
  1399. /// <summary>
  1400. /// Opens a <see cref="SftpFileStream"/> on the specified path with read/write access.
  1401. /// </summary>
  1402. /// <param name="path">The file to open.</param>
  1403. /// <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>
  1404. /// <returns>
  1405. /// An unshared <see cref="SftpFileStream"/> that provides access to the specified file, with the specified mode and read/write access.
  1406. /// </returns>
  1407. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  1408. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1409. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1410. public SftpFileStream Open(string path, FileMode mode)
  1411. {
  1412. return Open(path, mode, FileAccess.ReadWrite);
  1413. }
  1414. /// <summary>
  1415. /// Opens a <see cref="SftpFileStream"/> on the specified path, with the specified mode and access.
  1416. /// </summary>
  1417. /// <param name="path">The file to open.</param>
  1418. /// <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>
  1419. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the file.</param>
  1420. /// <returns>
  1421. /// An unshared <see cref="SftpFileStream"/> that provides access to the specified file, with the specified mode and access.
  1422. /// </returns>
  1423. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  1424. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1425. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1426. public SftpFileStream Open(string path, FileMode mode, FileAccess access)
  1427. {
  1428. CheckDisposed();
  1429. return SftpFileStream.Open(_sftpSession, path, mode, access, (int)_bufferSize);
  1430. }
  1431. /// <summary>
  1432. /// Asynchronously opens a <see cref="SftpFileStream"/> on the specified path, with the specified mode and access.
  1433. /// </summary>
  1434. /// <param name="path">The file to open.</param>
  1435. /// <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>
  1436. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the file.</param>
  1437. /// <param name="cancellationToken">The <see cref="CancellationToken"/> to observe.</param>
  1438. /// <returns>
  1439. /// A <see cref="Task{SftpFileStream}"/> that represents the asynchronous open operation.
  1440. /// The task result contains the <see cref="SftpFileStream"/> that provides access to the specified file, with the specified mode and access.
  1441. /// </returns>
  1442. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  1443. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1444. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1445. public Task<SftpFileStream> OpenAsync(string path, FileMode mode, FileAccess access, CancellationToken cancellationToken)
  1446. {
  1447. CheckDisposed();
  1448. return SftpFileStream.OpenAsync(_sftpSession, path, mode, access, (int)_bufferSize, cancellationToken);
  1449. }
  1450. /// <summary>
  1451. /// Opens an existing file for reading.
  1452. /// </summary>
  1453. /// <param name="path">The file to be opened for reading.</param>
  1454. /// <returns>
  1455. /// A read-only <see cref="SftpFileStream"/> on the specified path.
  1456. /// </returns>
  1457. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  1458. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1459. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1460. public SftpFileStream OpenRead(string path)
  1461. {
  1462. return Open(path, FileMode.Open, FileAccess.Read);
  1463. }
  1464. /// <summary>
  1465. /// Opens an existing UTF-8 encoded text file for reading.
  1466. /// </summary>
  1467. /// <param name="path">The file to be opened for reading.</param>
  1468. /// <returns>
  1469. /// A <see cref="StreamReader"/> on the specified path.
  1470. /// </returns>
  1471. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  1472. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1473. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1474. public StreamReader OpenText(string path)
  1475. {
  1476. return new StreamReader(OpenRead(path), Encoding.UTF8);
  1477. }
  1478. /// <summary>
  1479. /// Opens a file for writing.
  1480. /// </summary>
  1481. /// <param name="path">The file to be opened for writing.</param>
  1482. /// <returns>
  1483. /// An unshared <see cref="SftpFileStream"/> object on the specified path with <see cref="FileAccess.Write"/> access.
  1484. /// </returns>
  1485. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  1486. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1487. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1488. /// <remarks>
  1489. /// If the file does not exist, it is created.
  1490. /// </remarks>
  1491. public SftpFileStream OpenWrite(string path)
  1492. {
  1493. return Open(path, FileMode.OpenOrCreate, FileAccess.Write);
  1494. }
  1495. /// <summary>
  1496. /// Opens a binary file, reads the contents of the file into a byte array, and closes the file.
  1497. /// </summary>
  1498. /// <param name="path">The file to open for reading.</param>
  1499. /// <returns>
  1500. /// A byte array containing the contents of the file.
  1501. /// </returns>
  1502. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  1503. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1504. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1505. public byte[] ReadAllBytes(string path)
  1506. {
  1507. using (var stream = OpenRead(path))
  1508. {
  1509. var buffer = new byte[stream.Length];
  1510. stream.ReadExactly(buffer, 0, buffer.Length);
  1511. return buffer;
  1512. }
  1513. }
  1514. /// <summary>
  1515. /// Opens a text file, reads all lines of the file using UTF-8 encoding, and closes the file.
  1516. /// </summary>
  1517. /// <param name="path">The file to open for reading.</param>
  1518. /// <returns>
  1519. /// A string array containing all lines of the file.
  1520. /// </returns>
  1521. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  1522. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1523. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1524. public string[] ReadAllLines(string path)
  1525. {
  1526. return ReadAllLines(path, Encoding.UTF8);
  1527. }
  1528. /// <summary>
  1529. /// Opens a file, reads all lines of the file with the specified encoding, and closes the file.
  1530. /// </summary>
  1531. /// <param name="path">The file to open for reading.</param>
  1532. /// <param name="encoding">The encoding applied to the contents of the file.</param>
  1533. /// <returns>
  1534. /// A string array containing all lines of the file.
  1535. /// </returns>
  1536. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  1537. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1538. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1539. public string[] ReadAllLines(string path, Encoding encoding)
  1540. {
  1541. return ReadLines(path, encoding).ToArray();
  1542. }
  1543. /// <summary>
  1544. /// Opens a text file, reads all lines of the file with the UTF-8 encoding, and closes the file.
  1545. /// </summary>
  1546. /// <param name="path">The file to open for reading.</param>
  1547. /// <returns>
  1548. /// A string containing all lines of the file.
  1549. /// </returns>
  1550. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  1551. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1552. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1553. public string ReadAllText(string path)
  1554. {
  1555. return ReadAllText(path, Encoding.UTF8);
  1556. }
  1557. /// <summary>
  1558. /// Opens a file, reads all lines of the file with the specified encoding, and closes the file.
  1559. /// </summary>
  1560. /// <param name="path">The file to open for reading.</param>
  1561. /// <param name="encoding">The encoding applied to the contents of the file.</param>
  1562. /// <returns>
  1563. /// A string containing all lines of the file.
  1564. /// </returns>
  1565. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  1566. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1567. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1568. public string ReadAllText(string path, Encoding encoding)
  1569. {
  1570. using var sr = new StreamReader(OpenRead(path), encoding);
  1571. return sr.ReadToEnd();
  1572. }
  1573. /// <summary>
  1574. /// Reads the lines of a file with the UTF-8 encoding.
  1575. /// </summary>
  1576. /// <param name="path">The file to read.</param>
  1577. /// <returns>
  1578. /// The lines of the file.
  1579. /// </returns>
  1580. /// <remarks>
  1581. /// The lines are enumerated lazily. The opening of the file and any resulting exceptions occur
  1582. /// upon enumeration.
  1583. /// </remarks>
  1584. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>. Thrown eagerly.</exception>
  1585. /// <exception cref="SshConnectionException">Client is not connected upon enumeration.</exception>
  1586. /// <exception cref="ObjectDisposedException">The return value is enumerated after the client is disposed.</exception>
  1587. public IEnumerable<string> ReadLines(string path)
  1588. {
  1589. return ReadLines(path, Encoding.UTF8);
  1590. }
  1591. /// <summary>
  1592. /// Read the lines of a file that has a specified encoding.
  1593. /// </summary>
  1594. /// <param name="path">The file to read.</param>
  1595. /// <param name="encoding">The encoding that is applied to the contents of the file.</param>
  1596. /// <returns>
  1597. /// The lines of the file.
  1598. /// </returns>
  1599. /// <remarks>
  1600. /// The lines are enumerated lazily. The opening of the file and any resulting exceptions occur
  1601. /// upon enumeration.
  1602. /// </remarks>
  1603. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>. Thrown eagerly.</exception>
  1604. /// <exception cref="SshConnectionException">Client is not connected upon enumeration.</exception>
  1605. /// <exception cref="ObjectDisposedException">The return value is enumerated after the client is disposed.</exception>
  1606. public IEnumerable<string> ReadLines(string path, Encoding encoding)
  1607. {
  1608. // We allow this usage exception to throw eagerly...
  1609. ThrowHelper.ThrowIfNull(path);
  1610. // ... but other exceptions will throw lazily i.e. inside the state machine created
  1611. // by yield. We could choose to open the file eagerly as well in order to throw
  1612. // file-related exceptions eagerly (matching what File.ReadLines does), but this
  1613. // complicates double enumeration, and introduces the problem that File.ReadLines
  1614. // has whereby the file is not closed if the return value is not enumerated.
  1615. return Enumerate();
  1616. IEnumerable<string> Enumerate()
  1617. {
  1618. using var sr = new StreamReader(OpenRead(path), encoding);
  1619. string? line;
  1620. while ((line = sr.ReadLine()) != null)
  1621. {
  1622. yield return line;
  1623. }
  1624. }
  1625. }
  1626. /// <summary>
  1627. /// Sets the date and time the specified file was last accessed.
  1628. /// </summary>
  1629. /// <param name="path">The file for which to set the access date and time information.</param>
  1630. /// <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>
  1631. public void SetLastAccessTime(string path, DateTime lastAccessTime)
  1632. {
  1633. var attributes = GetAttributes(path);
  1634. attributes.LastAccessTime = lastAccessTime;
  1635. SetAttributes(path, attributes);
  1636. }
  1637. /// <summary>
  1638. /// Sets the date and time, in coordinated universal time (UTC), that the specified file was last accessed.
  1639. /// </summary>
  1640. /// <param name="path">The file for which to set the access date and time information.</param>
  1641. /// <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>
  1642. public void SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc)
  1643. {
  1644. var attributes = GetAttributes(path);
  1645. attributes.LastAccessTimeUtc = lastAccessTimeUtc;
  1646. SetAttributes(path, attributes);
  1647. }
  1648. /// <summary>
  1649. /// Sets the date and time that the specified file was last written to.
  1650. /// </summary>
  1651. /// <param name="path">The file for which to set the date and time information.</param>
  1652. /// <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>
  1653. public void SetLastWriteTime(string path, DateTime lastWriteTime)
  1654. {
  1655. var attributes = GetAttributes(path);
  1656. attributes.LastWriteTime = lastWriteTime;
  1657. SetAttributes(path, attributes);
  1658. }
  1659. /// <summary>
  1660. /// Sets the date and time, in coordinated universal time (UTC), that the specified file was last written to.
  1661. /// </summary>
  1662. /// <param name="path">The file for which to set the date and time information.</param>
  1663. /// <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>
  1664. public void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc)
  1665. {
  1666. var attributes = GetAttributes(path);
  1667. attributes.LastWriteTimeUtc = lastWriteTimeUtc;
  1668. SetAttributes(path, attributes);
  1669. }
  1670. /// <inheritdoc/>
  1671. public void WriteAllBytes(string path, byte[] bytes)
  1672. {
  1673. ThrowHelper.ThrowIfNull(bytes);
  1674. UploadFile(new MemoryStream(bytes), path);
  1675. }
  1676. /// <inheritdoc/>
  1677. public void WriteAllLines(string path, IEnumerable<string> contents)
  1678. {
  1679. WriteAllLines(path, contents, Utf8NoBOM);
  1680. }
  1681. /// <inheritdoc/>
  1682. public void WriteAllLines(string path, string[] contents)
  1683. {
  1684. WriteAllLines(path, contents, Utf8NoBOM);
  1685. }
  1686. /// <inheritdoc/>
  1687. public void WriteAllLines(string path, IEnumerable<string> contents, Encoding encoding)
  1688. {
  1689. using (var stream = CreateText(path, encoding))
  1690. {
  1691. foreach (var line in contents)
  1692. {
  1693. stream.WriteLine(line);
  1694. }
  1695. }
  1696. }
  1697. /// <inheritdoc/>
  1698. public void WriteAllLines(string path, string[] contents, Encoding encoding)
  1699. {
  1700. WriteAllLines(path, (IEnumerable<string>)contents, encoding);
  1701. }
  1702. /// <inheritdoc/>
  1703. public void WriteAllText(string path, string contents)
  1704. {
  1705. using (var stream = CreateText(path))
  1706. {
  1707. stream.Write(contents);
  1708. }
  1709. }
  1710. /// <inheritdoc/>
  1711. public void WriteAllText(string path, string contents, Encoding encoding)
  1712. {
  1713. using (var stream = CreateText(path, encoding))
  1714. {
  1715. stream.Write(contents);
  1716. }
  1717. }
  1718. /// <summary>
  1719. /// Gets the <see cref="SftpFileAttributes"/> of the file on the path.
  1720. /// </summary>
  1721. /// <param name="path">The path to the file.</param>
  1722. /// <returns>
  1723. /// The <see cref="SftpFileAttributes"/> of the file on the path.
  1724. /// </returns>
  1725. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  1726. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1727. /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>
  1728. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1729. public SftpFileAttributes GetAttributes(string path)
  1730. {
  1731. CheckDisposed();
  1732. if (_sftpSession is null)
  1733. {
  1734. throw new SshConnectionException("Client not connected.");
  1735. }
  1736. var fullPath = _sftpSession.GetCanonicalPath(path);
  1737. return _sftpSession.RequestLStat(fullPath);
  1738. }
  1739. /// <summary>
  1740. /// Gets the <see cref="SftpFileAttributes"/> of the file on the path.
  1741. /// </summary>
  1742. /// <param name="path">The path to the file.</param>
  1743. /// <param name="cancellationToken">The <see cref="CancellationToken"/> to observe.</param>
  1744. /// <returns>
  1745. /// A <see cref="Task{SftpFileAttributes}"/> that represents the attribute retrieval operation.
  1746. /// The task result contains the <see cref="SftpFileAttributes"/> of the file on the path.
  1747. /// </returns>
  1748. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  1749. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1750. /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>
  1751. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1752. public async Task<SftpFileAttributes> GetAttributesAsync(string path, CancellationToken cancellationToken)
  1753. {
  1754. CheckDisposed();
  1755. if (_sftpSession is null)
  1756. {
  1757. throw new SshConnectionException("Client not connected.");
  1758. }
  1759. var fullPath = await _sftpSession.GetCanonicalPathAsync(path, cancellationToken).ConfigureAwait(false);
  1760. return await _sftpSession.RequestLStatAsync(fullPath, cancellationToken).ConfigureAwait(false);
  1761. }
  1762. /// <summary>
  1763. /// Sets the specified <see cref="SftpFileAttributes"/> of the file on the specified path.
  1764. /// </summary>
  1765. /// <param name="path">The path to the file.</param>
  1766. /// <param name="fileAttributes">The desired <see cref="SftpFileAttributes"/>.</param>
  1767. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  1768. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  1769. /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
  1770. public void SetAttributes(string path, SftpFileAttributes fileAttributes)
  1771. {
  1772. CheckDisposed();
  1773. if (_sftpSession is null)
  1774. {
  1775. throw new SshConnectionException("Client not connected.");
  1776. }
  1777. var fullPath = _sftpSession.GetCanonicalPath(path);
  1778. _sftpSession.RequestSetStat(fullPath, fileAttributes);
  1779. }
  1780. #endregion // File Methods
  1781. #region SynchronizeDirectories
  1782. /// <summary>
  1783. /// Synchronizes the directories.
  1784. /// </summary>
  1785. /// <param name="sourcePath">The source path.</param>
  1786. /// <param name="destinationPath">The destination path.</param>
  1787. /// <param name="searchPattern">The search pattern.</param>
  1788. /// <returns>
  1789. /// A list of uploaded files.
  1790. /// </returns>
  1791. /// <exception cref="ArgumentNullException"><paramref name="sourcePath"/> is <see langword="null"/>.</exception>
  1792. /// <exception cref="ArgumentException"><paramref name="destinationPath"/> is <see langword="null"/> or contains only whitespace.</exception>
  1793. /// <exception cref="SftpPathNotFoundException"><paramref name="destinationPath"/> was not found on the remote host.</exception>
  1794. /// <exception cref="SshException">If a problem occurs while copying the file.</exception>
  1795. public IEnumerable<FileInfo> SynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern)
  1796. {
  1797. ThrowHelper.ThrowIfNull(sourcePath);
  1798. ThrowHelper.ThrowIfNullOrWhiteSpace(destinationPath);
  1799. return InternalSynchronizeDirectories(sourcePath, destinationPath, searchPattern, asyncResult: null);
  1800. }
  1801. /// <summary>
  1802. /// Begins the synchronize directories.
  1803. /// </summary>
  1804. /// <param name="sourcePath">The source path.</param>
  1805. /// <param name="destinationPath">The destination path.</param>
  1806. /// <param name="searchPattern">The search pattern.</param>
  1807. /// <param name="asyncCallback">The async callback.</param>
  1808. /// <param name="state">The state.</param>
  1809. /// <returns>
  1810. /// An <see cref="IAsyncResult" /> that represents the asynchronous directory synchronization.
  1811. /// </returns>
  1812. /// <exception cref="ArgumentNullException"><paramref name="sourcePath"/> or <paramref name="searchPattern"/> is <see langword="null"/>.</exception>
  1813. /// <exception cref="ArgumentException"><paramref name="destinationPath"/> is <see langword="null"/> or contains only whitespace.</exception>
  1814. /// <exception cref="SshException">If a problem occurs while copying the file.</exception>
  1815. public IAsyncResult BeginSynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern, AsyncCallback? asyncCallback, object? state)
  1816. {
  1817. ThrowHelper.ThrowIfNull(sourcePath);
  1818. ThrowHelper.ThrowIfNullOrWhiteSpace(destinationPath);
  1819. ThrowHelper.ThrowIfNull(searchPattern);
  1820. var asyncResult = new SftpSynchronizeDirectoriesAsyncResult(asyncCallback, state);
  1821. ThreadAbstraction.ExecuteThread(() =>
  1822. {
  1823. try
  1824. {
  1825. var result = InternalSynchronizeDirectories(sourcePath, destinationPath, searchPattern, asyncResult);
  1826. asyncResult.SetAsCompleted(result, completedSynchronously: false);
  1827. }
  1828. catch (Exception exp)
  1829. {
  1830. asyncResult.SetAsCompleted(exp, completedSynchronously: false);
  1831. }
  1832. });
  1833. return asyncResult;
  1834. }
  1835. /// <summary>
  1836. /// Ends the synchronize directories.
  1837. /// </summary>
  1838. /// <param name="asyncResult">The async result.</param>
  1839. /// <returns>
  1840. /// A list of uploaded files.
  1841. /// </returns>
  1842. /// <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>
  1843. /// <exception cref="SftpPathNotFoundException">The destination path was not found on the remote host.</exception>
  1844. public IEnumerable<FileInfo> EndSynchronizeDirectories(IAsyncResult asyncResult)
  1845. {
  1846. if (asyncResult is not SftpSynchronizeDirectoriesAsyncResult ar || ar.EndInvokeCalled)
  1847. {
  1848. 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.");
  1849. }
  1850. // Wait for operation to complete, then return result or throw exception
  1851. return ar.EndInvoke();
  1852. }
  1853. private List<FileInfo> InternalSynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern, SftpSynchronizeDirectoriesAsyncResult? asyncResult)
  1854. {
  1855. if (!Directory.Exists(sourcePath))
  1856. {
  1857. throw new FileNotFoundException(string.Format("Source directory not found: {0}", sourcePath));
  1858. }
  1859. var uploadedFiles = new List<FileInfo>();
  1860. var sourceDirectory = new DirectoryInfo(sourcePath);
  1861. using (var sourceFiles = sourceDirectory.EnumerateFiles(searchPattern).GetEnumerator())
  1862. {
  1863. if (!sourceFiles.MoveNext())
  1864. {
  1865. return uploadedFiles;
  1866. }
  1867. #region Existing Files at The Destination
  1868. var destFiles = InternalListDirectory(destinationPath, asyncResult: null, listCallback: null);
  1869. var destDict = new Dictionary<string, ISftpFile>();
  1870. foreach (var destFile in destFiles)
  1871. {
  1872. if (destFile.IsDirectory)
  1873. {
  1874. continue;
  1875. }
  1876. destDict.Add(destFile.Name, destFile);
  1877. }
  1878. #endregion
  1879. #region Upload the difference
  1880. const Flags uploadFlag = Flags.Write | Flags.Truncate | Flags.CreateNewOrOpen;
  1881. do
  1882. {
  1883. var localFile = sourceFiles.Current;
  1884. if (localFile is null)
  1885. {
  1886. continue;
  1887. }
  1888. var isDifferent = true;
  1889. if (destDict.TryGetValue(localFile.Name, out var remoteFile))
  1890. {
  1891. // File exists at the destination, use filesize to detect if there's a difference
  1892. isDifferent = localFile.Length != remoteFile.Length;
  1893. }
  1894. if (isDifferent)
  1895. {
  1896. var remoteFileName = string.Format(CultureInfo.InvariantCulture, @"{0}/{1}", destinationPath, localFile.Name);
  1897. try
  1898. {
  1899. using (var file = File.OpenRead(localFile.FullName))
  1900. {
  1901. InternalUploadFile(
  1902. file,
  1903. remoteFileName,
  1904. uploadFlag,
  1905. asyncResult: null,
  1906. uploadCallback: null,
  1907. isAsync: false,
  1908. CancellationToken.None).GetAwaiter().GetResult();
  1909. }
  1910. uploadedFiles.Add(localFile);
  1911. asyncResult?.Update(uploadedFiles.Count);
  1912. }
  1913. catch (Exception ex)
  1914. {
  1915. throw new SshException($"Failed to upload {localFile.FullName} to {remoteFileName}", ex);
  1916. }
  1917. }
  1918. }
  1919. while (sourceFiles.MoveNext());
  1920. }
  1921. #endregion
  1922. return uploadedFiles;
  1923. }
  1924. #endregion
  1925. /// <summary>
  1926. /// Internals the list directory.
  1927. /// </summary>
  1928. /// <param name="path">The path.</param>
  1929. /// <param name="asyncResult">An <see cref="IAsyncResult"/> that references the asynchronous request.</param>
  1930. /// <param name="listCallback">The list callback.</param>
  1931. /// <returns>
  1932. /// A list of files in the specified directory.
  1933. /// </returns>
  1934. /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null"/>.</exception>
  1935. /// <exception cref="SshConnectionException">Client not connected.</exception>
  1936. private List<ISftpFile> InternalListDirectory(string path, SftpListDirectoryAsyncResult? asyncResult, Action<int>? listCallback)
  1937. {
  1938. ThrowHelper.ThrowIfNull(path);
  1939. if (_sftpSession is null)
  1940. {
  1941. throw new SshConnectionException("Client not connected.");
  1942. }
  1943. var fullPath = _sftpSession.GetCanonicalPath(path);
  1944. var handle = _sftpSession.RequestOpenDir(fullPath);
  1945. var basePath = fullPath;
  1946. #if NET
  1947. if (!basePath.EndsWith('/'))
  1948. #else
  1949. if (!basePath.EndsWith("/", StringComparison.Ordinal))
  1950. #endif
  1951. {
  1952. basePath = string.Format("{0}/", fullPath);
  1953. }
  1954. var result = new List<ISftpFile>();
  1955. var files = _sftpSession.RequestReadDir(handle);
  1956. while (files is not null)
  1957. {
  1958. foreach (var f in files)
  1959. {
  1960. result.Add(new SftpFile(_sftpSession,
  1961. string.Format(CultureInfo.InvariantCulture, "{0}{1}", basePath, f.Key),
  1962. f.Value));
  1963. }
  1964. asyncResult?.Update(result.Count);
  1965. // Call callback to report number of files read
  1966. if (listCallback is not null)
  1967. {
  1968. // Execute callback on different thread
  1969. ThreadAbstraction.ExecuteThread(() => listCallback(result.Count));
  1970. }
  1971. files = _sftpSession.RequestReadDir(handle);
  1972. }
  1973. _sftpSession.RequestClose(handle);
  1974. return result;
  1975. }
  1976. /// <summary>
  1977. /// Internals the download file.
  1978. /// </summary>
  1979. /// <param name="path">The path.</param>
  1980. /// <param name="output">The output.</param>
  1981. /// <param name="asyncResult">An <see cref="IAsyncResult"/> that references the asynchronous request.</param>
  1982. /// <param name="downloadCallback">The download callback.</param>
  1983. /// <exception cref="ArgumentNullException"><paramref name="output" /> is <see langword="null"/>.</exception>
  1984. /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains whitespace.</exception>
  1985. /// <exception cref="SshConnectionException">Client not connected.</exception>
  1986. private void InternalDownloadFile(string path, Stream output, SftpDownloadAsyncResult? asyncResult, Action<ulong>? downloadCallback)
  1987. {
  1988. ThrowHelper.ThrowIfNull(output);
  1989. ThrowHelper.ThrowIfNullOrWhiteSpace(path);
  1990. if (_sftpSession is null)
  1991. {
  1992. throw new SshConnectionException("Client not connected.");
  1993. }
  1994. var fullPath = _sftpSession.GetCanonicalPath(path);
  1995. using (var fileReader = ServiceFactory.CreateSftpFileReader(fullPath, _sftpSession, _bufferSize))
  1996. {
  1997. var totalBytesRead = 0UL;
  1998. while (true)
  1999. {
  2000. // Cancel download
  2001. if (asyncResult is not null && asyncResult.IsDownloadCanceled)
  2002. {
  2003. break;
  2004. }
  2005. var data = fileReader.Read();
  2006. if (data.Length == 0)
  2007. {
  2008. break;
  2009. }
  2010. output.Write(data, 0, data.Length);
  2011. totalBytesRead += (ulong)data.Length;
  2012. asyncResult?.Update(totalBytesRead);
  2013. if (downloadCallback is not null)
  2014. {
  2015. // Copy offset to ensure it's not modified between now and execution of callback
  2016. var downloadOffset = totalBytesRead;
  2017. // Execute callback on different thread
  2018. ThreadAbstraction.ExecuteThread(() => { downloadCallback(downloadOffset); });
  2019. }
  2020. }
  2021. }
  2022. }
  2023. private async Task InternalDownloadFileAsync(string path, Stream output, CancellationToken cancellationToken)
  2024. {
  2025. ThrowHelper.ThrowIfNull(output);
  2026. ThrowHelper.ThrowIfNullOrWhiteSpace(path);
  2027. if (_sftpSession is null)
  2028. {
  2029. throw new SshConnectionException("Client not connected.");
  2030. }
  2031. cancellationToken.ThrowIfCancellationRequested();
  2032. var fullPath = await _sftpSession.GetCanonicalPathAsync(path, cancellationToken).ConfigureAwait(false);
  2033. var openStreamTask = SftpFileStream.OpenAsync(_sftpSession, fullPath, FileMode.Open, FileAccess.Read, (int)_bufferSize, cancellationToken);
  2034. using (var input = await openStreamTask.ConfigureAwait(false))
  2035. {
  2036. await input.CopyToAsync(output, 81920, cancellationToken).ConfigureAwait(false);
  2037. }
  2038. }
  2039. #pragma warning disable S6966 // Awaitable method should be used
  2040. private async Task InternalUploadFile(
  2041. Stream input,
  2042. string path,
  2043. Flags flags,
  2044. SftpUploadAsyncResult? asyncResult,
  2045. Action<ulong>? uploadCallback,
  2046. bool isAsync,
  2047. CancellationToken cancellationToken)
  2048. {
  2049. Debug.Assert(isAsync || cancellationToken == default);
  2050. if (_sftpSession is null)
  2051. {
  2052. throw new SshConnectionException("Client not connected.");
  2053. }
  2054. string fullPath;
  2055. byte[] handle;
  2056. if (isAsync)
  2057. {
  2058. fullPath = await _sftpSession.GetCanonicalPathAsync(path, cancellationToken).ConfigureAwait(false);
  2059. handle = await _sftpSession.RequestOpenAsync(fullPath, flags, cancellationToken).ConfigureAwait(false);
  2060. }
  2061. else
  2062. {
  2063. fullPath = _sftpSession.GetCanonicalPath(path);
  2064. handle = _sftpSession.RequestOpen(fullPath, flags);
  2065. }
  2066. ulong offset = 0;
  2067. // create buffer of optimal length
  2068. var buffer = new byte[_sftpSession.CalculateOptimalWriteLength(_bufferSize, handle)];
  2069. var expectedResponses = 0;
  2070. // We will send out all the write requests without waiting for each response.
  2071. // Afterwards, we may wait on this handle until all responses are received
  2072. // or an error has occurred.
  2073. using var mres = new ManualResetEventSlim(initialState: false);
  2074. ExceptionDispatchInfo? exception = null;
  2075. while (true)
  2076. {
  2077. var bytesRead = isAsync
  2078. #if NET
  2079. ? await input.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)
  2080. #else
  2081. ? await input.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)
  2082. #endif
  2083. : input.Read(buffer, 0, buffer.Length);
  2084. if (bytesRead == 0)
  2085. {
  2086. break;
  2087. }
  2088. if (asyncResult is not null && asyncResult.IsUploadCanceled)
  2089. {
  2090. break;
  2091. }
  2092. exception?.Throw();
  2093. var writtenBytes = offset + (ulong)bytesRead;
  2094. _ = Interlocked.Increment(ref expectedResponses);
  2095. mres.Reset();
  2096. _sftpSession.RequestWrite(handle, offset, buffer, offset: 0, bytesRead, wait: null, s =>
  2097. {
  2098. var setHandle = false;
  2099. try
  2100. {
  2101. if (Sftp.SftpSession.GetSftpException(s) is Exception ex)
  2102. {
  2103. exception = ExceptionDispatchInfo.Capture(ex);
  2104. }
  2105. if (exception is not null)
  2106. {
  2107. setHandle = true;
  2108. return;
  2109. }
  2110. Debug.Assert(s.StatusCode == StatusCodes.Ok);
  2111. asyncResult?.Update(writtenBytes);
  2112. // Call callback to report number of bytes written
  2113. if (uploadCallback is not null)
  2114. {
  2115. // Execute callback on different thread
  2116. ThreadAbstraction.ExecuteThread(() => uploadCallback(writtenBytes));
  2117. }
  2118. }
  2119. finally
  2120. {
  2121. if (Interlocked.Decrement(ref expectedResponses) == 0 || setHandle)
  2122. {
  2123. mres.Set();
  2124. }
  2125. }
  2126. });
  2127. offset += (ulong)bytesRead;
  2128. }
  2129. // Make sure the read of exception cannot be executed ahead of
  2130. // the read of expectedResponses so that we do not miss an
  2131. // exception.
  2132. if (Volatile.Read(ref expectedResponses) != 0)
  2133. {
  2134. if (isAsync)
  2135. {
  2136. await _sftpSession.WaitOnHandleAsync(mres.WaitHandle, _operationTimeout, cancellationToken).ConfigureAwait(false);
  2137. }
  2138. else
  2139. {
  2140. _sftpSession.WaitOnHandle(mres.WaitHandle, _operationTimeout);
  2141. }
  2142. }
  2143. exception?.Throw();
  2144. if (isAsync)
  2145. {
  2146. await _sftpSession.RequestCloseAsync(handle, cancellationToken).ConfigureAwait(false);
  2147. }
  2148. else
  2149. {
  2150. _sftpSession.RequestClose(handle);
  2151. }
  2152. }
  2153. #pragma warning restore S6966 // Awaitable method should be used
  2154. /// <summary>
  2155. /// Called when client is connected to the server.
  2156. /// </summary>
  2157. protected override void OnConnected()
  2158. {
  2159. base.OnConnected();
  2160. _sftpSession?.Dispose();
  2161. _sftpSession = CreateAndConnectToSftpSession();
  2162. }
  2163. /// <summary>
  2164. /// Called when client is disconnecting from the server.
  2165. /// </summary>
  2166. protected override void OnDisconnecting()
  2167. {
  2168. base.OnDisconnecting();
  2169. // disconnect, dispose and dereference the SFTP session since we create a new SFTP session
  2170. // on each connect
  2171. var sftpSession = _sftpSession;
  2172. if (sftpSession is not null)
  2173. {
  2174. _sftpSession = null;
  2175. sftpSession.Dispose();
  2176. }
  2177. }
  2178. /// <summary>
  2179. /// Releases unmanaged and - optionally - managed resources.
  2180. /// </summary>
  2181. /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
  2182. protected override void Dispose(bool disposing)
  2183. {
  2184. base.Dispose(disposing);
  2185. if (disposing)
  2186. {
  2187. var sftpSession = _sftpSession;
  2188. if (sftpSession is not null)
  2189. {
  2190. _sftpSession = null;
  2191. sftpSession.Dispose();
  2192. }
  2193. }
  2194. }
  2195. private ISftpSession CreateAndConnectToSftpSession()
  2196. {
  2197. var sftpSession = ServiceFactory.CreateSftpSession(Session,
  2198. _operationTimeout,
  2199. ConnectionInfo.Encoding,
  2200. ServiceFactory.CreateSftpResponseFactory());
  2201. try
  2202. {
  2203. sftpSession.Connect();
  2204. return sftpSession;
  2205. }
  2206. catch
  2207. {
  2208. sftpSession.Dispose();
  2209. throw;
  2210. }
  2211. }
  2212. }
  2213. }