WORKAN DEBUG

This commit is contained in:
2026-09-25 16:10:17 -06:00
parent 4fea9dbccc
commit 608e225833
+70 -61
View File
@@ -127,39 +127,39 @@ EXAMPLES
} }
process { process {
$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\vert{}\?)$') { 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 (-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;
$skipExtensions = @('.md5', '.sha1', '.sha256', '.sha384', '.sha512'); $skipExtensions = @('.md5', '.sha1', '.sha256', '.sha384', '.sha512');
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) {
@@ -173,7 +173,7 @@ process {
}; };
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;
}; };
@@ -232,18 +232,18 @@ process {
}; };
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]);
@@ -254,33 +254,35 @@ process {
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; };
$expectedHash =$null; $expectedHash = $null;
$targetFile =$null; $targetFile = $null;
if ($line -match '^([a-fA-F0-9]+)\s+[* ]?(.*)$') { if ($line -match '^([a-fA-F0-9]+)\s+[* ]?(.*)$') {
$expectedHash =$Matches[1].ToLower(); $expectedHash = $Matches[1].ToLower();
$targetFile =$Matches[2].Trim().TrimStart('*'); $targetFile = $Matches[2].Trim().TrimStart('*');
} }
elseif ($line -match '^([a-fA-F0-9]{128}\vert{}[a-fA-F0-9]{96}\vert{}[a-fA-F0-9]{64}\vert{}[a-fA-F0-9]{40}\vert{}[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) {$targetFullPath = Join-Path -Path ($baseDir) -ChildPath ($targetFile); if ($expectedHash -and $targetFile) {
$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(".\") -or $displayPath.StartsWith("./")) { $displayPath =$displayPath.Substring(2); }; if ($displayPath.StartsWith(".\") -or $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 {
@@ -288,9 +290,10 @@ process {
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) {
if ($displayPath.StartsWith(".\") -or $displayPath.StartsWith("./")) { $displayPath =$displayPath.Substring(2); }; $displayPath = Join-Path -Path (Resolve-Path -Path ($baseDir) -Relative -ErrorAction 'SilentlyContinue') -ChildPath ($targetFile);
if ($displayPath.StartsWith(".\") -or $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;
@@ -301,94 +304,100 @@ process {
} }
# --- GENERATION MODE --- # --- GENERATION MODE ---
else { else {
if ([string]::IsNullOrWhiteSpace($Algorithm)) {$Algorithm = 'SHA256'; if ([string]::IsNullOrWhiteSpace($Algorithm)) {
$Algorithm = 'SHA256';
}; };
$wantCustomOut =$PSBoundParameters.ContainsKey('OutFile'); $wantCustomOut = $PSBoundParameters.ContainsKey('OutFile');
$wantAutoOut =$AutoOut.IsPresent; $wantAutoOut = $AutoOut.IsPresent;
if ($wantCustomOut -or$wantAutoOut) { if ($wantCustomOut -or $wantAutoOut) {
# Recursive wildcard auto-saves per directory # Recursive wildcard auto-saves per directory
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 ($wantCustomOut) {$currentOutFile = Join-Path -Path ($dirPath) -ChildPath ($OutFile); if ($wantCustomOut) {
$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())";
}; };
$linesToWrite = @(); $linesToWrite = @();
foreach ($item in ($filesInDir)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower(); foreach ($item in ($filesInDir)) {
$relativePath =$item.Name; $hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower();
$relativePath = $item.Name;
$outputLine = "${hash}${relativePath}"; $outputLine = "${hash} ${relativePath}";
$linesToWrite +=$outputLine; $linesToWrite += $outputLine;
Write-Output $outputLine; Write-Output $outputLine;
}; };
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 # 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())";
}; };
$linesToWrite = @(); $linesToWrite = @();
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(".\") -or $relativePath.StartsWith("./")) { if ($relativePath.StartsWith(".\") -or $relativePath.StartsWith("./")) {
$relativePath =$relativePath.Substring(2); $relativePath = $relativePath.Substring(2);
}; };
$outputLine = "${hash}${relativePath}"; $outputLine = "${hash} ${relativePath}";
$linesToWrite +=$outputLine; $linesToWrite += $outputLine;
Write-Output $outputLine; Write-Output $outputLine;
}; };
Set-Content -Path ($singleOutFile) -Value$linesToWrite -Encoding utf8; Set-Content -Path ($singleOutFile) -Value $linesToWrite -Encoding utf8;
} }
# Auto file for explicit individual files (No wildcard) # Auto file for explicit individual files (No wildcard)
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(".\") -or $relativePath.StartsWith("./")) { if ($relativePath.StartsWith(".\") -or $relativePath.StartsWith("./")) {
$relativePath =$relativePath.Substring(2); $relativePath = $relativePath.Substring(2);
}; };
$outputLine = "${hash}${relativePath}"; $outputLine = "${hash} ${relativePath}";
Write-Output $outputLine; Write-Output $outputLine;
# Save directly next to the original file: <filename>.<algo> # Save directly next to the original file: <filename>.<algo>
$itemOutFile = "$($item.FullName).$($Algorithm.ToLower())"; $itemOutFile = "$($item.FullName).$($Algorithm.ToLower())";
Set-Content -Path ($itemOutFile) -Value$outputLine -Encoding utf8; Set-Content -Path ($itemOutFile) -Value $outputLine -Encoding utf8;
}; };
}; };
}; };
} }
# Standard console output (No save flags passed) # Standard console output (No save flags passed)
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(".\") -or $relativePath.StartsWith("./")) { if ($relativePath.StartsWith(".\") -or $relativePath.StartsWith("./")) {
$relativePath =$relativePath.Substring(2); $relativePath = $relativePath.Substring(2);
}; };
$outputLine = "${hash}${relativePath}"; $outputLine = "${hash} ${relativePath}";
Write-Output $outputLine; Write-Output $outputLine;
}; };
}; };