close.php 2.9 KB

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