File.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Cache;
  27. use OC\Files\Filesystem;
  28. use OC\Files\View;
  29. use OCP\ICache;
  30. use OCP\Security\ISecureRandom;
  31. class File implements ICache {
  32. /** @var View */
  33. protected $storage;
  34. /**
  35. * Returns the cache storage for the logged in user
  36. *
  37. * @return \OC\Files\View cache storage
  38. * @throws \OC\ForbiddenException
  39. * @throws \OC\User\NoUserException
  40. */
  41. protected function getStorage() {
  42. if (isset($this->storage)) {
  43. return $this->storage;
  44. }
  45. if (\OC_User::isLoggedIn()) {
  46. $rootView = new View();
  47. $user = \OC::$server->getUserSession()->getUser();
  48. Filesystem::initMountPoints($user->getUID());
  49. if (!$rootView->file_exists('/' . $user->getUID() . '/cache')) {
  50. $rootView->mkdir('/' . $user->getUID() . '/cache');
  51. }
  52. $this->storage = new View('/' . $user->getUID() . '/cache');
  53. return $this->storage;
  54. } else {
  55. \OCP\Util::writeLog('core', 'Can\'t get cache storage, user not logged in', \OCP\Util::ERROR);
  56. throw new \OC\ForbiddenException('Can\t get cache storage, user not logged in');
  57. }
  58. }
  59. /**
  60. * @param string $key
  61. * @return mixed|null
  62. * @throws \OC\ForbiddenException
  63. */
  64. public function get($key) {
  65. $result = null;
  66. if ($this->hasKey($key)) {
  67. $storage = $this->getStorage();
  68. $result = $storage->file_get_contents($key);
  69. }
  70. return $result;
  71. }
  72. /**
  73. * Returns the size of the stored/cached data
  74. *
  75. * @param string $key
  76. * @return int
  77. */
  78. public function size($key) {
  79. $result = 0;
  80. if ($this->hasKey($key)) {
  81. $storage = $this->getStorage();
  82. $result = $storage->filesize($key);
  83. }
  84. return $result;
  85. }
  86. /**
  87. * @param string $key
  88. * @param mixed $value
  89. * @param int $ttl
  90. * @return bool|mixed
  91. * @throws \OC\ForbiddenException
  92. */
  93. public function set($key, $value, $ttl = 0) {
  94. $storage = $this->getStorage();
  95. $result = false;
  96. // unique id to avoid chunk collision, just in case
  97. $uniqueId = \OC::$server->getSecureRandom()->generate(
  98. 16,
  99. ISecureRandom::CHAR_DIGITS . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER
  100. );
  101. // use part file to prevent hasKey() to find the key
  102. // while it is being written
  103. $keyPart = $key . '.' . $uniqueId . '.part';
  104. if ($storage and $storage->file_put_contents($keyPart, $value)) {
  105. if ($ttl === 0) {
  106. $ttl = 86400; // 60*60*24
  107. }
  108. $result = $storage->touch($keyPart, time() + $ttl);
  109. $result &= $storage->rename($keyPart, $key);
  110. }
  111. return $result;
  112. }
  113. /**
  114. * @param string $key
  115. * @return bool
  116. * @throws \OC\ForbiddenException
  117. */
  118. public function hasKey($key) {
  119. $storage = $this->getStorage();
  120. if ($storage && $storage->is_file($key) && $storage->isReadable($key)) {
  121. return true;
  122. }
  123. return false;
  124. }
  125. /**
  126. * @param string $key
  127. * @return bool|mixed
  128. * @throws \OC\ForbiddenException
  129. */
  130. public function remove($key) {
  131. $storage = $this->getStorage();
  132. if (!$storage) {
  133. return false;
  134. }
  135. return $storage->unlink($key);
  136. }
  137. /**
  138. * @param string $prefix
  139. * @return bool
  140. * @throws \OC\ForbiddenException
  141. */
  142. public function clear($prefix = '') {
  143. $storage = $this->getStorage();
  144. if ($storage and $storage->is_dir('/')) {
  145. $dh = $storage->opendir('/');
  146. if (is_resource($dh)) {
  147. while (($file = readdir($dh)) !== false) {
  148. if ($file != '.' and $file != '..' and ($prefix === '' || strpos($file, $prefix) === 0)) {
  149. $storage->unlink('/' . $file);
  150. }
  151. }
  152. }
  153. }
  154. return true;
  155. }
  156. /**
  157. * Runs GC
  158. * @throws \OC\ForbiddenException
  159. */
  160. public function gc() {
  161. $storage = $this->getStorage();
  162. if ($storage and $storage->is_dir('/')) {
  163. // extra hour safety, in case of stray part chunks that take longer to write,
  164. // because touch() is only called after the chunk was finished
  165. $now = time() - 3600;
  166. $dh = $storage->opendir('/');
  167. if (!is_resource($dh)) {
  168. return null;
  169. }
  170. while (($file = readdir($dh)) !== false) {
  171. if ($file != '.' and $file != '..') {
  172. try {
  173. $mtime = $storage->filemtime('/' . $file);
  174. if ($mtime < $now) {
  175. $storage->unlink('/' . $file);
  176. }
  177. } catch (\OCP\Lock\LockedException $e) {
  178. // ignore locked chunks
  179. \OC::$server->getLogger()->debug('Could not cleanup locked chunk "' . $file . '"', array('app' => 'core'));
  180. } catch (\OCP\Files\ForbiddenException $e) {
  181. \OC::$server->getLogger()->debug('Could not cleanup forbidden chunk "' . $file . '"', array('app' => 'core'));
  182. } catch (\OCP\Files\LockNotAcquiredException $e) {
  183. \OC::$server->getLogger()->debug('Could not cleanup locked chunk "' . $file . '"', array('app' => 'core'));
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }