You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

558 lines
18 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  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 Externals.Compression.Core
  37. {
  38. #region EventArgs
  39. /// <summary>
  40. /// Event arguments for scanning.
  41. /// </summary>
  42. internal 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. internal 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. internal 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. internal 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. internal 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. internal 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. internal 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. internal 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. internal 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. internal delegate void FileFailureHandler(object sender, ScanFailureEventArgs e);
  260. #endregion
  261. /// <summary>
  262. /// FileSystemScanner provides facilities scanning of files and directories.
  263. /// </summary>
  264. internal 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 = null;
  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. {
  337. ScanFailureEventArgs args = new ScanFailureEventArgs(directory, e);
  338. handler(this, args);
  339. alive_ = args.ContinueRunning;
  340. }
  341. return result;
  342. }
  343. /// <summary>
  344. /// Raise the FileFailure event.
  345. /// </summary>
  346. /// <param name="file">The file name.</param>
  347. /// <param name="e">The exception detected.</param>
  348. bool OnFileFailure(string file, Exception e)
  349. {
  350. FileFailureHandler handler = FileFailure;
  351. bool result = (handler != null);
  352. if (result)
  353. {
  354. ScanFailureEventArgs args = new ScanFailureEventArgs(file, e);
  355. FileFailure(this, args);
  356. alive_ = args.ContinueRunning;
  357. }
  358. return result;
  359. }
  360. /// <summary>
  361. /// Raise the ProcessFile event.
  362. /// </summary>
  363. /// <param name="file">The file name.</param>
  364. void OnProcessFile(string file)
  365. {
  366. ProcessFileHandler handler = ProcessFile;
  367. if (handler != null)
  368. {
  369. ScanEventArgs args = new ScanEventArgs(file);
  370. handler(this, args);
  371. alive_ = args.ContinueRunning;
  372. }
  373. }
  374. /// <summary>
  375. /// Raise the complete file event
  376. /// </summary>
  377. /// <param name="file">The file name</param>
  378. void OnCompleteFile(string file)
  379. {
  380. CompletedFileHandler handler = CompletedFile;
  381. if (handler != null)
  382. {
  383. ScanEventArgs args = new ScanEventArgs(file);
  384. handler(this, args);
  385. alive_ = args.ContinueRunning;
  386. }
  387. }
  388. /// <summary>
  389. /// Raise the ProcessDirectory event.
  390. /// </summary>
  391. /// <param name="directory">The directory name.</param>
  392. /// <param name="hasMatchingFiles">Flag indicating if the directory has matching files.</param>
  393. void OnProcessDirectory(string directory, bool hasMatchingFiles)
  394. {
  395. ProcessDirectoryHandler handler = ProcessDirectory;
  396. if (handler != null)
  397. {
  398. DirectoryEventArgs args = new DirectoryEventArgs(directory, hasMatchingFiles);
  399. handler(this, args);
  400. alive_ = args.ContinueRunning;
  401. }
  402. }
  403. /// <summary>
  404. /// Scan a directory.
  405. /// </summary>
  406. /// <param name="directory">The base directory to scan.</param>
  407. /// <param name="recurse">True to recurse subdirectories, false to scan a single directory.</param>
  408. public void Scan(string directory, bool recurse)
  409. {
  410. alive_ = true;
  411. ScanDir(directory, recurse);
  412. }
  413. void ScanDir(string directory, bool recurse)
  414. {
  415. try
  416. {
  417. string[] names = System.IO.Directory.GetFiles(directory);
  418. bool hasMatch = false;
  419. for (int fileIndex = 0; fileIndex < names.Length; ++fileIndex)
  420. {
  421. if (!fileFilter_.IsMatch(names[fileIndex]))
  422. {
  423. names[fileIndex] = null;
  424. }
  425. else
  426. {
  427. hasMatch = true;
  428. }
  429. }
  430. OnProcessDirectory(directory, hasMatch);
  431. if (alive_ && hasMatch)
  432. {
  433. foreach (string fileName in names)
  434. {
  435. try
  436. {
  437. if (fileName != null)
  438. {
  439. OnProcessFile(fileName);
  440. if (!alive_)
  441. {
  442. break;
  443. }
  444. }
  445. }
  446. catch (Exception e)
  447. {
  448. if (!OnFileFailure(fileName, e))
  449. {
  450. throw;
  451. }
  452. }
  453. }
  454. }
  455. }
  456. catch (Exception e)
  457. {
  458. if (!OnDirectoryFailure(directory, e))
  459. {
  460. throw;
  461. }
  462. }
  463. if (alive_ && recurse)
  464. {
  465. try
  466. {
  467. string[] names = System.IO.Directory.GetDirectories(directory);
  468. foreach (string fulldir in names)
  469. {
  470. if ((directoryFilter_ == null) || (directoryFilter_.IsMatch(fulldir)))
  471. {
  472. ScanDir(fulldir, true);
  473. if (!alive_)
  474. {
  475. break;
  476. }
  477. }
  478. }
  479. }
  480. catch (Exception e)
  481. {
  482. if (!OnDirectoryFailure(directory, e))
  483. {
  484. throw;
  485. }
  486. }
  487. }
  488. }
  489. #region Instance Fields
  490. /// <summary>
  491. /// The file filter currently in use.
  492. /// </summary>
  493. IScanFilter fileFilter_;
  494. /// <summary>
  495. /// The directory filter currently in use.
  496. /// </summary>
  497. IScanFilter directoryFilter_;
  498. /// <summary>
  499. /// Flag indicating if scanning should continue running.
  500. /// </summary>
  501. bool alive_;
  502. #endregion
  503. }
  504. }