FAMWatcher.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. 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. public static bool GetInstance (out IFileWatcher watcher)
  77. {
  78. lock (typeof (FAMWatcher)) {
  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. }
  99. public void StartDispatching (FileSystemWatcher fsw)
  100. {
  101. FAMData data;
  102. lock (this) {
  103. if (thread == null) {
  104. thread = new Thread (new ThreadStart (Monitor));
  105. thread.IsBackground = true;
  106. thread.Start ();
  107. }
  108. data = (FAMData) watches [fsw];
  109. }
  110. if (data == null) {
  111. data = new FAMData ();
  112. data.FSW = fsw;
  113. data.Directory = fsw.FullPath;
  114. data.FileMask = fsw.MangledFilter;
  115. data.IncludeSubdirs = fsw.IncludeSubdirectories;
  116. if (data.IncludeSubdirs)
  117. data.SubDirs = new Hashtable ();
  118. data.Enabled = true;
  119. lock (this) {
  120. StartMonitoringDirectory (data);
  121. watches [fsw] = data;
  122. requests [data.Request.ReqNum] = data;
  123. stop = false;
  124. }
  125. }
  126. }
  127. static void StartMonitoringDirectory (FAMData data)
  128. {
  129. FAMRequest fr;
  130. if (FAMMonitorDirectory (ref conn, data.Directory, out fr, IntPtr.Zero) == -1)
  131. throw new Win32Exception ();
  132. data.Request = fr;
  133. if (!data.IncludeSubdirs)
  134. return;
  135. foreach (string directory in Directory.GetDirectories (data.Directory)) {
  136. FAMData fd = new FAMData ();
  137. fd.FSW = data.FSW;
  138. fd.Directory = directory;
  139. fd.FileMask = data.FSW.MangledFilter;
  140. fd.IncludeSubdirs = true;
  141. fd.SubDirs = new Hashtable ();
  142. fd.Enabled = true;
  143. StartMonitoringDirectory (fd);
  144. data.SubDirs [directory] = fd;
  145. requests [fd.Request.ReqNum] = fd;
  146. }
  147. }
  148. public void StopDispatching (FileSystemWatcher fsw)
  149. {
  150. FAMData data;
  151. lock (this) {
  152. data = (FAMData) watches [fsw];
  153. if (data == null)
  154. return;
  155. StopMonitoringDirectory (data);
  156. watches.Remove (fsw);
  157. requests.Remove (data.Request.ReqNum);
  158. if (watches.Count == 0)
  159. stop = true;
  160. if (!data.IncludeSubdirs)
  161. return;
  162. foreach (FAMData fd in data.SubDirs) {
  163. StopMonitoringDirectory (fd);
  164. requests.Remove (fd.Request.ReqNum);
  165. }
  166. }
  167. }
  168. static void StopMonitoringDirectory (FAMData data)
  169. {
  170. if (FAMCancelMonitor (ref conn, ref data.Request) == -1)
  171. throw new Win32Exception ();
  172. }
  173. void Monitor ()
  174. {
  175. while (!stop) {
  176. int haveEvents;
  177. lock (this) {
  178. haveEvents = FAMPending (ref conn);
  179. }
  180. if (haveEvents > 0) {
  181. ProcessEvents ();
  182. } else {
  183. Thread.Sleep (500);
  184. }
  185. }
  186. lock (this) {
  187. thread = null;
  188. stop = false;
  189. }
  190. }
  191. const NotifyFilters changed = NotifyFilters.Attributes |
  192. NotifyFilters.LastAccess |
  193. NotifyFilters.Size |
  194. NotifyFilters.LastWrite;
  195. void ProcessEvents ()
  196. {
  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. FAMData fd = new FAMData ();
  252. fd.FSW = fsw;
  253. fd.Directory = datadir;
  254. fd.FileMask = fsw.MangledFilter;
  255. fd.IncludeSubdirs = true;
  256. fd.SubDirs = new Hashtable ();
  257. fd.Enabled = true;
  258. lock (instance) {
  259. StartMonitoringDirectory (fd);
  260. }
  261. lock (data) {
  262. data.SubDirs [datadir] = fd;
  263. }
  264. requests [fd.Request.ReqNum] = fd;
  265. }
  266. }
  267. if (filename != data.Directory && !fsw.Pattern.IsMatch (filename))
  268. continue;
  269. lock (fsw) {
  270. fsw.DispatchEvents (fa, filename, ref renamed);
  271. if (fsw.Waiting) {
  272. fsw.Waiting = false;
  273. System.Threading.Monitor.PulseAll (fsw);
  274. }
  275. }
  276. } while (FAMPending (ref conn) > 0);
  277. }
  278. }
  279. [DllImport ("libfam.so.0")]
  280. extern static int FAMOpen (out FAMConnection fc);
  281. [DllImport ("libfam.so.0")]
  282. extern static int FAMClose (ref FAMConnection fc);
  283. [DllImport ("libfam.so.0")]
  284. extern static int FAMMonitorDirectory (ref FAMConnection fc, string filename,
  285. out FAMRequest fr, IntPtr user_data);
  286. [DllImport ("libfam.so.0")]
  287. extern static int FAMCancelMonitor (ref FAMConnection fc, ref FAMRequest fr);
  288. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  289. extern static int InternalFAMNextEvent (ref FAMConnection fc, out string filename,
  290. out int code, out int reqnum);
  291. [DllImport ("libfam.so.0")]
  292. extern static int FAMPending (ref FAMConnection fc);
  293. }
  294. }