Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 70 additions & 4 deletions ProtectionScan/Features/MainFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -55,6 +58,7 @@ public MainFeature()

Add(DebugInput);
Add(FileOnlyInput);
Add(JsonInput);
Add(NoContentsInput);
Add(NoArchivesInput);
Add(NoPathsInput);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -112,7 +116,7 @@ private static void Changed(object? source, ProtectionProgress value)
/// </summary>
/// <param name="scanner">Scanner object to use</param>
/// <param name="path">File or directory path</param>
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);
Expand All @@ -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)
{
Expand Down Expand Up @@ -166,7 +179,6 @@ private void WriteProtectionResultFile(string path, Dictionary<string, List<stri
catch
{
Console.WriteLine("Could not open protection log file for writing. Only a console log will be provided.");
FileOnly = false;
}

// Sort the keys for consistent output
Expand Down Expand Up @@ -199,5 +211,59 @@ private void WriteProtectionResultFile(string path, Dictionary<string, List<stri
// Dispose of the writer
sw?.Dispose();
}

#if NETCOREAPP
/// <summary>
/// Write the protection results from a single path to a json 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 WriteProtectionResultJson(string path, Dictionary<string, List<string>> 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();
}

/// <summary>
/// JSON serializer options for output printing
/// </summary>
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
}
}