waypoit debuguing

This commit is contained in:
2026-09-25 16:52:30 -06:00
parent 251c9aa06c
commit 56b08f32da
+80 -93
View File
@@ -60,14 +60,13 @@ param(
) )
# --- ENVIRONMENT SANITY CHECK --- # --- ENVIRONMENT SANITY CHECK ---
# Ensure PowerShell 7+ is used since ForEach-Object -Parallel is required.
if ($PSVersionTable.PSVersion.Major -lt 7) { if ($PSVersionTable.PSVersion.Major -lt 7) {
Write-Error "checksum: This script requires PowerShell 7 or higher for parallel execution (-Parallel support). Current version is $($PSVersionTable.PSVersion)." Write-Error "checksum: This script requires PowerShell 7 or higher for parallel execution (-Parallel support). Current version is $($PSVersionTable.PSVersion)."
exit 1 exit 1
} }
begin { # --- HELPER FUNCTIONS ---
function Show-Help { function Show-Help {
@' @'
Princess Pi's Magical CHECKSUM! Princess Pi's Magical CHECKSUM!
@@ -143,16 +142,16 @@ Princess Pi LOVES YOU!~
https://linktr.ee/princesspi3 https://linktr.ee/princesspi3
'@ '@
}; }
function Write-ErrorWithHelp { function Write-ErrorWithHelp {
param([string]$ErrorMessage); param([string]$ErrorMessage);
Show-Help; Show-Help;
Write-Host ""; Write-Host "";
Write-Error $ErrorMessage; Write-Error $ErrorMessage;
}; }
function Show-PrincessPiArt { function Show-PrincessPiArt {
$art = @' $art = @'
=#= -- ##*+++++ *+ =#= -- ##*+++++ *+
@@ -213,9 +212,9 @@ https://linktr.ee/princesspi3
'@ '@
Write-Host $art -ForegroundColor Blue; Write-Host $art -ForegroundColor Blue;
}; }
function Get-AlgorithmFromHashLength { function Get-AlgorithmFromHashLength {
param([string]$HashString); param([string]$HashString);
switch ($HashString.Trim().Length) { switch ($HashString.Trim().Length) {
@@ -228,50 +227,48 @@ https://linktr.ee/princesspi3
Write-ErrorWithHelp "checksum: Could not auto-detect algorithm for hash length $($HashString.Length). Please specify -Algorithm."; Write-ErrorWithHelp "checksum: Could not auto-detect algorithm for hash length $($HashString.Length). Please specify -Algorithm.";
return $null; return $null;
} }
}; }
};
} }
process { # --- MAIN EXECUTION FLOW ---
$helpInPath = $false; $helpInPath = $false;
if ($FilePath) { if ($FilePath) {
foreach ($p in ($FilePath)) { foreach ($p in ($FilePath)) {
if ($p -match '^(?i)[-/]{1,2}(h|help|\?)$') { if ($p -match '^(?i)[-/]{1,2}(h|help|\?)$') {
$helpInPath = $true; $helpInPath = $true;
break; break;
} }
}; }
}; }
if ($Help.IsPresent -or $helpInPath) { if ($Help.IsPresent -or $helpInPath) {
Show-Help; Show-Help;
return; return;
}; }
if ($PrincessPi.IsPresent) { if ($PrincessPi.IsPresent) {
Show-PrincessPiArt; Show-PrincessPiArt;
return; return;
}; }
if (-not [string]::IsNullOrWhiteSpace($Algorithm)) { if (-not [string]::IsNullOrWhiteSpace($Algorithm)) {
$Algorithm = $Algorithm.ToUpper(); $Algorithm = $Algorithm.ToUpper();
}; }
# Determine if progress bar should display (-r, -c/-Check, or wildcard input '*') $isWildcardGlobal = $false;
$isWildcardGlobal = $false; if ($FilePath) {
if ($FilePath) {
foreach ($p in $FilePath) { foreach ($p in $FilePath) {
if ($p -match '\*') { $isWildcardGlobal = $true; } if ($p -match '\*') { $isWildcardGlobal = $true; }
} }
} }
$showProgress = $Recursive.IsPresent -or $isWildcardGlobal -or ($PSCmdlet.ParameterSetName -eq 'Verify'); $showProgress = $Recursive.IsPresent -or $isWildcardGlobal -or ($PSCmdlet.ParameterSetName -eq 'Verify');
# --- 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-ErrorWithHelp "checksum: Missing file path parameter. Use -Help for usage."; Write-ErrorWithHelp "checksum: Missing file path parameter. Use -Help for usage.";
return; return;
}; }
$allItems = @(); $allItems = @();
$skipExtensions = @('.md5', '.sha1', '.sha256', '.sha384', '.sha512'); $skipExtensions = @('.md5', '.sha1', '.sha256', '.sha384', '.sha512');
@@ -288,43 +285,42 @@ process {
} else { } else {
$resolvedItems = Get-Item -Path ($pathEntry) -ErrorAction 'SilentlyContinue'; $resolvedItems = Get-Item -Path ($pathEntry) -ErrorAction 'SilentlyContinue';
} }
}; }
if (-not $resolvedItems) { if (-not $resolvedItems) {
Write-ErrorWithHelp "checksum: $($pathEntry): No such file or directory"; Write-ErrorWithHelp "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();
$totalFiles = $allItems.Count; $totalFiles = $allItems.Count;
$currentIndex = 0; $currentIndex = 0;
# Stream parallel results and update progress bar in real time
$allItems | ForEach-Object -Parallel { $allItems | ForEach-Object -Parallel {
$item = $_ $item = $_
$algo = $using:Algorithm $algo = $using:Algorithm
@@ -332,8 +328,8 @@ process {
$actualHash = (Get-FileHash -Path ($item.FullName) -Algorithm ($algo)).Hash.ToLower(); $actualHash = (Get-FileHash -Path ($item.FullName) -Algorithm ($algo)).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(".\") -or $targetFile.StartsWith("./")) { $targetFile = $targetFile.Substring(2); }; if ($targetFile.StartsWith(".\") -or $targetFile.StartsWith("./")) { $targetFile = $targetFile.Substring(2); }
[PSCustomObject]@{ [PSCustomObject]@{
TargetFile = $targetFile TargetFile = $targetFile
@@ -352,14 +348,14 @@ process {
Write-Host "$($_.TargetFile): " -NoNewline; Write-Host "$($_.TargetFile): " -NoNewline;
Write-Host "FAILED" -ForegroundColor Red; Write-Host "FAILED" -ForegroundColor Red;
} }
}; }
if ($showProgress) { if ($showProgress) {
Write-Progress -Activity "Matching Checksums ($Algorithm)" -Completed; Write-Progress -Activity "Matching Checksums ($Algorithm)" -Completed;
} }
} }
# --- 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;
@@ -368,12 +364,12 @@ process {
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-ErrorWithHelp "checksum: $($Check): No such file or directory"; Write-ErrorWithHelp "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);
@@ -382,7 +378,7 @@ process {
$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]);
@@ -392,14 +388,13 @@ process {
$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'; }
}; }
# Parse verification items
$verifyItems = @(); $verifyItems = @();
foreach ($line in ($lines)) { foreach ($line in ($lines)) {
if ([string]::IsNullOrWhiteSpace($line) -or $line.StartsWith("#")) { continue; }; if ([string]::IsNullOrWhiteSpace($line) -or $line.StartsWith("#")) { continue; }
$expectedHash = $null; $expectedHash = $null;
$targetFile = $null; $targetFile = $null;
@@ -411,7 +406,7 @@ process {
elseif ($line -match '^([a-fA-F0-9]{128}|[a-fA-F0-9]{96}|[a-fA-F0-9]{64}|[a-fA-F0-9]{40}|[a-fA-F0-9]{32})[* ]?(.*)$') { elseif ($line -match '^([a-fA-F0-9]{128}|[a-fA-F0-9]{96}|[a-fA-F0-9]{64}|[a-fA-F0-9]{40}|[a-fA-F0-9]{32})[* ]?(.*)$') {
$expectedHash = $Matches[1].ToLower(); $expectedHash = $Matches[1].ToLower();
$targetFile = $Matches[2].Trim().TrimStart('*'); $targetFile = $Matches[2].Trim().TrimStart('*');
}; }
if ($expectedHash -and $targetFile) { if ($expectedHash -and $targetFile) {
$targetFullPath = Join-Path -Path ($baseDir) -ChildPath ($targetFile); $targetFullPath = Join-Path -Path ($baseDir) -ChildPath ($targetFile);
@@ -427,7 +422,6 @@ process {
$totalFiles = $verifyItems.Count; $totalFiles = $verifyItems.Count;
$currentIndex = 0; $currentIndex = 0;
# Run verification items in parallel and stream progress live
$verifyItems | ForEach-Object -Parallel { $verifyItems | ForEach-Object -Parallel {
$item = $_ $item = $_
$algo = $using:fileAlgo $algo = $using:fileAlgo
@@ -436,8 +430,8 @@ process {
$actualHash = (Get-FileHash -Path ($item.TargetFullPath) -Algorithm ($algo)).Hash.ToLower(); $actualHash = (Get-FileHash -Path ($item.TargetFullPath) -Algorithm ($algo)).Hash.ToLower();
$displayPath = Resolve-Path -Path ($item.TargetFullPath) -Relative -ErrorAction 'SilentlyContinue'; $displayPath = Resolve-Path -Path ($item.TargetFullPath) -Relative -ErrorAction 'SilentlyContinue';
if (-not $displayPath) { $displayPath = $item.TargetFile; }; if (-not $displayPath) { $displayPath = $item.TargetFile; }
if ($displayPath.StartsWith(".\") -or $displayPath.StartsWith("./")) { $displayPath = $displayPath.Substring(2); }; if ($displayPath.StartsWith(".\") -or $displayPath.StartsWith("./")) { $displayPath = $displayPath.Substring(2); }
[PSCustomObject]@{ [PSCustomObject]@{
DisplayPath = $displayPath DisplayPath = $displayPath
@@ -448,8 +442,8 @@ process {
$displayPath = $item.TargetFile; $displayPath = $item.TargetFile;
if ($item.BaseDir -ne (Get-Location).Path) { if ($item.BaseDir -ne (Get-Location).Path) {
$displayPath = Join-Path -Path (Resolve-Path -Path ($item.BaseDir) -Relative -ErrorAction 'SilentlyContinue') -ChildPath ($item.TargetFile); $displayPath = Join-Path -Path (Resolve-Path -Path ($item.BaseDir) -Relative -ErrorAction 'SilentlyContinue') -ChildPath ($item.TargetFile);
if ($displayPath.StartsWith(".\") -or $displayPath.StartsWith("./")) { $displayPath = $displayPath.Substring(2); }; if ($displayPath.StartsWith(".\") -or $displayPath.StartsWith("./")) { $displayPath = $displayPath.Substring(2); }
}; }
[PSCustomObject]@{ [PSCustomObject]@{
DisplayPath = $displayPath DisplayPath = $displayPath
Status = "FAILED open or read" Status = "FAILED open or read"
@@ -470,21 +464,20 @@ process {
} else { } else {
Write-Host "FAILED" -ForegroundColor Red; Write-Host "FAILED" -ForegroundColor Red;
} }
};
Write-Progress -Activity "Verifying Checksums ($fileAlgo)" -Completed;
};
} }
# --- GENERATION MODE --- Write-Progress -Activity "Verifying Checksums ($fileAlgo)" -Completed;
else { }
}
# --- GENERATION MODE ---
else {
if ([string]::IsNullOrWhiteSpace($Algorithm)) { if ([string]::IsNullOrWhiteSpace($Algorithm)) {
$Algorithm = 'SHA256'; $Algorithm = 'SHA256';
}; }
$wantCustomOut = $PSBoundParameters.ContainsKey('OutFile'); $wantCustomOut = $PSBoundParameters.ContainsKey('OutFile');
$wantAutoOut = $Save.IsPresent; $wantAutoOut = $Save.IsPresent;
if ($wantCustomOut -or $wantAutoOut) { if ($wantCustomOut -or $wantAutoOut) {
# Recursive wildcard auto-saves per directory
if ($Recursive -and $isWildcardGlobal) { if ($Recursive -and $isWildcardGlobal) {
$groupedItems = $allItems | Group-Object DirectoryName; $groupedItems = $allItems | Group-Object DirectoryName;
@@ -496,7 +489,7 @@ process {
$currentOutFile = Join-Path -Path ($dirPath) -ChildPath ($OutFile); $currentOutFile = Join-Path -Path ($dirPath) -ChildPath ($OutFile);
} else { } else {
$currentOutFile = Join-Path -Path ($dirPath) -ChildPath "checksums.$($Algorithm.ToLower())"; $currentOutFile = Join-Path -Path ($dirPath) -ChildPath "checksums.$($Algorithm.ToLower())";
}; }
$totalFiles = $filesInDir.Count; $totalFiles = $filesInDir.Count;
$currentIndex = 0; $currentIndex = 0;
@@ -509,10 +502,10 @@ process {
$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($algo)).Hash.ToLower(); $hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($algo)).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(".\") -or $relativePath.StartsWith("./")) { if ($relativePath.StartsWith(".\") -or $relativePath.StartsWith("./")) {
$relativePath = $relativePath.Substring(2); $relativePath = $relativePath.Substring(2);
}; }
[PSCustomObject]@{ [PSCustomObject]@{
Item = $item Item = $item
@@ -529,23 +522,22 @@ process {
$linesToWrite += $_.OutputLine; $linesToWrite += $_.OutputLine;
Write-Output $_.OutputLine; Write-Output $_.OutputLine;
}; }
if ($showProgress) { if ($showProgress) {
Write-Progress -Activity "Computing Checksums ($Algorithm)" -Completed; Write-Progress -Activity "Computing Checksums ($Algorithm)" -Completed;
} }
Set-Content -Path ($currentOutFile) -Value $linesToWrite -Encoding utf8; Set-Content -Path ($currentOutFile) -Value $linesToWrite -Encoding utf8;
}; }
} }
else { else {
# Custom file OR Auto file with a wildcard
if ($wantCustomOut -or ($wantAutoOut -and $isWildcardGlobal)) { if ($wantCustomOut -or ($wantAutoOut -and $isWildcardGlobal)) {
if ($wantCustomOut) { if ($wantCustomOut) {
$singleOutFile = $OutFile; $singleOutFile = $OutFile;
} else { } else {
$singleOutFile = "checksums.$($Algorithm.ToLower())"; $singleOutFile = "checksums.$($Algorithm.ToLower())";
}; }
$totalFiles = $allItems.Count; $totalFiles = $allItems.Count;
$currentIndex = 0; $currentIndex = 0;
@@ -558,10 +550,10 @@ process {
$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($algo)).Hash.ToLower(); $hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($algo)).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(".\") -or $relativePath.StartsWith("./")) { if ($relativePath.StartsWith(".\") -or $relativePath.StartsWith("./")) {
$relativePath = $relativePath.Substring(2); $relativePath = $relativePath.Substring(2);
}; }
[PSCustomObject]@{ [PSCustomObject]@{
Item = $item Item = $item
@@ -578,7 +570,7 @@ process {
$linesToWrite += $_.OutputLine; $linesToWrite += $_.OutputLine;
Write-Output $_.OutputLine; Write-Output $_.OutputLine;
}; }
if ($showProgress) { if ($showProgress) {
Write-Progress -Activity "Computing Checksums ($Algorithm)" -Completed; Write-Progress -Activity "Computing Checksums ($Algorithm)" -Completed;
@@ -586,7 +578,6 @@ process {
Set-Content -Path ($singleOutFile) -Value $linesToWrite -Encoding utf8; Set-Content -Path ($singleOutFile) -Value $linesToWrite -Encoding utf8;
} }
# Auto file for explicit individual files (No wildcard)
else { else {
$totalFiles = $allItems.Count; $totalFiles = $allItems.Count;
$currentIndex = 0; $currentIndex = 0;
@@ -598,10 +589,10 @@ process {
$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($algo)).Hash.ToLower(); $hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($algo)).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(".\") -or $relativePath.StartsWith("./")) { if ($relativePath.StartsWith(".\") -or $relativePath.StartsWith("./")) {
$relativePath = $relativePath.Substring(2); $relativePath = $relativePath.Substring(2);
}; }
[PSCustomObject]@{ [PSCustomObject]@{
Item = $item Item = $item
@@ -618,15 +609,14 @@ process {
Write-Output $_.OutputLine; Write-Output $_.OutputLine;
Set-Content -Path ($_.ItemOutFile) -Value $_.OutputLine -Encoding utf8; Set-Content -Path ($_.ItemOutFile) -Value $_.OutputLine -Encoding utf8;
}; }
if ($showProgress) { if ($showProgress) {
Write-Progress -Activity "Computing Checksums ($Algorithm)" -Completed; Write-Progress -Activity "Computing Checksums ($Algorithm)" -Completed;
} }
};
};
} }
# Standard console output (No save flags passed) }
}
else { else {
$totalFiles = $allItems.Count; $totalFiles = $allItems.Count;
$currentIndex = 0; $currentIndex = 0;
@@ -638,10 +628,10 @@ process {
$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($algo)).Hash.ToLower(); $hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($algo)).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(".\") -or $relativePath.StartsWith("./")) { if ($relativePath.StartsWith(".\") -or $relativePath.StartsWith("./")) {
$relativePath = $relativePath.Substring(2); $relativePath = $relativePath.Substring(2);
}; }
[PSCustomObject]@{ [PSCustomObject]@{
Item = $item Item = $item
@@ -657,13 +647,10 @@ process {
} }
Write-Output $_.OutputLine; Write-Output $_.OutputLine;
}; }
if ($showProgress) { if ($showProgress) {
Write-Progress -Activity "Computing Checksums ($Algorithm)" -Completed; Write-Progress -Activity "Computing Checksums ($Algorithm)" -Completed;
} }
};
} }
} }
end {}