2 // System.IO.FAM.cs: interface with libfam
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (c) 2004 Novell, Inc. (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections;
33 using System.ComponentModel;
34 using System.Runtime.CompilerServices;
35 using System.Runtime.InteropServices;
37 using System.Threading;
40 struct FAMConnection {
62 public FileSystemWatcher FSW;
63 public string Directory;
64 public string FileMask;
65 public bool IncludeSubdirs;
67 public FAMRequest Request;
68 public Hashtable SubDirs;
71 class FAMWatcher : IFileWatcher
74 static FAMWatcher instance;
75 static Hashtable watches;
76 static Hashtable requests;
77 static FAMConnection conn;
80 static bool use_gamin;
87 public static bool GetInstance (out IFileWatcher watcher, bool gamin)
94 if (instance != null) {
100 watches = Hashtable.Synchronized (new Hashtable ());
101 requests = Hashtable.Synchronized (new Hashtable ());
102 if (FAMOpen (out conn) == -1) {
108 instance = new FAMWatcher ();
113 public void StartDispatching (FileSystemWatcher fsw)
117 if (thread == null) {
118 thread = new Thread (new ThreadStart (Monitor));
119 thread.IsBackground = true;
123 data = (FAMData) watches [fsw];
127 data = new FAMData ();
129 data.Directory = fsw.FullPath;
130 data.FileMask = fsw.MangledFilter;
131 data.IncludeSubdirs = fsw.IncludeSubdirectories;
132 if (data.IncludeSubdirs)
133 data.SubDirs = new Hashtable ();
136 StartMonitoringDirectory (data);
138 watches [fsw] = data;
139 requests [data.Request.ReqNum] = data;
145 static void StartMonitoringDirectory (FAMData data)
148 if (FAMMonitorDirectory (ref conn, data.Directory, out fr, IntPtr.Zero) == -1)
149 throw new Win32Exception ();
152 if (!data.IncludeSubdirs)
155 foreach (string directory in Directory.GetDirectories (data.Directory)) {
156 FAMData fd = new FAMData ();
158 fd.Directory = directory;
159 fd.FileMask = data.FSW.MangledFilter;
160 fd.IncludeSubdirs = true;
161 fd.SubDirs = new Hashtable ();
164 StartMonitoringDirectory (fd);
165 data.SubDirs [directory] = fd;
166 requests [fd.Request.ReqNum] = fd;
170 public void StopDispatching (FileSystemWatcher fsw)
174 data = (FAMData) watches [fsw];
178 StopMonitoringDirectory (data);
179 watches.Remove (fsw);
180 requests.Remove (data.Request.ReqNum);
181 if (watches.Count == 0)
184 if (!data.IncludeSubdirs)
187 foreach (FAMData fd in data.SubDirs.Values) {
188 StopMonitoringDirectory (fd);
189 requests.Remove (fd.Request.ReqNum);
194 static void StopMonitoringDirectory (FAMData data)
196 if (FAMCancelMonitor (ref conn, ref data.Request) == -1)
197 throw new Win32Exception ();
205 haveEvents = FAMPending (ref conn);
208 if (haveEvents > 0) {
221 const NotifyFilters changed = NotifyFilters.Attributes |
222 NotifyFilters.LastAccess |
224 NotifyFilters.LastWrite;
226 void ProcessEvents ()
228 ArrayList newdirs = null;
234 FileSystemWatcher fsw;
236 if (InternalFAMNextEvent (ref conn, out filename,
237 out code, out requestNumber) != 1)
241 switch ((FAMCodes) code) {
242 case FAMCodes.Changed:
243 case FAMCodes.Deleted:
244 case FAMCodes.Created:
245 found = requests.ContainsKey (requestNumber);
248 case FAMCodes.StartExecuting:
249 case FAMCodes.StopExecuting:
250 case FAMCodes.Acknowledge:
251 case FAMCodes.Exists:
252 case FAMCodes.EndExist:
261 FAMData data = (FAMData) requests [requestNumber];
266 NotifyFilters flt = fsw.NotifyFilter;
267 RenamedEventArgs renamed = null;
269 if (code == (int) FAMCodes.Changed && (flt & changed) != 0)
270 fa = FileAction.Modified;
271 else if (code == (int) FAMCodes.Deleted)
272 fa = FileAction.Removed;
273 else if (code == (int) FAMCodes.Created)
274 fa = FileAction.Added;
279 if (fsw.IncludeSubdirectories) {
280 string full = fsw.FullPath;
281 string datadir = data.Directory;
282 if (datadir != full) {
283 string reldir = datadir.Substring (full.Length + 1);
284 datadir = Path.Combine (datadir, filename);
285 filename = Path.Combine (reldir, filename);
287 datadir = Path.Combine (fsw.FullPath, filename);
290 if (fa == FileAction.Added && Directory.Exists (datadir)) {
292 newdirs = new ArrayList (4);
294 FAMData fd = new FAMData ();
296 fd.Directory = datadir;
297 fd.FileMask = fsw.MangledFilter;
298 fd.IncludeSubdirs = true;
299 fd.SubDirs = new Hashtable ();
303 requests [fd.Request.ReqNum] = fd;
307 if (filename != data.Directory && !fsw.Pattern.IsMatch (filename))
311 fsw.DispatchEvents (fa, filename, ref renamed);
314 System.Threading.Monitor.PulseAll (fsw);
317 } while (FAMPending (ref conn) > 0);
321 if (newdirs != null) {
322 int count = newdirs.Count;
323 for (int n = 0; n < count; n += 2) {
324 FAMData newdir = (FAMData) newdirs [n];
325 FAMData parent = (FAMData) newdirs [n + 1];
326 StartMonitoringDirectory (newdir);
328 parent.SubDirs [newdir.Directory] = newdir;
340 static int FAMOpen (out FAMConnection fc)
343 return gamin_Open (out fc);
344 return fam_Open (out fc);
347 static int FAMClose (ref FAMConnection fc)
350 return gamin_Close (ref fc);
351 return fam_Close (ref fc);
354 static int FAMMonitorDirectory (ref FAMConnection fc, string filename, out FAMRequest fr, IntPtr user_data)
357 return gamin_MonitorDirectory (ref fc, filename, out fr, user_data);
358 return fam_MonitorDirectory (ref fc, filename, out fr, user_data);
361 static int FAMCancelMonitor (ref FAMConnection fc, ref FAMRequest fr)
364 return gamin_CancelMonitor (ref fc, ref fr);
365 return fam_CancelMonitor (ref fc, ref fr);
368 static int FAMPending (ref FAMConnection fc)
371 return gamin_Pending (ref fc);
372 return fam_Pending (ref fc);
377 [DllImport ("libfam.so.0", EntryPoint="FAMOpen")]
378 extern static int fam_Open (out FAMConnection fc);
380 [DllImport ("libfam.so.0", EntryPoint="FAMClose")]
381 extern static int fam_Close (ref FAMConnection fc);
383 [DllImport ("libfam.so.0", EntryPoint="FAMMonitorDirectory")]
384 extern static int fam_MonitorDirectory (ref FAMConnection fc, string filename,
385 out FAMRequest fr, IntPtr user_data);
387 [DllImport ("libfam.so.0", EntryPoint="FAMCancelMonitor")]
388 extern static int fam_CancelMonitor (ref FAMConnection fc, ref FAMRequest fr);
390 [DllImport ("libfam.so.0", EntryPoint="FAMPending")]
391 extern static int fam_Pending (ref FAMConnection fc);
393 [DllImport ("libgamin-1.so.0", EntryPoint="FAMOpen")]
394 extern static int gamin_Open (out FAMConnection fc);
396 [DllImport ("libgamin-1.so.0", EntryPoint="FAMClose")]
397 extern static int gamin_Close (ref FAMConnection fc);
399 [DllImport ("libgamin-1.so.0", EntryPoint="FAMMonitorDirectory")]
400 extern static int gamin_MonitorDirectory (ref FAMConnection fc, string filename,
401 out FAMRequest fr, IntPtr user_data);
403 [DllImport ("libgamin-1.so.0", EntryPoint="FAMCancelMonitor")]
404 extern static int gamin_CancelMonitor (ref FAMConnection fc, ref FAMRequest fr);
406 [DllImport ("libgamin-1.so.0", EntryPoint="FAMPending")]
407 extern static int gamin_Pending (ref FAMConnection fc);
409 [MethodImplAttribute(MethodImplOptions.InternalCall)]
410 extern static int InternalFAMNextEvent (ref FAMConnection fc, out string filename,
411 out int code, out int reqnum);