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 ()
67 public static bool GetInstance (out IFileWatcher watcher)
69 if (instance != null) {
74 instance = new DefaultWatcher ();
79 public void StartDispatching (FileSystemWatcher fsw)
81 DefaultWatcherData data;
84 watches = new Hashtable ();
87 thread = new Thread (new ThreadStart (Monitor));
88 thread.IsBackground = true;
94 data = (DefaultWatcherData) watches [fsw];
96 data = new DefaultWatcherData ();
97 data.Files = new Hashtable ();
102 data.Directory = fsw.FullPath;
103 data.NoWildcards = !fsw.Pattern.HasWildcard;
104 if (data.NoWildcards)
105 data.FileMask = Path.Combine (data.Directory, fsw.MangledFilter);
107 data.FileMask = fsw.MangledFilter;
109 data.IncludeSubdirs = fsw.IncludeSubdirectories;
111 data.DisabledTime = DateTime.MaxValue;
112 UpdateDataAndDispatch (data, false);
116 public void StopDispatching (FileSystemWatcher fsw)
118 DefaultWatcherData data;
120 if (watches == null) return;
124 data = (DefaultWatcherData) watches [fsw];
126 data.Enabled = false;
127 data.DisabledTime = DateTime.Now;
140 Hashtable my_watches;
142 if (watches.Count == 0) {
148 my_watches = (Hashtable) watches.Clone ();
151 if (watches.Count != 0) {
153 foreach (DefaultWatcherData data in my_watches.Values) {
154 bool remove = UpdateDataAndDispatch (data, true);
157 watches.Remove (data.FSW);
167 bool UpdateDataAndDispatch (DefaultWatcherData data, bool dispatch)
170 return (data.DisabledTime != DateTime.MaxValue &&
171 (DateTime.Now - data.DisabledTime).TotalSeconds > 5);
174 DoFiles (data, data.Directory, dispatch);
178 static void DispatchEvents (FileSystemWatcher fsw, FileAction action, string filename)
180 RenamedEventArgs renamed = null;
183 fsw.DispatchEvents (action, filename, ref renamed);
186 System.Threading.Monitor.PulseAll (fsw);
191 static string [] NoStringsArray = new string [0];
192 void DoFiles (DefaultWatcherData data, string directory, bool dispatch)
194 bool direxists = Directory.Exists (directory);
195 if (direxists && data.IncludeSubdirs) {
196 foreach (string d in Directory.GetDirectories (directory))
197 DoFiles (data, d, dispatch);
200 string [] files = null;
202 files = NoStringsArray;
203 } else if (!data.NoWildcards) {
204 files = Directory.GetFileSystemEntries (directory, data.FileMask);
206 // The pattern does not have wildcards
207 if (File.Exists (data.FileMask) || Directory.Exists (data.FileMask))
208 files = new string [] { data.FileMask };
210 files = NoStringsArray;
213 /* Set all as untested */
214 foreach (string filename in data.Files.Keys) {
215 FileData fd = (FileData) data.Files [filename];
216 if (fd.Directory == directory)
221 foreach (string filename in files) {
222 FileData fd = (FileData) data.Files [filename];
225 data.Files.Add (filename, CreateFileData (directory, filename));
227 // The file might have been removed in the meanwhile
228 data.Files.Remove (filename);
233 DispatchEvents (data.FSW, FileAction.Added, filename);
234 } else if (fd.Directory == directory) {
235 fd.NotExists = false;
239 if (!dispatch) // We only initialize the file list
243 ArrayList removed = null;
244 foreach (string filename in data.Files.Keys) {
245 FileData fd = (FileData) data.Files [filename];
248 removed = new ArrayList ();
250 removed.Add (filename);
251 DispatchEvents (data.FSW, FileAction.Removed, filename);
255 if (removed != null) {
256 foreach (string filename in removed)
257 data.Files.Remove (filename);
263 foreach (string filename in data.Files.Keys) {
264 FileData fd = (FileData) data.Files [filename];
265 DateTime creation, write;
267 creation = File.GetCreationTime (filename);
268 write = File.GetLastWriteTime (filename);
272 removed = new ArrayList ();
274 removed.Add (filename);
275 DispatchEvents (data.FSW, FileAction.Removed, filename);
279 if (creation != fd.CreationTime || write != fd.LastWriteTime) {
280 fd.CreationTime = creation;
281 fd.LastWriteTime = write;
282 DispatchEvents (data.FSW, FileAction.Modified, filename);
286 if (removed != null) {
287 foreach (string filename in removed)
288 data.Files.Remove (filename);
293 static FileData CreateFileData (string directory, string filename)
295 FileData fd = new FileData ();
296 string fullpath = Path.Combine (directory, filename);
297 fd.Directory = directory;
298 fd.Attributes = File.GetAttributes (fullpath);
299 fd.CreationTime = File.GetCreationTime (fullpath);
300 fd.LastWriteTime = File.GetLastWriteTime (fullpath);