SftpClient.cs 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  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. using System.Threading;
  10. using System.Diagnostics.CodeAnalysis;
  11. namespace Renci.SshNet
  12. {
  13. /// <summary>
  14. /// Implementation of the SSH File Transfer Protocol (SFTP) over SSH.
  15. /// </summary>
  16. public partial class SftpClient : BaseClient
  17. {
  18. /// <summary>
  19. /// Holds SftpSession instance that used to communicate to the SFTP server
  20. /// </summary>
  21. private SftpSession _sftpSession;
  22. private bool _disposeConnectionInfo;
  23. /// <summary>
  24. /// Gets or sets the operation timeout.
  25. /// </summary>
  26. /// <value>The operation timeout.</value>
  27. public TimeSpan OperationTimeout { get; set; }
  28. /// <summary>
  29. /// Gets or sets the size of the buffer.
  30. /// </summary>
  31. /// <value>The size of the buffer.</value>
  32. public uint BufferSize { get; set; }
  33. /// <summary>
  34. /// Gets remote working directory.
  35. /// </summary>
  36. public string WorkingDirectory
  37. {
  38. get
  39. {
  40. if (this._sftpSession == null)
  41. return null;
  42. return this._sftpSession.WorkingDirectory;
  43. }
  44. }
  45. /// <summary>
  46. /// Gets sftp protocol version.
  47. /// </summary>
  48. public int ProtocolVersion { get; private set; }
  49. #region Constructors
  50. /// <summary>
  51. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  52. /// </summary>
  53. /// <param name="connectionInfo">The connection info.</param>
  54. /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <b>null</b>.</exception>
  55. public SftpClient(ConnectionInfo connectionInfo)
  56. : base(connectionInfo)
  57. {
  58. this.OperationTimeout = new TimeSpan(0, 0, 0, 0, -1);
  59. this.BufferSize = 1024 * 16;
  60. }
  61. /// <summary>
  62. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  63. /// </summary>
  64. /// <param name="host">Connection host.</param>
  65. /// <param name="port">Connection port.</param>
  66. /// <param name="username">Authentication username.</param>
  67. /// <param name="password">Authentication password.</param>
  68. /// <exception cref="ArgumentNullException"><paramref name="password"/> is <b>null</b>.</exception>
  69. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid. <para>-or-</para> <paramref name="username"/> is <b>null</b> or contains whitespace characters.</exception>
  70. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="F:System.Net.IPEndPoint.MinPort"/> and <see cref="System.Net.IPEndPoint.MaxPort"/>.</exception>
  71. [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope", Justification = "Disposed in Dispose(bool) method.")]
  72. public SftpClient(string host, int port, string username, string password)
  73. : this(new PasswordConnectionInfo(host, port, username, password))
  74. {
  75. this._disposeConnectionInfo = true;
  76. }
  77. /// <summary>
  78. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  79. /// </summary>
  80. /// <param name="host">Connection host.</param>
  81. /// <param name="username">Authentication username.</param>
  82. /// <param name="password">Authentication password.</param>
  83. /// <exception cref="ArgumentNullException"><paramref name="password"/> is <b>null</b>.</exception>
  84. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid. <para>-or-</para> <paramref name="username"/> is <b>null</b> contains whitespace characters.</exception>
  85. public SftpClient(string host, string username, string password)
  86. : this(host, ConnectionInfo.DEFAULT_PORT, username, password)
  87. {
  88. }
  89. /// <summary>
  90. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  91. /// </summary>
  92. /// <param name="host">Connection host.</param>
  93. /// <param name="port">Connection port.</param>
  94. /// <param name="username">Authentication username.</param>
  95. /// <param name="keyFiles">Authentication private key file(s) .</param>
  96. /// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is <b>null</b>.</exception>
  97. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid. <para>-or-</para> <paramref name="username"/> is nu<b>null</b>ll or contains whitespace characters.</exception>
  98. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="F:System.Net.IPEndPoint.MinPort"/> and <see cref="System.Net.IPEndPoint.MaxPort"/>.</exception>
  99. [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope", Justification = "Disposed in Dispose(bool) method.")]
  100. public SftpClient(string host, int port, string username, params PrivateKeyFile[] keyFiles)
  101. : this(new PrivateKeyConnectionInfo(host, port, username, keyFiles))
  102. {
  103. this._disposeConnectionInfo = true;
  104. }
  105. /// <summary>
  106. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  107. /// </summary>
  108. /// <param name="host">Connection host.</param>
  109. /// <param name="username">Authentication username.</param>
  110. /// <param name="keyFiles">Authentication private key file(s) .</param>
  111. /// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is <b>null</b>.</exception>
  112. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid. <para>-or-</para> <paramref name="username"/> is <b>null</b> or contains whitespace characters.</exception>
  113. public SftpClient(string host, string username, params PrivateKeyFile[] keyFiles)
  114. : this(host, ConnectionInfo.DEFAULT_PORT, username, keyFiles)
  115. {
  116. }
  117. #endregion
  118. /// <summary>
  119. /// Changes remote directory to path.
  120. /// </summary>
  121. /// <param name="path">New directory path.</param>
  122. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  123. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  124. /// <exception cref="SftpPermissionDeniedException">Permission to change directory denied by remote host. <para>-or-</para> A SSH command was denied by the server.</exception>
  125. /// <exception cref="SftpPathNotFoundException">The path in <paramref name="path"/> was not found on the remote host.</exception>
  126. /// <exception cref="T:Renci.SshNet.Common.SshException">A SSH error where <see cref="P:System.Exception.Message"/> is the message from the remote host.</exception>
  127. public void ChangeDirectory(string path)
  128. {
  129. if (path == null)
  130. throw new ArgumentNullException("path");
  131. this._sftpSession.ChangeDirectory(path);
  132. }
  133. /// <summary>
  134. /// Changes permissions of file(s) to specified mode.
  135. /// </summary>
  136. /// <param name="path">File(s) path, may match multiple files.</param>
  137. /// <param name="mode">The mode.</param>
  138. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  139. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  140. /// <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>
  141. /// <exception cref="SftpPathNotFoundException">The path in <paramref name="path"/> was not found on the remote host.</exception>
  142. /// <exception cref="T:Renci.SshNet.Common.SshException">A SSH error where <see cref="P:System.Exception.Message"/> is the message from the remote host.</exception>
  143. public void ChangePermissions(string path, short mode)
  144. {
  145. var file = this.Get(path);
  146. file.SetPermissions(mode);
  147. }
  148. /// <summary>
  149. /// Creates remote directory specified by path.
  150. /// </summary>
  151. /// <param name="path">Directory path to create.</param>
  152. /// <exception cref="ArgumentException"><paramref name="path"/> is <b>null</b> or contains whitespace characters.</exception>
  153. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  154. /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to create the directory was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>
  155. /// <exception cref="T:Renci.SshNet.Common.SshException">A SSH error where <see cref="P:System.Exception.Message"/> is the message from the remote host.</exception>
  156. public void CreateDirectory(string path)
  157. {
  158. if (path.IsNullOrWhiteSpace())
  159. throw new ArgumentException(path);
  160. var fullPath = this._sftpSession.GetCanonicalPath(path);
  161. this._sftpSession.RequestMkDir(fullPath);
  162. }
  163. /// <summary>
  164. /// Deletes remote directory specified by path.
  165. /// </summary>
  166. /// <param name="path">Directory to be deleted path.</param>
  167. /// <exception cref="ArgumentException"><paramref name="path"/> is <b>null</b> or contains whitespace characters.</exception>
  168. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  169. /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to delete the directory was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>
  170. /// <exception cref="T:Renci.SshNet.Common.SshException">A SSH error where <see cref="P:System.Exception.Message"/> is the message from the remote host.</exception>
  171. public void DeleteDirectory(string path)
  172. {
  173. if (path.IsNullOrWhiteSpace())
  174. throw new ArgumentException("path");
  175. var fullPath = this._sftpSession.GetCanonicalPath(path);
  176. this._sftpSession.RequestRmDir(fullPath);
  177. }
  178. /// <summary>
  179. /// Deletes remote file specified by path.
  180. /// </summary>
  181. /// <param name="path">File to be deleted path.</param>
  182. /// <exception cref="ArgumentException"><paramref name="path"/> is <b>null</b> or contains whitespace characters.</exception>
  183. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  184. /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to delete the file was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>
  185. /// <exception cref="T:Renci.SshNet.Common.SshException">A SSH error where <see cref="P:System.Exception.Message"/> is the message from the remote host.</exception>
  186. public void DeleteFile(string path)
  187. {
  188. if (path.IsNullOrWhiteSpace())
  189. throw new ArgumentException("path");
  190. var fullPath = this._sftpSession.GetCanonicalPath(path);
  191. this._sftpSession.RequestRemove(fullPath);
  192. }
  193. /// <summary>
  194. /// Renames remote file from old path to new path.
  195. /// </summary>
  196. /// <param name="oldPath">Path to the old file location.</param>
  197. /// <param name="newPath">Path to the new file location.</param>
  198. /// <exception cref="ArgumentNullException"><paramref name="oldPath"/> is <b>null</b>. <para>-or-</para> or <paramref name="newPath"/> is <b>null</b>.</exception>
  199. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  200. /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to rename the file was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>
  201. /// <exception cref="T:Renci.SshNet.Common.SshException">A SSH error where <see cref="P:System.Exception.Message"/> is the message from the remote host.</exception>
  202. public void RenameFile(string oldPath, string newPath)
  203. {
  204. this.RenameFile(oldPath, newPath, false);
  205. }
  206. /// <summary>
  207. /// Renames remote file from old path to new path.
  208. /// </summary>
  209. /// <param name="oldPath">Path to the old file location.</param>
  210. /// <param name="newPath">Path to the new file location.</param>
  211. /// <param name="isPosix">if set to <c>true</c> then perform a posix rename.</param>
  212. /// <exception cref="System.ArgumentNullException">oldPath</exception>
  213. /// <exception cref="ArgumentNullException"><paramref name="oldPath" /> is <b>null</b>. <para>-or-</para> or <paramref name="newPath" /> is <b>null</b>.</exception>
  214. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  215. /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to rename the file was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>
  216. /// <exception cref="T:Renci.SshNet.Common.SshException">A SSH error where <see cref="P:System.Exception.Message" /> is the message from the remote host.</exception>
  217. public void RenameFile(string oldPath, string newPath, bool isPosix)
  218. {
  219. if (oldPath == null)
  220. throw new ArgumentNullException("oldPath");
  221. if (newPath == null)
  222. throw new ArgumentNullException("newPath");
  223. var oldFullPath = this._sftpSession.GetCanonicalPath(oldPath);
  224. var newFullPath = this._sftpSession.GetCanonicalPath(newPath);
  225. if (isPosix)
  226. {
  227. this._sftpSession.RequestPosixRename(oldFullPath, newFullPath);
  228. }
  229. else
  230. {
  231. this._sftpSession.RequestRename(oldFullPath, newFullPath);
  232. }
  233. }
  234. /// <summary>
  235. /// Creates a symbolic link from old path to new path.
  236. /// </summary>
  237. /// <param name="path">The old path.</param>
  238. /// <param name="linkPath">The new path.</param>
  239. /// <exception cref="ArgumentException"><paramref name="path"/> is <b>null</b>. <para>-or-</para> <paramref name="linkPath"/> is <b>null</b> or contains whitespace characters.</exception>
  240. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  241. /// <exception cref="Renci.SshNet.Common.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>
  242. /// <exception cref="T:Renci.SshNet.Common.SshException">A SSH error where <see cref="P:System.Exception.Message"/> is the message from the remote host.</exception>
  243. public void SymbolicLink(string path, string linkPath)
  244. {
  245. if (path.IsNullOrWhiteSpace())
  246. throw new ArgumentException("path");
  247. if (linkPath.IsNullOrWhiteSpace())
  248. throw new ArgumentException("linkPath");
  249. var fullPath = this._sftpSession.GetCanonicalPath(path);
  250. var linkFullPath = this._sftpSession.GetCanonicalPath(linkPath);
  251. this._sftpSession.RequestSymLink(fullPath, linkFullPath);
  252. }
  253. /// <summary>
  254. /// Retrieves list of files in remote directory.
  255. /// </summary>
  256. /// <param name="path">The path.</param>
  257. /// <param name="listCallback">The list callback.</param>
  258. /// <returns>
  259. /// List of directory entries
  260. /// </returns>
  261. /// <exception cref="ArgumentNullException"><paramref name="path" /> is <b>null</b>.</exception>
  262. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  263. /// <exception cref="Renci.SshNet.Common.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>
  264. /// <exception cref="T:Renci.SshNet.Common.SshException">A SSH error where <see cref="P:System.Exception.Message" /> is the message from the remote host.</exception>
  265. public IEnumerable<SftpFile> ListDirectory(string path, Action<int> listCallback = null)
  266. {
  267. return InternalListDirectory(path, listCallback);
  268. }
  269. /// <summary>
  270. /// Begins an asynchronous operation of retrieving list of files in remote directory.
  271. /// </summary>
  272. /// <param name="path">The path.</param>
  273. /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>
  274. /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests.</param>
  275. /// <param name="listCallback">The list callback.</param>
  276. /// <returns>
  277. /// An <see cref="IAsyncResult" /> that references the asynchronous operation.
  278. /// </returns>
  279. public IAsyncResult BeginListDirectory(string path, AsyncCallback asyncCallback, object state, Action<int> listCallback = null)
  280. {
  281. var asyncResult = new SftpListDirectoryAsyncResult(asyncCallback, state);
  282. this.ExecuteThread(() =>
  283. {
  284. try
  285. {
  286. var result = this.InternalListDirectory(path, (count) =>
  287. {
  288. asyncResult.Update(count);
  289. if (listCallback != null)
  290. {
  291. listCallback(count);
  292. }
  293. });
  294. asyncResult.SetAsCompleted(result, false);
  295. }
  296. catch (Exception exp)
  297. {
  298. asyncResult.SetAsCompleted(exp, false);
  299. }
  300. });
  301. return asyncResult;
  302. }
  303. /// <summary>
  304. /// Ends an asynchronous operation of retrieving list of files in remote directory.
  305. /// </summary>
  306. /// <param name="asyncResult">The pending asynchronous SFTP request.</param>
  307. /// <returns>
  308. /// List of files
  309. /// </returns>
  310. /// <exception cref="ArgumentException">The IAsyncResult object (<paramref name="asyncResult"/>) did not come from the corresponding async method on this type. <para>-or-</para> EndExecute was called multiple times with the same IAsyncResult.</exception>
  311. public IEnumerable<SftpFile> EndListDirectory(IAsyncResult asyncResult)
  312. {
  313. var ar = asyncResult as SftpListDirectoryAsyncResult;
  314. if (ar == null || ar.EndInvokeCalled)
  315. 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.");
  316. // Wait for operation to complete, then return result or throw exception
  317. return ar.EndInvoke();
  318. }
  319. /// <summary>
  320. /// Gets reference to remote file or directory.
  321. /// </summary>
  322. /// <param name="path">The path.</param>
  323. /// <returns>Reference to <see cref="Renci.SshNet.Sftp.SftpFile"/> file object.</returns>
  324. /// <exception cref="System.ArgumentNullException">path</exception>
  325. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  326. /// <exception cref="ArgumentNullException"><paramref name="path" /> is <b>null</b>.</exception>
  327. public SftpFile Get(string path)
  328. {
  329. if (path == null)
  330. throw new ArgumentNullException("path");
  331. var fullPath = this._sftpSession.GetCanonicalPath(path);
  332. var attributes = this._sftpSession.RequestLStat(fullPath);
  333. return new SftpFile(this._sftpSession, fullPath, attributes);
  334. }
  335. /// <summary>
  336. /// Checks whether file pr directory exists;
  337. /// </summary>
  338. /// <param name="path">The path.</param>
  339. /// <returns><c>true</c> if directory or file exists; otherwise <c>false</c>.</returns>
  340. /// <exception cref="ArgumentException"><paramref name="path"/> is <b>null</b> or contains whitespace characters.</exception>
  341. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  342. /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to perform the operation was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>
  343. /// <exception cref="T:Renci.SshNet.Common.SshException">A SSH error where <see cref="P:System.Exception.Message"/> is the message from the remote host.</exception>
  344. public bool Exists(string path)
  345. {
  346. if (path.IsNullOrWhiteSpace())
  347. throw new ArgumentException("path");
  348. var fullPath = this._sftpSession.GetCanonicalPath(path);
  349. // Try to open as a file
  350. var handle = this._sftpSession.RequestOpen(fullPath, Flags.Read, true);
  351. if (handle == null)
  352. {
  353. handle = this._sftpSession.RequestOpenDir(fullPath, true);
  354. }
  355. if (handle == null)
  356. {
  357. return false;
  358. }
  359. else
  360. {
  361. this._sftpSession.RequestClose(handle);
  362. return true;
  363. }
  364. }
  365. /// <summary>
  366. /// Downloads remote file specified by the path into the stream.
  367. /// </summary>
  368. /// <param name="path">File to download.</param>
  369. /// <param name="output">Stream to write the file into.</param>
  370. /// <param name="downloadCallback">The download callback.</param>
  371. /// <exception cref="ArgumentNullException"><paramref name="output" /> is <b>null</b>.</exception>
  372. /// <exception cref="ArgumentException"><paramref name="path" /> is <b>null</b> or contains whitespace characters.</exception>
  373. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  374. /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to perform the operation was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>
  375. /// <exception cref="T:Renci.SshNet.Common.SshException">A SSH error where <see cref="P:System.Exception.Message" /> is the message from the remote host.</exception>
  376. /// <remarks>
  377. /// Method calls made by this method to <paramref name="output" />, may under certain conditions result in exceptions thrown by the stream.
  378. /// </remarks>
  379. public void DownloadFile(string path, Stream output, Action<ulong> downloadCallback = null)
  380. {
  381. this.InternalDownloadFile(path, output, null, downloadCallback);
  382. }
  383. /// <summary>
  384. /// Begins an asynchronous file downloading into the stream.
  385. /// </summary>
  386. /// <param name="path">The path.</param>
  387. /// <param name="output">The output.</param>
  388. /// <returns>
  389. /// An <see cref="IAsyncResult" /> that references the asynchronous operation.
  390. /// </returns>
  391. /// <exception cref="System.ArgumentException">path</exception>
  392. /// <exception cref="System.ArgumentNullException">output</exception>
  393. /// <exception cref="ArgumentNullException"><paramref name="output" /> is <b>null</b>.</exception>
  394. /// <exception cref="ArgumentException"><paramref name="path" /> is <b>null</b> or contains whitespace characters.</exception>
  395. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  396. /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to perform the operation was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>
  397. /// <exception cref="T:Renci.SshNet.Common.SshException">A SSH error where <see cref="P:System.Exception.Message" /> is the message from the remote host.</exception>
  398. /// <remarks>
  399. /// Method calls made by this method to <paramref name="output" />, may under certain conditions result in exceptions thrown by the stream.
  400. /// </remarks>
  401. public IAsyncResult BeginDownloadFile(string path, Stream output)
  402. {
  403. return this.BeginDownloadFile(path, output, null, null, null);
  404. }
  405. /// <summary>
  406. /// Begins an asynchronous file downloading into the stream.
  407. /// </summary>
  408. /// <param name="path">The path.</param>
  409. /// <param name="output">The output.</param>
  410. /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>
  411. /// <returns>
  412. /// An <see cref="IAsyncResult" /> that references the asynchronous operation.
  413. /// </returns>
  414. /// <exception cref="System.ArgumentException">path</exception>
  415. /// <exception cref="System.ArgumentNullException">output</exception>
  416. /// <exception cref="ArgumentNullException"><paramref name="output" /> is <b>null</b>.</exception>
  417. /// <exception cref="ArgumentException"><paramref name="path" /> is <b>null</b> or contains whitespace characters.</exception>
  418. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  419. /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to perform the operation was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>
  420. /// <exception cref="T:Renci.SshNet.Common.SshException">A SSH error where <see cref="P:System.Exception.Message" /> is the message from the remote host.</exception>
  421. /// <remarks>
  422. /// Method calls made by this method to <paramref name="output" />, may under certain conditions result in exceptions thrown by the stream.
  423. /// </remarks>
  424. public IAsyncResult BeginDownloadFile(string path, Stream output, AsyncCallback asyncCallback)
  425. {
  426. return this.BeginDownloadFile(path, output, asyncCallback, null, null);
  427. }
  428. /// <summary>
  429. /// Begins an asynchronous file downloading into the stream.
  430. /// </summary>
  431. /// <param name="path">The path.</param>
  432. /// <param name="output">The output.</param>
  433. /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>
  434. /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests.</param>
  435. /// <param name="downloadCallback">The download callback.</param>
  436. /// <returns>
  437. /// An <see cref="IAsyncResult" /> that references the asynchronous operation.
  438. /// </returns>
  439. /// <exception cref="System.ArgumentException">path</exception>
  440. /// <exception cref="System.ArgumentNullException">output</exception>
  441. /// <exception cref="ArgumentNullException"><paramref name="output" /> is <b>null</b>.</exception>
  442. /// <exception cref="ArgumentException"><paramref name="path" /> is <b>null</b> or contains whitespace characters.</exception>
  443. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  444. /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to perform the operation was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>
  445. /// <exception cref="T:Renci.SshNet.Common.SshException">A SSH error where <see cref="P:System.Exception.Message" /> is the message from the remote host.</exception>
  446. /// <remarks>
  447. /// Method calls made by this method to <paramref name="output" />, may under certain conditions result in exceptions thrown by the stream.
  448. /// </remarks>
  449. public IAsyncResult BeginDownloadFile(string path, Stream output, AsyncCallback asyncCallback, object state, Action<ulong> downloadCallback = null)
  450. {
  451. if (path.IsNullOrWhiteSpace())
  452. throw new ArgumentException("path");
  453. if (output == null)
  454. throw new ArgumentNullException("output");
  455. var asyncResult = new SftpDownloadAsyncResult(asyncCallback, state);
  456. this.ExecuteThread(() =>
  457. {
  458. try
  459. {
  460. this.InternalDownloadFile(path, output, asyncResult, (offset) =>
  461. {
  462. asyncResult.Update(offset);
  463. if (downloadCallback != null)
  464. {
  465. downloadCallback(offset);
  466. }
  467. });
  468. asyncResult.SetAsCompleted(null, false);
  469. }
  470. catch (Exception exp)
  471. {
  472. asyncResult.SetAsCompleted(exp, false);
  473. }
  474. });
  475. return asyncResult;
  476. }
  477. /// <summary>
  478. /// Ends an asynchronous file downloading into the stream.
  479. /// </summary>
  480. /// <param name="asyncResult">The pending asynchronous SFTP request.</param>
  481. /// <exception cref="ArgumentException">The IAsyncResult object (<paramref name="asyncResult"/>) did not come from the corresponding async method on this type. <para>-or-</para> EndExecute was called multiple times with the same IAsyncResult.</exception>
  482. public void EndDownloadFile(IAsyncResult asyncResult)
  483. {
  484. var ar = asyncResult as SftpDownloadAsyncResult;
  485. if (ar == null || ar.EndInvokeCalled)
  486. 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.");
  487. // Wait for operation to complete, then return result or throw exception
  488. ar.EndInvoke();
  489. }
  490. /// <summary>
  491. /// Uploads stream into remote file..
  492. /// </summary>
  493. /// <param name="input">Data input stream.</param>
  494. /// <param name="path">Remote file path.</param>
  495. /// <param name="uploadCallback">The upload callback.</param>
  496. /// <exception cref="ArgumentNullException"><paramref name="input" /> is <b>null</b>.</exception>
  497. /// <exception cref="ArgumentException"><paramref name="path" /> is <b>null</b> or contains whitespace characters.</exception>
  498. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  499. /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to upload the file was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>
  500. /// <exception cref="T:Renci.SshNet.Common.SshException">A SSH error where <see cref="P:System.Exception.Message" /> is the message from the remote host.</exception>
  501. /// <remarks>
  502. /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in exceptions thrown by the stream.
  503. /// </remarks>
  504. public void UploadFile(Stream input, string path, Action<ulong> uploadCallback = null)
  505. {
  506. this.UploadFile(input, path, true, uploadCallback);
  507. }
  508. /// <summary>
  509. /// Uploads stream into remote file..
  510. /// </summary>
  511. /// <param name="input">Data input stream.</param>
  512. /// <param name="path">Remote file path.</param>
  513. /// <param name="canOverride">if set to <c>true</c> then existing file will be overwritten.</param>
  514. /// <param name="uploadCallback">The upload callback.</param>
  515. /// <exception cref="ArgumentNullException"><paramref name="input" /> is <b>null</b>.</exception>
  516. /// <exception cref="ArgumentException"><paramref name="path" /> is <b>null</b> or contains whitespace characters.</exception>
  517. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  518. /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to upload the file was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>
  519. /// <exception cref="T:Renci.SshNet.Common.SshException">A SSH error where <see cref="P:System.Exception.Message" /> is the message from the remote host.</exception>
  520. /// <remarks>
  521. /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in exceptions thrown by the stream.
  522. /// </remarks>
  523. public void UploadFile(Stream input, string path, bool canOverride, Action<ulong> uploadCallback = null)
  524. {
  525. var flags = Flags.Write | Flags.Truncate;
  526. if (canOverride)
  527. flags |= Flags.CreateNewOrOpen;
  528. else
  529. flags |= Flags.CreateNew;
  530. this.InternalUploadFile(input, path, flags, null, uploadCallback);
  531. }
  532. /// <summary>
  533. /// Begins an asynchronous uploading the steam into remote file.
  534. /// </summary>
  535. /// <param name="input">Data input stream.</param>
  536. /// <param name="path">Remote file path.</param>
  537. /// <returns>
  538. /// An <see cref="IAsyncResult" /> that references the asynchronous operation.
  539. /// </returns>
  540. /// <exception cref="ArgumentNullException"><paramref name="input" /> is <b>null</b>.</exception>
  541. /// <exception cref="ArgumentException"><paramref name="path" /> is <b>null</b> or contains whitespace characters.</exception>
  542. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  543. /// <exception cref="Renci.SshNet.Common.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>
  544. /// <exception cref="T:Renci.SshNet.Common.SshException">A SSH error where <see cref="P:System.Exception.Message" /> is the message from the remote host.</exception>
  545. /// <remarks>
  546. /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in exceptions thrown by the stream.
  547. /// </remarks>
  548. public IAsyncResult BeginUploadFile(Stream input, string path)
  549. {
  550. return this.BeginUploadFile(input, path, true, null, null, null);
  551. }
  552. /// <summary>
  553. /// Begins an asynchronous uploading the steam into remote file.
  554. /// </summary>
  555. /// <param name="input">Data input stream.</param>
  556. /// <param name="path">Remote file path.</param>
  557. /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>
  558. /// <returns>
  559. /// An <see cref="IAsyncResult" /> that references the asynchronous operation.
  560. /// </returns>
  561. /// <exception cref="ArgumentNullException"><paramref name="input" /> is <b>null</b>.</exception>
  562. /// <exception cref="ArgumentException"><paramref name="path" /> is <b>null</b> or contains whitespace characters.</exception>
  563. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  564. /// <exception cref="Renci.SshNet.Common.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>
  565. /// <exception cref="T:Renci.SshNet.Common.SshException">A SSH error where <see cref="P:System.Exception.Message" /> is the message from the remote host.</exception>
  566. /// <remarks>
  567. /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in exceptions thrown by the stream.
  568. /// </remarks>
  569. public IAsyncResult BeginUploadFile(Stream input, string path, AsyncCallback asyncCallback)
  570. {
  571. return this.BeginUploadFile(input, path, true, asyncCallback, null, null);
  572. }
  573. /// <summary>
  574. /// Begins an asynchronous uploading the steam into remote file.
  575. /// </summary>
  576. /// <param name="input">Data input stream.</param>
  577. /// <param name="path">Remote file path.</param>
  578. /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>
  579. /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests.</param>
  580. /// <param name="uploadCallback">The upload callback.</param>
  581. /// <returns>
  582. /// An <see cref="IAsyncResult" /> that references the asynchronous operation.
  583. /// </returns>
  584. /// <exception cref="ArgumentNullException"><paramref name="input" /> is <b>null</b>.</exception>
  585. /// <exception cref="ArgumentException"><paramref name="path" /> is <b>null</b> or contains whitespace characters.</exception>
  586. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  587. /// <exception cref="Renci.SshNet.Common.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>
  588. /// <exception cref="T:Renci.SshNet.Common.SshException">A SSH error where <see cref="P:System.Exception.Message" /> is the message from the remote host.</exception>
  589. /// <remarks>
  590. /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in exceptions thrown by the stream.
  591. /// </remarks>
  592. public IAsyncResult BeginUploadFile(Stream input, string path, AsyncCallback asyncCallback, object state, Action<ulong> uploadCallback = null)
  593. {
  594. return this.BeginUploadFile(input, path, true, asyncCallback, state, uploadCallback);
  595. }
  596. /// <summary>
  597. /// Begins an asynchronous uploading the steam into remote file.
  598. /// </summary>
  599. /// <param name="input">Data input stream.</param>
  600. /// <param name="path">Remote file path.</param>
  601. /// <param name="canOverride">if set to <c>true</c> then existing file will be overwritten.</param>
  602. /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>
  603. /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests.</param>
  604. /// <param name="uploadCallback">The upload callback.</param>
  605. /// <returns>
  606. /// An <see cref="IAsyncResult" /> that references the asynchronous operation.
  607. /// </returns>
  608. /// <exception cref="System.ArgumentNullException">input</exception>
  609. /// <exception cref="System.ArgumentException">path</exception>
  610. /// <exception cref="ArgumentNullException"><paramref name="input" /> is <b>null</b>.</exception>
  611. /// <exception cref="ArgumentException"><paramref name="path" /> is <b>null</b> or contains whitespace characters.</exception>
  612. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  613. /// <exception cref="Renci.SshNet.Common.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>
  614. /// <exception cref="T:Renci.SshNet.Common.SshException">A SSH error where <see cref="P:System.Exception.Message" /> is the message from the remote host.</exception>
  615. /// <remarks>
  616. /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in exceptions thrown by the stream.
  617. /// </remarks>
  618. public IAsyncResult BeginUploadFile(Stream input, string path, bool canOverride, AsyncCallback asyncCallback, object state, Action<ulong> uploadCallback = null)
  619. {
  620. if (input == null)
  621. throw new ArgumentNullException("input");
  622. if (path.IsNullOrWhiteSpace())
  623. throw new ArgumentException("path");
  624. var flags = Flags.Write | Flags.Truncate;
  625. if (canOverride)
  626. flags |= Flags.CreateNewOrOpen;
  627. else
  628. flags |= Flags.CreateNew;
  629. var asyncResult = new SftpUploadAsyncResult(asyncCallback, state);
  630. this.ExecuteThread(() =>
  631. {
  632. try
  633. {
  634. this.InternalUploadFile(input, path, flags, asyncResult, (offset) =>
  635. {
  636. asyncResult.Update(offset);
  637. if (uploadCallback != null)
  638. {
  639. uploadCallback(offset);
  640. }
  641. });
  642. asyncResult.SetAsCompleted(null, false);
  643. }
  644. catch (Exception exp)
  645. {
  646. asyncResult.SetAsCompleted(exp, false);
  647. }
  648. });
  649. return asyncResult;
  650. }
  651. /// <summary>
  652. /// Ends an asynchronous uploading the steam into remote file.
  653. /// </summary>
  654. /// <param name="asyncResult">The pending asynchronous SFTP request.</param>
  655. /// <exception cref="System.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.</exception>
  656. /// <exception cref="ArgumentException">The IAsyncResult object (<paramref name="asyncResult" />) did not come from the corresponding async method on this type. <para>-or-</para> EndExecute was called multiple times with the same IAsyncResult.</exception>
  657. public void EndUploadFile(IAsyncResult asyncResult)
  658. {
  659. var ar = asyncResult as SftpUploadAsyncResult;
  660. if (ar == null || ar.EndInvokeCalled)
  661. 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.");
  662. // Wait for operation to complete, then return result or throw exception
  663. ar.EndInvoke();
  664. }
  665. /// <summary>
  666. /// Gets status using statvfs@openssh.com request.
  667. /// </summary>
  668. /// <param name="path">The path.</param>
  669. /// <returns>Reference to <see cref="Renci.SshNet.Sftp.SftpFileSytemInformation"/> object that contains file status information.</returns>
  670. /// <exception cref="System.ArgumentNullException">path</exception>
  671. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  672. /// <exception cref="ArgumentNullException"><paramref name="path" /> is <b>null</b>.</exception>
  673. public SftpFileSytemInformation GetStatus(string path)
  674. {
  675. if (path == null)
  676. throw new ArgumentNullException("path");
  677. var fullPath = this._sftpSession.GetCanonicalPath(path);
  678. return this._sftpSession.RequestStatVfs(fullPath);
  679. }
  680. #region File Methods
  681. /// <summary>
  682. /// Appends lines to a file, and then closes the file.
  683. /// </summary>
  684. /// <param name="path">The file to append the lines to. The file is created if it does not already exist.</param>
  685. /// <param name="contents">The lines to append to the file.</param>
  686. /// <exception cref="ArgumentNullException"><paramref name="path"/> is<b>null</b> <para>-or-</para> <paramref name="contents"/> is <b>null</b>.</exception>
  687. public void AppendAllLines(string path, IEnumerable<string> contents)
  688. {
  689. if (contents == null)
  690. throw new ArgumentNullException("contents");
  691. using (var stream = this.AppendText(path))
  692. {
  693. foreach (var line in contents)
  694. {
  695. stream.WriteLine(line);
  696. }
  697. }
  698. }
  699. /// <summary>
  700. /// Appends lines to a file by using a specified encoding, and then closes the file.
  701. /// </summary>
  702. /// <param name="path">The file to append the lines to. The file is created if it does not already exist.</param>
  703. /// <param name="contents">The lines to append to the file.</param>
  704. /// <param name="encoding">The character encoding to use.</param>
  705. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>. <para>-or-</para> <paramref name="contents"/> is <b>null</b>. <para>-or-</para> <paramref name="encoding"/> is <b>null</b>.</exception>
  706. public void AppendAllLines(string path, IEnumerable<string> contents, Encoding encoding)
  707. {
  708. if (contents == null)
  709. throw new ArgumentNullException("contents");
  710. using (var stream = this.AppendText(path, encoding))
  711. {
  712. foreach (var line in contents)
  713. {
  714. stream.WriteLine(line);
  715. }
  716. }
  717. }
  718. /// <summary>
  719. /// Opens a file, appends the specified string to the file, and then closes the file.
  720. /// If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.
  721. /// </summary>
  722. /// <param name="path">The file to append the specified string to.</param>
  723. /// <param name="contents">The string to append to the file.</param>
  724. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>. <para>-or-</para> <paramref name="contents"/> is <b>null</b>.</exception>
  725. public void AppendAllText(string path, string contents)
  726. {
  727. using (var stream = this.AppendText(path))
  728. {
  729. stream.Write(contents);
  730. }
  731. }
  732. /// <summary>
  733. /// Opens a file, appends the specified string to the file, and then closes the file.
  734. /// If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.
  735. /// </summary>
  736. /// <param name="path">The file to append the specified string to.</param>
  737. /// <param name="contents">The string to append to the file.</param>
  738. /// <param name="encoding">The character encoding to use.</param>
  739. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>. <para>-or-</para> <paramref name="contents"/> is <b>null</b>. <para>-or-</para> <paramref name="encoding"/> is <b>null</b>.</exception>
  740. public void AppendAllText(string path, string contents, Encoding encoding)
  741. {
  742. using (var stream = this.AppendText(path, encoding))
  743. {
  744. stream.Write(contents);
  745. }
  746. }
  747. /// <summary>
  748. /// Creates a <see cref="System.IO.StreamWriter"/> that appends UTF-8 encoded text to an existing file.
  749. /// </summary>
  750. /// <param name="path">The path to the file to append to.</param>
  751. /// <returns>A StreamWriter that appends UTF-8 encoded text to an existing file.</returns>
  752. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  753. public StreamWriter AppendText(string path)
  754. {
  755. return this.AppendText(path, Encoding.UTF8);
  756. }
  757. /// <summary>
  758. /// Creates a <see cref="System.IO.StreamWriter"/> that appends UTF-8 encoded text to an existing file.
  759. /// </summary>
  760. /// <param name="path">The path to the file to append to.</param>
  761. /// <param name="encoding">The character encoding to use.</param>
  762. /// <returns>
  763. /// A StreamWriter that appends UTF-8 encoded text to an existing file.
  764. /// </returns>
  765. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>. <para>-or-</para> <paramref name="encoding"/> is <b>null</b>.</exception>
  766. public StreamWriter AppendText(string path, Encoding encoding)
  767. {
  768. if (encoding == null)
  769. throw new ArgumentNullException("encoding");
  770. return new StreamWriter(new SftpFileStream(this._sftpSession, path, FileMode.Append, FileAccess.Write), encoding);
  771. }
  772. /// <summary>
  773. /// Creates or overwrites a file in the specified path.
  774. /// </summary>
  775. /// <param name="path">The path and name of the file to create.</param>
  776. /// <returns>A <see cref="SftpFileStream"/> that provides read/write access to the file specified in path</returns>
  777. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  778. public SftpFileStream Create(string path)
  779. {
  780. return new SftpFileStream(this._sftpSession, path, FileMode.Create, FileAccess.ReadWrite);
  781. }
  782. /// <summary>
  783. /// Creates or overwrites the specified file.
  784. /// </summary>
  785. /// <param name="path">The path and name of the file to create.</param>
  786. /// <param name="bufferSize">The number of bytes buffered for reads and writes to the file.</param>
  787. /// <returns>A <see cref="SftpFileStream"/> that provides read/write access to the file specified in path</returns>
  788. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  789. public SftpFileStream Create(string path, int bufferSize)
  790. {
  791. return new SftpFileStream(this._sftpSession, path, FileMode.Create, FileAccess.ReadWrite, bufferSize);
  792. }
  793. /// <summary>
  794. /// Creates or opens a file for writing UTF-8 encoded text.
  795. /// </summary>
  796. /// <param name="path">The file to be opened for writing.</param>
  797. /// <returns>A <see cref="System.IO.StreamWriter"/> that writes to the specified file using UTF-8 encoding.</returns>
  798. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  799. public StreamWriter CreateText(string path)
  800. {
  801. return new StreamWriter(this.OpenWrite(path), Encoding.UTF8);
  802. }
  803. /// <summary>
  804. /// Creates or opens a file for writing UTF-8 encoded text.
  805. /// </summary>
  806. /// <param name="path">The file to be opened for writing.</param>
  807. /// <param name="encoding">The character encoding to use.</param>
  808. /// <returns> A <see cref="System.IO.StreamWriter"/> that writes to the specified file using UTF-8 encoding. </returns>
  809. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  810. public StreamWriter CreateText(string path, Encoding encoding)
  811. {
  812. return new StreamWriter(this.OpenWrite(path), encoding);
  813. }
  814. /// <summary>
  815. /// Deletes the specified file or directory. An exception is not thrown if the specified file does not exist.
  816. /// </summary>
  817. /// <param name="path">The name of the file or directory to be deleted. Wildcard characters are not supported.</param>
  818. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  819. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  820. public void Delete(string path)
  821. {
  822. var file = this.Get(path);
  823. file.Delete();
  824. }
  825. /// <summary>
  826. /// Returns the date and time the specified file or directory was last accessed.
  827. /// </summary>
  828. /// <param name="path">The file or directory for which to obtain access date and time information.</param>
  829. /// <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>
  830. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  831. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  832. public DateTime GetLastAccessTime(string path)
  833. {
  834. var file = this.Get(path);
  835. return file.LastAccessTime;
  836. }
  837. /// <summary>
  838. /// Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last accessed.
  839. /// </summary>
  840. /// <param name="path">The file or directory for which to obtain access date and time information.</param>
  841. /// <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>
  842. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  843. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  844. public DateTime GetLastAccessTimeUtc(string path)
  845. {
  846. var file = this.Get(path);
  847. return file.LastAccessTime.ToUniversalTime();
  848. }
  849. /// <summary>
  850. /// Returns the date and time the specified file or directory was last written to.
  851. /// </summary>
  852. /// <param name="path">The file or directory for which to obtain write date and time information.</param>
  853. /// <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>
  854. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  855. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  856. public DateTime GetLastWriteTime(string path)
  857. {
  858. var file = this.Get(path);
  859. return file.LastWriteTime;
  860. }
  861. /// <summary>
  862. /// Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last written to.
  863. /// </summary>
  864. /// <param name="path">The file or directory for which to obtain write date and time information.</param>
  865. /// <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>
  866. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  867. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  868. public DateTime GetLastWriteTimeUtc(string path)
  869. {
  870. var file = this.Get(path);
  871. return file.LastWriteTime.ToUniversalTime();
  872. }
  873. /// <summary>
  874. /// Opens a <see cref="SftpFileStream"/> on the specified path with read/write access.
  875. /// </summary>
  876. /// <param name="path">The file to open.</param>
  877. /// <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>
  878. /// <returns>An unshared <see cref="SftpFileStream"/> that provides access to the specified file, with the specified mode and access.</returns>
  879. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  880. public SftpFileStream Open(string path, FileMode mode)
  881. {
  882. return new SftpFileStream(this._sftpSession, path, mode, FileAccess.ReadWrite);
  883. }
  884. /// <summary>
  885. /// Opens a <see cref="SftpFileStream"/> on the specified path, with the specified mode and access.
  886. /// </summary>
  887. /// <param name="path">The file to open.</param>
  888. /// <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>
  889. /// <param name="access">A <see cref="System.IO.FileAccess"/> value that specifies the operations that can be performed on the file.</param>
  890. /// <returns>An unshared <see cref="SftpFileStream"/> that provides access to the specified file, with the specified mode and access.</returns>
  891. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  892. public SftpFileStream Open(string path, FileMode mode, FileAccess access)
  893. {
  894. return new SftpFileStream(this._sftpSession, path, mode, access);
  895. }
  896. /// <summary>
  897. /// Opens an existing file for reading.
  898. /// </summary>
  899. /// <param name="path">The file to be opened for reading.</param>
  900. /// <returns>A read-only System.IO.FileStream on the specified path.</returns>
  901. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  902. public SftpFileStream OpenRead(string path)
  903. {
  904. return new SftpFileStream(this._sftpSession, path, FileMode.Open, FileAccess.Read);
  905. }
  906. /// <summary>
  907. /// Opens an existing UTF-8 encoded text file for reading.
  908. /// </summary>
  909. /// <param name="path">The file to be opened for reading.</param>
  910. /// <returns>A <see cref="System.IO.StreamReader"/> on the specified path.</returns>
  911. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  912. public StreamReader OpenText(string path)
  913. {
  914. return new StreamReader(this.OpenRead(path), Encoding.UTF8);
  915. }
  916. /// <summary>
  917. /// Opens an existing file for writing.
  918. /// </summary>
  919. /// <param name="path">The file to be opened for writing.</param>
  920. /// <returns>An unshared <see cref="SftpFileStream"/> object on the specified path with <see cref="System.IO.FileAccess.Write"/> access.</returns>
  921. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  922. public SftpFileStream OpenWrite(string path)
  923. {
  924. return new SftpFileStream(this._sftpSession, path, FileMode.OpenOrCreate, FileAccess.Write);
  925. }
  926. /// <summary>
  927. /// Opens a binary file, reads the contents of the file into a byte array, and then closes the file.
  928. /// </summary>
  929. /// <param name="path">The file to open for reading.</param>
  930. /// <returns>A byte array containing the contents of the file.</returns>
  931. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  932. public byte[] ReadAllBytes(string path)
  933. {
  934. using (var stream = this.OpenRead(path))
  935. {
  936. var buffer = new byte[stream.Length];
  937. stream.Read(buffer, 0, buffer.Length);
  938. return buffer;
  939. }
  940. }
  941. /// <summary>
  942. /// Opens a text file, reads all lines of the file, and then closes the file.
  943. /// </summary>
  944. /// <param name="path">The file to open for reading.</param>
  945. /// <returns>A string array containing all lines of the file.</returns>
  946. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  947. public string[] ReadAllLines(string path)
  948. {
  949. return this.ReadAllLines(path, Encoding.UTF8);
  950. }
  951. /// <summary>
  952. /// Opens a file, reads all lines of the file with the specified encoding, and then closes the file.
  953. /// </summary>
  954. /// <param name="path">The file to open for reading.</param>
  955. /// <param name="encoding">The encoding applied to the contents of the file.</param>
  956. /// <returns>A string array containing all lines of the file.</returns>
  957. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  958. public string[] ReadAllLines(string path, Encoding encoding)
  959. {
  960. var lines = new List<string>();
  961. using (var stream = new StreamReader(this.OpenRead(path), encoding))
  962. {
  963. while (!stream.EndOfStream)
  964. {
  965. lines.Add(stream.ReadLine());
  966. }
  967. }
  968. return lines.ToArray();
  969. }
  970. /// <summary>
  971. /// Opens a text file, reads all lines of the file, and then closes the file.
  972. /// </summary>
  973. /// <param name="path">The file to open for reading.</param>
  974. /// <returns>A string containing all lines of the file.</returns>
  975. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  976. public string ReadAllText(string path)
  977. {
  978. return this.ReadAllText(path, Encoding.UTF8);
  979. }
  980. /// <summary>
  981. /// Opens a file, reads all lines of the file with the specified encoding, and then closes the file.
  982. /// </summary>
  983. /// <param name="path">The file to open for reading.</param>
  984. /// <param name="encoding">The encoding applied to the contents of the file.</param>
  985. /// <returns>A string containing all lines of the file.</returns>
  986. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  987. public string ReadAllText(string path, Encoding encoding)
  988. {
  989. var lines = new List<string>();
  990. using (var stream = new StreamReader(this.OpenRead(path), encoding))
  991. {
  992. return stream.ReadToEnd();
  993. }
  994. }
  995. /// <summary>
  996. /// Reads the lines of a file.
  997. /// </summary>
  998. /// <param name="path">The file to read.</param>
  999. /// <returns>The lines of the file.</returns>
  1000. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  1001. public IEnumerable<string> ReadLines(string path)
  1002. {
  1003. return this.ReadAllLines(path);
  1004. }
  1005. /// <summary>
  1006. /// Read the lines of a file that has a specified encoding.
  1007. /// </summary>
  1008. /// <param name="path">The file to read.</param>
  1009. /// <param name="encoding">The encoding that is applied to the contents of the file.</param>
  1010. /// <returns>The lines of the file.</returns>
  1011. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  1012. public IEnumerable<string> ReadLines(string path, Encoding encoding)
  1013. {
  1014. return this.ReadAllLines(path, encoding);
  1015. }
  1016. /// <summary>
  1017. /// Sets the date and time the specified file was last accessed.
  1018. /// </summary>
  1019. /// <param name="path">The file for which to set the access date and time information.</param>
  1020. /// <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>
  1021. [Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")]
  1022. public void SetLastAccessTime(string path, DateTime lastAccessTime)
  1023. {
  1024. throw new NotImplementedException();
  1025. }
  1026. /// <summary>
  1027. /// Sets the date and time, in coordinated universal time (UTC), that the specified file was last accessed.
  1028. /// </summary>
  1029. /// <param name="path">The file for which to set the access date and time information.</param>
  1030. /// <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>
  1031. [Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")]
  1032. public void SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc)
  1033. {
  1034. throw new NotImplementedException();
  1035. }
  1036. /// <summary>
  1037. /// Sets the date and time that the specified file was last written to.
  1038. /// </summary>
  1039. /// <param name="path">The file for which to set the date and time information.</param>
  1040. /// <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>
  1041. [Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")]
  1042. public void SetLastWriteTime(string path, DateTime lastWriteTime)
  1043. {
  1044. throw new NotImplementedException();
  1045. }
  1046. /// <summary>
  1047. /// Sets the date and time, in coordinated universal time (UTC), that the specified file was last written to.
  1048. /// </summary>
  1049. /// <param name="path">The file for which to set the date and time information.</param>
  1050. /// <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>
  1051. [Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")]
  1052. public void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc)
  1053. {
  1054. throw new NotImplementedException();
  1055. }
  1056. /// <summary>
  1057. /// 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.
  1058. /// </summary>
  1059. /// <param name="path">The file to write to.</param>
  1060. /// <param name="bytes">The bytes to write to the file.</param>
  1061. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  1062. public void WriteAllBytes(string path, byte[] bytes)
  1063. {
  1064. using (var stream = this.OpenWrite(path))
  1065. {
  1066. stream.Write(bytes, 0, bytes.Length);
  1067. }
  1068. }
  1069. /// <summary>
  1070. /// Creates a new file, writes a collection of strings to the file, and then closes the file.
  1071. /// </summary>
  1072. /// <param name="path">The file to write to.</param>
  1073. /// <param name="contents">The lines to write to the file.</param>
  1074. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  1075. public void WriteAllLines(string path, IEnumerable<string> contents)
  1076. {
  1077. this.WriteAllLines(path, contents, Encoding.UTF8);
  1078. }
  1079. /// <summary>
  1080. /// Creates a new file, write the specified string array to the file, and then closes the file.
  1081. /// </summary>
  1082. /// <param name="path">The file to write to.</param>
  1083. /// <param name="contents">The string array to write to the file.</param>
  1084. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  1085. public void WriteAllLines(string path, string[] contents)
  1086. {
  1087. this.WriteAllLines(path, contents, Encoding.UTF8);
  1088. }
  1089. /// <summary>
  1090. /// Creates a new file by using the specified encoding, writes a collection of strings to the file, and then closes the file.
  1091. /// </summary>
  1092. /// <param name="path">The file to write to.</param>
  1093. /// <param name="contents">The lines to write to the file.</param>
  1094. /// <param name="encoding">The character encoding to use.</param>
  1095. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  1096. public void WriteAllLines(string path, IEnumerable<string> contents, Encoding encoding)
  1097. {
  1098. using (var stream = this.CreateText(path, encoding))
  1099. {
  1100. foreach (var line in contents)
  1101. {
  1102. stream.WriteLine(line);
  1103. }
  1104. }
  1105. }
  1106. /// <summary>
  1107. /// Creates a new file, writes the specified string array to the file by using the specified encoding, and then closes the file.
  1108. /// </summary>
  1109. /// <param name="path">The file to write to.</param>
  1110. /// <param name="contents">The string array to write to the file.</param>
  1111. /// <param name="encoding">An <see cref="System.Text.Encoding"/> object that represents the character encoding applied to the string array.</param>
  1112. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  1113. public void WriteAllLines(string path, string[] contents, Encoding encoding)
  1114. {
  1115. using (var stream = this.CreateText(path, encoding))
  1116. {
  1117. foreach (var line in contents)
  1118. {
  1119. stream.WriteLine(line);
  1120. }
  1121. }
  1122. }
  1123. /// <summary>
  1124. /// 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.
  1125. /// </summary>
  1126. /// <param name="path">The file to write to.</param>
  1127. /// <param name="contents">The string to write to the file.</param>
  1128. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  1129. public void WriteAllText(string path, string contents)
  1130. {
  1131. using (var stream = this.CreateText(path))
  1132. {
  1133. stream.Write(contents);
  1134. }
  1135. }
  1136. /// <summary>
  1137. /// 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.
  1138. /// </summary>
  1139. /// <param name="path">The file to write to.</param>
  1140. /// <param name="contents">The string to write to the file.</param>
  1141. /// <param name="encoding">The encoding to apply to the string.</param>
  1142. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  1143. public void WriteAllText(string path, string contents, Encoding encoding)
  1144. {
  1145. using (var stream = this.CreateText(path, encoding))
  1146. {
  1147. stream.Write(contents);
  1148. }
  1149. }
  1150. /// <summary>
  1151. /// Gets the <see cref="SftpFileAttributes"/> of the file on the path.
  1152. /// </summary>
  1153. /// <param name="path">The path to the file.</param>
  1154. /// <returns>The <see cref="SftpFileAttributes"/> of the file on the path.</returns>
  1155. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  1156. public SftpFileAttributes GetAttributes(string path)
  1157. {
  1158. var fullPath = this._sftpSession.GetCanonicalPath(path);
  1159. return this._sftpSession.RequestLStat(fullPath);
  1160. }
  1161. /// <summary>
  1162. /// Sets the specified <see cref="SftpFileAttributes"/> of the file on the specified path.
  1163. /// </summary>
  1164. /// <param name="path">The path to the file.</param>
  1165. /// <param name="fileAttributes">The desired <see cref="SftpFileAttributes"/>.</param>
  1166. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  1167. public void SetAttributes(string path, SftpFileAttributes fileAttributes)
  1168. {
  1169. var fullPath = this._sftpSession.GetCanonicalPath(path);
  1170. this._sftpSession.RequestSetStat(fullPath, fileAttributes);
  1171. }
  1172. // Please don't forget this when you implement these methods: <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
  1173. //public FileSecurity GetAccessControl(string path);
  1174. //public FileSecurity GetAccessControl(string path, AccessControlSections includeSections);
  1175. //public DateTime GetCreationTime(string path);
  1176. //public DateTime GetCreationTimeUtc(string path);
  1177. //public void SetAccessControl(string path, FileSecurity fileSecurity);
  1178. //public void SetCreationTime(string path, DateTime creationTime);
  1179. //public void SetCreationTimeUtc(string path, DateTime creationTimeUtc);
  1180. #endregion
  1181. /// <summary>
  1182. /// Internals the list directory.
  1183. /// </summary>
  1184. /// <param name="path">The path.</param>
  1185. /// <param name="listCallback">The list callback.</param>
  1186. /// <returns></returns>
  1187. /// <exception cref="System.ArgumentNullException">path</exception>
  1188. /// <exception cref="ArgumentNullException"><paramref name="path" /> is <b>null</b>.</exception>
  1189. /// <exception cref="SshConnectionException">Client not connected.</exception>
  1190. private IEnumerable<SftpFile> InternalListDirectory(string path, Action<int> listCallback)
  1191. {
  1192. if (path == null)
  1193. throw new ArgumentNullException("path");
  1194. var fullPath = this._sftpSession.GetCanonicalPath(path);
  1195. var handle = this._sftpSession.RequestOpenDir(fullPath);
  1196. var basePath = fullPath;
  1197. if (!basePath.EndsWith("/"))
  1198. basePath = string.Format("{0}/", fullPath);
  1199. var result = new List<SftpFile>();
  1200. var files = this._sftpSession.RequestReadDir(handle);
  1201. while (files != null)
  1202. {
  1203. result.AddRange(from f in files
  1204. select new SftpFile(this._sftpSession, string.Format(CultureInfo.InvariantCulture, "{0}{1}", basePath, f.Key), f.Value));
  1205. // Call callback to report number of files read
  1206. if (listCallback != null)
  1207. {
  1208. // Execute callback on different thread
  1209. this.ExecuteThread(() => { listCallback(result.Count); });
  1210. }
  1211. files = this._sftpSession.RequestReadDir(handle);
  1212. }
  1213. this._sftpSession.RequestClose(handle);
  1214. return result;
  1215. }
  1216. /// <summary>
  1217. /// Internals the download file.
  1218. /// </summary>
  1219. /// <param name="path">The path.</param>
  1220. /// <param name="output">The output.</param>
  1221. /// <param name="downloadCallback">The download callback.</param>
  1222. /// <exception cref="System.ArgumentNullException">output</exception>
  1223. /// <exception cref="System.ArgumentException">path</exception>
  1224. /// <exception cref="ArgumentNullException"><paramref name="path" /> is <b>null</b> or contains whitespace.</exception>
  1225. /// <exception cref="ArgumentException"><paramref name="output" /> is <b>null</b>.</exception>
  1226. /// <exception cref="SshConnectionException">Client not connected.</exception>
  1227. private void InternalDownloadFile(string path, Stream output, SftpDownloadAsyncResult asyncResult, Action<ulong> downloadCallback)
  1228. {
  1229. if (output == null)
  1230. throw new ArgumentNullException("output");
  1231. if (path.IsNullOrWhiteSpace())
  1232. throw new ArgumentException("path");
  1233. var fullPath = this._sftpSession.GetCanonicalPath(path);
  1234. var handle = this._sftpSession.RequestOpen(fullPath, Flags.Read);
  1235. ulong offset = 0;
  1236. var data = this._sftpSession.RequestRead(handle, offset, this.BufferSize);
  1237. // Read data while available
  1238. while (data.Length > 0)
  1239. {
  1240. // Cancel download
  1241. if (asyncResult != null && asyncResult.IsDownloadCanceled)
  1242. break;
  1243. output.Write(data, 0, data.Length);
  1244. output.Flush();
  1245. offset += (ulong)data.Length;
  1246. // Call callback to report number of bytes read
  1247. if (downloadCallback != null)
  1248. {
  1249. // Execute callback on different thread
  1250. this.ExecuteThread(() => { downloadCallback(offset); });
  1251. }
  1252. data = this._sftpSession.RequestRead(handle, offset, this.BufferSize);
  1253. }
  1254. this._sftpSession.RequestClose(handle);
  1255. }
  1256. /// <summary>
  1257. /// Internals the upload file.
  1258. /// </summary>
  1259. /// <param name="input">The input.</param>
  1260. /// <param name="path">The path.</param>
  1261. /// <param name="flags">The flags.</param>
  1262. /// <param name="uploadCallback">The upload callback.</param>
  1263. /// <exception cref="System.ArgumentNullException">input</exception>
  1264. /// <exception cref="System.ArgumentException">path</exception>
  1265. /// <exception cref="ArgumentNullException"><paramref name="input" /> is <b>null</b>.</exception>
  1266. /// <exception cref="ArgumentException"><paramref name="path" /> is <b>null</b> or contains whitespace.</exception>
  1267. /// <exception cref="SshConnectionException">Client not connected.</exception>
  1268. private void InternalUploadFile(Stream input, string path, Flags flags, SftpUploadAsyncResult asyncResult, Action<ulong> uploadCallback)
  1269. {
  1270. if (input == null)
  1271. throw new ArgumentNullException("input");
  1272. if (path.IsNullOrWhiteSpace())
  1273. throw new ArgumentException("path");
  1274. var fullPath = this._sftpSession.GetCanonicalPath(path);
  1275. var handle = this._sftpSession.RequestOpen(fullPath, flags);
  1276. ulong offset = 0;
  1277. var buffer = new byte[this.BufferSize];
  1278. var bytesRead = input.Read(buffer, 0, buffer.Length);
  1279. var expectedResponses = 0;
  1280. var expectedResponsesLock = new object();
  1281. do
  1282. {
  1283. // Cancel upload
  1284. if (asyncResult != null && asyncResult.IsUploadCanceled)
  1285. break;
  1286. if (bytesRead > 0)
  1287. {
  1288. if (bytesRead < this.BufferSize)
  1289. {
  1290. // Replace buffer for last chunk of data
  1291. var data = new byte[bytesRead];
  1292. Buffer.BlockCopy(buffer, 0, data, 0, bytesRead);
  1293. buffer = data;
  1294. }
  1295. var writtenBytes = offset + (ulong)buffer.Length;
  1296. this._sftpSession.RequestWrite(handle, offset, buffer, null, (s) =>
  1297. {
  1298. if (s.StatusCode == StatusCodes.Ok)
  1299. {
  1300. lock (expectedResponsesLock)
  1301. {
  1302. expectedResponses--;
  1303. // Call callback to report number of bytes written
  1304. if (uploadCallback != null)
  1305. {
  1306. // Execute callback on different thread
  1307. this.ExecuteThread(() => { uploadCallback(writtenBytes); });
  1308. }
  1309. Monitor.Pulse(expectedResponsesLock);
  1310. }
  1311. }
  1312. });
  1313. lock (expectedResponsesLock)
  1314. {
  1315. expectedResponses++;
  1316. }
  1317. offset += (uint)bytesRead;
  1318. bytesRead = input.Read(buffer, 0, buffer.Length);
  1319. }
  1320. else if (expectedResponses > 0)
  1321. {
  1322. // Wait for expectedResponses to change
  1323. lock (expectedResponsesLock)
  1324. {
  1325. Monitor.Wait(expectedResponsesLock);
  1326. }
  1327. }
  1328. } while (expectedResponses > 0);
  1329. this._sftpSession.RequestClose(handle);
  1330. }
  1331. partial void ExecuteThread(Action action);
  1332. /// <summary>
  1333. /// Called when client is connected to the server.
  1334. /// </summary>
  1335. protected override void OnConnected()
  1336. {
  1337. base.OnConnected();
  1338. this._sftpSession = new SftpSession(this.Session, this.OperationTimeout, this.ConnectionInfo.Encoding);
  1339. this._sftpSession.Connect();
  1340. // Resolve current running version
  1341. this.ProtocolVersion = (int)this._sftpSession.ProtocolVersion;
  1342. }
  1343. /// <summary>
  1344. /// Called when client is disconnecting from the server.
  1345. /// </summary>
  1346. protected override void OnDisconnecting()
  1347. {
  1348. base.OnDisconnecting();
  1349. this._sftpSession.Disconnect();
  1350. }
  1351. /// <summary>
  1352. /// Releases unmanaged and - optionally - managed resources
  1353. /// </summary>
  1354. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
  1355. protected override void Dispose(bool disposing)
  1356. {
  1357. if (this._sftpSession != null)
  1358. {
  1359. this._sftpSession.Dispose();
  1360. this._sftpSession = null;
  1361. }
  1362. if (this._disposeConnectionInfo)
  1363. ((IDisposable)this.ConnectionInfo).Dispose();
  1364. base.Dispose(disposing);
  1365. }
  1366. }
  1367. }