|  | @@ -346,141 +346,6 @@ namespace Renci.SshNet
 | 
	
		
			
				|  |  |              return ar.EndInvoke();
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        #region SynchronizeDirectories
 | 
	
		
			
				|  |  | -#if (!WINDOWS_PHONE)
 | 
	
		
			
				|  |  | -        public IEnumerable<FileInfo> SynchronizeDirectories(string sourceDir
 | 
	
		
			
				|  |  | -            , string destDir
 | 
	
		
			
				|  |  | -            , string searchPattern)
 | 
	
		
			
				|  |  | -        {
 | 
	
		
			
				|  |  | -            return InternalSynchronizeDirectories(sourceDir, destDir, searchPattern, null);
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        public IAsyncResult BeginSynchronizeDirectories(string sourceDir
 | 
	
		
			
				|  |  | -            , string destDir
 | 
	
		
			
				|  |  | -            , string searchPattern, AsyncCallback asyncCallback, object state)
 | 
	
		
			
				|  |  | -        {
 | 
	
		
			
				|  |  | -            if (sourceDir == null)
 | 
	
		
			
				|  |  | -                throw new ArgumentNullException("sourceDir");
 | 
	
		
			
				|  |  | -            if (destDir.IsNullOrWhiteSpace())
 | 
	
		
			
				|  |  | -                throw new ArgumentException("destDir");
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            //  Ensure that connection is established.
 | 
	
		
			
				|  |  | -            this.EnsureConnection();
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            var asyncResult = new SftpSynchronizeDirectoriesAsyncResult(asyncCallback, state);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            this.ExecuteThread(() =>
 | 
	
		
			
				|  |  | -            {
 | 
	
		
			
				|  |  | -                try
 | 
	
		
			
				|  |  | -                {
 | 
	
		
			
				|  |  | -                    var result = this.InternalSynchronizeDirectories(sourceDir, destDir, searchPattern, asyncResult);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -                    asyncResult.SetAsCompleted(result, false);
 | 
	
		
			
				|  |  | -                }
 | 
	
		
			
				|  |  | -                catch (Exception exp)
 | 
	
		
			
				|  |  | -                {
 | 
	
		
			
				|  |  | -                    asyncResult.SetAsCompleted(exp, false);
 | 
	
		
			
				|  |  | -                }
 | 
	
		
			
				|  |  | -            });
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            return asyncResult;
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        public IEnumerable<FileInfo> EndSynchronizeDirectories(IAsyncResult asyncResult)
 | 
	
		
			
				|  |  | -        {
 | 
	
		
			
				|  |  | -            var ar = asyncResult as SftpSynchronizeDirectoriesAsyncResult;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            if (ar == null || ar.EndInvokeCalled)
 | 
	
		
			
				|  |  | -                throw new ArgumentException("Either the IAsyncResult object did not come from the corresponding async method on this type, or EndExecute was called multiple times with the same IAsyncResult.");
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            // Wait for operation to complete, then return result or throw exception
 | 
	
		
			
				|  |  | -            return ar.EndInvoke();
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        private IEnumerable<FileInfo> InternalSynchronizeDirectories(string sourceDir,
 | 
	
		
			
				|  |  | -            string destDir,
 | 
	
		
			
				|  |  | -            string searchPattern,
 | 
	
		
			
				|  |  | -            SftpSynchronizeDirectoriesAsyncResult asynchResult)
 | 
	
		
			
				|  |  | -        {
 | 
	
		
			
				|  |  | -            if (destDir.IsNullOrWhiteSpace())
 | 
	
		
			
				|  |  | -                throw new ArgumentException("destDir");
 | 
	
		
			
				|  |  | -            if (!Directory.Exists(sourceDir))
 | 
	
		
			
				|  |  | -                throw new FileNotFoundException(string.Format("Source directory not found: {0}", sourceDir));
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            IList<FileInfo> uploadedFiles = new List<FileInfo>();
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            DirectoryInfo source = new DirectoryInfo(sourceDir);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -#if (SILVERLIGHT)
 | 
	
		
			
				|  |  | -            var sourceFiles = source.EnumerateFiles(searchPattern);
 | 
	
		
			
				|  |  | -#else
 | 
	
		
			
				|  |  | -            var sourceFiles = source.GetFiles(searchPattern);
 | 
	
		
			
				|  |  | -#endif
 | 
	
		
			
				|  |  | -            if (sourceFiles == null || sourceFiles.Count() <= 0)
 | 
	
		
			
				|  |  | -                return uploadedFiles;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            try
 | 
	
		
			
				|  |  | -            {
 | 
	
		
			
				|  |  | -                #region Existing Files at The Destination
 | 
	
		
			
				|  |  | -                var destFiles = InternalListDirectory(destDir, null);
 | 
	
		
			
				|  |  | -                Dictionary<string, SftpFile> destDict = new Dictionary<string, SftpFile>();
 | 
	
		
			
				|  |  | -                foreach (var destFile in destFiles)
 | 
	
		
			
				|  |  | -                {
 | 
	
		
			
				|  |  | -                    if (destFile.IsDirectory)
 | 
	
		
			
				|  |  | -                        continue;
 | 
	
		
			
				|  |  | -                    destDict.Add(destFile.Name, destFile);
 | 
	
		
			
				|  |  | -                }
 | 
	
		
			
				|  |  | -                #endregion
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -                #region Upload the difference
 | 
	
		
			
				|  |  | -                string remoteFileName;
 | 
	
		
			
				|  |  | -                bool isDifferent = false;
 | 
	
		
			
				|  |  | -                Flags uploadFlag = Flags.Write | Flags.Truncate | Flags.CreateNewOrOpen;
 | 
	
		
			
				|  |  | -                foreach (var localFile in sourceFiles)
 | 
	
		
			
				|  |  | -                {
 | 
	
		
			
				|  |  | -                    isDifferent = !destDict.ContainsKey(localFile.Name);
 | 
	
		
			
				|  |  | -                    if (!isDifferent)
 | 
	
		
			
				|  |  | -                    {
 | 
	
		
			
				|  |  | -                        SftpFile temp = destDict[localFile.Name];
 | 
	
		
			
				|  |  | -                        //ltang: File exists at the destination => Using filesize to detect the difference
 | 
	
		
			
				|  |  | -                        isDifferent = localFile.Length != temp.Length;
 | 
	
		
			
				|  |  | -                    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -                    if (isDifferent)
 | 
	
		
			
				|  |  | -                    {
 | 
	
		
			
				|  |  | -                        remoteFileName = string.Format(CultureInfo.InvariantCulture, @"{0}/{1}", destDir, localFile.Name);
 | 
	
		
			
				|  |  | -                        try
 | 
	
		
			
				|  |  | -                        {
 | 
	
		
			
				|  |  | -                            using (var file = File.OpenRead(localFile.FullName))
 | 
	
		
			
				|  |  | -                            {
 | 
	
		
			
				|  |  | -                                InternalUploadFile(file, remoteFileName, null, uploadFlag);
 | 
	
		
			
				|  |  | -                            }
 | 
	
		
			
				|  |  | -                            uploadedFiles.Add(localFile);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -                            if (asynchResult != null)
 | 
	
		
			
				|  |  | -                            {
 | 
	
		
			
				|  |  | -                                asynchResult.Update(uploadedFiles.Count);
 | 
	
		
			
				|  |  | -                            }
 | 
	
		
			
				|  |  | -                        }
 | 
	
		
			
				|  |  | -                        catch (Exception ex)
 | 
	
		
			
				|  |  | -                        {
 | 
	
		
			
				|  |  | -                            throw new Exception(string.Format("Failed to upload {0} to {1}", localFile.FullName, remoteFileName), ex);
 | 
	
		
			
				|  |  | -                        }
 | 
	
		
			
				|  |  | -                    }
 | 
	
		
			
				|  |  | -                }
 | 
	
		
			
				|  |  | -                #endregion
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | -            catch
 | 
	
		
			
				|  |  | -            {
 | 
	
		
			
				|  |  | -                throw;
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | -            return uploadedFiles;
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -#endif        
 | 
	
		
			
				|  |  | -        #endregion
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |          /// <summary>
 | 
	
		
			
				|  |  |          /// Gets reference to remote file or directory.
 | 
	
		
			
				|  |  |          /// </summary>
 | 
	
	
		
			
				|  | @@ -1415,7 +1280,7 @@ namespace Renci.SshNet
 | 
	
		
			
				|  |  |                  if (bytesRead < this.BufferSize)
 | 
	
		
			
				|  |  |                  {
 | 
	
		
			
				|  |  |                      var data = new byte[bytesRead];
 | 
	
		
			
				|  |  | -                    Buffer.BlockCopy(buffer, 0, data, 0, bytesRead); 
 | 
	
		
			
				|  |  | +                    Buffer.BlockCopy(buffer, 0, data, 0, bytesRead);
 | 
	
		
			
				|  |  |                      using (var wait = new AutoResetEvent(false))
 | 
	
		
			
				|  |  |                      {
 | 
	
		
			
				|  |  |                          this._sftpSession.RequestWrite(handle, offset, data, wait);
 |