[platform] refining sub-path detection in the Defender configuration script (IJPL-164957)

(cherry picked from commit c11857dc34299b734a93acfbf035c2dcd5b36944)

IJ-CR-149682

GitOrigin-RevId: e0263de6336c8078cd897d9c5b5365e618953540
This commit is contained in:
Roman Shevchenko
2024-11-28 18:44:10 +01:00
committed by intellij-monorepo-bot
parent 33ae337a93
commit c4c67df3c3

View File

@@ -34,10 +34,18 @@ try {
return $result
}
# returns `$true` when the given path is a sub-path of the given parent
function Test-StartsWith ([string] $path, [string] $parent) {
$pathNorm = $path.Trim('\') + '\'
$parentNorm = $parent.Trim('\') + '\'
return $pathNorm.Equals($parentNorm, [StringComparison]::OrdinalIgnoreCase) -or `
$pathNorm.StartsWith($parentNorm, [StringComparison]::OrdinalIgnoreCase)
}
# returns `$true` when a path is already covered by the exclusion list
function Test-Excluded ([string] $path, [string[]] $exclusions) {
foreach ($exclusion in $exclusions) {
if (((Get-Item -Path $exclusion) -is [System.IO.DirectoryInfo]) -and ([cultureinfo]::InvariantCulture.CompareInfo.IsPrefix($path, $exclusion, @("IgnoreCase"))) -and ($exclusion.TrimEnd('\').split('\').Count -gt $path.TrimEnd('\').Split('\').Count)) {
if (Test-StartsWith $path $exclusion) {
return $true
}
}