KeventWatcher.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. //
  2. // System.IO.KeventWatcher.cs: interface with osx kevent
  3. //
  4. // Authors:
  5. // Geoff Norton ([email protected])
  6. //
  7. // (c) 2004 Geoff Norton
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.ComponentModel;
  31. using System.Runtime.CompilerServices;
  32. using System.Runtime.InteropServices;
  33. using System.Text;
  34. using System.Threading;
  35. namespace System.IO {
  36. struct kevent : IDisposable {
  37. public int ident;
  38. public short filter;
  39. public ushort flags;
  40. public uint fflags;
  41. public int data;
  42. public IntPtr udata;
  43. public void Dispose ()
  44. {
  45. if (udata != IntPtr.Zero)
  46. Marshal.FreeHGlobal (udata);
  47. }
  48. }
  49. struct timespec {
  50. public int tv_sec;
  51. public int tv_usec;
  52. }
  53. class KeventFileData {
  54. public FileSystemInfo fsi;
  55. public DateTime LastAccessTime;
  56. public DateTime LastWriteTime;
  57. public KeventFileData(FileSystemInfo fsi, DateTime LastAccessTime, DateTime LastWriteTime) {
  58. this.fsi = fsi;
  59. this.LastAccessTime = LastAccessTime;
  60. this.LastWriteTime = LastWriteTime;
  61. }
  62. }
  63. class KeventData {
  64. public FileSystemWatcher FSW;
  65. public string Directory;
  66. public string FileMask;
  67. public bool IncludeSubdirs;
  68. public bool Enabled;
  69. public Hashtable DirEntries;
  70. public kevent ev;
  71. }
  72. class KeventWatcher : IFileWatcher
  73. {
  74. static bool failed;
  75. static KeventWatcher instance;
  76. static Hashtable watches;
  77. static Hashtable requests;
  78. static Thread thread;
  79. static int conn;
  80. static bool stop;
  81. private KeventWatcher ()
  82. {
  83. }
  84. // Locked by caller
  85. public static bool GetInstance (out IFileWatcher watcher)
  86. {
  87. if (failed == true) {
  88. watcher = null;
  89. return false;
  90. }
  91. if (instance != null) {
  92. watcher = instance;
  93. return true;
  94. }
  95. watches = Hashtable.Synchronized (new Hashtable ());
  96. requests = Hashtable.Synchronized (new Hashtable ());
  97. conn = kqueue();
  98. if (conn == -1) {
  99. failed = true;
  100. watcher = null;
  101. return false;
  102. }
  103. instance = new KeventWatcher ();
  104. watcher = instance;
  105. return true;
  106. }
  107. public void StartDispatching (FileSystemWatcher fsw)
  108. {
  109. KeventData data;
  110. lock (this) {
  111. if (thread == null) {
  112. thread = new Thread (new ThreadStart (Monitor));
  113. thread.IsBackground = true;
  114. thread.Start ();
  115. }
  116. data = (KeventData) watches [fsw];
  117. }
  118. if (data == null) {
  119. data = new KeventData ();
  120. data.FSW = fsw;
  121. data.Directory = fsw.FullPath;
  122. data.FileMask = fsw.MangledFilter;
  123. data.IncludeSubdirs = fsw.IncludeSubdirectories;
  124. data.Enabled = true;
  125. lock (this) {
  126. StartMonitoringDirectory (data);
  127. watches [fsw] = data;
  128. stop = false;
  129. }
  130. }
  131. }
  132. static void StartMonitoringDirectory (KeventData data)
  133. {
  134. DirectoryInfo dir = new DirectoryInfo (data.Directory);
  135. if(data.DirEntries == null) {
  136. data.DirEntries = new Hashtable();
  137. foreach (FileSystemInfo fsi in dir.GetFileSystemInfos() )
  138. data.DirEntries.Add(fsi.FullName, new KeventFileData(fsi, fsi.LastAccessTime, fsi.LastWriteTime));
  139. }
  140. int fd = open(data.Directory, 0, 0);
  141. kevent ev = new kevent();
  142. ev.udata = IntPtr.Zero;
  143. timespec nullts = new timespec();
  144. nullts.tv_sec = 0;
  145. nullts.tv_usec = 0;
  146. if (fd > 0) {
  147. ev.ident = fd;
  148. ev.filter = -4;
  149. ev.flags = 1 | 4 | 20;
  150. ev.fflags = 20 | 2 | 1 | 8;
  151. ev.data = 0;
  152. ev.udata = Marshal.StringToHGlobalAuto (data.Directory);
  153. kevent outev = new kevent();
  154. outev.udata = IntPtr.Zero;
  155. kevent (conn, ref ev, 1, ref outev, 0, ref nullts);
  156. data.ev = ev;
  157. requests [fd] = data;
  158. }
  159. if (!data.IncludeSubdirs)
  160. return;
  161. }
  162. public void StopDispatching (FileSystemWatcher fsw)
  163. {
  164. KeventData data;
  165. lock (this) {
  166. data = (KeventData) watches [fsw];
  167. if (data == null)
  168. return;
  169. StopMonitoringDirectory (data);
  170. watches.Remove (fsw);
  171. if (watches.Count == 0)
  172. stop = true;
  173. if (!data.IncludeSubdirs)
  174. return;
  175. }
  176. }
  177. static void StopMonitoringDirectory (KeventData data)
  178. {
  179. close(data.ev.ident);
  180. }
  181. void Monitor ()
  182. {
  183. while (!stop) {
  184. kevent ev = new kevent();
  185. ev.udata = IntPtr.Zero;
  186. kevent nullev = new kevent();
  187. nullev.udata = IntPtr.Zero;
  188. timespec ts = new timespec();
  189. ts.tv_sec = 0;
  190. ts.tv_usec = 0;
  191. int haveEvents;
  192. lock (this) {
  193. haveEvents = kevent (conn, ref nullev, 0, ref ev, 1, ref ts);
  194. }
  195. if (haveEvents > 0) {
  196. // Restart monitoring
  197. KeventData data = (KeventData) requests [ev.ident];
  198. StopMonitoringDirectory (data);
  199. StartMonitoringDirectory (data);
  200. ProcessEvent (ev);
  201. } else {
  202. System.Threading.Thread.Sleep (500);
  203. }
  204. }
  205. lock (this) {
  206. thread = null;
  207. stop = false;
  208. }
  209. }
  210. void ProcessEvent (kevent ev)
  211. {
  212. lock (this) {
  213. KeventData data = (KeventData) requests [ev.ident];
  214. if (!data.Enabled)
  215. return;
  216. FileSystemWatcher fsw;
  217. string filename = "";
  218. fsw = data.FSW;
  219. FileAction fa = 0;
  220. DirectoryInfo dir = new DirectoryInfo (data.Directory);
  221. FileSystemInfo changedFsi = null;
  222. try {
  223. foreach (FileSystemInfo fsi in dir.GetFileSystemInfos() )
  224. if (data.DirEntries.ContainsKey (fsi.FullName) && (fsi is FileInfo)) {
  225. KeventFileData entry = (KeventFileData) data.DirEntries [fsi.FullName];
  226. if (entry.LastWriteTime != fsi.LastWriteTime) {
  227. filename = fsi.Name;
  228. fa = FileAction.Modified;
  229. data.DirEntries [fsi.FullName] = new KeventFileData(fsi, fsi.LastAccessTime, fsi.LastWriteTime);
  230. if (fsw.IncludeSubdirectories && fsi is DirectoryInfo) {
  231. data.Directory = filename;
  232. requests [ev.ident] = data;
  233. ProcessEvent(ev);
  234. }
  235. PostEvent(filename, fsw, fa, changedFsi);
  236. }
  237. }
  238. } catch (Exception) {
  239. // The file system infos were changed while we processed them
  240. }
  241. // Deleted
  242. try {
  243. bool deleteMatched = true;
  244. while(deleteMatched) {
  245. foreach (KeventFileData entry in data.DirEntries.Values) {
  246. if (!File.Exists (entry.fsi.FullName) && !Directory.Exists (entry.fsi.FullName)) {
  247. filename = entry.fsi.Name;
  248. fa = FileAction.Removed;
  249. data.DirEntries.Remove (entry.fsi.FullName);
  250. PostEvent(filename, fsw, fa, changedFsi);
  251. break;
  252. }
  253. }
  254. deleteMatched = false;
  255. }
  256. } catch (Exception) {
  257. // The file system infos were changed while we processed them
  258. }
  259. // Added
  260. try {
  261. foreach (FileSystemInfo fsi in dir.GetFileSystemInfos())
  262. if (!data.DirEntries.ContainsKey (fsi.FullName)) {
  263. changedFsi = fsi;
  264. filename = fsi.Name;
  265. fa = FileAction.Added;
  266. data.DirEntries [fsi.FullName] = new KeventFileData(fsi, fsi.LastAccessTime, fsi.LastWriteTime);
  267. PostEvent(filename, fsw, fa, changedFsi);
  268. }
  269. } catch (Exception) {
  270. // The file system infos were changed while we processed them
  271. }
  272. }
  273. }
  274. private void PostEvent (string filename, FileSystemWatcher fsw, FileAction fa, FileSystemInfo changedFsi) {
  275. RenamedEventArgs renamed = null;
  276. if (fa == 0)
  277. return;
  278. if (fsw.IncludeSubdirectories && fa == FileAction.Added) {
  279. if (changedFsi is DirectoryInfo) {
  280. KeventData newdirdata = new KeventData ();
  281. newdirdata.FSW = fsw;
  282. newdirdata.Directory = changedFsi.FullName;
  283. newdirdata.FileMask = fsw.MangledFilter;
  284. newdirdata.IncludeSubdirs = fsw.IncludeSubdirectories;
  285. newdirdata.Enabled = true;
  286. lock (this) {
  287. StartMonitoringDirectory (newdirdata);
  288. }
  289. }
  290. }
  291. if (!fsw.Pattern.IsMatch(filename, true))
  292. return;
  293. lock (fsw) {
  294. fsw.DispatchEvents (fa, filename, ref renamed);
  295. if (fsw.Waiting) {
  296. fsw.Waiting = false;
  297. System.Threading.Monitor.PulseAll (fsw);
  298. }
  299. }
  300. }
  301. [DllImport ("libc")]
  302. extern static int open(string path, int flags, int mode_t);
  303. [DllImport ("libc")]
  304. extern static int close(int fd);
  305. [DllImport ("libc")]
  306. extern static int kqueue();
  307. [DllImport ("libc")]
  308. extern static int kevent(int kqueue, ref kevent ev, int nchanges, ref kevent evtlist, int nevents, ref timespec ts);
  309. }
  310. }