3 namespace Org\Mxchange\CoreFramework\Filesystem\File;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Filesystem\Block;
9 use Org\Mxchange\CoreFramework\Filesystem\Block\CalculatableBlock;
10 use Org\Mxchange\CoreFramework\Filesystem\File\BaseAbstractFile;
16 * A general binary file class
18 * @author Roland Haeder <webmaster@ship-simu.org>
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
21 * @license GNU GPL 3.0 or any newer version
22 * @link http://www.ship-simu.org
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 abstract class BaseBinaryFile extends BaseAbstractFile {
39 * Separator for header data
41 const SEPARATOR_HEADER_DATA = 0x01;
44 * Separator header->entries
46 const SEPARATOR_HEADER_ENTRIES = 0x02;
49 * Separator group->hash
51 const SEPARATOR_GROUP_HASH = 0x03;
54 * Separator hash->value
56 const SEPARATOR_HASH_VALUE = 0x04;
59 * Separator entry->entry
61 const SEPARATOR_ENTRIES = 0x05;
64 * Separator type->position
66 const SEPARATOR_TYPE_POSITION = 0x06;
71 const LENGTH_COUNT = 20;
76 const LENGTH_POSITION = 20;
81 const LENGTH_GROUP = 10;
84 * Maximum length of entry type
86 const LENGTH_TYPE = 20;
88 //***** Array elements for 'gaps' array *****
93 const GAPS_INDEX_START = 'start';
98 const GAPS_INDEX_END = 'end';
101 * Current seek position
103 private $seekPosition = 0;
108 private $headerSize = 0;
113 private $header = array();
116 * Seek positions for gaps ("fragmentation")
118 private $gaps = array();
121 * Seek positions for damaged entries (e.g. mismatching hash sum, ...)
123 private $damagedEntries = array();
128 private $backBuffer = '';
131 * Currently loaded block (will be returned by current())
133 private $currentBlock = '';
136 * Protected constructor
138 * @param $className Name of the class
141 protected function __construct ($className) {
142 // Call parent constructor
143 parent::__construct($className);
145 // Init counters and gaps array
146 $this->initCountersGapsArray();
150 * Checks whether the abstracted file only contains gaps by counting all
151 * gaps' bytes together and compare it to total length.
153 * @return $isGapsOnly Whether the abstracted file only contains gaps
155 private function isFileOnlyGaps () {
156 // First/last gap found?
157 /* Only for debugging
158 if (isset($this->gaps[0])) {
159 // Output first and last gap
160 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)));
164 // Now count every gap
166 foreach ($this->gaps as $gap) {
167 // Calculate size of found gap: end-start including both
168 $gapsSize += ($gap[self::GAPS_INDEX_END] - $gap[self::GAPS_INDEX_START]);
172 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] gapsSize=%s,this->headerSize=%s', __METHOD__, __LINE__, $gapsSize, $this->getHeaderSize()));
174 // Total gap size + header size must be same as file size
175 $isGapsOnly = (($this->getHeaderSize() + $gapsSize) == $this->getFileSize());
182 * Initializes counter for valid entries, arrays for damaged entries and
183 * an array for gap seek positions. If you call this method on your own,
184 * please re-analyze the file structure. So you are better to call
185 * analyzeFile() instead of this method.
189 public function initCountersGapsArray () {
190 // Init counter and seek position
191 $this->setCounter(0);
192 $this->setSeekPosition(0);
195 $this->gaps = array();
196 $this->damagedEntries = array();
200 * Getter for header size
202 * @return $totalEntries Size of file header
204 public final function getHeaderSize () {
206 return $this->headerSize;
210 * Setter for header size
212 * @param $headerSize Size of file header
215 public final function setHeaderSize ($headerSize) {
217 $this->headerSize = $headerSize;
221 * Getter for header array
223 * @return $totalEntries Size of file header
225 public final function getHeader () {
227 return $this->header;
233 * @param $header Array for a file header
236 public final function setHeader (array $header) {
238 $this->header = $header;
242 * Getter for seek position
244 * @return $seekPosition Current seek position (stored here in object)
246 public final function getSeekPosition () {
248 return $this->seekPosition;
252 * Setter for seek position
254 * @param $seekPosition Current seek position (stored here in object)
257 protected final function setSeekPosition ($seekPosition) {
259 $this->seekPosition = $seekPosition;
263 * Updates seekPosition attribute from file to avoid to much access on file.
267 public function updateSeekPosition () {
268 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
270 // Get key (= seek position)
271 $seekPosition = $this->key();
272 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Setting seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
275 $this->setSeekPosition($seekPosition);
277 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
281 * Seeks to beginning of file, updates seek position in this object and
282 * flushes the header.
286 protected function rewindUpdateSeekPosition () {
287 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
289 // flushFileHeader must be callable
290 assert(is_callable(array($this, 'flushFileHeader')));
292 // Seek to beginning of file
295 // And update seek position ...
296 $this->updateSeekPosition();
298 // ... to write it back into the file
299 $this->flushFileHeader();
301 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
305 * Seeks to old position
309 protected function seekToOldPosition () {
310 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
312 // Seek to currently ("old") saved position
313 $this->seek($this->getSeekPosition());
315 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
319 * Checks whether the block separator has been found
321 * @param $str String to look in
322 * @return $isFound Whether the block separator has been found
324 public static function isBlockSeparatorFound ($str) {
326 $isFound = (strpos($str, chr(self::SEPARATOR_ENTRIES)) !== false);
333 * Initializes the back-buffer by setting it to an empty string.
337 private function initBackBuffer () {
338 // Simply call the setter
339 $this->setBackBuffer('');
343 * Setter for backBuffer field
345 * @param $backBuffer Characters to "store" in back-buffer
348 private function setBackBuffer ($backBuffer) {
349 // Cast to string (so no arrays or objects)
350 $backBuffer = (string) $backBuffer;
353 $this->backBuffer = $backBuffer;
357 * Getter for backBuffer field
359 * @return $backBuffer Characters "stored" in back-buffer
361 private function getBackBuffer () {
362 return $this->backBuffer;
366 * Setter for currentBlock field
368 * @param $currentBlock Characters to set a currently loaded block
371 private function setCurrentBlock ($currentBlock) {
372 // Cast to string (so no arrays or objects)
373 $currentBlock = (string) $currentBlock;
376 $this->currentBlock = $currentBlock;
380 * Gets currently read data
382 * @return $current Currently read data
384 public function getCurrentBlock () {
386 return $this->currentBlock;
390 * Initializes this file class
392 * @param $infoInstance An instance of a SplFileInfo class
395 protected function initFile (SplFileInfo $infoInstance) {
396 // Get a file i/o pointer instance
397 $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($infoInstance));
399 // ... and set it here
400 $this->setPointerInstance($pointerInstance);
404 * Writes data at given position
406 * @param $seekPosition Seek position
407 * @param $data Data to be written
408 * @param $flushHeader Whether to flush the header (default: flush)
411 public function writeData ($seekPosition, $data, $flushHeader = true) {
412 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data()=%d - CALLED!', __METHOD__, __LINE__, $seekPosition, strlen($data)));
414 // Write data at given position
415 $this->getPointerInstance()->writeAtPosition($seekPosition, $data);
418 $this->incrementCounter();
420 // Update seek position
421 $this->updateSeekPosition();
424 if ($flushHeader === true) {
426 $this->flushFileHeader();
428 // Seek to old position
429 $this->seekToOldPosition();
432 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
436 * Marks the currently loaded block as empty (with length of the block)
438 * @param $length Length of the block
441 protected function markCurrentBlockAsEmpty ($length) {
442 // Get current seek position
443 $currentPosition = $this->key();
445 // Now add it as gap entry
446 array_push($this->gaps, array(
447 self::GAPS_INDEX_START => ($currentPosition - $length),
448 self::GAPS_INDEX_END => $currentPosition,
453 * Checks whether the file header is initialized
455 * @return $isInitialized Whether the file header is initialized
457 public function isFileHeaderInitialized () {
458 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
460 // Default is not initialized
461 $isInitialized = false;
463 // Is the file initialized?
464 if ($this->isFileInitialized()) {
465 // Some bytes has been written, so rewind to start of it.
466 $rewindStatus = $this->rewind();
467 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] rewindStatus=%s', __METHOD__, __LINE__, $rewindStatus));
469 // Is the rewind() call successfull?
470 if ($rewindStatus != 1) {
471 // Something bad happened
472 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Could not rewind().', __METHOD__, __LINE__));
476 $this->readFileHeader();
478 // The above method does already check the header
479 $isInitialized = true;
483 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
484 return $isInitialized;
488 * Checks whether the assigned file has been initialized
490 * @return $isInitialized Whether the file's size is zero
492 public function isFileInitialized () {
493 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
495 // Get it from iterator which holds the pointer instance. If false is returned
496 $fileSize = $this->size();
497 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] fileSize=%s', __METHOD__, __LINE__, $fileSize));
500 * The returned file size should not be false or NULL as this means
501 * that the pointer class does not work correctly.
503 assert(is_int($fileSize));
505 // Is more than 0 returned?
506 $isInitialized = ($fileSize > 0);
509 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
510 return $isInitialized;
514 * Creates the assigned file
518 public function createFileHeader () {
519 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
521 // The file's header should not be initialized here
522 assert(!$this->isFileHeaderInitialized());
524 // Simple flush file header which will create it.
525 $this->flushFileHeader();
527 // Rewind seek position (to beginning of file) and update/flush file header
528 $this->rewindUpdateSeekPosition();
530 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
534 * Pre-allocates file (if enabled) with some space for later faster write access.
536 * @param $type Type of the file
539 public function preAllocateFile ($type) {
540 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
543 if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($type . '_pre_allocate_enabled') != 'Y') {
545 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Not pre-allocating file.', __METHOD__, __LINE__));
547 // Don't continue here.
552 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Pre-allocating file ...', __METHOD__, __LINE__));
554 // Calculate minimum length for one entry
555 $minLengthEntry = $this->getBlockInstance()->calculateMinimumBlockLength();
556 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] minLengthEntry=%s', __METHOD__, __LINE__, $minLengthEntry));
558 // Calulcate seek position
559 $seekPosition = $minLengthEntry * FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($type . '_pre_allocate_count');
560 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
562 // Now simply write a NUL there. This will pre-allocate the file.
563 $this->writeData($seekPosition, chr(0));
565 // Rewind seek position (to beginning of file) and update/flush file header
566 $this->rewindUpdateSeekPosition();
568 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
572 * Determines seek position
574 * @return $seekPosition Current seek position
576 public function determineSeekPosition () {
577 // Call pointer instance
578 return $this->getPointerInstance()->determineSeekPosition();
582 * Seek to given offset (default) or other possibilities as fseek() gives.
584 * @param $offset Offset to seek to (or used as "base" for other seeks)
585 * @param $whence Added to offset (default: only use offset to seek to)
586 * @return $status Status of file seek: 0 = success, -1 = failed
588 public function seek ($offset, $whence = SEEK_SET) {
589 // Call pointer instance
590 return $this->getPointerInstance()->seek($offset, $whence);
594 * Reads given amount of bytes from file.
596 * @param $bytes Amount of bytes to read
597 * @return $data Data read from file
599 public function read ($bytes = NULL) {
600 // $bytes shall be integer
601 assert(is_int($bytes));
603 // Call pointer instance
604 return $this->getPointerInstance()->read($bytes);
608 * Rewinds to the beginning of the file
610 * @return $status Status of this operation
612 public function rewind () {
613 // Call pointer instance
614 return $this->getPointerInstance()->rewind();
618 * Analyzes entries in index file. This will count all found (and valid)
619 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
620 * only gaps are found, the file is considered as "virgin" (no entries).
624 public function analyzeFile () {
625 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
627 // Make sure the file is initialized
628 assert($this->isFileInitialized());
630 // Init counters and gaps array
631 $this->initCountersGapsArray();
633 // Output message (as this may take some time)
634 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__));
636 // First rewind to the begining
639 // Then try to load all entries
640 while ($this->valid()) {
645 $current = $this->getCurrentBlock();
648 * If the block is empty, maybe the whole file is? This could mean
649 * that the file has been pre-allocated.
651 if (empty($current)) {
652 // Then skip this part
657 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] current()=%d', __METHOD__, __LINE__, strlen($current)));
660 // If the last read block is empty, check gaps
661 if (empty($current)) {
663 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Found a total of %s gaps.', __METHOD__, __LINE__, count($this->gaps)));
665 // Check gaps, if the whole file is empty.
666 if ($this->isFileOnlyGaps()) {
667 // Only gaps, so don't continue here.
672 * The above call has calculated a total size of all gaps. If the
673 * percentage of gaps passes a "soft" limit and last
674 * defragmentation is to far in the past, or if a "hard" limit has
675 * reached, run defragmentation.
677 if ($this->isDefragmentationNeeded()) {
678 // Run "defragmentation"
679 $this->doRunDefragmentation();
682 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
686 * Advances to next "block" of bytes
690 public function next () {
691 // Is there nothing to read?
692 if (!$this->valid()) {
698 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] key()=%d', __FUNCTION__, __LINE__, $this->key()));
700 // Make sure the block instance is set
701 assert($this->getBlockInstance() instanceof CalculatableBlock);
703 // First calculate minimum block length
704 $length = $this->getBlockInstance()->calculateMinimumBlockLength();
705 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] length=%s', __FUNCTION__, __LINE__, $length));
707 // Short be more than zero!
710 // Read possibly back-buffered bytes from previous call of next().
711 $data = $this->getBackBuffer();
714 * Read until a entry/block separator has been found. The next read
715 * "block" may not fit, so this loop will continue until the EOB or EOF
716 * has been reached whatever comes first.
718 while ((!$this->isEndOfFileReached()) && (!self::isBlockSeparatorFound($data))) {
719 // Then read the next possible block
720 $block = $this->read($length);
723 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] block()=%d,length=%s', __FUNCTION__, __LINE__, strlen($block), $length));
726 if (strlen(trim($block)) == 0) {
727 // Mark this block as empty
728 $this->markCurrentBlockAsEmpty(strlen($block));
730 // Skip to next block
734 // At this block then
738 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] data()=%d', __FUNCTION__, __LINE__, strlen($data)));
742 if ($this->isEndOfFileReached()) {
743 // Set whole data as current read block
744 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Calling setCurrentBlock(' . strlen($data) . ') ...');
745 $this->setCurrentBlock($data);
747 // Then abort here silently
748 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('EOF reached.');
753 * Init back-buffer which is the data that has been found beyond the
756 $this->initBackBuffer();
759 $dataArray = explode(chr(self::SEPARATOR_ENTRIES), $data);
761 // This array must contain two elements
762 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('dataArray=' . print_r($dataArray, true));
763 assert(count($dataArray) == 2);
765 // Left part is the actual block, right one the back-buffer data
766 $this->setCurrentBlock($dataArray[0]);
767 $this->setBackBuffer($dataArray[1]);
771 * Checks wether the current entry is valid (not at the end of the file).
772 * This method will return true if an emptied (nulled) entry has been found.
774 * @return $isValid Whether the next entry is valid
776 public function valid () {
777 // Make sure the block instance is set
778 assert($this->getBlockInstance() instanceof Block);
780 // First calculate minimum block length
781 $length = $this->getBlockInstance()->calculateMinimumBlockLength();
783 // Short be more than zero!
786 // Get current seek position
787 $seekPosition = $this->key();
789 // Then try to read it
790 $data = $this->read($length);
792 // If some bytes could be read, all is fine
793 $isValid = ((is_string($data)) && (strlen($data) > 0));
796 $headerSize = $this->getHeaderSize();
798 // Is the seek position at or beyond the header?
799 if ($seekPosition >= $headerSize) {
800 // Seek back to old position
801 $this->seek($seekPosition);
803 // Seek directly behind the header
804 $this->seek($headerSize);
812 * Gets current seek position ("key").
814 * @return $key Current key in iteration
816 public function key () {
817 // Call pointer instance
818 return $this->getPointerInstance()->determineSeekPosition();
822 * Reads the file header
826 public function readFileHeader () {
827 // Make sure the block instance is set
828 assert($this->getBlockInstance() instanceof Block);
830 // Call block instance
831 $this->getBlockInstance()->readFileHeader();
835 * Flushes the file header
839 public function flushFileHeader () {
840 // Make sure the block instance is set
841 assert($this->getBlockInstance() instanceof Block);
843 // Call block instance
844 $this->getBlockInstance()->flushFileHeader();
848 * Searches for next suitable gap the given length of data can fit in
849 * including padding bytes.
851 * @param $length Length of raw data
852 * @return $seekPosition Found next gap's seek position
854 public function searchNextGap ($length) {
855 // If the file is only gaps, no need to seek
856 if ($this->isFileOnlyGaps()) {
857 // The first empty block is the first one right after the header
858 return ($this->getHeaderSize() + 1);
862 $this->partialStub('length=' . $length);