Availability.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Files\Storage\Wrapper;
  8. use OCP\Files\Storage\IStorage;
  9. use OCP\Files\StorageAuthException;
  10. use OCP\Files\StorageNotAvailableException;
  11. use OCP\IConfig;
  12. /**
  13. * Availability checker for storages
  14. *
  15. * Throws a StorageNotAvailableException for storages with known failures
  16. */
  17. class Availability extends Wrapper {
  18. public const RECHECK_TTL_SEC = 600; // 10 minutes
  19. /** @var IConfig */
  20. protected $config;
  21. public function __construct(array $parameters) {
  22. $this->config = $parameters['config'] ?? \OCP\Server::get(IConfig::class);
  23. parent::__construct($parameters);
  24. }
  25. public static function shouldRecheck($availability): bool {
  26. if (!$availability['available']) {
  27. // trigger a recheck if TTL reached
  28. if ((time() - $availability['last_checked']) > self::RECHECK_TTL_SEC) {
  29. return true;
  30. }
  31. }
  32. return false;
  33. }
  34. /**
  35. * Only called if availability === false
  36. */
  37. private function updateAvailability(): bool {
  38. // reset availability to false so that multiple requests don't recheck concurrently
  39. $this->setAvailability(false);
  40. try {
  41. $result = $this->test();
  42. } catch (\Exception $e) {
  43. $result = false;
  44. }
  45. $this->setAvailability($result);
  46. return $result;
  47. }
  48. private function isAvailable(): bool {
  49. $availability = $this->getAvailability();
  50. if (self::shouldRecheck($availability)) {
  51. return $this->updateAvailability();
  52. }
  53. return $availability['available'];
  54. }
  55. /**
  56. * @throws StorageNotAvailableException
  57. */
  58. private function checkAvailability(): void {
  59. if (!$this->isAvailable()) {
  60. throw new StorageNotAvailableException();
  61. }
  62. }
  63. /**
  64. * Handles availability checks and delegates method calls dynamically
  65. */
  66. private function handleAvailability(string $method, mixed ...$args): mixed {
  67. $this->checkAvailability();
  68. try {
  69. return call_user_func_array([parent::class, $method], $args);
  70. } catch (StorageNotAvailableException $e) {
  71. $this->setUnavailable($e);
  72. return false;
  73. }
  74. }
  75. public function mkdir(string $path): bool {
  76. return $this->handleAvailability('mkdir', $path);
  77. }
  78. public function rmdir(string $path): bool {
  79. return $this->handleAvailability('rmdir', $path);
  80. }
  81. public function opendir(string $path) {
  82. return $this->handleAvailability('opendir', $path);
  83. }
  84. public function is_dir(string $path): bool {
  85. return $this->handleAvailability('is_dir', $path);
  86. }
  87. public function is_file(string $path): bool {
  88. return $this->handleAvailability('is_file', $path);
  89. }
  90. public function stat(string $path): array|false {
  91. return $this->handleAvailability('stat', $path);
  92. }
  93. public function filetype(string $path): string|false {
  94. return $this->handleAvailability('filetype', $path);
  95. }
  96. public function filesize(string $path): int|float|false {
  97. return $this->handleAvailability('filesize', $path);
  98. }
  99. public function isCreatable(string $path): bool {
  100. return $this->handleAvailability('isCreatable', $path);
  101. }
  102. public function isReadable(string $path): bool {
  103. return $this->handleAvailability('isReadable', $path);
  104. }
  105. public function isUpdatable(string $path): bool {
  106. return $this->handleAvailability('isUpdatable', $path);
  107. }
  108. public function isDeletable(string $path): bool {
  109. return $this->handleAvailability('isDeletable', $path);
  110. }
  111. public function isSharable(string $path): bool {
  112. return $this->handleAvailability('isSharable', $path);
  113. }
  114. public function getPermissions(string $path): int {
  115. return $this->handleAvailability('getPermissions', $path);
  116. }
  117. public function file_exists(string $path): bool {
  118. if ($path === '') {
  119. return true;
  120. }
  121. return $this->handleAvailability('file_exists', $path);
  122. }
  123. public function filemtime(string $path): int|false {
  124. return $this->handleAvailability('filemtime', $path);
  125. }
  126. public function file_get_contents(string $path): string|false {
  127. return $this->handleAvailability('file_get_contents', $path);
  128. }
  129. public function file_put_contents(string $path, mixed $data): int|float|false {
  130. return $this->handleAvailability('file_put_contents', $path, $data);
  131. }
  132. public function unlink(string $path): bool {
  133. return $this->handleAvailability('unlink', $path);
  134. }
  135. public function rename(string $source, string $target): bool {
  136. return $this->handleAvailability('rename', $source, $target);
  137. }
  138. public function copy(string $source, string $target): bool {
  139. return $this->handleAvailability('copy', $source, $target);
  140. }
  141. public function fopen(string $path, string $mode) {
  142. return $this->handleAvailability('fopen', $path, $mode);
  143. }
  144. public function getMimeType(string $path): string|false {
  145. return $this->handleAvailability('getMimeType', $path);
  146. }
  147. public function hash(string $type, string $path, bool $raw = false): string|false {
  148. return $this->handleAvailability('hash', $type, $path, $raw);
  149. }
  150. public function free_space(string $path): int|float|false {
  151. return $this->handleAvailability('free_space', $path);
  152. }
  153. public function touch(string $path, ?int $mtime = null): bool {
  154. return $this->handleAvailability('touch', $path, $mtime);
  155. }
  156. public function getLocalFile(string $path): string|false {
  157. return $this->handleAvailability('getLocalFile', $path);
  158. }
  159. public function hasUpdated(string $path, int $time): bool {
  160. if (!$this->isAvailable()) {
  161. return false;
  162. }
  163. try {
  164. return parent::hasUpdated($path, $time);
  165. } catch (StorageNotAvailableException $e) {
  166. // set unavailable but don't rethrow
  167. $this->setUnavailable(null);
  168. return false;
  169. }
  170. }
  171. public function getOwner(string $path): string|false {
  172. try {
  173. return parent::getOwner($path);
  174. } catch (StorageNotAvailableException $e) {
  175. $this->setUnavailable($e);
  176. return false;
  177. }
  178. }
  179. public function getETag(string $path): string|false {
  180. return $this->handleAvailability('getETag', $path);
  181. }
  182. public function getDirectDownload(string $path): array|false {
  183. return $this->handleAvailability('getDirectDownload', $path);
  184. }
  185. public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool {
  186. return $this->handleAvailability('copyFromStorage', $sourceStorage, $sourceInternalPath, $targetInternalPath);
  187. }
  188. public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool {
  189. return $this->handleAvailability('moveFromStorage', $sourceStorage, $sourceInternalPath, $targetInternalPath);
  190. }
  191. public function getMetaData(string $path): ?array {
  192. $this->checkAvailability();
  193. try {
  194. return parent::getMetaData($path);
  195. } catch (StorageNotAvailableException $e) {
  196. $this->setUnavailable($e);
  197. return null;
  198. }
  199. }
  200. /**
  201. * @template T of StorageNotAvailableException|null
  202. * @param T $e
  203. * @psalm-return (T is null ? void : never)
  204. * @throws StorageNotAvailableException
  205. */
  206. protected function setUnavailable(?StorageNotAvailableException $e): void {
  207. $delay = self::RECHECK_TTL_SEC;
  208. if ($e instanceof StorageAuthException) {
  209. $delay = max(
  210. // 30min
  211. $this->config->getSystemValueInt('external_storage.auth_availability_delay', 1800),
  212. self::RECHECK_TTL_SEC
  213. );
  214. }
  215. $this->getStorageCache()->setAvailability(false, $delay);
  216. if ($e !== null) {
  217. throw $e;
  218. }
  219. }
  220. public function getDirectoryContent(string $directory): \Traversable {
  221. $this->checkAvailability();
  222. try {
  223. return parent::getDirectoryContent($directory);
  224. } catch (StorageNotAvailableException $e) {
  225. $this->setUnavailable($e);
  226. return new \EmptyIterator();
  227. }
  228. }
  229. }