New test.
[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                 enum EventType {
358                         FileSystemEvent,
359                         ErrorEvent,
360                         RenameEvent
361                 }
362                 private void RaiseEvent (Delegate ev, EventArgs arg, EventType evtype)
363                 {
364                         if (ev == null)
365                                 return;
366
367                         if (synchronizingObject == null) {
368                                 Delegate [] delegates = ev.GetInvocationList ();
369                                 if (evtype == EventType.RenameEvent) {
370                                         foreach (RenamedEventHandler d in delegates){
371                                                 d.BeginInvoke (this, (RenamedEventArgs) arg, null, null);
372                                         }
373                                 } else if (evtype == EventType.ErrorEvent) {
374                                         foreach (ErrorEventHandler d in delegates){
375                                                 d.BeginInvoke (this, (ErrorEventArgs) arg, null, null);
376                                         }
377                                 } else {
378                                         foreach (FileSystemEventHandler d in delegates){
379                                                 d.BeginInvoke (this, (FileSystemEventArgs) arg, null, null);
380                                         }
381                                 }
382                                 return;
383                         }
384                         
385                         synchronizingObject.BeginInvoke (ev, new object [] {this, arg});
386                 }
387
388                 protected void OnChanged (FileSystemEventArgs e)
389                 {
390                         RaiseEvent (Changed, e, EventType.FileSystemEvent);
391                 }
392
393                 protected void OnCreated (FileSystemEventArgs e)
394                 {
395                         RaiseEvent (Created, e, EventType.FileSystemEvent);
396                 }
397
398                 protected void OnDeleted (FileSystemEventArgs e)
399                 {
400                         RaiseEvent (Deleted, e, EventType.FileSystemEvent);
401                 }
402
403                 protected void OnError (ErrorEventArgs e)
404                 {
405                         RaiseEvent (Error, e, EventType.ErrorEvent);
406                 }
407
408                 protected void OnRenamed (RenamedEventArgs e)
409                 {
410                         RaiseEvent (Renamed, e, EventType.RenameEvent);
411                 }
412
413                 public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType)
414                 {
415                         return WaitForChanged (changeType, Timeout.Infinite);
416                 }
417
418                 public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType, int timeout)
419                 {
420                         WaitForChangedResult result = new WaitForChangedResult ();
421                         bool prevEnabled = EnableRaisingEvents;
422                         if (!prevEnabled)
423                                 EnableRaisingEvents = true;
424
425                         bool gotData;
426                         lock (this) {
427                                 waiting = true;
428                                 gotData = Monitor.Wait (this, timeout);
429                                 if (gotData)
430                                         result = this.lastData;
431                         }
432
433                         EnableRaisingEvents = prevEnabled;
434                         if (!gotData)
435                                 result.TimedOut = true;
436
437                         return result;
438                 }
439
440                 internal void DispatchEvents (FileAction act, string filename, ref RenamedEventArgs renamed)
441                 {
442                         if (waiting) {
443                                 lastData = new WaitForChangedResult ();
444                         }
445
446                         switch (act) {
447                         case FileAction.Added:
448                                 lastData.Name = filename;
449                                 lastData.ChangeType = WatcherChangeTypes.Created;
450                                 OnCreated (new FileSystemEventArgs (WatcherChangeTypes.Created, path, filename));
451                                 break;
452                         case FileAction.Removed:
453                                 lastData.Name = filename;
454                                 lastData.ChangeType = WatcherChangeTypes.Deleted;
455                                 OnDeleted (new FileSystemEventArgs (WatcherChangeTypes.Deleted, path, filename));
456                                 break;
457                         case FileAction.Modified:
458                                 lastData.Name = filename;
459                                 lastData.ChangeType = WatcherChangeTypes.Changed;
460                                 OnChanged (new FileSystemEventArgs (WatcherChangeTypes.Changed, path, filename));
461                                 break;
462                         case FileAction.RenamedOldName:
463                                 if (renamed != null) {
464                                         OnRenamed (renamed);
465                                 }
466                                 lastData.OldName = filename;
467                                 lastData.ChangeType = WatcherChangeTypes.Renamed;
468                                 renamed = new RenamedEventArgs (WatcherChangeTypes.Renamed, path, filename, "");
469                                 break;
470                         case FileAction.RenamedNewName:
471                                 lastData.Name = filename;
472                                 lastData.ChangeType = WatcherChangeTypes.Renamed;
473                                 if (renamed == null) {
474                                         renamed = new RenamedEventArgs (WatcherChangeTypes.Renamed, path, "", filename);
475                                 }
476                                 OnRenamed (renamed);
477                                 renamed = null;
478                                 break;
479                         default:
480                                 break;
481                         }
482                 }
483
484                 void Start ()
485                 {
486                         watcher.StartDispatching (this);
487                 }
488
489                 void Stop ()
490                 {
491                         watcher.StopDispatching (this);
492                 }
493                 #endregion // Methods
494
495                 #region Events and Delegates
496
497                 [IODescription("Occurs when a file/directory change matches the filter")]
498                 public event FileSystemEventHandler Changed;
499
500                 [IODescription("Occurs when a file/directory creation matches the filter")]
501                 public event FileSystemEventHandler Created;
502
503                 [IODescription("Occurs when a file/directory deletion matches the filter")]
504                 public event FileSystemEventHandler Deleted;
505
506                 [Browsable(false)]
507                 public event ErrorEventHandler Error;
508
509                 [IODescription("Occurs when a file/directory rename matches the filter")]
510                 public event RenamedEventHandler Renamed;
511
512                 #endregion // Events and Delegates
513
514                 /* 0 -> not supported   */
515                 /* 1 -> windows         */
516                 /* 2 -> FAM             */
517                 /* 3 -> Kevent          */
518                 /* 4 -> gamin           */
519                 /* 5 -> inotify         */
520                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
521                 static extern int InternalSupportsFSW ();
522         }
523 }
524