SftpMessage.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using System;
  2. #if !TUNING
  3. using System.Collections.Generic;
  4. #endif
  5. using System.IO;
  6. #if !TUNING
  7. using System.Linq;
  8. #endif
  9. using Renci.SshNet.Common;
  10. using System.Globalization;
  11. using Renci.SshNet.Sftp.Responses;
  12. using System.Text;
  13. namespace Renci.SshNet.Sftp
  14. {
  15. internal abstract class SftpMessage : SshData
  16. {
  17. public static SftpMessage Load(uint protocolVersion, byte[] data, Encoding encoding)
  18. {
  19. #if TUNING
  20. var messageType = (SftpMessageTypes) data[4]; // skip packet length bytes
  21. #else
  22. var messageType = (SftpMessageTypes)data.FirstOrDefault();
  23. #endif
  24. return Load(protocolVersion, data, messageType, encoding);
  25. }
  26. protected override int ZeroReaderIndex
  27. {
  28. get
  29. {
  30. #if TUNING
  31. // 4 bytes for the length of the SFTP data
  32. // 1 byte for the SFTP message type
  33. return 5;
  34. #else
  35. return 1;
  36. #endif
  37. }
  38. }
  39. #if TUNING
  40. /// <summary>
  41. /// Gets the size of the message in bytes.
  42. /// </summary>
  43. /// <value>
  44. /// The size of the messages in bytes.
  45. /// </value>
  46. protected override int BufferCapacity
  47. {
  48. get { return ZeroReaderIndex; }
  49. }
  50. #endif
  51. public abstract SftpMessageTypes SftpMessageType { get; }
  52. protected override void LoadData()
  53. {
  54. }
  55. protected override void SaveData()
  56. {
  57. Write((byte) SftpMessageType);
  58. }
  59. #if TUNING
  60. /// <summary>
  61. /// Writes the current message to the specified <see cref="SshDataStream"/>.
  62. /// </summary>
  63. /// <param name="stream">The <see cref="SshDataStream"/> to write the message to.</param>
  64. protected override void WriteBytes(SshDataStream stream)
  65. {
  66. const int sizeOfDataLengthBytes = 4;
  67. var startPosition = stream.Position;
  68. // skip 4 bytes for the length of the SFTP message data
  69. stream.Seek(sizeOfDataLengthBytes, SeekOrigin.Current);
  70. // write the SFTP message data to the stream
  71. base.WriteBytes(stream);
  72. // save where we were positioned when we finished writing the SSH message data
  73. var endPosition = stream.Position;
  74. // determine the length of the SSH message data
  75. var dataLength = endPosition - startPosition - sizeOfDataLengthBytes;
  76. // write the length of the SFTP message where we were positioned before we started
  77. // writing the SFTP message data
  78. stream.Position = startPosition;
  79. stream.Write((uint) dataLength);
  80. // move back to we were positioned when we finished writing the SFTP message data
  81. stream.Position = endPosition;
  82. }
  83. #endif
  84. protected SftpFileAttributes ReadAttributes()
  85. {
  86. #if TUNING
  87. return SftpFileAttributes.FromBytes(DataStream);
  88. #else
  89. var flag = ReadUInt32();
  90. long size = -1;
  91. var userId = -1;
  92. var groupId = -1;
  93. uint permissions = 0;
  94. var accessTime = DateTime.MinValue;
  95. var modifyTime = DateTime.MinValue;
  96. IDictionary<string, string> extensions = null;
  97. if ((flag & 0x00000001) == 0x00000001) // SSH_FILEXFER_ATTR_SIZE
  98. {
  99. size = (long)ReadUInt64();
  100. }
  101. if ((flag & 0x00000002) == 0x00000002) // SSH_FILEXFER_ATTR_UIDGID
  102. {
  103. userId = (int)ReadUInt32();
  104. groupId = (int)ReadUInt32();
  105. }
  106. if ((flag & 0x00000004) == 0x00000004) // SSH_FILEXFER_ATTR_PERMISSIONS
  107. {
  108. permissions = ReadUInt32();
  109. }
  110. if ((flag & 0x00000008) == 0x00000008) // SSH_FILEXFER_ATTR_ACMODTIME
  111. {
  112. var time = ReadUInt32();
  113. accessTime = DateTime.FromFileTime((time + 11644473600) * 10000000);
  114. time = ReadUInt32();
  115. modifyTime = DateTime.FromFileTime((time + 11644473600) * 10000000);
  116. }
  117. if ((flag & 0x80000000) == 0x80000000) // SSH_FILEXFER_ATTR_ACMODTIME
  118. {
  119. var extendedCount = ReadUInt32();
  120. extensions = ReadExtensionPair();
  121. }
  122. var attributes = new SftpFileAttributes(accessTime, modifyTime, size, userId, groupId, permissions, extensions);
  123. return attributes;
  124. #endif
  125. }
  126. #if !TUNING
  127. protected void Write(SftpFileAttributes attributes)
  128. {
  129. if (attributes == null)
  130. {
  131. Write((uint)0);
  132. return;
  133. }
  134. UInt32 flag = 0;
  135. if (attributes.IsSizeChanged && attributes.IsRegularFile)
  136. {
  137. flag |= 0x00000001;
  138. }
  139. if (attributes.IsUserIdChanged|| attributes.IsGroupIdChanged)
  140. {
  141. flag |= 0x00000002;
  142. }
  143. if (attributes.IsPermissionsChanged)
  144. {
  145. flag |= 0x00000004;
  146. }
  147. if (attributes.IsLastAccessTimeChanged || attributes.IsLastWriteTimeChanged)
  148. {
  149. flag |= 0x00000008;
  150. }
  151. if (attributes.IsExtensionsChanged)
  152. {
  153. flag |= 0x80000000;
  154. }
  155. Write(flag);
  156. if (attributes.IsSizeChanged && attributes.IsRegularFile)
  157. {
  158. Write((UInt64)attributes.Size);
  159. }
  160. if (attributes.IsUserIdChanged|| attributes.IsGroupIdChanged)
  161. {
  162. Write((UInt32)attributes.UserId);
  163. Write((UInt32)attributes.GroupId);
  164. }
  165. if (attributes.IsPermissionsChanged)
  166. {
  167. Write(attributes.Permissions);
  168. }
  169. if (attributes.IsLastAccessTimeChanged || attributes.IsLastWriteTimeChanged)
  170. {
  171. var time = (uint)(attributes.LastAccessTime.ToFileTime() / 10000000 - 11644473600);
  172. Write(time);
  173. time = (uint)(attributes.LastWriteTime.ToFileTime() / 10000000 - 11644473600);
  174. Write(time);
  175. }
  176. if (attributes.IsExtensionsChanged)
  177. {
  178. Write(attributes.Extensions);
  179. }
  180. }
  181. #endif
  182. private static SftpMessage Load(uint protocolVersion, byte[] data, SftpMessageTypes messageType, Encoding encoding)
  183. {
  184. SftpMessage message;
  185. switch (messageType)
  186. {
  187. case SftpMessageTypes.Version:
  188. message = new SftpVersionResponse();
  189. break;
  190. case SftpMessageTypes.Status:
  191. message = new SftpStatusResponse(protocolVersion);
  192. break;
  193. case SftpMessageTypes.Data:
  194. message = new SftpDataResponse(protocolVersion);
  195. break;
  196. case SftpMessageTypes.Handle:
  197. message = new SftpHandleResponse(protocolVersion);
  198. break;
  199. case SftpMessageTypes.Name:
  200. message = new SftpNameResponse(protocolVersion, encoding);
  201. break;
  202. case SftpMessageTypes.Attrs:
  203. message = new SftpAttrsResponse(protocolVersion);
  204. break;
  205. case SftpMessageTypes.ExtendedReply:
  206. message = new SftpExtendedReplyResponse(protocolVersion);
  207. break;
  208. default:
  209. throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Message type '{0}' is not supported.", messageType));
  210. }
  211. #if TUNING
  212. message.Load(data);
  213. #else
  214. message.LoadBytes(data);
  215. message.ResetReader();
  216. message.LoadData();
  217. #endif
  218. return message;
  219. }
  220. public override string ToString()
  221. {
  222. return string.Format(CultureInfo.CurrentCulture, "SFTP Message : {0}", SftpMessageType);
  223. }
  224. }
  225. }