PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/ABB.SrcML/FileSystemFolderMonitor.cs

https://github.com/nkcsgexi/SrcML.NET
C# | 163 lines | 83 code | 19 blank | 61 comment | 5 complexity | 50083f81f041a7db243bc58cefc98868 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.IO;
  7. using System.Collections.ObjectModel;
  8. namespace ABB.SrcML {
  9. /// <summary>
  10. /// An implementation of <see cref="AbstractFileMonitor"/> that responds to file system events.
  11. /// </summary>
  12. public class FileSystemFolderMonitor : AbstractFileMonitor {
  13. private DirectoryInfo _folderInfo;
  14. private DirectoryInfo _monitorStorageInfo;
  15. private FileSystemWatcher _directoryWatcher;
  16. /// <summary>
  17. /// Creates a new file system monitor
  18. /// </summary>
  19. /// <param name="pathToSourceFolder">The folder to watch</param>
  20. /// <param name="monitoringStorage">The base directory for the archive data</param>
  21. /// <param name="defaultArchive">The default archive</param>
  22. /// <param name="otherArchives">Other archives to register</param>
  23. public FileSystemFolderMonitor(string pathToSourceFolder, string monitoringStorage, IArchive defaultArchive, params IArchive[] otherArchives)
  24. : base(monitoringStorage, defaultArchive, otherArchives) {
  25. this.FullFolderPath = pathToSourceFolder;
  26. this._monitorStorageInfo = new DirectoryInfo(monitoringStorage);
  27. SetupFileSystemWatcher();
  28. }
  29. /// <summary>
  30. /// The full path to the folder being monitored
  31. /// </summary>
  32. public string FullFolderPath {
  33. get {
  34. return this._folderInfo.FullName;
  35. }
  36. set {
  37. this._folderInfo = new DirectoryInfo(value);
  38. }
  39. }
  40. #region AbstractArchive Members
  41. /// <summary>
  42. /// Start monitoring
  43. /// </summary>
  44. public override void StartMonitoring() {
  45. this._directoryWatcher.EnableRaisingEvents = true;
  46. }
  47. /// <summary>
  48. /// Stop monitoring
  49. /// </summary>
  50. public override void StopMonitoring() {
  51. this._directoryWatcher.EnableRaisingEvents = false;
  52. }
  53. /// <summary>
  54. /// Get files from folder
  55. /// </summary>
  56. /// <returns></returns>
  57. public override Collection<string> GetFilesFromSource() {
  58. var filePaths = from file in this._folderInfo.GetFiles("*", SearchOption.AllDirectories)
  59. select file.FullName;
  60. return new Collection<string>(filePaths.ToList<string>());
  61. }
  62. #endregion
  63. /// <summary>
  64. /// Sets up the internal file system monitor
  65. /// </summary>
  66. private void SetupFileSystemWatcher() {
  67. this._directoryWatcher = new FileSystemWatcher(this.FullFolderPath);
  68. this._directoryWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.Attributes;
  69. this._directoryWatcher.IncludeSubdirectories = true;
  70. this._directoryWatcher.Changed += HandleFileChanged;
  71. this._directoryWatcher.Created += HandleFileCreated;
  72. this._directoryWatcher.Deleted += HandleFileDeleted;
  73. this._directoryWatcher.Error += HandleFileWatcherError;
  74. this._directoryWatcher.Renamed += HandleFileRenamed;
  75. }
  76. /// <summary>
  77. /// Respond to a file-changed event
  78. /// </summary>
  79. /// <param name="sender">The sender</param>
  80. /// <param name="e">event arguments</param>
  81. void HandleFileChanged(object sender, FileSystemEventArgs e) {
  82. if(IsNotInMonitoringStorage(e.FullPath)) {
  83. UpdateFile(e.FullPath);
  84. }
  85. }
  86. /// <summary>
  87. /// Respond to a file-created event
  88. /// </summary>
  89. /// <param name="sender">The sender</param>
  90. /// <param name="e">event arguments</param>
  91. void HandleFileCreated(object sender, FileSystemEventArgs e) {
  92. if(IsNotInMonitoringStorage(e.FullPath)) {
  93. AddFile(e.FullPath);
  94. }
  95. }
  96. /// <summary>
  97. /// Respond to a file-changed deleted
  98. /// </summary>
  99. /// <param name="sender">The sender</param>
  100. /// <param name="e">event arguments</param>
  101. void HandleFileDeleted(object sender, FileSystemEventArgs e) {
  102. if(IsNotInMonitoringStorage(e.FullPath)) {
  103. DeleteFile(e.FullPath);
  104. }
  105. }
  106. /// <summary>
  107. /// Respond to an error for the file system watcher. Not implemented.
  108. /// </summary>
  109. /// <param name="sender">The sender</param>
  110. /// <param name="e">event arguments</param>
  111. void HandleFileWatcherError(object sender, ErrorEventArgs e) {
  112. throw new NotImplementedException();
  113. }
  114. /// <summary>
  115. /// Respond to a file-rename event
  116. /// </summary>
  117. /// <param name="sender">The sender</param>
  118. /// <param name="e">event arguments</param>
  119. void HandleFileRenamed(object sender, RenamedEventArgs e) {
  120. if(IsNotInMonitoringStorage(e.FullPath)) {
  121. RenameFile(e.OldFullPath, e.FullPath);
  122. }
  123. }
  124. /// <summary>
  125. /// Checks if the path points to a file
  126. /// </summary>
  127. /// <param name="fullPath">the path to check</param>
  128. /// <returns>true if the path represents a file; false otherwise</returns>
  129. private static bool isFile(string fullPath) {
  130. if(!File.Exists(fullPath))
  131. return false;
  132. var pathAttributes = File.GetAttributes(fullPath);
  133. return !pathAttributes.HasFlag(FileAttributes.Directory);
  134. }
  135. /// <summary>
  136. /// Checks if the path is located within the archive directory
  137. /// </summary>
  138. /// <param name="filePath">the path to check</param>
  139. /// <returns>True if the path is in the <see cref="AbstractFileMonitor.MonitorStoragePath"/></returns>
  140. private bool IsNotInMonitoringStorage(string filePath) {
  141. return !Path.GetFullPath(filePath).StartsWith(_monitorStorageInfo.FullName, StringComparison.InvariantCultureIgnoreCase);
  142. }
  143. }
  144. }