Capture-MclCache: read files MCL still has open, skip any it cannot

This commit is contained in:
cproudlock
2026-10-18 09:21:28 -04:00
parent 3b3041173b
commit 902ce5780a
2 changed files with 100 additions and 4 deletions

View File

@@ -74,13 +74,31 @@ $rows | Sort-Object Path | Export-Csv -LiteralPath (Join-Path $dest "tree.csv")
$copied = 0
$redacted = 0
$skippedBig = New-Object System.Collections.Generic.List[string]
$unreadable = New-Object System.Collections.Generic.List[string]
foreach ($f in $all) {
if ($textExt -notcontains $f.Extension.ToLower()) { continue }
$rel = $f.FullName.Substring($Root.Length + 1)
if ($f.Length -gt $maxCopy) { $skippedBig.Add("$rel ($($f.Length) bytes)"); continue }
$target = Join-Path $filesDir $rel
New-Item -ItemType Directory -Path (Split-Path $target -Parent) -Force | Out-Null
$bytes = [System.IO.File]::ReadAllBytes($f.FullName)
# Share mode lets this read the log MCL still has open.
try {
$fs = New-Object System.IO.FileStream($f.FullName, [System.IO.FileMode]::Open,
[System.IO.FileAccess]::Read, ([System.IO.FileShare]::ReadWrite -bor [System.IO.FileShare]::Delete))
try {
$bytes = New-Object byte[] $fs.Length
$read = 0
while ($read -lt $bytes.Length) {
$n = $fs.Read($bytes, $read, $bytes.Length - $read)
if ($n -le 0) { break }
$read += $n
}
if ($read -lt $bytes.Length) { $bytes = $bytes[0..($read - 1)] }
} finally { $fs.Dispose() }
} catch {
$unreadable.Add("$rel ($($_.Exception.InnerException.Message))")
continue
}
$text = [System.Text.Encoding]::UTF8.GetString($bytes)
if ($text -match $sasPattern) {
$text = [regex]::Replace($text, $sasPattern, '${1}REDACTED')
@@ -116,6 +134,11 @@ foreach ($g in ($rows | Group-Object { ($_.Path -split '\\')[0..1] -join '\' } |
$gb = [math]::Round((($g.Group | Measure-Object Length -Sum).Sum) / 1GB, 2)
$sum.Add(" $($g.Name): $($g.Count) files, $gb GB")
}
if ($unreadable.Count) {
$sum.Add("")
$sum.Add("Text files that could not be read, not copied:")
foreach ($s in $unreadable) { $sum.Add(" $s") }
}
if ($skippedBig.Count) {
$sum.Add("")
$sum.Add("Text files over 5 MB, not copied:")
@@ -131,4 +154,7 @@ Write-Host "Captured $($all.Count) files ($copied text files copied)." -Foregrou
if ($tags.Count -eq 0) {
Write-Host "WARNING: no .tag file. MCL did not finish this run." -ForegroundColor Yellow
}
if ($unreadable.Count) {
Write-Host "$($unreadable.Count) file(s) could not be read; listed in summary.txt." -ForegroundColor Yellow
}
Write-Host "Send back: $zip"