SftpStatRequest.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Text;
  3. using Renci.SshNet.Sftp.Responses;
  4. namespace Renci.SshNet.Sftp.Requests
  5. {
  6. internal class SftpStatRequest : SftpRequest
  7. {
  8. private byte[] _path;
  9. private readonly Action<SftpAttrsResponse> _attrsAction;
  10. public override SftpMessageTypes SftpMessageType
  11. {
  12. get { return SftpMessageTypes.Stat; }
  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 SftpStatRequest(uint protocolVersion, uint requestId, string path, Encoding encoding, Action<SftpAttrsResponse> attrsAction, Action<SftpStatusResponse> statusAction)
  37. : base(protocolVersion, requestId, statusAction)
  38. {
  39. Encoding = encoding;
  40. Path = path;
  41. _attrsAction = attrsAction;
  42. }
  43. protected override void LoadData()
  44. {
  45. base.LoadData();
  46. _path = ReadBinary();
  47. }
  48. protected override void SaveData()
  49. {
  50. base.SaveData();
  51. WriteBinaryString(_path);
  52. }
  53. public override void Complete(SftpResponse response)
  54. {
  55. var attrsResponse = response as SftpAttrsResponse;
  56. if (attrsResponse != null)
  57. {
  58. _attrsAction(attrsResponse);
  59. }
  60. else
  61. {
  62. base.Complete(response);
  63. }
  64. }
  65. }
  66. }