Skip to content

Commit 17d86bf

Browse files
author
Pedro Fonseca
committed
Add support for resuming file upload/download
1 parent f072c5f commit 17d86bf

14 files changed

+36
-24
lines changed

src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateSftpFileReader_EndLStatThrowsSshException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private void SetupMocks()
5959
.Setup(p => p.EndLStat(_statAsyncResult))
6060
.Throws(new SshException());
6161
_sftpSessionMock.InSequence(seq)
62-
.Setup(p => p.CreateFileReader(_handle, _sftpSessionMock.Object, _chunkSize, 3, null))
62+
.Setup(p => p.CreateFileReader(_handle, _sftpSessionMock.Object, _chunkSize, 3, null, 0))
6363
.Returns(_sftpFileReaderMock.Object);
6464
}
6565

src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateSftpFileReader_FileSizeIsAlmostSixTimesGreaterThanChunkSize.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private void SetupMocks()
6363
.Setup(p => p.EndLStat(_statAsyncResult))
6464
.Returns(_fileAttributes);
6565
_sftpSessionMock.InSequence(seq)
66-
.Setup(p => p.CreateFileReader(_handle, _sftpSessionMock.Object, _chunkSize, 7, _fileSize))
66+
.Setup(p => p.CreateFileReader(_handle, _sftpSessionMock.Object, _chunkSize, 7, _fileSize, 0))
6767
.Returns(_sftpFileReaderMock.Object);
6868
}
6969

src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateSftpFileReader_FileSizeIsEqualToChunkSize.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private void SetupMocks()
6363
.Setup(p => p.EndLStat(_statAsyncResult))
6464
.Returns(_fileAttributes);
6565
_sftpSessionMock.InSequence(seq)
66-
.Setup(p => p.CreateFileReader(_handle, _sftpSessionMock.Object, _chunkSize, 2, _fileSize))
66+
.Setup(p => p.CreateFileReader(_handle, _sftpSessionMock.Object, _chunkSize, 2, _fileSize, 0))
6767
.Returns(_sftpFileReaderMock.Object);
6868
}
6969

src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateSftpFileReader_FileSizeIsExactlyFiveTimesGreaterThanChunkSize.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private void SetupMocks()
6363
.Setup(p => p.EndLStat(_statAsyncResult))
6464
.Returns(_fileAttributes);
6565
_sftpSessionMock.InSequence(seq)
66-
.Setup(p => p.CreateFileReader(_handle, _sftpSessionMock.Object, _chunkSize, 6, _fileSize))
66+
.Setup(p => p.CreateFileReader(_handle, _sftpSessionMock.Object, _chunkSize, 6, _fileSize, 0))
6767
.Returns(_sftpFileReaderMock.Object);
6868
}
6969

src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateSftpFileReader_FileSizeIsLessThanChunkSize.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private void SetupMocks()
6363
.Setup(p => p.EndLStat(_statAsyncResult))
6464
.Returns(_fileAttributes);
6565
_sftpSessionMock.InSequence(seq)
66-
.Setup(p => p.CreateFileReader(_handle, _sftpSessionMock.Object, _chunkSize, 2, _fileSize))
66+
.Setup(p => p.CreateFileReader(_handle, _sftpSessionMock.Object, _chunkSize, 2, _fileSize, 0))
6767
.Returns(_sftpFileReaderMock.Object);
6868
}
6969

src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateSftpFileReader_FileSizeIsLittleMoreThanFiveTimesGreaterThanChunkSize.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private void SetupMocks()
6363
.Setup(p => p.EndLStat(_statAsyncResult))
6464
.Returns(_fileAttributes);
6565
_sftpSessionMock.InSequence(seq)
66-
.Setup(p => p.CreateFileReader(_handle, _sftpSessionMock.Object, _chunkSize, 7, _fileSize))
66+
.Setup(p => p.CreateFileReader(_handle, _sftpSessionMock.Object, _chunkSize, 7, _fileSize, 0))
6767
.Returns(_sftpFileReaderMock.Object);
6868
}
6969

