ISftpSession.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Renci.SshNet.Sftp.Responses;
  6. namespace Renci.SshNet.Sftp
  7. {
  8. internal interface ISftpSession : ISubsystemSession
  9. {
  10. /// <summary>
  11. /// Gets the SFTP protocol version.
  12. /// </summary>
  13. /// <value>
  14. /// The SFTP protocol version.
  15. /// </value>
  16. uint ProtocolVersion { get; }
  17. /// <summary>
  18. /// Gets the remote working directory.
  19. /// </summary>
  20. /// <value>
  21. /// The remote working directory.
  22. /// </value>
  23. string WorkingDirectory { get; }
  24. /// <summary>
  25. /// Changes the current working directory to the specified path.
  26. /// </summary>
  27. /// <param name="path">The new working directory.</param>
  28. void ChangeDirectory(string path);
  29. /// <summary>
  30. /// Resolves a given path into an absolute path on the server.
  31. /// </summary>
  32. /// <param name="path">The path to resolve.</param>
  33. /// <returns>
  34. /// The absolute path.
  35. /// </returns>
  36. string GetCanonicalPath(string path);
  37. Task<string> GetCanonicalPathAsync(string path, CancellationToken cancellationToken);
  38. /// <summary>
  39. /// Performs SSH_FXP_FSTAT request.
  40. /// </summary>
  41. /// <param name="handle">The handle.</param>
  42. /// <param name="nullOnError">if set to <see langword="true"/> returns <see langword="null"/> instead of throwing an exception.</param>
  43. /// <returns>
  44. /// File attributes
  45. /// </returns>
  46. SftpFileAttributes RequestFStat(byte[] handle, bool nullOnError);
  47. Task<SftpFileAttributes> RequestFStatAsync(byte[] handle, CancellationToken cancellationToken);
  48. /// <summary>
  49. /// Performs SSH_FXP_STAT request.
  50. /// </summary>
  51. /// <param name="path">The path.</param>
  52. /// <param name="nullOnError">if set to <see langword="true"/> returns null instead of throwing an exception.</param>
  53. /// <returns>
  54. /// File attributes.
  55. /// </returns>
  56. SftpFileAttributes RequestStat(string path, bool nullOnError = false);
  57. /// <summary>
  58. /// Performs SSH_FXP_STAT request.
  59. /// </summary>
  60. /// <param name="path">The path.</param>
  61. /// <param name="callback">The <see cref="AsyncCallback"/> delegate that is executed when <see cref="BeginOpen(string, Flags, AsyncCallback, object)"/> completes.</param>
  62. /// <param name="state">An object that contains any additional user-defined data.</param>
  63. /// <returns>
  64. /// A <see cref="SftpOpenAsyncResult"/> that represents the asynchronous call.
  65. /// </returns>
  66. SFtpStatAsyncResult BeginStat(string path, AsyncCallback callback, object state);
  67. /// <summary>
  68. /// Handles the end of an asynchronous read.
  69. /// </summary>
  70. /// <param name="asyncResult">An <see cref="SFtpStatAsyncResult"/> that represents an asynchronous call.</param>
  71. /// <returns>
  72. /// The file attributes.
  73. /// </returns>
  74. /// <exception cref="ArgumentNullException"><paramref name="asyncResult"/> is <see langword="null"/>.</exception>
  75. SftpFileAttributes EndStat(SFtpStatAsyncResult asyncResult);
  76. /// <summary>
  77. /// Performs SSH_FXP_LSTAT request.
  78. /// </summary>
  79. /// <param name="path">The path.</param>
  80. /// <returns>
  81. /// File attributes
  82. /// </returns>
  83. SftpFileAttributes RequestLStat(string path);
  84. /// <summary>
  85. /// Performs SSH_FXP_LSTAT request.
  86. /// </summary>
  87. /// <param name="path">The path.</param>
  88. /// <param name="callback">The <see cref="AsyncCallback"/> delegate that is executed when <see cref="BeginLStat(string, AsyncCallback, object)"/> completes.</param>
  89. /// <param name="state">An object that contains any additional user-defined data.</param>
  90. /// <returns>
  91. /// A <see cref="SFtpStatAsyncResult"/> that represents the asynchronous call.
  92. /// </returns>
  93. SFtpStatAsyncResult BeginLStat(string path, AsyncCallback callback, object state);
  94. /// <summary>
  95. /// Handles the end of an asynchronous SSH_FXP_LSTAT request.
  96. /// </summary>
  97. /// <param name="asyncResult">An <see cref="SFtpStatAsyncResult"/> that represents an asynchronous call.</param>
  98. /// <returns>
  99. /// The file attributes.
  100. /// </returns>
  101. /// <exception cref="ArgumentNullException"><paramref name="asyncResult"/> is <see langword="null"/>.</exception>
  102. SftpFileAttributes EndLStat(SFtpStatAsyncResult asyncResult);
  103. /// <summary>
  104. /// Performs SSH_FXP_MKDIR request.
  105. /// </summary>
  106. /// <param name="path">The path.</param>
  107. void RequestMkDir(string path);
  108. /// <summary>
  109. /// Performs SSH_FXP_OPEN request.
  110. /// </summary>
  111. /// <param name="path">The path.</param>
  112. /// <param name="flags">The flags.</param>
  113. /// <param name="nullOnError">if set to <see langword="true"/> returns <see langword="null"/> instead of throwing an exception.</param>
  114. /// <returns>File handle.</returns>
  115. byte[] RequestOpen(string path, Flags flags, bool nullOnError = false);
  116. Task<byte[]> RequestOpenAsync(string path, Flags flags, CancellationToken cancellationToken);
  117. /// <summary>
  118. /// Performs SSH_FXP_OPEN request.
  119. /// </summary>
  120. /// <param name="path">The path.</param>
  121. /// <param name="flags">The flags.</param>
  122. /// <param name="callback">The <see cref="AsyncCallback"/> delegate that is executed when <see cref="BeginOpen(string, Flags, AsyncCallback, object)"/> completes.</param>
  123. /// <param name="state">An object that contains any additional user-defined data.</param>
  124. /// <returns>
  125. /// A <see cref="SftpOpenAsyncResult"/> that represents the asynchronous call.
  126. /// </returns>
  127. SftpOpenAsyncResult BeginOpen(string path, Flags flags, AsyncCallback callback, object state);
  128. /// <summary>
  129. /// Handles the end of an asynchronous read.
  130. /// </summary>
  131. /// <param name="asyncResult">An <see cref="SftpOpenAsyncResult"/> that represents an asynchronous call.</param>
  132. /// <returns>
  133. /// A <see cref="byte"/> array representing a file handle.
  134. /// </returns>
  135. /// <remarks>
  136. /// If all available data has been read, the <see cref="EndOpen(SftpOpenAsyncResult)"/> method completes
  137. /// immediately and returns zero bytes.
  138. /// </remarks>
  139. /// <exception cref="ArgumentNullException"><paramref name="asyncResult"/> is <see langword="null"/>.</exception>
  140. byte[] EndOpen(SftpOpenAsyncResult asyncResult);
  141. /// <summary>
  142. /// Performs SSH_FXP_OPENDIR request.
  143. /// </summary>
  144. /// <param name="path">The path.</param>
  145. /// <param name="nullOnError">if set to <see langword="true"/> returns null instead of throwing an exception.</param>
  146. /// <returns>File handle.</returns>
  147. byte[] RequestOpenDir(string path, bool nullOnError = false);
  148. Task<byte[]> RequestOpenDirAsync(string path, CancellationToken cancellationToken);
  149. /// <summary>
  150. /// Performs posix-rename@openssh.com extended request.
  151. /// </summary>
  152. /// <param name="oldPath">The old path.</param>
  153. /// <param name="newPath">The new path.</param>
  154. void RequestPosixRename(string oldPath, string newPath);
  155. /// <summary>
  156. /// Performs SSH_FXP_READ request.
  157. /// </summary>
  158. /// <param name="handle">The handle.</param>
  159. /// <param name="offset">The offset.</param>
  160. /// <param name="length">The length.</param>
  161. /// <returns>data array; null if EOF</returns>
  162. byte[] RequestRead(byte[] handle, ulong offset, uint length);
  163. /// <summary>
  164. /// Begins an asynchronous read using a SSH_FXP_READ request.
  165. /// </summary>
  166. /// <param name="handle">The handle to the file to read from.</param>
  167. /// <param name="offset">The offset in the file to start reading from.</param>
  168. /// <param name="length">The number of bytes to read.</param>
  169. /// <param name="callback">The <see cref="AsyncCallback"/> delegate that is executed when <see cref="BeginRead(byte[], ulong, uint, AsyncCallback, object)"/> completes.</param>
  170. /// <param name="state">An object that contains any additional user-defined data.</param>
  171. /// <returns>
  172. /// A <see cref="SftpReadAsyncResult"/> that represents the asynchronous call.
  173. /// </returns>
  174. SftpReadAsyncResult BeginRead(byte[] handle, ulong offset, uint length, AsyncCallback callback, object state);
  175. /// <summary>
  176. /// Handles the end of an asynchronous read.
  177. /// </summary>
  178. /// <param name="asyncResult">An <see cref="SftpReadAsyncResult"/> that represents an asynchronous call.</param>
  179. /// <returns>
  180. /// A <see cref="byte"/> array representing the data read.
  181. /// </returns>
  182. /// <remarks>
  183. /// If all available data has been read, the <see cref="EndRead(SftpReadAsyncResult)"/> method completes
  184. /// immediately and returns zero bytes.
  185. /// </remarks>
  186. /// <exception cref="ArgumentNullException"><paramref name="asyncResult"/> is <see langword="null"/>.</exception>
  187. byte[] EndRead(SftpReadAsyncResult asyncResult);
  188. Task<byte[]> RequestReadAsync(byte[] handle, ulong offset, uint length, CancellationToken cancellationToken);
  189. /// <summary>
  190. /// Performs SSH_FXP_READDIR request.
  191. /// </summary>
  192. /// <param name="handle">The handle.</param>
  193. /// <returns></returns>
  194. KeyValuePair<string, SftpFileAttributes>[] RequestReadDir(byte[] handle);
  195. Task<KeyValuePair<string, SftpFileAttributes>[]> RequestReadDirAsync(byte[] handle, CancellationToken cancellationToken);
  196. /// <summary>
  197. /// Performs SSH_FXP_REALPATH request.
  198. /// </summary>
  199. /// <param name="path">The path.</param>
  200. /// <param name="callback">The <see cref="AsyncCallback"/> delegate that is executed when <see cref="BeginRealPath(string, AsyncCallback, object)"/> completes.</param>
  201. /// <param name="state">An object that contains any additional user-defined data.</param>
  202. /// <returns>
  203. /// A <see cref="SftpRealPathAsyncResult"/> that represents the asynchronous call.
  204. /// </returns>
  205. SftpRealPathAsyncResult BeginRealPath(string path, AsyncCallback callback, object state);
  206. /// <summary>
  207. /// Handles the end of an asynchronous SSH_FXP_REALPATH request.
  208. /// </summary>
  209. /// <param name="asyncResult">An <see cref="SftpRealPathAsyncResult"/> that represents an asynchronous call.</param>
  210. /// <returns>
  211. /// The absolute path.
  212. /// </returns>
  213. /// <exception cref="ArgumentNullException"><paramref name="asyncResult"/> is <see langword="null"/>.</exception>
  214. string EndRealPath(SftpRealPathAsyncResult asyncResult);
  215. /// <summary>
  216. /// Performs SSH_FXP_REMOVE request.
  217. /// </summary>
  218. /// <param name="path">The path.</param>
  219. void RequestRemove(string path);
  220. Task RequestRemoveAsync(string path, CancellationToken cancellationToken);
  221. /// <summary>
  222. /// Performs SSH_FXP_RENAME request.
  223. /// </summary>
  224. /// <param name="oldPath">The old path.</param>
  225. /// <param name="newPath">The new path.</param>
  226. void RequestRename(string oldPath, string newPath);
  227. Task RequestRenameAsync(string oldPath, string newPath, CancellationToken cancellationToken);
  228. /// <summary>
  229. /// Performs SSH_FXP_RMDIR request.
  230. /// </summary>
  231. /// <param name="path">The path.</param>
  232. void RequestRmDir(string path);
  233. /// <summary>
  234. /// Performs SSH_FXP_SETSTAT request.
  235. /// </summary>
  236. /// <param name="path">The path.</param>
  237. /// <param name="attributes">The attributes.</param>
  238. void RequestSetStat(string path, SftpFileAttributes attributes);
  239. /// <summary>
  240. /// Performs statvfs@openssh.com extended request.
  241. /// </summary>
  242. /// <param name="path">The path.</param>
  243. /// <param name="nullOnError">if set to <see langword="true"/> [null on error].</param>
  244. /// <returns></returns>
  245. SftpFileSytemInformation RequestStatVfs(string path, bool nullOnError = false);
  246. Task<SftpFileSytemInformation> RequestStatVfsAsync(string path, CancellationToken cancellationToken);
  247. /// <summary>
  248. /// Performs SSH_FXP_SYMLINK request.
  249. /// </summary>
  250. /// <param name="linkpath">The linkpath.</param>
  251. /// <param name="targetpath">The targetpath.</param>
  252. void RequestSymLink(string linkpath, string targetpath);
  253. /// <summary>
  254. /// Performs SSH_FXP_FSETSTAT request.
  255. /// </summary>
  256. /// <param name="handle">The handle.</param>
  257. /// <param name="attributes">The attributes.</param>
  258. void RequestFSetStat(byte[] handle, SftpFileAttributes attributes);
  259. /// <summary>
  260. /// Performs SSH_FXP_WRITE request.
  261. /// </summary>
  262. /// <param name="handle">The handle.</param>
  263. /// <param name="serverOffset">The the zero-based offset (in bytes) relative to the beginning of the file that the write must start at.</param>
  264. /// <param name="data">The buffer holding the data to write.</param>
  265. /// <param name="offset">the zero-based offset in <paramref name="data" /> at which to begin taking bytes to write.</param>
  266. /// <param name="length">The length (in bytes) of the data to write.</param>
  267. /// <param name="wait">The wait event handle if needed.</param>
  268. /// <param name="writeCompleted">The callback to invoke when the write has completed.</param>
  269. void RequestWrite(byte[] handle,
  270. ulong serverOffset,
  271. byte[] data,
  272. int offset,
  273. int length,
  274. AutoResetEvent wait,
  275. Action<SftpStatusResponse> writeCompleted = null);
  276. Task RequestWriteAsync(byte[] handle, ulong serverOffset, byte[] data, int offset, int length, CancellationToken cancellationToken);
  277. /// <summary>
  278. /// Performs SSH_FXP_CLOSE request.
  279. /// </summary>
  280. /// <param name="handle">The handle.</param>
  281. void RequestClose(byte[] handle);
  282. Task RequestCloseAsync(byte[] handle, CancellationToken cancellationToken);
  283. /// <summary>
  284. /// Performs SSH_FXP_CLOSE request.
  285. /// </summary>
  286. /// <param name="handle">The handle.</param>
  287. /// <param name="callback">The <see cref="AsyncCallback"/> delegate that is executed when <see cref="BeginClose(byte[], AsyncCallback, object)"/> completes.</param>
  288. /// <param name="state">An object that contains any additional user-defined data.</param>
  289. /// <returns>
  290. /// A <see cref="SftpCloseAsyncResult"/> that represents the asynchronous call.
  291. /// </returns>
  292. SftpCloseAsyncResult BeginClose(byte[] handle, AsyncCallback callback, object state);
  293. /// <summary>
  294. /// Handles the end of an asynchronous close.
  295. /// </summary>
  296. /// <param name="asyncResult">An <see cref="SftpCloseAsyncResult"/> that represents an asynchronous call.</param>
  297. /// <exception cref="ArgumentNullException"><paramref name="asyncResult"/> is <see langword="null"/>.</exception>
  298. void EndClose(SftpCloseAsyncResult asyncResult);
  299. /// <summary>
  300. /// Calculates the optimal size of the buffer to read data from the channel.
  301. /// </summary>
  302. /// <param name="bufferSize">The buffer size configured on the client.</param>
  303. /// <returns>
  304. /// The optimal size of the buffer to read data from the channel.
  305. /// </returns>
  306. uint CalculateOptimalReadLength(uint bufferSize);
  307. /// <summary>
  308. /// Calculates the optimal size of the buffer to write data on the channel.
  309. /// </summary>
  310. /// <param name="bufferSize">The buffer size configured on the client.</param>
  311. /// <param name="handle">The file handle.</param>
  312. /// <returns>
  313. /// The optimal size of the buffer to write data on the channel.
  314. /// </returns>
  315. /// <remarks>
  316. /// Currently, we do not take the remote window size into account.
  317. /// </remarks>
  318. uint CalculateOptimalWriteLength(uint bufferSize, byte[] handle);
  319. ISftpFileReader CreateFileReader(byte[] handle, ISftpSession sftpSession, uint chunkSize, int maxPendingReads, long? fileSize);
  320. }
  321. }