aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/files/stream
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2013-09-25 13:36:30 +0200
committerThomas Müller <thomas.mueller@tmit.eu>2013-09-30 16:36:59 +0200
commit9c9dc276b7a1d2592c4fb0a887888632dc1f1e29 (patch)
treebbe3aed3e09c31c68806bdb8acffef70ba08f51c /lib/private/files/stream
parenta711399e62d5a9f14d4b748efe4354ee37e61f13 (diff)
downloadnextcloud-server-9c9dc276b7a1d2592c4fb0a887888632dc1f1e29.tar.gz
nextcloud-server-9c9dc276b7a1d2592c4fb0a887888632dc1f1e29.zip
move the private namespace OC into lib/private - OCP will stay in lib/public
Conflicts: lib/private/vcategories.php
Diffstat (limited to 'lib/private/files/stream')
-rw-r--r--lib/private/files/stream/close.php100
-rw-r--r--lib/private/files/stream/dir.php47
-rw-r--r--lib/private/files/stream/oc.php129
-rw-r--r--lib/private/files/stream/quota.php128
-rw-r--r--lib/private/files/stream/staticstream.php156
5 files changed, 560 insertions, 0 deletions
diff --git a/lib/private/files/stream/close.php b/lib/private/files/stream/close.php
new file mode 100644
index 00000000000..80de3497c36
--- /dev/null
+++ b/lib/private/files/stream/close.php
@@ -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;
+ }
+}
diff --git a/lib/private/files/stream/dir.php b/lib/private/files/stream/dir.php
new file mode 100644
index 00000000000..6ca884fc994
--- /dev/null
+++ b/lib/private/files/stream/dir.php
@@ -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;
+ }
+}
diff --git a/lib/private/files/stream/oc.php b/lib/private/files/stream/oc.php
new file mode 100644
index 00000000000..88e7e062df9
--- /dev/null
+++ b/lib/private/files/stream/oc.php
@@ -0,0 +1,129 @@
+<?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;
+
+/**
+ * a stream wrappers for ownCloud's virtual filesystem
+ */
+class OC {
+ /**
+ * @var \OC\Files\View
+ */
+ static private $rootView;
+
+ private $path;
+ private $dirSource;
+ private $fileSource;
+ private $meta;
+
+ private function setup(){
+ if (!self::$rootView) {
+ self::$rootView = new \OC\Files\View('');
+ }
+ }
+
+ public function stream_open($path, $mode, $options, &$opened_path) {
+ $this->setup();
+ $path = substr($path, strlen('oc://'));
+ $this->path = $path;
+ $this->fileSource = self::$rootView->fopen($path, $mode);
+ if (is_resource($this->fileSource)) {
+ $this->meta = stream_get_meta_data($this->fileSource);
+ }
+ return is_resource($this->fileSource);
+ }
+
+ public function stream_seek($offset, $whence = SEEK_SET) {
+ fseek($this->fileSource, $offset, $whence);
+ }
+
+ public function stream_tell() {
+ return ftell($this->fileSource);
+ }
+
+ public function stream_read($count) {
+ return fread($this->fileSource, $count);
+ }
+
+ public function stream_write($data) {
+ return fwrite($this->fileSource, $data);
+ }
+
+ public function stream_set_option($option, $arg1, $arg2) {
+ switch ($option) {
+ case STREAM_OPTION_BLOCKING:
+ stream_set_blocking($this->fileSource, $arg1);
+ break;
+ case STREAM_OPTION_READ_TIMEOUT:
+ stream_set_timeout($this->fileSource, $arg1, $arg2);
+ break;
+ case STREAM_OPTION_WRITE_BUFFER:
+ stream_set_write_buffer($this->fileSource, $arg1, $arg2);
+ }
+ }
+
+ public function stream_stat() {
+ return fstat($this->fileSource);
+ }
+
+ public function stream_lock($mode) {
+ flock($this->fileSource, $mode);
+ }
+
+ public function stream_flush() {
+ return fflush($this->fileSource);
+ }
+
+ public function stream_eof() {
+ return feof($this->fileSource);
+ }
+
+ public function url_stat($path) {
+ $this->setup();
+ $path = substr($path, strlen('oc://'));
+ if (self::$rootView->file_exists($path)) {
+ return self::$rootView->stat($path);
+ } else {
+ return false;
+ }
+ }
+
+ public function stream_close() {
+ fclose($this->fileSource);
+ }
+
+ public function unlink($path) {
+ $this->setup();
+ $path = substr($path, strlen('oc://'));
+ return self::$rootView->unlink($path);
+ }
+
+ public function dir_opendir($path, $options) {
+ $this->setup();
+ $path = substr($path, strlen('oc://'));
+ $this->path = $path;
+ $this->dirSource = self::$rootView->opendir($path);
+ if (is_resource($this->dirSource)) {
+ $this->meta = stream_get_meta_data($this->dirSource);
+ }
+ return is_resource($this->dirSource);
+ }
+
+ public function dir_readdir() {
+ return readdir($this->dirSource);
+ }
+
+ public function dir_closedir() {
+ closedir($this->dirSource);
+ }
+
+ public function dir_rewinddir() {
+ rewinddir($this->dirSource);
+ }
+}
diff --git a/lib/private/files/stream/quota.php b/lib/private/files/stream/quota.php
new file mode 100644
index 00000000000..53d8a03d30f
--- /dev/null
+++ b/lib/private/files/stream/quota.php
@@ -0,0 +1,128 @@
+<?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 limits the amount of data that can be written to a stream
+ *
+ * usage: void \OC\Files\Stream\Quota::register($id, $stream, $limit)
+ * or: resource \OC\Files\Stream\Quota::wrap($stream, $limit)
+ */
+class Quota {
+ private static $streams = array();
+
+ /**
+ * @var resource $source
+ */
+ private $source;
+
+ /**
+ * @var int $limit
+ */
+ private $limit;
+
+ /**
+ * @param string $id
+ * @param resource $stream
+ * @param int $limit
+ */
+ public static function register($id, $stream, $limit) {
+ self::$streams[$id] = array($stream, $limit);
+ }
+
+ /**
+ * remove all registered streams
+ */
+ public static function clear() {
+ self::$streams = array();
+ }
+
+ /**
+ * @param resource $stream
+ * @param int $limit
+ * @return resource
+ */
+ static public function wrap($stream, $limit) {
+ $id = uniqid();
+ self::register($id, $stream, $limit);
+ $meta = stream_get_meta_data($stream);
+ return fopen('quota://' . $id, $meta['mode']);
+ }
+
+ public function stream_open($path, $mode, $options, &$opened_path) {
+ $id = substr($path, strlen('quota://'));
+ if (isset(self::$streams[$id])) {
+ list($this->source, $this->limit) = self::$streams[$id];
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ public function stream_seek($offset, $whence = SEEK_SET) {
+ if ($whence === SEEK_SET) {
+ $this->limit += $this->stream_tell() - $offset;
+ } else {
+ $this->limit -= $offset;
+ }
+ fseek($this->source, $offset, $whence);
+ }
+
+ public function stream_tell() {
+ return ftell($this->source);
+ }
+
+ public function stream_read($count) {
+ $this->limit -= $count;
+ return fread($this->source, $count);
+ }
+
+ public function stream_write($data) {
+ $size = strlen($data);
+ if ($size > $this->limit) {
+ $data = substr($data, 0, $this->limit);
+ $size = $this->limit;
+ }
+ $this->limit -= $size;
+ 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 stream_close() {
+ fclose($this->source);
+ }
+}
diff --git a/lib/private/files/stream/staticstream.php b/lib/private/files/stream/staticstream.php
new file mode 100644
index 00000000000..45b1a7a81f8
--- /dev/null
+++ b/lib/private/files/stream/staticstream.php
@@ -0,0 +1,156 @@
+<?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 StaticStream {
+ const MODE_FILE = 0100000;
+
+ public $context;
+ protected static $data = array();
+
+ protected $path = '';
+ protected $pointer = 0;
+ protected $writable = false;
+
+ public function stream_close() {
+ }
+
+ public function stream_eof() {
+ return $this->pointer >= strlen(self::$data[$this->path]);
+ }
+
+ public function stream_flush() {
+ }
+
+ public static function clear() {
+ self::$data = array();
+ }
+
+ public function stream_open($path, $mode, $options, &$opened_path) {
+ switch ($mode[0]) {
+ case 'r':
+ if (!isset(self::$data[$path])) return false;
+ $this->path = $path;
+ $this->writable = isset($mode[1]) && $mode[1] == '+';
+ break;
+ case 'w':
+ self::$data[$path] = '';
+ $this->path = $path;
+ $this->writable = true;
+ break;
+ case 'a':
+ if (!isset(self::$data[$path])) self::$data[$path] = '';
+ $this->path = $path;
+ $this->writable = true;
+ $this->pointer = strlen(self::$data[$path]);
+ break;
+ case 'x':
+ if (isset(self::$data[$path])) return false;
+ $this->path = $path;
+ $this->writable = true;
+ break;
+ case 'c':
+ if (!isset(self::$data[$path])) self::$data[$path] = '';
+ $this->path = $path;
+ $this->writable = true;
+ break;
+ default:
+ return false;
+ }
+ $opened_path = $this->path;
+ return true;
+ }
+
+ public function stream_read($count) {
+ $bytes = min(strlen(self::$data[$this->path]) - $this->pointer, $count);
+ $data = substr(self::$data[$this->path], $this->pointer, $bytes);
+ $this->pointer += $bytes;
+ return $data;
+ }
+
+ public function stream_seek($offset, $whence = SEEK_SET) {
+ $len = strlen(self::$data[$this->path]);
+ switch ($whence) {
+ case SEEK_SET:
+ if ($offset <= $len) {
+ $this->pointer = $offset;
+ return true;
+ }
+ break;
+ case SEEK_CUR:
+ if ($this->pointer + $offset <= $len) {
+ $this->pointer += $offset;
+ return true;
+ }
+ break;
+ case SEEK_END:
+ if ($len + $offset <= $len) {
+ $this->pointer = $len + $offset;
+ return true;
+ }
+ break;
+ }
+ return false;
+ }
+
+ public function stream_stat() {
+ return $this->url_stat($this->path);
+ }
+
+ public function stream_tell() {
+ return $this->pointer;
+ }
+
+ public function stream_write($data) {
+ if (!$this->writable) return 0;
+ $size = strlen($data);
+ if ($this->stream_eof()) {
+ self::$data[$this->path] .= $data;
+ } else {
+ self::$data[$this->path] = substr_replace(
+ self::$data[$this->path],
+ $data,
+ $this->pointer
+ );
+ }
+ $this->pointer += $size;
+ return $size;
+ }
+
+ public function unlink($path) {
+ if (isset(self::$data[$path])) {
+ unset(self::$data[$path]);
+ }
+ return true;
+ }
+
+ public function url_stat($path) {
+ if (isset(self::$data[$path])) {
+ $size = strlen(self::$data[$path]);
+ $time = time();
+ $data = array(
+ 'dev' => 0,
+ 'ino' => 0,
+ 'mode' => self::MODE_FILE | 0777,
+ 'nlink' => 1,
+ 'uid' => 0,
+ 'gid' => 0,
+ 'rdev' => '',
+ 'size' => $size,
+ 'atime' => $time,
+ 'mtime' => $time,
+ 'ctime' => $time,
+ 'blksize' => -1,
+ 'blocks' => -1,
+ );
+ return array_values($data) + $data;
+ }
+ return false;
+ }
+}