src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateSftpFileReader_FileSizeIsMoreThanTenTimesGreaterThanChunkSize.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private void SetupMocks()
6363
.Setup(p => p.EndLStat(_statAsyncResult))
6464
.Returns(_fileAttributes);
6565
_sftpSessionMock.InSequence(seq)
66-
.Setup(p => p.CreateFileReader(_handle, _sftpSessionMock.Object, _chunkSize, 10, _fileSize))
66+
.Setup(p => p.CreateFileReader(_handle, _sftpSessionMock.Object, _chunkSize, 10, _fileSize, 0))
6767
.Returns(_sftpFileReaderMock.Object);
6868
}
6969

src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateSftpFileReader_FileSizeIsZero.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private void SetupMocks()
6363
.Setup(p => p.EndLStat(_statAsyncResult))
6464
.Returns(_fileAttributes);
6565
_sftpSessionMock.InSequence(seq)
66-
.Setup(p => p.CreateFileReader(_handle, _sftpSessionMock.Object, _chunkSize, 1, _fileSize))
66+
.Setup(p => p.CreateFileReader(_handle, _sftpSessionMock.Object, _chunkSize, 1, _fileSize, 0))
6767
.Returns(_sftpFileReaderMock.Object);
6868
}
6969

src/Renci.SshNet/IServiceFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ internal partial interface IServiceFactory
6464
/// <exception cref="SshConnectionException">No key exchange algorithm is supported by both client and server.</exception>
6565
IKeyExchange CreateKeyExchange(IDictionary<string, Type> clientAlgorithms, string[] serverAlgorithms);
6666

67-
ISftpFileReader CreateSftpFileReader(string fileName, ISftpSession sftpSession, uint bufferSize);
67+
ISftpFileReader CreateSftpFileReader(string fileName, ISftpSession sftpSession, uint bufferSize, ulong offset = 0);
6868

6969
ISftpResponseFactory CreateSftpResponseFactory();
7070

src/Renci.SshNet/ServiceFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ from s in serverAlgorithms
110110
return keyExchangeAlgorithmType.CreateInstance<IKeyExchange>();
111111
}
112112

113-
public ISftpFileReader CreateSftpFileReader(string fileName, ISftpSession sftpSession, uint bufferSize)
113+
public ISftpFileReader CreateSftpFileReader(string fileName, ISftpSession sftpSession, uint bufferSize, ulong offset = 0)
114114
{
115115
const int defaultMaxPendingReads = 3;
116116

@@ -132,7 +132,7 @@ public ISftpFileReader CreateSftpFileReader(string fileName, ISftpSession sftpSe
132132
{
133133
var fileAttributes = sftpSession.EndLStat(statAsyncResult);
134134
fileSize = fileAttributes.Size;
135-
maxPendingReads = Math.Min(10, (int) Math.Ceiling((double) fileAttributes.Size / chunkSize) + 1);
135+
maxPendingReads = Math.Min(10, (int) Math.Ceiling((double)(fileSize - (long)offset) / chunkSize) + 1);
136136
}
137137
catch (SshException ex)
138138
{
@@ -142,7 +142,7 @@ public ISftpFileReader CreateSftpFileReader(string fileName, ISftpSession sftpSe
142142
DiagnosticAbstraction.Log(string.Format("Failed to obtain size of file. Allowing maximum {0} pending reads: {1}", maxPendingReads, ex));
143143
}
144144

145-
return sftpSession.CreateFileReader(handle, sftpSession, chunkSize, maxPendingReads, fileSize);
145+
return sftpSession.CreateFileReader(handle, sftpSession, chunkSize, maxPendingReads, fileSize, offset);
146146
}
147147

148148
public ISftpResponseFactory CreateSftpResponseFactory()

0 commit comments

Comments
 (0)