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 <<<<<<< HEAD:framework/main/classes/file_directories/binary/class_BaseBinaryFile.php
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
22 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
23 >>>>>>> Some updates::inc/main/classes/file_directories/binary/class_BaseBinaryFile.php
24 * @license GNU GPL 3.0 or any newer version
25 * @link http://www.ship-simu.org
27 * This program is free software: you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation, either version 3 of the License, or
30 * (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program. If not, see <http://www.gnu.org/licenses/>.
40 abstract class BaseBinaryFile extends BaseAbstractFile {
42 * Separator for header data
44 const SEPARATOR_HEADER_DATA = 0x01;
47 * Separator header->entries
49 const SEPARATOR_HEADER_ENTRIES = 0x02;
52 * Separator group->hash
54 const SEPARATOR_GROUP_HASH = 0x03;
57 * Separator hash->value
59 const SEPARATOR_HASH_VALUE = 0x04;
62 * Separator entry->entry
64 const SEPARATOR_ENTRIES = 0x05;
67 * Separator type->position
69 const SEPARATOR_TYPE_POSITION = 0x06;
74 const LENGTH_COUNT = 20;
79 const LENGTH_POSITION = 20;
84 const LENGTH_GROUP = 10;
87 * Maximum length of entry type
89 const LENGTH_TYPE = 20;
91 //***** Array elements for 'gaps' array *****
96 const GAPS_INDEX_START = 'start';
101 const GAPS_INDEX_END = 'end';
104 * Current seek position
106 private $seekPosition = 0;
111 private $headerSize = 0;
116 private $header = array();
119 * Seek positions for gaps ("fragmentation")
121 private $gaps = array();
124 * Seek positions for damaged entries (e.g. mismatching hash sum, ...)
126 private $damagedEntries = array();
131 private $backBuffer = '';
134 * Currently loaded block (will be returned by current())
136 private $currentBlock = '';
139 * Protected constructor
141 * @param $className Name of the class
144 protected function __construct ($className) {
145 // Call parent constructor
146 parent::__construct($className);
148 // Init counters and gaps array
149 $this->initCountersGapsArray();
153 * Checks whether the abstracted file only contains gaps by counting all
154 * gaps' bytes together and compare it to total length.
156 * @return $isGapsOnly Whether the abstracted file only contains gaps
158 private function isFileOnlyGaps () {
159 // First/last gap found?
160 /* Only for debugging
161 if (isset($this->gaps[0])) {
162 // Output first and last gap
163 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)));
167 // Now count every gap
169 foreach ($this->gaps as $gap) {
170 // Calculate size of found gap: end-start including both
171 $gapsSize += ($gap[self::GAPS_INDEX_END] - $gap[self::GAPS_INDEX_START]);
175 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] gapsSize=%s,this->headerSize=%s', __METHOD__, __LINE__, $gapsSize, $this->getHeaderSize()));
177 // Total gap size + header size must be same as file size
178 $isGapsOnly = (($this->getHeaderSize() + $gapsSize) == $this->getFileSize());
185 * Initializes counter for valid entries, arrays for damaged entries and
186 * an array for gap seek positions. If you call this method on your own,
187 * please re-analyze the file structure. So you are better to call
188 * analyzeFile() instead of this method.
192 public function initCountersGapsArray () {
193 // Init counter and seek position
194 $this->setCounter(0);
195 $this->setSeekPosition(0);
198 $this->gaps = array();
199 $this->damagedEntries = array();
203 * Getter for header size
205 * @return $totalEntries Size of file header
207 public final function getHeaderSize () {
209 return $this->headerSize;
213 * Setter for header size
215 * @param $headerSize Size of file header
218 public final function setHeaderSize ($headerSize) {
220 $this->headerSize = $headerSize;
224 * Getter for header array
226 * @return $totalEntries Size of file header
228 public final function getHeader () {
230 return $this->header;
236 * @param $header Array for a file header
239 public final function setHeader (array $header) {
241 $this->header = $header;
245 * Getter for seek position
247 * @return $seekPosition Current seek position (stored here in object)
249 public final function getSeekPosition () {
251 return $this->seekPosition;
255 * Setter for seek position
257 * @param $seekPosition Current seek position (stored here in object)
260 protected final function setSeekPosition ($seekPosition) {
262 $this->seekPosition = $seekPosition;
266 * Updates seekPosition attribute from file to avoid to much access on file.
270 public function updateSeekPosition () {
271 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
273 // Get key (= seek position)
274 $seekPosition = $this->key();
275 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Setting seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
278 $this->setSeekPosition($seekPosition);
280 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
284 * Seeks to beginning of file, updates seek position in this object and
285 * flushes the header.
289 protected function rewindUpdateSeekPosition () {
290 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
292 // flushFileHeader must be callable
293 assert(is_callable(array($this, 'flushFileHeader')));
295 // Seek to beginning of file
298 // And update seek position ...
299 $this->updateSeekPosition();
301 // ... to write it back into the file
302 $this->flushFileHeader();
304 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
308 * Seeks to old position
312 protected function seekToOldPosition () {
313 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
315 // Seek to currently ("old") saved position
316 $this->seek($this->getSeekPosition());
318 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
322 * Checks whether the block separator has been found
324 * @param $str String to look in
325 * @return $isFound Whether the block separator has been found
327 public static function isBlockSeparatorFound ($str) {
329 $isFound = (strpos($str, chr(self::SEPARATOR_ENTRIES)) !== false);
336 * Initializes the back-buffer by setting it to an empty string.
340 private function initBackBuffer () {
341 // Simply call the setter
342 $this->setBackBuffer('');
346 * Setter for backBuffer field
348 * @param $backBuffer Characters to "store" in back-buffer
351 private function setBackBuffer ($backBuffer) {
352 // Cast to string (so no arrays or objects)
353 $backBuffer = (string) $backBuffer;
356 $this->backBuffer = $backBuffer;
360 * Getter for backBuffer field
362 * @return $backBuffer Characters "stored" in back-buffer
364 private function getBackBuffer () {
365 return $this->backBuffer;
369 * Setter for currentBlock field
371 * @param $currentBlock Characters to set a currently loaded block
374 private function setCurrentBlock ($currentBlock) {
375 // Cast to string (so no arrays or objects)
376 $currentBlock = (string) $currentBlock;
379 $this->currentBlock = $currentBlock;
383 * Gets currently read data
385 * @return $current Currently read data
387 public function getCurrentBlock () {
389 return $this->currentBlock;
393 * Initializes this file class
395 * @param $infoInstance An instance of a SplFileInfo class
398 protected function initFile (SplFileInfo $infoInstance) {
399 // Get a file i/o pointer instance
400 $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($infoInstance));
402 // ... and set it here
403 $this->setPointerInstance($pointerInstance);
407 * Writes data at given position
409 * @param $seekPosition Seek position
410 * @param $data Data to be written
411 * @param $flushHeader Whether to flush the header (default: flush)
414 public function writeData ($seekPosition, $data, $flushHeader = true) {
415 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data()=%d - CALLED!', __METHOD__, __LINE__, $seekPosition, strlen($data)));
417 // Write data at given position
418 $this->getPointerInstance()->writeAtPosition($seekPosition, $data);
421 $this->incrementCounter();
423 // Update seek position
424 $this->updateSeekPosition();
427 if ($flushHeader === true) {
429 $this->flushFileHeader();
431 // Seek to old position
432 $this->seekToOldPosition();
435 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
439 * Marks the currently loaded block as empty (with length of the block)
441 * @param $length Length of the block
444 protected function markCurrentBlockAsEmpty ($length) {
445 // Get current seek position
446 $currentPosition = $this->key();
448 // Now add it as gap entry
449 array_push($this->gaps, array(
450 self::GAPS_INDEX_START => ($currentPosition - $length),
451 self::GAPS_INDEX_END => $currentPosition,
456 * Checks whether the file header is initialized
458 * @return $isInitialized Whether the file header is initialized
460 public function isFileHeaderInitialized () {
461 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
463 // Default is not initialized
464 $isInitialized = false;
466 // Is the file initialized?
467 if ($this->isFileInitialized()) {
468 // Some bytes has been written, so rewind to start of it.
469 $rewindStatus = $this->rewind();
470 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] rewindStatus=%s', __METHOD__, __LINE__, $rewindStatus));
472 // Is the rewind() call successfull?
473 if ($rewindStatus != 1) {
474 // Something bad happened
475 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Could not rewind().', __METHOD__, __LINE__));
479 $this->readFileHeader();
481 // The above method does already check the header
482 $isInitialized = true;
486 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
487 return $isInitialized;
491 * Checks whether the assigned file has been initialized
493 * @return $isInitialized Whether the file's size is zero
495 public function isFileInitialized () {
496 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
498 // Get it from iterator which holds the pointer instance. If false is returned
499 $fileSize = $this->size();
500 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] fileSize=%s', __METHOD__, __LINE__, $fileSize));
503 * The returned file size should not be false or NULL as this means
504 * that the pointer class does not work correctly.
506 assert(is_int($fileSize));
508 // Is more than 0 returned?
509 $isInitialized = ($fileSize > 0);
512 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
513 return $isInitialized;
517 * Creates the assigned file
521 public function createFileHeader () {
522 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
524 // The file's header should not be initialized here
525 assert(!$this->isFileHeaderInitialized());
527 // Simple flush file header which will create it.
528 $this->flushFileHeader();
530 // Rewind seek position (to beginning of file) and update/flush file header
531 $this->rewindUpdateSeekPosition();
533 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
537 * Pre-allocates file (if enabled) with some space for later faster write access.
539 * @param $type Type of the file
542 public function preAllocateFile ($type) {
543 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
546 if ($this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_enabled') != 'Y') {
548 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Not pre-allocating file.', __METHOD__, __LINE__));
550 // Don't continue here.
555 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Pre-allocating file ...', __METHOD__, __LINE__));
557 // Calculate minimum length for one entry
558 $minLengthEntry = $this->getBlockInstance()->calculateMinimumBlockLength();
559 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] minLengthEntry=%s', __METHOD__, __LINE__, $minLengthEntry));
561 // Calulcate seek position
562 $seekPosition = $minLengthEntry * $this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_count');
563 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
565 // Now simply write a NUL there. This will pre-allocate the file.
566 $this->writeData($seekPosition, chr(0));
568 // Rewind seek position (to beginning of file) and update/flush file header
569 $this->rewindUpdateSeekPosition();
571 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
575 * Determines seek position
577 * @return $seekPosition Current seek position
579 public function determineSeekPosition () {
580 // Call pointer instance
581 return $this->getPointerInstance()->determineSeekPosition();
585 * Seek to given offset (default) or other possibilities as fseek() gives.
587 * @param $offset Offset to seek to (or used as "base" for other seeks)
588 * @param $whence Added to offset (default: only use offset to seek to)
589 * @return $status Status of file seek: 0 = success, -1 = failed
591 public function seek ($offset, $whence = SEEK_SET) {
592 // Call pointer instance
593 return $this->getPointerInstance()->seek($offset, $whence);
597 * Reads given amount of bytes from file.
599 * @param $bytes Amount of bytes to read
600 * @return $data Data read from file
602 public function read ($bytes = NULL) {
603 // $bytes shall be integer
604 assert(is_int($bytes));
606 // Call pointer instance
607 return $this->getPointerInstance()->read($bytes);
611 * Rewinds to the beginning of the file
613 * @return $status Status of this operation
615 public function rewind () {
616 // Call pointer instance
617 return $this->getPointerInstance()->rewind();
621 * Analyzes entries in index file. This will count all found (and valid)
622 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
623 * only gaps are found, the file is considered as "virgin" (no entries).
627 public function analyzeFile () {
628 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
630 // Make sure the file is initialized
631 assert($this->isFileInitialized());
633 // Init counters and gaps array
634 $this->initCountersGapsArray();
636 // Output message (as this may take some time)
637 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__));
639 // First rewind to the begining
642 // Then try to load all entries
643 while ($this->valid()) {
648 $current = $this->getCurrentBlock();
651 * If the block is empty, maybe the whole file is? This could mean
652 * that the file has been pre-allocated.
654 if (empty($current)) {
655 // Then skip this part
660 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] current()=%d', __METHOD__, __LINE__, strlen($current)));
663 // If the last read block is empty, check gaps
664 if (empty($current)) {
666 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Found a total of %s gaps.', __METHOD__, __LINE__, count($this->gaps)));
668 // Check gaps, if the whole file is empty.
669 if ($this->isFileOnlyGaps()) {
670 // Only gaps, so don't continue here.
675 * The above call has calculated a total size of all gaps. If the
676 * percentage of gaps passes a "soft" limit and last
677 * defragmentation is to far in the past, or if a "hard" limit has
678 * reached, run defragmentation.
680 if ($this->isDefragmentationNeeded()) {
681 // Run "defragmentation"
682 $this->doRunDefragmentation();
685 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
689 * Advances to next "block" of bytes
693 public function next () {
694 // Is there nothing to read?
695 if (!$this->valid()) {
701 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] key()=%d', __FUNCTION__, __LINE__, $this->key()));
703 // Make sure the block instance is set
704 assert($this->getBlockInstance() instanceof CalculatableBlock);
706 // First calculate minimum block length
707 $length = $this->getBlockInstance()->calculateMinimumBlockLength();
708 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] length=%s', __FUNCTION__, __LINE__, $length));
710 // Short be more than zero!
713 // Read possibly back-buffered bytes from previous call of next().
714 $data = $this->getBackBuffer();
717 * Read until a entry/block separator has been found. The next read
718 * "block" may not fit, so this loop will continue until the EOB or EOF
719 * has been reached whatever comes first.
721 while ((!$this->isEndOfFileReached()) && (!self::isBlockSeparatorFound($data))) {
722 // Then read the next possible block
723 $block = $this->read($length);
726 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] block()=%d,length=%s', __FUNCTION__, __LINE__, strlen($block), $length));
729 if (strlen(trim($block)) == 0) {
730 // Mark this block as empty
731 $this->markCurrentBlockAsEmpty(strlen($block));
733 // Skip to next block
737 // At this block then
741 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] data()=%d', __FUNCTION__, __LINE__, strlen($data)));
745 if ($this->isEndOfFileReached()) {
746 // Set whole data as current read block
747 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Calling setCurrentBlock(' . strlen($data) . ') ...');
748 $this->setCurrentBlock($data);
750 // Then abort here silently
751 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('EOF reached.');
756 * Init back-buffer which is the data that has been found beyond the
759 $this->initBackBuffer();
762 $dataArray = explode(chr(self::SEPARATOR_ENTRIES), $data);
764 // This array must contain two elements
765 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('dataArray=' . print_r($dataArray, true));
766 assert(count($dataArray) == 2);
768 // Left part is the actual block, right one the back-buffer data
769 $this->setCurrentBlock($dataArray[0]);
770 $this->setBackBuffer($dataArray[1]);
774 * Checks wether the current entry is valid (not at the end of the file).
775 * This method will return true if an emptied (nulled) entry has been found.
777 * @return $isValid Whether the next entry is valid
779 public function valid () {
780 // Make sure the block instance is set
781 assert($this->getBlockInstance() instanceof Block);
783 // First calculate minimum block length
784 $length = $this->getBlockInstance()->calculateMinimumBlockLength();
786 // Short be more than zero!
789 // Get current seek position
790 $seekPosition = $this->key();
792 // Then try to read it
793 $data = $this->read($length);
795 // If some bytes could be read, all is fine
796 $isValid = ((is_string($data)) && (strlen($data) > 0));
799 $headerSize = $this->getHeaderSize();
801 // Is the seek position at or beyond the header?
802 if ($seekPosition >= $headerSize) {
803 // Seek back to old position
804 $this->seek($seekPosition);
806 // Seek directly behind the header
807 $this->seek($headerSize);
815 * Gets current seek position ("key").
817 * @return $key Current key in iteration
819 public function key () {
820 // Call pointer instance
821 return $this->getPointerInstance()->determineSeekPosition();
825 * Reads the file header
829 public function readFileHeader () {
830 // Make sure the block instance is set
831 assert($this->getBlockInstance() instanceof Block);
833 // Call block instance
834 $this->getBlockInstance()->readFileHeader();
838 * Flushes the file header
842 public function flushFileHeader () {
843 // Make sure the block instance is set
844 assert($this->getBlockInstance() instanceof Block);
846 // Call block instance
847 $this->getBlockInstance()->flushFileHeader();
851 * Searches for next suitable gap the given length of data can fit in
852 * including padding bytes.
854 * @param $length Length of raw data
855 * @return $seekPosition Found next gap's seek position
857 public function searchNextGap ($length) {
858 // If the file is only gaps, no need to seek
859 if ($this->isFileOnlyGaps()) {
860 // The first empty block is the first one right after the header
861 return ($this->getHeaderSize() + 1);
865 $this->partialStub('length=' . $length);