Skip to content

Commit b739243

Browse files
author
Pedro Fonseca
committed
add comments
1 parent f9a3b01 commit b739243

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

src/Renci.SshNet/ServiceFactory.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ public ISftpFileReader CreateSftpFileReader(string fileName, ISftpSession sftpSe
164164
{
165165
var fileAttributes = sftpSession.EndLStat(statAsyncResult);
166166
fileSize = fileAttributes.Size;
167+
168+
// calculate maxPendingReads based on remaining size, not total filesize (needed for resume support)
167169
maxPendingReads = Math.Min(10, (int) Math.Ceiling((double)(fileSize - (long)offset) / chunkSize) + 1);
168170
}
169171
catch (SshException ex)

src/Renci.SshNet/Sftp/SftpFileReader.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,18 @@ public SftpFileReader(byte[] handle, ISftpSession sftpSession, uint chunkSize, i
6363
_sftpSession = sftpSession;
6464
_chunkSize = chunkSize;
6565
_fileSize = fileSize;
66-
_offset = offset;
67-
_readAheadOffset = offset;
6866
_semaphore = new SemaphoreLight(maxPendingReads);
6967
_queue = new Dictionary<int, BufferedRead>(maxPendingReads);
7068
_readLock = new object();
7169
_readAheadCompleted = new ManualResetEvent(initialState: false);
7270
_disposingWaitHandle = new ManualResetEvent(initialState: false);
7371
_waitHandles = _sftpSession.CreateWaitHandleArray(_disposingWaitHandle, _semaphore.AvailableWaitHandle);
7472

73+
// When resuming a download (offset > 0), set the initial offset of the remote file to
74+
// the same offset as the local output file. Read-ahead also starts at the same offset.
75+
_offset = offset;
76+
_readAheadOffset = offset;
77+
7578
StartReadAhead();
7679
}
7780

src/Renci.SshNet/SftpClient.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,7 @@ public void UploadFile(Stream input, string path, bool canOverride, Action<ulong
980980

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

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

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

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

24372440
// create buffer of optimal length

0 commit comments

Comments
 (0)