Skip to content

Instantly share code, notes, and snippets.

@Hashbrown777
Last active June 22, 2023 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hashbrown777/f3790ef4586ff17b68a2785f70a525e7 to your computer and use it in GitHub Desktop.
Save Hashbrown777/f3790ef4586ff17b68a2785f70a525e7 to your computer and use it in GitHub Desktop.
In files identified as containing corrupted blocks punch holes in the valid blocks so they don't take up as much space
$blockSize = 4KB
$buffer=[byte[]]::new($blockSize)
Get-ChildItem -File `
| Sort-Object -Property Length
| %{
"$($_.Name)`t$($_.Length / 1MB -bor 0)MB"
$path = $_.FullName
#useful if your files are patchable eg torrent-sourced
sudo cp --reflink=always $path "${path}_rescued"
chmod u+r $path
$file = [System.IO.File]::OpenRead($path)
chmod u-r $path
$current = $NULL
$read = $NULL
$occupied = 0
$bad = 0
while ($file.CanRead) {
$position = $file.Position
if ($position -and !($position % 256MB)) {
"`t$($position / 1MB)MB"
}
try {
$read = $file.Read($buffer, 0, $blockSize)
$current = $True
if ($read -lt $blockSize) {
$file.Close()
$current = $False
$position += $read
}
}
catch {
$current = $False
++$bad
$file.Seek($blockSize, 1) | Out-Null
sudo fallocate -p -o $position -l $blockSize "${path}_rescued"
}
if ($occupied -lt 0) {
if ($current) {
$occupied = $position
}
}
elseif (!$current) {
if ($current = $position - $occupied) {
"`tPunching $($current / 1KB -bor 0)KB"
sudo fallocate -p -o $occupied -l $current $path
}
$occupied = -1
}
}
"`t$bad bad ${blockSize}B blocks of $($_.Length / $blockSize -bor 0)`n"
}
#!/usr/bin/pwsh
Filter Output { Param($colour)
$num = ($_ -split '(?<=^[a-f0-9]+:\s)')[0]
$num | Write-Host -NoNewline
$_ = $_ -replace "^$num",'' -split ' (?=.{10}$)'
# $_[0] | Write-Host -NoNewline -ForegroundColor $colour
# ' ' | Write-Host -NoNewline
# $_[1].PadRight(10, ' ') | Write-Host -NoNewline -BackgroundColor $colour
(
[char]27,
'[',
(31,32)[$colour -eq 'Green'],
'm',
$_[0],
[char]27,
'[39m',
' ',
[char]27,
'[',
(41,42)[$colour -eq 'Green'],
'm',
$_[1].PadRight(10, ' '),
[char]27,
'[49m'
) | Write-Host -NoNewline
}
#$context = [System.Collections.Queue]::new()
$context = [PSCustomObject]@{
Count = 0
Index = 0
Queue = @($NULL, $NULL, $NULL, $NULL, $NULL, $NULL)
} `
| Add-Member `
-Name Enqueue `
-MemberType ScriptMethod `
-Value { Param($item)
if ($this.Count -eq $this.Queue.Count) {
throw $this.Count
}
$this.Queue[($this.Index + $this.Count) % $this.Queue.Count] = $item
++$this.Count
} `
-PassThru `
| Add-Member `
-Name Dequeue `
-MemberType ScriptMethod `
-Value { Param()
if ($this.Count -eq 0) {
throw $this.Count
}
$this.Queue[$this.Index++]
$this.Index %= $this.Queue.Count
--$this.Count
} `
-PassThru
$elipsed = @($False)
Function Context {
for ($count = 0; $context.Count -and $count -lt 3; ++$count) {
$line = $context.Dequeue()
$line[0] | Output -colour Green
' ' | Write-Host -NoNewline
$line[1] | Output -colour Green
'' | Write-Host
}
if ($elipsed[0] = !!$context.Count) {
# ' ' * 61 + '...' | Write-Host -ForegroundColor Green
[char]27,'[32m',(' ' * 61),'...',[char]27,'[39m' -join ''
}
}
'diff -y <(xxd "$1") <(xxd "$2")' `
| bash -s $args `
| %{
$line = $_ -split '\t' -replace '(?<= .{10}) \|',''
if ($line[0] -eq $line[1]) {
if ($context.Count -eq 6) {
Context
}
$context.Enqueue($line)
if ($elipsed[0]) {
$context.Dequeue() | Out-Null
}
}
else {
Context
$line[0] | Output -colour Red
' ' | Write-Host -NoNewline
$line[1] | Output -colour Red
'' | Write-Host
}
}
Context
@Hashbrown777
Copy link
Author

Once you have fixed your file, check what was missing:
vimdiff <(xxd FILE_rescued) <(xxd DIR/FILE)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment