FileSystemScanner.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. // FileSystemScanner.cs
  2. //
  3. // Copyright 2005 John Reilly
  4. //
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. //
  19. // Linking this library statically or dynamically with other modules is
  20. // making a combined work based on this library. Thus, the terms and
  21. // conditions of the GNU General Public License cover the whole
  22. // combination.
  23. //
  24. // As a special exception, the copyright holders of this library give you
  25. // permission to link this library with independent modules to produce an
  26. // executable, regardless of the license terms of these independent
  27. // modules, and to copy and distribute the resulting executable under
  28. // terms of your choice, provided that you also meet, for each linked
  29. // independent module, the terms and conditions of the license of that
  30. // module. An independent module is a module which is not derived from
  31. // or based on this library. If you modify this library, you may extend
  32. // this exception to your version of the library, but you are not
  33. // obligated to do so. If you do not wish to do so, delete this
  34. // exception statement from your version.
  35. using System;
  36. namespace CommonMPQ.SharpZipLib.Core
  37. {
  38. #region EventArgs
  39. /// <summary>
  40. /// Event arguments for scanning.
  41. /// </summary>
  42. public class ScanEventArgs : EventArgs
  43. {
  44. #region Constructors
  45. /// <summary>
  46. /// Initialise a new instance of <see cref="ScanEventArgs"/>
  47. /// </summary>
  48. /// <param name="name">The file or directory name.</param>
  49. public ScanEventArgs(string name)
  50. {
  51. name_ = name;
  52. }
  53. #endregion
  54. /// <summary>
  55. /// The file or directory name for this event.
  56. /// </summary>
  57. public string Name
  58. {
  59. get { return name_; }
  60. }
  61. /// <summary>
  62. /// Get set a value indicating if scanning should continue or not.
  63. /// </summary>
  64. public bool ContinueRunning
  65. {
  66. get { return continueRunning_; }
  67. set { continueRunning_ = value; }
  68. }
  69. #region Instance Fields
  70. string name_;
  71. bool continueRunning_ = true;
  72. #endregion
  73. }
  74. /// <summary>
  75. /// Event arguments during processing of a single file or directory.
  76. /// </summary>
  77. public class ProgressEventArgs : EventArgs
  78. {
  79. #region Constructors
  80. /// <summary>
  81. /// Initialise a new instance of <see cref="ScanEventArgs"/>
  82. /// </summary>
  83. /// <param name="name">The file or directory name if known.</param>
  84. /// <param name="processed">The number of bytes processed so far</param>
  85. /// <param name="target">The total number of bytes to process, 0 if not known</param>
  86. public ProgressEventArgs(string name, long processed, long target)
  87. {
  88. name_ = name;
  89. processed_ = processed;
  90. target_ = target;
  91. }
  92. #endregion
  93. /// <summary>
  94. /// The name for this event if known.
  95. /// </summary>
  96. public string Name
  97. {
  98. get { return name_; }
  99. }
  100. /// <summary>
  101. /// Get set a value indicating wether scanning should continue or not.
  102. /// </summary>
  103. public bool ContinueRunning
  104. {
  105. get { return continueRunning_; }
  106. set { continueRunning_ = value; }
  107. }
  108. /// <summary>
  109. /// Get a percentage representing how much of the <see cref="Target"></see> has been processed
  110. /// </summary>
  111. /// <value>0.0 to 100.0 percent; 0 if target is not known.</value>
  112. public float PercentComplete
  113. {
  114. get
  115. {
  116. float result;
  117. if (target_ <= 0)
  118. {
  119. result = 0;
  120. }
  121. else
  122. {
  123. result = ((float)processed_ / (float)target_) * 100.0f;
  124. }
  125. return result;
  126. }
  127. }
  128. /// <summary>
  129. /// The number of bytes processed so far
  130. /// </summary>
  131. public long Processed
  132. {
  133. get { return processed_; }
  134. }
  135. /// <summary>
  136. /// The number of bytes to process.
  137. /// </summary>
  138. /// <remarks>Target may be 0 or negative if the value isnt known.</remarks>
  139. public long Target
  140. {
  141. get { return target_; }
  142. }
  143. #region Instance Fields
  144. string name_;
  145. long processed_;
  146. long target_;
  147. bool continueRunning_ = true;
  148. #endregion
  149. }
  150. /// <summary>
  151. /// Event arguments for directories.
  152. /// </summary>
  153. public class DirectoryEventArgs : ScanEventArgs
  154. {
  155. #region Constructors
  156. /// <summary>
  157. /// Initialize an instance of <see cref="DirectoryEventArgs"></see>.
  158. /// </summary>
  159. /// <param name="name">The name for this directory.</param>
  160. /// <param name="hasMatchingFiles">Flag value indicating if any matching files are contained in this directory.</param>
  161. public DirectoryEventArgs(string name, bool hasMatchingFiles)
  162. : base (name)
  163. {
  164. hasMatchingFiles_ = hasMatchingFiles;
  165. }
  166. #endregion
  167. /// <summary>
  168. /// Get a value indicating if the directory contains any matching files or not.
  169. /// </summary>
  170. public bool HasMatchingFiles
  171. {
  172. get { return hasMatchingFiles_; }
  173. }
  174. #region Instance Fields
  175. bool hasMatchingFiles_;
  176. #endregion
  177. }
  178. /// <summary>
  179. /// Arguments passed when scan failures are detected.
  180. /// </summary>
  181. public class ScanFailureEventArgs : EventArgs
  182. {
  183. #region Constructors
  184. /// <summary>
  185. /// Initialise a new instance of <see cref="ScanFailureEventArgs"></see>
  186. /// </summary>
  187. /// <param name="name">The name to apply.</param>
  188. /// <param name="e">The exception to use.</param>
  189. public ScanFailureEventArgs(string name, Exception e)
  190. {
  191. name_ = name;
  192. exception_ = e;
  193. continueRunning_ = true;
  194. }
  195. #endregion
  196. /// <summary>
  197. /// The applicable name.
  198. /// </summary>
  199. public string Name
  200. {
  201. get { return name_; }
  202. }
  203. /// <summary>
  204. /// The applicable exception.
  205. /// </summary>
  206. public Exception Exception
  207. {
  208. get { return exception_; }
  209. }
  210. /// <summary>
  211. /// Get / set a value indicating wether scanning should continue.
  212. /// </summary>
  213. public bool ContinueRunning
  214. {
  215. get { return continueRunning_; }
  216. set { continueRunning_ = value; }
  217. }
  218. #region Instance Fields
  219. string name_;
  220. Exception exception_;
  221. bool continueRunning_;
  222. #endregion
  223. }
  224. #endregion
  225. #region Delegates
  226. /// <summary>
  227. /// Delegate invoked before starting to process a directory.
  228. /// </summary>
  229. public delegate void ProcessDirectoryHandler(object sender, DirectoryEventArgs e);
  230. /// <summary>
  231. /// Delegate invoked before starting to process a file.
  232. /// </summary>
  233. /// <param name="sender">The source of the event</param>
  234. /// <param name="e">The event arguments.</param>
  235. public delegate void ProcessFileHandler(object sender, ScanEventArgs e);
  236. /// <summary>
  237. /// Delegate invoked during processing of a file or directory
  238. /// </summary>
  239. /// <param name="sender">The source of the event</param>
  240. /// <param name="e">The event arguments.</param>
  241. public delegate void ProgressHandler(object sender, ProgressEventArgs e);
  242. /// <summary>
  243. /// Delegate invoked when a file has been completely processed.
  244. /// </summary>
  245. /// <param name="sender">The source of the event</param>
  246. /// <param name="e">The event arguments.</param>
  247. public delegate void CompletedFileHandler(object sender, ScanEventArgs e);
  248. /// <summary>
  249. /// Delegate invoked when a directory failure is detected.
  250. /// </summary>
  251. /// <param name="sender">The source of the event</param>
  252. /// <param name="e">The event arguments.</param>
  253. public delegate void DirectoryFailureHandler(object sender, ScanFailureEventArgs e);
  254. /// <summary>
  255. /// Delegate invoked when a file failure is detected.
  256. /// </summary>
  257. /// <param name="sender">The source of the event</param>
  258. /// <param name="e">The event arguments.</param>
  259. public delegate void FileFailureHandler(object sender, ScanFailureEventArgs e);
  260. #endregion
  261. /// <summary>
  262. /// FileSystemScanner provides facilities scanning of files and directories.
  263. /// </summary>
  264. public class FileSystemScanner
  265. {
  266. #region Constructors
  267. /// <summary>
  268. /// Initialise a new instance of <see cref="FileSystemScanner"></see>
  269. /// </summary>
  270. /// <param name="filter">The <see cref="PathFilter">file filter</see> to apply when scanning.</param>
  271. public FileSystemScanner(string filter)
  272. {
  273. fileFilter_ = new PathFilter(filter);
  274. }
  275. /// <summary>
  276. /// Initialise a new instance of <see cref="FileSystemScanner"></see>
  277. /// </summary>
  278. /// <param name="fileFilter">The <see cref="PathFilter">file filter</see> to apply.</param>
  279. /// <param name="directoryFilter">The <see cref="PathFilter"> directory filter</see> to apply.</param>
  280. public FileSystemScanner(string fileFilter, string directoryFilter)
  281. {
  282. fileFilter_ = new PathFilter(fileFilter);
  283. directoryFilter_ = new PathFilter(directoryFilter);
  284. }
  285. /// <summary>
  286. /// Initialise a new instance of <see cref="FileSystemScanner"></see>
  287. /// </summary>
  288. /// <param name="fileFilter">The file <see cref="IScanFilter">filter</see> to apply.</param>
  289. public FileSystemScanner(IScanFilter fileFilter)
  290. {
  291. fileFilter_ = fileFilter;
  292. }
  293. /// <summary>
  294. /// Initialise a new instance of <see cref="FileSystemScanner"></see>
  295. /// </summary>
  296. /// <param name="fileFilter">The file <see cref="IScanFilter">filter</see> to apply.</param>
  297. /// <param name="directoryFilter">The directory <see cref="IScanFilter">filter</see> to apply.</param>
  298. public FileSystemScanner(IScanFilter fileFilter, IScanFilter directoryFilter)
  299. {
  300. fileFilter_ = fileFilter;
  301. directoryFilter_ = directoryFilter;
  302. }
  303. #endregion
  304. #region Delegates
  305. /// <summary>
  306. /// Delegate to invoke when a directory is processed.
  307. /// </summary>
  308. public ProcessDirectoryHandler ProcessDirectory;
  309. /// <summary>
  310. /// Delegate to invoke when a file is processed.
  311. /// </summary>
  312. public ProcessFileHandler ProcessFile;
  313. /// <summary>
  314. /// Delegate to invoke when processing for a file has finished.
  315. /// </summary>
  316. public CompletedFileHandler CompletedFile;
  317. /// <summary>
  318. /// Delegate to invoke when a directory failure is detected.
  319. /// </summary>
  320. public DirectoryFailureHandler DirectoryFailure;
  321. /// <summary>
  322. /// Delegate to invoke when a file failure is detected.
  323. /// </summary>
  324. public FileFailureHandler FileFailure;
  325. #endregion
  326. /// <summary>
  327. /// Raise the DirectoryFailure event.
  328. /// </summary>
  329. /// <param name="directory">The directory name.</param>
  330. /// <param name="e">The exception detected.</param>
  331. bool OnDirectoryFailure(string directory, Exception e)
  332. {
  333. DirectoryFailureHandler handler = DirectoryFailure;
  334. bool result = (handler != null);
  335. if ( result ) {
  336. ScanFailureEventArgs args = new ScanFailureEventArgs(directory, e);
  337. handler(this, args);
  338. alive_ = args.ContinueRunning;
  339. }
  340. return result;
  341. }
  342. /// <summary>
  343. /// Raise the FileFailure event.
  344. /// </summary>
  345. /// <param name="file">The file name.</param>
  346. /// <param name="e">The exception detected.</param>
  347. bool OnFileFailure(string file, Exception e)
  348. {
  349. FileFailureHandler handler = FileFailure;
  350. bool result = (handler != null);
  351. if ( result ){
  352. ScanFailureEventArgs args = new ScanFailureEventArgs(file, e);
  353. FileFailure(this, args);
  354. alive_ = args.ContinueRunning;
  355. }
  356. return result;
  357. }
  358. /// <summary>
  359. /// Raise the ProcessFile event.
  360. /// </summary>
  361. /// <param name="file">The file name.</param>
  362. void OnProcessFile(string file)
  363. {
  364. ProcessFileHandler handler = ProcessFile;
  365. if ( handler!= null ) {
  366. ScanEventArgs args = new ScanEventArgs(file);
  367. handler(this, args);
  368. alive_ = args.ContinueRunning;
  369. }
  370. }
  371. /// <summary>
  372. /// Raise the complete file event
  373. /// </summary>
  374. /// <param name="file">The file name</param>
  375. void OnCompleteFile(string file)
  376. {
  377. CompletedFileHandler handler = CompletedFile;
  378. if (handler != null)
  379. {
  380. ScanEventArgs args = new ScanEventArgs(file);
  381. handler(this, args);
  382. alive_ = args.ContinueRunning;
  383. }
  384. }
  385. /// <summary>
  386. /// Raise the ProcessDirectory event.
  387. /// </summary>
  388. /// <param name="directory">The directory name.</param>
  389. /// <param name="hasMatchingFiles">Flag indicating if the directory has matching files.</param>
  390. void OnProcessDirectory(string directory, bool hasMatchingFiles)
  391. {
  392. ProcessDirectoryHandler handler = ProcessDirectory;
  393. if ( handler != null ) {
  394. DirectoryEventArgs args = new DirectoryEventArgs(directory, hasMatchingFiles);
  395. handler(this, args);
  396. alive_ = args.ContinueRunning;
  397. }
  398. }
  399. /// <summary>
  400. /// Scan a directory.
  401. /// </summary>
  402. /// <param name="directory">The base directory to scan.</param>
  403. /// <param name="recurse">True to recurse subdirectories, false to scan a single directory.</param>
  404. public void Scan(string directory, bool recurse)
  405. {
  406. alive_ = true;
  407. ScanDir(directory, recurse);
  408. }
  409. void ScanDir(string directory, bool recurse)
  410. {
  411. try {
  412. string[] names = System.IO.Directory.GetFiles(directory);
  413. bool hasMatch = false;
  414. for (int fileIndex = 0; fileIndex < names.Length; ++fileIndex) {
  415. if ( !fileFilter_.IsMatch(names[fileIndex]) ) {
  416. names[fileIndex] = null;
  417. } else {
  418. hasMatch = true;
  419. }
  420. }
  421. OnProcessDirectory(directory, hasMatch);
  422. if ( alive_ && hasMatch ) {
  423. foreach (string fileName in names) {
  424. try {
  425. if ( fileName != null ) {
  426. OnProcessFile(fileName);
  427. if ( !alive_ ) {
  428. break;
  429. }
  430. }
  431. }
  432. catch (Exception e) {
  433. if (!OnFileFailure(fileName, e)) {
  434. throw;
  435. }
  436. }
  437. }
  438. }
  439. }
  440. catch (Exception e) {
  441. if (!OnDirectoryFailure(directory, e)) {
  442. throw;
  443. }
  444. }
  445. if ( alive_ && recurse ) {
  446. try {
  447. string[] names = System.IO.Directory.GetDirectories(directory);
  448. foreach (string fulldir in names) {
  449. if ((directoryFilter_ == null) || (directoryFilter_.IsMatch(fulldir))) {
  450. ScanDir(fulldir, true);
  451. if ( !alive_ ) {
  452. break;
  453. }
  454. }
  455. }
  456. }
  457. catch (Exception e) {
  458. if (!OnDirectoryFailure(directory, e)) {
  459. throw;
  460. }
  461. }
  462. }
  463. }
  464. #region Instance Fields
  465. /// <summary>
  466. /// The file filter currently in use.
  467. /// </summary>
  468. IScanFilter fileFilter_;
  469. /// <summary>
  470. /// The directory filter currently in use.
  471. /// </summary>
  472. IScanFilter directoryFilter_;
  473. /// <summary>
  474. /// Flag indicating if scanning should continue running.
  475. /// </summary>
  476. bool alive_;
  477. #endregion
  478. }
  479. }