Availability.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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($parameters) {
  22. $this->config = $parameters['config'] ?? \OC::$server->getConfig();
  23. parent::__construct($parameters);
  24. }
  25. public static function shouldRecheck($availability) {
  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. * @return bool
  38. */
  39. private function updateAvailability() {
  40. // reset availability to false so that multiple requests don't recheck concurrently
  41. $this->setAvailability(false);
  42. try {
  43. $result = $this->test();
  44. } catch (\Exception $e) {
  45. $result = false;
  46. }
  47. $this->setAvailability($result);
  48. return $result;
  49. }
  50. /**
  51. * @return bool
  52. */
  53. private function isAvailable() {
  54. $availability = $this->getAvailability();
  55. if (self::shouldRecheck($availability)) {
  56. return $this->updateAvailability();
  57. }
  58. return $availability['available'];
  59. }
  60. /**
  61. * @throws StorageNotAvailableException
  62. */
  63. private function checkAvailability() {
  64. if (!$this->isAvailable()) {
  65. throw new StorageNotAvailableException();
  66. }
  67. }
  68. /** {@inheritdoc} */
  69. public function mkdir($path) {
  70. $this->checkAvailability();
  71. try {
  72. return parent::mkdir($path);
  73. } catch (StorageNotAvailableException $e) {
  74. $this->setUnavailable($e);
  75. }
  76. }
  77. /** {@inheritdoc} */
  78. public function rmdir($path) {
  79. $this->checkAvailability();
  80. try {
  81. return parent::rmdir($path);
  82. } catch (StorageNotAvailableException $e) {
  83. $this->setUnavailable($e);
  84. }
  85. }
  86. /** {@inheritdoc} */
  87. public function opendir($path) {
  88. $this->checkAvailability();
  89. try {
  90. return parent::opendir($path);
  91. } catch (StorageNotAvailableException $e) {
  92. $this->setUnavailable($e);
  93. }
  94. }
  95. /** {@inheritdoc} */
  96. public function is_dir($path) {
  97. $this->checkAvailability();
  98. try {
  99. return parent::is_dir($path);
  100. } catch (StorageNotAvailableException $e) {
  101. $this->setUnavailable($e);
  102. }
  103. }
  104. /** {@inheritdoc} */
  105. public function is_file($path) {
  106. $this->checkAvailability();
  107. try {
  108. return parent::is_file($path);
  109. } catch (StorageNotAvailableException $e) {
  110. $this->setUnavailable($e);
  111. }
  112. }
  113. /** {@inheritdoc} */
  114. public function stat($path) {
  115. $this->checkAvailability();
  116. try {
  117. return parent::stat($path);
  118. } catch (StorageNotAvailableException $e) {
  119. $this->setUnavailable($e);
  120. }
  121. }
  122. /** {@inheritdoc} */
  123. public function filetype($path) {
  124. $this->checkAvailability();
  125. try {
  126. return parent::filetype($path);
  127. } catch (StorageNotAvailableException $e) {
  128. $this->setUnavailable($e);
  129. }
  130. }
  131. /** {@inheritdoc} */
  132. public function filesize($path): false|int|float {
  133. $this->checkAvailability();
  134. try {
  135. return parent::filesize($path);
  136. } catch (StorageNotAvailableException $e) {
  137. $this->setUnavailable($e);
  138. }
  139. }
  140. /** {@inheritdoc} */
  141. public function isCreatable($path) {
  142. $this->checkAvailability();
  143. try {
  144. return parent::isCreatable($path);
  145. } catch (StorageNotAvailableException $e) {
  146. $this->setUnavailable($e);
  147. }
  148. }
  149. /** {@inheritdoc} */
  150. public function isReadable($path) {
  151. $this->checkAvailability();
  152. try {
  153. return parent::isReadable($path);
  154. } catch (StorageNotAvailableException $e) {
  155. $this->setUnavailable($e);
  156. }
  157. }
  158. /** {@inheritdoc} */
  159. public function isUpdatable($path) {
  160. $this->checkAvailability();
  161. try {
  162. return parent::isUpdatable($path);
  163. } catch (StorageNotAvailableException $e) {
  164. $this->setUnavailable($e);
  165. }
  166. }
  167. /** {@inheritdoc} */
  168. public function isDeletable($path) {
  169. $this->checkAvailability();
  170. try {
  171. return parent::isDeletable($path);
  172. } catch (StorageNotAvailableException $e) {
  173. $this->setUnavailable($e);
  174. }
  175. }
  176. /** {@inheritdoc} */
  177. public function isSharable($path) {
  178. $this->checkAvailability();
  179. try {
  180. return parent::isSharable($path);
  181. } catch (StorageNotAvailableException $e) {
  182. $this->setUnavailable($e);
  183. }
  184. }
  185. /** {@inheritdoc} */
  186. public function getPermissions($path) {
  187. $this->checkAvailability();
  188. try {
  189. return parent::getPermissions($path);
  190. } catch (StorageNotAvailableException $e) {
  191. $this->setUnavailable($e);
  192. }
  193. }
  194. /** {@inheritdoc} */
  195. public function file_exists($path) {
  196. if ($path === '') {
  197. return true;
  198. }
  199. $this->checkAvailability();
  200. try {
  201. return parent::file_exists($path);
  202. } catch (StorageNotAvailableException $e) {
  203. $this->setUnavailable($e);
  204. }
  205. }
  206. /** {@inheritdoc} */
  207. public function filemtime($path) {
  208. $this->checkAvailability();
  209. try {
  210. return parent::filemtime($path);
  211. } catch (StorageNotAvailableException $e) {
  212. $this->setUnavailable($e);
  213. }
  214. }
  215. /** {@inheritdoc} */
  216. public function file_get_contents($path) {
  217. $this->checkAvailability();
  218. try {
  219. return parent::file_get_contents($path);
  220. } catch (StorageNotAvailableException $e) {
  221. $this->setUnavailable($e);
  222. }
  223. }
  224. /** {@inheritdoc} */
  225. public function file_put_contents($path, $data) {
  226. $this->checkAvailability();
  227. try {
  228. return parent::file_put_contents($path, $data);
  229. } catch (StorageNotAvailableException $e) {
  230. $this->setUnavailable($e);
  231. }
  232. }
  233. /** {@inheritdoc} */
  234. public function unlink($path) {
  235. $this->checkAvailability();
  236. try {
  237. return parent::unlink($path);
  238. } catch (StorageNotAvailableException $e) {
  239. $this->setUnavailable($e);
  240. }
  241. }
  242. /** {@inheritdoc} */
  243. public function rename($source, $target) {
  244. $this->checkAvailability();
  245. try {
  246. return parent::rename($source, $target);
  247. } catch (StorageNotAvailableException $e) {
  248. $this->setUnavailable($e);
  249. }
  250. }
  251. /** {@inheritdoc} */
  252. public function copy($source, $target) {
  253. $this->checkAvailability();
  254. try {
  255. return parent::copy($source, $target);
  256. } catch (StorageNotAvailableException $e) {
  257. $this->setUnavailable($e);
  258. }
  259. }
  260. /** {@inheritdoc} */
  261. public function fopen($path, $mode) {
  262. $this->checkAvailability();
  263. try {
  264. return parent::fopen($path, $mode);
  265. } catch (StorageNotAvailableException $e) {
  266. $this->setUnavailable($e);
  267. }
  268. }
  269. /** {@inheritdoc} */
  270. public function getMimeType($path) {
  271. $this->checkAvailability();
  272. try {
  273. return parent::getMimeType($path);
  274. } catch (StorageNotAvailableException $e) {
  275. $this->setUnavailable($e);
  276. }
  277. }
  278. /** {@inheritdoc} */
  279. public function hash($type, $path, $raw = false) {
  280. $this->checkAvailability();
  281. try {
  282. return parent::hash($type, $path, $raw);
  283. } catch (StorageNotAvailableException $e) {
  284. $this->setUnavailable($e);
  285. }
  286. }
  287. /** {@inheritdoc} */
  288. public function free_space($path) {
  289. $this->checkAvailability();
  290. try {
  291. return parent::free_space($path);
  292. } catch (StorageNotAvailableException $e) {
  293. $this->setUnavailable($e);
  294. }
  295. }
  296. /** {@inheritdoc} */
  297. public function search($query) {
  298. $this->checkAvailability();
  299. try {
  300. return parent::search($query);
  301. } catch (StorageNotAvailableException $e) {
  302. $this->setUnavailable($e);
  303. }
  304. }
  305. /** {@inheritdoc} */
  306. public function touch($path, $mtime = null) {
  307. $this->checkAvailability();
  308. try {
  309. return parent::touch($path, $mtime);
  310. } catch (StorageNotAvailableException $e) {
  311. $this->setUnavailable($e);
  312. }
  313. }
  314. /** {@inheritdoc} */
  315. public function getLocalFile($path) {
  316. $this->checkAvailability();
  317. try {
  318. return parent::getLocalFile($path);
  319. } catch (StorageNotAvailableException $e) {
  320. $this->setUnavailable($e);
  321. }
  322. }
  323. /** {@inheritdoc} */
  324. public function hasUpdated($path, $time) {
  325. if (!$this->isAvailable()) {
  326. return false;
  327. }
  328. try {
  329. return parent::hasUpdated($path, $time);
  330. } catch (StorageNotAvailableException $e) {
  331. // set unavailable but don't rethrow
  332. $this->setUnavailable(null);
  333. return false;
  334. }
  335. }
  336. /** {@inheritdoc} */
  337. public function getOwner($path) {
  338. try {
  339. return parent::getOwner($path);
  340. } catch (StorageNotAvailableException $e) {
  341. $this->setUnavailable($e);
  342. }
  343. }
  344. /** {@inheritdoc} */
  345. public function getETag($path) {
  346. $this->checkAvailability();
  347. try {
  348. return parent::getETag($path);
  349. } catch (StorageNotAvailableException $e) {
  350. $this->setUnavailable($e);
  351. }
  352. }
  353. /** {@inheritdoc} */
  354. public function getDirectDownload($path) {
  355. $this->checkAvailability();
  356. try {
  357. return parent::getDirectDownload($path);
  358. } catch (StorageNotAvailableException $e) {
  359. $this->setUnavailable($e);
  360. }
  361. }
  362. /** {@inheritdoc} */
  363. public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  364. $this->checkAvailability();
  365. try {
  366. return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  367. } catch (StorageNotAvailableException $e) {
  368. $this->setUnavailable($e);
  369. }
  370. }
  371. /** {@inheritdoc} */
  372. public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  373. $this->checkAvailability();
  374. try {
  375. return parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  376. } catch (StorageNotAvailableException $e) {
  377. $this->setUnavailable($e);
  378. }
  379. }
  380. /** {@inheritdoc} */
  381. public function getMetaData($path) {
  382. $this->checkAvailability();
  383. try {
  384. return parent::getMetaData($path);
  385. } catch (StorageNotAvailableException $e) {
  386. $this->setUnavailable($e);
  387. }
  388. }
  389. /**
  390. * @template T of StorageNotAvailableException|null
  391. * @param T $e
  392. * @psalm-return (T is null ? void : never)
  393. * @throws StorageNotAvailableException
  394. */
  395. protected function setUnavailable(?StorageNotAvailableException $e): void {
  396. $delay = self::RECHECK_TTL_SEC;
  397. if ($e instanceof StorageAuthException) {
  398. $delay = max(
  399. // 30min
  400. $this->config->getSystemValueInt('external_storage.auth_availability_delay', 1800),
  401. self::RECHECK_TTL_SEC
  402. );
  403. }
  404. $this->getStorageCache()->setAvailability(false, $delay);
  405. if ($e !== null) {
  406. throw $e;
  407. }
  408. }
  409. public function getDirectoryContent($directory): \Traversable {
  410. $this->checkAvailability();
  411. try {
  412. return parent::getDirectoryContent($directory);
  413. } catch (StorageNotAvailableException $e) {
  414. $this->setUnavailable($e);
  415. }
  416. }
  417. }