Only seek back to old position if the header has been flushed.
[core.git] / inc / classes / main / class_BaseFrameworkSystem.php
index ddd03a4007a70fac05e025bc03ae5a9aeed8ba93..033718d949958c02f9eb54fee8e5cfe9d24cb094 100644 (file)
@@ -3044,23 +3044,47 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         */
        protected function createFileHeader () {
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
+
+               // The method flushFileHeader() must be callable
+               assert(is_callable(array($this, 'flushFileHeader')));
+
                // The file's header should not be initialized here
                assert(!$this->isFileHeaderInitialized());
 
                // Simple flush file header which will create it.
                $this->flushFileHeader();
 
+               // Seek to beginning of file
+               $this->getIteratorInstance()->rewind();
+
+               // And update seek position ...
+               $this->updateSeekPosition();
+
+               // ... to write it back into the file
+               $this->flushFileHeader();
+
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__));
        }
 
+       /**
+        * Seeks to old position
+        *
+        * @return      void
+        */
+       protected function seekToOldPosition () {
+               // Seek to currently ("old") saved position
+               $this->getIteratorInstance()->seek($this->getSeekPosition());
+       }
+
        /**
         * Writes data at given position
         *
         * @param       $seekPosition   Seek position
         * @param       $data                   Data to be written
+        * @param       $flushHeader    Whether to flush the header (default: flush)
         * @return      void
         */
-       protected function writeData ($seekPosition, $data) {
+       protected function writeData ($seekPosition, $data, $flushHeader = TRUE) {
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data()=%s - CALLED!', __METHOD__, __LINE__, $seekPosition, strlen($data)));
 
                // Write data at given position
@@ -3069,11 +3093,15 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                // Update seek position
                $this->updateSeekPosition();
 
-               // Flush header
-               $this->flushHeader();
+               // Flush the header?
+               if ($flushHeader === TRUE) {
+                       // Flush header
+                       $this->flushFileHeader();
+
+                       // Seek to old position
+                       $this->seekToOldPosition();
+               } // END - if
 
-               // Seek to old position
-               $this->seekToOldPosition();
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
        }