1
0

File.php 5.7 KB

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