3 namespace CoreFramework\Filesystem\File;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Filesystem\Block;
10 * A general binary file class
12 * @author Roland Haeder <webmaster@ship-simu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.ship-simu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class BaseBinaryFile extends BaseAbstractFile {
33 * Separator for header data
35 const SEPARATOR_HEADER_DATA = 0x01;
38 * Separator header->entries
40 const SEPARATOR_HEADER_ENTRIES = 0x02;
43 * Separator group->hash
45 const SEPARATOR_GROUP_HASH = 0x03;
48 * Separator hash->value
50 const SEPARATOR_HASH_VALUE = 0x04;
53 * Separator entry->entry
55 const SEPARATOR_ENTRIES = 0x05;
58 * Separator type->position
60 const SEPARATOR_TYPE_POSITION = 0x06;
65 const LENGTH_COUNT = 20;
70 const LENGTH_POSITION = 20;
75 const LENGTH_GROUP = 10;
78 * Maximum length of entry type
80 const LENGTH_TYPE = 20;
82 //***** Array elements for 'gaps' array *****
87 const GAPS_INDEX_START = 'start';
92 const GAPS_INDEX_END = 'end';
95 * Current seek position
97 private $seekPosition = 0;
102 private $headerSize = 0;
107 private $header = array();
110 * Seek positions for gaps ("fragmentation")
112 private $gaps = array();
115 * Seek positions for damaged entries (e.g. mismatching hash sum, ...)
117 private $damagedEntries = array();
122 private $backBuffer = '';
125 * Currently loaded block (will be returned by current())
127 private $currentBlock = '';
130 * Protected constructor
132 * @param $className Name of the class
135 protected function __construct ($className) {
136 // Call parent constructor
137 parent::__construct($className);
139 // Init counters and gaps array
140 $this->initCountersGapsArray();
144 * Checks whether the abstracted file only contains gaps by counting all
145 * gaps' bytes together and compare it to total length.
147 * @return $isGapsOnly Whether the abstracted file only contains gaps
149 private function isFileOnlyGaps () {
150 // First/last gap found?
151 /* Only for debugging
152 if (isset($this->gaps[0])) {
153 // Output first and last gap
154 self::createDebugInstance(__CLASS__, __LINE__)->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)));
158 // Now count every gap
160 foreach ($this->gaps as $gap) {
161 // Calculate size of found gap: end-start including both
162 $gapsSize += ($gap[self::GAPS_INDEX_END] - $gap[self::GAPS_INDEX_START]);
166 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] gapsSize=%s,this->headerSize=%s', __METHOD__, __LINE__, $gapsSize, $this->getHeaderSize()));
168 // Total gap size + header size must be same as file size
169 $isGapsOnly = (($this->getHeaderSize() + $gapsSize) == $this->getFileSize());
176 * Initializes counter for valid entries, arrays for damaged entries and
177 * an array for gap seek positions. If you call this method on your own,
178 * please re-analyze the file structure. So you are better to call
179 * analyzeFile() instead of this method.
183 public function initCountersGapsArray () {
184 // Init counter and seek position
185 $this->setCounter(0);
186 $this->setSeekPosition(0);
189 $this->gaps = array();
190 $this->damagedEntries = array();
194 * Getter for header size
196 * @return $totalEntries Size of file header
198 public final function getHeaderSize () {
200 return $this->headerSize;
204 * Setter for header size
206 * @param $headerSize Size of file header
209 public final function setHeaderSize ($headerSize) {
211 $this->headerSize = $headerSize;
215 * Getter for header array
217 * @return $totalEntries Size of file header
219 public final function getHeader () {
221 return $this->header;
227 * @param $header Array for a file header
230 public final function setHeader (array $header) {
232 $this->header = $header;
236 * Getter for seek position
238 * @return $seekPosition Current seek position (stored here in object)
240 public final function getSeekPosition () {
242 return $this->seekPosition;
246 * Setter for seek position
248 * @param $seekPosition Current seek position (stored here in object)
251 protected final function setSeekPosition ($seekPosition) {
253 $this->seekPosition = $seekPosition;
257 * Updates seekPosition attribute from file to avoid to much access on file.
261 public function updateSeekPosition () {
262 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
264 // Get key (= seek position)
265 $seekPosition = $this->key();
266 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Setting seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
269 $this->setSeekPosition($seekPosition);
271 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
275 * Seeks to beginning of file, updates seek position in this object and
276 * flushes the header.
280 protected function rewindUpdateSeekPosition () {
281 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
283 // flushFileHeader must be callable
284 assert(is_callable(array($this, 'flushFileHeader')));
286 // Seek to beginning of file
289 // And update seek position ...
290 $this->updateSeekPosition();
292 // ... to write it back into the file
293 $this->flushFileHeader();
295 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
299 * Seeks to old position
303 protected function seekToOldPosition () {
304 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
306 // Seek to currently ("old") saved position
307 $this->seek($this->getSeekPosition());
309 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
313 * Checks whether the block separator has been found
315 * @param $str String to look in
316 * @return $isFound Whether the block separator has been found
318 public static function isBlockSeparatorFound ($str) {
320 $isFound = (strpos($str, chr(self::SEPARATOR_ENTRIES)) !== FALSE);
327 * Initializes the back-buffer by setting it to an empty string.
331 private function initBackBuffer () {
332 // Simply call the setter
333 $this->setBackBuffer('');
337 * Setter for backBuffer field
339 * @param $backBuffer Characters to "store" in back-buffer
342 private function setBackBuffer ($backBuffer) {
343 // Cast to string (so no arrays or objects)
344 $backBuffer = (string) $backBuffer;
347 $this->backBuffer = $backBuffer;
351 * Getter for backBuffer field
353 * @return $backBuffer Characters "stored" in back-buffer
355 private function getBackBuffer () {
356 return $this->backBuffer;
360 * Setter for currentBlock field
362 * @param $currentBlock Characters to set a currently loaded block
365 private function setCurrentBlock ($currentBlock) {
366 // Cast to string (so no arrays or objects)
367 $currentBlock = (string) $currentBlock;
370 $this->currentBlock = $currentBlock;
374 * Gets currently read data
376 * @return $current Currently read data
378 public function getCurrentBlock () {
380 return $this->currentBlock;
384 * Initializes this file class
386 * @param $fileName Name of this abstract file
389 protected function initFile ($fileName) {
390 // Get a file i/o pointer instance
391 $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName));
393 // ... and set it here
394 $this->setPointerInstance($pointerInstance);
398 * Writes data at given position
400 * @param $seekPosition Seek position
401 * @param $data Data to be written
402 * @param $flushHeader Whether to flush the header (default: flush)
405 public function writeData ($seekPosition, $data, $flushHeader = TRUE) {
406 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data()=%d - CALLED!', __METHOD__, __LINE__, $seekPosition, strlen($data)));
408 // Write data at given position
409 $this->getPointerInstance()->writeAtPosition($seekPosition, $data);
412 $this->incrementCounter();
414 // Update seek position
415 $this->updateSeekPosition();
418 if ($flushHeader === TRUE) {
420 $this->flushFileHeader();
422 // Seek to old position
423 $this->seekToOldPosition();
426 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
430 * Marks the currently loaded block as empty (with length of the block)
432 * @param $length Length of the block
435 protected function markCurrentBlockAsEmpty ($length) {
436 // Get current seek position
437 $currentPosition = $this->key();
439 // Now add it as gap entry
440 array_push($this->gaps, array(
441 self::GAPS_INDEX_START => ($currentPosition - $length),
442 self::GAPS_INDEX_END => $currentPosition,
447 * Checks whether the file header is initialized
449 * @return $isInitialized Whether the file header is initialized
451 public function isFileHeaderInitialized () {
452 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
454 // Default is not initialized
455 $isInitialized = FALSE;
457 // Is the file initialized?
458 if ($this->isFileInitialized()) {
459 // Some bytes has been written, so rewind to start of it.
460 $rewindStatus = $this->rewind();
461 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] rewindStatus=%s', __METHOD__, __LINE__, $rewindStatus));
463 // Is the rewind() call successfull?
464 if ($rewindStatus != 1) {
465 // Something bad happened
466 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Could not rewind().', __METHOD__, __LINE__));
470 $this->readFileHeader();
472 // The above method does already check the header
473 $isInitialized = TRUE;
477 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
478 return $isInitialized;
482 * Checks whether the assigned file has been initialized
484 * @return $isInitialized Whether the file's size is zero
486 public function isFileInitialized () {
487 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
489 // Get it from iterator which holds the pointer instance. If FALSE is returned
490 $fileSize = $this->size();
491 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] fileSize=%s', __METHOD__, __LINE__, $fileSize));
494 * The returned file size should not be FALSE or NULL as this means
495 * that the pointer class does not work correctly.
497 assert(is_int($fileSize));
499 // Is more than 0 returned?
500 $isInitialized = ($fileSize > 0);
503 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
504 return $isInitialized;
508 * Creates the assigned file
512 public function createFileHeader () {
513 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
515 // The file's header should not be initialized here
516 assert(!$this->isFileHeaderInitialized());
518 // Simple flush file header which will create it.
519 $this->flushFileHeader();
521 // Rewind seek position (to beginning of file) and update/flush file header
522 $this->rewindUpdateSeekPosition();
524 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
528 * Pre-allocates file (if enabled) with some space for later faster write access.
530 * @param $type Type of the file
533 public function preAllocateFile ($type) {
534 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
537 if ($this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_enabled') != 'Y') {
539 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Not pre-allocating file.', __METHOD__, __LINE__));
541 // Don't continue here.
546 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Pre-allocating file ...', __METHOD__, __LINE__));
548 // Calculate minimum length for one entry
549 $minLengthEntry = $this->getBlockInstance()->calculateMinimumBlockLength();
550 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] minLengthEntry=%s', __METHOD__, __LINE__, $minLengthEntry));
552 // Calulcate seek position
553 $seekPosition = $minLengthEntry * $this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_count');
554 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
556 // Now simply write a NUL there. This will pre-allocate the file.
557 $this->writeData($seekPosition, chr(0));
559 // Rewind seek position (to beginning of file) and update/flush file header
560 $this->rewindUpdateSeekPosition();
562 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
566 * Determines seek position
568 * @return $seekPosition Current seek position
570 public function determineSeekPosition () {
571 // Call pointer instance
572 return $this->getPointerInstance()->determineSeekPosition();
576 * Seek to given offset (default) or other possibilities as fseek() gives.
578 * @param $offset Offset to seek to (or used as "base" for other seeks)
579 * @param $whence Added to offset (default: only use offset to seek to)
580 * @return $status Status of file seek: 0 = success, -1 = failed
582 public function seek ($offset, $whence = SEEK_SET) {
583 // Call pointer instance
584 return $this->getPointerInstance()->seek($offset, $whence);
588 * Reads given amount of bytes from file.
590 * @param $bytes Amount of bytes to read
591 * @return $data Data read from file
593 public function read ($bytes = NULL) {
594 // $bytes shall be integer
595 assert(is_int($bytes));
597 // Call pointer instance
598 return $this->getPointerInstance()->read($bytes);
602 * Rewinds to the beginning of the file
604 * @return $status Status of this operation
606 public function rewind () {
607 // Call pointer instance
608 return $this->getPointerInstance()->rewind();
612 * Analyzes entries in index file. This will count all found (and valid)
613 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
614 * only gaps are found, the file is considered as "virgin" (no entries).
618 public function analyzeFile () {
619 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
621 // Make sure the file is initialized
622 assert($this->isFileInitialized());
624 // Init counters and gaps array
625 $this->initCountersGapsArray();
627 // Output message (as this may take some time)
628 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__));
630 // First rewind to the begining
633 // Then try to load all entries
634 while ($this->valid()) {
639 $current = $this->getCurrentBlock();
642 * If the block is empty, maybe the whole file is? This could mean
643 * that the file has been pre-allocated.
645 if (empty($current)) {
646 // Then skip this part
651 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] current()=%d', __METHOD__, __LINE__, strlen($current)));
654 // If the last read block is empty, check gaps
655 if (empty($current)) {
657 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Found a total of %s gaps.', __METHOD__, __LINE__, count($this->gaps)));
659 // Check gaps, if the whole file is empty.
660 if ($this->isFileOnlyGaps()) {
661 // Only gaps, so don't continue here.
666 * The above call has calculated a total size of all gaps. If the
667 * percentage of gaps passes a "soft" limit and last
668 * defragmentation is to far in the past, or if a "hard" limit has
669 * reached, run defragmentation.
671 if ($this->isDefragmentationNeeded()) {
672 // Run "defragmentation"
673 $this->doRunDefragmentation();
676 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
680 * Advances to next "block" of bytes
684 public function next () {
685 // Is there nothing to read?
686 if (!$this->valid()) {
692 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] key()=%d', __FUNCTION__, __LINE__, $this->key()));
694 // Make sure the block instance is set
695 assert($this->getBlockInstance() instanceof CalculatableBlock);
697 // First calculate minimum block length
698 $length = $this->getBlockInstance()->calculateMinimumBlockLength();
699 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] length=%s', __FUNCTION__, __LINE__, $length));
701 // Short be more than zero!
704 // Read possibly back-buffered bytes from previous call of next().
705 $data = $this->getBackBuffer();
708 * Read until a entry/block separator has been found. The next read
709 * "block" may not fit, so this loop will continue until the EOB or EOF
710 * has been reached whatever comes first.
712 while ((!$this->isEndOfFileReached()) && (!self::isBlockSeparatorFound($data))) {
713 // Then read the next possible block
714 $block = $this->read($length);
717 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] block()=%d,length=%s', __FUNCTION__, __LINE__, strlen($block), $length));
720 if (strlen(trim($block)) == 0) {
721 // Mark this block as empty
722 $this->markCurrentBlockAsEmpty(strlen($block));
724 // Skip to next block
728 // At this block then
732 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] data()=%d', __FUNCTION__, __LINE__, strlen($data)));
736 if ($this->isEndOfFileReached()) {
737 // Set whole data as current read block
738 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Calling setCurrentBlock(' . strlen($data) . ') ...');
739 $this->setCurrentBlock($data);
741 // Then abort here silently
742 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('EOF reached.');
747 * Init back-buffer which is the data that has been found beyond the
750 $this->initBackBuffer();
753 $dataArray = explode(chr(self::SEPARATOR_ENTRIES), $data);
755 // This array must contain two elements
756 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('dataArray=' . print_r($dataArray, TRUE));
757 assert(count($dataArray) == 2);
759 // Left part is the actual block, right one the back-buffer data
760 $this->setCurrentBlock($dataArray[0]);
761 $this->setBackBuffer($dataArray[1]);
765 * Checks wether the current entry is valid (not at the end of the file).
766 * This method will return TRUE if an emptied (nulled) entry has been found.
768 * @return $isValid Whether the next entry is valid
770 public function valid () {
771 // Make sure the block instance is set
772 assert($this->getBlockInstance() instanceof Block);
774 // First calculate minimum block length
775 $length = $this->getBlockInstance()->calculateMinimumBlockLength();
777 // Short be more than zero!
780 // Get current seek position
781 $seekPosition = $this->key();
783 // Then try to read it
784 $data = $this->read($length);
786 // If some bytes could be read, all is fine
787 $isValid = ((is_string($data)) && (strlen($data) > 0));
790 $headerSize = $this->getHeaderSize();
792 // Is the seek position at or beyond the header?
793 if ($seekPosition >= $headerSize) {
794 // Seek back to old position
795 $this->seek($seekPosition);
797 // Seek directly behind the header
798 $this->seek($headerSize);
806 * Gets current seek position ("key").
808 * @return $key Current key in iteration
810 public function key () {
811 // Call pointer instance
812 return $this->getPointerInstance()->determineSeekPosition();
816 * Reads the file header
820 public function readFileHeader () {
821 // Make sure the block instance is set
822 assert($this->getBlockInstance() instanceof Block);
824 // Call block instance
825 $this->getBlockInstance()->readFileHeader();
829 * Flushes the file header
833 public function flushFileHeader () {
834 // Make sure the block instance is set
835 assert($this->getBlockInstance() instanceof Block);
837 // Call block instance
838 $this->getBlockInstance()->flushFileHeader();
842 * Searches for next suitable gap the given length of data can fit in
843 * including padding bytes.
845 * @param $length Length of raw data
846 * @return $seekPosition Found next gap's seek position
848 public function searchNextGap ($length) {
849 // If the file is only gaps, no need to seek
850 if ($this->isFileOnlyGaps()) {
851 // The first empty block is the first one right after the header
852 return ($this->getHeaderSize() + 1);
856 $this->partialStub('length=' . $length);