Skip to content

Commit af279d2

Browse files
authored
Fix SftpFileAttributes file type detection (#1688)
* Fix SftpFileAttributes file type detection To get the file type, S_IFMT should be used as the mask. Instead it was using each file type as the mask. It meant that e.g. a symbolic link would also show as a regular file and a character device. Also allow setting and retrieving the setuid/setgid/sticky bits * fix build
1 parent c335ce2 commit af279d2

22 files changed

+673
-359
lines changed

src/Renci.SshNet/Sftp/Requests/SftpMkDirRequest.cs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ namespace Renci.SshNet.Sftp.Requests
88
internal sealed class SftpMkDirRequest : SftpRequest
99
{
1010
private byte[] _path;
11-
private byte[] _attributesBytes;
1211

1312
public override SftpMessageTypes SftpMessageType
1413
{
@@ -23,18 +22,6 @@ public string Path
2322

2423
public Encoding Encoding { get; private set; }
2524

26-
private SftpFileAttributes Attributes { get; set; }
27-
28-
private byte[] AttributesBytes
29-
{
30-
get
31-
{
32-
_attributesBytes ??= Attributes.GetBytes();
33-
34-
return _attributesBytes;
35-
}
36-
}
37-
3825
/// <summary>
3926
/// Gets the size of the message in bytes.
4027
/// </summary>
@@ -48,36 +35,30 @@ protected override int BufferCapacity
4835
var capacity = base.BufferCapacity;
4936
capacity += 4; // Path length
5037
capacity += _path.Length; // Path
51-
capacity += AttributesBytes.Length; // Attributes
38+
capacity += 4; // Attributes
5239
return capacity;
5340
}
5441
}
5542

5643
public SftpMkDirRequest(uint protocolVersion, uint requestId, string path, Encoding encoding, Action<SftpStatusResponse> statusAction)
57-
: this(protocolVersion, requestId, path, encoding, SftpFileAttributes.Empty, statusAction)
58-
{
59-
}
60-
61-
private SftpMkDirRequest(uint protocolVersion, uint requestId, string path, Encoding encoding, SftpFileAttributes attributes, Action<SftpStatusResponse> statusAction)
6244
: base(protocolVersion, requestId, statusAction)
6345
{
6446
Encoding = encoding;
6547
Path = path;
66-
Attributes = attributes;
6748
}
6849

6950
protected override void LoadData()
7051
{
7152
base.LoadData();
7253
_path = ReadBinary();
73-
Attributes = ReadAttributes();
54+
_ = ReadAttributes();
7455
}
7556

7657
protected override void SaveData()
7758
{
7859
base.SaveData();
7960
WriteBinaryString(_path);
80-
Write(AttributesBytes);
61+
Write(0u); // empty attributes
8162
}
8263
}
8364
}

src/Renci.SshNet/Sftp/Requests/SftpOpenRequest.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ internal sealed class SftpOpenRequest : SftpRequest
99
{
1010
private readonly Action<SftpHandleResponse> _handleAction;
1111
private byte[] _fileName;
12-
private byte[] _attributes;
1312

1413
public override SftpMessageTypes SftpMessageType
1514
{
@@ -24,12 +23,6 @@ public string Filename
2423

2524
public Flags Flags { get; }
2625

27-
public SftpFileAttributes Attributes
28-
{
29-
get { return SftpFileAttributes.FromBytes(_attributes); }
30-
private set { _attributes = value.GetBytes(); }
31-
}
32-
3326
public Encoding Encoding { get; }
3427

3528
/// <summary>
@@ -46,23 +39,17 @@ protected override int BufferCapacity
4639
capacity += 4; // FileName length
4740
capacity += _fileName.Length; // FileName
4841
capacity += 4; // Flags
49-
capacity += _attributes.Length; // Attributes
42+
capacity += 4; // Attributes
5043
return capacity;
5144
}
5245
}
5346

5447
public SftpOpenRequest(uint protocolVersion, uint requestId, string fileName, Encoding encoding, Flags flags, Action<SftpHandleResponse> handleAction, Action<SftpStatusResponse> statusAction)
55-
: this(protocolVersion, requestId, fileName, encoding, flags, SftpFileAttributes.Empty, handleAction, statusAction)
56-
{
57-
}
58-
59-
private SftpOpenRequest(uint protocolVersion, uint requestId, string fileName, Encoding encoding, Flags flags, SftpFileAttributes attributes, Action<SftpHandleResponse> handleAction, Action<SftpStatusResponse> statusAction)
6048
: base(protocolVersion, requestId, statusAction)
6149
{
6250
Encoding = encoding;
6351
Filename = fileName;
6452
Flags = flags;
65-
Attributes = attributes;
6653

6754
_handleAction = handleAction;
6855
}
@@ -79,7 +66,7 @@ protected override void SaveData()
7966

8067
WriteBinaryString(_fileName);
8168
Write((uint)Flags);
82-
Write(_attributes);
69+
Write(0u); // empty attributes
8370
}
8471

8572
public override void Complete(SftpResponse response)

src/Renci.SshNet/Sftp/Responses/SftpVersionResponse.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ public override SftpMessageTypes SftpMessageType
1111

1212
public uint Version { get; set; }
1313

14-
public IDictionary<string, string> Extentions { get; set; }
14+
public IDictionary<string, string> Extensions { get; set; }
1515

1616
protected override void LoadData()
1717
{
1818
base.LoadData();
1919

2020
Version = ReadUInt32();
21-
Extentions = ReadExtensionPair();
21+
Extensions = ReadExtensionPair();
2222
}
2323

2424
protected override void SaveData()
@@ -27,9 +27,9 @@ protected override void SaveData()
2727

2828
Write(Version);
2929

30-
if (Extentions != null)
30+
if (Extensions != null)
3131
{
32-
Write(Extentions);
32+
Write(Extensions);
3333
}
3434
}
3535
}

0 commit comments

Comments
 (0)