Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmds committed Oct 19, 2024
1 parent 72217a9 commit d63f28d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 12 additions & 3 deletions src/Tmds.Ssh/SftpChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ internal int GetCopyBetweenSftpFilesBufferSize(byte[] destinationHandle)

internal SftpExtension EnabledExtensions => _supportedExtensions;


private bool SupportsCopyData => (_supportedExtensions & SftpExtension.CopyData) != 0;

public void Dispose()
Expand Down Expand Up @@ -209,7 +208,7 @@ public async ValueTask CopyFileAsync(string sourcePath, string destinationPath,
sourceAttributesTask = sourceFile.GetAttributesAsync(cancellationToken);
#pragma warning restore CS8619

// When we are overwriting. The file may exists and be larger than the source file.
// When we are overwriting, the file may exists and be larger than the source file.
// We could open with Truncate but then the user would lose their data if they (by accident) uses a source and destination that are the same file.
// To avoid that, we'll truncate after copying the data instead.
SftpOpenFlags openFlags = overwrite ? SftpOpenFlags.OpenOrCreate : SftpOpenFlags.CreateNew;
Expand Down Expand Up @@ -246,7 +245,7 @@ public async ValueTask CopyFileAsync(string sourcePath, string destinationPath,
long initialLength = await initialLengthTask.ConfigureAwait(false);
if (initialLength > copyLength)
{
await destinationFile.SetLengthAsync(copyLength);
await destinationFile.SetLengthAsync(copyLength).ConfigureAwait(false);
}

async ValueTask CopyAsync(long length, CancellationToken cancellationToken)
Expand Down Expand Up @@ -286,12 +285,15 @@ async ValueTask CopyBuffer(ValueTask previousCopy, long offset, int length)
{
return;
}

buffer = ArrayPool<byte>.Shared.Rent(length);
bytesRead = await sourceFile.ReadAtAsync(buffer, sourceFile.Position + offset, cancellationToken).ConfigureAwait(false);
if (bytesRead == 0)
{
break;
}

// Our download buffer becomes an upload buffer.
await s_uploadBufferSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
}
catch
Expand All @@ -316,6 +318,11 @@ async ValueTask CopyBuffer(ValueTask previousCopy, long offset, int length)
}
finally
{
if (buffer != null)
{
ArrayPool<byte>.Shared.Return(buffer);
buffer = null;
}
s_uploadBufferSemaphore.Release();
}
}
Expand Down Expand Up @@ -810,6 +817,7 @@ async ValueTask CopyBuffer(ValueTask previousCopy, long offset, int length)
{
return;
}

buffer = ArrayPool<byte>.Shared.Rent(length);
do
{
Expand Down Expand Up @@ -1100,6 +1108,7 @@ async ValueTask CopyBuffer(ValueTask previousCopy, long offset, int length)
{
return;
}

buffer = ArrayPool<byte>.Shared.Rent(length);
do
{
Expand Down
2 changes: 1 addition & 1 deletion test/Tmds.Ssh.Tests/sshd_container/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM quay.io/centos/centos:stream9
FROM quay.io/centos/centos:stream10-development

EXPOSE 88/tcp
EXPOSE 88/udp
Expand Down

0 comments on commit d63f28d

Please sign in to comment.