FAMWatcher.cs 8.6 KB

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