Skip to content

Commit

Permalink
SftpFileEntry.Path: don't append a directory separator when directory…
Browse files Browse the repository at this point in the history
… is '/'. (#236)
  • Loading branch information
tmds authored Oct 7, 2024
1 parent 9292dee commit c8307f2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Tmds.Ssh/RemotePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static ReadOnlySpan<char> TrimEndingDirectorySeparators(ReadOnlySpan<char
return path;
}

private static bool EndsInDirectorySeparator(ReadOnlySpan<char> path) =>
public static bool EndsInDirectorySeparator(ReadOnlySpan<char> path) =>
path.Length > 0 && IsDirectorySeparator(path[path.Length - 1]);

private static bool IsDirectorySeparator(char c)
Expand Down
2 changes: 1 addition & 1 deletion src/Tmds.Ssh/SftpChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ public async ValueTask DownloadDirectoryEntriesAsync(string remoteDirPath, strin
DownloadEntriesOptions.ReplaceCharacters replaceInvalidCharacters = options.ReplaceInvalidCharacters ?? throw new ArgumentNullException(nameof(options.ReplaceInvalidCharacters));

int trimRemoteDirectory = remoteDirPath.Length;
if (!LocalPath.EndsInDirectorySeparator(remoteDirPath))
if (remoteDirPath.Length != 0 && !RemotePath.EndsInDirectorySeparator(remoteDirPath))
{
trimRemoteDirectory++;
}
Expand Down
15 changes: 11 additions & 4 deletions src/Tmds.Ssh/SftpFileEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,18 @@ public ReadOnlySpan<char> Path
{
if (_pathLength == 0)
{
_directoryPath.AsSpan().CopyTo(_pathBuffer);
int length = _directoryPath.Length;

_pathBuffer[length] = RemotePath.DirectorySeparatorChar;
length++;
if (length > 0)
{
_directoryPath.AsSpan().CopyTo(_pathBuffer);

// Append '/' unless _directorPath == '/'.
if (length != 1 || _directoryPath[0] != RemotePath.DirectorySeparatorChar)
{
_pathBuffer[length] = RemotePath.DirectorySeparatorChar;
length++;
}
}

ReadOnlySpan<char> filename = FileName;
filename.CopyTo(_pathBuffer.AsSpan(length));
Expand Down
17 changes: 17 additions & 0 deletions test/Tmds.Ssh.Tests/SftpClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,23 @@ public async Task EnumerateDirectoryFollowDirectoryLinks(bool follow)
}
}

[Fact]
public async Task EnumerateRootDirectory()
{
using var sftpClient = await _sshServer.CreateSftpClientAsync();

var entries = await sftpClient.GetDirectoryEntriesAsync("/").ToListAsync();
Assert.NotEmpty(entries);

foreach (var entry in entries)
{
string path = entry.Path;
Assert.True(path.Length > 2);
Assert.StartsWith("/", path);
Assert.False(path.StartsWith("//"));
}
}

[Fact]
public void DefaultEnumerationOptions()
{
Expand Down

0 comments on commit c8307f2

Please sign in to comment.