parent::__destruct();
}
+ /**
+ * Checks whether the abstracted file only contains gaps by counting all
+ * gaps' bytes together and compare it to total length.
+ *
+ * @return $isGapsOnly Whether the abstracted file only contains gaps
+ */
+ private function isFileOnlyGaps () {
+ // First/last gap found?
+ /* Only for debugging
+ if (isset($this->gaps[0])) {
+ // Output first and last gap
+ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] this->gaps[0]=%s,this->gaps[%s]=%s', __METHOD__, __LINE__, print_r($this->gaps[0], TRUE), (count($this->gaps) - 1), print_r($this->gaps[count($this->gaps) - 1], TRUE)));
+ } // END - if
+ */
+
+ // Now count every gap
+ $gapsSize = 0;
+ foreach ($this->gaps as $gap) {
+ // Calculate size of found gap: end-start including both
+ $gapsSize += ($gap[self::GAPS_INDEX_END] - $gap[self::GAPS_INDEX_START]);
+ } // END - if
+
+ // Debug output
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] gapsSize=%s,this->headerSize=%s', __METHOD__, __LINE__, $gapsSize, $this->getHeaderSize()));
+
+ // Total gap size + header size must be same as file size
+ $isGapsOnly = (($this->getHeaderSize() + $gapsSize) == $this->getFileSize());
+
+ // Return status
+ return $isGapsOnly;
+ }
+
/**
* Initializes counter for valid entries, arrays for damaged entries and
* an array for gap seek positions. If you call this method on your own,
$this->damagedEntries = array();
}
+ /**
+ * "Getter" for abstracted file size
+ *
+ * @return $fileSize Size of abstracted file
+ */
+ public function getFileSize () {
+ // Call block instanze
+ return $this->getBlockInstance()->getFileSize();
+ }
+
/**
* Getter for total entries
*
// Get current entry
$current = $this->getCurrentBlock();
+ /*
+ * If the block is empty, maybe the whole file is? This could mean
+ * that the file has been pre-allocated.
+ */
+ if (empty($current)) {
+ // Then skip this part
+ continue;
+ } // END - if
+
// Debug message
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] current()=%s', __METHOD__, __LINE__, strlen($current)));
} // END - while
+ // If the last read block is empty, check gaps
+ if (empty($current)) {
+ // Output message
+ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Found a total of %s gaps.', __METHOD__, __LINE__, count($this->gaps)));
+
+ // Check gaps, if the whole file is empty.
+ if ($this->isFileOnlyGaps()) {
+ // Only gaps, so don't continue here.
+ return;
+ } // END - if
+
+ /*
+ * The above call has calculated a total size of all gaps. If the
+ * percentage of gaps passes a "soft" limit and last
+ * defragmentation is to far in the past, or if a "hard" limit has
+ * reached, run defragmentation.
+ */
+ if ($this->isDefragmentationNeeded()) {
+ // Run "defragmentation"
+ $this->doRunDefragmentation();
+ } // END - if
+ } // END - if
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
}
return;
} // END - if
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] key()=%s', __FUNCTION__, __LINE__, $this->key()));
+
// Make sure the block instance is set
assert($this->getBlockInstance() instanceof CalculatableBlock);
// First calculate minimum block length
$length = $this->getBlockInstance()->calculateMinimumBlockLength();
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] length=%s', __FUNCTION__, __LINE__, $length));
// Short be more than zero!
assert($length > 0);
// Then read the next possible block
$block = $this->read($length);
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] block()=%s,length=%s', __FUNCTION__, __LINE__, strlen($block), $length));
+
// Is it all empty?
if (strlen(trim($block)) == 0) {
// Mark this block as empty
- $this->markCurrentBlockAsEmpty($length);
+ $this->markCurrentBlockAsEmpty(strlen($block));
// Skip to next block
continue;
// A debug message
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] data()=%s', __FUNCTION__, __LINE__, strlen($data)));
- } // END - if
+ } // END - while
// EOF reached?
if ($this->isEndOfFileReached()) {