Skip to content

Commit 5cbe0f0

Browse files
fix
1 parent f067304 commit 5cbe0f0

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

ProtectionScan/Features/MainFeature.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ private void WriteProtectionResultJson(string path, Dictionary<string, List<stri
263263
// A nested dictionary is used to achieve proper serialization.
264264
var nestedDictionary = new Dictionary<string, object>();
265265
var trimmedPath = path.TrimEnd(['\\', '/']);
266+
267+
// Move nested dictionary into final dictionary with the base path as a key.
268+
//var finalDictionary = new Dictionary<string, Dictionary<string, object>>();
269+
//finalDictionary.Add(trimmedPath, nestedDictionary);
266270

267271
// Sort the keys for consistent output
268272
string[] keys = [.. protections.Keys];
@@ -283,7 +287,7 @@ private void WriteProtectionResultJson(string path, Dictionary<string, List<stri
283287
Array.Sort(fileProtections);
284288

285289
// Inserts key and protections into nested dictionary, with the key trimmed of the base path.
286-
InsertNode(nestedDictionary, key[trimmedPath.Length..], fileProtections, modifyNodeList);
290+
InsertNode(nestedDictionary, key[trimmedPath.Length..], trimmedPath, fileProtections, modifyNodeList);
287291
}
288292

289293
// Adds the non-leaf-node protections back in
@@ -297,15 +301,9 @@ private void WriteProtectionResultJson(string path, Dictionary<string, List<stri
297301

298302
modifyNodeList[i].Item1[modifyNodeList[i].Item2] = modifyNode;
299303
}
300-
301-
// Move nested dictionary into final dictionary with the base path as a key.
302-
var finalDictionary = new Dictionary<string, Dictionary<string, object>>()
303-
{
304-
{trimmedPath, nestedDictionary}
305-
};
306-
304+
307305
// Create the output data
308-
serializedData = System.Text.Json.JsonSerializer.Serialize(finalDictionary, jsonSerializerOptions);
306+
serializedData = System.Text.Json.JsonSerializer.Serialize(nestedDictionary, jsonSerializerOptions);
309307
}
310308
else
311309
{
@@ -334,15 +332,30 @@ private void WriteProtectionResultJson(string path, Dictionary<string, List<stri
334332
/// <param name="protections">The scanned protection(s) for a given file</param>
335333
private static void InsertNode(Dictionary<string, object> nestedDictionary,
336334
string path,
335+
string fullPath,
337336
string[] protections,
338337
List<(Dictionary<string, object>, string, string[])> modifyNodeList)
339338
{
340-
var current = nestedDictionary;
339+
341340
var pathParts = path.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries);
342341

342+
if (pathParts.Length <= 0)
343+
{
344+
modifyNodeList.Add((nestedDictionary, fullPath, protections));
345+
return;
346+
}
347+
348+
var current = nestedDictionary;
349+
if (!current.ContainsKey(fullPath))
350+
current[fullPath] = new Dictionary<string, object>();
351+
352+
current = (Dictionary<string, object>)current[fullPath];
353+
354+
343355
// Traverses the nested dictionary until the "leaf" dictionary is reached.
344356
for (int i = 0; i < pathParts.Length - 1; i++)
345357
{
358+
346359
var part = pathParts[i];
347360

348361
// Inserts new subdictionaries if one doesn't already exist
@@ -366,7 +379,6 @@ private static void InsertNode(Dictionary<string, object> nestedDictionary,
366379
current = (Dictionary<string, object>)current[part];
367380
}
368381

369-
// If the "leaf" dictionary has been reached, add the file and its protections.
370382
current.Add(pathParts[^1], protections);
371383
}
372384
#endif

0 commit comments

Comments
 (0)