Move streamwrappers to seperate files and put them in a namespace

This commit is contained in:
Robin Appelman 2013-01-28 15:34:15 +01:00
parent bca5ce724e
commit c9c919da57
14 changed files with 242 additions and 204 deletions

View File

@ -104,7 +104,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
foreach ($response->body->CommonPrefixes as $object) { foreach ($response->body->CommonPrefixes as $object) {
$files[] = basename($object->Prefix); $files[] = basename($object->Prefix);
} }
\OC_FakeDirStream::$dirs['amazons3'.$path] = $files; \OC\Files\Stream\Dir::register('amazons3' . $path, $files);
return opendir('fakedir://amazons3' . $path); return opendir('fakedir://amazons3' . $path);
} }
return false; return false;
@ -194,7 +194,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
$ext = ''; $ext = '';
} }
$tmpFile = \OC_Helper::tmpFile($ext); $tmpFile = \OC_Helper::tmpFile($ext);
\OC_CloseStreamWrapper::$callBacks[$tmpFile] = array($this, 'writeBack'); \OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
if ($this->file_exists($path)) { if ($this->file_exists($path)) {
$source = $this->fopen($path, 'r'); $source = $this->fopen($path, 'r');
file_put_contents($tmpFile, $source); file_put_contents($tmpFile, $source);

View File

@ -114,7 +114,7 @@ class Dropbox extends \OC\Files\Storage\Common {
foreach ($contents as $file) { foreach ($contents as $file) {
$files[] = basename($file['path']); $files[] = basename($file['path']);
} }
\OC_FakeDirStream::$dirs['dropbox'.$path] = $files; \OC\Files\Stream\Dir::register('dropbox'.$path, $files);
return opendir('fakedir://dropbox'.$path); return opendir('fakedir://dropbox'.$path);
} }
return false; return false;
@ -232,7 +232,7 @@ class Dropbox extends \OC\Files\Storage\Common {
$ext = ''; $ext = '';
} }
$tmpFile = \OC_Helper::tmpFile($ext); $tmpFile = \OC_Helper::tmpFile($ext);
\OC_CloseStreamWrapper::$callBacks[$tmpFile] = array($this, 'writeBack'); \OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
if ($this->file_exists($path)) { if ($this->file_exists($path)) {
$source = $this->fopen($path, 'r'); $source = $this->fopen($path, 'r');
file_put_contents($tmpFile, $source); file_put_contents($tmpFile, $source);

View File

@ -84,7 +84,7 @@ class FTP extends \OC\Files\Storage\StreamWrapper{
$ext=''; $ext='';
} }
$tmpFile=OCP\Files::tmpFile($ext); $tmpFile=OCP\Files::tmpFile($ext);
OC_CloseStreamWrapper::$callBacks[$tmpFile]=array($this, 'writeBack'); \OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
if ($this->file_exists($path)) { if ($this->file_exists($path)) {
$this->getFile($path, $tmpFile); $this->getFile($path, $tmpFile);
} }

View File

@ -273,7 +273,7 @@ class Google extends \OC\Files\Storage\Common {
$this->entries[$name] = $entry; $this->entries[$name] = $entry;
} }
} }
\OC_FakeDirStream::$dirs['google'.$path] = $files; \OC\Files\Stream\Dir::register('google'.$path, $files);
return opendir('fakedir://google'.$path); return opendir('fakedir://google'.$path);
} }
@ -450,7 +450,7 @@ class Google extends \OC\Files\Storage\Common {
$ext = ''; $ext = '';
} }
$tmpFile = \OC_Helper::tmpFile($ext); $tmpFile = \OC_Helper::tmpFile($ext);
\OC_CloseStreamWrapper::$callBacks[$tmpFile] = array($this, 'writeBack'); \OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
if ($this->file_exists($path)) { if ($this->file_exists($path)) {
$source = $this->fopen($path, 'r'); $source = $this->fopen($path, 'r');
file_put_contents($tmpFile, $source); file_put_contents($tmpFile, $source);

View File

@ -371,7 +371,7 @@ class SWIFT extends \OC\Files\Storage\Common{
$subContainers=$this->getSubContainers($container); $subContainers=$this->getSubContainers($container);
$files=array_merge($files, $subContainers); $files=array_merge($files, $subContainers);
$id=$this->getContainerName($path); $id=$this->getContainerName($path);
\OC_FakeDirStream::$dirs[$id]=$files; \OC\Files\Stream\Dir::register($id, $files);
return opendir('fakedir://'.$id); return opendir('fakedir://'.$id);
} }
@ -465,7 +465,7 @@ class SWIFT extends \OC\Files\Storage\Common{
case 'c': case 'c':
case 'c+': case 'c+':
$tmpFile=$this->getTmpFile($path); $tmpFile=$this->getTmpFile($path);
OC_CloseStreamWrapper::$callBacks[$tmpFile]=array($this, 'writeBack'); \OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
self::$tempFiles[$tmpFile]=$path; self::$tempFiles[$tmpFile]=$path;
return fopen('close://'.$tmpFile, $mode); return fopen('close://'.$tmpFile, $mode);
} }

