2 // System.IO.DefaultWatcher.cs: default IFileWatcher
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (c) 2004 Novell, Inc. (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
34 using System.Threading;
37 class DefaultWatcherData {
38 public FileSystemWatcher FSW;
39 public string Directory;
40 public string FileMask; // If NoWildcards, contains the full path to the file.
41 public bool IncludeSubdirs;
43 public bool NoWildcards;
44 public DateTime DisabledTime;
45 public Hashtable Files;
49 public string Directory;
50 public FileAttributes Attributes;
51 public bool NotExists;
52 public DateTime CreationTime;
53 public DateTime LastWriteTime;
56 class DefaultWatcher : IFileWatcher
58 static DefaultWatcher instance;
60 static Hashtable watches;
62 private DefaultWatcher ()
66 public static bool GetInstance (out IFileWatcher watcher)
68 lock (typeof (DefaultWatcher)) {
69 if (instance != null) {
74 instance = new DefaultWatcher ();
80 public void StartDispatching (FileSystemWatcher fsw)
82 DefaultWatcherData data;
85 watches = new Hashtable ();
88 thread = new Thread (new ThreadStart (Monitor));
89 thread.IsBackground = true;
95 data = (DefaultWatcherData) watches [fsw];
97 data = new DefaultWatcherData ();
98 data.Files = new Hashtable ();
103 data.Directory = fsw.FullPath;
104 data.NoWildcards = !fsw.Pattern.HasWildcard;
105 if (data.NoWildcards)
106 data.FileMask = Path.Combine (data.Directory, fsw.MangledFilter);
108 data.FileMask = fsw.MangledFilter;
110 data.IncludeSubdirs = fsw.IncludeSubdirectories;
112 data.DisabledTime = DateTime.MaxValue;
113 UpdateDataAndDispatch (data, false);
117 public void StopDispatching (FileSystemWatcher fsw)
119 DefaultWatcherData data;
121 data = (DefaultWatcherData) watches [fsw];
123 data.Enabled = false;
124 data.DisabledTime = DateTime.Now;
137 Hashtable my_watches;
139 if (watches.Count == 0) {
145 my_watches = (Hashtable) watches.Clone ();
148 if (watches.Count != 0) {
150 foreach (DefaultWatcherData data in my_watches.Values) {
151 bool remove = UpdateDataAndDispatch (data, true);
154 watches.Remove (data.FSW);
164 bool UpdateDataAndDispatch (DefaultWatcherData data, bool dispatch)
167 return (data.DisabledTime != DateTime.MaxValue &&
168 (DateTime.Now - data.DisabledTime).TotalSeconds > 5);
171 DoFiles (data, data.Directory, dispatch);
175 static void DispatchEvents (FileSystemWatcher fsw, FileAction action, string filename)
177 RenamedEventArgs renamed = null;
180 fsw.DispatchEvents (action, filename, ref renamed);
183 System.Threading.Monitor.PulseAll (fsw);
188 static string [] NoStringsArray = new string [0];
189 void DoFiles (DefaultWatcherData data, string directory, bool dispatch)
191 bool direxists = Directory.Exists (directory);
192 if (direxists && data.IncludeSubdirs) {
193 foreach (string d in Directory.GetDirectories (directory))
194 DoFiles (data, d, dispatch);
197 string [] files = null;
199 files = NoStringsArray;
200 } else if (!data.NoWildcards) {
201 files = Directory.GetFileSystemEntries (directory, data.FileMask);
203 // The pattern does not have wildcards
204 if (File.Exists (data.FileMask) || Directory.Exists (data.FileMask))
205 files = new string [] { data.FileMask };
207 files = NoStringsArray;
210 /* Set all as untested */
211 foreach (string filename in data.Files.Keys) {
212 FileData fd = (FileData) data.Files [filename];
213 if (fd.Directory == directory)
218 foreach (string filename in files) {
219 FileData fd = (FileData) data.Files [filename];
222 data.Files.Add (filename, CreateFileData (directory, filename));
224 // The file might have been removed in the meanwhile
225 data.Files.Remove (filename);
230 DispatchEvents (data.FSW, FileAction.Added, filename);
231 } else if (fd.Directory == directory) {
232 fd.NotExists = false;
236 if (!dispatch) // We only initialize the file list
240 ArrayList removed = null;
241 foreach (string filename in data.Files.Keys) {
242 FileData fd = (FileData) data.Files [filename];
245 removed = new ArrayList ();
247 removed.Add (filename);
248 DispatchEvents (data.FSW, FileAction.Removed, filename);
252 if (removed != null) {
253 foreach (string filename in removed)
254 data.Files.Remove (filename);
260 foreach (string filename in data.Files.Keys) {
261 FileData fd = (FileData) data.Files [filename];
262 DateTime creation, write;
264 creation = File.GetCreationTime (filename);
265 write = File.GetLastWriteTime (filename);
269 removed = new ArrayList ();
271 removed.Add (filename);
272 DispatchEvents (data.FSW, FileAction.Removed, filename);
276 if (creation != fd.CreationTime || write != fd.LastWriteTime) {
277 fd.CreationTime = creation;
278 fd.LastWriteTime = write;
279 DispatchEvents (data.FSW, FileAction.Modified, filename);
283 if (removed != null) {
284 foreach (string filename in removed)
285 data.Files.Remove (filename);
290 static FileData CreateFileData (string directory, string filename)
292 FileData fd = new FileData ();
293 string fullpath = Path.Combine (directory, filename);
294 fd.Directory = directory;
295 fd.Attributes = File.GetAttributes (fullpath);
296 fd.CreationTime = File.GetCreationTime (fullpath);
297 fd.LastWriteTime = File.GetLastWriteTime (fullpath);