DefaultWatcher.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. //
  2. // System.IO.DefaultWatcher.cs: default IFileWatcher
  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.IO;
  32. using System.Threading;
  33. namespace System.IO {
  34. class DefaultWatcherData {
  35. public FileSystemWatcher FSW;
  36. public string Directory;
  37. public string FileMask;
  38. public bool IncludeSubdirs;
  39. public bool Enabled;
  40. public DateTime DisabledTime;
  41. public Hashtable Files;
  42. }
  43. class FileData {
  44. public string Directory;
  45. public FileAttributes Attributes;
  46. public bool NotExists;
  47. public DateTime CreationTime;
  48. public DateTime LastWriteTime;
  49. }
  50. class DefaultWatcher : IFileWatcher
  51. {
  52. static DefaultWatcher instance;
  53. static Thread thread;
  54. static Hashtable watches;
  55. private DefaultWatcher ()
  56. {
  57. }
  58. public static bool GetInstance (out IFileWatcher watcher)
  59. {
  60. lock (typeof (DefaultWatcher)) {
  61. if (instance != null) {
  62. watcher = instance;
  63. return true;
  64. }
  65. instance = new DefaultWatcher ();
  66. watcher = instance;
  67. return true;
  68. }
  69. }
  70. public void StartDispatching (FileSystemWatcher fsw)
  71. {
  72. DefaultWatcherData data;
  73. lock (this) {
  74. if (watches == null)
  75. watches = new Hashtable ();
  76. if (thread == null) {
  77. thread = new Thread (new ThreadStart (Monitor));
  78. thread.IsBackground = true;
  79. thread.Start ();
  80. }
  81. }
  82. lock (watches) {
  83. data = (DefaultWatcherData) watches [fsw];
  84. if (data == null) {
  85. data = new DefaultWatcherData ();
  86. data.Files = new Hashtable ();
  87. watches [fsw] = data;
  88. }
  89. data.FSW = fsw;
  90. data.Directory = fsw.FullPath;
  91. data.FileMask = fsw.MangledFilter;
  92. data.IncludeSubdirs = fsw.IncludeSubdirectories;
  93. data.Enabled = true;
  94. data.DisabledTime = DateTime.MaxValue;
  95. UpdateDataAndDispatch (data, false);
  96. }
  97. }
  98. public void StopDispatching (FileSystemWatcher fsw)
  99. {
  100. DefaultWatcherData data;
  101. lock (watches) {
  102. data = (DefaultWatcherData) watches [fsw];
  103. if (data != null) {
  104. data.Enabled = false;
  105. data.DisabledTime = DateTime.Now;
  106. }
  107. }
  108. }
  109. void Monitor ()
  110. {
  111. int zeroes = 0;
  112. while (true) {
  113. lock (watches) {
  114. if (watches.Count > 0) {
  115. zeroes = 0;
  116. ArrayList removed = null;
  117. foreach (DefaultWatcherData data in watches.Values) {
  118. bool remove = UpdateDataAndDispatch (data, true);
  119. if (remove) {
  120. if (removed == null)
  121. removed = new ArrayList ();
  122. removed.Add (data);
  123. }
  124. }
  125. if (removed != null) {
  126. foreach (DefaultWatcherData data in removed)
  127. watches.Remove (data.FSW);
  128. removed.Clear ();
  129. removed = null;
  130. }
  131. } else {
  132. zeroes++;
  133. if (zeroes == 20)
  134. break;
  135. }
  136. }
  137. Thread.Sleep (750);
  138. }
  139. lock (this) {
  140. thread = null;
  141. }
  142. }
  143. bool UpdateDataAndDispatch (DefaultWatcherData data, bool dispatch)
  144. {
  145. if (!data.Enabled) {
  146. return (data.DisabledTime != DateTime.MaxValue &&
  147. (DateTime.Now - data.DisabledTime).TotalSeconds > 5);
  148. }
  149. DoFiles (data, data.Directory, dispatch);
  150. return false;
  151. }
  152. static void DispatchEvents (FileSystemWatcher fsw, FileAction action, string filename)
  153. {
  154. RenamedEventArgs renamed = null;
  155. lock (fsw) {
  156. fsw.DispatchEvents (action, filename, ref renamed);
  157. if (fsw.Waiting) {
  158. fsw.Waiting = false;
  159. System.Threading.Monitor.PulseAll (fsw);
  160. }
  161. }
  162. }
  163. void DoFiles (DefaultWatcherData data, string directory, bool dispatch)
  164. {
  165. bool direxists = Directory.Exists (directory);
  166. if (direxists && data.IncludeSubdirs) {
  167. foreach (string d in Directory.GetDirectories (directory))
  168. DoFiles (data, d, dispatch);
  169. }
  170. string [] files = null;
  171. if (direxists) {
  172. files = Directory.GetFileSystemEntries (directory, data.FileMask);
  173. } else {
  174. files = new string [0];
  175. }
  176. /* Set all as untested */
  177. foreach (string filename in data.Files.Keys) {
  178. FileData fd = (FileData) data.Files [filename];
  179. if (fd.Directory == directory)
  180. fd.NotExists = true;
  181. }
  182. /* New files */
  183. foreach (string filename in files) {
  184. FileData fd = (FileData) data.Files [filename];
  185. if (fd == null) {
  186. data.Files.Add (filename, CreateFileData (directory, filename));
  187. if (dispatch)
  188. DispatchEvents (data.FSW, FileAction.Added, filename);
  189. } else if (fd.Directory == directory) {
  190. fd.NotExists = false;
  191. }
  192. }
  193. if (!dispatch) // We only initialize the file list
  194. return;
  195. /* Removed files */
  196. ArrayList removed = null;
  197. foreach (string filename in data.Files.Keys) {
  198. FileData fd = (FileData) data.Files [filename];
  199. if (fd.NotExists) {
  200. if (removed == null)
  201. removed = new ArrayList ();
  202. removed.Add (filename);
  203. DispatchEvents (data.FSW, FileAction.Removed, filename);
  204. }
  205. }
  206. if (removed != null) {
  207. foreach (string filename in removed)
  208. data.Files.Remove (filename);
  209. removed = null;
  210. }
  211. /* Changed files */
  212. foreach (string filename in data.Files.Keys) {
  213. FileData fd = (FileData) data.Files [filename];
  214. DateTime creation, write;
  215. try {
  216. creation = File.GetCreationTime (filename);
  217. write = File.GetLastWriteTime (filename);
  218. } catch {
  219. /* Deleted */
  220. if (removed == null)
  221. removed = new ArrayList ();
  222. removed.Add (filename);
  223. DispatchEvents (data.FSW, FileAction.Removed, filename);
  224. continue;
  225. }
  226. if (creation != fd.CreationTime || write != fd.LastWriteTime) {
  227. fd.CreationTime = creation;
  228. fd.LastWriteTime = write;
  229. DispatchEvents (data.FSW, FileAction.Modified, filename);
  230. }
  231. }
  232. if (removed != null) {
  233. foreach (string filename in removed)
  234. data.Files.Remove (filename);
  235. }
  236. }
  237. static FileData CreateFileData (string directory, string filename)
  238. {
  239. FileData fd = new FileData ();
  240. string fullpath = Path.Combine (directory, filename);
  241. fd.Directory = directory;
  242. fd.Attributes = File.GetAttributes (fullpath);
  243. fd.CreationTime = File.GetCreationTime (fullpath);
  244. fd.LastWriteTime = File.GetLastWriteTime (fullpath);
  245. return fd;
  246. }
  247. }
  248. }