SftpRealPathRequest.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Text;
  3. using Renci.SshNet.Sftp.Responses;
  4. namespace Renci.SshNet.Sftp.Requests
  5. {
  6. internal class SftpRealPathRequest : SftpRequest
  7. {
  8. private byte[] _path;
  9. private readonly Action<SftpNameResponse> _nameAction;
  10. public override SftpMessageTypes SftpMessageType
  11. {
  12. get { return SftpMessageTypes.RealPath; }
  13. }
  14. public string Path
  15. {
  16. get { return Encoding.GetString(_path, 0, _path.Length); }
  17. private set { _path = Encoding.GetBytes(value); }
  18. }
  19. public Encoding Encoding { get; private set; }
  20. /// <summary>
  21. /// Gets the size of the message in bytes.
  22. /// </summary>
  23. /// <value>
  24. /// The size of the messages in bytes.
  25. /// </value>
  26. protected override int BufferCapacity
  27. {
  28. get
  29. {
  30. var capacity = base.BufferCapacity;
  31. capacity += 4; // Path length
  32. capacity += _path.Length; // Path
  33. return capacity;
  34. }
  35. }
  36. public SftpRealPathRequest(uint protocolVersion, uint requestId, string path, Encoding encoding, Action<SftpNameResponse> nameAction, Action<SftpStatusResponse> statusAction)
  37. : base(protocolVersion, requestId, statusAction)
  38. {
  39. if (nameAction == null)
  40. throw new ArgumentNullException("nameAction");
  41. Encoding = encoding;
  42. Path = path;
  43. _nameAction = nameAction;
  44. }
  45. protected override void SaveData()
  46. {
  47. base.SaveData();
  48. WriteBinaryString(_path);
  49. }
  50. public override void Complete(SftpResponse response)
  51. {
  52. var nameResponse = response as SftpNameResponse;
  53. if (nameResponse != null)
  54. {
  55. _nameAction(nameResponse);
  56. }
  57. else
  58. {
  59. base.Complete(response);
  60. }
  61. }
  62. }
  63. }