Skip to content

Commit

Permalink
Merge pull request #29 from samsmithnz/FixToAccess
Browse files Browse the repository at this point in the history
fix to process/capture dirs you don't have access too
  • Loading branch information
samsmithnz authored Aug 5, 2022
2 parents 735bb49 + 91d1132 commit c2b6afa
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/DotNetCensus.Core/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.

using System.Diagnostics.CodeAnalysis;

//Not a clear rule. Suppressing.
[assembly: SuppressMessage("Style", "IDE0054:Use compound assignment", Justification = "<Pending>", Scope = "member", Target = "~M:DotNetCensus.Core.ProjectScanning.EnumerateFiles(System.String,System.String)~System.Collections.Generic.IEnumerable{System.IO.FileInfo}")]
56 changes: 55 additions & 1 deletion src/DotNetCensus.Core/ProjectScanning.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DotNetCensus.Core.Models;
using System.Security;
using System.Text.Json;

namespace DotNetCensus.Core
Expand All @@ -12,7 +13,8 @@ public static List<Project> SearchDirectory(string directory)
List<Project> projects = new();
if (string.IsNullOrEmpty(directory) == false)
{
foreach (FileInfo fileInfo in new DirectoryInfo(directory).GetFiles("*.*", SearchOption.AllDirectories))
//foreach (FileInfo fileInfo in new DirectoryInfo(directory).GetFiles("*.*", SearchOption.AllDirectories))
foreach (FileInfo fileInfo in EnumerateFiles(directory,"*.*"))
{
//if .NET project files are found, process them
switch (fileInfo.Extension.ToLower())
Expand Down Expand Up @@ -373,5 +375,57 @@ private static string GetHistoricalFrameworkVersion(string line)
return "unknown";
}
}

//From: https://stackoverflow.com/questions/37294702/directoryinfo-getfiles-error-system-unathorizedaccessexception
public static IEnumerable<FileInfo> EnumerateFiles(string path, string? searchPattern = null)
{
if (searchPattern != null)
{
searchPattern = searchPattern ?? "*";
}

var queue = new Queue<string>();
queue.Enqueue(path);

do
{
path = queue.Dequeue();
foreach (var file in SafeEnumerateFiles(path, searchPattern))
{
yield return new FileInfo(file);
}
foreach (var directory in SafeEnumerateDirectories(path))
{
queue.Enqueue(directory);
}
}
while (queue.Any());
}

static IEnumerable<string> SafeEnumerateFiles(string path, string searchPattern)
{
try
{
return Directory.EnumerateFiles(path, searchPattern);
}
catch (DirectoryNotFoundException) { }
catch (SecurityException) { }
catch (UnauthorizedAccessException) { }

return Enumerable.Empty<string>();
}

static IEnumerable<string> SafeEnumerateDirectories(string path)
{
try
{
return Directory.EnumerateDirectories(path);
}
catch (DirectoryNotFoundException) { }
catch (SecurityException) { }
catch (UnauthorizedAccessException) { }

return Enumerable.Empty<string>();
}
}
}
1 change: 1 addition & 0 deletions src/DotNetCensus.Tests/ConsoleAppTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,5 @@ public void RunSamplesWithInvalidParametersTest()
}
}
}

}

0 comments on commit c2b6afa

Please sign in to comment.