SftpClient.NET.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using Renci.SshNet.Sftp;
  6. using System.Globalization;
  7. namespace Renci.SshNet
  8. {
  9. /// <summary>
  10. /// Implementation of the SSH File Transfer Protocol (SFTP) over SSH.
  11. /// </summary>
  12. public partial class SftpClient : BaseClient
  13. {
  14. #region SynchronizeDirectories
  15. /// <summary>
  16. /// Synchronizes the directories.
  17. /// </summary>
  18. /// <param name="sourcePath">The source path.</param>
  19. /// <param name="destinationPath">The destination path.</param>
  20. /// <param name="searchPattern">The search pattern.</param>
  21. /// <returns>List of uploaded files.</returns>
  22. public IEnumerable<FileInfo> SynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern)
  23. {
  24. return InternalSynchronizeDirectories(sourcePath, destinationPath, searchPattern, null);
  25. }
  26. /// <summary>
  27. /// Begins the synchronize directories.
  28. /// </summary>
  29. /// <param name="sourcePath">The source path.</param>
  30. /// <param name="destinationPath">The destination path.</param>
  31. /// <param name="searchPattern">The search pattern.</param>
  32. /// <param name="asyncCallback">The async callback.</param>
  33. /// <param name="state">The state.</param>
  34. /// <returns>
  35. /// An <see cref="System.IAsyncResult" /> that represents the asynchronous directory synchronization.
  36. /// </returns>
  37. /// <exception cref="System.ArgumentNullException"><paramref name="sourcePath"/> is <c>null</c>.</exception>
  38. /// <exception cref="System.ArgumentException"><paramref name="destinationPath"/> is <c>null</c> or contains only whitespace.</exception>
  39. public IAsyncResult BeginSynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern, AsyncCallback asyncCallback, object state)
  40. {
  41. if (sourcePath == null)
  42. throw new ArgumentNullException("sourcePath");
  43. if (destinationPath.IsNullOrWhiteSpace())
  44. throw new ArgumentException("destDir");
  45. var asyncResult = new SftpSynchronizeDirectoriesAsyncResult(asyncCallback, state);
  46. this.ExecuteThread(() =>
  47. {
  48. try
  49. {
  50. var result = this.InternalSynchronizeDirectories(sourcePath, destinationPath, searchPattern, asyncResult);
  51. asyncResult.SetAsCompleted(result, false);
  52. }
  53. catch (Exception exp)
  54. {
  55. asyncResult.SetAsCompleted(exp, false);
  56. }
  57. });
  58. return asyncResult;
  59. }
  60. /// <summary>
  61. /// Ends the synchronize directories.
  62. /// </summary>
  63. /// <param name="asyncResult">The async result.</param>
  64. /// <returns>List of uploaded files.</returns>
  65. /// <exception cref="System.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.</exception>
  66. public IEnumerable<FileInfo> EndSynchronizeDirectories(IAsyncResult asyncResult)
  67. {
  68. var ar = asyncResult as SftpSynchronizeDirectoriesAsyncResult;
  69. if (ar == null || ar.EndInvokeCalled)
  70. 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.");
  71. // Wait for operation to complete, then return result or throw exception
  72. return ar.EndInvoke();
  73. }
  74. private IEnumerable<FileInfo> InternalSynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern, SftpSynchronizeDirectoriesAsyncResult asynchResult)
  75. {
  76. if (destinationPath.IsNullOrWhiteSpace())
  77. throw new ArgumentException("destinationPath");
  78. if (!Directory.Exists(sourcePath))
  79. throw new FileNotFoundException(string.Format("Source directory not found: {0}", sourcePath));
  80. IList<FileInfo> uploadedFiles = new List<FileInfo>();
  81. DirectoryInfo sourceDirectory = new DirectoryInfo(sourcePath);
  82. #if SILVERLIGHT
  83. var sourceFiles = sourceDirectory.EnumerateFiles(searchPattern);
  84. #else
  85. var sourceFiles = sourceDirectory.GetFiles(searchPattern);
  86. #endif
  87. if (sourceFiles == null || !sourceFiles.Any())
  88. return uploadedFiles;
  89. #region Existing Files at The Destination
  90. var destFiles = InternalListDirectory(destinationPath, null);
  91. Dictionary<string, SftpFile> destDict = new Dictionary<string, SftpFile>();
  92. foreach (var destFile in destFiles)
  93. {
  94. if (destFile.IsDirectory)
  95. continue;
  96. destDict.Add(destFile.Name, destFile);
  97. }
  98. #endregion
  99. #region Upload the difference
  100. const Flags uploadFlag = Flags.Write | Flags.Truncate | Flags.CreateNewOrOpen;
  101. foreach (var localFile in sourceFiles)
  102. {
  103. bool isDifferent = !destDict.ContainsKey(localFile.Name);
  104. if (!isDifferent)
  105. {
  106. SftpFile temp = destDict[localFile.Name];
  107. // TODO: Use md5 to detect a difference
  108. //ltang: File exists at the destination => Using filesize to detect the difference
  109. isDifferent = localFile.Length != temp.Length;
  110. }
  111. if (isDifferent)
  112. {
  113. var remoteFileName = string.Format(CultureInfo.InvariantCulture, @"{0}/{1}", destinationPath, localFile.Name);
  114. try
  115. {
  116. using (var file = File.OpenRead(localFile.FullName))
  117. {
  118. this.InternalUploadFile(file, remoteFileName, uploadFlag, null, null);
  119. }
  120. uploadedFiles.Add(localFile);
  121. if (asynchResult != null)
  122. {
  123. asynchResult.Update(uploadedFiles.Count);
  124. }
  125. }
  126. catch (Exception ex)
  127. {
  128. throw new Exception(string.Format("Failed to upload {0} to {1}", localFile.FullName, remoteFileName), ex);
  129. }
  130. }
  131. }
  132. #endregion
  133. return uploadedFiles;
  134. }
  135. #endregion
  136. }
  137. }