2004-11-29 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System / System.IO / DefaultWatcher.cs
1 // 
2 // System.IO.DefaultWatcher.cs: default IFileWatcher
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (c) 2004 Novell, Inc. (http://www.novell.com)
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System;
32 using System.Collections;
33 using System.IO;
34 using System.Threading;
35
36 namespace System.IO {
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;
42                 public bool Enabled;
43                 public bool NoWildcards;
44                 public DateTime DisabledTime;
45                 public Hashtable Files;
46         }
47
48         class FileData {
49                 public string Directory;
50                 public FileAttributes Attributes;
51                 public bool NotExists;
52                 public DateTime CreationTime;
53                 public DateTime LastWriteTime;
54         }
55
56         class DefaultWatcher : IFileWatcher
57         {
58                 static DefaultWatcher instance;
59                 static Thread thread;
60                 static Hashtable watches;
61
62                 private DefaultWatcher ()
63                 {
64                 }
65                 
66                 public static bool GetInstance (out IFileWatcher watcher)
67                 {
68                         lock (typeof (DefaultWatcher)) {
69                                 if (instance != null) {
70                                         watcher = instance;
71                                         return true;
72                                 }
73
74                                 instance = new DefaultWatcher ();
75                                 watcher = instance;
76                                 return true;
77                         }
78                 }
79                 
80                 public void StartDispatching (FileSystemWatcher fsw)
81                 {
82                         DefaultWatcherData data;
83                         lock (this) {
84                                 if (watches == null)
85                                         watches = new Hashtable ();
86
87                                 if (thread == null) {
88                                         thread = new Thread (new ThreadStart (Monitor));
89                                         thread.IsBackground = true;
90                                         thread.Start ();
91                                 }
92                         }
93
94                         lock (watches) {
95                                 data = (DefaultWatcherData) watches [fsw];
96                                 if (data == null) {
97                                         data = new DefaultWatcherData ();
98                                         data.Files = new Hashtable ();
99                                         watches [fsw] = data;
100                                 }
101
102                                 data.FSW = fsw;
103                                 data.Directory = fsw.FullPath;
104                                 data.NoWildcards = !fsw.Pattern.HasWildcard;
105                                 if (data.NoWildcards)
106                                         data.FileMask = Path.Combine (data.Directory, fsw.MangledFilter);
107                                 else
108                                         data.FileMask = fsw.MangledFilter;
109
110                                 data.IncludeSubdirs = fsw.IncludeSubdirectories;
111                                 data.Enabled = true;
112                                 data.DisabledTime = DateTime.MaxValue;
113                                 UpdateDataAndDispatch (data, false);
114                         }
115                 }
116
117                 public void StopDispatching (FileSystemWatcher fsw)
118                 {
119                         DefaultWatcherData data;
120                         lock (watches) {
121                                 data = (DefaultWatcherData) watches [fsw];
122                                 if (data != null) {
123                                         data.Enabled = false;
124                                         data.DisabledTime = DateTime.Now;
125                                 }
126                         }
127                 }
128
129
130                 void Monitor ()
131                 {
132                         int zeroes = 0;
133
134                         while (true) {
135                                 Thread.Sleep (750);
136                                 
137                                 Hashtable my_watches;
138                                 lock (watches) {
139                                         if (watches.Count == 0) {
140                                                 if (++zeroes == 20)
141                                                         break;
142                                                 continue;
143                                         }
144                                         
145                                         my_watches = (Hashtable) watches.Clone ();
146                                 }
147                                 
148                                 if (watches.Count != 0) {
149                                         zeroes = 0;
150                                         foreach (DefaultWatcherData data in my_watches.Values) {
151                                                 bool remove = UpdateDataAndDispatch (data, true);
152                                                 if (remove)
153                                                         lock (watches)
154                                                                 watches.Remove (data.FSW);
155                                         }
156                                 }
157                         }
158
159                         lock (this) {
160                                 thread = null;
161                         }
162                 }
163                 
164                 bool UpdateDataAndDispatch (DefaultWatcherData data, bool dispatch)
165                 {
166                         if (!data.Enabled) {
167                                 return (data.DisabledTime != DateTime.MaxValue &&
168                                         (DateTime.Now - data.DisabledTime).TotalSeconds > 5);
169                         }
170
171                         DoFiles (data, data.Directory, dispatch);
172                         return false;
173                 }
174
175                 static void DispatchEvents (FileSystemWatcher fsw, FileAction action, string filename)
176                 {
177                         RenamedEventArgs renamed = null;
178
179                         lock (fsw) {
180                                 fsw.DispatchEvents (action, filename, ref renamed);
181                                 if (fsw.Waiting) {
182                                         fsw.Waiting = false;
183                                         System.Threading.Monitor.PulseAll (fsw);
184                                 }
185                         }
186                 }
187
188                 static string [] NoStringsArray = new string [0];
189                 void DoFiles (DefaultWatcherData data, string directory, bool dispatch)
190                 {
191                         bool direxists = Directory.Exists (directory);
192                         if (direxists && data.IncludeSubdirs) {
193                                 foreach (string d in Directory.GetDirectories (directory))
194                                         DoFiles (data, d, dispatch);
195                         }
196
197                         string [] files = null;
198                         if (!direxists) {
199                                 files = NoStringsArray;
200                         } else if (!data.NoWildcards) {
201                                 files = Directory.GetFileSystemEntries (directory, data.FileMask);
202                         } else {
203                                 // The pattern does not have wildcards
204                                 if (File.Exists (data.FileMask) || Directory.Exists (data.FileMask))
205                                         files = new string [] { data.FileMask };
206                                 else
207                                         files = NoStringsArray;
208                         }
209
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)
214                                         fd.NotExists = true;
215                         }
216
217                         /* New files */
218                         foreach (string filename in files) {
219                                 FileData fd = (FileData) data.Files [filename];
220                                 if (fd == null) {
221                                         try {
222                                                 data.Files.Add (filename, CreateFileData (directory, filename));
223                                         } catch {
224                                                 // The file might have been removed in the meanwhile
225                                                 data.Files.Remove (filename);
226                                                 continue;
227                                         }
228                                         
229                                         if (dispatch)
230                                                 DispatchEvents (data.FSW, FileAction.Added, filename);
231                                 } else if (fd.Directory == directory) {
232                                         fd.NotExists = false;
233                                 }
234                         }
235
236                         if (!dispatch) // We only initialize the file list
237                                 return;
238
239                         /* Removed files */
240                         ArrayList removed = null;
241                         foreach (string filename in data.Files.Keys) {
242                                 FileData fd = (FileData) data.Files [filename];
243                                 if (fd.NotExists) {
244                                         if (removed == null)
245                                                 removed = new ArrayList ();
246
247                                         removed.Add (filename);
248                                         DispatchEvents (data.FSW, FileAction.Removed, filename);
249                                 }
250                         }
251
252                         if (removed != null) {
253                                 foreach (string filename in removed)
254                                         data.Files.Remove (filename);
255
256                                 removed = null;
257                         }
258
259                         /* Changed files */
260                         foreach (string filename in data.Files.Keys) {
261                                 FileData fd = (FileData) data.Files [filename];
262                                 DateTime creation, write;
263                                 try {
264                                         creation = File.GetCreationTime (filename);
265                                         write = File.GetLastWriteTime (filename);
266                                 } catch {
267                                         /* Deleted */
268                                         if (removed == null)
269                                                 removed = new ArrayList ();
270
271                                         removed.Add (filename);
272                                         DispatchEvents (data.FSW, FileAction.Removed, filename);
273                                         continue;
274                                 }
275                                 
276                                 if (creation != fd.CreationTime || write != fd.LastWriteTime) {
277                                         fd.CreationTime = creation;
278                                         fd.LastWriteTime = write;
279                                         DispatchEvents (data.FSW, FileAction.Modified, filename);
280                                 }
281                         }
282
283                         if (removed != null) {
284                                 foreach (string filename in removed)
285                                         data.Files.Remove (filename);
286                         }
287
288                 }
289
290                 static FileData CreateFileData (string directory, string filename)
291                 {
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);
298                         return fd;
299                 }
300         }
301 }
302