From 6b94732bdfdc59836968240e0f4682b964a5253a Mon Sep 17 00:00:00 2001 From: ringmaster Date: Sat, 24 May 2014 09:18:24 -0400 Subject: [PATCH] Respect locked files, surface correct exception. --- .../connector/sabre/exception/filelocked.php | 28 +++++++++++++++++++ lib/private/connector/sabre/file.php | 21 ++++++++++---- 2 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 lib/private/connector/sabre/exception/filelocked.php diff --git a/lib/private/connector/sabre/exception/filelocked.php b/lib/private/connector/sabre/exception/filelocked.php new file mode 100644 index 00000000000..c00fd783529 --- /dev/null +++ b/lib/private/connector/sabre/exception/filelocked.php @@ -0,0 +1,28 @@ + + * + */ + +class OC_Connector_Sabre_Exception_FileLocked extends Sabre_DAV_Exception { + + public function __construct($message = "", $code = 0, Exception $previous = null) { + if($previous instanceof \OCP\Files\LockNotAcquiredException) { + $message = sprintf('Target file %s is locked by another process. %s', $previous->path, $previous->getMessage()); + } + parent::__construct($message, $code, $previous); + } + + /** + * Returns the HTTP statuscode for this exception + * + * @return int + */ + public function getHTTPCode() { + + return 503; + } +} diff --git a/lib/private/connector/sabre/file.php b/lib/private/connector/sabre/file.php index 8a16ba55e7a..fc57c82624e 100644 --- a/lib/private/connector/sabre/file.php +++ b/lib/private/connector/sabre/file.php @@ -97,15 +97,24 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements Sabre_D // the path for the file was not valid // TODO: find proper http status code for this case throw new Sabre_DAV_Exception_Forbidden($e->getMessage()); + } catch (\OCP\Files\LockNotAcquiredException $e) { + // the file is currently being written to by another process + throw new OC_Connector_Sabre_Exception_FileLocked(sprintf('Target file %s is locked by another process. %s', $e->path, $e->getMessage()), $e->getCode(), $e); } // rename to correct path - $renameOkay = $this->fileView->rename($partpath, $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($partpath); - throw new Sabre_DAV_Exception('Could not rename part file to final file'); + try { + $renameOkay = $this->fileView->rename($partpath, $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($partpath); + 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 -- 2.39.5