DefaultWatcher.cs 7.7 KB

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