2004-09-04 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System / System.IO / FAMWatcher.cs
1 // 
2 // System.IO.FAM.cs: interface with libfam
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (c) 2004 Novell, Inc. (http://www.novell.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections;
33 using System.ComponentModel;
34 using System.Runtime.CompilerServices;
35 using System.Runtime.InteropServices;
36 using System.Text;
37 using System.Threading;
38
39 namespace System.IO {
40         struct FAMConnection {
41                 public int FD;
42                 IntPtr opaque;
43         }
44
45         struct FAMRequest {
46                 public int ReqNum;
47         }
48
49         enum FAMCodes {
50                 Changed = 1,
51                 Deleted = 2,
52                 StartExecuting = 3, 
53                 StopExecuting = 4,
54                 Created = 5,
55                 Moved = 6, 
56                 Acknowledge = 7,
57                 Exists = 8,
58                 EndExist = 9
59         };
60
61         class FAMData {
62                 public FileSystemWatcher FSW;
63                 public string Directory;
64                 public string FileMask;
65                 public bool IncludeSubdirs;
66                 public bool Enabled;
67                 public FAMRequest Request;
68                 public Hashtable SubDirs;
69         }
70
71         class FAMWatcher : IFileWatcher
72         {
73                 static bool failed;
74                 static FAMWatcher instance;
75                 static Hashtable watches;
76                 static Hashtable requests;
77                 static FAMConnection conn;
78                 static Thread thread;
79                 static bool stop;
80                 
81                 private FAMWatcher ()
82                 {
83                 }
84                 
85                 public static bool GetInstance (out IFileWatcher watcher)
86                 {
87                         lock (typeof (FAMWatcher)) {
88                                 if (failed == true) {
89                                         watcher = null;
90                                         return false;
91                                 }
92
93                                 if (instance != null) {
94                                         watcher = instance;
95                                         return true;
96                                 }
97
98                                 watches = Hashtable.Synchronized (new Hashtable ());
99                                 requests = Hashtable.Synchronized (new Hashtable ());
100                                 if (FAMOpen (out conn) == -1) {
101                                         failed = true;
102                                         watcher = null;
103                                         return false;
104                                 }
105
106                                 instance = new FAMWatcher ();
107                                 watcher = instance;
108                                 return true;
109                         }
110                 }
111                 
112                 public void StartDispatching (FileSystemWatcher fsw)
113                 {
114                         FAMData data;
115                         lock (this) {
116                                 if (thread == null) {
117                                         thread = new Thread (new ThreadStart (Monitor));
118                                         thread.IsBackground = true;
119                                         thread.Start ();
120                                 }
121
122                                 data = (FAMData) watches [fsw];
123                         }
124
125                         if (data == null) {
126                                 data = new FAMData ();
127                                 data.FSW = fsw;
128                                 data.Directory = fsw.FullPath;
129                                 data.FileMask = fsw.MangledFilter;
130                                 data.IncludeSubdirs = fsw.IncludeSubdirectories;
131                                 if (data.IncludeSubdirs)
132                                         data.SubDirs = new Hashtable ();
133
134                                 data.Enabled = true;
135                                 lock (this) {
136                                         StartMonitoringDirectory (data);
137                                         watches [fsw] = data;
138                                         requests [data.Request.ReqNum] = data;
139                                         stop = false;
140                                 }
141                         }
142                 }
143
144                 static void StartMonitoringDirectory (FAMData data)
145                 {
146                         FAMRequest fr;
147                         if (FAMMonitorDirectory (ref conn, data.Directory, out fr, IntPtr.Zero) == -1)
148                                 throw new Win32Exception ();
149
150                         data.Request = fr;
151                         if (!data.IncludeSubdirs)
152                                 return;
153
154                         foreach (string directory in Directory.GetDirectories (data.Directory)) {
155                                 FAMData fd = new FAMData ();
156                                 fd.FSW = data.FSW;
157                                 fd.Directory = directory;
158                                 fd.FileMask = data.FSW.MangledFilter;
159                                 fd.IncludeSubdirs = true;
160                                 fd.SubDirs = new Hashtable ();
161                                 fd.Enabled = true;
162
163                                 StartMonitoringDirectory (fd);
164                                 data.SubDirs [directory] = fd;
165                                 requests [fd.Request.ReqNum] = fd;
166                         }
167                 }
168
169                 public void StopDispatching (FileSystemWatcher fsw)
170                 {
171                         FAMData data;
172                         lock (this) {
173                                 data = (FAMData) watches [fsw];
174                                 if (data == null)
175                                         return;
176
177                                 StopMonitoringDirectory (data);
178                                 watches.Remove (fsw);
179                                 requests.Remove (data.Request.ReqNum);
180                                 if (watches.Count == 0)
181                                         stop = true;
182
183                                 if (!data.IncludeSubdirs)
184                                         return;
185
186                                 foreach (FAMData fd in data.SubDirs) {
187                                         StopMonitoringDirectory (fd);
188                                         requests.Remove (fd.Request.ReqNum);
189                                 }
190                         }
191                 }
192
193                 static void StopMonitoringDirectory (FAMData data)
194                 {
195                         if (FAMCancelMonitor (ref conn, ref data.Request) == -1)
196                                 throw new Win32Exception ();
197                 }
198
199                 void Monitor ()
200                 {
201                         while (!stop) {
202                                 int haveEvents;
203                                 lock (this) {
204                                         haveEvents = FAMPending (ref conn);
205                                 }
206
207                                 if (haveEvents > 0) {
208                                         ProcessEvents ();
209                                 } else {
210                                         Thread.Sleep (500);
211                                 }
212                         }
213
214                         lock (this) {
215                                 thread = null;
216                                 stop = false;
217                         }
218                 }
219
220                 const NotifyFilters changed =   NotifyFilters.Attributes |
221                                                 NotifyFilters.LastAccess |
222                                                 NotifyFilters.Size      |
223                                                 NotifyFilters.LastWrite;
224
225                 void ProcessEvents ()
226                 {
227                         lock (this) {
228                                 do {
229                                         int code;
230                                         string filename;
231                                         int requestNumber;
232                                         FileSystemWatcher fsw;
233
234                                         if (InternalFAMNextEvent (ref conn, out filename,
235                                                                   out code, out requestNumber) != 1)
236                                                 return;
237
238                                         bool found = false;
239                                         switch ((FAMCodes) code) {
240                                         case FAMCodes.Changed:
241                                         case FAMCodes.Deleted:
242                                         case FAMCodes.Created:
243                                                 found = requests.ContainsKey (requestNumber);
244                                                 break;
245                                         case FAMCodes.Moved:
246                                         case FAMCodes.StartExecuting:
247                                         case FAMCodes.StopExecuting:
248                                         case FAMCodes.Acknowledge:
249                                         case FAMCodes.Exists:
250                                         case FAMCodes.EndExist:
251                                         default:
252                                                 found = false;
253                                                 break;
254                                         }
255
256                                         if (!found)
257                                                 continue;
258                                         
259                                         FAMData data = (FAMData) requests [requestNumber];
260                                         if (!data.Enabled)
261                                                 continue;
262
263                                         fsw = data.FSW;
264                                         NotifyFilters flt = fsw.NotifyFilter;
265                                         RenamedEventArgs renamed = null;
266                                         FileAction fa = 0;
267                                         if (code == (int) FAMCodes.Changed && (flt & changed) != 0)
268                                                 fa = FileAction.Modified;
269                                         else if (code == (int) FAMCodes.Deleted)
270                                                 fa = FileAction.Removed;
271                                         else if (code == (int) FAMCodes.Created)
272                                                 fa = FileAction.Added;
273
274                                         if (fa == 0)
275                                                 continue;
276
277                                         if (fsw.IncludeSubdirectories) {
278                                                 string full = fsw.FullPath;
279                                                 string datadir = data.Directory;
280                                                 if (datadir != full) {
281                                                         string reldir = datadir.Substring (full.Length + 1);
282                                                         datadir = Path.Combine (datadir, filename);
283                                                         filename = Path.Combine (reldir, filename);
284                                                 } else {
285                                                         datadir = Path.Combine (fsw.FullPath, filename);
286                                                 }
287
288                                                 if (fa == FileAction.Added && Directory.Exists (datadir)) {
289                                                         FAMData fd = new FAMData ();
290                                                         fd.FSW = fsw;
291                                                         fd.Directory = datadir;
292                                                         fd.FileMask = fsw.MangledFilter;
293                                                         fd.IncludeSubdirs = true;
294                                                         fd.SubDirs = new Hashtable ();
295                                                         fd.Enabled = true;
296
297                                                         lock (instance) {
298                                                                 StartMonitoringDirectory (fd);
299                                                         }
300
301                                                         lock (data) {
302                                                                 data.SubDirs [datadir] = fd;
303                                                         }
304
305                                                         requests [fd.Request.ReqNum] = fd;
306                                                 }
307                                         }
308
309                                         if (filename != data.Directory && !fsw.Pattern.IsMatch (filename))
310                                                 continue;
311
312                                         lock (fsw) {
313                                                 fsw.DispatchEvents (fa, filename, ref renamed);
314                                                 if (fsw.Waiting) {
315                                                         fsw.Waiting = false;
316                                                         System.Threading.Monitor.PulseAll (fsw);
317                                                 }
318                                         }
319                                 } while (FAMPending (ref conn) > 0);
320                         }
321                 }
322
323                 [DllImport ("libfam.so.0")]
324                 extern static int FAMOpen (out FAMConnection fc);
325
326                 [DllImport ("libfam.so.0")]
327                 extern static int FAMClose (ref FAMConnection fc);
328
329                 [DllImport ("libfam.so.0")]
330                 extern static int FAMMonitorDirectory (ref FAMConnection fc, string filename,
331                                                         out FAMRequest fr, IntPtr user_data);
332
333                 [DllImport ("libfam.so.0")]
334                 extern static int FAMCancelMonitor (ref FAMConnection fc, ref FAMRequest fr);
335
336                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
337                 extern static int InternalFAMNextEvent (ref FAMConnection fc, out string filename,
338                                                         out int code, out int reqnum);
339
340                 [DllImport ("libfam.so.0")]
341                 extern static int FAMPending (ref FAMConnection fc);
342         }
343 }
344