2 // FileChangeNotificationSystemEntry.cs
5 // Marek Habersack <mhabersack@novell.com>
7 // Copyright (C) 2010 Novell, Inc. (http://novell.com/)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections.Generic;
31 using System.Runtime.Caching.Hosting;
34 namespace System.Runtime.Caching
36 // This class is NOT thread-safe - the caller needs to assure access serialization when
38 sealed class FileChangeNotificationSystemEntry : IDisposable
40 string watchedDirPath;
41 FileChangeNotificationSystem owner;
42 FileSystemWatcher watcher;
43 SortedDictionary <string, bool> filePaths;
45 SortedDictionary <string, bool> FilePaths {
47 if (filePaths == null)
48 filePaths = new SortedDictionary <string, bool> (Helpers.StringComparer);
54 get { return (filePaths == null || filePaths.Count == 0); }
57 public FileChangeNotificationSystemEntry (string dirPath, FileChangeNotificationSystem owner)
59 if (String.IsNullOrEmpty (dirPath))
60 throw new ArgumentNullException ("dirPath");
63 throw new ArgumentNullException ("owner");
65 watchedDirPath = dirPath;
69 public void Add (string filePath)
71 if (String.IsNullOrEmpty (filePath))
76 if (watcher == null) {
77 watcher = new FileSystemWatcher (watchedDirPath);
78 watcher.IncludeSubdirectories = false;
79 watcher.Filter = String.Empty;
80 watcher.NotifyFilter = NotifyFilters.FileName |
81 NotifyFilters.DirectoryName |
83 NotifyFilters.LastWrite |
84 NotifyFilters.CreationTime;
85 watcher.Changed += OnChanged;
86 watcher.Created += OnChanged;
87 watcher.Deleted += OnChanged;
88 watcher.Renamed += OnRenamed;
89 } else if (watcher.EnableRaisingEvents) {
94 SortedDictionary <string, bool> filePaths = FilePaths;
95 if (filePaths.ContainsKey (filePath))
98 filePaths.Add (filePath, true);
105 public void Remove (string filePath)
107 if (filePath == null)
112 if (watcher != null && watcher.EnableRaisingEvents) {
117 SortedDictionary <string, bool> filePaths = FilePaths;
118 if (!filePaths.ContainsKey (filePath))
121 filePaths.Remove (filePath);
133 watcher.EnableRaisingEvents = true;
141 watcher.EnableRaisingEvents = false;
144 public void Dispose ()
156 void IDisposable.Dispose ()
161 void OnChanged (object source, FileSystemEventArgs e)
163 string fullPath = e.FullPath;
164 if (owner == null || filePaths == null || !filePaths.ContainsKey (fullPath))
167 owner.OnChanged (e.ChangeType, fullPath, null);
170 void OnRenamed (object source, RenamedEventArgs e)
172 string fullPath = e.FullPath;
173 if (owner == null || filePaths == null || !filePaths.ContainsKey (fullPath))
176 owner.OnChanged (e.ChangeType, e.OldFullPath, e.FullPath);