Skip to content

Commit

Permalink
Allow storage wrappers to through a forbidden exception with retry in…
Browse files Browse the repository at this point in the history
…formation
  • Loading branch information
nickvergessen committed Nov 16, 2015
1 parent a03b1f1 commit 0ab8a4c
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 0 deletions.
8 changes: 8 additions & 0 deletions apps/dav/lib/connector/sabre/directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
*/
namespace OCA\DAV\Connector\Sabre;

use OCA\DAV\Connector\Sabre\Exception\Forbidden;
use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
use OCA\DAV\Connector\Sabre\Exception\FileLocked;
use OCP\Files\ForbiddenException;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use Sabre\DAV\Exception\Locked;
Expand Down Expand Up @@ -117,6 +119,8 @@ public function createFile($name, $data = null) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
} catch (\OCP\Files\InvalidPathException $ex) {
throw new InvalidPath($ex->getMessage());
} catch (ForbiddenException $ex) {
throw new Forbidden($ex->getMessage(), $ex->getRetry());
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}
Expand Down Expand Up @@ -146,6 +150,8 @@ public function createDirectory($name) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
} catch (\OCP\Files\InvalidPathException $ex) {
throw new InvalidPath($ex->getMessage());
} catch (ForbiddenException $ex) {
throw new Forbidden($ex->getMessage(), $ex->getRetry());
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}
Expand Down Expand Up @@ -247,6 +253,8 @@ public function delete() {
// assume it wasn't possible to remove due to permission issue
throw new \Sabre\DAV\Exception\Forbidden();
}
} catch (ForbiddenException $ex) {
throw new Forbidden($ex->getMessage(), $ex->getRetry());
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}
Expand Down
64 changes: 64 additions & 0 deletions apps/dav/lib/connector/sabre/exception/forbidden.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* @author Joas Schilling <nickvergessen@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @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,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\DAV\Connector\Sabre\Exception;

