3 namespace Org\Mxchange\CoreFramework\Filesystem\File;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Filesystem\Block;
8 use Org\Mxchange\CoreFramework\Filesystem\Block\CalculatableBlock;
9 use Org\Mxchange\CoreFramework\Filesystem\File\BaseAbstractFile;
15 * A general binary file class
17 * @author Roland Haeder <webmaster@ship-simu.org>
19 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2019 Core Developer Team
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.ship-simu.org
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 abstract class BaseBinaryFile extends BaseAbstractFile {
38 * Separator for header data
40 const SEPARATOR_HEADER_DATA = 0x01;
43 * Separator header->entries
45 const SEPARATOR_HEADER_ENTRIES = 0x02;
48 * Separator group->hash
50 const SEPARATOR_GROUP_HASH = 0x03;
53 * Separator hash->value
55 const SEPARATOR_HASH_VALUE = 0x04;
58 * Separator entry->entry
60 const SEPARATOR_ENTRIES = 0x05;
63 * Separator type->position
65 const SEPARATOR_TYPE_POSITION = 0x06;
70 const LENGTH_COUNT = 20;
75 const LENGTH_POSITION = 20;
80 const LENGTH_GROUP = 10;
83 * Maximum length of entry type
85 const LENGTH_TYPE = 20;
87 //***** Array elements for 'gaps' array *****
92 const GAPS_INDEX_START = 'start';
97 const GAPS_INDEX_END = 'end';
100 * Current seek position
102 private $seekPosition = 0;
107 private $headerSize = 0;
112 private $header = array();
115 * Seek positions for gaps ("fragmentation")
117 private $gaps = array();
120 * Seek positions for damaged entries (e.g. mismatching hash sum, ...)
122 private $damagedEntries = array();
127 private $backBuffer = '';
130 * Currently loaded block (will be returned by current())
132 private $currentBlock = '';
135 * Protected constructor
137 * @param $className Name of the class
140 protected function __construct ($className) {
141 // Call parent constructor
142 parent::__construct($className);
144 // Init counters and gaps array
145 $this->initCountersGapsArray();
149 * Checks whether the abstracted file only contains gaps by counting all
150 * gaps' bytes together and compare it to total length.
152 * @return $isGapsOnly Whether the abstracted file only contains gaps
154 private function isFileOnlyGaps () {
155 // First/last gap found?
156 /* Only for debugging
157 if (isset($this->gaps[0])) {
158 // Output first and last gap
159 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)));
163 // Now count every gap
165 foreach ($this->gaps as $gap) {
166 // Calculate size of found gap: end-start including both
167 $gapsSize += ($gap[self::GAPS_INDEX_END] - $gap[self::GAPS_INDEX_START]);
171 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] gapsSize=%s,this->headerSize=%s', __METHOD__, __LINE__, $gapsSize, $this->getHeaderSize()));
173 // Total gap size + header size must be same as file size
174 $isGapsOnly = (($this->getHeaderSize() + $gapsSize) == $this->getFileSize());
181 * Initializes counter for valid entries, arrays for damaged entries and
182 * an array for gap seek positions. If you call this method on your own,
183 * please re-analyze the file structure. So you are better to call
184 * analyzeFile() instead of this method.
188 public function initCountersGapsArray () {
189 // Init counter and seek position
190 $this->setCounter(0);
191 $this->setSeekPosition(0);
194 $this->gaps = array();
195 $this->damagedEntries = array();
199 * Getter for header size
201 * @return $totalEntries Size of file header
203 public final function getHeaderSize () {
205 return $this->headerSize;
209 * Setter for header size
211 * @param $headerSize Size of file header
214 public final function setHeaderSize ($headerSize) {
216 $this->headerSize = $headerSize;
220 * Getter for header array
222 * @return $totalEntries Size of file header
224 public final function getHeader () {
226 return $this->header;
232 * @param $header Array for a file header
235 public final function setHeader (array $header) {
237 $this->header = $header;
241 * Getter for seek position
243 * @return $seekPosition Current seek position (stored here in object)
245 public final function getSeekPosition () {
247 return $this->seekPosition;
251 * Setter for seek position
253 * @param $seekPosition Current seek position (stored here in object)
256 protected final function setSeekPosition ($seekPosition) {
258 $this->seekPosition = $seekPosition;
262 * Updates seekPosition attribute from file to avoid to much access on file.
266 public function updateSeekPosition () {
267 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
269 // Get key (= seek position)
270 $seekPosition = $this->key();
271 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Setting seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
274 $this->setSeekPosition($seekPosition);
276 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
280 * Seeks to beginning of file, updates seek position in this object and
281 * flushes the header.
285 protected function rewindUpdateSeekPosition () {
286 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
288 // flushFileHeader must be callable
289 assert(is_callable(array($this, 'flushFileHeader')));
291 // Seek to beginning of file
294 // And update seek position ...
295 $this->updateSeekPosition();
297 // ... to write it back into the file
298 $this->flushFileHeader();
300 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
304 * Seeks to old position
308 protected function seekToOldPosition () {
309 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
311 // Seek to currently ("old") saved position
312 $this->seek($this->getSeekPosition());
314 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
318 * Checks whether the block separator has been found
320 * @param $str String to look in
321 * @return $isFound Whether the block separator has been found
323 public static function isBlockSeparatorFound ($str) {
325 $isFound = (strpos($str, chr(self::SEPARATOR_ENTRIES)) !== false);
332 * Initializes the back-buffer by setting it to an empty string.
336 private function initBackBuffer () {
337 // Simply call the setter
338 $this->setBackBuffer('');
342 * Setter for backBuffer field
344 * @param $backBuffer Characters to "store" in back-buffer
347 private function setBackBuffer ($backBuffer) {
348 // Cast to string (so no arrays or objects)
349 $backBuffer = (string) $backBuffer;
352 $this->backBuffer = $backBuffer;
356 * Getter for backBuffer field
358 * @return $backBuffer Characters "stored" in back-buffer
360 private function getBackBuffer () {
361 return $this->backBuffer;
365 * Setter for currentBlock field
367 * @param $currentBlock Characters to set a currently loaded block
370 private function setCurrentBlock ($currentBlock) {
371 // Cast to string (so no arrays or objects)
372 $currentBlock = (string) $currentBlock;
375 $this->currentBlock = $currentBlock;
379 * Gets currently read data
381 * @return $current Currently read data
383 public function getCurrentBlock () {
385 return $this->currentBlock;
389 * Initializes this file class
391 * @param $infoInstance An instance of a SplFileInfo class
394 protected function initFile (SplFileInfo $infoInstance) {
395 // Get a file i/o pointer instance
396 $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($infoInstance));
398 // ... and set it here
399 $this->setPointerInstance($pointerInstance);
403 * Writes data at given position
405 * @param $seekPosition Seek position
406 * @param $data Data to be written
407 * @param $flushHeader Whether to flush the header (default: flush)
410 public function writeData ($seekPosition, $data, $flushHeader = true) {
411 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data()=%d - CALLED!', __METHOD__, __LINE__, $seekPosition, strlen($data)));
413 // Write data at given position
414 $this->getPointerInstance()->writeAtPosition($seekPosition, $data);
417 $this->incrementCounter();
419 // Update seek position
420 $this->updateSeekPosition();
423 if ($flushHeader === true) {
425 $this->flushFileHeader();
427 // Seek to old position
428 $this->seekToOldPosition();
431 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
435 * Marks the currently loaded block as empty (with length of the block)
437 * @param $length Length of the block
440 protected function markCurrentBlockAsEmpty ($length) {
441 // Get current seek position
442 $currentPosition = $this->key();
444 // Now add it as gap entry
445 array_push($this->gaps, array(
446 self::GAPS_INDEX_START => ($currentPosition - $length),
447 self::GAPS_INDEX_END => $currentPosition,
452 * Checks whether the file header is initialized
454 * @return $isInitialized Whether the file header is initialized
456 public function isFileHeaderInitialized () {
457 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
459 // Default is not initialized
460 $isInitialized = false;
462 // Is the file initialized?
463 if ($this->isFileInitialized()) {
464 // Some bytes has been written, so rewind to start of it.
465 $rewindStatus = $this->rewind();
466 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] rewindStatus=%s', __METHOD__, __LINE__, $rewindStatus));
468 // Is the rewind() call successfull?
469 if ($rewindStatus != 1) {
470 // Something bad happened
471 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Could not rewind().', __METHOD__, __LINE__));
475 $this->readFileHeader();
477 // The above method does already check the header
478 $isInitialized = true;
482 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
483 return $isInitialized;
487 * Checks whether the assigned file has been initialized
489 * @return $isInitialized Whether the file's size is zero
491 public function isFileInitialized () {
492 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
494 // Get it from iterator which holds the pointer instance. If false is returned
495 $fileSize = $this->size();
496 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] fileSize=%s', __METHOD__, __LINE__, $fileSize));
499 * The returned file size should not be false or NULL as this means
500 * that the pointer class does not work correctly.
502 assert(is_int($fileSize));
504 // Is more than 0 returned?
505 $isInitialized = ($fileSize > 0);
508 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
509 return $isInitialized;
513 * Creates the assigned file
517 public function createFileHeader () {
518 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
520 // The file's header should not be initialized here
521 assert(!$this->isFileHeaderInitialized());
523 // Simple flush file header which will create it.
524 $this->flushFileHeader();
526 // Rewind seek position (to beginning of file) and update/flush file header
527 $this->rewindUpdateSeekPosition();
529 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
533 * Pre-allocates file (if enabled) with some space for later faster write access.
535 * @param $type Type of the file
538 public function preAllocateFile ($type) {
539 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
542 if ($this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_enabled') != 'Y') {
544 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Not pre-allocating file.', __METHOD__, __LINE__));
546 // Don't continue here.
551 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Pre-allocating file ...', __METHOD__, __LINE__));
553 // Calculate minimum length for one entry
554 $minLengthEntry = $this->getBlockInstance()->calculateMinimumBlockLength();
555 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] minLengthEntry=%s', __METHOD__, __LINE__, $minLengthEntry));
557 // Calulcate seek position
558 $seekPosition = $minLengthEntry * $this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_count');
559 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
561 // Now simply write a NUL there. This will pre-allocate the file.
562 $this->writeData($seekPosition, chr(0));
564 // Rewind seek position (to beginning of file) and update/flush file header
565 $this->rewindUpdateSeekPosition();
567 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
571 * Determines seek position
573 * @return $seekPosition Current seek position
575 public function determineSeekPosition () {
576 // Call pointer instance
577 return $this->getPointerInstance()->determineSeekPosition();
581 * Seek to given offset (default) or other possibilities as fseek() gives.
583 * @param $offset Offset to seek to (or used as "base" for other seeks)
584 * @param $whence Added to offset (default: only use offset to seek to)
585 * @return $status Status of file seek: 0 = success, -1 = failed
587 public function seek ($offset, $whence = SEEK_SET) {
588 // Call pointer instance
589 return $this->getPointerInstance()->seek($offset, $whence);
593 * Reads given amount of bytes from file.
595 * @param $bytes Amount of bytes to read
596 * @return $data Data read from file
598 public function read ($bytes = NULL) {
599 // $bytes shall be integer
600 assert(is_int($bytes));
602 // Call pointer instance
603 return $this->getPointerInstance()->read($bytes);
607 * Rewinds to the beginning of the file
609 * @return $status Status of this operation
611 public function rewind () {
612 // Call pointer instance
613 return $this->getPointerInstance()->rewind();
617 * Analyzes entries in index file. This will count all found (and valid)
618 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
619 * only gaps are found, the file is considered as "virgin" (no entries).
623 public function analyzeFile () {
624 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
626 // Make sure the file is initialized
627 assert($this->isFileInitialized());
629 // Init counters and gaps array
630 $this->initCountersGapsArray();
632 // Output message (as this may take some time)
633 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__));
635 // First rewind to the begining
638 // Then try to load all entries
639 while ($this->valid()) {
644 $current = $this->getCurrentBlock();
647 * If the block is empty, maybe the whole file is? This could mean
648 * that the file has been pre-allocated.
650 if (empty($current)) {
651 // Then skip this part
656 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] current()=%d', __METHOD__, __LINE__, strlen($current)));
659 // If the last read block is empty, check gaps
660 if (empty($current)) {
662 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Found a total of %s gaps.', __METHOD__, __LINE__, count($this->gaps)));
664 // Check gaps, if the whole file is empty.
665 if ($this->isFileOnlyGaps()) {
666 // Only gaps, so don't continue here.
671 * The above call has calculated a total size of all gaps. If the
672 * percentage of gaps passes a "soft" limit and last
673 * defragmentation is to far in the past, or if a "hard" limit has
674 * reached, run defragmentation.
676 if ($this->isDefragmentationNeeded()) {
677 // Run "defragmentation"
678 $this->doRunDefragmentation();
681 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
685 * Advances to next "block" of bytes
689 public function next () {
690 // Is there nothing to read?
691 if (!$this->valid()) {
697 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] key()=%d', __FUNCTION__, __LINE__, $this->key()));
699 // Make sure the block instance is set
700 assert($this->getBlockInstance() instanceof CalculatableBlock);
702 // First calculate minimum block length
703 $length = $this->getBlockInstance()->calculateMinimumBlockLength();
704 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] length=%s', __FUNCTION__, __LINE__, $length));
706 // Short be more than zero!
709 // Read possibly back-buffered bytes from previous call of next().
710 $data = $this->getBackBuffer();
713 * Read until a entry/block separator has been found. The next read
714 * "block" may not fit, so this loop will continue until the EOB or EOF
715 * has been reached whatever comes first.
717 while ((!$this->isEndOfFileReached()) && (!self::isBlockSeparatorFound($data))) {
718 // Then read the next possible block
719 $block = $this->read($length);
722 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] block()=%d,length=%s', __FUNCTION__, __LINE__, strlen($block), $length));
725 if (strlen(trim($block)) == 0) {
726 // Mark this block as empty
727 $this->markCurrentBlockAsEmpty(strlen($block));
729 // Skip to next block
733 // At this block then
737 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] data()=%d', __FUNCTION__, __LINE__, strlen($data)));
741 if ($this->isEndOfFileReached()) {
742 // Set whole data as current read block
743 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Calling setCurrentBlock(' . strlen($data) . ') ...');
744 $this->setCurrentBlock($data);
746 // Then abort here silently
747 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('EOF reached.');
752 * Init back-buffer which is the data that has been found beyond the
755 $this->initBackBuffer();
758 $dataArray = explode(chr(self::SEPARATOR_ENTRIES), $data);
760 // This array must contain two elements
761 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('dataArray=' . print_r($dataArray, true));
762 assert(count($dataArray) == 2);
764 // Left part is the actual block, right one the back-buffer data
765 $this->setCurrentBlock($dataArray[0]);
766 $this->setBackBuffer($dataArray[1]);
770 * Checks wether the current entry is valid (not at the end of the file).
771 * This method will return true if an emptied (nulled) entry has been found.
773 * @return $isValid Whether the next entry is valid
775 public function valid () {
776 // Make sure the block instance is set
777 assert($this->getBlockInstance() instanceof Block);
779 // First calculate minimum block length
780 $length = $this->getBlockInstance()->calculateMinimumBlockLength();
782 // Short be more than zero!
785 // Get current seek position
786 $seekPosition = $this->key();
788 // Then try to read it
789 $data = $this->read($length);
791 // If some bytes could be read, all is fine
792 $isValid = ((is_string($data)) && (strlen($data) > 0));
795 $headerSize = $this->getHeaderSize();
797 // Is the seek position at or beyond the header?
798 if ($seekPosition >= $headerSize) {
799 // Seek back to old position
800 $this->seek($seekPosition);
802 // Seek directly behind the header
803 $this->seek($headerSize);
811 * Gets current seek position ("key").
813 * @return $key Current key in iteration
815 public function key () {
816 // Call pointer instance
817 return $this->getPointerInstance()->determineSeekPosition();
821 * Reads the file header
825 public function readFileHeader () {
826 // Make sure the block instance is set
827 assert($this->getBlockInstance() instanceof Block);
829 // Call block instance
830 $this->getBlockInstance()->readFileHeader();
834 * Flushes the file header
838 public function flushFileHeader () {
839 // Make sure the block instance is set
840 assert($this->getBlockInstance() instanceof Block);
842 // Call block instance
843 $this->getBlockInstance()->flushFileHeader();
847 * Searches for next suitable gap the given length of data can fit in
848 * including padding bytes.
850 * @param $length Length of raw data
851 * @return $seekPosition Found next gap's seek position
853 public function searchNextGap ($length) {
854 // If the file is only gaps, no need to seek
855 if ($this->isFileOnlyGaps()) {
856 // The first empty block is the first one right after the header
857 return ($this->getHeaderSize() + 1);
861 $this->partialStub('length=' . $length);