Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable10] Improve doc blocks, import classes #29272

Merged
merged 1 commit into from
Oct 19, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 93 additions & 75 deletions apps/dav/lib/Connector/Sabre/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,36 @@
*/
namespace OCA\DAV\Connector\Sabre;

use OC\Files\FileInfo;
use OC\Files\Filesystem;
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\Files\InvalidPathException;
use OCP\Files\StorageNotAvailableException;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use Sabre\DAV\Exception\Locked;
use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\Exception\Locked as SabreLocked;
use Sabre\DAV\Exception\NotFound as SabreNotFound;
use Sabre\DAV\Exception\ServiceUnavailable as SabreServiceUnavailable;
use Sabre\DAV\Exception\Forbidden as SabreForbidden;
use Sabre\DAV\ICollection;
use Sabre\DAV\IMoveTarget;
use Sabre\DAV\INode;
use Sabre\DAV\Exception\BadRequest;
use OC\Files\Mount\MoveableMount;
use Sabre\DAV\IFile;
use OCA\DAV\Upload\FutureFile;
use Sabre\DAV\IQuota;
use Sabre\HTTP\URLUtil;

class Directory extends \OCA\DAV\Connector\Sabre\Node
implements \Sabre\DAV\ICollection, \Sabre\DAV\IQuota, \Sabre\DAV\IMoveTarget {
class Directory extends Node implements ICollection, IQuota, IMoveTarget {

/**
* Cached directory content
*
* @var \OCP\Files\FileInfo[]
* @var INode[]
*/
private $dirContent;

Expand Down Expand Up @@ -108,22 +117,22 @@ public function __construct($view, $info, $tree = null, $shareManager = null) {
* @throws InvalidPath
* @throws \Sabre\DAV\Exception
* @throws \Sabre\DAV\Exception\BadRequest
* @throws \Sabre\DAV\Exception\Forbidden
* @throws \Sabre\DAV\Exception\ServiceUnavailable
* @throws SabreForbidden
* @throws ServiceUnavailable
*/
public function createFile($name, $data = null) {

# the check here is necessary, because createFile uses put covered in sabre/file.php
# and not touch covered in files/view.php
if (\OC\Files\Filesystem::isForbiddenFileOrDir($name)) {
throw new \Sabre\DAV\Exception\Forbidden();
if (Filesystem::isForbiddenFileOrDir($name)) {
throw new SabreForbidden();
}

try {
# the check here is necessary, because createFile uses put covered in sabre/file.php
# and not touch covered in files/view.php
if (\OC\Files\Filesystem::isForbiddenFileOrDir($name)) {
throw new \Sabre\DAV\Exception\Forbidden();
if (Filesystem::isForbiddenFileOrDir($name)) {
throw new SabreForbidden();
}

$info = false;
Expand All @@ -136,15 +145,15 @@ public function createFile($name, $data = null) {
if (!$this->fileView->isCreatable($this->path) &&
!$this->fileView->isUpdatable($this->path . '/' . $chunkInfo['name'])
) {
throw new \Sabre\DAV\Exception\Forbidden();
throw new SabreForbidden();
}
} else if (FutureFile::isFutureFile()) {
// Future file (chunked upload) requires fileinfo
$info = $this->fileView->getFileInfo($this->path . '/' . $name);
} else {
// For non-chunked upload it is enough to check if we can create a new file
if (!$this->fileView->isCreatable($this->path)) {
throw new \Sabre\DAV\Exception\Forbidden();
throw new SabreForbidden();
}
}

Expand All @@ -154,15 +163,15 @@ public function createFile($name, $data = null) {

if (!$info) {
// use a dummy FileInfo which is acceptable here since it will be refreshed after the put is complete
$info = new \OC\Files\FileInfo($path, null, null, [], null);
$info = new FileInfo($path, null, null, [], null);
}

$node = new \OCA\DAV\Connector\Sabre\File($this->fileView, $info);
$node = new File($this->fileView, $info);
$node->acquireLock(ILockingProvider::LOCK_SHARED);
return $node->put($data);
} catch (\OCP\Files\StorageNotAvailableException $e) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
} catch (\OCP\Files\InvalidPathException $ex) {
} catch (StorageNotAvailableException $e) {
throw new SabreServiceUnavailable($e->getMessage());
} catch (InvalidPathException $ex) {
throw new InvalidPath($ex->getMessage());
} catch (ForbiddenException $ex) {
throw new Forbidden($ex->getMessage(), $ex->getRetry());
Expand All @@ -177,34 +186,34 @@ public function createFile($name, $data = null) {
* @param string $name
* @throws FileLocked
* @throws InvalidPath
* @throws \Sabre\DAV\Exception\Forbidden
* @throws \Sabre\DAV\Exception\ServiceUnavailable
* @throws SabreForbidden
* @throws SabreServiceUnavailable
*/
public function createDirectory($name) {

# the check here is necessary, because createDirectory does not use the methods in files/view.php
if (\OC\Files\Filesystem::isForbiddenFileOrDir($name)) {
throw new \Sabre\DAV\Exception\Forbidden();
if (Filesystem::isForbiddenFileOrDir($name)) {
throw new SabreForbidden();
}

try {
# the check here is necessary, because createDirectory does not use the methods in files/view.php
if (\OC\Files\Filesystem::isForbiddenFileOrDir($name)) {
throw new \Sabre\DAV\Exception\Forbidden();
if (Filesystem::isForbiddenFileOrDir($name)) {
throw new SabreForbidden();
}

if (!$this->info->isCreatable()) {
throw new \Sabre\DAV\Exception\Forbidden();
throw new SabreForbidden();
}

$this->fileView->verifyPath($this->path, $name);
$newPath = $this->path . '/' . $name;
if (!$this->fileView->mkdir($newPath)) {
throw new \Sabre\DAV\Exception\Forbidden('Could not create directory ' . $newPath);
throw new SabreForbidden('Could not create directory ' . $newPath);
}
} catch (\OCP\Files\StorageNotAvailableException $e) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
} catch (\OCP\Files\InvalidPathException $ex) {
} catch (StorageNotAvailableException $e) {
throw new SabreServiceUnavailable($e->getMessage());
} catch (InvalidPathException $ex) {
throw new InvalidPath($ex->getMessage());
} catch (ForbiddenException $ex) {
throw new Forbidden($ex->getMessage(), $ex->getRetry());
Expand All @@ -220,37 +229,38 @@ public function createDirectory($name) {
* @param \OCP\Files\FileInfo $info
* @return \Sabre\DAV\INode
* @throws InvalidPath
* @throws \Sabre\DAV\Exception\NotFound
* @throws \Sabre\DAV\Exception\ServiceUnavailable
* @throws SabreNotFound
* @throws SabreServiceUnavailable
* @throws SabreForbidden
*/
public function getChild($name, $info = null) {
if (!$this->info->isReadable()) {
// avoid detecting files through this way
throw new NotFound();
throw new SabreNotFound();
}

$path = $this->path . '/' . $name;
if (is_null($info)) {
try {
$this->fileView->verifyPath($this->path, $name);
$info = $this->fileView->getFileInfo($path);
} catch (\OCP\Files\StorageNotAvailableException $e) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
} catch (\OCP\Files\InvalidPathException $ex) {
} catch (StorageNotAvailableException $e) {
throw new SabreServiceUnavailable($e->getMessage());
} catch (InvalidPathException $ex) {
throw new InvalidPath($ex->getMessage());
} catch (ForbiddenException $e) {
throw new \Sabre\DAV\Exception\Forbidden();
throw new SabreForbidden();
}
}

if (!$info) {
throw new \Sabre\DAV\Exception\NotFound('File with name ' . $path . ' could not be located');
throw new SabreNotFound('File with name ' . $path . ' could not be located');
}

if ($info['mimetype'] == 'httpd/unix-directory') {
$node = new \OCA\DAV\Connector\Sabre\Directory($this->fileView, $info, $this->tree, $this->shareManager);
$node = new Directory($this->fileView, $info, $this->tree, $this->shareManager);
} else {
$node = new \OCA\DAV\Connector\Sabre\File($this->fileView, $info, $this->shareManager);
$node = new File($this->fileView, $info, $this->shareManager);
}
if ($this->tree) {
$this->tree->cacheNode($node);
Expand All @@ -262,6 +272,8 @@ public function getChild($name, $info = null) {
* Returns an array with all the child nodes
*
* @return \Sabre\DAV\INode[]
* @throws Forbidden
* @throws SabreLocked
*/
public function getChildren() {
if (!is_null($this->dirContent)) {
Expand All @@ -275,7 +287,7 @@ public function getChildren() {
}
$folderContent = $this->fileView->getDirectoryContent($this->path);
} catch (LockedException $e) {
throw new Locked();
throw new SabreLocked();
}

$nodes = [];
Expand Down Expand Up @@ -311,18 +323,18 @@ public function childExists($name) {
*
* @return void
* @throws FileLocked
* @throws \Sabre\DAV\Exception\Forbidden
* @throws SabreForbidden
*/
public function delete() {

if ($this->path === '' || $this->path === '/' || !$this->info->isDeletable()) {
throw new \Sabre\DAV\Exception\Forbidden();
throw new SabreForbidden();
}

try {
if (!$this->fileView->rmdir($this->path)) {
// assume it wasn't possible to remove due to permission issue
throw new \Sabre\DAV\Exception\Forbidden();
throw new SabreForbidden();
}
} catch (ForbiddenException $ex) {
throw new Forbidden($ex->getMessage(), $ex->getRetry());
Expand Down Expand Up @@ -352,31 +364,37 @@ public function getQuotaInfo() {
$free
];
return $this->quotaInfo;
} catch (\OCP\Files\StorageNotAvailableException $e) {
} catch (StorageNotAvailableException $e) {
return [0, 0];
}
}

/**
* Moves a node into this collection.
*
* It is up to the implementors to:
* 1. Create the new resource.
* 2. Remove the old resource.
* 3. Transfer any properties or other data.
*
* Generally you should make very sure that your collection can easily move
* the move.
*
* If you don't, just return false, which will trigger sabre/dav to handle
* the move itself. If you return true from this function, the assumption
* is that the move was successful.
*
* @param string $targetName New local file/collection name.
* @param string $fullSourcePath Full path to source node
* @param INode $sourceNode Source node itself
* @return bool
*/
/**
* Moves a node into this collection.
*
* It is up to the implementors to:
* 1. Create the new resource.
* 2. Remove the old resource.
* 3. Transfer any properties or other data.
*
* Generally you should make very sure that your collection can easily move
* the move.
*
* If you don't, just return false, which will trigger sabre/dav to handle
* the move itself. If you return true from this function, the assumption
* is that the move was successful.
*
* @param string $targetName New local file/collection name.
* @param string $fullSourcePath Full path to source node
* @param INode $sourceNode Source node itself
* @return bool
* @throws BadRequest
* @throws SabreServiceUnavailable
* @throws SabreForbidden
* @throws Forbidden
* @throws FileLocked
* @throws InvalidPath
*/
public function moveInto($targetName, $fullSourcePath, INode $sourceNode) {
if (!$sourceNode instanceof Node) {
// it's a file of another kind, like FutureFile
Expand All @@ -388,25 +406,25 @@ public function moveInto($targetName, $fullSourcePath, INode $sourceNode) {
}

if (!$this->fileView) {
throw new \Sabre\DAV\Exception\ServiceUnavailable('filesystem not setup');
throw new SabreServiceUnavailable('filesystem not setup');
}

$destinationPath = $this->getPath() . '/' . $targetName;

# check the destination path, for source see below
if (\OC\Files\Filesystem::isForbiddenFileOrDir($destinationPath)) {
throw new \Sabre\DAV\Exception\Forbidden();
if (Filesystem::isForbiddenFileOrDir($destinationPath)) {
throw new SabreForbidden();
}

$targetNodeExists = $this->childExists($targetName);

// at getNodeForPath we also check the path for isForbiddenFileOrDir
// with that we have covered both source and destination
if ($sourceNode instanceof Directory && $targetNodeExists) {
throw new \Sabre\DAV\Exception\Forbidden('Could not copy directory ' . $sourceNode->getName() . ', target exists');
throw new SabreForbidden('Could not copy directory ' . $sourceNode->getName() . ', target exists');
}

list($sourceDir,) = \Sabre\HTTP\URLUtil::splitPath($sourceNode->getPath());
list($sourceDir,) = URLUtil::splitPath($sourceNode->getPath());
$destinationDir = $this->getPath();

$sourcePath = $sourceNode->getPath();
Expand All @@ -424,35 +442,35 @@ public function moveInto($targetName, $fullSourcePath, INode $sourceNode) {
if ($targetNodeExists || $sameFolder) {
// note that renaming a share mount point is always allowed
if (!$this->fileView->isUpdatable($destinationDir) && !$isMovableMount) {
throw new \Sabre\DAV\Exception\Forbidden();
throw new SabreForbidden();
}
} else {
if (!$this->fileView->isCreatable($destinationDir)) {
throw new \Sabre\DAV\Exception\Forbidden();
throw new SabreForbidden();
}
}

if (!$sameFolder) {
// moving to a different folder, source will be gone, like a deletion
// note that moving a share mount point is always allowed
if (!$this->fileView->isDeletable($sourcePath) && !$isMovableMount) {
throw new \Sabre\DAV\Exception\Forbidden();
throw new SabreForbidden();
}
}

$fileName = basename($destinationPath);
try {
$this->fileView->verifyPath($destinationDir, $fileName);
} catch (\OCP\Files\InvalidPathException $ex) {
} catch (InvalidPathException $ex) {
throw new InvalidPath($ex->getMessage());
}

$renameOkay = $this->fileView->rename($sourcePath, $destinationPath);
if (!$renameOkay) {
throw new \Sabre\DAV\Exception\Forbidden('');
throw new SabreForbidden('');
}
} catch (StorageNotAvailableException $e) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
throw new SabreServiceUnavailable($e->getMessage());
} catch (ForbiddenException $ex) {
throw new Forbidden($ex->getMessage(), $ex->getRetry());
} catch (LockedException $e) {
Expand Down