Skip to content

🐛 AsyncResult contains invalid value (Fix #329) #680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 16 additions & 32 deletions src/Renci.SshNet/SftpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ public IEnumerable<SftpFile> ListDirectory(string path, Action<int> listCallback
{
CheckDisposed();

return InternalListDirectory(path, listCallback);
return InternalListDirectory(path, null, listCallback);
}

/// <summary>
Expand All @@ -497,15 +497,7 @@ public IAsyncResult BeginListDirectory(string path, AsyncCallback asyncCallback,
{
try
{
var result = InternalListDirectory(path, count =>
{
asyncResult.Update(count);

if (listCallback != null)
{
listCallback(count);
}
});
var result = InternalListDirectory(path, asyncResult, listCallback);

asyncResult.SetAsCompleted(result, false);
}
Expand Down Expand Up @@ -718,15 +710,7 @@ public IAsyncResult BeginDownloadFile(string path, Stream output, AsyncCallback
{
try
{
InternalDownloadFile(path, output, asyncResult, offset =>
{
asyncResult.Update(offset);

if (downloadCallback != null)
{
downloadCallback(offset);
}
});
InternalDownloadFile(path, output, asyncResult, downloadCallback);

asyncResult.SetAsCompleted(null, false);
}
Expand Down Expand Up @@ -942,16 +926,7 @@ public IAsyncResult BeginUploadFile(Stream input, string path, bool canOverride,
{
try
{
InternalUploadFile(input, path, flags, asyncResult, offset =>
{
asyncResult.Update(offset);

if (uploadCallback != null)
{
uploadCallback(offset);
}

});
InternalUploadFile(input, path, flags, asyncResult, uploadCallback);

asyncResult.SetAsCompleted(null, false);
}
Expand Down Expand Up @@ -1919,7 +1894,7 @@ private IEnumerable<FileInfo> InternalSynchronizeDirectories(string sourcePath,

#region Existing Files at The Destination

var destFiles = InternalListDirectory(destinationPath, null);
var destFiles = InternalListDirectory(destinationPath, null, null);
var destDict = new Dictionary<string, SftpFile>();
foreach (var destFile in destFiles)
{
Expand Down Expand Up @@ -1980,13 +1955,14 @@ private IEnumerable<FileInfo> InternalSynchronizeDirectories(string sourcePath,
/// Internals the list directory.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="asyncResult">An <see cref="IAsyncResult"/> that references the asynchronous request.</param>
/// <param name="listCallback">The list callback.</param>
/// <returns>
/// A list of files in the specfied directory.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="path" /> is <b>null</b>.</exception>
/// <exception cref="SshConnectionException">Client not connected.</exception>
private IEnumerable<SftpFile> InternalListDirectory(string path, Action<int> listCallback)
private IEnumerable<SftpFile> InternalListDirectory(string path, SftpListDirectoryAsyncResult asyncResult, Action<int> listCallback)
{
if (path == null)
throw new ArgumentNullException("path");
Expand All @@ -2012,6 +1988,9 @@ private IEnumerable<SftpFile> InternalListDirectory(string path, Action<int> lis
result.AddRange(from f in files
select new SftpFile(_sftpSession, string.Format(CultureInfo.InvariantCulture, "{0}{1}", basePath, f.Key), f.Value));

if (asyncResult != null)
asyncResult.Update(result.Count);

// Call callback to report number of files read
if (listCallback != null)
{
Expand Down Expand Up @@ -2068,6 +2047,9 @@ private void InternalDownloadFile(string path, Stream output, SftpDownloadAsyncR

totalBytesRead += (ulong) data.Length;

if (asyncResult != null)
asyncResult.Update(totalBytesRead);

if (downloadCallback != null)
{
// copy offset to ensure it's not modified between now and execution of callback
Expand Down Expand Up @@ -2124,14 +2106,16 @@ private void InternalUploadFile(Stream input, string path, Flags flags, SftpUplo
if (bytesRead > 0)
{
var writtenBytes = offset + (ulong) bytesRead;

_sftpSession.RequestWrite(handle, offset, buffer, 0, bytesRead, null, s =>
{
if (s.StatusCode == StatusCodes.Ok)
{
Interlocked.Decrement(ref expectedResponses);
responseReceivedWaitHandle.Set();

if (asyncResult != null)
asyncResult.Update(writtenBytes);

// Call callback to report number of bytes written
if (uploadCallback != null)
{
Expand Down