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