FileSystemScanner.cs 13 KB

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