Availability.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. * @author Robin McCorkell <robin@mccorkell.me.uk>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Files\Storage\Wrapper;
  24. /**
  25. * Availability checker for storages
  26. *
  27. * Throws a StorageNotAvailableException for storages with known failures
  28. */
  29. class Availability extends Wrapper {
  30. const RECHECK_TTL_SEC = 600; // 10 minutes
  31. public static function shouldRecheck($availability) {
  32. if (!$availability['available']) {
  33. // trigger a recheck if TTL reached
  34. if ((time() - $availability['last_checked']) > self::RECHECK_TTL_SEC) {
  35. return true;
  36. }
  37. }
  38. return false;
  39. }
  40. /**
  41. * Only called if availability === false
  42. *
  43. * @return bool
  44. */
  45. private function updateAvailability() {
  46. // reset availability to false so that multiple requests don't recheck concurrently
  47. $this->setAvailability(false);
  48. try {
  49. $result = $this->test();
  50. } catch (\Exception $e) {
  51. $result = false;
  52. }
  53. $this->setAvailability($result);
  54. return $result;
  55. }
  56. /**
  57. * @return bool
  58. */
  59. private function isAvailable() {
  60. $availability = $this->getAvailability();
  61. if (self::shouldRecheck($availability)) {
  62. return $this->updateAvailability();
  63. }
  64. return $availability['available'];
  65. }
  66. /**
  67. * @throws \OCP\Files\StorageNotAvailableException
  68. */
  69. private function checkAvailability() {
  70. if (!$this->isAvailable()) {
  71. throw new \OCP\Files\StorageNotAvailableException();
  72. }
  73. }
  74. /** {@inheritdoc} */
  75. public function mkdir($path) {
  76. $this->checkAvailability();
  77. try {
  78. return parent::mkdir($path);
  79. } catch (\OCP\Files\StorageNotAvailableException $e) {
  80. $this->setAvailability(false);
  81. throw $e;
  82. }
  83. }
  84. /** {@inheritdoc} */
  85. public function rmdir($path) {
  86. $this->checkAvailability();
  87. try {
  88. return parent::rmdir($path);
  89. } catch (\OCP\Files\StorageNotAvailableException $e) {
  90. $this->setAvailability(false);
  91. throw $e;
  92. }
  93. }
  94. /** {@inheritdoc} */
  95. public function opendir($path) {
  96. $this->checkAvailability();
  97. try {
  98. return parent::opendir($path);
  99. } catch (\OCP\Files\StorageNotAvailableException $e) {
  100. $this->setAvailability(false);
  101. throw $e;
  102. }
  103. }
  104. /** {@inheritdoc} */
  105. public function is_dir($path) {
  106. $this->checkAvailability();
  107. try {
  108. return parent::is_dir($path);
  109. } catch (\OCP\Files\StorageNotAvailableException $e) {
  110. $this->setAvailability(false);
  111. throw $e;
  112. }
  113. }
  114. /** {@inheritdoc} */
  115. public function is_file($path) {
  116. $this->checkAvailability();
  117. try {
  118. return parent::is_file($path);
  119. } catch (\OCP\Files\StorageNotAvailableException $e) {
  120. $this->setAvailability(false);
  121. throw $e;
  122. }
  123. }
  124. /** {@inheritdoc} */
  125. public function stat($path) {
  126. $this->checkAvailability();
  127. try {
  128. return parent::stat($path);
  129. } catch (\OCP\Files\StorageNotAvailableException $e) {
  130. $this->setAvailability(false);
  131. throw $e;
  132. }
  133. }
  134. /** {@inheritdoc} */
  135. public function filetype($path) {
  136. $this->checkAvailability();
  137. try {
  138. return parent::filetype($path);
  139. } catch (\OCP\Files\StorageNotAvailableException $e) {
  140. $this->setAvailability(false);
  141. throw $e;
  142. }
  143. }
  144. /** {@inheritdoc} */
  145. public function filesize($path) {
  146. $this->checkAvailability();
  147. try {
  148. return parent::filesize($path);
  149. } catch (\OCP\Files\StorageNotAvailableException $e) {
  150. $this->setAvailability(false);
  151. throw $e;
  152. }
  153. }
  154. /** {@inheritdoc} */
  155. public function isCreatable($path) {
  156. $this->checkAvailability();
  157. try {
  158. return parent::isCreatable($path);
  159. } catch (\OCP\Files\StorageNotAvailableException $e) {
  160. $this->setAvailability(false);
  161. throw $e;
  162. }
  163. }
  164. /** {@inheritdoc} */
  165. public function isReadable($path) {
  166. $this->checkAvailability();
  167. try {
  168. return parent::isReadable($path);
  169. } catch (\OCP\Files\StorageNotAvailableException $e) {
  170. $this->setAvailability(false);
  171. throw $e;
  172. }
  173. }
  174. /** {@inheritdoc} */
  175. public function isUpdatable($path) {
  176. $this->checkAvailability();
  177. try {
  178. return parent::isUpdatable($path);
  179. } catch (\OCP\Files\StorageNotAvailableException $e) {
  180. $this->setAvailability(false);
  181. throw $e;
  182. }
  183. }
  184. /** {@inheritdoc} */
  185. public function isDeletable($path) {
  186. $this->checkAvailability();
  187. try {
  188. return parent::isDeletable($path);
  189. } catch (\OCP\Files\StorageNotAvailableException $e) {
  190. $this->setAvailability(false);
  191. throw $e;
  192. }
  193. }
  194. /** {@inheritdoc} */
  195. public function isSharable($path) {
  196. $this->checkAvailability();
  197. try {
  198. return parent::isSharable($path);
  199. } catch (\OCP\Files\StorageNotAvailableException $e) {
  200. $this->setAvailability(false);
  201. throw $e;
  202. }
  203. }
  204. /** {@inheritdoc} */
  205. public function getPermissions($path) {
  206. $this->checkAvailability();
  207. try {
  208. return parent::getPermissions($path);
  209. } catch (\OCP\Files\StorageNotAvailableException $e) {
  210. $this->setAvailability(false);
  211. throw $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 (\OCP\Files\StorageNotAvailableException $e) {
  223. $this->setAvailability(false);
  224. throw $e;
  225. }
  226. }
  227. /** {@inheritdoc} */
  228. public function filemtime($path) {
  229. $this->checkAvailability();
  230. try {
  231. return parent::filemtime($path);
  232. } catch (\OCP\Files\StorageNotAvailableException $e) {
  233. $this->setAvailability(false);
  234. throw $e;
  235. }
  236. }
  237. /** {@inheritdoc} */
  238. public function file_get_contents($path) {
  239. $this->checkAvailability();
  240. try {
  241. return parent::file_get_contents($path);
  242. } catch (\OCP\Files\StorageNotAvailableException $e) {
  243. $this->setAvailability(false);
  244. throw $e;
  245. }
  246. }
  247. /** {@inheritdoc} */
  248. public function file_put_contents($path, $data) {
  249. $this->checkAvailability();
  250. try {
  251. return parent::file_put_contents($path, $data);
  252. } catch (\OCP\Files\StorageNotAvailableException $e) {
  253. $this->setAvailability(false);
  254. throw $e;
  255. }
  256. }
  257. /** {@inheritdoc} */
  258. public function unlink($path) {
  259. $this->checkAvailability();
  260. try {
  261. return parent::unlink($path);
  262. } catch (\OCP\Files\StorageNotAvailableException $e) {
  263. $this->setAvailability(false);
  264. throw $e;
  265. }
  266. }
  267. /** {@inheritdoc} */
  268. public function rename($path1, $path2) {
  269. $this->checkAvailability();
  270. try {
  271. return parent::rename($path1, $path2);
  272. } catch (\OCP\Files\StorageNotAvailableException $e) {
  273. $this->setAvailability(false);
  274. throw $e;
  275. }
  276. }
  277. /** {@inheritdoc} */
  278. public function copy($path1, $path2) {
  279. $this->checkAvailability();
  280. try {
  281. return parent::copy($path1, $path2);
  282. } catch (\OCP\Files\StorageNotAvailableException $e) {
  283. $this->setAvailability(false);
  284. throw $e;
  285. }
  286. }
  287. /** {@inheritdoc} */
  288. public function fopen($path, $mode) {
  289. $this->checkAvailability();
  290. try {
  291. return parent::fopen($path, $mode);
  292. } catch (\OCP\Files\StorageNotAvailableException $e) {
  293. $this->setAvailability(false);
  294. throw $e;
  295. }
  296. }
  297. /** {@inheritdoc} */
  298. public function getMimeType($path) {
  299. $this->checkAvailability();
  300. try {
  301. return parent::getMimeType($path);
  302. } catch (\OCP\Files\StorageNotAvailableException $e) {
  303. $this->setAvailability(false);
  304. throw $e;
  305. }
  306. }
  307. /** {@inheritdoc} */
  308. public function hash($type, $path, $raw = false) {
  309. $this->checkAvailability();
  310. try {
  311. return parent::hash($type, $path, $raw);
  312. } catch (\OCP\Files\StorageNotAvailableException $e) {
  313. $this->setAvailability(false);
  314. throw $e;
  315. }
  316. }
  317. /** {@inheritdoc} */
  318. public function free_space($path) {
  319. $this->checkAvailability();
  320. try {
  321. return parent::free_space($path);
  322. } catch (\OCP\Files\StorageNotAvailableException $e) {
  323. $this->setAvailability(false);
  324. throw $e;
  325. }
  326. }
  327. /** {@inheritdoc} */
  328. public function search($query) {
  329. $this->checkAvailability();
  330. try {
  331. return parent::search($query);
  332. } catch (\OCP\Files\StorageNotAvailableException $e) {
  333. $this->setAvailability(false);
  334. throw $e;
  335. }
  336. }
  337. /** {@inheritdoc} */
  338. public function touch($path, $mtime = null) {
  339. $this->checkAvailability();
  340. try {
  341. return parent::touch($path, $mtime);
  342. } catch (\OCP\Files\StorageNotAvailableException $e) {
  343. $this->setAvailability(false);
  344. throw $e;
  345. }
  346. }
  347. /** {@inheritdoc} */
  348. public function getLocalFile($path) {
  349. $this->checkAvailability();
  350. try {
  351. return parent::getLocalFile($path);
  352. } catch (\OCP\Files\StorageNotAvailableException $e) {
  353. $this->setAvailability(false);
  354. throw $e;
  355. }
  356. }
  357. /** {@inheritdoc} */
  358. public function hasUpdated($path, $time) {
  359. $this->checkAvailability();
  360. try {
  361. return parent::hasUpdated($path, $time);
  362. } catch (\OCP\Files\StorageNotAvailableException $e) {
  363. $this->setAvailability(false);
  364. throw $e;
  365. }
  366. }
  367. /** {@inheritdoc} */
  368. public function getOwner($path) {
  369. try {
  370. return parent::getOwner($path);
  371. } catch (\OCP\Files\StorageNotAvailableException $e) {
  372. $this->setAvailability(false);
  373. throw $e;
  374. }
  375. }
  376. /** {@inheritdoc} */
  377. public function getETag($path) {
  378. $this->checkAvailability();
  379. try {
  380. return parent::getETag($path);
  381. } catch (\OCP\Files\StorageNotAvailableException $e) {
  382. $this->setAvailability(false);
  383. throw $e;
  384. }
  385. }
  386. /** {@inheritdoc} */
  387. public function getDirectDownload($path) {
  388. $this->checkAvailability();
  389. try {
  390. return parent::getDirectDownload($path);
  391. } catch (\OCP\Files\StorageNotAvailableException $e) {
  392. $this->setAvailability(false);
  393. throw $e;
  394. }
  395. }
  396. /** {@inheritdoc} */
  397. public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  398. $this->checkAvailability();
  399. try {
  400. return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  401. } catch (\OCP\Files\StorageNotAvailableException $e) {
  402. $this->setAvailability(false);
  403. throw $e;
  404. }
  405. }
  406. /** {@inheritdoc} */
  407. public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  408. $this->checkAvailability();
  409. try {
  410. return parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  411. } catch (\OCP\Files\StorageNotAvailableException $e) {
  412. $this->setAvailability(false);
  413. throw $e;
  414. }
  415. }
  416. /** {@inheritdoc} */
  417. public function getMetaData($path) {
  418. $this->checkAvailability();
  419. try {
  420. return parent::getMetaData($path);
  421. } catch (\OCP\Files\StorageNotAvailableException $e) {
  422. $this->setAvailability(false);
  423. throw $e;
  424. }
  425. }
  426. }