oc.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. /**
  10. * a stream wrappers for ownCloud's virtual filesystem
  11. */
  12. class OC {
  13. /**
  14. * @var \OC\Files\View
  15. */
  16. static private $rootView;
  17. private $path;
  18. private $dirSource;
  19. private $fileSource;
  20. private $meta;
  21. private function setup(){
  22. if (!self::$rootView) {
  23. self::$rootView = new \OC\Files\View('');
  24. }
  25. }
  26. public function stream_open($path, $mode, $options, &$opened_path) {
  27. $this->setup();
  28. $path = substr($path, strlen('oc://'));
  29. $this->path = $path;
  30. $this->fileSource = self::$rootView->fopen($path, $mode);
  31. if (is_resource($this->fileSource)) {
  32. $this->meta = stream_get_meta_data($this->fileSource);
  33. }
  34. return is_resource($this->fileSource);
  35. }
  36. public function stream_seek($offset, $whence = SEEK_SET) {
  37. fseek($this->fileSource, $offset, $whence);
  38. }
  39. public function stream_tell() {
  40. return ftell($this->fileSource);
  41. }
  42. public function stream_read($count) {
  43. return fread($this->fileSource, $count);
  44. }
  45. public function stream_write($data) {
  46. return fwrite($this->fileSource, $data);
  47. }
  48. public function stream_set_option($option, $arg1, $arg2) {
  49. switch ($option) {
  50. case STREAM_OPTION_BLOCKING:
  51. stream_set_blocking($this->fileSource, $arg1);
  52. break;
  53. case STREAM_OPTION_READ_TIMEOUT:
  54. stream_set_timeout($this->fileSource, $arg1, $arg2);
  55. break;
  56. case STREAM_OPTION_WRITE_BUFFER:
  57. stream_set_write_buffer($this->fileSource, $arg1, $arg2);
  58. }
  59. }
  60. public function stream_stat() {
  61. return fstat($this->fileSource);
  62. }
  63. public function stream_lock($mode) {
  64. flock($this->fileSource, $mode);
  65. }
  66. public function stream_flush() {
  67. return fflush($this->fileSource);
  68. }
  69. public function stream_eof() {
  70. return feof($this->fileSource);
  71. }
  72. public function url_stat($path) {
  73. $this->setup();
  74. $path = substr($path, strlen('oc://'));
  75. if (self::$rootView->file_exists($path)) {
  76. return self::$rootView->stat($path);
  77. } else {
  78. return false;
  79. }
  80. }
  81. public function stream_close() {
  82. fclose($this->fileSource);
  83. }
  84. public function unlink($path) {
  85. $this->setup();
  86. $path = substr($path, strlen('oc://'));
  87. return self::$rootView->unlink($path);
  88. }
  89. public function dir_opendir($path, $options) {
  90. $this->setup();
  91. $path = substr($path, strlen('oc://'));
  92. $this->path = $path;
  93. $this->dirSource = self::$rootView->opendir($path);
  94. if (is_resource($this->dirSource)) {
  95. $this->meta = stream_get_meta_data($this->dirSource);
  96. }
  97. return is_resource($this->dirSource);
  98. }
  99. public function dir_readdir() {
  100. return readdir($this->dirSource);
  101. }
  102. public function dir_closedir() {
  103. closedir($this->dirSource);
  104. }
  105. public function dir_rewinddir() {
  106. rewinddir($this->dirSource);
  107. }
  108. }