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