Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions source/CCM/Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
using CCMEngine;
using System.Threading;
using System.Xml;
using System.Text;

using System.Text;
using System.Text.RegularExpressions;
namespace CCM
{
internal struct AnalyzeThreadParameters
Expand Down Expand Up @@ -109,20 +109,43 @@ public bool IsValidFile(string filename)

public bool PathShouldBeExcluded(string path)
{
if (this.configFile != null)
{
string[] pathFolderNames = path
.Split(new char[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);

foreach (string pathToExclude in this.configFile.ExcludeFolders)
if (this.configFile != null)
{
if (path.ToLower().StartsWith(pathToExclude.ToLower()))
return true;

if (pathFolderNames.Contains(pathToExclude, StringComparer.InvariantCultureIgnoreCase))
return true;
string[] pathFolderNames = path
.Split(new char[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);

foreach (string pathToExclude in this.configFile.ExcludeFolders)
{
if (path.ToLower().StartsWith(pathToExclude.ToLower()))
return true;



try
{
//Check if a regex path was provided. If it was, treat it as a RegEx. If not, treat it as a literal string
//This is in here because "path" was getting treated the same as "path*"
string escapedInput = Regex.Escape(pathToExclude);
if (escapedInput != pathToExclude)
{

Regex regex = new Regex(pathToExclude, RegexOptions.IgnoreCase);
if (regex.IsMatch(path))
return true;
}
else //Path without special regex characters
{
if (pathFolderNames.Contains(pathToExclude, StringComparer.InvariantCultureIgnoreCase))
return true;
}
}
catch (ArgumentException)
{
// The provided regex pattern is invalid
return false;
}
}
}
}

return false;
}
Expand Down
70 changes: 69 additions & 1 deletion source/CCMEngine/ConfigurationFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text;
using System.Xml;
using System.Linq;
using System.Security;

namespace CCMEngine
{
Expand Down Expand Up @@ -69,6 +70,69 @@ private void ParseThreshold(XmlDocument doc)
this.Threshold = int.Parse(metrics.InnerText);
}

//Function to allow environment variable substitution within a string
//It will check for three different syntax styles: ${NAME}, $NAME, and %NAME%
private static string SubstituteEnvironmentVariables(string input)
{
var result = input;

try
{
// Find all occurrences of "${VAR_NAME}" pattern (Linux-style with brackets)
var startIndex = result.IndexOf("${");
while (startIndex != -1)
{
var endIndex = result.IndexOf("}", startIndex + 2);
if (endIndex != -1)
{
var variableName = result.Substring(startIndex + 2, endIndex - startIndex - 2);
var variableValue = Environment.GetEnvironmentVariable(variableName);
result = result.Replace("${" + variableName + "}", variableValue ?? string.Empty);
}
startIndex = result.IndexOf("${", endIndex + 1);
}

// Find all occurrences of "$VAR_NAME" pattern (Linux-style without brackets)
startIndex = result.IndexOf("$");
while (startIndex != -1)
{
var endIndex = result.IndexOfAny(new[] { ' ', '\t', '\n', '\r', '$' }, startIndex + 1);
if (endIndex == -1)
endIndex = result.Length;

var variableName = result.Substring(startIndex + 1, endIndex - startIndex - 1);
var variableValue = Environment.GetEnvironmentVariable(variableName);
result = result.Replace("$" + variableName, variableValue ?? string.Empty);

startIndex = result.IndexOf("$", endIndex + 1);
}

// Find all occurrences of "%VAR_NAME%" pattern (Windows-style)
startIndex = result.IndexOf("%");
while (startIndex != -1)
{
var endIndex = result.IndexOf("%", startIndex + 1);
if (endIndex != -1)
{
var variableName = result.Substring(startIndex + 1, endIndex - startIndex - 1);
var variableValue = Environment.GetEnvironmentVariable(variableName);
result = result.Replace("%" + variableName + "%", variableValue ?? string.Empty);
}
startIndex = result.IndexOf("%", endIndex + 1);
}
}
catch (SecurityException ex)
{
Console.WriteLine($"Error trying to process '{input}': {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred while processing '{input}': {ex.Message}");
}

return result;
}

private void ParseExcludes(XmlDocument doc)
{
XmlNode root = doc.SelectSingleNode("/ccm/exclude");
Expand Down Expand Up @@ -105,7 +169,11 @@ private void ParseAnalyzeFolders(XmlDocument doc)
XmlNodeList folderNodes = root.SelectNodes("folder");

foreach (XmlNode folder in folderNodes)
this.AnalyzeFolders.Add(((XmlElement)folder).InnerText);
{
//Allow substituting env vars in this folder string
string translatedFolder = SubstituteEnvironmentVariables(((XmlElement)folder).InnerText);
this.AnalyzeFolders.Add(translatedFolder);
}
}
}

Expand Down