2015-01-24 00:53:21 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-01-24 00:53:21 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-01-24 00:53:21 +03:00
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2015-01-24 00:53:21 +03:00
|
|
|
namespace OC\Files\Storage\Wrapper;
|
|
|
|
|
2017-07-19 20:44:10 +03:00
|
|
|
use OCP\Files\Storage\IStorage;
|
2019-08-22 13:19:51 +03:00
|
|
|
use OCP\Files\StorageAuthException;
|
|
|
|
use OCP\Files\StorageNotAvailableException;
|
|
|
|
use OCP\IConfig;
|
2017-07-19 20:44:10 +03:00
|
|
|
|
2015-01-24 00:53:21 +03:00
|
|
|
/**
|
|
|
|
* Availability checker for storages
|
|
|
|
*
|
|
|
|
* Throws a StorageNotAvailableException for storages with known failures
|
|
|
|
*/
|
|
|
|
class Availability extends Wrapper {
|
|
|
|
const RECHECK_TTL_SEC = 600; // 10 minutes
|
|
|
|
|
2019-08-22 13:19:51 +03:00
|
|
|
/** @var IConfig */
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
public function __construct($parameters) {
|
|
|
|
$this->config = $parameters['config'] ?? \OC::$server->getConfig();
|
|
|
|
parent::__construct($parameters);
|
|
|
|
}
|
|
|
|
|
2016-03-03 16:19:34 +03:00
|
|
|
public static function shouldRecheck($availability) {
|
|
|
|
if (!$availability['available']) {
|
|
|
|
// trigger a recheck if TTL reached
|
|
|
|
if ((time() - $availability['last_checked']) > self::RECHECK_TTL_SEC) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-24 00:53:21 +03:00
|
|
|
/**
|
2016-04-27 00:30:09 +03:00
|
|
|
* Only called if availability === false
|
|
|
|
*
|
2015-01-24 00:53:21 +03:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function updateAvailability() {
|
2016-04-27 00:30:09 +03:00
|
|
|
// reset availability to false so that multiple requests don't recheck concurrently
|
|
|
|
$this->setAvailability(false);
|
2015-01-24 00:53:21 +03:00
|
|
|
try {
|
|
|
|
$result = $this->test();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
$this->setAvailability($result);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isAvailable() {
|
|
|
|
$availability = $this->getAvailability();
|
2016-03-03 16:19:34 +03:00
|
|
|
if (self::shouldRecheck($availability)) {
|
|
|
|
return $this->updateAvailability();
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
return $availability['available'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-22 13:19:51 +03:00
|
|
|
* @throws StorageNotAvailableException
|
2015-01-24 00:53:21 +03:00
|
|
|
*/
|
|
|
|
private function checkAvailability() {
|
|
|
|
if (!$this->isAvailable()) {
|
2019-08-22 13:19:51 +03:00
|
|
|
throw new StorageNotAvailableException();
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function mkdir($path) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::mkdir($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function rmdir($path) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::rmdir($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function opendir($path) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::opendir($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function is_dir($path) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::is_dir($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function is_file($path) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::is_file($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function stat($path) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::stat($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function filetype($path) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::filetype($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function filesize($path) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::filesize($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function isCreatable($path) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::isCreatable($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function isReadable($path) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::isReadable($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function isUpdatable($path) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::isUpdatable($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function isDeletable($path) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::isDeletable($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function isSharable($path) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::isSharable($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function getPermissions($path) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::getPermissions($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function file_exists($path) {
|
2015-10-05 18:50:28 +03:00
|
|
|
if ($path === '') {
|
|
|
|
return true;
|
|
|
|
}
|
2015-01-24 00:53:21 +03:00
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::file_exists($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function filemtime($path) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::filemtime($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function file_get_contents($path) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::file_get_contents($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function file_put_contents($path, $data) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::file_put_contents($path, $data);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function unlink($path) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::unlink($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function rename($path1, $path2) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::rename($path1, $path2);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function copy($path1, $path2) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::copy($path1, $path2);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function fopen($path, $mode) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::fopen($path, $mode);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function getMimeType($path) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::getMimeType($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function hash($type, $path, $raw = false) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::hash($type, $path, $raw);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function free_space($path) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::free_space($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function search($query) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::search($query);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function touch($path, $mtime = null) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::touch($path, $mtime);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function getLocalFile($path) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::getLocalFile($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function hasUpdated($path, $time) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::hasUpdated($path, $time);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function getOwner($path) {
|
|
|
|
try {
|
|
|
|
return parent::getOwner($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function getETag($path) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::getETag($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function getDirectDownload($path) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::getDirectDownload($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
2017-07-19 20:44:10 +03:00
|
|
|
public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
|
2015-01-24 00:53:21 +03:00
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
2017-07-19 20:44:10 +03:00
|
|
|
public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
|
2015-01-24 00:53:21 +03:00
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function getMetaData($path) {
|
|
|
|
$this->checkAvailability();
|
|
|
|
try {
|
|
|
|
return parent::getMetaData($path);
|
2019-08-22 13:19:51 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->setUnavailable($e);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
2019-08-22 13:19:51 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws StorageNotAvailableException
|
|
|
|
*/
|
|
|
|
protected function setUnavailable(StorageNotAvailableException $e) {
|
|
|
|
$delay = self::RECHECK_TTL_SEC;
|
|
|
|
if($e instanceof StorageAuthException) {
|
|
|
|
$delay = max(
|
|
|
|
// 30min
|
|
|
|
$this->config->getSystemValueInt('external_storage.auth_availability_delay', 1800),
|
|
|
|
self::RECHECK_TTL_SEC
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$this->getStorageCache()->setAvailability(false, $delay);
|
|
|
|
throw $e;
|
|
|
|
}
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|