Merge pull request #12072 from owncloud/sabre-convertstoragenotavailableexception-secondtry

Convert StorageNotAvailableException to SabreDAV exception
This commit is contained in:
Vincent Petry 2014-11-10 12:52:52 +01:00
commit 9b99c1d6f0
4 changed files with 155 additions and 113 deletions

View File

@ -51,29 +51,33 @@ class OC_Connector_Sabre_Directory extends OC_Connector_Sabre_Node
*/ */
public function createFile($name, $data = null) { public function createFile($name, $data = null) {
// for chunked upload also updating a existing file is a "createFile" try {
// because we create all the chunks before re-assemble them to the existing file. // for chunked upload also updating a existing file is a "createFile"
if (isset($_SERVER['HTTP_OC_CHUNKED'])) { // 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 // exit if we can't create a new file and we don't updatable existing file
$info = OC_FileChunking::decodeName($name); $info = OC_FileChunking::decodeName($name);
if (!$this->fileView->isCreatable($this->path) && if (!$this->fileView->isCreatable($this->path) &&
!$this->fileView->isUpdatable($this->path . '/' . $info['name'])) { !$this->fileView->isUpdatable($this->path . '/' . $info['name'])) {
throw new \Sabre\DAV\Exception\Forbidden(); 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 { $path = $this->fileView->getAbsolutePath($this->path) . '/' . $name;
// For non-chunked upload it is enough to check if we can create a new file // using a dummy FileInfo is acceptable here since it will be refreshed after the put is complete
if (!$this->fileView->isCreatable($this->path)) { $info = new \OC\Files\FileInfo($path, null, null, array());
throw new \Sabre\DAV\Exception\Forbidden(); $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());
} }
$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);
} }
/** /**
@ -84,15 +88,18 @@ class OC_Connector_Sabre_Directory extends OC_Connector_Sabre_Node
* @return void * @return void
*/ */
public function createDirectory($name) { public function createDirectory($name) {
if (!$this->info->isCreatable()) { try {
throw new \Sabre\DAV\Exception\Forbidden(); if (!$this->info->isCreatable()) {
} throw new \Sabre\DAV\Exception\Forbidden();
}
$newPath = $this->path . '/' . $name; $newPath = $this->path . '/' . $name;
if(!$this->fileView->mkdir($newPath)) { if(!$this->fileView->mkdir($newPath)) {
throw new \Sabre\DAV\Exception\Forbidden('Could not create directory '.$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 * @return \Sabre\DAV\INode
*/ */
public function getChild($name, $info = null) { public function getChild($name, $info = null) {
$path = $this->path . '/' . $name; $path = $this->path . '/' . $name;
if (is_null($info)) { 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) { if (!$info) {

View File

@ -50,9 +50,13 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements \Sabre\
* @return string|null * @return string|null
*/ */
public function put($data) { public function put($data) {
if ($this->info && $this->fileView->file_exists($this->path) && try {
!$this->info->isUpdateable()) { if ($this->info && $this->fileView->file_exists($this->path) &&
throw new \Sabre\DAV\Exception\Forbidden(); !$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 // throw an exception if encryption was disabled but the files are still encrypted
@ -102,43 +106,49 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements \Sabre\
throw new OC_Connector_Sabre_Exception_FileLocked($e->getMessage(), $e->getCode(), $e); throw new OC_Connector_Sabre_Exception_FileLocked($e->getMessage(), $e->getCode(), $e);
} catch (\OCA\Encryption\Exception\EncryptionException $e) { } catch (\OCA\Encryption\Exception\EncryptionException $e) {
throw new \Sabre\DAV\Exception\Forbidden($e->getMessage()); throw new \Sabre\DAV\Exception\Forbidden($e->getMessage());
} catch (\OCP\Files\StorageNotAvailableException $e) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
} }
// if content length is sent by client:
// 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 { try {
$renameOkay = $this->fileView->rename($partFilePath, $this->path); // if content length is sent by client:
$fileExists = $this->fileView->file_exists($this->path); // double check if the file was fully received
if ($renameOkay === false || $fileExists === false) { // compare expected and actual size
\OC_Log::write('webdav', '\OC\Files\Filesystem::rename() failed', \OC_Log::ERROR); if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] !== 'LOCK') {
$this->fileView->unlink($partFilePath); $expected = $_SERVER['CONTENT_LENGTH'];
throw new \Sabre\DAV\Exception('Could not rename part file to final file'); $actual = $this->fileView->filesize($partFilePath);
if ($actual != $expected) {
$this->fileView->unlink($partFilePath);
throw new \Sabre\DAV\Exception\BadRequest('expected filesize ' . $expected . ' got ' . $actual);
}
} }
}
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 // rename to correct path
$mtime = OC_Request::hasModificationTime(); try {
if ($mtime !== false) { $renameOkay = $this->fileView->rename($partFilePath, $this->path);
if($this->fileView->touch($this->path, $mtime)) { $fileExists = $this->fileView->file_exists($this->path);
header('X-OC-MTime: accepted'); 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);
}
// 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() . '"'; return '"' . $this->info->getEtag() . '"';
} }
@ -158,6 +168,8 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements \Sabre\
return $this->fileView->fopen(ltrim($this->path, '/'), 'rb'); return $this->fileView->fopen(ltrim($this->path, '/'), 'rb');
} catch (\OCA\Encryption\Exception\EncryptionException $e) { } catch (\OCA\Encryption\Exception\EncryptionException $e) {
throw new \Sabre\DAV\Exception\Forbidden($e->getMessage()); throw new \Sabre\DAV\Exception\Forbidden($e->getMessage());
} catch (\OCP\Files\StorageNotAvailableException $e) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
} }
} }
@ -174,9 +186,13 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements \Sabre\
throw new \Sabre\DAV\Exception\Forbidden(); throw new \Sabre\DAV\Exception\Forbidden();
} }
if (!$this->fileView->unlink($this->path)) { try {
// assume it wasn't possible to delete due to permissions if (!$this->fileView->unlink($this->path)) {
throw new \Sabre\DAV\Exception\Forbidden(); // 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 // remove properties
@ -235,33 +251,37 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements \Sabre\
if ($chunk_handler->isComplete()) { if ($chunk_handler->isComplete()) {
// we first assembly the target file as a part file try {
$partFile = $path . '/' . $info['name'] . '.ocTransferId' . $info['transferid'] . '.part'; // we first assembly the target file as a part file
$chunk_handler->file_assemble($partFile); $partFile = $path . '/' . $info['name'] . '.ocTransferId' . $info['transferid'] . '.part';
$chunk_handler->file_assemble($partFile);
// here is the final atomic rename // here is the final atomic rename
$targetPath = $path . '/' . $info['name']; $targetPath = $path . '/' . $info['name'];
$renameOkay = $this->fileView->rename($partFile, $targetPath); $renameOkay = $this->fileView->rename($partFile, $targetPath);
$fileExists = $this->fileView->file_exists($targetPath); $fileExists = $this->fileView->file_exists($targetPath);
if ($renameOkay === false || $fileExists === false) { if ($renameOkay === false || $fileExists === false) {
\OC_Log::write('webdav', '\OC\Files\Filesystem::rename() failed', \OC_Log::ERROR); \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 // only delete if an error occurred and the target file was already created
if ($fileExists) { if ($fileExists) {
$this->fileView->unlink($targetPath); $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 // allow sync clients to send the mtime along in a header
$mtime = OC_Request::hasModificationTime(); $mtime = OC_Request::hasModificationTime();
if ($mtime !== false) { if ($mtime !== false) {
if($this->fileView->touch($targetPath, $mtime)) { if($this->fileView->touch($targetPath, $mtime)) {
header('X-OC-MTime: accepted'); header('X-OC-MTime: accepted');
}
} }
}
$info = $this->fileView->getFileInfo($targetPath); $info = $this->fileView->getFileInfo($targetPath);
return $info->getEtag(); return $info->getEtag();
} catch (\OCP\Files\StorageNotAvailableException $e) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
}
} }
return null; return null;

View File

@ -138,27 +138,31 @@ class ObjectTree extends \Sabre\DAV\ObjectTree {
$isMovableMount = true; $isMovableMount = true;
} }
// check update privileges try {
if (!$this->fileView->isUpdatable($sourcePath) && !$isMovableMount) { // check update privileges
throw new \Sabre\DAV\Exception\Forbidden(); if (!$this->fileView->isUpdatable($sourcePath) && !$isMovableMount) {
}
if ($sourceDir !== $destinationDir) {
if (!$this->fileView->isCreatable($destinationDir)) {
throw new \Sabre\DAV\Exception\Forbidden(); throw new \Sabre\DAV\Exception\Forbidden();
} }
if (!$this->fileView->isDeletable($sourcePath) && !$isMovableMount) { if ($sourceDir !== $destinationDir) {
throw new \Sabre\DAV\Exception\Forbidden(); 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); $fileName = basename($destinationPath);
if (!\OCP\Util::isValidFileName($fileName)) { if (!\OCP\Util::isValidFileName($fileName)) {
throw new \Sabre\DAV\Exception\BadRequest(); throw new \Sabre\DAV\Exception\BadRequest();
} }
$renameOkay = $this->fileView->rename($sourcePath, $destinationPath); $renameOkay = $this->fileView->rename($sourcePath, $destinationPath);
if (!$renameOkay) { if (!$renameOkay) {
throw new \Sabre\DAV\Exception\Forbidden(''); throw new \Sabre\DAV\Exception\Forbidden('');
}
} catch (\OCP\Files\StorageNotAvailableException $e) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
} }
// update properties // update properties
@ -188,19 +192,23 @@ class ObjectTree extends \Sabre\DAV\ObjectTree {
throw new \Sabre\DAV\Exception\ServiceUnavailable('filesystem not setup'); throw new \Sabre\DAV\Exception\ServiceUnavailable('filesystem not setup');
} }
if ($this->fileView->is_file($source)) { try {
$this->fileView->copy($source, $destination); if ($this->fileView->is_file($source)) {
} else { $this->fileView->copy($source, $destination);
$this->fileView->mkdir($destination); } else {
$dh = $this->fileView->opendir($source); $this->fileView->mkdir($destination);
if (is_resource($dh)) { $dh = $this->fileView->opendir($source);
while (($subNode = readdir($dh)) !== false) { if (is_resource($dh)) {
while (($subNode = readdir($dh)) !== false) {
if ($subNode == '.' || $subNode == '..') continue; if ($subNode == '.' || $subNode == '..') continue;
$this->copy($source . '/' . $subNode, $destination . '/' . $subNode); $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); list($destinationDir,) = \Sabre\DAV\URLUtil::splitPath($destination);

View File

@ -102,7 +102,11 @@ class OC_Connector_Sabre_QuotaPlugin extends \Sabre\DAV\ServerPlugin {
* @return mixed * @return mixed
*/ */
public function getFreeSpace($parentUri) { public function getFreeSpace($parentUri) {
$freeSpace = $this->view->free_space($parentUri); try {
return $freeSpace; $freeSpace = $this->view->free_space($parentUri);
return $freeSpace;
} catch (\OCP\Files\StorageNotAvailableException $e) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
}
} }
} }