Merge pull request #4974 from owncloud/fixing-4011-master

error handling added in case file_put_contents is failing
This commit is contained in:
Thomas Müller 2013-10-07 02:58:00 -07:00
commit 1e47468c53
4 changed files with 99 additions and 70 deletions

View File

@ -54,47 +54,10 @@ class OC_Connector_Sabre_Directory extends OC_Connector_Sabre_Node implements Sa
throw new \Sabre_DAV_Exception_Forbidden();
}
if (isset($_SERVER['HTTP_OC_CHUNKED'])) {
$info = OC_FileChunking::decodeName($name);
if (empty($info)) {
throw new Sabre_DAV_Exception_NotImplemented();
}
$chunk_handler = new OC_FileChunking($info);
$chunk_handler->store($info['index'], $data);
if ($chunk_handler->isComplete()) {
$newPath = $this->path . '/' . $info['name'];
$chunk_handler->file_assemble($newPath);
return OC_Connector_Sabre_Node::getETagPropertyForPath($newPath);
}
} else {
$newPath = $this->path . '/' . $name;
$path = $this->path . '/' . $name;
$node = new OC_Connector_Sabre_File($path);
return $node->put($data);
// mark file as partial while uploading (ignored by the scanner)
$partpath = $newPath . '.part';
\OC\Files\Filesystem::file_put_contents($partpath, $data);
// rename to correct path
$renameOkay = \OC\Files\Filesystem::rename($partpath, $newPath);
$fileExists = \OC\Files\Filesystem::file_exists($newPath);
if ($renameOkay === false || $fileExists === false) {
\OC_Log::write('webdav', '\OC\Files\Filesystem::rename() failed', \OC_Log::ERROR);
\OC\Files\Filesystem::unlink($partpath);
throw new Sabre_DAV_Exception();
}
// allow sync clients to send the mtime along in a header
$mtime = OC_Request::hasModificationTime();
if ($mtime !== false) {
if(\OC\Files\Filesystem::touch($newPath, $mtime)) {
header('X-OC-MTime: accepted');
}
}
return OC_Connector_Sabre_Node::getETagPropertyForPath($newPath);
}
return null;
}
/**
@ -243,13 +206,12 @@ class OC_Connector_Sabre_Directory extends OC_Connector_Sabre_Node implements Sa
* If the array is empty, all properties should be returned
*
* @param array $properties
* @return void
* @return array
*/
public function getProperties($properties) {
$props = parent::getProperties($properties);
if (in_array(self::GETETAG_PROPERTYNAME, $properties) && !isset($props[self::GETETAG_PROPERTYNAME])) {
$props[self::GETETAG_PROPERTYNAME]
= OC_Connector_Sabre_Node::getETagPropertyForPath($this->path);
$props[self::GETETAG_PROPERTYNAME] = $this->getETagPropertyForPath($this->path);
}
return $props;
}

View File

