SftpClient.cs 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  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. /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>
  389. /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests.</param>
  390. /// <param name="downloadCallback">The download callback.</param>
  391. /// <returns>
  392. /// An <see cref="IAsyncResult" /> that references the asynchronous operation.
  393. /// </returns>
  394. /// <exception cref="System.ArgumentException">path</exception>
  395. /// <exception cref="System.ArgumentNullException">output</exception>
  396. /// <exception cref="ArgumentNullException"><paramref name="output" /> is <b>null</b>.</exception>
  397. /// <exception cref="ArgumentException"><paramref name="path" /> is <b>null</b> or contains whitespace characters.</exception>
  398. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  399. /// <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>
  400. /// <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>
  401. /// <remarks>
  402. /// Method calls made by this method to <paramref name="output" />, may under certain conditions result in exceptions thrown by the stream.
  403. /// </remarks>
  404. public IAsyncResult BeginDownloadFile(string path, Stream output, AsyncCallback asyncCallback, object state, Action<ulong> downloadCallback = null)
  405. {
  406. if (path.IsNullOrWhiteSpace())
  407. throw new ArgumentException("path");
  408. if (output == null)
  409. throw new ArgumentNullException("output");
  410. var asyncResult = new SftpDownloadAsyncResult(asyncCallback, state);
  411. this.ExecuteThread(() =>
  412. {
  413. try
  414. {
  415. this.InternalDownloadFile(path, output, asyncResult, (offset) =>
  416. {
  417. asyncResult.Update(offset);
  418. if (downloadCallback != null)
  419. {
  420. downloadCallback(offset);
  421. }
  422. });
  423. asyncResult.SetAsCompleted(null, false);
  424. }
  425. catch (Exception exp)
  426. {
  427. asyncResult.SetAsCompleted(exp, false);
  428. }
  429. });
  430. return asyncResult;
  431. }
  432. /// <summary>
  433. /// Ends an asynchronous file downloading into the stream.
  434. /// </summary>
  435. /// <param name="asyncResult">The pending asynchronous SFTP request.</param>
  436. /// <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>
  437. public void EndDownloadFile(IAsyncResult asyncResult)
  438. {
  439. var ar = asyncResult as SftpDownloadAsyncResult;
  440. if (ar == null || ar.EndInvokeCalled)
  441. 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.");
  442. // Wait for operation to complete, then return result or throw exception
  443. ar.EndInvoke();
  444. }
  445. /// <summary>
  446. /// Uploads stream into remote file..
  447. /// </summary>
  448. /// <param name="input">Data input stream.</param>
  449. /// <param name="path">Remote file path.</param>
  450. /// <param name="uploadCallback">The upload callback.</param>
  451. /// <exception cref="ArgumentNullException"><paramref name="input" /> is <b>null</b>.</exception>
  452. /// <exception cref="ArgumentException"><paramref name="path" /> is <b>null</b> or contains whitespace characters.</exception>
  453. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  454. /// <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>
  455. /// <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>
  456. /// <remarks>
  457. /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in exceptions thrown by the stream.
  458. /// </remarks>
  459. public void UploadFile(Stream input, string path, Action<ulong> uploadCallback = null)
  460. {
  461. this.UploadFile(input, path, true, uploadCallback);
  462. }
  463. /// <summary>
  464. /// Uploads stream into remote file..
  465. /// </summary>
  466. /// <param name="input">Data input stream.</param>
  467. /// <param name="path">Remote file path.</param>
  468. /// <param name="canOverride">if set to <c>true</c> then existing file will be overwritten.</param>
  469. /// <param name="uploadCallback">The upload callback.</param>
  470. /// <exception cref="ArgumentNullException"><paramref name="input" /> is <b>null</b>.</exception>
  471. /// <exception cref="ArgumentException"><paramref name="path" /> is <b>null</b> or contains whitespace characters.</exception>
  472. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  473. /// <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>
  474. /// <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>
  475. /// <remarks>
  476. /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in exceptions thrown by the stream.
  477. /// </remarks>
  478. public void UploadFile(Stream input, string path, bool canOverride, Action<ulong> uploadCallback = null)
  479. {
  480. var flags = Flags.Write | Flags.Truncate;
  481. if (canOverride)
  482. flags |= Flags.CreateNewOrOpen;
  483. else
  484. flags |= Flags.CreateNew;
  485. this.InternalUploadFile(input, path, flags, null, uploadCallback);
  486. }
  487. /// <summary>
  488. /// Begins an asynchronous uploading the steam into remote file.
  489. /// </summary>
  490. /// <param name="input">Data input stream.</param>
  491. /// <param name="path">Remote file path.</param>
  492. /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>
  493. /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests.</param>
  494. /// <param name="uploadCallback">The upload callback.</param>
  495. /// <returns>
  496. /// An <see cref="IAsyncResult" /> that references the asynchronous operation.
  497. /// </returns>
  498. /// <exception cref="ArgumentNullException"><paramref name="input" /> is <b>null</b>.</exception>
  499. /// <exception cref="ArgumentException"><paramref name="path" /> is <b>null</b> or contains whitespace characters.</exception>
  500. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  501. /// <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>
  502. /// <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>
  503. /// <remarks>
  504. /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in exceptions thrown by the stream.
  505. /// </remarks>
  506. public IAsyncResult BeginUploadFile(Stream input, string path, AsyncCallback asyncCallback, object state, Action<ulong> uploadCallback = null)
  507. {
  508. return this.BeginUploadFile(input, path, true, asyncCallback, state, uploadCallback);
  509. }
  510. /// <summary>
  511. /// Begins an asynchronous uploading the steam into remote file.
  512. /// </summary>
  513. /// <param name="input">Data input stream.</param>
  514. /// <param name="path">Remote file path.</param>
  515. /// <param name="canOverride">if set to <c>true</c> then existing file will be overwritten.</param>
  516. /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param>
  517. /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests.</param>
  518. /// <param name="uploadCallback">The upload callback.</param>
  519. /// <returns>
  520. /// An <see cref="IAsyncResult" /> that references the asynchronous operation.
  521. /// </returns>
  522. /// <exception cref="System.ArgumentNullException">input</exception>
  523. /// <exception cref="System.ArgumentException">path</exception>
  524. /// <exception cref="ArgumentNullException"><paramref name="input" /> is <b>null</b>.</exception>
  525. /// <exception cref="ArgumentException"><paramref name="path" /> is <b>null</b> or contains whitespace characters.</exception>
  526. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  527. /// <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>
  528. /// <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>
  529. /// <remarks>
  530. /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in exceptions thrown by the stream.
  531. /// </remarks>
  532. public IAsyncResult BeginUploadFile(Stream input, string path, bool canOverride, AsyncCallback asyncCallback, object state, Action<ulong> uploadCallback = null)
  533. {
  534. if (input == null)
  535. throw new ArgumentNullException("input");
  536. if (path.IsNullOrWhiteSpace())
  537. throw new ArgumentException("path");
  538. var flags = Flags.Write | Flags.Truncate;
  539. if (canOverride)
  540. flags |= Flags.CreateNewOrOpen;
  541. else
  542. flags |= Flags.CreateNew;
  543. var asyncResult = new SftpUploadAsyncResult(asyncCallback, state);
  544. this.ExecuteThread(() =>
  545. {
  546. try
  547. {
  548. this.InternalUploadFile(input, path, flags, asyncResult, (offset) =>
  549. {
  550. asyncResult.Update(offset);
  551. if (uploadCallback != null)
  552. {
  553. uploadCallback(offset);
  554. }
  555. });
  556. asyncResult.SetAsCompleted(null, false);
  557. }
  558. catch (Exception exp)
  559. {
  560. asyncResult.SetAsCompleted(exp, false);
  561. }
  562. });
  563. return asyncResult;
  564. }
  565. /// <summary>
  566. /// Ends an asynchronous uploading the steam into remote file.
  567. /// </summary>
  568. /// <param name="asyncResult">The pending asynchronous SFTP request.</param>
  569. /// <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>
  570. /// <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>
  571. public void EndUploadFile(IAsyncResult asyncResult)
  572. {
  573. var ar = asyncResult as SftpUploadAsyncResult;
  574. if (ar == null || ar.EndInvokeCalled)
  575. 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.");
  576. // Wait for operation to complete, then return result or throw exception
  577. ar.EndInvoke();
  578. }
  579. /// <summary>
  580. /// Gets status using statvfs@openssh.com request.
  581. /// </summary>
  582. /// <param name="path">The path.</param>
  583. /// <returns>Reference to <see cref="Renci.SshNet.Sftp.SftpFileSytemInformation"/> object that contains file status information.</returns>
  584. /// <exception cref="System.ArgumentNullException">path</exception>
  585. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  586. /// <exception cref="ArgumentNullException"><paramref name="path" /> is <b>null</b>.</exception>
  587. public SftpFileSytemInformation GetStatus(string path)
  588. {
  589. if (path == null)
  590. throw new ArgumentNullException("path");
  591. var fullPath = this._sftpSession.GetCanonicalPath(path);
  592. return this._sftpSession.RequestStatVfs(fullPath);
  593. }
  594. #region File Methods
  595. /// <summary>
  596. /// Appends lines to a file, and then closes the file.
  597. /// </summary>
  598. /// <param name="path">The file to append the lines to. The file is created if it does not already exist.</param>
  599. /// <param name="contents">The lines to append to the file.</param>
  600. /// <exception cref="ArgumentNullException"><paramref name="path"/> is<b>null</b> <para>-or-</para> <paramref name="contents"/> is <b>null</b>.</exception>
  601. public void AppendAllLines(string path, IEnumerable<string> contents)
  602. {
  603. if (contents == null)
  604. throw new ArgumentNullException("contents");
  605. using (var stream = this.AppendText(path))
  606. {
  607. foreach (var line in contents)
  608. {
  609. stream.WriteLine(line);
  610. }
  611. }
  612. }
  613. /// <summary>
  614. /// Appends lines to a file by using a specified encoding, and then closes the file.
  615. /// </summary>
  616. /// <param name="path">The file to append the lines to. The file is created if it does not already exist.</param>
  617. /// <param name="contents">The lines to append to the file.</param>
  618. /// <param name="encoding">The character encoding to use.</param>
  619. /// <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>
  620. public void AppendAllLines(string path, IEnumerable<string> contents, Encoding encoding)
  621. {
  622. if (contents == null)
  623. throw new ArgumentNullException("contents");
  624. using (var stream = this.AppendText(path, encoding))
  625. {
  626. foreach (var line in contents)
  627. {
  628. stream.WriteLine(line);
  629. }
  630. }
  631. }
  632. /// <summary>
  633. /// Opens a file, appends the specified string to the file, and then closes the file.
  634. /// If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.
  635. /// </summary>
  636. /// <param name="path">The file to append the specified string to.</param>
  637. /// <param name="contents">The string to append to the file.</param>
  638. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>. <para>-or-</para> <paramref name="contents"/> is <b>null</b>.</exception>
  639. public void AppendAllText(string path, string contents)
  640. {
  641. using (var stream = this.AppendText(path))
  642. {
  643. stream.Write(contents);
  644. }
  645. }
  646. /// <summary>
  647. /// Opens a file, appends the specified string to the file, and then closes the file.
  648. /// If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.
  649. /// </summary>
  650. /// <param name="path">The file to append the specified string to.</param>
  651. /// <param name="contents">The string to append to the file.</param>
  652. /// <param name="encoding">The character encoding to use.</param>
  653. /// <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>
  654. public void AppendAllText(string path, string contents, Encoding encoding)
  655. {
  656. using (var stream = this.AppendText(path, encoding))
  657. {
  658. stream.Write(contents);
  659. }
  660. }
  661. /// <summary>
  662. /// Creates a <see cref="System.IO.StreamWriter"/> that appends UTF-8 encoded text to an existing file.
  663. /// </summary>
  664. /// <param name="path">The path to the file to append to.</param>
  665. /// <returns>A StreamWriter that appends UTF-8 encoded text to an existing file.</returns>
  666. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  667. public StreamWriter AppendText(string path)
  668. {
  669. return this.AppendText(path, Encoding.UTF8);
  670. }
  671. /// <summary>
  672. /// Creates a <see cref="System.IO.StreamWriter"/> that appends UTF-8 encoded text to an existing file.
  673. /// </summary>
  674. /// <param name="path">The path to the file to append to.</param>
  675. /// <param name="encoding">The character encoding to use.</param>
  676. /// <returns>
  677. /// A StreamWriter that appends UTF-8 encoded text to an existing file.
  678. /// </returns>
  679. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>. <para>-or-</para> <paramref name="encoding"/> is <b>null</b>.</exception>
  680. public StreamWriter AppendText(string path, Encoding encoding)
  681. {
  682. if (encoding == null)
  683. throw new ArgumentNullException("encoding");
  684. return new StreamWriter(new SftpFileStream(this._sftpSession, path, FileMode.Append, FileAccess.Write), encoding);
  685. }
  686. /// <summary>
  687. /// Creates or overwrites a file in the specified path.
  688. /// </summary>
  689. /// <param name="path">The path and name of the file to create.</param>
  690. /// <returns>A <see cref="SftpFileStream"/> that provides read/write access to the file specified in path</returns>
  691. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  692. public SftpFileStream Create(string path)
  693. {
  694. return new SftpFileStream(this._sftpSession, path, FileMode.Create, FileAccess.ReadWrite);
  695. }
  696. /// <summary>
  697. /// Creates or overwrites the specified file.
  698. /// </summary>
  699. /// <param name="path">The path and name of the file to create.</param>
  700. /// <param name="bufferSize">The number of bytes buffered for reads and writes to the file.</param>
  701. /// <returns>A <see cref="SftpFileStream"/> that provides read/write access to the file specified in path</returns>
  702. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  703. public SftpFileStream Create(string path, int bufferSize)
  704. {
  705. return new SftpFileStream(this._sftpSession, path, FileMode.Create, FileAccess.ReadWrite, bufferSize);
  706. }
  707. /// <summary>
  708. /// Creates or opens a file for writing UTF-8 encoded text.
  709. /// </summary>
  710. /// <param name="path">The file to be opened for writing.</param>
  711. /// <returns>A <see cref="System.IO.StreamWriter"/> that writes to the specified file using UTF-8 encoding.</returns>
  712. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  713. public StreamWriter CreateText(string path)
  714. {
  715. return new StreamWriter(this.OpenWrite(path), Encoding.UTF8);
  716. }
  717. /// <summary>
  718. /// Creates or opens a file for writing UTF-8 encoded text.
  719. /// </summary>
  720. /// <param name="path">The file to be opened for writing.</param>
  721. /// <param name="encoding">The character encoding to use.</param>
  722. /// <returns> A <see cref="System.IO.StreamWriter"/> that writes to the specified file using UTF-8 encoding. </returns>
  723. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  724. public StreamWriter CreateText(string path, Encoding encoding)
  725. {
  726. return new StreamWriter(this.OpenWrite(path), encoding);
  727. }
  728. /// <summary>
  729. /// Deletes the specified file or directory. An exception is not thrown if the specified file does not exist.
  730. /// </summary>
  731. /// <param name="path">The name of the file or directory to be deleted. Wildcard characters are not supported.</param>
  732. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  733. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  734. public void Delete(string path)
  735. {
  736. var file = this.Get(path);
  737. file.Delete();
  738. }
  739. /// <summary>
  740. /// Returns the date and time the specified file or directory was last accessed.
  741. /// </summary>
  742. /// <param name="path">The file or directory for which to obtain access date and time information.</param>
  743. /// <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>
  744. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  745. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  746. public DateTime GetLastAccessTime(string path)
  747. {
  748. var file = this.Get(path);
  749. return file.LastAccessTime;
  750. }
  751. /// <summary>
  752. /// Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last accessed.
  753. /// </summary>
  754. /// <param name="path">The file or directory for which to obtain access date and time information.</param>
  755. /// <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>
  756. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  757. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  758. public DateTime GetLastAccessTimeUtc(string path)
  759. {
  760. var file = this.Get(path);
  761. return file.LastAccessTime.ToUniversalTime();
  762. }
  763. /// <summary>
  764. /// Returns the date and time the specified file or directory was last written to.
  765. /// </summary>
  766. /// <param name="path">The file or directory for which to obtain write date and time information.</param>
  767. /// <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>
  768. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  769. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  770. public DateTime GetLastWriteTime(string path)
  771. {
  772. var file = this.Get(path);
  773. return file.LastWriteTime;
  774. }
  775. /// <summary>
  776. /// Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last written to.
  777. /// </summary>
  778. /// <param name="path">The file or directory for which to obtain write date and time information.</param>
  779. /// <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>
  780. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  781. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  782. public DateTime GetLastWriteTimeUtc(string path)
  783. {
  784. var file = this.Get(path);
  785. return file.LastWriteTime.ToUniversalTime();
  786. }
  787. /// <summary>
  788. /// Opens a <see cref="SftpFileStream"/> on the specified path with read/write access.
  789. /// </summary>
  790. /// <param name="path">The file to open.</param>
  791. /// <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>
  792. /// <returns>An unshared <see cref="SftpFileStream"/> that provides access to the specified file, with the specified mode and access.</returns>
  793. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  794. public SftpFileStream Open(string path, FileMode mode)
  795. {
  796. return new SftpFileStream(this._sftpSession, path, mode, FileAccess.ReadWrite);
  797. }
  798. /// <summary>
  799. /// Opens a <see cref="SftpFileStream"/> on the specified path, with the specified mode and access.
  800. /// </summary>
  801. /// <param name="path">The file to open.</param>
  802. /// <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>
  803. /// <param name="access">A <see cref="System.IO.FileAccess"/> value that specifies the operations that can be performed on the file.</param>
  804. /// <returns>An unshared <see cref="SftpFileStream"/> that provides access to the specified file, with the specified mode and access.</returns>
  805. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  806. public SftpFileStream Open(string path, FileMode mode, FileAccess access)
  807. {
  808. return new SftpFileStream(this._sftpSession, path, mode, access);
  809. }
  810. /// <summary>
  811. /// Opens an existing file for reading.
  812. /// </summary>
  813. /// <param name="path">The file to be opened for reading.</param>
  814. /// <returns>A read-only System.IO.FileStream on the specified path.</returns>
  815. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  816. public SftpFileStream OpenRead(string path)
  817. {
  818. return new SftpFileStream(this._sftpSession, path, FileMode.Open, FileAccess.Read);
  819. }
  820. /// <summary>
  821. /// Opens an existing UTF-8 encoded text file for reading.
  822. /// </summary>
  823. /// <param name="path">The file to be opened for reading.</param>
  824. /// <returns>A <see cref="System.IO.StreamReader"/> on the specified path.</returns>
  825. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  826. public StreamReader OpenText(string path)
  827. {
  828. return new StreamReader(this.OpenRead(path), Encoding.UTF8);
  829. }
  830. /// <summary>
  831. /// Opens an existing file for writing.
  832. /// </summary>
  833. /// <param name="path">The file to be opened for writing.</param>
  834. /// <returns>An unshared <see cref="SftpFileStream"/> object on the specified path with <see cref="System.IO.FileAccess.Write"/> access.</returns>
  835. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  836. public SftpFileStream OpenWrite(string path)
  837. {
  838. return new SftpFileStream(this._sftpSession, path, FileMode.OpenOrCreate, FileAccess.Write);
  839. }
  840. /// <summary>
  841. /// Opens a binary file, reads the contents of the file into a byte array, and then closes the file.
  842. /// </summary>
  843. /// <param name="path">The file to open for reading.</param>
  844. /// <returns>A byte array containing the contents of the file.</returns>
  845. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  846. public byte[] ReadAllBytes(string path)
  847. {
  848. using (var stream = this.OpenRead(path))
  849. {
  850. var buffer = new byte[stream.Length];
  851. stream.Read(buffer, 0, buffer.Length);
  852. return buffer;
  853. }
  854. }
  855. /// <summary>
  856. /// Opens a text file, reads all lines of the file, and then closes the file.
  857. /// </summary>
  858. /// <param name="path">The file to open for reading.</param>
  859. /// <returns>A string array containing all lines of the file.</returns>
  860. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  861. public string[] ReadAllLines(string path)
  862. {
  863. return this.ReadAllLines(path, Encoding.UTF8);
  864. }
  865. /// <summary>
  866. /// Opens a file, reads all lines of the file with the specified encoding, and then closes the file.
  867. /// </summary>
  868. /// <param name="path">The file to open for reading.</param>
  869. /// <param name="encoding">The encoding applied to the contents of the file.</param>
  870. /// <returns>A string array containing all lines of the file.</returns>
  871. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  872. public string[] ReadAllLines(string path, Encoding encoding)
  873. {
  874. var lines = new List<string>();
  875. using (var stream = new StreamReader(this.OpenRead(path), encoding))
  876. {
  877. while (!stream.EndOfStream)
  878. {
  879. lines.Add(stream.ReadLine());
  880. }
  881. }
  882. return lines.ToArray();
  883. }
  884. /// <summary>
  885. /// Opens a text file, reads all lines of the file, and then closes the file.
  886. /// </summary>
  887. /// <param name="path">The file to open for reading.</param>
  888. /// <returns>A string containing all lines of the file.</returns>
  889. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  890. public string ReadAllText(string path)
  891. {
  892. return this.ReadAllText(path, Encoding.UTF8);
  893. }
  894. /// <summary>
  895. /// Opens a file, reads all lines of the file with the specified encoding, and then closes the file.
  896. /// </summary>
  897. /// <param name="path">The file to open for reading.</param>
  898. /// <param name="encoding">The encoding applied to the contents of the file.</param>
  899. /// <returns>A string containing all lines of the file.</returns>
  900. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  901. public string ReadAllText(string path, Encoding encoding)
  902. {
  903. var lines = new List<string>();
  904. using (var stream = new StreamReader(this.OpenRead(path), encoding))
  905. {
  906. return stream.ReadToEnd();
  907. }
  908. }
  909. /// <summary>
  910. /// Reads the lines of a file.
  911. /// </summary>
  912. /// <param name="path">The file to read.</param>
  913. /// <returns>The lines of the file.</returns>
  914. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  915. public IEnumerable<string> ReadLines(string path)
  916. {
  917. return this.ReadAllLines(path);
  918. }
  919. /// <summary>
  920. /// Read the lines of a file that has a specified encoding.
  921. /// </summary>
  922. /// <param name="path">The file to read.</param>
  923. /// <param name="encoding">The encoding that is applied to the contents of the file.</param>
  924. /// <returns>The lines of the file.</returns>
  925. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  926. public IEnumerable<string> ReadLines(string path, Encoding encoding)
  927. {
  928. return this.ReadAllLines(path, encoding);
  929. }
  930. /// <summary>
  931. /// Sets the date and time the specified file was last accessed.
  932. /// </summary>
  933. /// <param name="path">The file for which to set the access date and time information.</param>
  934. /// <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>
  935. [Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")]
  936. public void SetLastAccessTime(string path, DateTime lastAccessTime)
  937. {
  938. throw new NotImplementedException();
  939. }
  940. /// <summary>
  941. /// Sets the date and time, in coordinated universal time (UTC), that the specified file was last accessed.
  942. /// </summary>
  943. /// <param name="path">The file for which to set the access date and time information.</param>
  944. /// <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>
  945. [Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")]
  946. public void SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc)
  947. {
  948. throw new NotImplementedException();
  949. }
  950. /// <summary>
  951. /// Sets the date and time that the specified file was last written to.
  952. /// </summary>
  953. /// <param name="path">The file for which to set the date and time information.</param>
  954. /// <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>
  955. [Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")]
  956. public void SetLastWriteTime(string path, DateTime lastWriteTime)
  957. {
  958. throw new NotImplementedException();
  959. }
  960. /// <summary>
  961. /// Sets the date and time, in coordinated universal time (UTC), that the specified file was last written to.
  962. /// </summary>
  963. /// <param name="path">The file for which to set the date and time information.</param>
  964. /// <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>
  965. [Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")]
  966. public void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc)
  967. {
  968. throw new NotImplementedException();
  969. }
  970. /// <summary>
  971. /// 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.
  972. /// </summary>
  973. /// <param name="path">The file to write to.</param>
  974. /// <param name="bytes">The bytes to write to the file.</param>
  975. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  976. public void WriteAllBytes(string path, byte[] bytes)
  977. {
  978. using (var stream = this.OpenWrite(path))
  979. {
  980. stream.Write(bytes, 0, bytes.Length);
  981. }
  982. }
  983. /// <summary>
  984. /// Creates a new file, writes a collection of strings to the file, and then closes the file.
  985. /// </summary>
  986. /// <param name="path">The file to write to.</param>
  987. /// <param name="contents">The lines to write to the file.</param>
  988. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  989. public void WriteAllLines(string path, IEnumerable<string> contents)
  990. {
  991. this.WriteAllLines(path, contents, Encoding.UTF8);
  992. }
  993. /// <summary>
  994. /// Creates a new file, write the specified string array to the file, and then closes the file.
  995. /// </summary>
  996. /// <param name="path">The file to write to.</param>
  997. /// <param name="contents">The string array to write to the file.</param>
  998. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  999. public void WriteAllLines(string path, string[] contents)
  1000. {
  1001. this.WriteAllLines(path, contents, Encoding.UTF8);
  1002. }
  1003. /// <summary>
  1004. /// Creates a new file by using the specified encoding, writes a collection of strings to the file, and then closes the file.
  1005. /// </summary>
  1006. /// <param name="path">The file to write to.</param>
  1007. /// <param name="contents">The lines to write to the file.</param>
  1008. /// <param name="encoding">The character encoding to use.</param>
  1009. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  1010. public void WriteAllLines(string path, IEnumerable<string> contents, Encoding encoding)
  1011. {
  1012. using (var stream = this.CreateText(path, encoding))
  1013. {
  1014. foreach (var line in contents)
  1015. {
  1016. stream.WriteLine(line);
  1017. }
  1018. }
  1019. }
  1020. /// <summary>
  1021. /// Creates a new file, writes the specified string array to the file by using the specified encoding, and then closes the file.
  1022. /// </summary>
  1023. /// <param name="path">The file to write to.</param>
  1024. /// <param name="contents">The string array to write to the file.</param>
  1025. /// <param name="encoding">An <see cref="System.Text.Encoding"/> object that represents the character encoding applied to the string array.</param>
  1026. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  1027. public void WriteAllLines(string path, string[] contents, Encoding encoding)
  1028. {
  1029. using (var stream = this.CreateText(path, encoding))
  1030. {
  1031. foreach (var line in contents)
  1032. {
  1033. stream.WriteLine(line);
  1034. }
  1035. }
  1036. }
  1037. /// <summary>
  1038. /// 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.
  1039. /// </summary>
  1040. /// <param name="path">The file to write to.</param>
  1041. /// <param name="contents">The string to write to the file.</param>
  1042. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  1043. public void WriteAllText(string path, string contents)
  1044. {
  1045. using (var stream = this.CreateText(path))
  1046. {
  1047. stream.Write(contents);
  1048. }
  1049. }
  1050. /// <summary>
  1051. /// 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.
  1052. /// </summary>
  1053. /// <param name="path">The file to write to.</param>
  1054. /// <param name="contents">The string to write to the file.</param>
  1055. /// <param name="encoding">The encoding to apply to the string.</param>
  1056. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  1057. public void WriteAllText(string path, string contents, Encoding encoding)
  1058. {
  1059. using (var stream = this.CreateText(path, encoding))
  1060. {
  1061. stream.Write(contents);
  1062. }
  1063. }
  1064. /// <summary>
  1065. /// Gets the <see cref="SftpFileAttributes"/> of the file on the path.
  1066. /// </summary>
  1067. /// <param name="path">The path to the file.</param>
  1068. /// <returns>The <see cref="SftpFileAttributes"/> of the file on the path.</returns>
  1069. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  1070. public SftpFileAttributes GetAttributes(string path)
  1071. {
  1072. var fullPath = this._sftpSession.GetCanonicalPath(path);
  1073. return this._sftpSession.RequestLStat(fullPath);
  1074. }
  1075. /// <summary>
  1076. /// Sets the specified <see cref="SftpFileAttributes"/> of the file on the specified path.
  1077. /// </summary>
  1078. /// <param name="path">The path to the file.</param>
  1079. /// <param name="fileAttributes">The desired <see cref="SftpFileAttributes"/>.</param>
  1080. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
  1081. public void SetAttributes(string path, SftpFileAttributes fileAttributes)
  1082. {
  1083. var fullPath = this._sftpSession.GetCanonicalPath(path);
  1084. this._sftpSession.RequestSetStat(fullPath, fileAttributes);
  1085. }
  1086. // Please don't forget this when you implement these methods: <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
  1087. //public FileSecurity GetAccessControl(string path);
  1088. //public FileSecurity GetAccessControl(string path, AccessControlSections includeSections);
  1089. //public DateTime GetCreationTime(string path);
  1090. //public DateTime GetCreationTimeUtc(string path);
  1091. //public void SetAccessControl(string path, FileSecurity fileSecurity);
  1092. //public void SetCreationTime(string path, DateTime creationTime);
  1093. //public void SetCreationTimeUtc(string path, DateTime creationTimeUtc);
  1094. #endregion
  1095. /// <summary>
  1096. /// Internals the list directory.
  1097. /// </summary>
  1098. /// <param name="path">The path.</param>
  1099. /// <param name="listCallback">The list callback.</param>
  1100. /// <returns></returns>
  1101. /// <exception cref="System.ArgumentNullException">path</exception>
  1102. /// <exception cref="ArgumentNullException"><paramref name="path" /> is <b>null</b>.</exception>
  1103. /// <exception cref="SshConnectionException">Client not connected.</exception>
  1104. private IEnumerable<SftpFile> InternalListDirectory(string path, Action<int> listCallback)
  1105. {
  1106. if (path == null)
  1107. throw new ArgumentNullException("path");
  1108. var fullPath = this._sftpSession.GetCanonicalPath(path);
  1109. var handle = this._sftpSession.RequestOpenDir(fullPath);
  1110. var basePath = fullPath;
  1111. if (!basePath.EndsWith("/"))
  1112. basePath = string.Format("{0}/", fullPath);
  1113. var result = new List<SftpFile>();
  1114. var files = this._sftpSession.RequestReadDir(handle);
  1115. while (files != null)
  1116. {
  1117. result.AddRange(from f in files
  1118. select new SftpFile(this._sftpSession, string.Format(CultureInfo.InvariantCulture, "{0}{1}", basePath, f.Key), f.Value));
  1119. // Call callback to report number of files read
  1120. if (listCallback != null)
  1121. {
  1122. // Execute callback on different thread
  1123. this.ExecuteThread(() => { listCallback(result.Count); });
  1124. }
  1125. files = this._sftpSession.RequestReadDir(handle);
  1126. }
  1127. this._sftpSession.RequestClose(handle);
  1128. return result;
  1129. }
  1130. /// <summary>
  1131. /// Internals the download file.
  1132. /// </summary>
  1133. /// <param name="path">The path.</param>
  1134. /// <param name="output">The output.</param>
  1135. /// <param name="downloadCallback">The download callback.</param>
  1136. /// <exception cref="System.ArgumentNullException">output</exception>
  1137. /// <exception cref="System.ArgumentException">path</exception>
  1138. /// <exception cref="ArgumentNullException"><paramref name="path" /> is <b>null</b> or contains whitespace.</exception>
  1139. /// <exception cref="ArgumentException"><paramref name="output" /> is <b>null</b>.</exception>
  1140. /// <exception cref="SshConnectionException">Client not connected.</exception>
  1141. private void InternalDownloadFile(string path, Stream output, SftpDownloadAsyncResult asyncResult, Action<ulong> downloadCallback)
  1142. {
  1143. if (output == null)
  1144. throw new ArgumentNullException("output");
  1145. if (path.IsNullOrWhiteSpace())
  1146. throw new ArgumentException("path");
  1147. var fullPath = this._sftpSession.GetCanonicalPath(path);
  1148. var handle = this._sftpSession.RequestOpen(fullPath, Flags.Read);
  1149. ulong offset = 0;
  1150. var data = this._sftpSession.RequestRead(handle, offset, this.BufferSize);
  1151. // Read data while available
  1152. while (data.Length > 0)
  1153. {
  1154. // Cancel download
  1155. if (asyncResult != null && asyncResult.IsDownloadCanceled)
  1156. break;
  1157. output.Write(data, 0, data.Length);
  1158. output.Flush();
  1159. offset += (ulong)data.Length;
  1160. // Call callback to report number of bytes read
  1161. if (downloadCallback != null)
  1162. {
  1163. // Execute callback on different thread
  1164. this.ExecuteThread(() => { downloadCallback(offset); });
  1165. }
  1166. data = this._sftpSession.RequestRead(handle, offset, this.BufferSize);
  1167. }
  1168. this._sftpSession.RequestClose(handle);
  1169. }
  1170. /// <summary>
  1171. /// Internals the upload file.
  1172. /// </summary>
  1173. /// <param name="input">The input.</param>
  1174. /// <param name="path">The path.</param>
  1175. /// <param name="flags">The flags.</param>
  1176. /// <param name="uploadCallback">The upload callback.</param>
  1177. /// <exception cref="System.ArgumentNullException">input</exception>
  1178. /// <exception cref="System.ArgumentException">path</exception>
  1179. /// <exception cref="ArgumentNullException"><paramref name="input" /> is <b>null</b>.</exception>
  1180. /// <exception cref="ArgumentException"><paramref name="path" /> is <b>null</b> or contains whitespace.</exception>
  1181. /// <exception cref="SshConnectionException">Client not connected.</exception>
  1182. private void InternalUploadFile(Stream input, string path, Flags flags, SftpUploadAsyncResult asyncResult, Action<ulong> uploadCallback)
  1183. {
  1184. if (input == null)
  1185. throw new ArgumentNullException("input");
  1186. if (path.IsNullOrWhiteSpace())
  1187. throw new ArgumentException("path");
  1188. var fullPath = this._sftpSession.GetCanonicalPath(path);
  1189. var handle = this._sftpSession.RequestOpen(fullPath, flags);
  1190. ulong offset = 0;
  1191. var buffer = new byte[this.BufferSize];
  1192. var bytesRead = input.Read(buffer, 0, buffer.Length);
  1193. var expectedResponses = 0;
  1194. var expectedResponsesLock = new object();
  1195. do
  1196. {
  1197. // Cancel upload
  1198. if (asyncResult != null && asyncResult.IsUploadCanceled)
  1199. break;
  1200. if (bytesRead > 0)
  1201. {
  1202. if (bytesRead < this.BufferSize)
  1203. {
  1204. // Replace buffer for last chunk of data
  1205. var data = new byte[bytesRead];
  1206. Buffer.BlockCopy(buffer, 0, data, 0, bytesRead);
  1207. buffer = data;
  1208. }
  1209. var writtenBytes = offset + (ulong)buffer.Length;
  1210. this._sftpSession.RequestWrite(handle, offset, buffer, null, (s) =>
  1211. {
  1212. if (s.StatusCode == StatusCodes.Ok)
  1213. {
  1214. lock (expectedResponsesLock)
  1215. {
  1216. expectedResponses--;
  1217. // Call callback to report number of bytes written
  1218. if (uploadCallback != null)
  1219. {
  1220. // Execute callback on different thread
  1221. this.ExecuteThread(() => { uploadCallback(writtenBytes); });
  1222. }
  1223. Monitor.Pulse(expectedResponsesLock);
  1224. }
  1225. }
  1226. });
  1227. lock (expectedResponsesLock)
  1228. {
  1229. expectedResponses++;
  1230. }
  1231. offset += (uint)bytesRead;
  1232. bytesRead = input.Read(buffer, 0, buffer.Length);
  1233. }
  1234. else if (expectedResponses > 0)
  1235. {
  1236. // Wait for expectedResponses to change
  1237. lock (expectedResponsesLock)
  1238. {
  1239. Monitor.Wait(expectedResponsesLock);
  1240. }
  1241. }
  1242. } while (expectedResponses > 0);
  1243. this._sftpSession.RequestClose(handle);
  1244. }
  1245. partial void ExecuteThread(Action action);
  1246. /// <summary>
  1247. /// Called when client is connected to the server.
  1248. /// </summary>
  1249. protected override void OnConnected()
  1250. {
  1251. base.OnConnected();
  1252. this._sftpSession = new SftpSession(this.Session, this.OperationTimeout, this.ConnectionInfo.Encoding);
  1253. this._sftpSession.Connect();
  1254. // Resolve current running version
  1255. this.ProtocolVersion = (int)this._sftpSession.ProtocolVersion;
  1256. }
  1257. /// <summary>
  1258. /// Called when client is disconnecting from the server.
  1259. /// </summary>
  1260. protected override void OnDisconnecting()
  1261. {
  1262. base.OnDisconnecting();
  1263. this._sftpSession.Disconnect();
  1264. }
  1265. /// <summary>
  1266. /// Releases unmanaged and - optionally - managed resources
  1267. /// </summary>
  1268. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
  1269. protected override void Dispose(bool disposing)
  1270. {
  1271. if (this._sftpSession != null)
  1272. {
  1273. this._sftpSession.Dispose();
  1274. this._sftpSession = null;
  1275. }
  1276. if (this._disposeConnectionInfo)
  1277. ((IDisposable)this.ConnectionInfo).Dispose();
  1278. base.Dispose(disposing);
  1279. }
  1280. }
  1281. }