Skip to content

Commit

Permalink
- Now prints paths of unreadable/inaccessible files to console
Browse files Browse the repository at this point in the history
  • Loading branch information
sLill committed Dec 10, 2024
1 parent d43177f commit f1d4e02
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
8 changes: 5 additions & 3 deletions WindowsGrep/WindowsGrep.Engine/Data/SearchMetrics.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace WindowsGrep.Engine
using System.Collections.Generic;

namespace WindowsGrep.Engine
{
public class SearchMetrics
{
Expand All @@ -9,9 +11,9 @@ public class SearchMetrics

public int ReplacedSuccessCount { get; set; }

public int FileReadFailedCount { get; set; }
public List<string> FailedReadFiles { get; set; } = new List<string>();

public int FileWriteFailedCount { get; set; }
public List<string> FailedWriteFiles { get; set; } = new List<string>();
#endregion Properties..
}
}
20 changes: 12 additions & 8 deletions WindowsGrep/WindowsGrep.Engine/GrepEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private static async Task GetFileContentMatchesAsync(CommandResultCollection com
catch
{
lock (_metricsLock)
searchMetrics.FileReadFailedCount++;
searchMetrics.FailedReadFiles.Add(file);
}
});
}
Expand Down Expand Up @@ -265,7 +265,7 @@ private static void GetFileHashMatchesAsync(CommandResultCollection commandResul
catch
{
lock (_metricsLock)
searchMetrics.FileReadFailedCount++;
searchMetrics.FailedReadFiles.Add(file);
}
});

Expand Down Expand Up @@ -471,7 +471,7 @@ private static async Task<List<ConsoleItem>> PerformWriteOperationsAsync(GrepCom
consoleItemCollection.Add(new ConsoleItem() { ForegroundColor = ConsoleColor.Gray, BackgroundColor = ConsoleColor.DarkRed, Value = $"Access Denied" });

lock (_metricsLock)
searchMetrics.FileWriteFailedCount++;
searchMetrics.FailedWriteFiles.Add(filePath);
}
finally
{
Expand Down Expand Up @@ -518,18 +518,22 @@ private static void PublishCommandSummary(GrepCommand grepCommand, IList<Command

private static void PublishFileAccessSummary(SearchMetrics searchMetrics)
{
if (searchMetrics.FileReadFailedCount > 0 || searchMetrics.FileWriteFailedCount > 0)
if (searchMetrics.FailedReadFiles.Any() || searchMetrics.FailedWriteFiles.Any())
{
if (searchMetrics.FileReadFailedCount > 0)
if (searchMetrics.FailedReadFiles.Any())
{
string unreachableFiles = $"[{searchMetrics.FileReadFailedCount} file(s) unreadable/inaccessible]";
string unreachableFiles = $"[{searchMetrics.FailedReadFiles} file(s) unreadable/inaccessible]{Environment.NewLine}";
ConsoleUtils.WriteConsoleItem(new ConsoleItem() { ForegroundColor = ConsoleColor.Red, Value = unreachableFiles });

searchMetrics.FailedReadFiles.ForEach(x => ConsoleUtils.WriteConsoleItem(new ConsoleItem() { ForegroundColor = ConsoleColor.Red, Value = $"{x}{Environment.NewLine}" }));
}

if (searchMetrics.FileWriteFailedCount > 0)
if (searchMetrics.FailedWriteFiles.Any())
{
string unwriteableFiles = $"[{searchMetrics.FileWriteFailedCount} file(s) could not be modified]";
string unwriteableFiles = $"[{searchMetrics.FailedWriteFiles} file(s) could not be modified]{Environment.NewLine}";
ConsoleUtils.WriteConsoleItem(new ConsoleItem() { ForegroundColor = ConsoleColor.Red, Value = unwriteableFiles });

searchMetrics.FailedWriteFiles.ForEach(x => ConsoleUtils.WriteConsoleItem(new ConsoleItem() { ForegroundColor = ConsoleColor.Red, Value = $"{x}{Environment.NewLine}" }));
}

ConsoleUtils.WriteConsoleItem(new ConsoleItem() { Value = Environment.NewLine });
Expand Down

0 comments on commit f1d4e02

Please sign in to comment.