[Pkg-owncloud-commits] [owncloud] 01/15: Convert StorageNotAvailableException to SabreDAV exception

David Prévot taffit at moszumanska.debian.org
Thu Nov 13 18:17:33 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository owncloud.

commit 2db69a5328e9ec5f2ba36ab2a1545e2ea5e9c042
Author: Vincent Petry <pvince81 at owncloud.com>
Date:   Thu Nov 6 10:59:36 2014 +0100

    Convert StorageNotAvailableException to SabreDAV exception
    
    Convert \OCP\Files\StorageNotAvailableException to
    \Sabre\DAV\Exception\ServiceUnavailable for every file/directory
    operation happening inside of SabreDAV.
    
    This is necessary to avoid having the exception bubble up to remote.php
    which would return an exception page instead of an appropriate response.
    
    Conflicts:
    	lib/private/connector/sabre/directory.php
    	lib/private/connector/sabre/file.php
---
 lib/private/connector/sabre/directory.php   |  68 ++++++++------
 lib/private/connector/sabre/file.php        | 138 ++++++++++++++++------------
 lib/private/connector/sabre/objecttree.php  |  58 +++++++-----
 lib/private/connector/sabre/quotaplugin.php |   8 +-
 4 files changed, 158 insertions(+), 114 deletions(-)

diff --git a/lib/private/connector/sabre/directory.php b/lib/private/connector/sabre/directory.php
index 0080722..06e1d80 100644
--- a/lib/private/connector/sabre/directory.php
+++ b/lib/private/connector/sabre/directory.php
@@ -51,29 +51,33 @@ class OC_Connector_Sabre_Directory extends OC_Connector_Sabre_Node
 	 */
 	public function createFile($name, $data = null) {
 
-		// for chunked upload also updating a existing file is a "createFile"
-		// because we create all the chunks before re-assemble them to the existing file.
-		if (isset($_SERVER['HTTP_OC_CHUNKED'])) {
-
-			// exit if we can't create a new file and we don't updatable existing file
-			$info = OC_FileChunking::decodeName($name);
-			if (!$this->fileView->isCreatable($this->path) &&
-					!$this->fileView->isUpdatable($this->path . '/' . $info['name'])) {
-				throw new \Sabre\DAV\Exception\Forbidden();
-			}
+		try {
+			// for chunked upload also updating a existing file is a "createFile"
+			// because we create all the chunks before re-assemble them to the existing file.
+			if (isset($_SERVER['HTTP_OC_CHUNKED'])) {
+
+				// exit if we can't create a new file and we don't updatable existing file
+				$info = OC_FileChunking::decodeName($name);
+				if (!$this->fileView->isCreatable($this->path) &&
+						!$this->fileView->isUpdatable($this->path . '/' . $info['name'])) {
+					throw new \Sabre\DAV\Exception\Forbidden();
+				}
 
-		} 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();
+			} 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();
+				}
 			}
-		}
 
