staticstream.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Files\Stream;
  9. class StaticStream {
  10. const MODE_FILE = 0100000;
  11. public $context;
  12. protected static $data = array();
  13. protected $path = '';
  14. protected $pointer = 0;
  15. protected $writable = false;
  16. public function stream_close() {
  17. }
  18. public function stream_eof() {
  19. return $this->pointer >= strlen(self::$data[$this->path]);
  20. }
  21. public function stream_flush() {
  22. }
  23. public static function clear() {
  24. self::$data = array();
  25. }
  26. public function stream_open($path, $mode, $options, &$opened_path) {
  27. switch ($mode[0]) {
  28. case 'r':
  29. if (!isset(self::$data[$path])) return false;
  30. $this->path = $path;
  31. $this->writable = isset($mode[1]) && $mode[1] == '+';
  32. break;
  33. case 'w':
  34. self::$data[$path] = '';
  35. $this->path = $path;
  36. $this->writable = true;
  37. break;
  38. case 'a':
  39. if (!isset(self::$data[$path])) self::$data[$path] = '';
  40. $this->path = $path;
  41. $this->writable = true;
  42. $this->pointer = strlen(self::$data[$path]);
  43. break;
  44. case 'x':
  45. if (isset(self::$data[$path])) return false;
  46. $this->path = $path;
  47. $this->writable = true;
  48. break;
  49. case 'c':
  50. if (!isset(self::$data[$path])) self::$data[$path] = '';
  51. $this->path = $path;
  52. $this->writable = true;
  53. break;
  54. default:
  55. return false;
  56. }
  57. $opened_path = $this->path;
  58. return true;
  59. }
  60. public function stream_read($count) {
  61. $bytes = min(strlen(self::$data[$this->path]) - $this->pointer, $count);
  62. $data = substr(self::$data[$this->path], $this->pointer, $bytes);
  63. $this->pointer += $bytes;
  64. return $data;
  65. }
  66. public function stream_seek($offset, $whence = SEEK_SET) {
  67. $len = strlen(self::$data[$this->path]);
  68. switch ($whence) {
  69. case SEEK_SET:
  70. if ($offset <= $len) {
  71. $this->pointer = $offset;
  72. return true;
  73. }
  74. break;
  75. case SEEK_CUR:
  76. if ($this->pointer + $offset <= $len) {
  77. $this->pointer += $offset;
  78. return true;
  79. }
  80. break;
  81. case SEEK_END:
  82. if ($len + $offset <= $len) {
  83. $this->pointer = $len + $offset;
  84. return true;
  85. }
  86. break;
  87. }
  88. return false;
  89. }
  90. public function stream_stat() {
  91. return $this->url_stat($this->path);
  92. }
  93. public function stream_tell() {
  94. return $this->pointer;
  95. }
  96. public function stream_write($data) {
  97. if (!$this->writable) return 0;
  98. $size = strlen($data);
  99. if ($this->stream_eof()) {
  100. self::$data[$this->path] .= $data;
  101. } else {
  102. self::$data[$this->path] = substr_replace(
  103. self::$data[$this->path],
  104. $data,
  105. $this->pointer
  106. );
  107. }
  108. $this->pointer += $size;
  109. return $size;
  110. }
  111. public function unlink($path) {
  112. if (isset(self::$data[$path])) {
  113. unset(self::$data[$path]);
  114. }
  115. return true;
  116. }
  117. public function url_stat($path) {
  118. if (isset(self::$data[$path])) {
  119. $size = strlen(self::$data[$path]);
  120. $time = time();
  121. $data = array(
  122. 'dev' => 0,
  123. 'ino' => 0,
  124. 'mode' => self::MODE_FILE | 0777,
  125. 'nlink' => 1,
  126. 'uid' => 0,
  127. 'gid' => 0,
  128. 'rdev' => '',
  129. 'size' => $size,
  130. 'atime' => $time,
  131. 'mtime' => $time,
  132. 'ctime' => $time,
  133. 'blksize' => -1,
  134. 'blocks' => -1,
  135. );
  136. return array_values($data) + $data;
  137. }
  138. return false;
  139. }
  140. }