3 * A general binary file class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class BaseBinaryFile extends BaseFile {
26 * Separator for header data
28 const SEPARATOR_HEADER_DATA = 0x01;
31 * Separator header->entries
33 const SEPARATOR_HEADER_ENTRIES = 0x02;
36 * Separator hash->name
38 const SEPARATOR_HASH_NAME = 0x03;
41 * Separator entry->entry
43 const SEPARATOR_ENTRIES = 0x04;
46 * Separator type->position
48 const SEPARATOR_TYPE_POSITION = 0x05;
53 const LENGTH_COUNT = 20;
58 const LENGTH_POSITION = 20;
63 const LENGTH_NAME = 10;
66 * Maximum length of entry type
68 const LENGTH_TYPE = 20;
70 //***** Array elements for 'gaps' array *****
75 const GAPS_INDEX_START = 'start';
80 const GAPS_INDEX_END = 'end';
83 * Current seek position
85 private $seekPosition = 0;
90 private $headerSize = 0;
95 private $header = array();
98 * Seek positions for gaps ("fragmentation")
100 private $gaps = array();
103 * Seek positions for damaged entries (e.g. mismatching hash sum, ...)
105 private $damagedEntries = array();
110 private $backBuffer = '';
113 * Currently loaded block (will be returned by current())
115 private $currentBlock = '';
118 * Protected constructor
120 * @param $className Name of the class
123 protected function __construct ($className) {
124 // Call parent constructor
125 parent::__construct($className);
127 // Init counters and gaps array
128 $this->initCountersGapsArray();
132 * Checks whether the abstracted file only contains gaps by counting all
133 * gaps' bytes together and compare it to total length.
135 * @return $isGapsOnly Whether the abstracted file only contains gaps
137 private function isFileOnlyGaps () {
138 // First/last gap found?
139 /* Only for debugging
140 if (isset($this->gaps[0])) {
141 // Output first and last gap
142 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)));
146 // Now count every gap
148 foreach ($this->gaps as $gap) {
149 // Calculate size of found gap: end-start including both
150 $gapsSize += ($gap[self::GAPS_INDEX_END] - $gap[self::GAPS_INDEX_START]);
154 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] gapsSize=%s,this->headerSize=%s', __METHOD__, __LINE__, $gapsSize, $this->getHeaderSize()));
156 // Total gap size + header size must be same as file size
157 $isGapsOnly = (($this->getHeaderSize() + $gapsSize) == $this->getFileSize());
164 * Initializes counter for valid entries, arrays for damaged entries and
165 * an array for gap seek positions. If you call this method on your own,
166 * please re-analyze the file structure. So you are better to call
167 * analyzeFile() instead of this method.
171 public function initCountersGapsArray () {
172 // Init counter and seek position
173 $this->setCounter(0);
174 $this->setSeekPosition(0);
177 $this->gaps = array();
178 $this->damagedEntries = array();
182 * Getter for header size
184 * @return $totalEntries Size of file header
186 public final function getHeaderSize () {
188 return $this->headerSize;
192 * Setter for header size
194 * @param $headerSize Size of file header
197 public final function setHeaderSize ($headerSize) {
199 $this->headerSize = $headerSize;
203 * Getter for header array
205 * @return $totalEntries Size of file header
207 public final function getHeader () {
209 return $this->header;
215 * @param $header Array for a file header
218 public final function setHeader (array $header) {
220 $this->header = $header;
224 * Getter for seek position
226 * @return $seekPosition Current seek position (stored here in object)
228 protected final function getSeekPosition () {
230 return $this->seekPosition;
234 * Setter for seek position
236 * @param $seekPosition Current seek position (stored here in object)
239 protected final function setSeekPosition ($seekPosition) {
241 $this->seekPosition = $seekPosition;
245 * Updates seekPosition attribute from file to avoid to much access on file.
249 public function updateSeekPosition () {
250 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
252 // Get key (= seek position)
253 $seekPosition = $this->key();
254 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Setting seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
257 $this->setSeekPosition($seekPosition);
259 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
263 * Seeks to beginning of file, updates seek position in this object and
264 * flushes the header.
268 protected function rewindUpdateSeekPosition () {
269 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
271 // flushFileHeader must be callable
272 assert(is_callable(array($this, 'flushFileHeader')));
274 // Seek to beginning of file
277 // And update seek position ...
278 $this->updateSeekPosition();
280 // ... to write it back into the file
281 $this->flushFileHeader();
283 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__));
287 * Seeks to old position
291 protected function seekToOldPosition () {
292 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
294 // Seek to currently ("old") saved position
295 $this->seek($this->getSeekPosition());
297 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__));
301 * Checks whether the block separator has been found
303 * @param $str String to look in
304 * @return $isFound Whether the block separator has been found
306 public static function isBlockSeparatorFound ($str) {
308 $isFound = (strpos($str, chr(self::SEPARATOR_ENTRIES)) !== FALSE);
315 * Getter for the file pointer
317 * @return $filePointer The file pointer which shall be a valid file resource
318 * @throws UnsupportedOperationException If this method is called
320 public final function getPointer () {
321 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
325 * Initializes the back-buffer by setting it to an empty string.
329 private function initBackBuffer () {
330 // Simply call the setter
331 $this->setBackBuffer('');
335 * Setter for backBuffer field
337 * @param $backBuffer Characters to "store" in back-buffer
340 private function setBackBuffer ($backBuffer) {
341 // Cast to string (so no arrays or objects)
342 $backBuffer = (string) $backBuffer;
345 $this->backBuffer = $backBuffer;
349 * Getter for backBuffer field
351 * @return $backBuffer Characters "stored" in back-buffer
353 private function getBackBuffer () {
354 return $this->backBuffer;
358 * Setter for currentBlock field
360 * @param $currentBlock Characters to set a currently loaded block
363 private function setCurrentBlock ($currentBlock) {
364 // Cast to string (so no arrays or objects)
365 $currentBlock = (string) $currentBlock;
368 $this->currentBlock = $currentBlock;
372 * Gets currently read data
374 * @return $current Currently read data
376 public function getCurrentBlock () {
378 return $this->currentBlock;
382 * Initializes this file class
384 * @param $fileName Name of this abstract file
387 protected function initFile ($fileName) {
388 // Get a file i/o pointer instance
389 $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName));
391 // ... and set it here
392 $this->setPointerInstance($pointerInstance);
396 * Writes data at given position
398 * @param $seekPosition Seek position
399 * @param $data Data to be written
400 * @param $flushHeader Whether to flush the header (default: flush)
403 protected function writeData ($seekPosition, $data, $flushHeader = TRUE) {
404 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data()=%s - CALLED!', __METHOD__, __LINE__, $seekPosition, strlen($data)));
406 // Write data at given position
407 $this->getPointerInstance()->writeAtPosition($seekPosition, $data);
409 // Update seek position
410 $this->updateSeekPosition();
413 if ($flushHeader === TRUE) {
415 $this->flushFileHeader();
417 // Seek to old position
418 $this->seekToOldPosition();
421 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
425 * Marks the currently loaded block as empty (with length of the block)
427 * @param $length Length of the block
430 protected function markCurrentBlockAsEmpty ($length) {
431 // Get current seek position
432 $currentPosition = $this->key();
434 // Now add it as gap entry
435 array_push($this->gaps, array(
436 self::GAPS_INDEX_START => ($currentPosition - $length),
437 self::GAPS_INDEX_END => $currentPosition,
442 * Checks whether the file header is initialized
444 * @return $isInitialized Whether the file header is initialized
446 public function isFileHeaderInitialized () {
447 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
449 // Default is not initialized
450 $isInitialized = FALSE;
452 // Is the file initialized?
453 if ($this->isFileInitialized()) {
454 // Some bytes has been written, so rewind to start of it.
455 $rewindStatus = $this->rewind();
456 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] rewindStatus=%s', __METHOD__, __LINE__, $rewindStatus));
458 // Is the rewind() call successfull?
459 if ($rewindStatus != 1) {
460 // Something bad happened
461 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Could not rewind().', __METHOD__, __LINE__));
465 $this->readFileHeader();
467 // The above method does already check the header
468 $isInitialized = TRUE;
472 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
473 return $isInitialized;
477 * Checks whether the assigned file has been initialized
479 * @return $isInitialized Whether the file's size is zero
481 public function isFileInitialized () {
482 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
484 // Get it from iterator which holds the pointer instance. If FALSE is returned
485 $fileSize = $this->size();
486 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] fileSize=%s', __METHOD__, __LINE__, $fileSize));
489 * The returned file size should not be FALSE or NULL as this means
490 * that the pointer class does not work correctly.
492 assert(is_int($fileSize));
494 // Is more than 0 returned?
495 $isInitialized = ($fileSize > 0);
498 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
499 return $isInitialized;
503 * Creates the assigned file
507 public function createFileHeader () {
508 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
510 // The file's header should not be initialized here
511 assert(!$this->isFileHeaderInitialized());
513 // Simple flush file header which will create it.
514 $this->flushFileHeader();
516 // Rewind seek position (to beginning of file) and update/flush file header
517 $this->rewindUpdateSeekPosition();
519 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__));
523 * Pre-allocates file (if enabled) with some space for later faster write access.
525 * @param $type Type of the file
528 public function preAllocateFile ($type) {
529 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
532 if ($this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_enabled') != 'Y') {
534 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Not pre-allocating file.', __METHOD__, __LINE__));
536 // Don't continue here.
541 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Pre-allocating file ...', __METHOD__, __LINE__));
543 // Calculate minimum length for one entry
544 $minLengthEntry = $this->getBlockInstance()->calculateMinimumBlockLength();
545 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] minLengthEntry=%s', __METHOD__, __LINE__, $minLengthEntry));
547 // Calulcate seek position
548 $seekPosition = $minLengthEntry * $this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_count');
549 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
551 // Now simply write a NUL there. This will pre-allocate the file.
552 $this->writeData($seekPosition, chr(0));
554 // Rewind seek position (to beginning of file) and update/flush file header
555 $this->rewindUpdateSeekPosition();
557 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
561 * Determines seek position
563 * @return $seekPosition Current seek position
565 public function determineSeekPosition () {
566 // Call pointer instance
567 return $this->getPointerInstance()->determineSeekPosition();
571 * Seek to given offset (default) or other possibilities as fseek() gives.
573 * @param $offset Offset to seek to (or used as "base" for other seeks)
574 * @param $whence Added to offset (default: only use offset to seek to)
575 * @return $status Status of file seek: 0 = success, -1 = failed
577 public function seek ($offset, $whence = SEEK_SET) {
578 // Call pointer instance
579 return $this->getPointerInstance()->seek($offset, $whence);
583 * Reads given amount of bytes from file.
585 * @param $bytes Amount of bytes to read
586 * @return $data Data read from file
588 public function read ($bytes) {
589 // Call pointer instance
590 return $this->getPointerInstance()->read($bytes);
594 * Rewinds to the beginning of the file
596 * @return $status Status of this operation
598 public function rewind () {
599 // Call pointer instance
600 return $this->getPointerInstance()->rewind();
604 * Analyzes entries in index file. This will count all found (and valid)
605 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
606 * only gaps are found, the file is considered as "virgin" (no entries).
610 public function analyzeFile () {
611 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
613 // Make sure the file is initialized
614 assert($this->isFileInitialized());
616 // Init counters and gaps array
617 $this->initCountersGapsArray();
619 // Output message (as this may take some time)
620 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__));
622 // First rewind to the begining
625 // Then try to load all entries
626 while ($this->valid()) {
631 $current = $this->getCurrentBlock();
634 * If the block is empty, maybe the whole file is? This could mean
635 * that the file has been pre-allocated.
637 if (empty($current)) {
638 // Then skip this part
643 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] current()=%s', __METHOD__, __LINE__, strlen($current)));
646 // If the last read block is empty, check gaps
647 if (empty($current)) {
649 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Found a total of %s gaps.', __METHOD__, __LINE__, count($this->gaps)));
651 // Check gaps, if the whole file is empty.
652 if ($this->isFileOnlyGaps()) {
653 // Only gaps, so don't continue here.
658 * The above call has calculated a total size of all gaps. If the
659 * percentage of gaps passes a "soft" limit and last
660 * defragmentation is to far in the past, or if a "hard" limit has
661 * reached, run defragmentation.
663 if ($this->isDefragmentationNeeded()) {
664 // Run "defragmentation"
665 $this->doRunDefragmentation();
668 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
672 * Advances to next "block" of bytes
676 public function next () {
677 // Is there nothing to read?
678 if (!$this->valid()) {
684 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] key()=%s', __FUNCTION__, __LINE__, $this->key()));
686 // Make sure the block instance is set
687 assert($this->getBlockInstance() instanceof CalculatableBlock);
689 // First calculate minimum block length
690 $length = $this->getBlockInstance()->calculateMinimumBlockLength();
691 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] length=%s', __FUNCTION__, __LINE__, $length));
693 // Short be more than zero!
696 // Read possibly back-buffered bytes from previous call of next().
697 $data = $this->getBackBuffer();
700 * Read until a entry/block separator has been found. The next read
701 * "block" may not fit, so this loop will continue until the EOB or EOF
702 * has been reached whatever comes first.
704 while ((!$this->isEndOfFileReached()) && (!self::isBlockSeparatorFound($data))) {
705 // Then read the next possible block
706 $block = $this->read($length);
709 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] block()=%s,length=%s', __FUNCTION__, __LINE__, strlen($block), $length));
712 if (strlen(trim($block)) == 0) {
713 // Mark this block as empty
714 $this->markCurrentBlockAsEmpty(strlen($block));
716 // Skip to next block
720 // At this block then
724 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] data()=%s', __FUNCTION__, __LINE__, strlen($data)));
728 if ($this->isEndOfFileReached()) {
729 // Set whole data as current read block
730 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('Calling setCurrentBlock(' . strlen($data) . ') ...');
731 $this->setCurrentBlock($data);
733 // Then abort here silently
734 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('EOF reached.');
739 * Init back-buffer which is the data that has been found beyond the
742 $this->initBackBuffer();
745 $dataArray = explode(chr(self::SEPARATOR_ENTRIES), $data);
747 // This array must contain two elements
748 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('dataArray=' . print_r($dataArray, TRUE));
749 assert(count($dataArray) == 2);
751 // Left part is the actual block, right one the back-buffer data
752 $this->setCurrentBlock($dataArray[0]);
753 $this->setBackBuffer($dataArray[1]);
757 * Checks wether the current entry is valid (not at the end of the file).
758 * This method will return TRUE if an emptied (nulled) entry has been found.
760 * @return $isValid Whether the next entry is valid
762 public function valid () {
763 // Make sure the block instance is set
764 assert($this->getBlockInstance() instanceof Block);
766 // First calculate minimum block length
767 $length = $this->getBlockInstance()->calculateMinimumBlockLength();
769 // Short be more than zero!
772 // Get current seek position
773 $seekPosition = $this->key();
775 // Then try to read it
776 $data = $this->read($length);
778 // If some bytes could be read, all is fine
779 $isValid = ((is_string($data)) && (strlen($data) > 0));
782 $headerSize = $this->getHeaderSize();
784 // Is the seek position at or beyond the header?
785 if ($seekPosition >= $headerSize) {
786 // Seek back to old position
787 $this->seek($seekPosition);
789 // Seek directly behind the header
790 $this->seek($headerSize);
798 * Gets current seek position ("key").
800 * @return $key Current key in iteration
802 public function key () {
803 // Call pointer instance
804 return $this->getPointerInstance()->determineSeekPosition();
808 * Reads the file header
812 public function readFileHeader () {
813 // Make sure the block instance is set
814 assert($this->getBlockInstance() instanceof Block);
816 // Call block instance
817 $this->getBlockInstance()->readFileHeader();
821 * Flushes the file header
825 public function flushFileHeader () {
826 // Make sure the block instance is set
827 assert($this->getBlockInstance() instanceof Block);
829 // Call block instance
830 $this->getBlockInstance()->flushFileHeader();