2
0

SftpNameResponse.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Renci.SshNet.Sftp.Responses
  5. {
  6. internal sealed class SftpNameResponse : SftpResponse
  7. {
  8. public override SftpMessageTypes SftpMessageType
  9. {
  10. get { return SftpMessageTypes.Name; }
  11. }
  12. public uint Count { get; private set; }
  13. public Encoding Encoding { get; private set; }
  14. public KeyValuePair<string, SftpFileAttributes>[] Files { get; set; }
  15. public SftpNameResponse(uint protocolVersion, Encoding encoding)
  16. : base(protocolVersion)
  17. {
  18. Files = Array.Empty<KeyValuePair<string, SftpFileAttributes>>();
  19. Encoding = encoding;
  20. }
  21. protected override void LoadData()
  22. {
  23. base.LoadData();
  24. Count = ReadUInt32();
  25. Files = new KeyValuePair<string, SftpFileAttributes>[Count];
  26. for (var i = 0; i < Count; i++)
  27. {
  28. var fileName = ReadString(Encoding);
  29. if (SupportsLongName(ProtocolVersion))
  30. {
  31. _ = ReadString(Encoding); // skip longname
  32. }
  33. Files[i] = new KeyValuePair<string, SftpFileAttributes>(fileName, ReadAttributes());
  34. }
  35. }
  36. protected override void SaveData()
  37. {
  38. base.SaveData();
  39. Write((uint)Files.Length); // count
  40. for (var i = 0; i < Files.Length; i++)
  41. {
  42. var file = Files[i];
  43. Write(file.Key, Encoding); // filename
  44. if (SupportsLongName(ProtocolVersion))
  45. {
  46. Write(0U); // longname
  47. }
  48. Write(file.Value.GetBytes()); // attrs
  49. }
  50. }
  51. private static bool SupportsLongName(uint protocolVersion)
  52. {
  53. return protocolVersion <= 3U;
  54. }
  55. }
  56. }