|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Security.Policy; |
| 6 | +using Semmle.Util.Logging; |
| 7 | + |
| 8 | +namespace Semmle.Extraction.CSharp.DependencyFetching |
| 9 | +{ |
| 10 | + public class FileProvider |
| 11 | + { |
| 12 | + private static readonly HashSet<string> binaryFileExtensions = [".dll", ".exe"]; // TODO: add more binary file extensions. |
| 13 | + |
| 14 | + private readonly ILogger logger; |
| 15 | + private readonly FileInfo[] all; |
| 16 | + private readonly Lazy<FileInfo[]> allNonBinary; |
| 17 | + private readonly Lazy<string[]> smallNonBinary; |
| 18 | + private readonly Lazy<string[]> sources; |
| 19 | + private readonly Lazy<string[]> projects; |
| 20 | + private readonly Lazy<string[]> solutions; |
| 21 | + private readonly Lazy<string[]> dlls; |
| 22 | + private readonly Lazy<string[]> nugetConfigs; |
| 23 | + private readonly Lazy<string[]> globalJsons; |
| 24 | + private readonly Lazy<string[]> razorViews; |
| 25 | + private readonly Lazy<string?> rootNugetConfig; |
| 26 | + |
| 27 | + public FileProvider(DirectoryInfo sourceDir, ILogger logger) |
| 28 | + { |
| 29 | + SourceDir = sourceDir; |
| 30 | + this.logger = logger; |
| 31 | + |
| 32 | + all = GetAllFiles(); |
| 33 | + allNonBinary = new Lazy<FileInfo[]>(() => all.Where(f => !binaryFileExtensions.Contains(f.Extension.ToLowerInvariant())).ToArray()); |
| 34 | + smallNonBinary = new Lazy<string[]>(() => |
| 35 | + { |
| 36 | + var ret = SelectSmallFiles(allNonBinary.Value).SelectFileNames().ToArray(); |
| 37 | + logger.LogInfo($"Found {ret.Length} small non-binary files in {SourceDir}."); |
| 38 | + return ret; |
| 39 | + }); |
| 40 | + sources = new Lazy<string[]>(() => SelectTextFileNamesByExtension("source", ".cs")); |
| 41 | + projects = new Lazy<string[]>(() => SelectTextFileNamesByExtension("project", ".csproj")); |
| 42 | + solutions = new Lazy<string[]>(() => SelectTextFileNamesByExtension("solution", ".sln")); |
| 43 | + dlls = new Lazy<string[]>(() => SelectBinaryFileNamesByExtension("DLL", ".dll")); |
| 44 | + nugetConfigs = new Lazy<string[]>(() => allNonBinary.Value.SelectFileNamesByName("nuget.config").ToArray()); |
| 45 | + globalJsons = new Lazy<string[]>(() => allNonBinary.Value.SelectFileNamesByName("global.json").ToArray()); |
| 46 | + razorViews = new Lazy<string[]>(() => SelectTextFileNamesByExtension("razor view", ".cshtml", ".razor")); |
| 47 | + |
| 48 | + rootNugetConfig = new Lazy<string?>(() => all.SelectRootFiles(SourceDir).SelectFileNamesByName("nuget.config").FirstOrDefault()); |
| 49 | + } |
| 50 | + |
| 51 | + private string[] SelectTextFileNamesByExtension(string filetype, params string[] extensions) |
| 52 | + { |
| 53 | + var ret = allNonBinary.Value.SelectFileNamesByExtension(extensions).ToArray(); |
| 54 | + logger.LogInfo($"Found {ret.Length} {filetype} files in {SourceDir}."); |
| 55 | + return ret; |
| 56 | + } |
| 57 | + |
| 58 | + private string[] SelectBinaryFileNamesByExtension(string filetype, params string[] extensions) |
| 59 | + { |
| 60 | + var ret = all.SelectFileNamesByExtension(extensions).ToArray(); |
| 61 | + logger.LogInfo($"Found {ret.Length} {filetype} files in {SourceDir}."); |
| 62 | + return ret; |
| 63 | + } |
| 64 | + |
| 65 | + private IEnumerable<FileInfo> SelectSmallFiles(IEnumerable<FileInfo> files) |
| 66 | + { |
| 67 | + const int oneMb = 1_048_576; |
| 68 | + return files.Where(file => |
| 69 | + { |
| 70 | + if (file.Length > oneMb) |
| 71 | + { |
| 72 | + logger.LogDebug($"Skipping {file.FullName} because it is bigger than 1MB."); |
| 73 | + return false; |
| 74 | + } |
| 75 | + return true; |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + private FileInfo[] GetAllFiles() |
| 80 | + { |
| 81 | + logger.LogInfo($"Finding files in {SourceDir}..."); |
| 82 | + var files = SourceDir.GetFiles("*.*", new EnumerationOptions { RecurseSubdirectories = true }); |
| 83 | + |
| 84 | + var filteredFiles = files.Where(f => |
| 85 | + { |
| 86 | + try |
| 87 | + { |
| 88 | + if (f.Exists) |
| 89 | + { |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + logger.LogWarning($"File {f.FullName} could not be processed."); |
| 94 | + return false; |
| 95 | + } |
| 96 | + catch (Exception ex) |
| 97 | + { |
| 98 | + logger.LogWarning($"File {f.FullName} could not be processed: {ex.Message}"); |
| 99 | + return false; |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + var allFiles = new FilePathFilter(SourceDir, logger).Filter(filteredFiles).ToArray(); |
| 104 | + |
| 105 | + logger.LogInfo($"Found {allFiles.Length} files in {SourceDir}."); |
| 106 | + return allFiles; |
| 107 | + } |
| 108 | + |
| 109 | + public DirectoryInfo SourceDir { get; } |
| 110 | + public IEnumerable<string> SmallNonBinary => smallNonBinary.Value; |
| 111 | + public IEnumerable<string> Sources => sources.Value; |
| 112 | + public ICollection<string> Projects => projects.Value; |
| 113 | + public ICollection<string> Solutions => solutions.Value; |
| 114 | + public IEnumerable<string> Dlls => dlls.Value; |
| 115 | + public ICollection<string> NugetConfigs => nugetConfigs.Value; |
| 116 | + public string? RootNugetConfig => rootNugetConfig.Value; |
| 117 | + public IEnumerable<string> GlobalJsons => globalJsons.Value; |
| 118 | + public ICollection<string> RazorViews => razorViews.Value; |
| 119 | + } |
| 120 | +} |
0 commit comments