@ -28,7 +28,7 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements Sabre_D
*
* The data argument is a readable stream resource.
*
* After a succesful put operation, you may choose to return an ETag. The
* After a successful put operation, you may choose to return an ETag. The
* etag must always be surrounded by double-quotes. These quotes must
* appear in the actual string you're returning.
*
@ -45,8 +45,9 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements Sabre_D
* @return string|null
*/
public function put($data) {
if (!\OC\Files\Filesystem::isUpdatable($this->path)) {
$fs = $this->getFS();
if ($fs->file_exists($this->path) &&
!$fs->isUpdatable($this->path)) {
throw new \Sabre_DAV_Exception_Forbidden();
}
@ -54,44 +55,59 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements Sabre_D
if (\OC_Util::encryptedFiles()) {
throw new \Sabre_DAV_Exception_ServiceUnavailable();
}
// chunked handling
if (isset($_SERVER['HTTP_OC_CHUNKED'])) {
list(, $name) = \Sabre_DAV_URLUtil::splitPath($this->path);
$info = OC_FileChunking::decodeName($name);
if (empty($info)) {
throw new Sabre_DAV_Exception_NotImplemented();
}
$chunk_handler = new OC_FileChunking($info);
$chunk_handler->store($info['index'], $data);
if ($chunk_handler->isComplete()) {
$newPath = $this->path . '/' . $info['name'];
$chunk_handler->file_assemble($newPath);
return $this->getETagPropertyForPath($newPath);
}
return null;
}
// mark file as partial while uploading (ignored by the scanner)
$partpath = $this->path . '.part';
\OC\Files\Filesystem::file_put_contents($partpath, $data);
//detect aborted upload
if (isset ($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT') {
if (isset($_SERVER['CONTENT_LENGTH'])) {
$expected = $_SERVER['CONTENT_LENGTH'];
$actual = \OC\Files\Filesystem::filesize($partpath);
if ($actual != $expected) {
\OC\Files\Filesystem::unlink($partpath);
throw new Sabre_DAV_Exception_BadRequest(
'expected filesize ' . $expected . ' got ' . $actual);
}
try {
$putOkay = $fs->file_put_contents($partpath, $data);
if ($putOkay === false) {
\OC_Log::write('webdav', '\OC\Files\Filesystem::file_put_contents() failed', \OC_Log::ERROR);
$fs->unlink($partpath);
// because we have no clue about the cause we can only throw back a 500/Internal Server Error
throw new Sabre_DAV_Exception();
}
} catch (\OCP\Files\NotPermittedException $e) {
throw new Sabre_DAV_Exception_Forbidden();
}
// rename to correct path
$renameOkay = \OC\Files\Filesystem::rename($partpath, $this->path);
$fileExists = \OC\Files\Filesystem::file_exists($this->path);
$renameOkay = $fs->rename($partpath, $this->path);
$fileExists = $fs->file_exists($this->path);
if ($renameOkay === false || $fileExists === false) {
\OC_Log::write('webdav', '\OC\Files\Filesystem::rename() failed', \OC_Log::ERROR);
\OC\Files\Filesystem::unlink($partpath);
$fs->unlink($partpath);
throw new Sabre_DAV_Exception();
}
//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();
if ($mtime !== false) {
if (\OC\Files\Filesystem::touch($this->path, $mtime)) {
if($fs->touch($this->path, $mtime)) {
header('X-OC-MTime: accepted');
}
}
return OC_Connector_Sabre_Node::getETagPropertyForPath($this->path);
return $this->getETagPropertyForPath($this->path);
}
/**
@ -101,7 +117,7 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements Sabre_D
*/
public function get() {
//throw execption if encryption is disabled but files are still encrypted
//throw exception if encryption is disabled but files are still encrypted
if (\OC_Util::encryptedFiles()) {
throw new \Sabre_DAV_Exception_ServiceUnavailable();
} else {
@ -144,7 +160,7 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements Sabre_D
*
* An ETag is a unique identifier representing the current version of the
* file. If the file changes, the ETag MUST change. The ETag is an
* arbritrary string, but MUST be surrounded by double-quotes.
* arbitrary string, but MUST be surrounded by double-quotes.
*
* Return null if the ETag can not effectively be determined
*

View File

@ -32,6 +32,13 @@ abstract class OC_Connector_Sabre_Node implements Sabre_DAV_INode, Sabre_DAV_IPr
*/
public static $ETagFunction = null;
/**
* is kept public to allow overwrite for unit testing
*
* @var \OC\Files\View
*/
public $fileView;
/**
* The path to the current node
*
@ -234,12 +241,18 @@ abstract class OC_Connector_Sabre_Node implements Sabre_DAV_INode, Sabre_DAV_IPr
* @param string $path Path of the file
* @return string|null Returns null if the ETag can not effectively be determined
*/
static public function getETagPropertyForPath($path) {
$data = \OC\Files\Filesystem::getFileInfo($path);
protected function getETagPropertyForPath($path) {
$data = $this->getFS()->getFileInfo($path);
if (isset($data['etag'])) {
return '"'.$data['etag'].'"';
}
return null;
}
protected function getFS() {
if (is_null($this->fileView)) {
$this->fileView = \OC\Files\Filesystem::getView();
}
return $this->fileView;
}
}

View File

@ -0,0 +1,38 @@
<?php
/**
* Copyright (c) 2013 Thomas Müller <thomas.mueller@tmit.eu>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
class Test_OC_Connector_Sabre_File extends PHPUnit_Framework_TestCase {
/**
* @expectedException Sabre_DAV_Exception
*/
public function testSimplePutFails() {
// setup
$file = new OC_Connector_Sabre_File('/test.txt');
$file->fileView = $this->getMock('\OC\Files\View', array('file_put_contents'), array(), '', FALSE);
$file->fileView->expects($this->any())->method('file_put_contents')->withAnyParameters()->will($this->returnValue(false));
// action
$etag = $file->put('test data');
}
/**
* @expectedException Sabre_DAV_Exception
*/
public function testSimplePutFailsOnRename() {
// setup
$file = new OC_Connector_Sabre_File('/test.txt');
$file->fileView = $this->getMock('\OC\Files\View', array('file_put_contents', 'rename'), array(), '', FALSE);
$file->fileView->expects($this->any())->method('file_put_contents')->withAnyParameters()->will($this->returnValue(true));
$file->fileView->expects($this->any())->method('rename')->withAnyParameters()->will($this->returnValue(false));
// action
$etag = $file->put('test data');
}
}