SftpOpenRequest.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Text;
  3. using Renci.SshNet.Sftp.Responses;
  4. namespace Renci.SshNet.Sftp.Requests
  5. {
  6. internal class SftpOpenRequest : SftpRequest
  7. {
  8. private byte[] _fileName;
  9. private byte[] _attributes;
  10. private readonly Action<SftpHandleResponse> _handleAction;
  11. public override SftpMessageTypes SftpMessageType
  12. {
  13. get { return SftpMessageTypes.Open; }
  14. }
  15. public string Filename
  16. {
  17. get { return Encoding.GetString(_fileName, 0, _fileName.Length); }
  18. private set { _fileName = Encoding.GetBytes(value); }
  19. }
  20. public Flags Flags { get; private set; }
  21. public SftpFileAttributes Attributes
  22. {
  23. get { return SftpFileAttributes.FromBytes(_attributes); }
  24. private set { _attributes = value.GetBytes(); }
  25. }
  26. public Encoding Encoding { get; private set; }
  27. /// <summary>
  28. /// Gets the size of the message in bytes.
  29. /// </summary>
  30. /// <value>
  31. /// The size of the messages in bytes.
  32. /// </value>
  33. protected override int BufferCapacity
  34. {
  35. get
  36. {
  37. var capacity = base.BufferCapacity;
  38. capacity += 4; // FileName length
  39. capacity += _fileName.Length; // FileName
  40. capacity += 4; // Flags
  41. capacity += _attributes.Length; // Attributes
  42. return capacity;
  43. }
  44. }
  45. public SftpOpenRequest(uint protocolVersion, uint requestId, string fileName, Encoding encoding, Flags flags, Action<SftpHandleResponse> handleAction, Action<SftpStatusResponse> statusAction)
  46. : this(protocolVersion, requestId, fileName, encoding, flags, SftpFileAttributes.Empty, handleAction, statusAction)
  47. {
  48. }
  49. private SftpOpenRequest(uint protocolVersion, uint requestId, string fileName, Encoding encoding, Flags flags, SftpFileAttributes attributes, Action<SftpHandleResponse> handleAction, Action<SftpStatusResponse> statusAction)
  50. : base(protocolVersion, requestId, statusAction)
  51. {
  52. Encoding = encoding;
  53. Filename = fileName;
  54. Flags = flags;
  55. Attributes = attributes;
  56. _handleAction = handleAction;
  57. }
  58. protected override void LoadData()
  59. {
  60. base.LoadData();
  61. throw new NotSupportedException();
  62. }
  63. protected override void SaveData()
  64. {
  65. base.SaveData();
  66. WriteBinaryString(_fileName);
  67. Write((uint) Flags);
  68. Write(_attributes);
  69. }
  70. public override void Complete(SftpResponse response)
  71. {
  72. var handleResponse = response as SftpHandleResponse;
  73. if (handleResponse != null)
  74. {
  75. _handleAction(handleResponse);
  76. }
  77. else
  78. {
  79. base.Complete(response);
  80. }
  81. }
  82. }
  83. }