Availability.php 11 KB

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