From 055c79343ba68091262363fa9aa106eb44d99b76 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Wed, 14 Oct 2026 21:56:04 -0400 Subject: [PATCH] Add light cancellation token to main scan loop --- BinaryObjectScanner/Scanner.cs | 37 +++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/BinaryObjectScanner/Scanner.cs b/BinaryObjectScanner/Scanner.cs index 44e06f70..35f13067 100644 --- a/BinaryObjectScanner/Scanner.cs +++ b/BinaryObjectScanner/Scanner.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Threading; using BinaryObjectScanner.Data; using BinaryObjectScanner.Interfaces; using SabreTools.Collections.Extensions; @@ -44,6 +45,11 @@ namespace BinaryObjectScanner /// private readonly IProgress? _fileProgress; + /// + /// Optional cancellation token for interrupting scans + /// + private readonly CancellationToken? _cancellationToken; + #endregion #region Static Variables @@ -71,12 +77,14 @@ namespace BinaryObjectScanner /// Enable scanning subdirectories /// Enable including debug information /// Optional progress callback + /// Optional cancellation token public Scanner(bool scanArchives, bool scanContents, bool scanPaths, bool scanSubdirectories, bool includeDebug, - IProgress? fileProgress = null) + IProgress? fileProgress = null, + CancellationToken? cancellationToken = null) { _scanArchives = scanArchives; _scanContents = scanContents; @@ -84,6 +92,7 @@ namespace BinaryObjectScanner _scanSubdirectories = scanSubdirectories; _includeDebug = includeDebug; _fileProgress = fileProgress; + _cancellationToken = cancellationToken; #if NET462_OR_GREATER || NETCOREAPP || NETSTANDARD2_0_OR_GREATER // Register the codepages @@ -143,6 +152,19 @@ namespace BinaryObjectScanner var protections = new ProtectionDictionary(); foreach (string path in paths) { + // Check for cancellation + if (_cancellationToken?.IsCancellationRequested == true) + { + // Clear out any empty keys + protections.ClearEmptyKeys(); + + // If we're in debug, output the elasped time to console + if (_includeDebug) + Console.WriteLine($"Time elapsed: {DateTime.UtcNow.Subtract(startTime)}"); + + return protections; + } + // Directories scan each internal file individually if (Directory.Exists(path)) { @@ -160,6 +182,19 @@ namespace BinaryObjectScanner // Scan each file in directory separately for (int i = 0; i < files.Count; i++) { + // Check for cancellation + if (_cancellationToken?.IsCancellationRequested == true) + { + // Clear out any empty keys + protections.ClearEmptyKeys(); + + // If we're in debug, output the elasped time to console + if (_includeDebug) + Console.WriteLine($"Time elapsed: {DateTime.UtcNow.Subtract(startTime)}"); + + return protections; + } + // Get the current file string file = files[i];