private function cleanGarbage()
{
$sectors = [];
foreach ($this->locationTable as $index => $data) {
//Calculate file usage
if ($data[0] === 0 or $data[1] === 0) {
$this->locationTable[$index] = [0, 0, 0];
continue;
}
for ($i = 0; $i < $data[1]; ++$i) {
$sectors[$data[0]] = $index;
}
}
if (count($sectors) === $this->lastSector - 2) {
//No collection needed
return 0;
}
ksort($sectors);
$shift = 0;
$lastSector = 1;
//First chunk - 1
fseek($this->filePointer, 8192);
$sector = 2;
foreach ($sectors as $sector => $index) {
if ($sector - $lastSector > 1) {
$shift += $sector - $lastSector - 1;
}
if ($shift > 0) {
fseek($this->filePointer, $sector << 12);
$old = fread($this->filePointer, 4096);
fseek($this->filePointer, $sector - $shift << 12);
fwrite($this->filePointer, $old, 4096);
}
$this->locationTable[$index][0] -= $shift;
$lastSector = $sector;
}
ftruncate($this->filePointer, $sector + 1 << 12);
//Truncate to the end of file written
return $shift;
}