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