| | | 1 | | using System; |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | using System.IO; |
| | | 4 | | using JetBrains.Annotations; |
| | | 5 | | using WireMock.Validation; |
| | | 6 | | |
| | | 7 | | namespace WireMock.Util |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// An EnhancedFileSystemWatcher, which can be used to suppress duplicate events that fire on a single change to the |
| | | 11 | | /// </summary> |
| | | 12 | | /// <seealso cref="FileSystemWatcher" /> |
| | | 13 | | public class EnhancedFileSystemWatcher : FileSystemWatcher |
| | | 14 | | { |
| | | 15 | | #region Private Members |
| | | 16 | | // Default Watch Interval in Milliseconds |
| | | 17 | | private const int DefaultWatchInterval = 100; |
| | | 18 | | |
| | | 19 | | // This Dictionary keeps the track of when an event occured last for a particular file |
| | | 20 | | private ConcurrentDictionary<string, DateTime> _lastFileEvent; |
| | | 21 | | |
| | | 22 | | // Watch Interval in Milliseconds |
| | | 23 | | private int _interval; |
| | | 24 | | |
| | | 25 | | // Timespan created when interval is set |
| | | 26 | | private TimeSpan _recentTimeSpan; |
| | | 27 | | #endregion |
| | | 28 | | |
| | | 29 | | #region Public Properties |
| | | 30 | | /// <summary> |
| | | 31 | | /// Interval, in milliseconds, within which events are considered "recent". |
| | | 32 | | /// </summary> |
| | | 33 | | [PublicAPI] |
| | | 34 | | public int Interval |
| | | 35 | | { |
| | 0 | 36 | | get => _interval; |
| | | 37 | | set |
| | 0 | 38 | | { |
| | 0 | 39 | | _interval = value; |
| | | 40 | | |
| | | 41 | | // Set timespan based on the value passed |
| | 0 | 42 | | _recentTimeSpan = new TimeSpan(0, 0, 0, 0, value); |
| | 0 | 43 | | } |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Allows user to set whether to filter recent events. |
| | | 48 | | /// If this is set a false, this class behaves like System.IO.FileSystemWatcher class. |
| | | 49 | | /// </summary> |
| | | 50 | | [PublicAPI] |
| | 0 | 51 | | public bool FilterRecentEvents { get; set; } |
| | | 52 | | #endregion |
| | | 53 | | |
| | | 54 | | #region Constructors |
| | | 55 | | /// <summary> |
| | | 56 | | /// Initializes a new instance of the <see cref="EnhancedFileSystemWatcher"/> class. |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="interval">The interval.</param> |
| | 0 | 59 | | public EnhancedFileSystemWatcher(int interval = DefaultWatchInterval) |
| | 0 | 60 | | { |
| | 0 | 61 | | Check.Condition(interval, i => i >= 0, nameof(interval)); |
| | | 62 | | |
| | 0 | 63 | | InitializeMembers(interval); |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Initializes a new instance of the <see cref="EnhancedFileSystemWatcher"/> class. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="path">The directory to monitor, in standard or Universal Naming Convention (UNC) notation.</par |
| | | 70 | | /// <param name="interval">The interval.</param> |
| | 0 | 71 | | public EnhancedFileSystemWatcher([NotNull] string path, int interval = DefaultWatchInterval) : base(path) |
| | 0 | 72 | | { |
| | 0 | 73 | | Check.NotNullOrEmpty(path, nameof(path)); |
| | 0 | 74 | | Check.Condition(interval, i => i >= 0, nameof(interval)); |
| | | 75 | | |
| | 0 | 76 | | InitializeMembers(interval); |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Initializes a new instance of the <see cref="EnhancedFileSystemWatcher"/> class. |
| | | 81 | | /// </summary> |
| | | 82 | | /// <param name="path">The directory to monitor, in standard or Universal Naming Convention (UNC) notation.</par |
| | | 83 | | /// <param name="filter">The type of files to watch. For example, "*.txt" watches for changes to all text files. |
| | | 84 | | /// <param name="interval">The interval.</param> |
| | 0 | 85 | | public EnhancedFileSystemWatcher([NotNull] string path, [NotNull] string filter, int interval = DefaultWatchInte |
| | 0 | 86 | | { |
| | 0 | 87 | | Check.NotNullOrEmpty(path, nameof(path)); |
| | 0 | 88 | | Check.NotNullOrEmpty(filter, nameof(filter)); |
| | 0 | 89 | | Check.Condition(interval, i => i >= 0, nameof(interval)); |
| | | 90 | | |
| | 0 | 91 | | InitializeMembers(interval); |
| | 0 | 92 | | } |
| | | 93 | | #endregion |
| | | 94 | | |
| | | 95 | | #region Events |
| | | 96 | | // These events hide the events from the base class. |
| | | 97 | | // We want to raise these events appropriately and we do not want the |
| | | 98 | | // users of this class subscribing to these events of the base class accidentally |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Occurs when a file or directory in the specified <see cref="P:System.IO.FileSystemWatcher.Path" /> is change |
| | | 102 | | /// </summary> |
| | | 103 | | public new event FileSystemEventHandler Changed; |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Occurs when a file or directory in the specified <see cref="P:System.IO.FileSystemWatcher.Path" /> is create |
| | | 107 | | /// </summary> |
| | | 108 | | public new event FileSystemEventHandler Created; |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Occurs when a file or directory in the specified <see cref="P:System.IO.FileSystemWatcher.Path" /> is delete |
| | | 112 | | /// </summary> |
| | | 113 | | public new event FileSystemEventHandler Deleted; |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Occurs when a file or directory in the specified <see cref="P:System.IO.FileSystemWatcher.Path" /> is rename |
| | | 117 | | /// </summary> |
| | | 118 | | public new event RenamedEventHandler Renamed; |
| | | 119 | | #endregion |
| | | 120 | | |
| | | 121 | | #region Protected Methods to raise the Events for this class |
| | | 122 | | /// <summary> |
| | | 123 | | /// Raises the <see cref="E:System.IO.FileSystemWatcher.Changed" /> event. |
| | | 124 | | /// </summary> |
| | | 125 | | /// <param name="e">A <see cref="T:System.IO.FileSystemEventArgs" /> that contains the event data.</param> |
| | | 126 | | protected new virtual void OnChanged(FileSystemEventArgs e) |
| | 0 | 127 | | { |
| | 0 | 128 | | Changed?.Invoke(this, e); |
| | 0 | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Raises the <see cref="E:System.IO.FileSystemWatcher.Created" /> event. |
| | | 133 | | /// </summary> |
| | | 134 | | /// <param name="e">A <see cref="T:System.IO.FileSystemEventArgs" /> that contains the event data.</param> |
| | | 135 | | protected new virtual void OnCreated(FileSystemEventArgs e) |
| | 0 | 136 | | { |
| | 0 | 137 | | Created?.Invoke(this, e); |
| | 0 | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Raises the <see cref="E:System.IO.FileSystemWatcher.Deleted" /> event. |
| | | 142 | | /// </summary> |
| | | 143 | | /// <param name="e">A <see cref="T:System.IO.FileSystemEventArgs" /> that contains the event data.</param> |
| | | 144 | | protected new virtual void OnDeleted(FileSystemEventArgs e) |
| | 0 | 145 | | { |
| | 0 | 146 | | Deleted?.Invoke(this, e); |
| | 0 | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Raises the <see cref="E:System.IO.FileSystemWatcher.Renamed" /> event. |
| | | 151 | | /// </summary> |
| | | 152 | | /// <param name="e">A <see cref="T:System.IO.RenamedEventArgs" /> that contains the event data.</param> |
| | | 153 | | protected new virtual void OnRenamed(RenamedEventArgs e) |
| | 0 | 154 | | { |
| | 0 | 155 | | Renamed?.Invoke(this, e); |
| | 0 | 156 | | } |
| | | 157 | | #endregion |
| | | 158 | | |
| | | 159 | | #region Private Methods |
| | | 160 | | /// <summary> |
| | | 161 | | /// This Method Initializes the private members. |
| | | 162 | | /// Interval is set to its default value of 100 millisecond. |
| | | 163 | | /// FilterRecentEvents is set to true, _lastFileEvent dictionary is initialized. |
| | | 164 | | /// We subscribe to the base class events. |
| | | 165 | | /// </summary> |
| | | 166 | | private void InitializeMembers(int interval = 100) |
| | 0 | 167 | | { |
| | 0 | 168 | | Interval = interval; |
| | 0 | 169 | | FilterRecentEvents = true; |
| | 0 | 170 | | _lastFileEvent = new ConcurrentDictionary<string, DateTime>(); |
| | | 171 | | |
| | 0 | 172 | | base.Created += OnCreated; |
| | 0 | 173 | | base.Changed += OnChanged; |
| | 0 | 174 | | base.Deleted += OnDeleted; |
| | 0 | 175 | | base.Renamed += OnRenamed; |
| | 0 | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// This method searches the dictionary to find out when the last event occured |
| | | 180 | | /// for a particular file. If that event occured within the specified timespan |
| | | 181 | | /// it returns true, else false |
| | | 182 | | /// </summary> |
| | | 183 | | /// <param name="fileName">The filename to be checked</param> |
| | | 184 | | /// <returns>True if an event has occured within the specified interval, False otherwise</returns> |
| | | 185 | | private bool HasAnotherFileEventOccuredRecently(string fileName) |
| | 0 | 186 | | { |
| | | 187 | | // Check dictionary only if user wants to filter recent events otherwise return value stays false. |
| | 0 | 188 | | if (!FilterRecentEvents) |
| | 0 | 189 | | { |
| | 0 | 190 | | return false; |
| | | 191 | | } |
| | | 192 | | |
| | 0 | 193 | | bool retVal = false; |
| | 0 | 194 | | if (_lastFileEvent.ContainsKey(fileName)) |
| | 0 | 195 | | { |
| | | 196 | | // If dictionary contains the filename, check how much time has elapsed |
| | | 197 | | // since the last event occured. If the timespan is less that the |
| | | 198 | | // specified interval, set return value to true |
| | | 199 | | // and store current datetime in dictionary for this file |
| | 0 | 200 | | DateTime lastEventTime = _lastFileEvent[fileName]; |
| | 0 | 201 | | DateTime currentTime = DateTime.Now; |
| | 0 | 202 | | TimeSpan timeSinceLastEvent = currentTime - lastEventTime; |
| | 0 | 203 | | retVal = timeSinceLastEvent < _recentTimeSpan; |
| | 0 | 204 | | _lastFileEvent[fileName] = currentTime; |
| | 0 | 205 | | } |
| | | 206 | | else |
| | 0 | 207 | | { |
| | | 208 | | // If dictionary does not contain the filename, |
| | | 209 | | // no event has occured in past for this file, so set return value to false |
| | | 210 | | // and append filename along with current datetime to the dictionary |
| | 0 | 211 | | _lastFileEvent.TryAdd(fileName, DateTime.Now); |
| | 0 | 212 | | } |
| | | 213 | | |
| | 0 | 214 | | return retVal; |
| | 0 | 215 | | } |
| | | 216 | | |
| | | 217 | | #region FileSystemWatcher EventHandlers |
| | | 218 | | // Base class Event Handlers. Check if an event has occured recently and call method |
| | | 219 | | // to raise appropriate event only if no recent event is detected |
| | | 220 | | private void OnChanged(object sender, FileSystemEventArgs e) |
| | 0 | 221 | | { |
| | 0 | 222 | | if (!HasAnotherFileEventOccuredRecently(e.FullPath)) |
| | 0 | 223 | | { |
| | 0 | 224 | | OnChanged(e); |
| | 0 | 225 | | } |
| | 0 | 226 | | } |
| | | 227 | | |
| | | 228 | | private void OnCreated(object sender, FileSystemEventArgs e) |
| | 0 | 229 | | { |
| | 0 | 230 | | if (!HasAnotherFileEventOccuredRecently(e.FullPath)) |
| | 0 | 231 | | { |
| | 0 | 232 | | OnCreated(e); |
| | 0 | 233 | | } |
| | 0 | 234 | | } |
| | | 235 | | |
| | | 236 | | private void OnDeleted(object sender, FileSystemEventArgs e) |
| | 0 | 237 | | { |
| | 0 | 238 | | if (!HasAnotherFileEventOccuredRecently(e.FullPath)) |
| | 0 | 239 | | { |
| | 0 | 240 | | OnDeleted(e); |
| | 0 | 241 | | } |
| | 0 | 242 | | } |
| | | 243 | | |
| | | 244 | | private void OnRenamed(object sender, RenamedEventArgs e) |
| | 0 | 245 | | { |
| | 0 | 246 | | if (!HasAnotherFileEventOccuredRecently(e.OldFullPath)) |
| | 0 | 247 | | { |
| | 0 | 248 | | OnRenamed(e); |
| | 0 | 249 | | } |
| | 0 | 250 | | } |
| | | 251 | | #endregion |
| | | 252 | | #endregion |
| | | 253 | | } |
| | | 254 | | } |