Skip to content

Commit

Permalink
Add UploadEntriesOptions.ShouldRecurse. (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmds authored Oct 9, 2024
1 parent c8307f2 commit 822cc09
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ class UploadEntriesOptions
bool RecurseSubdirectories { get; set; } = true;
bool FollowFileLinks { get; set; } = true;
bool FollowDirectoryLinks { get; set; } = true;
LocalFileEntryPredicate? ShouldRecurse { get; set; }
}
delegate T SftpFileEntryTransform<T>(ref SftpFileEntry entry);
delegate bool SftpFileEntryPredicate(ref SftpFileEntry entry);
Expand All @@ -379,7 +380,12 @@ ref struct SftpFileEntry
ReadOnlySpan<char> FileName { get; }

FileEntryAttributes ToAttributes();
string ToPath()
string ToPath();
}
delegate bool LocalFileEntryPredicate(ref LocalFileEntry entry);
ref struct LocalFileEntry
{
string ToFullPath();
}
enum UnixFileType
{
Expand Down
21 changes: 21 additions & 0 deletions src/Tmds.Ssh/LocalFileEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// This file is part of Tmds.Ssh which is released under MIT.
// See file LICENSE for full license details.

using System.IO.Enumeration;

namespace Tmds.Ssh;

public delegate bool LocalFileEntryPredicate(ref LocalFileEntry entry);

// Library version of FileSystemEntry.
public ref struct LocalFileEntry
{
private readonly FileSystemEntry _entry;

public string ToFullPath() => _entry.ToFullPath();

internal LocalFileEntry(ref FileSystemEntry entry)
{
_entry = entry;
}
}
21 changes: 21 additions & 0 deletions src/Tmds.Ssh/SftpChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,27 @@ public async ValueTask UploadDirectoryEntriesAsync(string localDirPath, string r
RecurseSubdirectories = recurse
});

LocalFileEntryPredicate? shouldRecurse = options.ShouldRecurse;
if (recurse && (!followDirectoryLinks || shouldRecurse is not null))
{
fse.ShouldRecursePredicate = (ref FileSystemEntry entry) =>
{
bool isLink = (entry.Attributes & FileAttributes.ReparsePoint) != 0;
if (isLink && !followDirectoryLinks)
{
return false;
}

if (shouldRecurse is null)
{
return true;
}

LocalFileEntry localFileEntry = new LocalFileEntry(ref entry);
return shouldRecurse(ref localFileEntry);
};
}

var onGoing = new Queue<ValueTask>();
var bufferSemaphore = new SemaphoreSlim(MaxConcurrentBuffers, MaxConcurrentBuffers);
try
Expand Down
1 change: 1 addition & 0 deletions src/Tmds.Ssh/UploadEntriesOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public sealed class UploadEntriesOptions
public bool RecurseSubdirectories { get; set; } = true;
public bool FollowFileLinks { get; set; } = true;
public bool FollowDirectoryLinks { get; set; } = true;
public LocalFileEntryPredicate? ShouldRecurse { get; set; }
}
38 changes: 38 additions & 0 deletions test/Tmds.Ssh.Tests/SftpClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,44 @@ public async Task UploadFollowDirectoryLinks(bool follow)
}
}

[InlineData(true)]
[InlineData(false)]
[Theory]
public async Task UploadShouldRecurse(bool recurse)
{
using var sftpClient = await _sshServer.CreateSftpClientAsync();

string sourceDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(sourceDir);
string childDir = $"{sourceDir}/dir";
Directory.CreateDirectory(childDir);
File.OpenWrite($"{childDir}/file").Dispose();

string remoteDir = $"/tmp/{Path.GetRandomFileName()}";
await sftpClient.CreateNewDirectoryAsync(remoteDir);
await sftpClient.UploadDirectoryEntriesAsync(sourceDir, remoteDir, new UploadEntriesOptions()
{
ShouldRecurse = (ref LocalFileEntry entry) =>
{
Assert.Equal(childDir, entry.ToFullPath());
return recurse;
}
});

var fileAttributes = await sftpClient.GetAttributesAsync($"{remoteDir}/dir/file");
if (recurse)
{
Assert.NotNull(fileAttributes);
}
else
{
Assert.Null(fileAttributes);
}

var dirAttributes = await sftpClient.GetAttributesAsync($"{remoteDir}/dir");
Assert.NotNull(dirAttributes);
}

[Fact]
public async Task FullPath()
{
Expand Down

0 comments on commit 822cc09

Please sign in to comment.