close.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Files\Stream;
  25. /**
  26. * stream wrapper that provides a callback on stream close
  27. */
  28. class Close {
  29. private static $callBacks = array();
  30. private $path = '';
  31. private $source;
  32. private static $open = array();
  33. public function stream_open($path, $mode, $options, &$opened_path) {
  34. $path = substr($path, strlen('close://'));
  35. $this->path = $path;
  36. $this->source = fopen($path, $mode);
  37. if (is_resource($this->source)) {
  38. $this->meta = stream_get_meta_data($this->source);
  39. }
  40. self::$open[] = $path;
  41. return is_resource($this->source);
  42. }
  43. public function stream_seek($offset, $whence = SEEK_SET) {
  44. return fseek($this->source, $offset, $whence) === 0;
  45. }
  46. public function stream_tell() {
  47. return ftell($this->source);
  48. }
  49. public function stream_read($count) {
  50. return fread($this->source, $count);
  51. }
  52. public function stream_write($data) {
  53. return fwrite($this->source, $data);
  54. }
  55. public function stream_set_option($option, $arg1, $arg2) {
  56. switch ($option) {
  57. case STREAM_OPTION_BLOCKING:
  58. stream_set_blocking($this->source, $arg1);
  59. break;
  60. case STREAM_OPTION_READ_TIMEOUT:
  61. stream_set_timeout($this->source, $arg1, $arg2);
  62. break;
  63. case STREAM_OPTION_WRITE_BUFFER:
  64. stream_set_write_buffer($this->source, $arg1, $arg2);
  65. }
  66. }
  67. public function stream_stat() {
  68. return fstat($this->source);
  69. }
  70. public function stream_lock($mode) {
  71. flock($this->source, $mode);
  72. }
  73. public function stream_flush() {
  74. return fflush($this->source);
  75. }
  76. public function stream_eof() {
  77. return feof($this->source);
  78. }
  79. public function url_stat($path) {
  80. $path = substr($path, strlen('close://'));
  81. if (file_exists($path)) {
  82. return stat($path);
  83. } else {
  84. return false;
  85. }
  86. }
  87. public function stream_close() {
  88. fclose($this->source);
  89. if (isset(self::$callBacks[$this->path])) {
  90. call_user_func(self::$callBacks[$this->path], $this->path);
  91. }
  92. }
  93. public function unlink($path) {
  94. $path = substr($path, strlen('close://'));
  95. return unlink($path);
  96. }
  97. /**
  98. * @param string $path
  99. */
  100. public static function registerCallback($path, $callback) {
  101. self::$callBacks[$path] = $callback;
  102. }
  103. }