Availability.php 11 KB

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