View File

@ -104,13 +104,15 @@ class DAV extends \OC\Files\Storage\Common{
try { try {
$response=$this->client->propfind($path, array(), 1); $response=$this->client->propfind($path, array(), 1);
$id=md5('webdav'.$this->root.$path); $id=md5('webdav'.$this->root.$path);
$content = array();
\OC_FakeDirStream::$dirs[$id]=array(); \OC_FakeDirStream::$dirs[$id]=array();
$files=array_keys($response); $files=array_keys($response);
array_shift($files);//the first entry is the current directory array_shift($files);//the first entry is the current directory
foreach ($files as $file) { foreach ($files as $file) {
$file = urldecode(basename($file)); $file = urldecode(basename($file));
\OC_FakeDirStream::$dirs[$id][]=$file; $content[]=$file;
} }
\OC\Files\Stream\Dir::register($id, $content);
return opendir('fakedir://'.$id); return opendir('fakedir://'.$id);
} catch(\Exception $e) { } catch(\Exception $e) {
return false; return false;
@ -194,7 +196,7 @@ class DAV extends \OC\Files\Storage\Common{
$ext=''; $ext='';
} }
$tmpFile = \OCP\Files::tmpFile($ext); $tmpFile = \OCP\Files::tmpFile($ext);
\OC_CloseStreamWrapper::$callBacks[$tmpFile]=array($this, 'writeBack'); \OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
if($this->file_exists($path)) { if($this->file_exists($path)) {
$this->getFile($path, $tmpFile); $this->getFile($path, $tmpFile);
} }

View File

@ -102,7 +102,7 @@ class Shared extends \OC\Files\Storage\Common {
public function opendir($path) { public function opendir($path) {
if ($path == '' || $path == '/') { if ($path == '' || $path == '/') {
$files = \OCP\Share::getItemsSharedWith('file', \OC_Share_Backend_Folder::FORMAT_OPENDIR); $files = \OCP\Share::getItemsSharedWith('file', \OC_Share_Backend_Folder::FORMAT_OPENDIR);
\OC_FakeDirStream::$dirs['shared'] = $files; \OC\Files\Stream\Dir::register('shared', $files);
return opendir('fakedir://shared'); return opendir('fakedir://shared');
} else if ($source = $this->getSourcePath($path)) { } else if ($source = $this->getSourcePath($path)) {
list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source); list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);

View File

@ -308,7 +308,7 @@ class OC_Archive_TAR extends OC_Archive{
if($mode=='r' or $mode=='rb') { if($mode=='r' or $mode=='rb') {
return fopen($tmpFile, $mode); return fopen($tmpFile, $mode);
}else{ }else{
OC_CloseStreamWrapper::$callBacks[$tmpFile]=array($this, 'writeBack'); \OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
self::$tempFiles[$tmpFile]=$path; self::$tempFiles[$tmpFile]=$path;
return fopen('close://'.$tmpFile, $mode); return fopen('close://'.$tmpFile, $mode);
} }

View File

@ -171,7 +171,7 @@ class OC_Archive_ZIP extends OC_Archive{
$ext=''; $ext='';
} }
$tmpFile=OCP\Files::tmpFile($ext); $tmpFile=OCP\Files::tmpFile($ext);
OC_CloseStreamWrapper::$callBacks[$tmpFile]=array($this, 'writeBack'); \OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
if($this->fileExists($path)) { if($this->fileExists($path)) {
$this->extractFile($path, $tmpFile); $this->extractFile($path, $tmpFile);
} }

View File

@ -421,10 +421,9 @@ class OC
} }
// register the stream wrappers // register the stream wrappers
require_once 'streamwrappers.php'; stream_wrapper_register('fakedir', 'OC\Files\Stream\Dir');
stream_wrapper_register("fakedir", "OC_FakeDirStream"); stream_wrapper_register('static', 'OC\Files\Stream\StaticStream');
stream_wrapper_register('static', 'OC_StaticStreamWrapper'); stream_wrapper_register('close', 'OC\Files\Stream\Close');
stream_wrapper_register('close', 'OC_CloseStreamWrapper');
self::checkConfig(); self::checkConfig();
self::checkInstalled(); self::checkInstalled();

100
lib/files/stream/close.php Normal file
View File

@ -0,0 +1,100 @@
<?php
/**
* Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OC\Files\Stream;
/**
* stream wrapper that provides a callback on stream close
*/
class Close {
private static $callBacks = array();
private $path = '';
private $source;
private static $open = array();
public function stream_open($path, $mode, $options, &$opened_path) {
$path = substr($path, strlen('close://'));
$this->path = $path;
$this->source = fopen($path, $mode);
if (is_resource($this->source)) {
$this->meta = stream_get_meta_data($this->source);
}
self::$open[] = $path;
return is_resource($this->source);
}
public function stream_seek($offset, $whence = SEEK_SET) {
fseek($this->source, $offset, $whence);
}
public function stream_tell() {
return ftell($this->source);
}
public function stream_read($count) {
return fread($this->source, $count);
}
public function stream_write($data) {
return fwrite($this->source, $data);
}
public function stream_set_option($option, $arg1, $arg2) {
switch ($option) {
case STREAM_OPTION_BLOCKING:
stream_set_blocking($this->source, $arg1);
break;
case STREAM_OPTION_READ_TIMEOUT:
stream_set_timeout($this->source, $arg1, $arg2);
break;
case STREAM_OPTION_WRITE_BUFFER:
stream_set_write_buffer($this->source, $arg1, $arg2);
}
}
public function stream_stat() {
return fstat($this->source);
}
public function stream_lock($mode) {
flock($this->source, $mode);
}
public function stream_flush() {
return fflush($this->source);
}
public function stream_eof() {
return feof($this->source);
}
public function url_stat($path) {
$path = substr($path, strlen('close://'));
if (file_exists($path)) {
return stat($path);
} else {
return false;
}
}
public function stream_close() {
fclose($this->source);
if (isset(self::$callBacks[$this->path])) {
call_user_func(self::$callBacks[$this->path], $this->path);
}
}
public function unlink($path) {
$path = substr($path, strlen('close://'));
return unlink($path);
}
public static function registerCallback($path, $callback) {
self::$callBacks[$path] = $callback;
}
}

47
lib/files/stream/dir.php Normal file
View File

@ -0,0 +1,47 @@
<?php
/**
* Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OC\Files\Stream;
class Dir {
private static $dirs = array();
private $name;
private $index;
public function dir_opendir($path, $options) {
$this->name = substr($path, strlen('fakedir://'));
$this->index = 0;
if (!isset(self::$dirs[$this->name])) {
self::$dirs[$this->name] = array();
}
return true;
}
public function dir_readdir() {
if ($this->index >= count(self::$dirs[$this->name])) {
return false;
}
$filename = self::$dirs[$this->name][$this->index];
$this->index++;
return $filename;
}
public function dir_closedir() {
$this->name = '';
return true;
}
public function dir_rewinddir() {
$this->index = 0;
return true;
}
public static function register($path, $content) {
self::$dirs[$path] = $content;
}
}

View File

@ -1,40 +1,14 @@
<?php <?php
/**
* Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
class OC_FakeDirStream{ namespace OC\Files\Stream;
public static $dirs=array();
private $name;
private $index;
public function dir_opendir($path, $options) { class StaticStream {
$this->name=substr($path, strlen('fakedir://'));
$this->index=0;
if(!isset(self::$dirs[$this->name])) {
self::$dirs[$this->name]=array();
}
return true;
}
public function dir_readdir() {
if($this->index>=count(self::$dirs[$this->name])) {
return false;
}
$filename=self::$dirs[$this->name][$this->index];
$this->index++;
return $filename;
}
public function dir_closedir() {
$this->name='';
return true;
}
public function dir_rewinddir() {
$this->index=0;
return true;
}
}
class OC_StaticStreamWrapper {
public $context; public $context;
protected static $data = array(); protected static $data = array();
@ -42,13 +16,15 @@ class OC_StaticStreamWrapper {
protected $pointer = 0; protected $pointer = 0;
protected $writable = false; protected $writable = false;
public function stream_close() {} public function stream_close() {
}
public function stream_eof() { public function stream_eof() {
return $this->pointer >= strlen(self::$data[$this->path]); return $this->pointer >= strlen(self::$data[$this->path]);
} }
public function stream_flush() {} public function stream_flush() {
}
public function stream_open($path, $mode, $options, &$opened_path) { public function stream_open($path, $mode, $options, &$opened_path) {
switch ($mode[0]) { switch ($mode[0]) {
@ -213,89 +189,3 @@ class OC_StaticStreamWrapper {
return false; return false;
} }
} }
/**
* stream wrapper that provides a callback on stream close
*/
class OC_CloseStreamWrapper{
public static $callBacks=array();
private $path='';
private $source;
private static $open=array();
public function stream_open($path, $mode, $options, &$opened_path) {
$path=substr($path, strlen('close://'));
$this->path=$path;
$this->source=fopen($path, $mode);
if(is_resource($this->source)) {
$this->meta=stream_get_meta_data($this->source);
}
self::$open[]=$path;
return is_resource($this->source);
}
public function stream_seek($offset, $whence=SEEK_SET) {
fseek($this->source, $offset, $whence);
}
public function stream_tell() {
return ftell($this->source);
}
public function stream_read($count) {
return fread($this->source, $count);
}
public function stream_write($data) {
return fwrite($this->source, $data);
}
public function stream_set_option($option, $arg1, $arg2) {
switch($option) {
case STREAM_OPTION_BLOCKING:
stream_set_blocking($this->source, $arg1);
break;
case STREAM_OPTION_READ_TIMEOUT:
stream_set_timeout($this->source, $arg1, $arg2);
break;
case STREAM_OPTION_WRITE_BUFFER:
stream_set_write_buffer($this->source, $arg1, $arg2);
}
}
public function stream_stat() {
return fstat($this->source);
}
public function stream_lock($mode) {
flock($this->source, $mode);
}
public function stream_flush() {
return fflush($this->source);
}
public function stream_eof() {
return feof($this->source);
}
public function url_stat($path) {
$path=substr($path, strlen('close://'));
if(file_exists($path)) {
return stat($path);
}else{
return false;
}
}
public function stream_close() {
fclose($this->source);
if(isset(self::$callBacks[$this->path])) {
call_user_func(self::$callBacks[$this->path], $this->path);
}
}
public function unlink($path) {
$path=substr($path, strlen('close://'));
return unlink($path);
}
}

View File

@ -23,7 +23,7 @@
class Test_StreamWrappers extends PHPUnit_Framework_TestCase { class Test_StreamWrappers extends PHPUnit_Framework_TestCase {
public function testFakeDir() { public function testFakeDir() {
$items = array('foo', 'bar'); $items = array('foo', 'bar');
OC_FakeDirStream::$dirs['test']=$items; \OC\Files\Stream\Dir::register('test', $items);
$dh = opendir('fakedir://test'); $dh = opendir('fakedir://test');
$result = array(); $result = array();
while ($file = readdir($dh)) { while ($file = readdir($dh)) {
@ -60,7 +60,7 @@ class Test_StreamWrappers extends PHPUnit_Framework_TestCase {
//test callback //test callback
$tmpFile = OC_Helper::TmpFile('.txt'); $tmpFile = OC_Helper::TmpFile('.txt');
$file = 'close://' . $tmpFile; $file = 'close://' . $tmpFile;
OC_CloseStreamWrapper::$callBacks[$tmpFile]=array('Test_StreamWrappers', 'closeCallBack'); \OC\Files\Stream\Close::registerCallback($tmpFile, array('Test_StreamWrappers', 'closeCallBack'));
$fh = fopen($file, 'w'); $fh = fopen($file, 'w');
fwrite($fh, 'asd'); fwrite($fh, 'asd');
try { try {