summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2019-01-23 08:19:35 +0100
committerGitHub <noreply@github.com>2019-01-23 08:19:35 +0100
commitb558e4969a18382e4c87c05895f8053dc2e5957c (patch)
tree27be586abcd2fc2ce25e342d41ff2996f13f784e
parent544d28ad606eeeff44b7bd309ab52de41bd571c5 (diff)
parent9126cdc7a88460c4c8c11c4e6ce29b7a2df932b5 (diff)
downloadnextcloud-server-b558e4969a18382e4c87c05895f8053dc2e5957c.tar.gz
nextcloud-server-b558e4969a18382e4c87c05895f8053dc2e5957c.zip
Merge pull request #13746 from nextcloud/backport/13739/stable13
[stable13] cleanup shared lock if changing to exclusive lock failed
-rw-r--r--lib/private/Files/View.php8
-rw-r--r--tests/lib/Files/ViewTest.php31
2 files changed, 38 insertions, 1 deletions
diff --git a/lib/private/Files/View.php b/lib/private/Files/View.php
index cd1219fd77e..0acd918f899 100644
--- a/lib/private/Files/View.php
+++ b/lib/private/Files/View.php
@@ -1139,7 +1139,13 @@ class View {
list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix);
if ($run and $storage) {
if (in_array('write', $hooks) || in_array('delete', $hooks)) {
- $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE);
+ try {
+ $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE);
+ } catch (LockedException $e) {
+ // release the shared lock we acquired before quiting
+ $this->unlockFile($path, ILockingProvider::LOCK_SHARED);
+ throw $e;
+ }
}
try {
if (!is_null($extraParam)) {
diff --git a/tests/lib/Files/ViewTest.php b/tests/lib/Files/ViewTest.php
index 33d5cc0a8a6..8ac7433aaa2 100644
--- a/tests/lib/Files/ViewTest.php
+++ b/tests/lib/Files/ViewTest.php
@@ -1987,6 +1987,37 @@ class ViewTest extends \Test\TestCase {
$this->assertNull($this->getFileLockType($view, $path), 'File got unlocked after exception');
}
+ public function testLockBasicOperationUnlocksAfterLockException() {
+ $view = new View('/' . $this->user . '/files/');
+
+ $storage = new Temporary([]);
+
+ Filesystem::mount($storage, array(), $this->user . '/');
+
+ $storage->mkdir('files');
+ $storage->mkdir('files/dir');
+ $storage->file_put_contents('files/test.txt', 'blah');
+ $storage->getScanner()->scan('files');
+
+ // get a shared lock
+ $handle = $view->fopen('test.txt', 'r');
+
+ $thrown = false;
+ try {
+ // try (and fail) to get a write lock
+ $view->unlink('test.txt');
+ } catch (\Exception $e) {
+ $thrown = true;
+ $this->assertInstanceOf(LockedException::class, $e);
+ }
+ $this->assertTrue($thrown, 'Exception was rethrown');
+
+ // clean shared lock
+ fclose($handle);
+
+ $this->assertNull($this->getFileLockType($view, 'test.txt'), 'File got unlocked');
+ }
+
/**
* Test locks for fopen with fclose at the end
*