waypoint debug

This commit is contained in:
2026-09-25 15:27:37 -06:00
parent 776a52c3c3
commit b504218205
+135 -138
View File
@@ -101,52 +101,51 @@ EXAMPLES
3. Verify checksums from verification files recursively: 3. Verify checksums from verification files recursively:
.\checksum.ps1 -c checksums.sha256 -r .\checksum.ps1 -c checksums.sha256 -r
'@ '@
} };
function Get-AlgorithmFromHashLength { function Get-AlgorithmFromHashLength {
param([string]$HashString) param([string]$HashString);
switch ($HashString.Trim().Length) { switch ($HashString.Trim().Length) {
32 { return 'MD5' } 32 { return 'MD5'; }
40 { return 'SHA1' } 40 { return 'SHA1'; }
64 { return 'SHA256' } 64 { return 'SHA256'; }
96 { return 'SHA384' } 96 { return 'SHA384'; }
128 { return 'SHA512' } 128 { return 'SHA512'; }
default { default {
Write-Error "checksum: Could not auto-detect algorithm for hash length $($HashString.Length). Please specify -Algorithm." Write-Error "checksum: Could not auto-detect algorithm for hash length $($HashString.Length). Please specify -Algorithm.";
return $null return $null;
} }
} };
} };
} }
process { process {
# Bulletproof help check (catches --help passed positionally) $helpInPath =$false;
$helpInPath =$false
if ($FilePath) { if ($FilePath) {
foreach ($p in ($FilePath)) { foreach ($p in ($FilePath)) {
if ($p -match '^(?i)[-/]{1,2}(h\vert{}help)$') { if ($p -match '^(?i)[-/]{1,2}(h\vert{}help)$') {
$helpInPath =$true $helpInPath =$true;
break break;
} }
} };
} };
if ($Help.IsPresent -or$helpInPath) { if ($Help.IsPresent -or$helpInPath) {
Show-Help Show-Help;
return return;
} };
if (-not [string]::IsNullOrWhiteSpace($Algorithm)) { if (-not [string]::IsNullOrWhiteSpace($Algorithm)) {
$Algorithm =$Algorithm.ToUpper() $Algorithm =$Algorithm.ToUpper();
} };
# --- ITEM GATHERING (Generate & Match Modes) --- # --- ITEM GATHERING (Generate & Match Modes) ---
if ($PSCmdlet.ParameterSetName -in @('Generate', 'Match')) { if ($PSCmdlet.ParameterSetName -in @('Generate', 'Match')) {
if (-not $FilePath -or$FilePath.Count -eq 0) { if (-not $FilePath -or$FilePath.Count -eq 0) {
Write-Error "checksum: Missing file path parameter. Use -Help for usage." Write-Error "checksum: Missing file path parameter. Use -Help for usage.";
return return;
} };
$allItems = @(); $allItems = @();
$isWildcardGlobal =$false; $isWildcardGlobal =$false;
@@ -154,212 +153,210 @@ process {
foreach ($pathEntry in ($FilePath)) { foreach ($pathEntry in ($FilePath)) {
$isWildcard = ($pathEntry -match '\*'); $isWildcard = ($pathEntry -match '\*');
if ($isWildcard) { $isWildcardGlobal =$true } if ($isWildcard) { $isWildcardGlobal =$true; };
$resolvedItems = @(); $resolvedItems = @();
if ($Recursive) { if ($Recursive) {
# Parens around variables prevent space-stripping crashes $resolvedItems = Get-ChildItem -Path ($pathEntry) -Recurse -File -ErrorAction 'SilentlyContinue';
$resolvedItems = Get-ChildItem -Path ($pathEntry) -Recurse -File -ErrorAction 'SilentlyContinue'
} else { } else {
if ($isWildcard -or ($pathEntry -match '\?')) { if ($isWildcard -or ($pathEntry -match '\?')) {
$resolvedItems = Get-ChildItem -Path ($pathEntry) -File -ErrorAction 'SilentlyContinue' $resolvedItems = Get-ChildItem -Path ($pathEntry) -File -ErrorAction 'SilentlyContinue';
} else { } else {
$resolvedItems = Get-Item -Path ($pathEntry) -ErrorAction 'SilentlyContinue' $resolvedItems = Get-Item -Path ($pathEntry) -ErrorAction 'SilentlyContinue';
} }
} };
if (-not $resolvedItems) { if (-not $resolvedItems) {
Write-Error "checksum: $($pathEntry)`: No such file or directory" Write-Error "checksum: $($pathEntry)`: No such file or directory";
continue continue;
} };
$explicitFileFound = $false; $explicitFileFound = $false;
if ((-not $isWildcard) -and (Test-Path -Path ($pathEntry) -PathType 'Leaf')) { if ((-not $isWildcard) -and (Test-Path -Path ($pathEntry) -PathType 'Leaf')) {
$explicitFileFound = $true $explicitFileFound = $true;
} };
foreach ($item in ($resolvedItems)) { foreach ($item in ($resolvedItems)) {
if ($item.PSIsContainer) { continue } if ($item.PSIsContainer) { continue; };
if (-not $explicitFileFound) { if (-not $explicitFileFound) {
if ($skipExtensions -contains $item.Extension.ToLower()) { if ($skipExtensions -contains $item.Extension.ToLower()) {
continue continue;
} }
} };
$allItems += $item $allItems += $item;
} };
} };
} };
# --- DIRECT MATCH MODE (-m / -Match) --- # --- DIRECT MATCH MODE (-m / -Match) ---
if ($PSCmdlet.ParameterSetName -eq 'Match') { if ($PSCmdlet.ParameterSetName -eq 'Match') {
if ([string]::IsNullOrWhiteSpace($Algorithm)) { if ([string]::IsNullOrWhiteSpace($Algorithm)) {
$Algorithm = Get-AlgorithmFromHashLength -HashString ($Match) $Algorithm = Get-AlgorithmFromHashLength -HashString ($Match);
if (-not $Algorithm) { return } if (-not $Algorithm) { return; };
} };
$expectedHash = $Match.Trim().ToLower(); $expectedHash = $Match.Trim().ToLower();
foreach ($item in ($allItems)) { foreach ($item in ($allItems)) {
$actualHash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower() $actualHash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower();
$targetFile = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue' $targetFile = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue';
if (-not $targetFile) { $targetFile = $item.Name } if (-not $targetFile) { $targetFile = $item.Name; };
if ($targetFile.StartsWith(".\")) { $targetFile = $targetFile.Substring(2) } if ($targetFile.StartsWith(".\")) { $targetFile = $targetFile.Substring(2); };
if ($actualHash -eq $expectedHash) { if ($actualHash -eq $expectedHash) {
Write-Host "${targetFile}: " -NoNewline Write-Host "${targetFile}: " -NoNewline;
Write-Host "OK" -ForegroundColor Green Write-Host "OK" -ForegroundColor Green;
} else { } else {
Write-Host "${targetFile}: " -NoNewline Write-Host "${targetFile}: " -NoNewline;
Write-Host "FAILED" -ForegroundColor Red Write-Host "FAILED" -ForegroundColor Red;
} };
} };
} }
# --- VERIFICATION MODE (-c / -Check) --- # --- VERIFICATION MODE (-c / -Check) ---
elseif ($PSCmdlet.ParameterSetName -eq 'Verify') { elseif ($PSCmdlet.ParameterSetName -eq 'Verify') {
$checkFiles = @(); $checkFiles = @();
if ($Recursive) { if ($Recursive) {
$checkFileName = Split-Path -Path ($Check) -Leaf $checkFileName = Split-Path -Path ($Check) -Leaf;
$checkFiles = Get-ChildItem -Path '.\' -Filter ($checkFileName) -Recurse -File -ErrorAction 'SilentlyContinue' $checkFiles = Get-ChildItem -Path '.\' -Filter ($checkFileName) -Recurse -File -ErrorAction 'SilentlyContinue';
} else { } else {
if (Test-Path -Path ($Check) -PathType 'Leaf') { if (Test-Path -Path ($Check) -PathType 'Leaf') {
$checkFiles = @(Get-Item -Path ($Check)) $checkFiles = @(Get-Item -Path ($Check));
} }
} };
if (-not $checkFiles -or $checkFiles.Count -eq 0) { if (-not $checkFiles -or $checkFiles.Count -eq 0) {
Write-Error "checksum: $($Check)`: No such file or directory" Write-Error "checksum: $($Check)`: No such file or directory";
return return;
} };
foreach ($cf in ($checkFiles)) { foreach ($cf in ($checkFiles)) {
$lines = Get-Content -Path ($cf.FullName) $lines = Get-Content -Path ($cf.FullName);
$baseDir =$cf.DirectoryName $baseDir =$cf.DirectoryName;
$fileAlgo =$Algorithm $fileAlgo =$Algorithm;
if ([string]::IsNullOrWhiteSpace($fileAlgo)) { if ([string]::IsNullOrWhiteSpace($fileAlgo)) {
foreach ($line in ($lines)) { foreach ($line in ($lines)) {
if ([string]::IsNullOrWhiteSpace($line) -or$line.StartsWith("#")) { continue } if ([string]::IsNullOrWhiteSpace($line) -or$line.StartsWith("#")) { continue; };
if ($line -match '^([a-fA-F0-9]+)\s+') { if ($line -match '^([a-fA-F0-9]+)\s+') {
$fileAlgo = Get-AlgorithmFromHashLength -HashString ($Matches[1]) $fileAlgo = Get-AlgorithmFromHashLength -HashString ($Matches[1]);
break break;
} }
} };
if ([string]::IsNullOrWhiteSpace($fileAlgo)) {$fileAlgo = 'SHA256' } if ([string]::IsNullOrWhiteSpace($fileAlgo)) {$fileAlgo = 'SHA256'; };
} };
foreach ($line in ($lines)) { foreach ($line in ($lines)) {
if ([string]::IsNullOrWhiteSpace($line) -or$line.StartsWith("#")) { continue } if ([string]::IsNullOrWhiteSpace($line) -or$line.StartsWith("#")) { continue; };
if ($line -match '^([a-fA-F0-9]+)\s+[* ]?(.*)$') {$expectedHash = $Matches[1].ToLower()$targetFile = $Matches[2].Trim().TrimStart('*')$targetFullPath = Join-Path -Path ($baseDir) -ChildPath ($targetFile) if ($line -match '^([a-fA-F0-9]+)\s+[* ]?(.*)$') {$expectedHash = $Matches[1].ToLower();$targetFile = $Matches[2].Trim().TrimStart('*');$targetFullPath = Join-Path -Path ($baseDir) -ChildPath ($targetFile);
if (Test-Path -Path ($targetFullPath) -PathType 'Leaf') {$actualHash = (Get-FileHash -Path ($targetFullPath) -Algorithm ($fileAlgo)).Hash.ToLower() if (Test-Path -Path ($targetFullPath) -PathType 'Leaf') {$actualHash = (Get-FileHash -Path ($targetFullPath) -Algorithm ($fileAlgo)).Hash.ToLower();
$displayPath = Resolve-Path -Path ($targetFullPath) -Relative -ErrorAction 'SilentlyContinue' $displayPath = Resolve-Path -Path ($targetFullPath) -Relative -ErrorAction 'SilentlyContinue';
if (-not $displayPath) { $displayPath =$targetFile } if (-not $displayPath) { $displayPath =$targetFile; };
if ($displayPath.StartsWith(".\")) { $displayPath =$displayPath.Substring(2) } if ($displayPath.StartsWith(".\")) { $displayPath =$displayPath.Substring(2); };
if ($actualHash -eq$expectedHash) { if ($actualHash -eq$expectedHash) {
Write-Host "${displayPath}: " -NoNewline Write-Host "${displayPath}: " -NoNewline;
Write-Host "OK" -ForegroundColor Green Write-Host "OK" -ForegroundColor Green;
} else { } else {
Write-Host "${displayPath}: " -NoNewline Write-Host "${displayPath}: " -NoNewline;
Write-Host "FAILED" -ForegroundColor Red Write-Host "FAILED" -ForegroundColor Red;
} };
} else { } else {
$displayPath =$targetFile $displayPath =$targetFile;
if ($baseDir -ne (Get-Location).Path) {$displayPath = Join-Path -Path (Resolve-Path -Path ($baseDir) -Relative -ErrorAction 'SilentlyContinue') -ChildPath ($targetFile) if ($baseDir -ne (Get-Location).Path) {$displayPath = Join-Path -Path (Resolve-Path -Path ($baseDir) -Relative -ErrorAction 'SilentlyContinue') -ChildPath ($targetFile);
if ($displayPath.StartsWith(".\")) { $displayPath =$displayPath.Substring(2) } if ($displayPath.StartsWith(".\")) { $displayPath =$displayPath.Substring(2); };
} };
Write-Host "${displayPath}: " -NoNewline Write-Host "${displayPath}: " -NoNewline;
Write-Host "FAILED open or read" -ForegroundColor Red Write-Host "FAILED open or read" -ForegroundColor Red;
} };
} };
} };
} };
} }
# --- GENERATION MODE --- # --- GENERATION MODE ---
else { else {
if ([string]::IsNullOrWhiteSpace($Algorithm)) {$Algorithm = 'SHA256' if ([string]::IsNullOrWhiteSpace($Algorithm)) {$Algorithm = 'SHA256';
} };
# Enable output if the switch is used OR an explicit file is provided
$wantOutFile = $SaveToFile.IsPresent -or (-not [string]::IsNullOrWhiteSpace($OutFile)); $wantOutFile = $SaveToFile.IsPresent -or (-not [string]::IsNullOrWhiteSpace($OutFile));
if ($wantOutFile) { if ($wantOutFile) {
if ($Recursive -and$isWildcardGlobal) { if ($Recursive -and$isWildcardGlobal) {
$groupedItems =$allItems | Group-Object DirectoryName $groupedItems =$allItems | Group-Object DirectoryName;
foreach ($group in ($groupedItems)) { foreach ($group in ($groupedItems)) {
$dirPath =$group.Name $dirPath =$group.Name;
$filesInDir =$group.Group $filesInDir =$group.Group;
if ([string]::IsNullOrWhiteSpace($OutFile)) { if ([string]::IsNullOrWhiteSpace($OutFile)) {
$currentOutFile = Join-Path -Path ($dirPath) -ChildPath "checksums.$($Algorithm.ToLower())" $currentOutFile = Join-Path -Path ($dirPath) -ChildPath "checksums.$($Algorithm.ToLower())";
} else { } else {
$currentOutFile = Join-Path -Path ($dirPath) -ChildPath ($OutFile) $currentOutFile = Join-Path -Path ($dirPath) -ChildPath ($OutFile);
} };
$null = New-Item -Path ($currentOutFile) -ItemType 'File' -Force $null = New-Item -Path ($currentOutFile) -ItemType 'File' -Force;
foreach ($item in ($filesInDir)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower() foreach ($item in ($filesInDir)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower();
$relativePath =$item.Name $relativePath =$item.Name;
$outputLine = "$hash$relativePath" $outputLine = "$hash$relativePath";
Add-Content -Path ($currentOutFile) -Value ($outputLine) Add-Content -Path ($currentOutFile) -Value ($outputLine);
Write-Output $outputLine Write-Output $outputLine;
} };
} };
} }
else { else {
$singleOutFile =$null; $singleOutFile =$null;
if ([string]::IsNullOrWhiteSpace($OutFile)) { if ([string]::IsNullOrWhiteSpace($OutFile)) {
if ($isWildcardGlobal) {$singleOutFile = "checksums.$($Algorithm.ToLower())" if ($isWildcardGlobal) {$singleOutFile = "checksums.$($Algorithm.ToLower())";
} }
} else { } else {
$singleOutFile =$OutFile $singleOutFile =$OutFile;
} };
if ($singleOutFile) { if ($singleOutFile) {
$null = New-Item -Path ($singleOutFile) -ItemType 'File' -Force $null = New-Item -Path ($singleOutFile) -ItemType 'File' -Force;
} };
foreach ($item in ($allItems)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower() foreach ($item in ($allItems)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower();
$relativePath = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue' $relativePath = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue';
if (-not $relativePath) { $relativePath =$item.Name } if (-not $relativePath) { $relativePath =$item.Name; };
if ($relativePath.StartsWith(".\")) { if ($relativePath.StartsWith(".\")) {
$relativePath =$relativePath.Substring(2) $relativePath =$relativePath.Substring(2);
} };
$outputLine = "$hash$relativePath" $outputLine = "$hash$relativePath";
if ($singleOutFile) { if ($singleOutFile) {
Add-Content -Path ($singleOutFile) -Value ($outputLine) Add-Content -Path ($singleOutFile) -Value ($outputLine);
} else { } else {
$currentOutFile = "$($item.FullName).$($Algorithm.ToLower())" $currentOutFile = "$($item.FullName).$($Algorithm.ToLower())";
$null = New-Item -Path ($currentOutFile) -ItemType 'File' -Force $null = New-Item -Path ($currentOutFile) -ItemType 'File' -Force;
Add-Content -Path ($currentOutFile) -Value ($outputLine) Add-Content -Path ($currentOutFile) -Value ($outputLine);
} };
Write-Output $outputLine Write-Output $outputLine;
} };
} };
} }
else { else {
foreach ($item in ($allItems)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower() foreach ($item in ($allItems)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower();
$relativePath = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue' $relativePath = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue';
if (-not $relativePath) { $relativePath =$item.Name } if (-not $relativePath) { $relativePath =$item.Name; };
if ($relativePath.StartsWith(".\")) { if ($relativePath.StartsWith(".\")) {
$relativePath =$relativePath.Substring(2) $relativePath =$relativePath.Substring(2);
} };
$outputLine = "$hash$relativePath" $outputLine = "$hash$relativePath";
Write-Output $outputLine Write-Output $outputLine;
} };
} };
} }
} }