Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Fonseca committed Nov 1, 2023
1 parent f9a3b01 commit b739243
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Renci.SshNet/ServiceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ public ISftpFileReader CreateSftpFileReader(string fileName, ISftpSession sftpSe
{
var fileAttributes = sftpSession.EndLStat(statAsyncResult);
fileSize = fileAttributes.Size;

// calculate maxPendingReads based on remaining size, not total filesize (needed for resume support)
maxPendingReads = Math.Min(10, (int) Math.Ceiling((double)(fileSize - (long)offset) / chunkSize) + 1);
}
catch (SshException ex)
Expand Down
7 changes: 5 additions & 2 deletions src/Renci.SshNet/Sftp/SftpFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,18 @@ public SftpFileReader(byte[] handle, ISftpSession sftpSession, uint chunkSize, i
_sftpSession = sftpSession;
_chunkSize = chunkSize;
_fileSize = fileSize;
_offset = offset;
_readAheadOffset = offset;
_semaphore = new SemaphoreLight(maxPendingReads);
_queue = new Dictionary<int, BufferedRead>(maxPendingReads);
_readLock = new object();
_readAheadCompleted = new ManualResetEvent(initialState: false);
_disposingWaitHandle = new ManualResetEvent(initialState: false);
_waitHandles = _sftpSession.CreateWaitHandleArray(_disposingWaitHandle, _semaphore.AvailableWaitHandle);

// When resuming a download (offset > 0), set the initial offset of the remote file to
// the same offset as the local output file. Read-ahead also starts at the same offset.
_offset = offset;
_readAheadOffset = offset;

StartReadAhead();
}

Expand Down
3 changes: 3 additions & 0 deletions src/Renci.SshNet/SftpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,7 @@ public void UploadFile(Stream input, string path, bool canOverride, Action<ulong

if (input.Position > 0)
{
// if the local stream position is not zero, open the remote file in APPEND mode to resume upload
flags = Flags.Write | Flags.Append;
}
else if (canOverride)
Expand Down Expand Up @@ -1122,6 +1123,7 @@ public IAsyncResult BeginUploadFile(Stream input, string path, bool canOverride,

if (input.Position > 0)
{
// if the local stream position is not zero, open the remote file in APPEND mode to resume upload
flags = Flags.Write | Flags.Append;
}
else if (canOverride)
Expand Down Expand Up @@ -2432,6 +2434,7 @@ private void InternalUploadFile(Stream input, string path, Flags flags, SftpUplo

var handle = _sftpSession.RequestOpen(fullPath, flags);

// Set the initial offset of the remote file to the same as the local file to allow resuming
var offset = (ulong)input.Position;

// create buffer of optimal length
Expand Down

0 comments on commit b739243

Please sign in to comment.