ComparisonDifference.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. namespace WinSCP
  5. {
  6. [Guid("B1DAE3A0-5E56-4001-88D8-786F68557E28")]
  7. [ComVisible(true)]
  8. public enum SynchronizationAction
  9. {
  10. UploadNew = 1,
  11. DownloadNew = 2,
  12. UploadUpdate = 3,
  13. DownloadUpdate = 4,
  14. DeleteRemote = 5,
  15. DeleteLocal = 6,
  16. };
  17. [Guid("2D6EFFB5-69BA-47AA-90E8-A92953E8B58A")]
  18. [ComVisible(true)]
  19. public sealed class ComparisonFileInfo
  20. {
  21. public string FileName { get; internal set; }
  22. public DateTime LastWriteTime { get; internal set; }
  23. public long Length { get; internal set; }
  24. public int Length32 { get { return GetLength32(); } }
  25. internal ComparisonFileInfo()
  26. {
  27. }
  28. private int GetLength32()
  29. {
  30. return Tools.LengthTo32Bit(Length);
  31. }
  32. }
  33. [Guid("97F5222E-9379-4C24-9E50-E93C7334BBD5")]
  34. [ClassInterface(Constants.ClassInterface)]
  35. [ComVisible(true)]
  36. public sealed class ComparisonDifference
  37. {
  38. public SynchronizationAction Action { get; internal set; }
  39. public bool IsDirectory { get; internal set; }
  40. public ComparisonFileInfo Local { get; internal set; }
  41. public ComparisonFileInfo Remote { get; internal set; }
  42. internal ComparisonDifference(string localPath, string remotePath)
  43. {
  44. _localPath = localPath;
  45. _remotePath = remotePath;
  46. }
  47. public FileOperationEventArgs Resolve(Session session, TransferOptions options = null)
  48. {
  49. if (session == null)
  50. {
  51. throw new ArgumentNullException(nameof(session));
  52. }
  53. switch (Action)
  54. {
  55. case SynchronizationAction.UploadNew:
  56. case SynchronizationAction.UploadUpdate:
  57. {
  58. string remoteDirectory =
  59. RemotePath.TranslateLocalPathToRemote(Path.GetDirectoryName(Local.FileName), _localPath, _remotePath);
  60. if (!IsDirectory)
  61. {
  62. return session.PutFileToDirectory(Local.FileName, remoteDirectory, options: options);
  63. }
  64. else
  65. {
  66. session.PutEntryToDirectory(Local.FileName, remoteDirectory, options: options);
  67. return null;
  68. }
  69. }
  70. case SynchronizationAction.DownloadNew:
  71. case SynchronizationAction.DownloadUpdate:
  72. {
  73. string localDirectory =
  74. RemotePath.TranslateRemotePathToLocal(RemotePath.GetDirectoryName(Remote.FileName), _remotePath, _localPath);
  75. if (!IsDirectory)
  76. {
  77. return session.GetFileToDirectory(Remote.FileName, localDirectory, options: options);
  78. }
  79. else
  80. {
  81. session.GetEntryToDirectory(Remote.FileName, localDirectory, options: options);
  82. return null;
  83. }
  84. }
  85. case SynchronizationAction.DeleteRemote:
  86. if (!IsDirectory)
  87. {
  88. return session.RemoveFile(Remote.FileName);
  89. }
  90. else
  91. {
  92. session.RemoveEntry(Remote.FileName);
  93. return null;
  94. }
  95. case SynchronizationAction.DeleteLocal:
  96. if (!IsDirectory)
  97. {
  98. File.Delete(Local.FileName);
  99. }
  100. else
  101. {
  102. Directory.Delete(Local.FileName, true);
  103. }
  104. return null;
  105. default:
  106. throw session.Logger.WriteException(new InvalidOperationException());
  107. }
  108. }
  109. public void Reverse()
  110. {
  111. switch (Action)
  112. {
  113. case SynchronizationAction.UploadNew:
  114. Action = SynchronizationAction.DeleteLocal;
  115. break;
  116. case SynchronizationAction.DownloadNew:
  117. Action = SynchronizationAction.DeleteRemote;
  118. break;
  119. case SynchronizationAction.UploadUpdate:
  120. Action = SynchronizationAction.DownloadUpdate;
  121. break;
  122. case SynchronizationAction.DownloadUpdate:
  123. Action = SynchronizationAction.UploadUpdate;
  124. break;
  125. case SynchronizationAction.DeleteRemote:
  126. Action = SynchronizationAction.DownloadNew;
  127. break;
  128. case SynchronizationAction.DeleteLocal:
  129. Action = SynchronizationAction.UploadNew;
  130. break;
  131. default:
  132. throw new InvalidOperationException();
  133. }
  134. }
  135. private readonly string _localPath;
  136. private readonly string _remotePath;
  137. }
  138. }