Ejemplo n.º 1
0
 public function testFilePutContentsWriteError()
 {
     // Force error by simulating full disk
     vfsStream::setQuota(3);
     $filename = $this->_root->url() . '/test.txt';
     try {
         FileObject::filePutContents($filename, 'content');
         $this->fail('Expected exception has not been thrown');
     } catch (\RuntimeException $e) {
         $this->assertEquals("Error writing to file {$filename}", $e->getMessage());
         // A truncated file should remain on disk
         $this->assertFileExists($filename);
         $this->assertEquals('con', file_get_contents($filename));
     }
 }
Ejemplo n.º 2
0
 /**
  * Write Package content
  *
  * @param array $data Package data
  * @param string $file Source file
  * @param bool $deleteSource Delete source file
  * @return integer Number of fragments
  */
 public function writeContent($data, $file, $deleteSource)
 {
     $id = $data['Id'];
     $baseName = $this->getPath($id) . "/{$id}-";
     $fileSize = @$data['Size'];
     $maxFragmentSize = @$data['MaxFragmentSize'] * 1024;
     // Kilobytes => Bytes
     if (!$data['FileLocation']) {
         // No file
         $numFragments = 0;
     } elseif ($fileSize == 0 or $maxFragmentSize == 0 or $fileSize <= $maxFragmentSize) {
         // Don't split, just copy/move/rename the file
         if ($deleteSource) {
             \Library\FileObject::rename($file, $baseName . '1');
         } else {
             \Library\FileObject::copy($file, $baseName . '1');
         }
         $numFragments = 1;
     } else {
         // Split file into fragments of nearly identical size no bigger than $maxFragmentSize.
         $fragmentSize = ceil($fileSize / ceil($fileSize / $maxFragmentSize));
         // Determine number of fragments by files actually written
         $numFragments = 0;
         $input = new \Library\FileObject($file, 'rb');
         while ($fragment = $input->fread($fragmentSize)) {
             $numFragments++;
             \Library\FileObject::filePutContents($baseName . $numFragments, $fragment);
         }
         unset($input);
         // Close file before eventually trying to delete it
         if ($deleteSource) {
             \Library\FileObject::unlink($file);
         }
     }
     return $numFragments;
 }
Ejemplo n.º 3
0
 /**
  * Write XML content to file
  *
  * This is a reimplementation of \DomDocument::save() with improved error
  * handling. An exception is thrown on error, and no file remains on disk.
  *
  * @param string $filename
  * @param integer $options
  * @return integer number of bytes written
  * @throws \RuntimeException if a write error occurs
  */
 public function save($filename, $options = 0)
 {
     $xml = $this->saveXml(null, $options);
     try {
         \Library\FileObject::filePutContents($filename, $xml);
     } catch (\Exception $e) {
         if (is_file($filename)) {
             unlink($filename);
         }
         throw $e;
     }
     return strlen($xml);
 }