availability.php 11 KB

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