3 namespace CoreFramework\Filesystem\File;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Filesystem\Block;
8 use CoreFramework\Filesystem\Block\CalculatableBlock;
9 use CoreFramework\Filesystem\File\BaseAbstractFile;
12 * A general binary file class
14 * @author Roland Haeder <webmaster@ship-simu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.ship-simu.org
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 class BaseBinaryFile extends BaseAbstractFile {
35 * Separator for header data
37 const SEPARATOR_HEADER_DATA = 0x01;
40 * Separator header->entries
42 const SEPARATOR_HEADER_ENTRIES = 0x02;
45 * Separator group->hash
47 const SEPARATOR_GROUP_HASH = 0x03;
50 * Separator hash->value
52 const SEPARATOR_HASH_VALUE = 0x04;
55 * Separator entry->entry
57 const SEPARATOR_ENTRIES = 0x05;
60 * Separator type->position
62 const SEPARATOR_TYPE_POSITION = 0x06;
67 const LENGTH_COUNT = 20;
72 const LENGTH_POSITION = 20;
77 const LENGTH_GROUP = 10;
80 * Maximum length of entry type
82 const LENGTH_TYPE = 20;
84 //***** Array elements for 'gaps' array *****
89 const GAPS_INDEX_START = 'start';
94 const GAPS_INDEX_END = 'end';
97 * Current seek position
99 private $seekPosition = 0;
104 private $headerSize = 0;
109 private $header = array();
112 * Seek positions for gaps ("fragmentation")
114 private $gaps = array();
117 * Seek positions for damaged entries (e.g. mismatching hash sum, ...)
119 private $damagedEntries = array();
124 private $backBuffer = '';
127 * Currently loaded block (will be returned by current())
129 private $currentBlock = '';
132 * Protected constructor
134 * @param $className Name of the class
137 protected function __construct ($className) {
138 // Call parent constructor
139 parent::__construct($className);
141 // Init counters and gaps array
142 $this->initCountersGapsArray();
146 * Checks whether the abstracted file only contains gaps by counting all
147 * gaps' bytes together and compare it to total length.
149 * @return $isGapsOnly Whether the abstracted file only contains gaps
151 private function isFileOnlyGaps () {
152 // First/last gap found?
153 /* Only for debugging
154 if (isset($this->gaps[0])) {
155 // Output first and last gap
156 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)));
160 // Now count every gap
162 foreach ($this->gaps as $gap) {
163 // Calculate size of found gap: end-start including both
164 $gapsSize += ($gap[self::GAPS_INDEX_END] - $gap[self::GAPS_INDEX_START]);
168 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] gapsSize=%s,this->headerSize=%s', __METHOD__, __LINE__, $gapsSize, $this->getHeaderSize()));
170 // Total gap size + header size must be same as file size
171 $isGapsOnly = (($this->getHeaderSize() + $gapsSize) == $this->getFileSize());
178 * Initializes counter for valid entries, arrays for damaged entries and
179 * an array for gap seek positions. If you call this method on your own,
180 * please re-analyze the file structure. So you are better to call
181 * analyzeFile() instead of this method.
185 public function initCountersGapsArray () {
186 // Init counter and seek position
187 $this->setCounter(0);
188 $this->setSeekPosition(0);
191 $this->gaps = array();
192 $this->damagedEntries = array();
196 * Getter for header size
198 * @return $totalEntries Size of file header
200 public final function getHeaderSize () {
202 return $this->headerSize;
206 * Setter for header size
208 * @param $headerSize Size of file header
211 public final function setHeaderSize ($headerSize) {
213 $this->headerSize = $headerSize;
217 * Getter for header array
219 * @return $totalEntries Size of file header
221 public final function getHeader () {
223 return $this->header;
229 * @param $header Array for a file header
232 public final function setHeader (array $header) {
234 $this->header = $header;
238 * Getter for seek position
240 * @return $seekPosition Current seek position (stored here in object)
242 public final function getSeekPosition () {
244 return $this->seekPosition;
248 * Setter for seek position
250 * @param $seekPosition Current seek position (stored here in object)
253 protected final function setSeekPosition ($seekPosition) {
255 $this->seekPosition = $seekPosition;
259 * Updates seekPosition attribute from file to avoid to much access on file.
263 public function updateSeekPosition () {
264 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
266 // Get key (= seek position)
267 $seekPosition = $this->key();
268 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Setting seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
271 $this->setSeekPosition($seekPosition);
273 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
277 * Seeks to beginning of file, updates seek position in this object and
278 * flushes the header.
282 protected function rewindUpdateSeekPosition () {
283 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
285 // flushFileHeader must be callable
286 assert(is_callable(array($this, 'flushFileHeader')));
288 // Seek to beginning of file
291 // And update seek position ...
292 $this->updateSeekPosition();
294 // ... to write it back into the file
295 $this->flushFileHeader();
297 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
301 * Seeks to old position
305 protected function seekToOldPosition () {
306 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
308 // Seek to currently ("old") saved position
309 $this->seek($this->getSeekPosition());
311 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
315 * Checks whether the block separator has been found
317 * @param $str String to look in
318 * @return $isFound Whether the block separator has been found
320 public static function isBlockSeparatorFound ($str) {
322 $isFound = (strpos($str, chr(self::SEPARATOR_ENTRIES)) !== false);
329 * Initializes the back-buffer by setting it to an empty string.
333 private function initBackBuffer () {
334 // Simply call the setter
335 $this->setBackBuffer('');
339 * Setter for backBuffer field
341 * @param $backBuffer Characters to "store" in back-buffer
344 private function setBackBuffer ($backBuffer) {
345 // Cast to string (so no arrays or objects)
346 $backBuffer = (string) $backBuffer;
349 $this->backBuffer = $backBuffer;
353 * Getter for backBuffer field
355 * @return $backBuffer Characters "stored" in back-buffer
357 private function getBackBuffer () {
358 return $this->backBuffer;
362 * Setter for currentBlock field
364 * @param $currentBlock Characters to set a currently loaded block
367 private function setCurrentBlock ($currentBlock) {
368 // Cast to string (so no arrays or objects)
369 $currentBlock = (string) $currentBlock;
372 $this->currentBlock = $currentBlock;
376 * Gets currently read data
378 * @return $current Currently read data
380 public function getCurrentBlock () {
382 return $this->currentBlock;
386 * Initializes this file class
388 * @param $fileName Name of this abstract file
391 protected function initFile ($fileName) {
392 // Get a file i/o pointer instance
393 $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName));
395 // ... and set it here
396 $this->setPointerInstance($pointerInstance);
400 * Writes data at given position
402 * @param $seekPosition Seek position
403 * @param $data Data to be written
404 * @param $flushHeader Whether to flush the header (default: flush)
407 public function writeData ($seekPosition, $data, $flushHeader = true) {
408 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data()=%d - CALLED!', __METHOD__, __LINE__, $seekPosition, strlen($data)));
410 // Write data at given position
411 $this->getPointerInstance()->writeAtPosition($seekPosition, $data);
414 $this->incrementCounter();
416 // Update seek position
417 $this->updateSeekPosition();
420 if ($flushHeader === true) {
422 $this->flushFileHeader();
424 // Seek to old position
425 $this->seekToOldPosition();
428 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
432 * Marks the currently loaded block as empty (with length of the block)
434 * @param $length Length of the block
437 protected function markCurrentBlockAsEmpty ($length) {
438 // Get current seek position
439 $currentPosition = $this->key();
441 // Now add it as gap entry
442 array_push($this->gaps, array(
443 self::GAPS_INDEX_START => ($currentPosition - $length),
444 self::GAPS_INDEX_END => $currentPosition,
449 * Checks whether the file header is initialized
451 * @return $isInitialized Whether the file header is initialized
453 public function isFileHeaderInitialized () {
454 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
456 // Default is not initialized
457 $isInitialized = false;
459 // Is the file initialized?
460 if ($this->isFileInitialized()) {
461 // Some bytes has been written, so rewind to start of it.
462 $rewindStatus = $this->rewind();
463 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] rewindStatus=%s', __METHOD__, __LINE__, $rewindStatus));
465 // Is the rewind() call successfull?
466 if ($rewindStatus != 1) {
467 // Something bad happened
468 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Could not rewind().', __METHOD__, __LINE__));
472 $this->readFileHeader();
474 // The above method does already check the header
475 $isInitialized = true;
479 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
480 return $isInitialized;
484 * Checks whether the assigned file has been initialized
486 * @return $isInitialized Whether the file's size is zero
488 public function isFileInitialized () {
489 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
491 // Get it from iterator which holds the pointer instance. If false is returned
492 $fileSize = $this->size();
493 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] fileSize=%s', __METHOD__, __LINE__, $fileSize));
496 * The returned file size should not be false or NULL as this means
497 * that the pointer class does not work correctly.
499 assert(is_int($fileSize));
501 // Is more than 0 returned?
502 $isInitialized = ($fileSize > 0);
505 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
506 return $isInitialized;
510 * Creates the assigned file
514 public function createFileHeader () {
515 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
517 // The file's header should not be initialized here
518 assert(!$this->isFileHeaderInitialized());
520 // Simple flush file header which will create it.
521 $this->flushFileHeader();
523 // Rewind seek position (to beginning of file) and update/flush file header
524 $this->rewindUpdateSeekPosition();
526 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
530 * Pre-allocates file (if enabled) with some space for later faster write access.
532 * @param $type Type of the file
535 public function preAllocateFile ($type) {
536 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
539 if ($this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_enabled') != 'Y') {
541 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Not pre-allocating file.', __METHOD__, __LINE__));
543 // Don't continue here.
548 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Pre-allocating file ...', __METHOD__, __LINE__));
550 // Calculate minimum length for one entry
551 $minLengthEntry = $this->getBlockInstance()->calculateMinimumBlockLength();
552 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] minLengthEntry=%s', __METHOD__, __LINE__, $minLengthEntry));
554 // Calulcate seek position
555 $seekPosition = $minLengthEntry * $this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_count');
556 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
558 // Now simply write a NUL there. This will pre-allocate the file.
559 $this->writeData($seekPosition, chr(0));
561 // Rewind seek position (to beginning of file) and update/flush file header
562 $this->rewindUpdateSeekPosition();
564 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
568 * Determines seek position
570 * @return $seekPosition Current seek position
572 public function determineSeekPosition () {
573 // Call pointer instance
574 return $this->getPointerInstance()->determineSeekPosition();
578 * Seek to given offset (default) or other possibilities as fseek() gives.
580 * @param $offset Offset to seek to (or used as "base" for other seeks)
581 * @param $whence Added to offset (default: only use offset to seek to)
582 * @return $status Status of file seek: 0 = success, -1 = failed
584 public function seek ($offset, $whence = SEEK_SET) {
585 // Call pointer instance
586 return $this->getPointerInstance()->seek($offset, $whence);
590 * Reads given amount of bytes from file.
592 * @param $bytes Amount of bytes to read
593 * @return $data Data read from file
595 public function read ($bytes = NULL) {
596 // $bytes shall be integer
597 assert(is_int($bytes));
599 // Call pointer instance
600 return $this->getPointerInstance()->read($bytes);
604 * Rewinds to the beginning of the file
606 * @return $status Status of this operation
608 public function rewind () {
609 // Call pointer instance
610 return $this->getPointerInstance()->rewind();
614 * Analyzes entries in index file. This will count all found (and valid)
615 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
616 * only gaps are found, the file is considered as "virgin" (no entries).
620 public function analyzeFile () {
621 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
623 // Make sure the file is initialized
624 assert($this->isFileInitialized());
626 // Init counters and gaps array
627 $this->initCountersGapsArray();
629 // Output message (as this may take some time)
630 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__));
632 // First rewind to the begining
635 // Then try to load all entries
636 while ($this->valid()) {
641 $current = $this->getCurrentBlock();
644 * If the block is empty, maybe the whole file is? This could mean
645 * that the file has been pre-allocated.
647 if (empty($current)) {
648 // Then skip this part
653 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] current()=%d', __METHOD__, __LINE__, strlen($current)));
656 // If the last read block is empty, check gaps
657 if (empty($current)) {
659 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Found a total of %s gaps.', __METHOD__, __LINE__, count($this->gaps)));
661 // Check gaps, if the whole file is empty.
662 if ($this->isFileOnlyGaps()) {
663 // Only gaps, so don't continue here.
668 * The above call has calculated a total size of all gaps. If the
669 * percentage of gaps passes a "soft" limit and last
670 * defragmentation is to far in the past, or if a "hard" limit has
671 * reached, run defragmentation.
673 if ($this->isDefragmentationNeeded()) {
674 // Run "defragmentation"
675 $this->doRunDefragmentation();
678 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
682 * Advances to next "block" of bytes
686 public function next () {
687 // Is there nothing to read?
688 if (!$this->valid()) {
694 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] key()=%d', __FUNCTION__, __LINE__, $this->key()));
696 // Make sure the block instance is set
697 assert($this->getBlockInstance() instanceof CalculatableBlock);
699 // First calculate minimum block length
700 $length = $this->getBlockInstance()->calculateMinimumBlockLength();
701 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] length=%s', __FUNCTION__, __LINE__, $length));
703 // Short be more than zero!
706 // Read possibly back-buffered bytes from previous call of next().
707 $data = $this->getBackBuffer();
710 * Read until a entry/block separator has been found. The next read
711 * "block" may not fit, so this loop will continue until the EOB or EOF
712 * has been reached whatever comes first.
714 while ((!$this->isEndOfFileReached()) && (!self::isBlockSeparatorFound($data))) {
715 // Then read the next possible block
716 $block = $this->read($length);
719 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] block()=%d,length=%s', __FUNCTION__, __LINE__, strlen($block), $length));
722 if (strlen(trim($block)) == 0) {
723 // Mark this block as empty
724 $this->markCurrentBlockAsEmpty(strlen($block));
726 // Skip to next block
730 // At this block then
734 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] data()=%d', __FUNCTION__, __LINE__, strlen($data)));
738 if ($this->isEndOfFileReached()) {
739 // Set whole data as current read block
740 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Calling setCurrentBlock(' . strlen($data) . ') ...');
741 $this->setCurrentBlock($data);
743 // Then abort here silently
744 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('EOF reached.');
749 * Init back-buffer which is the data that has been found beyond the
752 $this->initBackBuffer();
755 $dataArray = explode(chr(self::SEPARATOR_ENTRIES), $data);
757 // This array must contain two elements
758 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('dataArray=' . print_r($dataArray, true));
759 assert(count($dataArray) == 2);
761 // Left part is the actual block, right one the back-buffer data
762 $this->setCurrentBlock($dataArray[0]);
763 $this->setBackBuffer($dataArray[1]);
767 * Checks wether the current entry is valid (not at the end of the file).
768 * This method will return true if an emptied (nulled) entry has been found.
770 * @return $isValid Whether the next entry is valid
772 public function valid () {
773 // Make sure the block instance is set
774 assert($this->getBlockInstance() instanceof Block);
776 // First calculate minimum block length
777 $length = $this->getBlockInstance()->calculateMinimumBlockLength();
779 // Short be more than zero!
782 // Get current seek position
783 $seekPosition = $this->key();
785 // Then try to read it
786 $data = $this->read($length);
788 // If some bytes could be read, all is fine
789 $isValid = ((is_string($data)) && (strlen($data) > 0));
792 $headerSize = $this->getHeaderSize();
794 // Is the seek position at or beyond the header?
795 if ($seekPosition >= $headerSize) {
796 // Seek back to old position
797 $this->seek($seekPosition);
799 // Seek directly behind the header
800 $this->seek($headerSize);
808 * Gets current seek position ("key").
810 * @return $key Current key in iteration
812 public function key () {
813 // Call pointer instance
814 return $this->getPointerInstance()->determineSeekPosition();
818 * Reads the file header
822 public function readFileHeader () {
823 // Make sure the block instance is set
824 assert($this->getBlockInstance() instanceof Block);
826 // Call block instance
827 $this->getBlockInstance()->readFileHeader();
831 * Flushes the file header
835 public function flushFileHeader () {
836 // Make sure the block instance is set
837 assert($this->getBlockInstance() instanceof Block);
839 // Call block instance
840 $this->getBlockInstance()->flushFileHeader();
844 * Searches for next suitable gap the given length of data can fit in
845 * including padding bytes.
847 * @param $length Length of raw data
848 * @return $seekPosition Found next gap's seek position
850 public function searchNextGap ($length) {
851 // If the file is only gaps, no need to seek
852 if ($this->isFileOnlyGaps()) {
853 // The first empty block is the first one right after the header
854 return ($this->getHeaderSize() + 1);
858 $this->partialStub('length=' . $length);