class Forbidden extends \Sabre\DAV\Exception\Forbidden {

const NS_OWNCLOUD = 'http://owncloud.org/ns';

/**
* @var bool
*/
private $retry;

/**
* @param string $message
* @param bool $retry
* @param \Exception $previous
*/
public function __construct($message, $retry = false, \Exception $previous) {
parent::__construct($message, 0, $previous);
$this->retry = $retry;
}

/**
* This method allows the exception to include additional information
* into the WebDAV error response
*
* @param \Sabre\DAV\Server $server
* @param \DOMElement $errorNode
* @return void
*/
public function serialize(\Sabre\DAV\Server $server,\DOMElement $errorNode) {

// set ownCloud namespace
$errorNode->setAttribute('xmlns:o', self::NS_OWNCLOUD);

// adding the retry node
$error = $errorNode->ownerDocument->createElementNS('o:','o:retry', var_export($this->retry, true));
$errorNode->appendChild($error);

// adding the message node
$error = $errorNode->ownerDocument->createElementNS('o:','o:reason', $this->getMessage());
$errorNode->appendChild($error);
}
}
12 changes: 12 additions & 0 deletions apps/dav/lib/connector/sabre/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
use OC\Files\Filesystem;
use OCA\DAV\Connector\Sabre\Exception\EntityTooLarge;
use OCA\DAV\Connector\Sabre\Exception\FileLocked;
use OCA\DAV\Connector\Sabre\Exception\Forbidden as DAVForbiddenException;
use OCA\DAV\Connector\Sabre\Exception\UnsupportedMediaType;
use OCP\Encryption\Exceptions\GenericEncryptionException;
use OCP\Files\EntityTooLargeException;
use OCP\Files\ForbiddenException;
use OCP\Files\InvalidContentException;
use OCP\Files\InvalidPathException;
use OCP\Files\LockNotAcquiredException;
Expand Down Expand Up @@ -175,6 +177,8 @@ public function put($data) {
\OCP\Util::writeLog('webdav', 'renaming part file to final file failed', \OCP\Util::ERROR);
throw new Exception('Could not rename part file to final file');
}
} catch (ForbiddenException $ex) {
throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry());
} catch (\Exception $e) {
$partStorage->unlink($internalPartPath);
$this->convertToSabreException($e);
Expand Down Expand Up @@ -273,6 +277,8 @@ public function get() {
throw new ServiceUnavailable("Encryption not ready: " . $e->getMessage());
} catch (StorageNotAvailableException $e) {
throw new ServiceUnavailable("Failed to open file: " . $e->getMessage());
} catch (ForbiddenException $ex) {
throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry());
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}
Expand All @@ -296,6 +302,8 @@ public function delete() {
}
} catch (StorageNotAvailableException $e) {
throw new ServiceUnavailable("Failed to unlink: " . $e->getMessage());
} catch (ForbiddenException $ex) {
throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry());
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}
Expand Down Expand Up @@ -474,6 +482,10 @@ private function convertToSabreException(\Exception $e) {
// a more general case - due to whatever reason the content could not be written
throw new Forbidden($e->getMessage(), 0, $e);
}
if ($e instanceof ForbiddenException) {
// the path for the file was forbidden
throw new DAVForbiddenException($e->getMessage(), $e->getRetry(), $e);
}
if ($e instanceof EntityTooLargeException) {
// the file is too big to be stored
throw new EntityTooLarge($e->getMessage(), 0, $e);
Expand Down
6 changes: 6 additions & 0 deletions apps/dav/lib/connector/sabre/objecttree.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@

namespace OCA\DAV\Connector\Sabre;

use OCA\DAV\Connector\Sabre\Exception\Forbidden;
use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
use OCA\DAV\Connector\Sabre\Exception\FileLocked;
use OC\Files\FileInfo;
use OC\Files\Mount\MoveableMount;
use OCP\Files\ForbiddenException;
use OCP\Files\StorageInvalidException;
use OCP\Files\StorageNotAvailableException;
use OCP\Lock\LockedException;
Expand Down Expand Up @@ -235,6 +237,8 @@ public function move($sourcePath, $destinationPath) {
}
} catch (StorageNotAvailableException $e) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
} catch (ForbiddenException $ex) {
throw new Forbidden($ex->getMessage(), $ex->getRetry());
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}
Expand Down Expand Up @@ -274,6 +278,8 @@ public function copy($source, $destination) {
$this->fileView->copy($source, $destination);
} catch (StorageNotAvailableException $e) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
} catch (ForbiddenException $ex) {
throw new Forbidden($ex->getMessage(), $ex->getRetry());
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}
Expand Down
2 changes: 2 additions & 0 deletions lib/private/cache/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ public function gc() {
} catch (\OCP\Lock\LockedException $e) {
// ignore locked chunks
\OC::$server->getLogger()->debug('Could not cleanup locked chunk "' . $file . '"', array('app' => 'core'));
} catch (\OCP\Files\ForbiddenException $e) {
\OC::$server->getLogger()->debug('Could not cleanup forbidden chunk "' . $file . '"', array('app' => 'core'));
} catch (\OCP\Files\LockNotAcquiredException $e) {
\OC::$server->getLogger()->debug('Could not cleanup locked chunk "' . $file . '"', array('app' => 'core'));
}
Expand Down
5 changes: 5 additions & 0 deletions lib/private/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ public static function get($dir, $files, $onlyHeader = false) {
$l = \OC::$server->getL10N('core');
$hint = method_exists($ex, 'getHint') ? $ex->getHint() : '';
\OC_Template::printErrorPage($l->t('File is currently busy, please try again later'), $hint);
} catch (\OCP\Files\ForbiddenException $ex) {
self::unlockAllTheFiles($dir, $files, $getType, $view, $filename);
OC::$server->getLogger()->logException($ex);
$l = \OC::$server->getL10N('core');
\OC_Template::printErrorPage($l->t('Can\'t read file'), $ex->getMessage());
} catch (\Exception $ex) {
self::unlockAllTheFiles($dir, $files, $getType, $view, $filename);
OC::$server->getLogger()->logException($ex);
Expand Down
2 changes: 2 additions & 0 deletions lib/private/files/cache/scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,8 @@ public function backgroundScan() {
// skip unavailable storages
} catch (\OCP\Files\StorageNotAvailableException $e) {
// skip unavailable storages
} catch (\OCP\Files\ForbiddenException $e) {
// skip forbidden storages
} catch (\OCP\Lock\LockedException $e) {
// skip unavailable storages
}
Expand Down
55 changes: 55 additions & 0 deletions lib/public/files/forbiddenexception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* @author Joas Schilling <nickvergessen@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @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,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

// use OCP namespace for all classes that are considered public.
// This means that they should be used by apps instead of the internal ownCloud classes
namespace OCP\Files;

/**
* Class ForbiddenException
*
* @package OCP\Files
* @since 9.0.0
*/
class ForbiddenException extends \Exception {

/** @var bool */
private $retry;

/**
* @param string $message
* @param bool $retry
* @param \Exception $previous previous exception for cascading
* @since 9.0.0
*/
public function __construct($message, $retry, \Exception $previous = null) {
parent::__construct($message, 0, $previous);
$this->retry = $retry;
}

/**
* @return bool
* @since 9.0.0
*/
public function getRetry() {
return (bool) $this->retry;
}
}

0 comments on commit 0ab8a4c

Please sign in to comment.