Files
BinaryObjectScanner/Test/Program.cs

101 lines
3.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using BurnOutSharp;
namespace Test
{
class Program
{
static void Main(string[] args)
{
2020-10-31 14:00:31 -07:00
// Create progress indicator
2020-11-12 22:47:33 -08:00
var p = new Progress<ProtectionProgress>();
p.ProgressChanged += Changed;
2020-10-31 14:00:31 -07:00
// Create scanner for all paths
2020-10-31 14:00:31 -07:00
var scanner = new Scanner(p)
{
2021-08-24 15:19:23 -07:00
IncludeDebug = true,
2020-10-31 14:00:31 -07:00
ScanAllFiles = false,
ScanArchives = true,
2020-10-31 14:48:25 -07:00
ScanPackers = true,
2020-10-31 14:00:31 -07:00
};
2021-03-23 16:55:19 -07:00
// Loop through the input paths
foreach (string arg in args)
{
2021-03-23 16:55:19 -07:00
GetAndWriteProtections(scanner, arg);
}
Console.WriteLine("Press enter to close the program...");
2021-03-23 16:55:19 -07:00
Console.ReadLine();
}
/// <summary>
/// Wrapper to get and log protections for a single path
/// </summary>
/// <param name="scanner">Scanner object to use</param>
/// <param name="path">File or directory path</param>
private static void GetAndWriteProtections(Scanner scanner, string path)
{
// An invalid path can't be scanned
if (!Directory.Exists(path) && !File.Exists(path))
{
Console.WriteLine($"{path} does not exist, skipping...");
return;
}
try
{
var protections = scanner.GetProtections(path);
WriteProtectionResultFile(path, protections);
}
catch (Exception ex)
{
using (StreamWriter sw = new StreamWriter(File.OpenWrite($"{DateTime.Now:yyyy-MM-dd_HHmmss}-exception.txt")))
{
2021-08-29 22:39:04 -07:00
sw.WriteLine(ex);
}
}
2021-03-23 16:55:19 -07:00
}
2021-03-23 16:55:19 -07:00
/// <summary>
/// Write the protection results from a single path to file, if possible
/// </summary>
/// <param name="path">File or directory path</param>
/// <param name="protections">Dictionary of protections found, if any</param>
private static void WriteProtectionResultFile(string path, ConcurrentDictionary<string, ConcurrentQueue<string>> protections)
2021-03-23 16:55:19 -07:00
{
if (protections == null)
{
Console.WriteLine($"No protections found for {path}");
return;
}
using (var sw = new StreamWriter(File.OpenWrite($"{DateTime.Now:yyyy-MM-dd_HHmmss}.txt")))
{
foreach (string key in protections.Keys.OrderBy(k => k))
2021-03-23 16:55:19 -07:00
{
// Skip over files with no protection
if (protections[key] == null || !protections[key].Any())
continue;
string line = $"{key}: {string.Join(", ", protections[key].OrderBy(p => p))}";
2021-03-23 16:55:19 -07:00
Console.WriteLine(line);
sw.WriteLine(line);
}
}
}
/// <summary>
/// Protection progress changed handler
/// </summary>
2020-11-12 22:47:33 -08:00
private static void Changed(object source, ProtectionProgress value)
{
2019-09-29 12:08:31 -07:00
Console.WriteLine($"{value.Percentage * 100:N2}%: {value.Filename} - {value.Protection}");
}
}
}