Skip to content

Commit

Permalink
Ensure we have a seekable stream when we need one.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrubinsk committed Feb 3, 2017
1 parent 1929f39 commit 503f48f
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 11 deletions.
17 changes: 17 additions & 0 deletions framework/Vfs/lib/Horde/Vfs/Base.php
Expand Up @@ -793,6 +793,23 @@ protected function _getDataSize($data)
return strlen($data);
}

protected function _ensureSeekable($stream)
{
if (!is_resource($stream)) {
return $stream;
}

$meta = stream_get_meta_data($stream);
if (!$meta['seekable']) {
$temp = new Horde_Stream_Temp();
$temp->add($stream, true);
return $temp->stream;
}

rewind($stream);
return $stream;
}

/**
* Checks the quota when preparing to delete data.
*
Expand Down
7 changes: 3 additions & 4 deletions framework/Vfs/lib/Horde/Vfs/File.php
Expand Up @@ -309,6 +309,9 @@ public function writeData($path, $name, $data, $autocreate = false)
}
}

// Ensure we have either a string or seekable stream.
$data = $this->_ensureSeekable($data);

// Treat an attempt to write an empty file as a touch() call,
// since otherwise the file will not be created at all.
if (!$this->_getDataSize($data)) {
Expand All @@ -321,10 +324,6 @@ public function writeData($path, $name, $data, $autocreate = false)
$this->_checkQuotaWrite('string', $data, $path, $name);

// Otherwise we go ahead and try to write out the file.
if (is_resource($data)) {
rewind($data);
}

if (!@file_put_contents($this->_getNativePath($path, $name), $data)) {
$this->_throwException(new Horde_Vfs_Exception(sprintf('Unable to write data to VFS file %s/%s.', $path, $name)));
}
Expand Down
4 changes: 1 addition & 3 deletions framework/Vfs/lib/Horde/Vfs/Ftp.php
Expand Up @@ -251,9 +251,7 @@ public function write($path, $name, $tmpFile, $autocreate = false)
public function writeData($path, $name, $data, $autocreate = false)
{
$tmpFile = Horde_Util::getTempFile('vfs');
if (is_resource($data)) {
rewind($data);
}
$data = $this->_ensureSeekable($data);
file_put_contents($tmpFile, $data);
try {
$this->write($path, $name, $tmpFile, $autocreate);
Expand Down
2 changes: 1 addition & 1 deletion framework/Vfs/lib/Horde/Vfs/Mongo.php
Expand Up @@ -203,8 +203,8 @@ protected function _write($type, $path, $name, $data, $autocreate)
// MONGO currently has no ability to stream data TO the
// server. I.e., there is no opposite version of
// MongoGridFSFile::getResource().
$data = $this->_ensureSeekable($data);
if (is_resource($data)) {
rewind($data);
$data = stream_get_contents($data);
}
$this->_files->storeBytes($data, $mdata);
Expand Down
1 change: 1 addition & 0 deletions framework/Vfs/lib/Horde/Vfs/Sql.php
Expand Up @@ -226,6 +226,7 @@ public function write($path, $name, $tmpFile, $autocreate = false)
*/
public function writeData($path, $name, $data, $autocreate = false)
{
$data = $this->_ensureSeekable($data);
$this->_checkQuotaWrite('string', $data, $path, $name);

$path = $this->_convertPath($path);
Expand Down
1 change: 1 addition & 0 deletions framework/Vfs/lib/Horde/Vfs/SqlFile.php
Expand Up @@ -75,6 +75,7 @@ public function write($path, $name, $tmpFile, $autocreate = false)
*/
public function writeData($path, $name, $data, $autocreate = false)
{
$data = $this->_ensureSeekable($data);
$this->_checkQuotaWrite('string', $data, $path, $name);

$fp = @fopen($this->_getNativePath($path, $name), 'w');
Expand Down
4 changes: 1 addition & 3 deletions framework/Vfs/lib/Horde/Vfs/Ssh2.php
Expand Up @@ -217,9 +217,7 @@ public function write($path, $name, $tmpFile, $autocreate = false)
public function writeData($path, $name, $data, $autocreate = false)
{
$tmpFile = Horde_Util::getTempFile('vfs');
if (is_resource($data)) {
rewind($data);
}
$data = $this->_ensureSeekable($data);
file_put_contents($tmpFile, $data);
clearstatcache();
$this->write($path, $name, $tmpFile, $autocreate);
Expand Down

0 comments on commit 503f48f

Please sign in to comment.