File.php 5.5 KB

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