ISftpSession.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using Renci.SshNet.Sftp.Responses;
  5. namespace Renci.SshNet.Sftp
  6. {
  7. internal interface ISftpSession : ISubsystemSession
  8. {
  9. /// <summary>
  10. /// Gets the SFTP protocol version.
  11. /// </summary>
  12. /// <value>
  13. /// The SFTP protocol version.
  14. /// </value>
  15. uint ProtocolVersion { get; }
  16. /// <summary>
  17. /// Gets the remote working directory.
  18. /// </summary>
  19. /// <value>
  20. /// The remote working directory.
  21. /// </value>
  22. string WorkingDirectory { get; }
  23. /// <summary>
  24. /// Changes the current working directory to the specified path.
  25. /// </summary>
  26. /// <param name="path">The new working directory.</param>
  27. void ChangeDirectory(string path);
  28. /// <summary>
  29. /// Resolves a given path into an absolute path on the server.
  30. /// </summary>
  31. /// <param name="path">The path to resolve.</param>
  32. /// <returns>
  33. /// The absolute path.
  34. /// </returns>
  35. string GetCanonicalPath(string path);
  36. /// <summary>
  37. /// Performs SSH_FXP_FSTAT request.
  38. /// </summary>
  39. /// <param name="handle">The handle.</param>
  40. /// <param name="nullOnError">if set to <c>true</c> returns <c>null</c> instead of throwing an exception.</param>
  41. /// <returns>
  42. /// File attributes
  43. /// </returns>
  44. SftpFileAttributes RequestFStat(byte[] handle, bool nullOnError);
  45. /// <summary>
  46. /// Performs SSH_FXP_LSTAT request.
  47. /// </summary>
  48. /// <param name="path">The path.</param>
  49. /// <returns>
  50. /// File attributes
  51. /// </returns>
  52. SftpFileAttributes RequestLStat(string path);
  53. /// <summary>
  54. /// Performs SSH_FXP_MKDIR request.
  55. /// </summary>
  56. /// <param name="path">The path.</param>
  57. void RequestMkDir(string path);
  58. /// <summary>
  59. /// Performs SSH_FXP_OPEN request
  60. /// </summary>
  61. /// <param name="path">The path.</param>
  62. /// <param name="flags">The flags.</param>
  63. /// <param name="nullOnError">if set to <c>true</c> returns <c>null</c> instead of throwing an exception.</param>
  64. /// <returns>File handle.</returns>
  65. byte[] RequestOpen(string path, Flags flags, bool nullOnError = false);
  66. /// <summary>
  67. /// Performs SSH_FXP_OPENDIR request
  68. /// </summary>
  69. /// <param name="path">The path.</param>
  70. /// <param name="nullOnError">if set to <c>true</c> returns null instead of throwing an exception.</param>
  71. /// <returns>File handle.</returns>
  72. byte[] RequestOpenDir(string path, bool nullOnError = false);
  73. /// <summary>
  74. /// Performs posix-rename@openssh.com extended request.
  75. /// </summary>
  76. /// <param name="oldPath">The old path.</param>
  77. /// <param name="newPath">The new path.</param>
  78. void RequestPosixRename(string oldPath, string newPath);
  79. /// <summary>
  80. /// Performs SSH_FXP_READ request.
  81. /// </summary>
  82. /// <param name="handle">The handle.</param>
  83. /// <param name="offset">The offset.</param>
  84. /// <param name="length">The length.</param>
  85. /// <returns>data array; null if EOF</returns>
  86. byte[] RequestRead(byte[] handle, ulong offset, uint length);
  87. /// <summary>
  88. /// Begins an asynchronous read using a SSH_FXP_READ request.
  89. /// </summary>
  90. /// <param name="handle">The handle to the file to read from.</param>
  91. /// <param name="offset">The offset in the file to start reading from.</param>
  92. /// <param name="length">The number of bytes to read.</param>
  93. /// <param name="callback">The <see cref="AsyncCallback"/> delegate that is executed when <see cref="BeginRead(byte[], ulong, uint, AsyncCallback, object)"/> completes.</param>
  94. /// <param name="state">An object that contains any additional user-defined data.</param>
  95. /// <returns>
  96. /// A <see cref="SftpReadAsyncResult"/> that represents the asynchronous call.
  97. /// </returns>
  98. SftpReadAsyncResult BeginRead(byte[] handle, ulong offset, uint length, AsyncCallback callback, object state);
  99. /// <summary>
  100. /// Handles the end of an asynchronous read.
  101. /// </summary>
  102. /// <param name="asyncResult">An <see cref="SftpReadAsyncResult"/> that represents an asynchronous call.</param>
  103. /// <returns>
  104. /// A <see cref="byte"/> array representing the data read.
  105. /// </returns>
  106. /// <remarks>
  107. /// If all available data has been read, the <see cref="EndRead(SftpReadAsyncResult)"/> method completes
  108. /// immediately and returns zero bytes.
  109. /// </remarks>
  110. /// <exception cref="ArgumentNullException"><paramref name="asyncResult"/> is <c>null</c>.</exception>
  111. byte[] EndRead(SftpReadAsyncResult asyncResult);
  112. /// <summary>
  113. /// Performs SSH_FXP_READDIR request
  114. /// </summary>
  115. /// <param name="handle">The handle.</param>
  116. /// <returns></returns>
  117. KeyValuePair<string, SftpFileAttributes>[] RequestReadDir(byte[] handle);
  118. /// <summary>
  119. /// Performs SSH_FXP_REMOVE request.
  120. /// </summary>
  121. /// <param name="path">The path.</param>
  122. void RequestRemove(string path);
  123. /// <summary>
  124. /// Performs SSH_FXP_RENAME request.
  125. /// </summary>
  126. /// <param name="oldPath">The old path.</param>
  127. /// <param name="newPath">The new path.</param>
  128. void RequestRename(string oldPath, string newPath);
  129. /// <summary>
  130. /// Performs SSH_FXP_RMDIR request.
  131. /// </summary>
  132. /// <param name="path">The path.</param>
  133. void RequestRmDir(string path);
  134. /// <summary>
  135. /// Performs SSH_FXP_SETSTAT request.
  136. /// </summary>
  137. /// <param name="path">The path.</param>
  138. /// <param name="attributes">The attributes.</param>
  139. void RequestSetStat(string path, SftpFileAttributes attributes);
  140. /// <summary>
  141. /// Performs statvfs@openssh.com extended request.
  142. /// </summary>
  143. /// <param name="path">The path.</param>
  144. /// <param name="nullOnError">if set to <c>true</c> [null on error].</param>
  145. /// <returns></returns>
  146. SftpFileSytemInformation RequestStatVfs(string path, bool nullOnError = false);
  147. /// <summary>
  148. /// Performs SSH_FXP_SYMLINK request.
  149. /// </summary>
  150. /// <param name="linkpath">The linkpath.</param>
  151. /// <param name="targetpath">The targetpath.</param>
  152. void RequestSymLink(string linkpath, string targetpath);
  153. /// <summary>
  154. /// Performs SSH_FXP_FSETSTAT request.
  155. /// </summary>
  156. /// <param name="handle">The handle.</param>
  157. /// <param name="attributes">The attributes.</param>
  158. void RequestFSetStat(byte[] handle, SftpFileAttributes attributes);
  159. /// <summary>
  160. /// Performs SSH_FXP_WRITE request.
  161. /// </summary>
  162. /// <param name="handle">The handle.</param>
  163. /// <param name="serverOffset">The the zero-based offset (in bytes) relative to the beginning of the file that the write must start at.</param>
  164. /// <param name="data">The buffer holding the data to write.</param>
  165. /// <param name="offset">the zero-based offset in <paramref name="data" /> at which to begin taking bytes to write.</param>
  166. /// <param name="length">The length (in bytes) of the data to write.</param>
  167. /// <param name="wait">The wait event handle if needed.</param>
  168. /// <param name="writeCompleted">The callback to invoke when the write has completed.</param>
  169. void RequestWrite(byte[] handle,
  170. ulong serverOffset,
  171. byte[] data,
  172. int offset,
  173. int length,
  174. AutoResetEvent wait,
  175. Action<SftpStatusResponse> writeCompleted = null);
  176. /// <summary>
  177. /// Performs SSH_FXP_CLOSE request.
  178. /// </summary>
  179. /// <param name="handle">The handle.</param>
  180. void RequestClose(byte[] handle);
  181. /// <summary>
  182. /// Calculates the optimal size of the buffer to read data from the channel.
  183. /// </summary>
  184. /// <param name="bufferSize">The buffer size configured on the client.</param>
  185. /// <returns>
  186. /// The optimal size of the buffer to read data from the channel.
  187. /// </returns>
  188. uint CalculateOptimalReadLength(uint bufferSize);
  189. /// <summary>
  190. /// Calculates the optimal size of the buffer to write data on the channel.
  191. /// </summary>
  192. /// <param name="bufferSize">The buffer size configured on the client.</param>
  193. /// <param name="handle">The file handle.</param>
  194. /// <returns>
  195. /// The optimal size of the buffer to write data on the channel.
  196. /// </returns>
  197. /// <remarks>
  198. /// Currently, we do not take the remote window size into account.
  199. /// </remarks>
  200. uint CalculateOptimalWriteLength(uint bufferSize, byte[] handle);
  201. ISftpFileReader CreateFileReader(string fileName, uint bufferSize);
  202. }
  203. }