Skip to content

Commit

Permalink
Fix read offset.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmds committed Dec 11, 2024
1 parent d26239a commit 9ef34a3
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Tmds.Ssh/SftpChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ async ValueTask CopyBuffer(ValueTask previousCopy, long offset, int length)
}

buffer = ArrayPool<byte>.Shared.Rent(length);
bytesRead = await sourceFile.ReadAtAsync(buffer, sourceFile.Position + offset, cancellationToken).ConfigureAwait(false);
bytesRead = await sourceFile.ReadAtAsync(buffer.AsMemory(0, length), sourceFile.Position + offset, cancellationToken).ConfigureAwait(false);
if (bytesRead == 0)
{
break;
Expand Down Expand Up @@ -864,19 +864,21 @@ async ValueTask CopyBuffer(ValueTask previousCopy, long offset, int length)

buffer = ArrayPool<byte>.Shared.Rent(length);
int remaining = length;
long readOffset = startOffset + offset;
do
{
int bytesRead;
lock (breakLoop) // Ensure only one thread is reading the Stream concurrently.
{
source.Position = startOffset + offset;
bytesRead = source.Read(buffer.AsSpan(length - remaining));
source.Position = readOffset;
bytesRead = source.Read(buffer.AsSpan(length - remaining, remaining));
}
if (bytesRead == 0)
{
throw new IOException("Unexpected end of file. The source was truncated during the upload.");
}
remaining -= bytesRead;
readOffset += bytesRead;
} while (remaining > 0);

await remoteFile.WriteAtAsync(buffer.AsMemory(0, length), offset, cancellationToken).ConfigureAwait(false);
Expand Down

0 comments on commit 9ef34a3

Please sign in to comment.