SftpClient.cs 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using Renci.SshNet.Sftp;
  6. using System.Text;
  7. using Renci.SshNet.Common;
  8. using System.Globalization;
  9. namespace Renci.SshNet
  10. {
  11. /// <summary>
  12. ///
  13. /// </summary>
  14. public partial class SftpClient : BaseClient
  15. {
  16. /// <summary>
  17. /// Holds SftpSession instance that used to communicate to the SFTP server
  18. /// </summary>
  19. private SftpSession _sftpSession;
  20. /// <summary>
  21. /// Gets or sets the operation timeout.
  22. /// </summary>
  23. /// <value>The operation timeout.</value>
  24. public TimeSpan OperationTimeout { get; set; }
  25. /// <summary>
  26. /// Gets or sets the size of the buffer.
  27. /// </summary>
  28. /// <value>The size of the buffer.</value>
  29. public uint BufferSize { get; set; }
  30. /// <summary>
  31. /// Gets remote working directory.
  32. /// </summary>
  33. public string WorkingDirectory
  34. {
  35. get
  36. {
  37. if (this._sftpSession == null)
  38. return null;
  39. return this._sftpSession.WorkingDirectory;
  40. }
  41. }
  42. /// <summary>
  43. /// Gets sftp protocol version.
  44. /// </summary>
  45. public int ProtocolVersion { get; private set; }
  46. #region Constructors
  47. /// <summary>
  48. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  49. /// </summary>
  50. /// <param name="connectionInfo">The connection info.</param>
  51. /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is null.</exception>
  52. public SftpClient(ConnectionInfo connectionInfo)
  53. : base(connectionInfo)
  54. {
  55. this.OperationTimeout = new TimeSpan(0, 0, 0, 0, -1);
  56. this.BufferSize = 1024 * 32 - 38;
  57. }
  58. /// <summary>
  59. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  60. /// </summary>
  61. /// <param name="host">Connection host.</param>
  62. /// <param name="port">Connection port.</param>
  63. /// <param name="username">Authentication username.</param>
  64. /// <param name="password">Authentication password.</param>
  65. /// <exception cref="ArgumentNullException"><paramref name="password"/> is null.</exception>
  66. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, or <paramref name="username"/> is null or contains whitespace characters.</exception>
  67. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="System.Net.IPEndPoint.MinPort"/> and <see cref="System.Net.IPEndPoint.MaxPort"/>.</exception>
  68. public SftpClient(string host, int port, string username, string password)
  69. : this(new PasswordConnectionInfo(host, port, username, password))
  70. {
  71. }
  72. /// <summary>
  73. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  74. /// </summary>
  75. /// <param name="host">Connection host.</param>
  76. /// <param name="username">Authentication username.</param>
  77. /// <param name="password">Authentication password.</param>
  78. /// <exception cref="ArgumentNullException"><paramref name="password"/> is null.</exception>
  79. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, or <paramref name="username"/> is null or contains whitespace characters.</exception>
  80. public SftpClient(string host, string username, string password)
  81. : this(host, 22, username, password)
  82. {
  83. }
  84. /// <summary>
  85. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  86. /// </summary>
  87. /// <param name="host">Connection host.</param>
  88. /// <param name="port">Connection port.</param>
  89. /// <param name="username">Authentication username.</param>
  90. /// <param name="keyFiles">Authentication private key file(s) .</param>
  91. /// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is null.</exception>
  92. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, -or- <paramref name="username"/> is null or contains whitespace characters.</exception>
  93. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="System.Net.IPEndPoint.MinPort"/> and <see cref="System.Net.IPEndPoint.MaxPort"/>.</exception>
  94. public SftpClient(string host, int port, string username, params PrivateKeyFile[] keyFiles)
  95. : this(new PrivateKeyConnectionInfo(host, port, username, keyFiles))
  96. {
  97. }
  98. /// <summary>
  99. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  100. /// </summary>
  101. /// <param name="host">Connection host.</param>
  102. /// <param name="username">Authentication username.</param>
  103. /// <param name="keyFiles">Authentication private key file(s) .</param>
  104. /// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is null.</exception>
  105. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, -or- <paramref name="username"/> is null or contains whitespace characters.</exception>
  106. public SftpClient(string host, string username, params PrivateKeyFile[] keyFiles)
  107. : this(host, 22, username, keyFiles)
  108. {
  109. }
  110. #endregion
  111. /// <summary>
  112. /// Changes remote directory to path.
  113. /// </summary>
  114. /// <param name="path">New directory path.</param>
  115. /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
  116. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  117. /// <exception cref="SftpPermissionDeniedException">Permission to change directory denied by remote host -or- a SSH command was denied by the server.</exception>
  118. /// <exception cref="SftpPathNotFoundException">The path in <paramref name="path"/> was not found on the remote host.</exception>
  119. /// <exception cref="SshException">A SSH error where <see cref="P:SshException.Message"/> is the message from the remote host.</exception>
  120. public void ChangeDirectory(string path)
  121. {
  122. if (path == null)
  123. throw new ArgumentNullException("path");
  124. // Ensure that connection is established.
  125. this.EnsureConnection();
  126. this._sftpSession.ChangeDirectory(path);
  127. }
  128. /// <summary>
  129. /// Changes permissions of file(s) to specified mode.
  130. /// </summary>
  131. /// <param name="path">File(s) path, may match multiple files.</param>
  132. /// <param name="mode">The mode.</param>
  133. /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
  134. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  135. /// <exception cref="SftpPermissionDeniedException">Permission to change permission on the path(s) was denied by the remote host -or- a SSH command was denied by the server.</exception>
  136. /// <exception cref="SftpPathNotFoundException">The path in <paramref name="path"/> was not found on the remote host.</exception>
  137. /// <exception cref="SshException">A SSH error where <see cref="P:SshException.Message"/> is the message from the remote host.</exception>
  138. public void ChangePermissions(string path, short mode)
  139. {
  140. var file = this.Get(path);
  141. file.SetPermissions(mode);
  142. }
  143. /// <summary>
  144. /// Creates remote directory specified by path.
  145. /// </summary>
  146. /// <param name="path">Directory path to create.</param>
  147. /// <exception cref="ArgumentException"><paramref name="path"/> is null or contains whitespace characters.</exception>
  148. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  149. /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to create the directory was denied by the remote host -or- a SSH command was denied by the server.</exception>
  150. /// <exception cref="Renci.SshNet.Common.SshException">A SSH error where <see cref="P:SshException.Message"/> is the message from the remote host.</exception>
  151. public void CreateDirectory(string path)
  152. {
  153. if (string.IsNullOrWhiteSpace(path))
  154. throw new ArgumentException(path);
  155. // Ensure that connection is established.
  156. this.EnsureConnection();
  157. var fullPath = this._sftpSession.GetCanonicalPath(path);
  158. this._sftpSession.RequestMkDir(fullPath);
  159. }
  160. /// <summary>
  161. /// Deletes remote directory specified by path.
  162. /// </summary>
  163. /// <param name="path">Directory to be deleted path.</param>
  164. /// <exception cref="ArgumentException"><paramref name="path"/> is null or contains whitespace characters.</exception>
  165. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  166. /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to delete the directory was denied by the remote host -or- a SSH command was denied by the server.</exception>
  167. /// <exception cref="Renci.SshNet.Common.SshException">A SSH error where <see cref="P:SshException.Message"/> is the message from the remote host.</exception>
  168. public void DeleteDirectory(string path)
  169. {
  170. if (string.IsNullOrWhiteSpace(path))
  171. throw new ArgumentException("path");
  172. // Ensure that connection is established.
  173. this.EnsureConnection();
  174. var fullPath = this._sftpSession.GetCanonicalPath(path);
  175. this._sftpSession.RequestRmDir(fullPath);
  176. }
  177. /// <summary>
  178. /// Deletes remote file specified by path.
  179. /// </summary>
  180. /// <param name="path">File to be deleted path.</param>
  181. /// <exception cref="ArgumentException"><paramref name="path"/> is null or contains whitespace characters.</exception>
  182. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  183. /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to delete the file was denied by the remote host -or- a SSH command was denied by the server.</exception>
  184. /// <exception cref="Renci.SshNet.Common.SshException">A SSH error where <see cref="P:SshException.Message"/> is the message from the remote host.</exception>
  185. public void DeleteFile(string path)
  186. {
  187. if (string.IsNullOrWhiteSpace(path))
  188. throw new ArgumentException("path");
  189. // Ensure that connection is established.
  190. this.EnsureConnection();
  191. var fullPath = this._sftpSession.GetCanonicalPath(path);
  192. this._sftpSession.RequestRemove(fullPath);
  193. }
  194. /// <summary>
  195. /// Renames remote file from old path to new path.
  196. /// </summary>
  197. /// <param name="oldPath">Path to the old file location.</param>
  198. /// <param name="newPath">Path to the new file location.</param>
  199. /// <exception cref="ArgumentNullException"><paramref name="oldPath"/> or <paramref name="newPath"/> is null.</exception>
  200. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  201. /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to rename the file was denied by the remote host -or- a SSH command was denied by the server.</exception>
  202. /// <exception cref="Renci.SshNet.Common.SshException">A SSH error where <see cref="P:SshException.Message"/> is the message from the remote host.</exception>
  203. public void RenameFile(string oldPath, string newPath)
  204. {
  205. if (oldPath == null)
  206. throw new ArgumentNullException("oldPath");
  207. if (newPath == null)
  208. throw new ArgumentNullException("newPath");
  209. // Ensure that connection is established.
  210. this.EnsureConnection();
  211. var oldFullPath = this._sftpSession.GetCanonicalPath(oldPath);
  212. var newFullPath = this._sftpSession.GetCanonicalPath(newPath);
  213. this._sftpSession.RequestRename(oldFullPath, newFullPath);
  214. }
  215. /// <summary>
  216. /// Creates a symbolic link from old path to new path.
  217. /// </summary>
  218. /// <param name="path">The old path.</param>
  219. /// <param name="linkPath">The new path.</param>
  220. /// <exception cref="ArgumentException"><paramref name="path"/> or <paramref name="linkPath"/> is null or contains whitespace characters.</exception>
  221. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  222. /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to create the symbolic link was denied by the remote host -or- a SSH command was denied by the server.</exception>
  223. /// <exception cref="Renci.SshNet.Common.SshException">A SSH error where <see cref="P:SshException.Message"/> is the message from the remote host.</exception>
  224. public void SymbolicLink(string path, string linkPath)
  225. {
  226. if (string.IsNullOrWhiteSpace(path))
  227. throw new ArgumentException("path");
  228. if (string.IsNullOrWhiteSpace(linkPath))
  229. throw new ArgumentException("linkPath");
  230. // Ensure that connection is established.
  231. this.EnsureConnection();
  232. var fullPath = this._sftpSession.GetCanonicalPath(path);
  233. var linkFullPath = this._sftpSession.GetCanonicalPath(linkPath);
  234. this._sftpSession.RequestSymLink(fullPath, linkFullPath);
  235. }
  236. /// <summary>
  237. /// Retrieves list of files in remote directory.
  238. /// </summary>
  239. /// <param name="path">The path.</param>
  240. /// <returns>List of directory entries</returns>
  241. /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
  242. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  243. /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to list the contents of the directory was denied by the remote host -or- a SSH command was denied by the server.</exception>
  244. /// <exception cref="Renci.SshNet.Common.SshException">A SSH error where <see cref="P:SshException.Message"/> is the message from the remote host.</exception>
  245. public IEnumerable<SftpFile> ListDirectory(string path)
  246. {
  247. return InternalListDirectory(path, null);
  248. }
  249. /// <summary>
  250. /// Begins an asynchronous operation of retrieving list of files in remote directory.
  251. /// </summary>
  252. /// <param name="path">The path.</param>
  253. /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>
  254. /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests.</param>
  255. /// <returns>
  256. /// An <see cref="IAsyncResult"/> that references the asynchronous operation.
  257. /// </returns>
  258. public IAsyncResult BeginListDirectory(string path, AsyncCallback asyncCallback, object state)
  259. {
  260. var asyncResult = new SftpListDirectoryAsyncResult(asyncCallback, state);
  261. this.ExecuteThread(() =>
  262. {
  263. try
  264. {
  265. var result = this.InternalListDirectory(path, asyncResult);
  266. asyncResult.SetAsCompleted(result, false);
  267. }
  268. catch (Exception exp)
  269. {
  270. asyncResult.SetAsCompleted(exp, false);
  271. }
  272. });
  273. return asyncResult;
  274. }
  275. /// <summary>
  276. /// Ends an asynchronous operation of retrieving list of files in remote directory.
  277. /// </summary>
  278. /// <param name="asyncResult">The pending asynchronous SFTP request.</param>
  279. /// <returns>
  280. /// List of files
  281. /// </returns>
  282. /// <exception cref="ArgumentException">Either the IAsyncResult object (<paramref name="asyncResult"/>) did not come from the corresponding async method on this type, or EndExecute was called multiple times with the same IAsyncResult.</exception>
  283. public IEnumerable<SftpFile> EndListDirectory(IAsyncResult asyncResult)
  284. {
  285. var ar = asyncResult as SftpListDirectoryAsyncResult;
  286. if (ar == null || ar.EndInvokeCalled)
  287. 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.");
  288. // Wait for operation to complete, then return result or throw exception
  289. return ar.EndInvoke();
  290. }
  291. /// <summary>
  292. /// Gets reference to remote file or directory.
  293. /// </summary>
  294. /// <param name="path">The path.</param>
  295. /// <returns></returns>
  296. /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
  297. public SftpFile Get(string path)
  298. {
  299. if (path == null)
  300. throw new ArgumentNullException("path");
  301. // Ensure that connection is established.
  302. this.EnsureConnection();
  303. var fullPath = this._sftpSession.GetCanonicalPath(path);
  304. var attributes = this._sftpSession.RequestLStat(fullPath);
  305. return new SftpFile(this._sftpSession, fullPath, attributes);
  306. }
  307. /// <summary>
  308. /// Checks whether file pr directory exists;
  309. /// </summary>
  310. /// <param name="path">The path.</param>
  311. /// <returns><c>true</c> if directory or file exists; otherwise <c>false</c>.</returns>
  312. /// <exception cref="ArgumentException"><paramref name="path"/> is null or contains whitespace characters.</exception>
  313. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  314. /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to perform the operation was denied by the remote host -or- a SSH command was denied by the server.</exception>
  315. /// <exception cref="Renci.SshNet.Common.SshException">A SSH error where <see cref="P:SshException.Message"/> is the message from the remote host.</exception>
  316. public bool Exists(string path)
  317. {
  318. if (string.IsNullOrWhiteSpace(path))
  319. throw new ArgumentException("path");
  320. // Ensure that connection is established.
  321. this.EnsureConnection();
  322. var fullPath = this._sftpSession.GetCanonicalPath(path);
  323. // Try to open as a file
  324. var handle = this._sftpSession.RequestOpen(fullPath, Flags.Read, true);
  325. if (handle == null)
  326. {
  327. handle = this._sftpSession.RequestOpenDir(fullPath, true);
  328. }
  329. if (handle == null)
  330. {
  331. return false;
  332. }
  333. else
  334. {
  335. this._sftpSession.RequestClose(handle);
  336. return true;
  337. }
  338. }
  339. /// <summary>
  340. /// Downloads remote file specified by the path into the stream.
  341. /// </summary>
  342. /// <param name="path">File to download.</param>
  343. /// <param name="output">Stream to write the file into.</param>
  344. /// <exception cref="ArgumentNullException"><paramref name="output"/> is null.</exception>
  345. /// <exception cref="ArgumentException"><paramref name="path"/> is null or contains whitespace characters.</exception>
  346. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  347. /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to perform the operation was denied by the remote host -or- a SSH command was denied by the server.</exception>
  348. /// <exception cref="Renci.SshNet.Common.SshException">A SSH error where <see cref="P:SshException.Message"/> is the message from the remote host.</exception>
  349. /// <remarks>Method calls made by this method to <paramref name="output"/>, may under certain conditions result in exceptions thrown by the stream.</remarks>
  350. public void DownloadFile(string path, Stream output)
  351. {
  352. this.InternalDownloadFile(path, output, null);
  353. }
  354. /// <summary>
  355. /// Begins an asynchronous file downloading into the stream.
  356. /// </summary>
  357. /// <param name="path">The path.</param>
  358. /// <param name="output">The output.</param>
  359. /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>
  360. /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests.</param>
  361. /// <returns>An <see cref="IAsyncResult"/> that references the asynchronous operation.</returns>
  362. /// <exception cref="ArgumentNullException"><paramref name="output"/> is null.</exception>
  363. /// <exception cref="ArgumentException"><paramref name="path"/> is null or contains whitespace characters.</exception>
  364. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  365. /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to perform the operation was denied by the remote host -or- a SSH command was denied by the server.</exception>
  366. /// <exception cref="Renci.SshNet.Common.SshException">A SSH error where <see cref="P:SshException.Message"/> is the message from the remote host.</exception>
  367. /// <remarks>Method calls made by this method to <paramref name="output"/>, may under certain conditions result in exceptions thrown by the stream.</remarks>
  368. public IAsyncResult BeginDownloadFile(string path, Stream output, AsyncCallback asyncCallback, object state)
  369. {
  370. if (string.IsNullOrWhiteSpace(path))
  371. throw new ArgumentException("path");
  372. if (output == null)
  373. throw new ArgumentNullException("output");
  374. // Ensure that connection is established.
  375. this.EnsureConnection();
  376. var asyncResult = new SftpDownloadAsyncResult(asyncCallback, state);
  377. this.ExecuteThread(() =>
  378. {
  379. try
  380. {
  381. this.InternalDownloadFile(path, output, asyncResult);
  382. asyncResult.SetAsCompleted(null, false);
  383. }
  384. catch (Exception exp)
  385. {
  386. asyncResult.SetAsCompleted(exp, false);
  387. }
  388. });
  389. return asyncResult;
  390. }
  391. /// <summary>
  392. /// Ends an asynchronous file downloading into the stream.
  393. /// </summary>
  394. /// <param name="asyncResult">The pending asynchronous SFTP request.</param>
  395. /// <exception cref="ArgumentException">Either the IAsyncResult object (<paramref name="asyncResult"/>) did not come from the corresponding async method on this type, or EndExecute was called multiple times with the same IAsyncResult.</exception>
  396. public void EndDownloadFile(IAsyncResult asyncResult)
  397. {
  398. var ar = asyncResult as SftpDownloadAsyncResult;
  399. if (ar == null || ar.EndInvokeCalled)
  400. 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.");
  401. // Wait for operation to complete, then return result or throw exception
  402. ar.EndInvoke();
  403. }
  404. /// <summary>
  405. /// Uploads stream into remote file..
  406. /// </summary>
  407. /// <param name="input">Data input stream.</param>
  408. /// <param name="path">Remote file path.</param>
  409. /// <exception cref="ArgumentNullException"><paramref name="input"/> is null.</exception>
  410. /// <exception cref="ArgumentException"><paramref name="path"/> is null or contains whitespace characters.</exception>
  411. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  412. /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to upload the file was denied by the remote host -or- a SSH command was denied by the server.</exception>
  413. /// <exception cref="Renci.SshNet.Common.SshException">A SSH error where <see cref="P:SshException.Message"/> is the message from the remote host.</exception>
  414. /// <remarks>Method calls made by this method to <paramref name="input"/>, may under certain conditions result in exceptions thrown by the stream.</remarks>
  415. public void UploadFile(Stream input, string path)
  416. {
  417. this.InternalUploadFile(input, path, null);
  418. }
  419. /// <summary>
  420. /// Begins an asynchronous uploading the steam into remote file.
  421. /// </summary>
  422. /// <param name="input">Data input stream.</param>
  423. /// <param name="path">Remote file path.</param>
  424. /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>
  425. /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests.</param>
  426. /// <returns>An <see cref="IAsyncResult"/> that references the asynchronous operation.</returns>
  427. /// <exception cref="ArgumentNullException"><paramref name="input"/> is null.</exception>
  428. /// <exception cref="ArgumentException"><paramref name="path"/> is null or contains whitespace characters.</exception>
  429. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  430. /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to list the contents of the directory was denied by the remote host -or- a SSH command was denied by the server.</exception>
  431. /// <exception cref="Renci.SshNet.Common.SshException">A SSH error where <see cref="P:SshException.Message"/> is the message from the remote host.</exception>
  432. /// <remarks>Method calls made by this method to <paramref name="input"/>, may under certain conditions result in exceptions thrown by the stream.</remarks>
  433. public IAsyncResult BeginUploadFile(Stream input, string path, AsyncCallback asyncCallback, object state)
  434. {
  435. if (input == null)
  436. throw new ArgumentNullException("input");
  437. if (string.IsNullOrWhiteSpace(path))
  438. throw new ArgumentException("path");
  439. // Ensure that connection is established.
  440. this.EnsureConnection();
  441. var asyncResult = new SftpUploadAsyncResult(asyncCallback, state);
  442. this.ExecuteThread(() =>
  443. {
  444. try
  445. {
  446. this.InternalUploadFile(input, path, asyncResult);
  447. asyncResult.SetAsCompleted(null, false);
  448. }
  449. catch (Exception exp)
  450. {
  451. asyncResult.SetAsCompleted(exp, false);
  452. }
  453. });
  454. return asyncResult;
  455. }
  456. /// <summary>
  457. /// Ends an asynchronous uploading the steam into remote file.
  458. /// </summary>
  459. /// <param name="asyncResult">The pending asynchronous SFTP request.</param>
  460. /// <exception cref="ArgumentException">Either the IAsyncResult object (<paramref name="asyncResult"/>) did not come from the corresponding async method on this type, or EndExecute was called multiple times with the same IAsyncResult.</exception>
  461. public void EndUploadFile(IAsyncResult asyncResult)
  462. {
  463. var ar = asyncResult as SftpUploadAsyncResult;
  464. if (ar == null || ar.EndInvokeCalled)
  465. 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.");
  466. // Wait for operation to complete, then return result or throw exception
  467. ar.EndInvoke();
  468. }
  469. #region File Methods
  470. /// <summary>
  471. /// Appends lines to a file, and then closes the file.
  472. /// </summary>
  473. /// <param name="path">The file to append the lines to. The file is created if it does not already exist.</param>
  474. /// <param name="contents">The lines to append to the file.</param>
  475. /// <exception cref="ArgumentNullException"><paramref name="path"/> or <paramref name="contents"/> is null.</exception>
  476. public void AppendAllLines(string path, IEnumerable<string> contents)
  477. {
  478. if (contents == null)
  479. throw new ArgumentNullException("contents");
  480. using (var stream = this.AppendText(path))
  481. {
  482. foreach (var line in contents)
  483. {
  484. stream.WriteLine(line);
  485. }
  486. }
  487. }
  488. /// <summary>
  489. /// Appends lines to a file by using a specified encoding, and then closes the file.
  490. /// </summary>
  491. /// <param name="path">The file to append the lines to. The file is created if it does not already exist.</param>
  492. /// <param name="contents">The lines to append to the file.</param>
  493. /// <param name="encoding">The character encoding to use.</param>
  494. /// <exception cref="ArgumentNullException"><paramref name="path"/>, <paramref name="contents"/> or <paramref name="encoding"/> is null.</exception>
  495. public void AppendAllLines(string path, IEnumerable<string> contents, Encoding encoding)
  496. {
  497. if (contents == null)
  498. throw new ArgumentNullException("contents");
  499. using (var stream = this.AppendText(path, encoding))
  500. {
  501. foreach (var line in contents)
  502. {
  503. stream.WriteLine(line);
  504. }
  505. }
  506. }
  507. /// <summary>
  508. /// Opens a file, appends the specified string to the file, and then closes the file.
  509. /// If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.
  510. /// </summary>
  511. /// <param name="path">The file to append the specified string to.</param>
  512. /// <param name="contents">The string to append to the file.</param>
  513. /// <exception cref="ArgumentNullException"><paramref name="path"/> or <paramref name="contents"/> is null.</exception>
  514. public void AppendAllText(string path, string contents)
  515. {
  516. using (var stream = this.AppendText(path))
  517. {
  518. stream.Write(contents);
  519. }
  520. }
  521. /// <summary>
  522. /// Opens a file, appends the specified string to the file, and then closes the file.
  523. /// If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.
  524. /// </summary>
  525. /// <param name="path">The file to append the specified string to.</param>
  526. /// <param name="contents">The string to append to the file.</param>
  527. /// <param name="encoding">The character encoding to use.</param>
  528. /// <exception cref="ArgumentNullException"><paramref name="path"/>, <paramref name="contents"/> or <paramref name="encoding"/> is null.</exception>
  529. public void AppendAllText(string path, string contents, Encoding encoding)
  530. {
  531. using (var stream = this.AppendText(path, encoding))
  532. {
  533. stream.Write(contents);
  534. }
  535. }
  536. /// <summary>
  537. /// Creates a <see cref="System.IO.StreamWriter"/> that appends UTF-8 encoded text to an existing file.
  538. /// </summary>
  539. /// <param name="path">The path to the file to append to.</param>
  540. /// <returns>A StreamWriter that appends UTF-8 encoded text to an existing file.</returns>
  541. /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
  542. public StreamWriter AppendText(string path)
  543. {
  544. return this.AppendText(path, Encoding.UTF8);
  545. }
  546. /// <summary>
  547. /// Creates a <see cref="System.IO.StreamWriter"/> that appends UTF-8 encoded text to an existing file.
  548. /// </summary>
  549. /// <param name="path">The path to the file to append to.</param>
  550. /// <param name="encoding">The character encoding to use.</param>
  551. /// <returns>
  552. /// A StreamWriter that appends UTF-8 encoded text to an existing file.
  553. /// </returns>
  554. /// <exception cref="ArgumentNullException"><paramref name="path"/> or <paramref name="encoding"/> is null.</exception>
  555. public StreamWriter AppendText(string path, Encoding encoding)
  556. {
  557. if (encoding == null)
  558. throw new ArgumentNullException("encoding");
  559. return new StreamWriter(new SftpFileStream(this._sftpSession, path, FileMode.Append, FileAccess.Write), encoding);
  560. }
  561. /// <summary>
  562. /// Creates or overwrites a file in the specified path.
  563. /// </summary>
  564. /// <param name="path">The path and name of the file to create.</param>
  565. /// <returns>A <see cref="SftpFileStream"/> that provides read/write access to the file specified in path</returns>
  566. public SftpFileStream Create(string path)
  567. {
  568. return new SftpFileStream(this._sftpSession, path, FileMode.Create, FileAccess.ReadWrite);
  569. }
  570. /// <summary>
  571. /// Creates or overwrites the specified file.
  572. /// </summary>
  573. /// <param name="path">The path and name of the file to create.</param>
  574. /// <param name="bufferSize">The number of bytes buffered for reads and writes to the file.</param>
  575. /// <returns>A <see cref="SftpFileStream"/> that provides read/write access to the file specified in path</returns>
  576. public SftpFileStream Create(string path, int bufferSize)
  577. {
  578. return new SftpFileStream(this._sftpSession, path, FileMode.Create, FileAccess.ReadWrite, bufferSize);
  579. }
  580. /// <summary>
  581. /// Creates or opens a file for writing UTF-8 encoded text.
  582. /// </summary>
  583. /// <param name="path">The file to be opened for writing.</param>
  584. /// <returns>A <see cref="System.IO.StreamWriter"/> that writes to the specified file using UTF-8 encoding.</returns>
  585. public StreamWriter CreateText(string path)
  586. {
  587. return new StreamWriter(this.OpenWrite(path), Encoding.UTF8);
  588. }
  589. /// <summary>
  590. /// Creates or opens a file for writing UTF-8 encoded text.
  591. /// </summary>
  592. /// <param name="path">The file to be opened for writing.</param>
  593. /// <param name="encoding">The character encoding to use.</param>
  594. /// <returns> A <see cref="System.IO.StreamWriter"/> that writes to the specified file using UTF-8 encoding. </returns>
  595. public StreamWriter CreateText(string path, Encoding encoding)
  596. {
  597. return new StreamWriter(this.OpenWrite(path), encoding);
  598. }
  599. /// <summary>
  600. /// Deletes the specified file or directory. An exception is not thrown if the specified file does not exist.
  601. /// </summary>
  602. /// <param name="path">The name of the file or directory to be deleted. Wildcard characters are not supported.</param>
  603. public void Delete(string path)
  604. {
  605. var file = this.Get(path);
  606. file.Delete();
  607. }
  608. /// <summary>
  609. /// Returns the date and time the specified file or directory was last accessed.
  610. /// </summary>
  611. /// <param name="path">The file or directory for which to obtain access date and time information.</param>
  612. /// <returns>A <see cref="System.DateTime"/> structure set to the date and time that the specified file or directory was last accessed. This value is expressed in local time.</returns>
  613. public DateTime GetLastAccessTime(string path)
  614. {
  615. var file = this.Get(path);
  616. return file.LastAccessTime;
  617. }
  618. /// <summary>
  619. /// Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last accessed.
  620. /// </summary>
  621. /// <param name="path">The file or directory for which to obtain access date and time information.</param>
  622. /// <returns>A <see cref="System.DateTime"/> structure set to the date and time that the specified file or directory was last accessed. This value is expressed in UTC time.</returns>
  623. public DateTime GetLastAccessTimeUtc(string path)
  624. {
  625. var file = this.Get(path);
  626. return file.LastAccessTime.ToUniversalTime();
  627. }
  628. /// <summary>
  629. /// Returns the date and time the specified file or directory was last written to.
  630. /// </summary>
  631. /// <param name="path">The file or directory for which to obtain write date and time information.</param>
  632. /// <returns>A <see cref="System.DateTime"/> structure set to the date and time that the specified file or directory was last written to. This value is expressed in local time.</returns>
  633. public DateTime GetLastWriteTime(string path)
  634. {
  635. var file = this.Get(path);
  636. return file.LastWriteTime;
  637. }
  638. /// <summary>
  639. /// Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last written to.
  640. /// </summary>
  641. /// <param name="path">The file or directory for which to obtain write date and time information.</param>
  642. /// <returns>A <see cref="System.DateTime"/> structure set to the date and time that the specified file or directory was last written to. This value is expressed in UTC time.</returns>
  643. public DateTime GetLastWriteTimeUtc(string path)
  644. {
  645. var file = this.Get(path);
  646. return file.LastWriteTime.ToUniversalTime();
  647. }
  648. /// <summary>
  649. /// Opens a <see cref="SftpFileStream"/> on the specified path with read/write access.
  650. /// </summary>
  651. /// <param name="path">The file to open.</param>
  652. /// <param name="mode">A <see cref="System.IO.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>
  653. /// <returns>An unshared <see cref="SftpFileStream"/> that provides access to the specified file, with the specified mode and access.</returns>
  654. public SftpFileStream Open(string path, FileMode mode)
  655. {
  656. return new SftpFileStream(this._sftpSession, path, mode, FileAccess.ReadWrite);
  657. }
  658. /// <summary>
  659. /// Opens a <see cref="SftpFileStream"/> on the specified path, with the specified mode and access.
  660. /// </summary>
  661. /// <param name="path">The file to open.</param>
  662. /// <param name="mode">A <see cref="System.IO.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>
  663. /// <param name="access">A <see cref="System.IO.FileAccess"/> value that specifies the operations that can be performed on the file.</param>
  664. /// <returns>An unshared <see cref="SftpFileStream"/> that provides access to the specified file, with the specified mode and access.</returns>
  665. public SftpFileStream Open(string path, FileMode mode, FileAccess access)
  666. {
  667. return new SftpFileStream(this._sftpSession, path, mode, access);
  668. }
  669. /// <summary>
  670. /// Opens an existing file for reading.
  671. /// </summary>
  672. /// <param name="path">The file to be opened for reading.</param>
  673. /// <returns>A read-only System.IO.FileStream on the specified path.</returns>
  674. public SftpFileStream OpenRead(string path)
  675. {
  676. return new SftpFileStream(this._sftpSession, path, FileMode.Open, FileAccess.Read);
  677. }
  678. /// <summary>
  679. /// Opens an existing UTF-8 encoded text file for reading.
  680. /// </summary>
  681. /// <param name="path">The file to be opened for reading.</param>
  682. /// <returns>A <see cref="System.IO.StreamReader"/> on the specified path.</returns>
  683. public StreamReader OpenText(string path)
  684. {
  685. return new StreamReader(this.OpenRead(path), Encoding.UTF8);
  686. }
  687. /// <summary>
  688. /// Opens an existing file for writing.
  689. /// </summary>
  690. /// <param name="path">The file to be opened for writing.</param>
  691. /// <returns>An unshared <see cref="SftpFileStream"/> object on the specified path with <see cref="System.IO.FileAccess.Write"/> access.</returns>
  692. public SftpFileStream OpenWrite(string path)
  693. {
  694. return new SftpFileStream(this._sftpSession, path, FileMode.OpenOrCreate, FileAccess.Write);
  695. }
  696. /// <summary>
  697. /// Opens a binary file, reads the contents of the file into a byte array, and then closes the file.
  698. /// </summary>
  699. /// <param name="path">The file to open for reading.</param>
  700. /// <returns>A byte array containing the contents of the file.</returns>
  701. public byte[] ReadAllBytes(string path)
  702. {
  703. using (var stream = this.OpenRead(path))
  704. {
  705. var buffer = new byte[stream.Length];
  706. stream.Read(buffer, 0, buffer.Length);
  707. return buffer;
  708. }
  709. }
  710. /// <summary>
  711. /// Opens a text file, reads all lines of the file, and then closes the file.
  712. /// </summary>
  713. /// <param name="path">The file to open for reading.</param>
  714. /// <returns>A string array containing all lines of the file.</returns>
  715. public string[] ReadAllLines(string path)
  716. {
  717. return this.ReadAllLines(path, Encoding.UTF8);
  718. }
  719. /// <summary>
  720. /// Opens a file, reads all lines of the file with the specified encoding, and then closes the file.
  721. /// </summary>
  722. /// <param name="path">The file to open for reading.</param>
  723. /// <param name="encoding">The encoding applied to the contents of the file.</param>
  724. /// <returns>A string array containing all lines of the file.</returns>
  725. public string[] ReadAllLines(string path, Encoding encoding)
  726. {
  727. var lines = new List<string>();
  728. using (var stream = new StreamReader(this.OpenRead(path), encoding))
  729. {
  730. while (!stream.EndOfStream)
  731. {
  732. lines.Add(stream.ReadLine());
  733. }
  734. }
  735. return lines.ToArray();
  736. }
  737. /// <summary>
  738. /// Opens a text file, reads all lines of the file, and then closes the file.
  739. /// </summary>
  740. /// <param name="path">The file to open for reading.</param>
  741. /// <returns>A string containing all lines of the file.</returns>
  742. public string ReadAllText(string path)
  743. {
  744. return this.ReadAllText(path, Encoding.UTF8);
  745. }
  746. /// <summary>
  747. /// Opens a file, reads all lines of the file with the specified encoding, and then closes the file.
  748. /// </summary>
  749. /// <param name="path">The file to open for reading.</param>
  750. /// <param name="encoding">The encoding applied to the contents of the file.</param>
  751. /// <returns>A string containing all lines of the file.</returns>
  752. public string ReadAllText(string path, Encoding encoding)
  753. {
  754. var lines = new List<string>();
  755. using (var stream = new StreamReader(this.OpenRead(path), encoding))
  756. {
  757. return stream.ReadToEnd();
  758. }
  759. }
  760. /// <summary>
  761. /// Reads the lines of a file.
  762. /// </summary>
  763. /// <param name="path">The file to read.</param>
  764. /// <returns>The lines of the file.</returns>
  765. public IEnumerable<string> ReadLines(string path)
  766. {
  767. return this.ReadAllLines(path);
  768. }
  769. /// <summary>
  770. /// Read the lines of a file that has a specified encoding.
  771. /// </summary>
  772. /// <param name="path">The file to read.</param>
  773. /// <param name="encoding">The encoding that is applied to the contents of the file.</param>
  774. /// <returns>The lines of the file.</returns>
  775. public IEnumerable<string> ReadLines(string path, Encoding encoding)
  776. {
  777. return this.ReadAllLines(path, encoding);
  778. }
  779. /// <summary>
  780. /// Sets the date and time the specified file was last accessed.
  781. /// </summary>
  782. /// <param name="path">The file for which to set the access date and time information.</param>
  783. /// <param name="lastAccessTime">A <see cref="System.DateTime"/> containing the value to set for the last access date and time of path. This value is expressed in local time.</param>
  784. [Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")]
  785. public void SetLastAccessTime(string path, DateTime lastAccessTime)
  786. {
  787. throw new NotImplementedException();
  788. }
  789. /// <summary>
  790. /// Sets the date and time, in coordinated universal time (UTC), that the specified file was last accessed.
  791. /// </summary>
  792. /// <param name="path">The file for which to set the access date and time information.</param>
  793. /// <param name="lastAccessTimeUtc">A <see cref="System.DateTime"/> containing the value to set for the last access date and time of path. This value is expressed in UTC time.</param>
  794. [Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")]
  795. public void SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc)
  796. {
  797. throw new NotImplementedException();
  798. }
  799. /// <summary>
  800. /// Sets the date and time that the specified file was last written to.
  801. /// </summary>
  802. /// <param name="path">The file for which to set the date and time information.</param>
  803. /// <param name="lastWriteTime">A System.DateTime containing the value to set for the last write date and time of path. This value is expressed in local time.</param>
  804. [Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")]
  805. public void SetLastWriteTime(string path, DateTime lastWriteTime)
  806. {
  807. throw new NotImplementedException();
  808. }
  809. /// <summary>
  810. /// Sets the date and time, in coordinated universal time (UTC), that the specified file was last written to.
  811. /// </summary>
  812. /// <param name="path">The file for which to set the date and time information.</param>
  813. /// <param name="lastWriteTimeUtc">A System.DateTime containing the value to set for the last write date and time of path. This value is expressed in UTC time.</param>
  814. [Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")]
  815. public void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc)
  816. {
  817. throw new NotImplementedException();
  818. }
  819. /// <summary>
  820. /// Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.
  821. /// </summary>
  822. /// <param name="path">The file to write to.</param>
  823. /// <param name="bytes">The bytes to write to the file.</param>
  824. public void WriteAllBytes(string path, byte[] bytes)
  825. {
  826. using (var stream = this.OpenWrite(path))
  827. {
  828. stream.Write(bytes, 0, bytes.Length);
  829. }
  830. }
  831. /// <summary>
  832. /// Creates a new file, writes a collection of strings to the file, and then closes the file.
  833. /// </summary>
  834. /// <param name="path">The file to write to.</param>
  835. /// <param name="contents">The lines to write to the file.</param>
  836. public void WriteAllLines(string path, IEnumerable<string> contents)
  837. {
  838. this.WriteAllLines(path, contents, Encoding.UTF8);
  839. }
  840. /// <summary>
  841. /// Creates a new file, write the specified string array to the file, and then closes the file.
  842. /// </summary>
  843. /// <param name="path">The file to write to.</param>
  844. /// <param name="contents">The string array to write to the file.</param>
  845. public void WriteAllLines(string path, string[] contents)
  846. {
  847. this.WriteAllLines(path, contents, Encoding.UTF8);
  848. }
  849. /// <summary>
  850. /// Creates a new file by using the specified encoding, writes a collection of strings to the file, and then closes the file.
  851. /// </summary>
  852. /// <param name="path">The file to write to.</param>
  853. /// <param name="contents">The lines to write to the file.</param>
  854. /// <param name="encoding">The character encoding to use.</param>
  855. public void WriteAllLines(string path, IEnumerable<string> contents, Encoding encoding)
  856. {
  857. using (var stream = this.CreateText(path, encoding))
  858. {
  859. foreach (var line in contents)
  860. {
  861. stream.WriteLine(line);
  862. }
  863. }
  864. }
  865. /// <summary>
  866. /// Creates a new file, writes the specified string array to the file by using the specified encoding, and then closes the file.
  867. /// </summary>
  868. /// <param name="path">The file to write to.</param>
  869. /// <param name="contents">The string array to write to the file.</param>
  870. /// <param name="encoding">An <see cref="System.Text.Encoding"/> object that represents the character encoding applied to the string array.</param>
  871. public void WriteAllLines(string path, string[] contents, Encoding encoding)
  872. {
  873. using (var stream = this.CreateText(path, encoding))
  874. {
  875. foreach (var line in contents)
  876. {
  877. stream.WriteLine(line);
  878. }
  879. }
  880. }
  881. /// <summary>
  882. /// Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.
  883. /// </summary>
  884. /// <param name="path">The file to write to.</param>
  885. /// <param name="contents">The string to write to the file.</param>
  886. public void WriteAllText(string path, string contents)
  887. {
  888. using (var stream = this.CreateText(path))
  889. {
  890. stream.Write(contents);
  891. }
  892. }
  893. /// <summary>
  894. /// Creates a new file, writes the specified string to the file using the specified encoding, and then closes the file. If the target file already exists, it is overwritten.
  895. /// </summary>
  896. /// <param name="path">The file to write to.</param>
  897. /// <param name="contents">The string to write to the file.</param>
  898. /// <param name="encoding">The encoding to apply to the string.</param>
  899. public void WriteAllText(string path, string contents, Encoding encoding)
  900. {
  901. using (var stream = this.CreateText(path, encoding))
  902. {
  903. stream.Write(contents);
  904. }
  905. }
  906. /// <summary>
  907. /// Gets the <see cref="SftpFileAttributes"/> of the file on the path.
  908. /// </summary>
  909. /// <param name="path">The path to the file.</param>
  910. /// <returns>The <see cref="SftpFileAttributes"/> of the file on the path.</returns>
  911. public SftpFileAttributes GetAttributes(string path)
  912. {
  913. var fullPath = this._sftpSession.GetCanonicalPath(path);
  914. return this._sftpSession.RequestLStat(fullPath);
  915. }
  916. /// <summary>
  917. /// Sets the specified <see cref="SftpFileAttributes"/> of the file on the specified path.
  918. /// </summary>
  919. /// <param name="path">The path to the file.</param>
  920. /// <param name="fileAttributes">The desired <see cref="SftpFileAttributes"/>.</param>
  921. public void SetAttributes(string path, SftpFileAttributes fileAttributes)
  922. {
  923. var fullPath = this._sftpSession.GetCanonicalPath(path);
  924. this._sftpSession.RequestSetStat(fullPath, fileAttributes);
  925. }
  926. //public FileSecurity GetAccessControl(string path);
  927. //public FileSecurity GetAccessControl(string path, AccessControlSections includeSections);
  928. //public DateTime GetCreationTime(string path);
  929. //public DateTime GetCreationTimeUtc(string path);
  930. //public void SetAccessControl(string path, FileSecurity fileSecurity);
  931. //public void SetCreationTime(string path, DateTime creationTime);
  932. //public void SetCreationTimeUtc(string path, DateTime creationTimeUtc);
  933. #endregion
  934. private IEnumerable<SftpFile> InternalListDirectory(string path, SftpListDirectoryAsyncResult asynchResult)
  935. {
  936. if (path == null)
  937. throw new ArgumentNullException("path");
  938. // Ensure that connection is established.
  939. this.EnsureConnection();
  940. var fullPath = this._sftpSession.GetCanonicalPath(path);
  941. var handle = this._sftpSession.RequestOpenDir(fullPath);
  942. var basePath = fullPath;
  943. if (!basePath.EndsWith("/"))
  944. basePath = string.Format("{0}/", fullPath);
  945. var result = new List<SftpFile>();
  946. var files = this._sftpSession.RequestReadDir(handle);
  947. while (files != null)
  948. {
  949. result.AddRange(from f in files
  950. select new SftpFile(this._sftpSession, string.Format(CultureInfo.InvariantCulture, "{0}{1}", basePath, f.Key), f.Value));
  951. if (asynchResult != null)
  952. {
  953. asynchResult.Update(result.Count);
  954. }
  955. files = this._sftpSession.RequestReadDir(handle);
  956. }
  957. this._sftpSession.RequestClose(handle);
  958. return result;
  959. }
  960. private void InternalDownloadFile(string path, Stream output, SftpDownloadAsyncResult asynchResult)
  961. {
  962. if (output == null)
  963. throw new ArgumentNullException("output");
  964. if (string.IsNullOrWhiteSpace(path))
  965. throw new ArgumentException("path");
  966. // Ensure that connection is established.
  967. this.EnsureConnection();
  968. var fullPath = this._sftpSession.GetCanonicalPath(path);
  969. var handle = this._sftpSession.RequestOpen(fullPath, Flags.Read);
  970. ulong offset = 0;
  971. var data = this._sftpSession.RequestRead(handle, offset, this.BufferSize);
  972. // Read data while available
  973. while (data.Length > 0)
  974. {
  975. output.Write(data, 0, data.Length);
  976. output.Flush();
  977. offset += (ulong)data.Length;
  978. // Call callback to report number of bytes read
  979. if (asynchResult != null)
  980. {
  981. asynchResult.Update(offset);
  982. }
  983. data = this._sftpSession.RequestRead(handle, offset, this.BufferSize);
  984. }
  985. this._sftpSession.RequestClose(handle);
  986. }
  987. private void InternalUploadFile(Stream input, string path, SftpUploadAsyncResult asynchResult)
  988. {
  989. if (input == null)
  990. throw new ArgumentNullException("input");
  991. if (string.IsNullOrWhiteSpace(path))
  992. throw new ArgumentException("path");
  993. // Ensure that connection is established.
  994. this.EnsureConnection();
  995. var fullPath = this._sftpSession.GetCanonicalPath(path);
  996. var handle = this._sftpSession.RequestOpen(fullPath, Flags.Write | Flags.CreateNewOrOpen | Flags.Truncate);
  997. ulong offset = 0;
  998. var buffer = new byte[this.BufferSize];
  999. var uploadCompleted = false;
  1000. do
  1001. {
  1002. var bytesRead = input.Read(buffer, 0, buffer.Length);
  1003. if (bytesRead < this.BufferSize)
  1004. {
  1005. var data = new byte[bytesRead];
  1006. Array.Copy(buffer, data, bytesRead);
  1007. this._sftpSession.RequestWrite(handle, offset, data);
  1008. uploadCompleted = true;
  1009. }
  1010. else
  1011. {
  1012. this._sftpSession.RequestWrite(handle, offset, buffer);
  1013. }
  1014. offset += (uint)bytesRead;
  1015. // Call callback to report number of bytes read
  1016. if (asynchResult != null)
  1017. {
  1018. asynchResult.Update(offset);
  1019. }
  1020. } while (!uploadCompleted);
  1021. this._sftpSession.RequestClose(handle);
  1022. }
  1023. partial void ExecuteThread(Action action);
  1024. /// <summary>
  1025. /// Called when client is connected to the server.
  1026. /// </summary>
  1027. protected override void OnConnected()
  1028. {
  1029. base.OnConnected();
  1030. this._sftpSession = new SftpSession(this.Session, this.OperationTimeout);
  1031. this._sftpSession.Connect();
  1032. // Resolve current running version
  1033. this.ProtocolVersion = this._sftpSession.ProtocolVersion;
  1034. }
  1035. /// <summary>
  1036. /// Called when client is disconnecting from the server.
  1037. /// </summary>
  1038. protected override void OnDisconnecting()
  1039. {
  1040. base.OnDisconnecting();
  1041. this._sftpSession.Disconnect();
  1042. }
  1043. /// <summary>
  1044. /// Releases unmanaged and - optionally - managed resources
  1045. /// </summary>
  1046. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
  1047. protected override void Dispose(bool disposing)
  1048. {
  1049. if (this._sftpSession != null)
  1050. {
  1051. this._sftpSession.Dispose();
  1052. this._sftpSession = null;
  1053. }
  1054. base.Dispose(disposing);
  1055. }
  1056. }
  1057. }