2005-12-01 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[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                                 switch (mode) {
119                                 case 1: // windows
120                                         ok = DefaultWatcher.GetInstance (out watcher);
121                                         //ok = WindowsWatcher.GetInstance (out watcher);
122                                         break;
123                                 case 2: // libfam
124                                         ok = FAMWatcher.GetInstance (out watcher, false);
125                                         break;
126                                 case 3: // kevent
127                                         ok = KeventWatcher.GetInstance (out watcher);
128                                         break;
129                                 case 4: // libgamin
130                                         ok = FAMWatcher.GetInstance (out watcher, true);
131                                         break;
132                                 }
133
134                                 if (mode == 0 || !ok)
135                                         DefaultWatcher.GetInstance (out watcher);
136                         }
137                 }
138
139                 #endregion // Constructors
140
141                 #region Properties
142
143                 /* If this is enabled, we Pulse this instance */
144                 internal bool Waiting {
145                         get { return waiting; }
146                         set { waiting = value; }
147                 }
148
149                 internal string MangledFilter {
150                         get {
151                                 if (filter != "*.*")
152                                         return filter;
153
154                                 if (mangledFilter != null)
155                                         return mangledFilter;
156
157                                 string filterLocal = "*.*";
158                                 if (!(watcher.GetType () == typeof (WindowsWatcher)))
159                                         filterLocal = "*";
160
161                                 return filterLocal;
162                         }
163                 }
164
165                 internal SearchPattern2 Pattern {
166                         get {
167                                 if (pattern == null) {
168                                         pattern = new SearchPattern2 (MangledFilter);
169                                 }
170                                 return pattern;
171                         }
172                 }
173
174                 internal string FullPath {
175                         get {
176                                 if (fullpath == null) {
177                                         if (path == null || path == "")
178                                                 fullpath = Environment.CurrentDirectory;
179                                         else
180                                                 fullpath = System.IO.Path.GetFullPath (path);
181                                 }
182
183                                 return fullpath;
184                         }
185                 }
186
187                 [DefaultValue(false)]
188                 [IODescription("Flag to indicate if this instance is active")]
189                 public bool EnableRaisingEvents {
190                         get { return enableRaisingEvents; }
191                         set {
192                                 if (value == enableRaisingEvents)
193                                         return; // Do nothing
194
195                                 enableRaisingEvents = value;
196                                 if (value) {
197                                         Start ();
198                                 } else {
199                                         Stop ();
200                                 }
201                         }
202                 }
203
204                 [DefaultValue("*.*")]
205                 [IODescription("File name filter pattern")]
206                 [RecommendedAsConfigurable(true)]
207                 [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
208                 public string Filter {
209                         get { return filter; }
210                         set {
211                                 if (value == null || value == "")
212                                         value = "*.*";
213
214                                 if (filter != value) {
215                                         filter = value;
216                                         pattern = null;
217                                         mangledFilter = null;
218                                 }
219                         }
220                 }
221
222                 [DefaultValue(false)]
223                 [IODescription("Flag to indicate we want to watch subdirectories")]
224                 public bool IncludeSubdirectories {
225                         get { return includeSubdirectories; }
226                         set {
227                                 if (includeSubdirectories == value)
228                                         return;
229
230                                 includeSubdirectories = value;
231                                 if (value && enableRaisingEvents) {
232                                         Stop ();
233                                         Start ();
234                                 }
235                         }
236                 }
237
238                 [Browsable(false)]
239                 [DefaultValue(8192)]
240                 public int InternalBufferSize {
241                         get { return internalBufferSize; }
242                         set {
243                                 if (internalBufferSize == value)
244                                         return;
245
246                                 if (value < 4196)
247                                         value = 4196;
248
249                                 internalBufferSize = value;
250                                 if (enableRaisingEvents) {
251                                         Stop ();
252                                         Start ();
253                                 }
254                         }
255                 }
256
257                 [DefaultValue(NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite)]
258                 [IODescription("Flag to indicate which change event we want to monitor")]
259                 public NotifyFilters NotifyFilter {
260                         get { return notifyFilter; }
261                         set {
262                                 if (notifyFilter == value)
263                                         return;
264                                         
265                                 notifyFilter = value;
266                                 if (enableRaisingEvents) {
267                                         Stop ();
268                                         Start ();
269                                 }
270                         }
271                 }
272
273                 [DefaultValue("")]
274                 [IODescription("The directory to monitor")]
275                 [RecommendedAsConfigurable(true)]
276                 [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
277                 [Editor ("System.Diagnostics.Design.FSWPathEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
278                 public string Path {
279                         get { return path; }
280                         set {
281                                 if (path == value)
282                                         return;
283
284                                 bool exists = false;
285                                 Exception exc = null;
286
287                                 try {
288                                         exists = Directory.Exists (value);
289                                 } catch (Exception e) {
290                                         exc = e;
291                                 }
292
293                                 if (exc != null)
294                                         throw new ArgumentException ("Invalid directory name", "value", exc);
295
296                                 if (!exists)
297                                         throw new ArgumentException ("Directory does not exists", "value");
298
299                                 path = value;
300                                 fullpath = null;
301                                 if (enableRaisingEvents) {
302                                         Stop ();
303                                         Start ();
304                                 }
305                         }
306                 }
307
308                 [Browsable(false)]
309                 public override ISite Site {
310                         get { return base.Site; }
311                         set { base.Site = value; }
312                 }
313
314                 [DefaultValue(null)]
315                 [IODescription("The object used to marshal the event handler calls resulting from a directory change")]
316                 public ISynchronizeInvoke SynchronizingObject {
317                         get { return synchronizingObject; }
318                         set { synchronizingObject = value; }
319                 }
320
321                 #endregion // Properties
322
323                 #region Methods
324         
325                 [MonoTODO]
326                 public void BeginInit ()
327                 {
328                         throw new NotImplementedException (); 
329                 }
330
331                 protected override void Dispose (bool disposing)
332                 {
333                         if (!disposed) {
334                                 disposed = true;
335                                 Stop ();
336                         }
337
338                         base.Dispose (disposing);
339                 }
340
341                 ~FileSystemWatcher ()
342                 {
343                         disposed = true;
344                         Stop ();
345                 }
346                 
347                 [MonoTODO]
348                 public void EndInit ()
349                 {
350                         throw new NotImplementedException (); 
351                 }
352
353                 private void RaiseEvent (Delegate ev, EventArgs arg)
354                 {
355                         if (ev == null)
356                                 return;
357
358                         object [] args = new object [] {this, arg};
359
360                         if (synchronizingObject == null) {
361                                 ev.DynamicInvoke (args);
362                                 return;
363                         }
364                         
365                         synchronizingObject.BeginInvoke (ev, args);
366                 }
367
368                 protected void OnChanged (FileSystemEventArgs e)
369                 {
370                         RaiseEvent (Changed, e);
371                 }
372
373                 protected void OnCreated (FileSystemEventArgs e)
374                 {
375                         RaiseEvent (Created, e);
376                 }
377
378                 protected void OnDeleted (FileSystemEventArgs e)
379                 {
380                         RaiseEvent (Deleted, e);
381                 }
382
383                 protected void OnError (ErrorEventArgs e)
384                 {
385                         RaiseEvent (Error, e);
386                 }
387
388                 protected void OnRenamed (RenamedEventArgs e)
389                 {
390                         RaiseEvent (Renamed, e);
391                 }
392
393                 public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType)
394                 {
395                         return WaitForChanged (changeType, Timeout.Infinite);
396                 }
397
398                 public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType, int timeout)
399                 {
400                         WaitForChangedResult result = new WaitForChangedResult ();
401                         bool prevEnabled = EnableRaisingEvents;
402                         if (!prevEnabled)
403                                 EnableRaisingEvents = true;
404
405                         bool gotData;
406                         lock (this) {
407                                 waiting = true;
408                                 gotData = Monitor.Wait (this, timeout);
409                                 if (gotData)
410                                         result = this.lastData;
411                         }
412
413                         EnableRaisingEvents = prevEnabled;
414                         if (!gotData)
415                                 result.TimedOut = true;
416
417                         return result;
418                 }
419
420                 internal void DispatchEvents (FileAction act, string filename, ref RenamedEventArgs renamed)
421                 {
422                         if (waiting) {
423                                 lastData = new WaitForChangedResult ();
424                         }
425
426                         switch (act) {
427                         case FileAction.Added:
428                                 lastData.Name = filename;
429                                 lastData.ChangeType = WatcherChangeTypes.Created;
430                                 OnCreated (new FileSystemEventArgs (WatcherChangeTypes.Created, path, filename));
431                                 break;
432                         case FileAction.Removed:
433                                 lastData.Name = filename;
434                                 lastData.ChangeType = WatcherChangeTypes.Deleted;
435                                 OnDeleted (new FileSystemEventArgs (WatcherChangeTypes.Deleted, path, filename));
436                                 break;
437                         case FileAction.Modified:
438                                 lastData.Name = filename;
439                                 lastData.ChangeType = WatcherChangeTypes.Changed;
440                                 OnChanged (new FileSystemEventArgs (WatcherChangeTypes.Changed, path, filename));
441                                 break;
442                         case FileAction.RenamedOldName:
443                                 if (renamed != null) {
444                                         OnRenamed (renamed);
445                                 }
446                                 lastData.OldName = filename;
447                                 lastData.ChangeType = WatcherChangeTypes.Renamed;
448                                 renamed = new RenamedEventArgs (WatcherChangeTypes.Renamed, path, null, filename);
449                                 break;
450                         case FileAction.RenamedNewName:
451                                 lastData.Name = filename;
452                                 lastData.ChangeType = WatcherChangeTypes.Renamed;
453                                 if (renamed != null) {
454                                         renamed.SetName (filename);
455                                 } else {
456                                         renamed = new RenamedEventArgs (WatcherChangeTypes.Renamed, path, filename, null);
457                                 }
458                                 OnRenamed (renamed);
459                                 renamed = null;
460                                 break;
461                         default:
462                                 break;
463                         }
464                 }
465
466                 void Start ()
467                 {
468                         watcher.StartDispatching (this);
469                 }
470
471                 void Stop ()
472                 {
473                         watcher.StopDispatching (this);
474                 }
475                 #endregion // Methods
476
477                 #region Events and Delegates
478
479                 [IODescription("Occurs when a file/directory change matches the filter")]
480                 public event FileSystemEventHandler Changed;
481
482                 [IODescription("Occurs when a file/directory creation matches the filter")]
483                 public event FileSystemEventHandler Created;
484
485                 [IODescription("Occurs when a file/directory deletion matches the filter")]
486                 public event FileSystemEventHandler Deleted;
487
488                 [Browsable(false)]
489                 public event ErrorEventHandler Error;
490
491                 [IODescription("Occurs when a file/directory rename matches the filter")]
492                 public event RenamedEventHandler Renamed;
493
494                 #endregion // Events and Delegates
495
496                 /* 0 -> not supported   */
497                 /* 1 -> windows         */
498                 /* 2 -> FAM             */
499                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
500                 static extern int InternalSupportsFSW ();
501                 
502                 /*[MethodImplAttribute(MethodImplOptions.InternalCall)]
503                 static extern IntPtr InternalOpenDirectory (string path, IntPtr reserved);
504
505                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
506                 static extern IntPtr InternalCloseDirectory (IntPtr handle);
507
508                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
509                 static extern bool InternalReadDirectoryChanges (IntPtr handle,
510                                                                  byte [] buffer,
511                                                                  bool includeSubdirectories,
512                                                                  NotifyFilters notifyFilter,
513                                                                  out NativeOverlapped overlap,
514                                                                  OverlappedHandler overlappedCallback);
515
516                 */
517         }
518 }