From 49ccb744978752a799faeae1ab4c54c803b40a25 Mon Sep 17 00:00:00 2001 From: cdujeu Date: Tue, 14 Jul 2015 12:38:11 +0200 Subject: [PATCH] fseek: $whence parameter is not properly passed to underlying stream. S3 driver: override appendUploadedData() function as the streamWrapper does not support "a+" mode. --- .../access.fs/class.fsAccessWrapper.php | 2 +- .../access.s3/class.s3AccessDriver.php | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/core/src/plugins/access.fs/class.fsAccessWrapper.php b/core/src/plugins/access.fs/class.fsAccessWrapper.php index ded7114f2a..cc06f5f2a2 100644 --- a/core/src/plugins/access.fs/class.fsAccessWrapper.php +++ b/core/src/plugins/access.fs/class.fsAccessWrapper.php @@ -290,7 +290,7 @@ public function stream_open($path, $mode, $options, &$context) public function stream_seek($offset , $whence = SEEK_SET) { - fseek($this->fp, $offset, SEEK_SET); + fseek($this->fp, $offset, $whence); } public function stream_tell() diff --git a/core/src/plugins/access.s3/class.s3AccessDriver.php b/core/src/plugins/access.s3/class.s3AccessDriver.php index 4f72c76a6f..2189d5f0f7 100755 --- a/core/src/plugins/access.s3/class.s3AccessDriver.php +++ b/core/src/plugins/access.s3/class.s3AccessDriver.php @@ -145,6 +145,41 @@ protected function parseSpecificContributions(&$contribNode) $this->disableArchiveBrowsingContributions($contribNode); } + /** + * We have to overwrite original FS function as S3 wrapper does not support "a+" open mode. + * + * @param String $folder Folder destination + * @param String $source Maybe updated by the function + * @param String $target Existing part to append data + * @return bool If the target file already existed or not. + * @throws Exception + */ + protected function appendUploadedData($folder, $source, $target){ + + $already_existed = false; + if($source == $target){ + throw new Exception("Something nasty happened: trying to copy $source into itself, it will create a loop!"); + } + // S3 does not really support append. Let's grab the remote target first. + if (file_exists($folder ."/" . $target)) { + $already_existed = true; + $this->logDebug("Should copy stream from $source to $target - folder is ($folder)"); + $partO = fopen($folder."/".$source, "r"); + $appendF = fopen($folder ."/". $target, 'a'); + while (!feof($partO)) { + $buf = fread($partO, 1024); + fwrite($appendF, $buf); + } + fclose($partO); + fclose($appendF); + $this->logDebug("Done, closing streams!"); + } + @unlink($folder."/".$source); + return $already_existed; + + } + + public function isWriteable($dir, $type="dir") { return true;