locking fixes
[mono.git] / mcs / class / System / System.IO / FileSystemWatcher.cs
1 // 
2 // System.IO.FileSystemWatcher.cs
3 //
4 // Authors:
5 //      Tim Coleman (tim@timcoleman.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // Copyright (C) Tim Coleman, 2002 
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
10 // (c) 2004 Novell, Inc. (http://www.novell.com)
11 //
12
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System;
35 using System.ComponentModel;
36 using System.Runtime.CompilerServices;
37 using System.Runtime.InteropServices;
38 using System.Threading;
39
40 namespace System.IO {
41         [DefaultEvent("Changed")]
42         public class FileSystemWatcher : Component, ISupportInitialize {
43
44                 #region Fields
45
46                 bool enableRaisingEvents;
47                 string filter;
48                 bool includeSubdirectories;
49                 int internalBufferSize;
50                 NotifyFilters notifyFilter;
51                 string path;
52                 string fullpath;
53                 ISynchronizeInvoke synchronizingObject;
54                 WaitForChangedResult lastData;
55                 bool waiting;
56                 SearchPattern2 pattern;
57                 bool disposed;
58                 string mangledFilter;
59                 static IFileWatcher watcher;
60                 static object lockobj = new object ();
61
62                 #endregion // Fields
63
64                 #region Constructors
65
66                 public FileSystemWatcher ()
67                 {
68                         this.notifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
69                         this.enableRaisingEvents = false;
70                         this.filter = "*.*";
71                         this.includeSubdirectories = false;
72                         this.internalBufferSize = 8192;
73                         this.path = "";
74                         InitWatcher ();
75                 }
76
77                 public FileSystemWatcher (string path)
78                         : this (path, "*.*")
79                 {
80                 }
81
82                 public FileSystemWatcher (string path, string filter)
83                 {
84                         if (path == null)
85                                 throw new ArgumentNullException ("path");
86
87                         if (filter == null)
88                                 throw new ArgumentNullException ("filter");
89
90                         if (path == String.Empty)
91                                 throw new ArgumentException ("Empty path", "path");
92
93                         if (!Directory.Exists (path))
94                                 throw new ArgumentException ("Directory does not exists", "path");
95
96                         this.enableRaisingEvents = false;
97                         this.filter = filter;
98                         this.includeSubdirectories = false;
99                         this.internalBufferSize = 8192;
100                         this.notifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
101                         this.path = path;
102                         this.synchronizingObject = null;
103                         InitWatcher ();
104                 }
105
106                 void InitWatcher ()
107                 {
108                         lock (lockobj) {
109                                 if (watcher != null)
110                                         return;
111
112                                 string managed = Environment.GetEnvironmentVariable ("MONO_MANAGED_WATCHER");
113                                 int mode = 0;
114                                 if (managed == null)
115                                         mode = InternalSupportsFSW ();
116
117                                 bool ok = false;
118                                 if (mode == 3)
119                                         ok = KeventWatcher.GetInstance (out watcher);
120                                 else if (mode == 2)
121                                         ok = FAMWatcher.GetInstance (out watcher);
122                                 else if (mode == 1)
123                                         ok = DefaultWatcher.GetInstance (out watcher);
124                                         //ok = WindowsWatcher.GetInstance (out watcher);
125
126                                 if (mode == 0 || !ok)
127                                         DefaultWatcher.GetInstance (out watcher);
128                         }
129                 }
130
131                 #endregion // Constructors
132
133                 #region Properties
134
135                 /* If this is enabled, we Pulse this instance */
136                 internal bool Waiting {
137                         get { return waiting; }
138                         set { waiting = value; }
139                 }
140
141                 internal string MangledFilter {
142                         get {
143                                 if (filter != "*.*")
144                                         return filter;
145
146                                 if (mangledFilter != null)
147                                         return mangledFilter;
148
149                                 string filterLocal = "*.*";
150                                 if (!(watcher.GetType () == typeof (WindowsWatcher)))
151                                         filterLocal = "*";
152
153                                 return filterLocal;
154                         }
155                 }
156
157                 internal SearchPattern2 Pattern {
158                         get {
159                                 if (pattern == null) {
160                                         pattern = new SearchPattern2 (MangledFilter);
161                                 }
162                                 return pattern;
163                         }
164                 }
165
166                 internal string FullPath {
167                         get {
168                                 if (fullpath == null) {
169                                         if (path == null || path == "")
170                                                 fullpath = Environment.CurrentDirectory;
171                                         else
172                                                 fullpath = System.IO.Path.GetFullPath (path);
173                                 }
174
175                                 return fullpath;
176                         }
177                 }
178
179                 [DefaultValue(false)]
180                 [IODescription("Flag to indicate if this instance is active")]
181                 public bool EnableRaisingEvents {
182                         get { return enableRaisingEvents; }
183                         set {
184                                 if (value == enableRaisingEvents)
185                                         return; // Do nothing
186
187                                 enableRaisingEvents = value;
188                                 if (value) {
189                                         Start ();
190                                 } else {
191                                         Stop ();
192                                 }
193                         }
194                 }
195
196                 [DefaultValue("*.*")]
197                 [IODescription("File name filter pattern")]
198                 [RecommendedAsConfigurable(true)]
199                 [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
200                 public string Filter {
201                         get { return filter; }
202                         set {
203                                 if (value == null || value == "")
204                                         value = "*.*";
205
206                                 if (filter != value) {
207                                         filter = value;
208                                         pattern = null;
209                                         mangledFilter = null;
210                                 }
211                         }
212                 }
213
214                 [DefaultValue(false)]
215                 [IODescription("Flag to indicate we want to watch subdirectories")]
216                 public bool IncludeSubdirectories {
217                         get { return includeSubdirectories; }
218                         set {
219                                 if (includeSubdirectories == value)
220                                         return;
221
222                                 includeSubdirectories = value;
223                                 if (value && enableRaisingEvents) {
224                                         Stop ();
225                                         Start ();
226                                 }
227                         }
228                 }
229
230                 [Browsable(false)]
231                 [DefaultValue(8192)]
232                 public int InternalBufferSize {
233                         get { return internalBufferSize; }
234                         set {
235                                 if (internalBufferSize == value)
236                                         return;
237
238                                 if (value < 4196)
239                                         value = 4196;
240
241                                 internalBufferSize = value;
242                                 if (enableRaisingEvents) {
243                                         Stop ();
244                                         Start ();
245                                 }
246                         }
247                 }
248
249                 [DefaultValue(NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite)]
250                 [IODescription("Flag to indicate which change event we want to monitor")]
251                 public NotifyFilters NotifyFilter {
252                         get { return notifyFilter; }
253                         set {
254                                 if (notifyFilter == value)
255                                         return;
256                                         
257                                 notifyFilter = value;
258                                 if (enableRaisingEvents) {
259                                         Stop ();
260                                         Start ();
261                                 }
262                         }
263                 }
264
265                 [DefaultValue("")]
266                 [IODescription("The directory to monitor")]
267                 [RecommendedAsConfigurable(true)]
268                 [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
269                 [Editor ("System.Diagnostics.Design.FSWPathEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
270                 public string Path {
271                         get { return path; }
272                         set {
273                                 if (path == value)
274                                         return;
275
276                                 bool exists = false;
277                                 Exception exc = null;
278
279                                 try {
280                                         exists = Directory.Exists (value);
281                                 } catch (Exception e) {
282                                         exc = e;
283                                 }
284
285                                 if (exc != null)
286                                         throw new ArgumentException ("Invalid directory name", "value", exc);
287
288                                 if (!exists)
289                                         throw new ArgumentException ("Directory does not exists", "value");
290
291                                 path = value;
292                                 fullpath = null;
293                                 if (enableRaisingEvents) {
294                                         Stop ();
295                                         Start ();
296                                 }
297                         }
298                 }
299
300                 [Browsable(false)]
301                 public override ISite Site {
302                         get { return base.Site; }
303                         set { base.Site = value; }
304                 }
305
306                 [DefaultValue(null)]
307                 [IODescription("The object used to marshal the event handler calls resulting from a directory change")]
308                 public ISynchronizeInvoke SynchronizingObject {
309                         get { return synchronizingObject; }
310                         set { synchronizingObject = value; }
311                 }
312
313                 #endregion // Properties
314
315                 #region Methods
316         
317                 [MonoTODO]
318                 public void BeginInit ()
319                 {
320                         throw new NotImplementedException (); 
321                 }
322
323                 protected override void Dispose (bool disposing)
324                 {
325                         if (!disposed) {
326                                 disposed = true;
327                                 Stop ();
328                         }
329
330                         base.Dispose (disposing);
331                 }
332
333                 ~FileSystemWatcher ()
334                 {
335                         disposed = true;
336                         Stop ();
337                 }
338                 
339                 [MonoTODO]
340                 public void EndInit ()
341                 {
342                         throw new NotImplementedException (); 
343                 }
344
345                 private void RaiseEvent (Delegate ev, EventArgs arg)
346                 {
347                         if (ev == null)
348                                 return;
349
350                         object [] args = new object [] {this, arg};
351
352                         if (synchronizingObject == null) {
353                                 ev.DynamicInvoke (args);
354                                 return;
355                         }
356                         
357                         synchronizingObject.BeginInvoke (ev, args);
358                 }
359
360                 protected void OnChanged (FileSystemEventArgs e)
361                 {
362                         RaiseEvent (Changed, e);
363                 }
364
365                 protected void OnCreated (FileSystemEventArgs e)
366                 {
367                         RaiseEvent (Created, e);
368                 }
369
370                 protected void OnDeleted (FileSystemEventArgs e)
371                 {
372                         RaiseEvent (Deleted, e);
373                 }
374
375                 protected void OnError (ErrorEventArgs e)
376                 {
377                         RaiseEvent (Error, e);
378                 }
379
380                 protected void OnRenamed (RenamedEventArgs e)
381                 {
382                         RaiseEvent (Renamed, e);
383                 }
384
385                 public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType)
386                 {
387                         return WaitForChanged (changeType, Timeout.Infinite);
388                 }
389
390                 public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType, int timeout)
391                 {
392                         WaitForChangedResult result = new WaitForChangedResult ();
393                         bool prevEnabled = EnableRaisingEvents;
394                         if (!prevEnabled)
395                                 EnableRaisingEvents = true;
396
397                         bool gotData;
398                         lock (this) {
399                                 waiting = true;
400                                 gotData = Monitor.Wait (this, timeout);
401                                 if (gotData)
402                                         result = this.lastData;
403                         }
404
405                         EnableRaisingEvents = prevEnabled;
406                         if (!gotData)
407                                 result.TimedOut = true;
408
409                         return result;
410                 }
411
412                 internal void DispatchEvents (FileAction act, string filename, ref RenamedEventArgs renamed)
413                 {
414                         if (waiting) {
415                                 lastData = new WaitForChangedResult ();
416                         }
417
418                         switch (act) {
419                         case FileAction.Added:
420                                 lastData.Name = filename;
421                                 lastData.ChangeType = WatcherChangeTypes.Created;
422                                 OnCreated (new FileSystemEventArgs (WatcherChangeTypes.Created, path, filename));
423                                 break;
424                         case FileAction.Removed:
425                                 lastData.Name = filename;
426                                 lastData.ChangeType = WatcherChangeTypes.Deleted;
427                                 OnDeleted (new FileSystemEventArgs (WatcherChangeTypes.Deleted, path, filename));
428                                 break;
429                         case FileAction.Modified:
430                                 lastData.Name = filename;
431                                 lastData.ChangeType = WatcherChangeTypes.Changed;
432                                 OnChanged (new FileSystemEventArgs (WatcherChangeTypes.Changed, path, filename));
433                                 break;
434                         case FileAction.RenamedOldName:
435                                 if (renamed != null) {
436                                         OnRenamed (renamed);
437                                 }
438                                 lastData.OldName = filename;
439                                 lastData.ChangeType = WatcherChangeTypes.Renamed;
440                                 renamed = new RenamedEventArgs (WatcherChangeTypes.Renamed, path, null, filename);
441                                 break;
442                         case FileAction.RenamedNewName:
443                                 lastData.Name = filename;
444                                 lastData.ChangeType = WatcherChangeTypes.Renamed;
445                                 if (renamed != null) {
446                                         renamed.SetName (filename);
447                                 } else {
448                                         renamed = new RenamedEventArgs (WatcherChangeTypes.Renamed, path, filename, null);
449                                 }
450                                 OnRenamed (renamed);
451                                 renamed = null;
452                                 break;
453                         default:
454                                 break;
455                         }
456                 }
457
458                 void Start ()
459                 {
460                         watcher.StartDispatching (this);
461                 }
462
463                 void Stop ()
464                 {
465                         watcher.StopDispatching (this);
466                 }
467                 #endregion // Methods
468
469                 #region Events and Delegates
470
471                 [IODescription("Occurs when a file/directory change matches the filter")]
472                 public event FileSystemEventHandler Changed;
473
474                 [IODescription("Occurs when a file/directory creation matches the filter")]
475                 public event FileSystemEventHandler Created;
476
477                 [IODescription("Occurs when a file/directory deletion matches the filter")]
478                 public event FileSystemEventHandler Deleted;
479
480                 [Browsable(false)]
481                 public event ErrorEventHandler Error;
482
483                 [IODescription("Occurs when a file/directory rename matches the filter")]
484                 public event RenamedEventHandler Renamed;
485
486                 #endregion // Events and Delegates
487
488                 /* 0 -> not supported   */
489                 /* 1 -> windows         */
490                 /* 2 -> FAM             */
491                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
492                 static extern int InternalSupportsFSW ();
493                 
494                 /*[MethodImplAttribute(MethodImplOptions.InternalCall)]
495                 static extern IntPtr InternalOpenDirectory (string path, IntPtr reserved);
496
497                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
498                 static extern IntPtr InternalCloseDirectory (IntPtr handle);
499
500                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
501                 static extern bool InternalReadDirectoryChanges (IntPtr handle,
502                                                                  byte [] buffer,
503                                                                  bool includeSubdirectories,
504                                                                  NotifyFilters notifyFilter,
505                                                                  out NativeOverlapped overlap,
506                                                                  OverlappedHandler overlappedCallback);
507
508                 */
509         }
510 }