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