Skip to content

Commit

Permalink
Improved wildcard check for files
Browse files Browse the repository at this point in the history
Improved wildcard check for files
  • Loading branch information
HotCakeX committed Dec 21, 2024
1 parent 4ecaad7 commit 0f6009d
Showing 1 changed file with 105 additions and 32 deletions.
137 changes: 105 additions & 32 deletions AppControl Manager/Logic/GetFilesFast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ internal static class FileUtility
};


// The Default App Control supported extensions, case-insensitive
private static readonly HashSet<string> appControlExtensions = new(StringComparer.OrdinalIgnoreCase)
{
".sys", ".exe", ".com", ".dll", ".rll", ".ocx", ".msp", ".mst", ".msi",
".js", ".vbs", ".ps1", ".appx", ".bin", ".bat", ".hxs", ".mui", ".lex", ".mof"
};


/// <summary>
/// Custom HashSet comparer to compare two FileInfo objects based on their FullName (full path of file)
/// </summary>
Expand Down Expand Up @@ -65,18 +73,18 @@ internal static List<FileInfo> GetFilesFast(
// Create a Stopwatch instance and start measuring time
Stopwatch stopwatch = Stopwatch.StartNew();

// Use the Default App Control supported extensions and make them case-insensitive
HashSet<string> extensions = new(StringComparer.OrdinalIgnoreCase)
{
".sys", ".exe", ".com", ".dll", ".rll", ".ocx", ".msp", ".mst", ".msi",
".js", ".vbs", ".ps1", ".appx", ".bin", ".bat", ".hxs", ".mui", ".lex", ".mof"
};
// A HashSet used to store extensions to filter files
HashSet<string> extensions = new(StringComparer.OrdinalIgnoreCase);

// If custom extensions are provided, use them and make them case-insensitive
if (extensionsToFilterBy is { Length: > 0 })
{
extensions = new HashSet<string>(extensionsToFilterBy, StringComparer.OrdinalIgnoreCase);
}
else
{
extensions = appControlExtensions;
}

// Define a HashSet to store the final output
HashSet<FileInfo> output = new(new FileInfoComparer());
Expand All @@ -89,6 +97,9 @@ internal static List<FileInfo> GetFilesFast(
// To store all of the tasks
List<Task> tasks = [];


#region Directories

// Process directories if provided
if (directories is { Length: > 0 })
{
Expand All @@ -98,30 +109,51 @@ internal static List<FileInfo> GetFilesFast(
tasks.Add(Task.Run(() =>
{
IEnumerator<FileInfo> enumerator = directory.EnumerateFiles("*", options2).GetEnumerator();
while (true)

// If there is wildcard in extensions to filter by, then add all files without performing extension check
if (extensions.Contains("*"))
{
try
while (true)
{
// Move to the next file
if (!enumerator.MoveNext())
{
// If we reach the end of the enumeration, we break out of the loop
break;
}

// Check if the file extension is in the Extensions HashSet or Wildcard was used
if (extensions.Contains(enumerator.Current.Extension) || extensions.Contains("*"))
try
{
// Move to the next file
if (!enumerator.MoveNext())
{
// If we reach the end of the enumeration, we break out of the loop
break;
}
bc.Add(enumerator.Current);
}
catch { }
}
catch { }
}
// Filter files by extensions if there is no wildcard character for filtering
else
{
while (true)
{
try
{
// Move to the next file
if (!enumerator.MoveNext())
{
// If we reach the end of the enumeration, we break out of the loop
break;
}

// Check if the file extension is in the Extensions HashSet or Wildcard was used
if (extensions.Contains(enumerator.Current.Extension))
{
bc.Add(enumerator.Current);
}
}
catch { }
}
}
}));



// Check for immediate sub-directories and process them if present
DirectoryInfo[] subDirectories = directory.GetDirectories();

Expand All @@ -133,24 +165,45 @@ internal static List<FileInfo> GetFilesFast(
tasks.Add(Task.Run(() =>
{
IEnumerator<FileInfo> subEnumerator = subDirectory.EnumerateFiles("*", options).GetEnumerator();
while (true)

if (extensions.Contains("*"))
{
try
while (true)
{
// Move to the next file
if (!subEnumerator.MoveNext())
try
{
// If we reach the end of the enumeration, we break out of the loop
break;
// Move to the next file
if (!subEnumerator.MoveNext())
{
// If we reach the end of the enumeration, we break out of the loop
break;
}
bc.Add(subEnumerator.Current);
}

// Check if the file extension is in the Extensions HashSet or Wildcard was used
if (extensions.Contains(subEnumerator.Current.Extension) || extensions.Contains("*"))
catch { }
}
}
else
{
while (true)
{
try
{
bc.Add(subEnumerator.Current);
// Move to the next file
if (!subEnumerator.MoveNext())
{
// If we reach the end of the enumeration, we break out of the loop
break;
}

// Check if the file extension is in the Extensions HashSet or Wildcard was used
if (extensions.Contains(subEnumerator.Current.Extension))
{
bc.Add(subEnumerator.Current);
}
}
catch { }
}
catch { }
}
}));
}
Expand All @@ -159,18 +212,38 @@ internal static List<FileInfo> GetFilesFast(
}
}

#endregion


#region Files

// If files are provided, process them
if (files is { Length: > 0 })
{
foreach (FileInfo file in files)
// If user provided wildcard then add all files without checking their extensions
if (extensions.Contains("*"))
{
if (extensions.Contains(file.Extension))
foreach (FileInfo file in files)
{
bc.Add(file);
}
}
// If user provided no extensions to filter by or provided extensions that are not wildcard
else
{
foreach (FileInfo file in files)
{
if (extensions.Contains(file.Extension))
{
bc.Add(file);
}
}
}
}

#endregion


// Wait for all tasks to be completed
Task.WaitAll(tasks);

Expand Down

0 comments on commit 0f6009d

Please sign in to comment.