-		$path = $this->fileView->getAbsolutePath($this->path) . '/' . $name;
-		// using a dummy FileInfo is acceptable here since it will be refreshed after the put is complete
-		$info = new \OC\Files\FileInfo($path, null, null, array());
-		$node = new OC_Connector_Sabre_File($this->fileView, $info);
-		return $node->put($data);
+			$path = $this->fileView->getAbsolutePath($this->path) . '/' . $name;
+			// using a dummy FileInfo is acceptable here since it will be refreshed after the put is complete
+			$info = new \OC\Files\FileInfo($path, null, null, array());
+			$node = new OC_Connector_Sabre_File($this->fileView, $info);
+			return $node->put($data);
+		} catch (\OCP\Files\StorageNotAvailableException $e) {
+			throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
+		}
 	}
 
 	/**
@@ -84,15 +88,18 @@ class OC_Connector_Sabre_Directory extends OC_Connector_Sabre_Node
 	 * @return void
 	 */
 	public function createDirectory($name) {
-		if (!$this->fileView->isCreatable($this->path)) {
-			throw new \Sabre\DAV\Exception\Forbidden();
-		}
+		try {
+			if (!$this->fileView->isCreatable($this->path)) {
+				throw new \Sabre\DAV\Exception\Forbidden();
+			}
 
-		$newPath = $this->path . '/' . $name;
-		if(!$this->fileView->mkdir($newPath)) {
-			throw new \Sabre\DAV\Exception\Forbidden('Could not create directory '.$newPath);
+			$newPath = $this->path . '/' . $name;
+			if(!$this->fileView->mkdir($newPath)) {
+				throw new \Sabre\DAV\Exception\Forbidden('Could not create directory '.$newPath);
+			}
+		} catch (\OCP\Files\StorageNotAvailableException $e) {
+			throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
 		}
-
 	}
 
 	/**
@@ -104,10 +111,13 @@ class OC_Connector_Sabre_Directory extends OC_Connector_Sabre_Node
 	 * @return \Sabre\DAV\INode
 	 */
 	public function getChild($name, $info = null) {
-
 		$path = $this->path . '/' . $name;
 		if (is_null($info)) {
-			$info = $this->fileView->getFileInfo($path);
+			try {
+				$info = $this->fileView->getFileInfo($path);
+			} catch (\OCP\Files\StorageNotAvailableException $e) {
+				throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
+			}
 		}
 
 		if (!$info) {
diff --git a/lib/private/connector/sabre/file.php b/lib/private/connector/sabre/file.php
index 13b3338..64f05f7 100644
--- a/lib/private/connector/sabre/file.php
+++ b/lib/private/connector/sabre/file.php
@@ -50,9 +50,13 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements \Sabre\
 	 * @return string|null
 	 */
 	public function put($data) {
-		if ($this->info && $this->fileView->file_exists($this->path) &&
-			!$this->info->isUpdateable()) {
-			throw new \Sabre\DAV\Exception\Forbidden();
+		try {
+			if ($this->info && $this->fileView->file_exists($this->path) &&
+				!$this->info->isUpdateable()) {
+				throw new \Sabre\DAV\Exception\Forbidden();
+			}
+		} catch (\OCP\Files\StorageNotAvailableException $e) {
+			throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
 		}
 
 		// throw an exception if encryption was disabled but the files are still encrypted
@@ -100,42 +104,48 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements \Sabre\
 		} catch (\OCP\Files\LockNotAcquiredException $e) {
 			// the file is currently being written to by another process
 			throw new OC_Connector_Sabre_Exception_FileLocked($e->getMessage(), $e->getCode(), $e);
+		} catch (\OCP\Files\StorageNotAvailableException $e) {
+			throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
 		}
 
-		// double check if the file was fully received
-		// compare expected and actual size
-		if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] !== 'LOCK') {
-			$expected = $_SERVER['CONTENT_LENGTH'];
-			$actual = $this->fileView->filesize($partFilePath);
-			if ($actual != $expected) {
-				$this->fileView->unlink($partFilePath);
-				throw new \Sabre\DAV\Exception\BadRequest('expected filesize ' . $expected . ' got ' . $actual);
+		try {
+			// double check if the file was fully received
+			// compare expected and actual size
+			if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] !== 'LOCK') {
+				$expected = $_SERVER['CONTENT_LENGTH'];
+				$actual = $this->fileView->filesize($partFilePath);
+				if ($actual != $expected) {
+					$this->fileView->unlink($partFilePath);
+					throw new \Sabre\DAV\Exception\BadRequest('expected filesize ' . $expected . ' got ' . $actual);
+				}
 			}
-		}
 
-		// rename to correct path
-		try {
-			$renameOkay = $this->fileView->rename($partFilePath, $this->path);
-			$fileExists = $this->fileView->file_exists($this->path);
-			if ($renameOkay === false || $fileExists === false) {
-				\OC_Log::write('webdav', '\OC\Files\Filesystem::rename() failed', \OC_Log::ERROR);
-				$this->fileView->unlink($partFilePath);
-				throw new \Sabre\DAV\Exception('Could not rename part file to final file');
+			// rename to correct path
+			try {
+				$renameOkay = $this->fileView->rename($partFilePath, $this->path);
+				$fileExists = $this->fileView->file_exists($this->path);
+				if ($renameOkay === false || $fileExists === false) {
+					\OC_Log::write('webdav', '\OC\Files\Filesystem::rename() failed', \OC_Log::ERROR);
+					$this->fileView->unlink($partFilePath);
+					throw new \Sabre\DAV\Exception('Could not rename part file to final file');
+				}
+			}
+			catch (\OCP\Files\LockNotAcquiredException $e) {
+				// the file is currently being written to by another process
+				throw new OC_Connector_Sabre_Exception_FileLocked($e->getMessage(), $e->getCode(), $e);
 			}
-		}
-		catch (\OCP\Files\LockNotAcquiredException $e) {
-			// the file is currently being written to by another process
-			throw new OC_Connector_Sabre_Exception_FileLocked($e->getMessage(), $e->getCode(), $e);
-		}
 
-		// allow sync clients to send the mtime along in a header
-		$mtime = OC_Request::hasModificationTime();
-		if ($mtime !== false) {
-			if($this->fileView->touch($this->path, $mtime)) {
-				header('X-OC-MTime: accepted');
+			// allow sync clients to send the mtime along in a header
+			$mtime = OC_Request::hasModificationTime();
+			if ($mtime !== false) {
+				if($this->fileView->touch($this->path, $mtime)) {
+					header('X-OC-MTime: accepted');
+				}
 			}
+			$this->refreshInfo();
+		} catch (\OCP\Files\StorageNotAvailableException $e) {
+			throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
 		}
-		$this->refreshInfo();
 
 		return '"' . $this->info->getEtag() . '"';
 	}
@@ -151,7 +161,11 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements \Sabre\
 		if (\OC_Util::encryptedFiles()) {
 			throw new \Sabre\DAV\Exception\ServiceUnavailable();
 		} else {
-			return $this->fileView->fopen(ltrim($this->path, '/'), 'rb');
+			try {
+				return $this->fileView->fopen(ltrim($this->path, '/'), 'rb');
+			} catch (\OCP\Files\StorageNotAvailableException $e) {
+				throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
+			}
 		}
 
 	}
@@ -167,9 +181,13 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements \Sabre\
 			throw new \Sabre\DAV\Exception\Forbidden();
 		}
 
-		if (!$this->fileView->unlink($this->path)) {
-			// assume it wasn't possible to delete due to permissions
-			throw new \Sabre\DAV\Exception\Forbidden();
+		try {
+			if (!$this->fileView->unlink($this->path)) {
+				// assume it wasn't possible to delete due to permissions
+				throw new \Sabre\DAV\Exception\Forbidden();
+			}
+		} catch (\OCP\Files\StorageNotAvailableException $e) {
+			throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
 		}
 
 		// remove properties
@@ -243,33 +261,37 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements \Sabre\
 
 		if ($chunk_handler->isComplete()) {
 
-			// we first assembly the target file as a part file
-			$partFile = $path . '/' . $info['name'] . '.ocTransferId' . $info['transferid'] . '.part';
-			$chunk_handler->file_assemble($partFile);
-
-			// here is the final atomic rename
-			$targetPath = $path . '/' . $info['name'];
-			$renameOkay = $this->fileView->rename($partFile, $targetPath);
-			$fileExists = $this->fileView->file_exists($targetPath);
-			if ($renameOkay === false || $fileExists === false) {
-				\OC_Log::write('webdav', '\OC\Files\Filesystem::rename() failed', \OC_Log::ERROR);
-				// only delete if an error occurred and the target file was already created
-				if ($fileExists) {
-					$this->fileView->unlink($targetPath);
+			try {
+				// we first assembly the target file as a part file
+				$partFile = $path . '/' . $info['name'] . '.ocTransferId' . $info['transferid'] . '.part';
+				$chunk_handler->file_assemble($partFile);
+
+				// here is the final atomic rename
+				$targetPath = $path . '/' . $info['name'];
+				$renameOkay = $this->fileView->rename($partFile, $targetPath);
+				$fileExists = $this->fileView->file_exists($targetPath);
+				if ($renameOkay === false || $fileExists === false) {
+					\OC_Log::write('webdav', '\OC\Files\Filesystem::rename() failed', \OC_Log::ERROR);
+					// only delete if an error occurred and the target file was already created
+					if ($fileExists) {
+						$this->fileView->unlink($targetPath);
+					}
+					throw new \Sabre\DAV\Exception('Could not rename part file assembled from chunks');
 				}
-				throw new \Sabre\DAV\Exception('Could not rename part file assembled from chunks');
-			}
 
-			// allow sync clients to send the mtime along in a header
-			$mtime = OC_Request::hasModificationTime();
-			if ($mtime !== false) {
-				if($this->fileView->touch($targetPath, $mtime)) {
-					header('X-OC-MTime: accepted');
+				// allow sync clients to send the mtime along in a header
+				$mtime = OC_Request::hasModificationTime();
+				if ($mtime !== false) {
+					if($this->fileView->touch($targetPath, $mtime)) {
+						header('X-OC-MTime: accepted');
+					}
 				}
-			}
 
-			$info = $this->fileView->getFileInfo($targetPath);
-			return $info->getEtag();
+				$info = $this->fileView->getFileInfo($targetPath);
+				return $info->getEtag();
+			} catch (\OCP\Files\StorageNotAvailableException $e) {
+				throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
+			}
 		}
 
 		return null;
diff --git a/lib/private/connector/sabre/objecttree.php b/lib/private/connector/sabre/objecttree.php
index d7a96cf..14a55b5 100644
--- a/lib/private/connector/sabre/objecttree.php
+++ b/lib/private/connector/sabre/objecttree.php
@@ -138,27 +138,31 @@ class ObjectTree extends \Sabre\DAV\ObjectTree {
 			$isMovableMount = true;
 		}
 
-		// check update privileges
-		if (!$this->fileView->isUpdatable($sourcePath) && !$isMovableMount) {
-			throw new \Sabre\DAV\Exception\Forbidden();
-		}
-		if ($sourceDir !== $destinationDir) {
-			if (!$this->fileView->isCreatable($destinationDir)) {
+		try {
+			// check update privileges
+			if (!$this->fileView->isUpdatable($sourcePath) && !$isMovableMount) {
 				throw new \Sabre\DAV\Exception\Forbidden();
 			}
-			if (!$this->fileView->isDeletable($sourcePath) && !$isMovableMount) {
-				throw new \Sabre\DAV\Exception\Forbidden();
+			if ($sourceDir !== $destinationDir) {
+				if (!$this->fileView->isCreatable($destinationDir)) {
+					throw new \Sabre\DAV\Exception\Forbidden();
+				}
+				if (!$this->fileView->isDeletable($sourcePath) && !$isMovableMount) {
+					throw new \Sabre\DAV\Exception\Forbidden();
+				}
 			}
-		}
 
-		$fileName = basename($destinationPath);
-		if (!\OCP\Util::isValidFileName($fileName)) {
-			throw new \Sabre\DAV\Exception\BadRequest();
-		}
+			$fileName = basename($destinationPath);
+			if (!\OCP\Util::isValidFileName($fileName)) {
+				throw new \Sabre\DAV\Exception\BadRequest();
+			}
 
-		$renameOkay = $this->fileView->rename($sourcePath, $destinationPath);
-		if (!$renameOkay) {
-			throw new \Sabre\DAV\Exception\Forbidden('');
+			$renameOkay = $this->fileView->rename($sourcePath, $destinationPath);
+			if (!$renameOkay) {
+				throw new \Sabre\DAV\Exception\Forbidden('');
+			}
+		} catch (\OCP\Files\StorageNotAvailableException $e) {
+			throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
 		}
 
 		// update properties
@@ -188,19 +192,23 @@ class ObjectTree extends \Sabre\DAV\ObjectTree {
 			throw new \Sabre\DAV\Exception\ServiceUnavailable('filesystem not setup');
 		}
 
-		if ($this->fileView->is_file($source)) {
-			$this->fileView->copy($source, $destination);
-		} else {
-			$this->fileView->mkdir($destination);
-			$dh = $this->fileView->opendir($source);
-			if (is_resource($dh)) {
-				while (($subNode = readdir($dh)) !== false) {
+		try {
+			if ($this->fileView->is_file($source)) {
+				$this->fileView->copy($source, $destination);
+			} else {
+				$this->fileView->mkdir($destination);
+				$dh = $this->fileView->opendir($source);
+				if (is_resource($dh)) {
+					while (($subNode = readdir($dh)) !== false) {
 
-					if ($subNode == '.' || $subNode == '..') continue;
-					$this->copy($source . '/' . $subNode, $destination . '/' . $subNode);
+						if ($subNode == '.' || $subNode == '..') continue;
+						$this->copy($source . '/' . $subNode, $destination . '/' . $subNode);
 
+					}
 				}
 			}
+		} catch (\OCP\Files\StorageNotAvailableException $e) {
+			throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
 		}
 
 		list($destinationDir,) = \Sabre\DAV\URLUtil::splitPath($destination);
diff --git a/lib/private/connector/sabre/quotaplugin.php b/lib/private/connector/sabre/quotaplugin.php
index cf3c110..6dab64d 100644
--- a/lib/private/connector/sabre/quotaplugin.php
+++ b/lib/private/connector/sabre/quotaplugin.php
@@ -102,7 +102,11 @@ class OC_Connector_Sabre_QuotaPlugin extends \Sabre\DAV\ServerPlugin {
 	 * @return mixed
 	 */
 	public function getFreeSpace($parentUri) {
-		$freeSpace = $this->view->free_space($parentUri);
-		return $freeSpace;
+		try {
+			$freeSpace = $this->view->free_space($parentUri);
+			return $freeSpace;
+		} catch (\OCP\Files\StorageNotAvailableException $e) {
+			throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
+		}
 	}
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git



More information about the Pkg-owncloud-commits mailing list