From 8667c2e2d1efec4e23883dc4cecd1c34e157ae47 Mon Sep 17 00:00:00 2001 From: HeroponRikiBestest Date: Fri, 7 Nov 2025 21:09:39 -0500 Subject: [PATCH 1/4] actual first pr --- ProtectionScan/Features/MainFeature.cs | 74 ++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 4 deletions(-) diff --git a/ProtectionScan/Features/MainFeature.cs b/ProtectionScan/Features/MainFeature.cs index 7c104bd6..9945bbfc 100644 --- a/ProtectionScan/Features/MainFeature.cs +++ b/ProtectionScan/Features/MainFeature.cs @@ -28,7 +28,10 @@ internal sealed class MainFeature : Feature private const string _fileOnlyName = "file-only"; internal readonly FlagInput FileOnlyInput = new(_fileOnlyName, ["-f", "--file"], "Print to file only"); - + + private const string _jsonName = "json"; + internal readonly FlagInput JsonInput = new(_jsonName, ["-j", "--json"], "Output to json file"); + private const string _noArchivesName = "no-archives"; internal readonly FlagInput NoArchivesInput = new(_noArchivesName, ["-na", "--no-archives"], "Disable scanning archives"); @@ -55,6 +58,7 @@ public MainFeature() Add(DebugInput); Add(FileOnlyInput); + Add(JsonInput); Add(NoContentsInput); Add(NoArchivesInput); Add(NoPathsInput); @@ -84,7 +88,7 @@ public override bool Execute() for (int i = 0; i < Inputs.Count; i++) { string arg = Inputs[i]; - GetAndWriteProtections(scanner, arg); + GetAndWriteProtections(scanner, arg, GetBoolean(_jsonName)); } return true; @@ -112,7 +116,7 @@ private static void Changed(object? source, ProtectionProgress value) /// /// Scanner object to use /// File or directory path - private void GetAndWriteProtections(Scanner scanner, string path) + private void GetAndWriteProtections(Scanner scanner, string path, bool json) { // Normalize by getting the full path path = Path.GetFullPath(path); @@ -127,7 +131,16 @@ private void GetAndWriteProtections(Scanner scanner, string path) try { var protections = scanner.GetProtections(path); +#if NETCOREAPP + + if (json) + WriteProtectionResultJson(path, protections); + else + WriteProtectionResultFile(path, protections); +#else WriteProtectionResultFile(path, protections); +#endif + } catch (Exception ex) { @@ -166,7 +179,6 @@ private void WriteProtectionResultFile(string path, Dictionary + /// Write the protection results from a single path to a json file, if possible + /// + /// File or directory path + /// Dictionary of protections found, if any + private static void WriteProtectionResultJson(string path, Dictionary> protections) + { + if (protections == null) + { + Console.WriteLine($"No protections found for {path}"); + return; + } + + // Attempt to open a protection file for writing + StreamWriter? jsw = null; + try + { + jsw = new StreamWriter(File.OpenWrite($"protection-{DateTime.Now:yyyy-MM-dd_HHmmss.ffff}.json")); + } + catch + { + Console.WriteLine("Could not open protection log file for writing. Only a console log will be provided."); + } + + // Create the output data + string serializedData = System.Text.Json.JsonSerializer.Serialize(protections, JsonSerializerOptions); + + // Write the output data + // TODO: this prints plus symbols wrong, probably some other things too + jsw?.WriteLine(serializedData); + jsw?.Flush(); + + // Dispose of the writer + jsw?.Dispose(); + } + + /// + /// JSON serializer options for output printing + /// + private static System.Text.Json.JsonSerializerOptions JsonSerializerOptions + { + get + { +#if NETCOREAPP3_1 + var serializer = new System.Text.Json.JsonSerializerOptions { WriteIndented = true }; +#else + var serializer = new System.Text.Json.JsonSerializerOptions { IncludeFields = true, WriteIndented = true }; +#endif + return serializer; + } + } +#endif } } From 8ab7fbb06d2fc6597a2fc3d05da03f468e729706 Mon Sep 17 00:00:00 2001 From: HeroponRikiBestest Date: Fri, 7 Nov 2025 21:27:57 -0500 Subject: [PATCH 2/4] Initial fixes --- ProtectionScan/Features/MainFeature.cs | 34 ++++++++++++-------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/ProtectionScan/Features/MainFeature.cs b/ProtectionScan/Features/MainFeature.cs index 9945bbfc..17253dcc 100644 --- a/ProtectionScan/Features/MainFeature.cs +++ b/ProtectionScan/Features/MainFeature.cs @@ -50,6 +50,11 @@ internal sealed class MainFeature : Feature /// Output information to file only, skip printing to console /// public bool FileOnly { get; private set; } + + /// + /// Output information to json + /// + public bool JsonFlag { get; private set; } public MainFeature() : base(DisplayName, _flags, _description) @@ -58,7 +63,9 @@ public MainFeature() Add(DebugInput); Add(FileOnlyInput); + #if NETCOREAPP Add(JsonInput); + #endif Add(NoContentsInput); Add(NoArchivesInput); Add(NoPathsInput); @@ -74,6 +81,7 @@ public override bool Execute() // Get the options from the arguments FileOnly = GetBoolean(_fileOnlyName); + JsonFlag = GetBoolean(_jsonName); // Create scanner for all paths var scanner = new Scanner( @@ -88,7 +96,7 @@ public override bool Execute() for (int i = 0; i < Inputs.Count; i++) { string arg = Inputs[i]; - GetAndWriteProtections(scanner, arg, GetBoolean(_jsonName)); + GetAndWriteProtections(scanner, arg); } return true; @@ -116,7 +124,7 @@ private static void Changed(object? source, ProtectionProgress value) /// /// Scanner object to use /// File or directory path - private void GetAndWriteProtections(Scanner scanner, string path, bool json) + private void GetAndWriteProtections(Scanner scanner, string path) { // Normalize by getting the full path path = Path.GetFullPath(path); @@ -131,16 +139,12 @@ private void GetAndWriteProtections(Scanner scanner, string path, bool json) try { var protections = scanner.GetProtections(path); -#if NETCOREAPP - if (json) - WriteProtectionResultJson(path, protections); - else - WriteProtectionResultFile(path, protections); -#else WriteProtectionResultFile(path, protections); + +#if NETCOREAPP + WriteProtectionResultJson(path, protections); #endif - } catch (Exception ex) { @@ -179,6 +183,7 @@ private void WriteProtectionResultFile(string path, Dictionary Date: Fri, 7 Nov 2025 21:46:04 -0500 Subject: [PATCH 3/4] Second round of fixes --- ProtectionScan/Features/MainFeature.cs | 33 ++++++++++---------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/ProtectionScan/Features/MainFeature.cs b/ProtectionScan/Features/MainFeature.cs index 17253dcc..f8da631f 100644 --- a/ProtectionScan/Features/MainFeature.cs +++ b/ProtectionScan/Features/MainFeature.cs @@ -63,9 +63,9 @@ public MainFeature() Add(DebugInput); Add(FileOnlyInput); - #if NETCOREAPP +#if NETCOREAPP Add(JsonInput); - #endif +#endif Add(NoContentsInput); Add(NoArchivesInput); Add(NoPathsInput); @@ -236,32 +236,25 @@ private static void WriteProtectionResultJson(string path, Dictionary /// JSON serializer options for output printing /// private static System.Text.Json.JsonSerializerOptions JsonSerializerOptions - { - get - { - var serializer = new System.Text.Json.JsonSerializerOptions { WriteIndented = true }; - return serializer; - } - } + => new System.Text.Json.JsonSerializerOptions { WriteIndented = true }; #endif } } From 9628a6b480d29791b02392c95f672dfdd9fd7c8d Mon Sep 17 00:00:00 2001 From: HeroponRikiBestest Date: Fri, 7 Nov 2025 21:57:59 -0500 Subject: [PATCH 4/4] Final fix --- ProtectionScan/Features/MainFeature.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/ProtectionScan/Features/MainFeature.cs b/ProtectionScan/Features/MainFeature.cs index f8da631f..6288f533 100644 --- a/ProtectionScan/Features/MainFeature.cs +++ b/ProtectionScan/Features/MainFeature.cs @@ -28,10 +28,12 @@ internal sealed class MainFeature : Feature private const string _fileOnlyName = "file-only"; internal readonly FlagInput FileOnlyInput = new(_fileOnlyName, ["-f", "--file"], "Print to file only"); - - private const string _jsonName = "json"; + +#if NETCOREAPP + private const string _jsonName = "json"; internal readonly FlagInput JsonInput = new(_jsonName, ["-j", "--json"], "Output to json file"); - +#endif + private const string _noArchivesName = "no-archives"; internal readonly FlagInput NoArchivesInput = new(_noArchivesName, ["-na", "--no-archives"], "Disable scanning archives"); @@ -81,7 +83,9 @@ public override bool Execute() // Get the options from the arguments FileOnly = GetBoolean(_fileOnlyName); +#if NETCOREAPP JsonFlag = GetBoolean(_jsonName); +#endif // Create scanner for all paths var scanner = new Scanner(