Skip to content
Snippets Groups Projects
Unverified Commit 22b3280a authored by Morris Jobke's avatar Morris Jobke Committed by GitHub
Browse files

Merge pull request #7897 from nextcloud/strict_lockingproviders

Made locking providers strict
parents cb0dbfa9 8edbfdb2
No related branches found
No related tags found
No related merge requests found
...@@ -51,7 +51,7 @@ class FakeDBLockingProvider extends \OC\Lock\DBLockingProvider { ...@@ -51,7 +51,7 @@ class FakeDBLockingProvider extends \OC\Lock\DBLockingProvider {
* @param string $path * @param string $path
* @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
*/ */
public function releaseLock($path, $type) { public function releaseLock(string $path, int $type) {
// we DONT keep shared locks till the end of the request // we DONT keep shared locks till the end of the request
if ($type === self::LOCK_SHARED) { if ($type === self::LOCK_SHARED) {
$this->db->executeUpdate( $this->db->executeUpdate(
......
<?php <?php
declare(strict_types=1);
/** /**
* @copyright Copyright (c) 2016, ownCloud, Inc. * @copyright Copyright (c) 2016, ownCloud, Inc.
* *
...@@ -29,6 +30,7 @@ use OCP\Lock\ILockingProvider; ...@@ -29,6 +30,7 @@ use OCP\Lock\ILockingProvider;
* to release any left over locks at the end of the request * to release any left over locks at the end of the request
*/ */
abstract class AbstractLockingProvider implements ILockingProvider { abstract class AbstractLockingProvider implements ILockingProvider {
/** @var int $ttl */
protected $ttl; // how long until we clear stray locks in seconds protected $ttl; // how long until we clear stray locks in seconds
protected $acquiredLocks = [ protected $acquiredLocks = [
...@@ -43,7 +45,7 @@ abstract class AbstractLockingProvider implements ILockingProvider { ...@@ -43,7 +45,7 @@ abstract class AbstractLockingProvider implements ILockingProvider {
* @param int $type * @param int $type
* @return bool * @return bool
*/ */
protected function hasAcquiredLock($path, $type) { protected function hasAcquiredLock(string $path, int $type): bool {
if ($type === self::LOCK_SHARED) { if ($type === self::LOCK_SHARED) {
return isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 0; return isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 0;
} else { } else {
...@@ -57,7 +59,7 @@ abstract class AbstractLockingProvider implements ILockingProvider { ...@@ -57,7 +59,7 @@ abstract class AbstractLockingProvider implements ILockingProvider {
* @param string $path * @param string $path
* @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
*/ */
protected function markAcquire($path, $type) { protected function markAcquire(string $path, int $type) {
if ($type === self::LOCK_SHARED) { if ($type === self::LOCK_SHARED) {
if (!isset($this->acquiredLocks['shared'][$path])) { if (!isset($this->acquiredLocks['shared'][$path])) {
$this->acquiredLocks['shared'][$path] = 0; $this->acquiredLocks['shared'][$path] = 0;
...@@ -74,7 +76,7 @@ abstract class AbstractLockingProvider implements ILockingProvider { ...@@ -74,7 +76,7 @@ abstract class AbstractLockingProvider implements ILockingProvider {
* @param string $path * @param string $path
* @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
*/ */
protected function markRelease($path, $type) { protected function markRelease(string $path, int $type) {
if ($type === self::LOCK_SHARED) { if ($type === self::LOCK_SHARED) {
if (isset($this->acquiredLocks['shared'][$path]) and $this->acquiredLocks['shared'][$path] > 0) { if (isset($this->acquiredLocks['shared'][$path]) and $this->acquiredLocks['shared'][$path] > 0) {
$this->acquiredLocks['shared'][$path]--; $this->acquiredLocks['shared'][$path]--;
...@@ -93,7 +95,7 @@ abstract class AbstractLockingProvider implements ILockingProvider { ...@@ -93,7 +95,7 @@ abstract class AbstractLockingProvider implements ILockingProvider {
* @param string $path * @param string $path
* @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE
*/ */
protected function markChange($path, $targetType) { protected function markChange(string $path, int $targetType) {
if ($targetType === self::LOCK_SHARED) { if ($targetType === self::LOCK_SHARED) {
unset($this->acquiredLocks['exclusive'][$path]); unset($this->acquiredLocks['exclusive'][$path]);
if (!isset($this->acquiredLocks['shared'][$path])) { if (!isset($this->acquiredLocks['shared'][$path])) {
...@@ -121,7 +123,7 @@ abstract class AbstractLockingProvider implements ILockingProvider { ...@@ -121,7 +123,7 @@ abstract class AbstractLockingProvider implements ILockingProvider {
} }
} }
protected function getOwnSharedLockCount($path) { protected function getOwnSharedLockCount(string $path) {
return isset($this->acquiredLocks['shared'][$path]) ? $this->acquiredLocks['shared'][$path] : 0; return isset($this->acquiredLocks['shared'][$path]) ? $this->acquiredLocks['shared'][$path] : 0;
} }
} }
...@@ -61,7 +61,7 @@ class DBLockingProvider extends AbstractLockingProvider { ...@@ -61,7 +61,7 @@ class DBLockingProvider extends AbstractLockingProvider {
* @param string $path * @param string $path
* @return bool * @return bool
*/ */
protected function isLocallyLocked($path) { protected function isLocallyLocked(string $path): bool {
return isset($this->sharedLocks[$path]) && $this->sharedLocks[$path]; return isset($this->sharedLocks[$path]) && $this->sharedLocks[$path];
} }
...@@ -71,7 +71,7 @@ class DBLockingProvider extends AbstractLockingProvider { ...@@ -71,7 +71,7 @@ class DBLockingProvider extends AbstractLockingProvider {
* @param string $path * @param string $path
* @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
*/ */
protected function markAcquire($path, $type) { protected function markAcquire(string $path, int $type) {
parent::markAcquire($path, $type); parent::markAcquire($path, $type);
if ($type === self::LOCK_SHARED) { if ($type === self::LOCK_SHARED) {
$this->sharedLocks[$path] = true; $this->sharedLocks[$path] = true;
...@@ -84,7 +84,7 @@ class DBLockingProvider extends AbstractLockingProvider { ...@@ -84,7 +84,7 @@ class DBLockingProvider extends AbstractLockingProvider {
* @param string $path * @param string $path
* @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE
*/ */
protected function markChange($path, $targetType) { protected function markChange(string $path, int $targetType) {
parent::markChange($path, $targetType); parent::markChange($path, $targetType);
if ($targetType === self::LOCK_SHARED) { if ($targetType === self::LOCK_SHARED) {
$this->sharedLocks[$path] = true; $this->sharedLocks[$path] = true;
...@@ -99,7 +99,7 @@ class DBLockingProvider extends AbstractLockingProvider { ...@@ -99,7 +99,7 @@ class DBLockingProvider extends AbstractLockingProvider {
* @param \OCP\AppFramework\Utility\ITimeFactory $timeFactory * @param \OCP\AppFramework\Utility\ITimeFactory $timeFactory
* @param int $ttl * @param int $ttl
*/ */
public function __construct(IDBConnection $connection, ILogger $logger, ITimeFactory $timeFactory, $ttl = 3600) { public function __construct(IDBConnection $connection, ILogger $logger, ITimeFactory $timeFactory, int $ttl = 3600) {
$this->connection = $connection; $this->connection = $connection;
$this->logger = $logger; $this->logger = $logger;
$this->timeFactory = $timeFactory; $this->timeFactory = $timeFactory;
...@@ -114,7 +114,7 @@ class DBLockingProvider extends AbstractLockingProvider { ...@@ -114,7 +114,7 @@ class DBLockingProvider extends AbstractLockingProvider {
* @return int number of inserted rows * @return int number of inserted rows
*/ */
protected function initLockField($path, $lock = 0) { protected function initLockField(string $path, int $lock = 0): int {
$expire = $this->getExpireTime(); $expire = $this->getExpireTime();
return $this->connection->insertIfNotExist('*PREFIX*file_locks', ['key' => $path, 'lock' => $lock, 'ttl' => $expire], ['key']); return $this->connection->insertIfNotExist('*PREFIX*file_locks', ['key' => $path, 'lock' => $lock, 'ttl' => $expire], ['key']);
} }
...@@ -122,7 +122,7 @@ class DBLockingProvider extends AbstractLockingProvider { ...@@ -122,7 +122,7 @@ class DBLockingProvider extends AbstractLockingProvider {
/** /**
* @return int * @return int
*/ */
protected function getExpireTime() { protected function getExpireTime(): int {
return $this->timeFactory->getTime() + $this->ttl; return $this->timeFactory->getTime() + $this->ttl;
} }
...@@ -131,7 +131,7 @@ class DBLockingProvider extends AbstractLockingProvider { ...@@ -131,7 +131,7 @@ class DBLockingProvider extends AbstractLockingProvider {
* @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
* @return bool * @return bool
*/ */
public function isLocked($path, $type) { public function isLocked(string $path, int $type): bool {
if ($this->hasAcquiredLock($path, $type)) { if ($this->hasAcquiredLock($path, $type)) {
return true; return true;
} }
...@@ -157,7 +157,7 @@ class DBLockingProvider extends AbstractLockingProvider { ...@@ -157,7 +157,7 @@ class DBLockingProvider extends AbstractLockingProvider {
* @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
* @throws \OCP\Lock\LockedException * @throws \OCP\Lock\LockedException
*/ */
public function acquireLock($path, $type) { public function acquireLock(string $path, int $type) {
$expire = $this->getExpireTime(); $expire = $this->getExpireTime();
if ($type === self::LOCK_SHARED) { if ($type === self::LOCK_SHARED) {
if (!$this->isLocallyLocked($path)) { if (!$this->isLocallyLocked($path)) {
...@@ -194,7 +194,7 @@ class DBLockingProvider extends AbstractLockingProvider { ...@@ -194,7 +194,7 @@ class DBLockingProvider extends AbstractLockingProvider {
* @param string $path * @param string $path
* @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
*/ */
public function releaseLock($path, $type) { public function releaseLock(string $path, int $type) {
$this->markRelease($path, $type); $this->markRelease($path, $type);
// we keep shared locks till the end of the request so we can re-use them // we keep shared locks till the end of the request so we can re-use them
...@@ -213,7 +213,7 @@ class DBLockingProvider extends AbstractLockingProvider { ...@@ -213,7 +213,7 @@ class DBLockingProvider extends AbstractLockingProvider {
* @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE
* @throws \OCP\Lock\LockedException * @throws \OCP\Lock\LockedException
*/ */
public function changeLock($path, $targetType) { public function changeLock(string $path, int $targetType) {
$expire = $this->getExpireTime(); $expire = $this->getExpireTime();
if ($targetType === self::LOCK_SHARED) { if ($targetType === self::LOCK_SHARED) {
$result = $this->connection->executeUpdate( $result = $this->connection->executeUpdate(
......
<?php <?php
declare(strict_types=1);
/** /**
* @copyright Copyright (c) 2016, ownCloud, Inc. * @copyright Copyright (c) 2016, ownCloud, Inc.
* *
...@@ -36,12 +37,12 @@ class MemcacheLockingProvider extends AbstractLockingProvider { ...@@ -36,12 +37,12 @@ class MemcacheLockingProvider extends AbstractLockingProvider {
* @param \OCP\IMemcache $memcache * @param \OCP\IMemcache $memcache
* @param int $ttl * @param int $ttl
*/ */
public function __construct(IMemcache $memcache, $ttl = 3600) { public function __construct(IMemcache $memcache, int $ttl = 3600) {
$this->memcache = $memcache; $this->memcache = $memcache;
$this->ttl = $ttl; $this->ttl = $ttl;
} }
private function setTTL($path) { private function setTTL(string $path) {
if ($this->memcache instanceof IMemcacheTTL) { if ($this->memcache instanceof IMemcacheTTL) {
$this->memcache->setTTL($path, $this->ttl); $this->memcache->setTTL($path, $this->ttl);
} }
...@@ -52,7 +53,7 @@ class MemcacheLockingProvider extends AbstractLockingProvider { ...@@ -52,7 +53,7 @@ class MemcacheLockingProvider extends AbstractLockingProvider {
* @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
* @return bool * @return bool
*/ */
public function isLocked($path, $type) { public function isLocked(string $path, int $type): bool {
$lockValue = $this->memcache->get($path); $lockValue = $this->memcache->get($path);
if ($type === self::LOCK_SHARED) { if ($type === self::LOCK_SHARED) {
return $lockValue > 0; return $lockValue > 0;
...@@ -68,7 +69,7 @@ class MemcacheLockingProvider extends AbstractLockingProvider { ...@@ -68,7 +69,7 @@ class MemcacheLockingProvider extends AbstractLockingProvider {
* @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
* @throws \OCP\Lock\LockedException * @throws \OCP\Lock\LockedException
*/ */
public function acquireLock($path, $type) { public function acquireLock(string $path, int $type) {
if ($type === self::LOCK_SHARED) { if ($type === self::LOCK_SHARED) {
if (!$this->memcache->inc($path)) { if (!$this->memcache->inc($path)) {
throw new LockedException($path); throw new LockedException($path);
...@@ -87,7 +88,7 @@ class MemcacheLockingProvider extends AbstractLockingProvider { ...@@ -87,7 +88,7 @@ class MemcacheLockingProvider extends AbstractLockingProvider {
* @param string $path * @param string $path
* @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
*/ */
public function releaseLock($path, $type) { public function releaseLock(string $path, int $type) {
if ($type === self::LOCK_SHARED) { if ($type === self::LOCK_SHARED) {
if ($this->getOwnSharedLockCount($path) === 1) { if ($this->getOwnSharedLockCount($path) === 1) {
$removed = $this->memcache->cad($path, 1); // if we're the only one having a shared lock we can remove it in one go $removed = $this->memcache->cad($path, 1); // if we're the only one having a shared lock we can remove it in one go
...@@ -111,7 +112,7 @@ class MemcacheLockingProvider extends AbstractLockingProvider { ...@@ -111,7 +112,7 @@ class MemcacheLockingProvider extends AbstractLockingProvider {
* @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE
* @throws \OCP\Lock\LockedException * @throws \OCP\Lock\LockedException
*/ */
public function changeLock($path, $targetType) { public function changeLock(string $path, int $targetType) {
if ($targetType === self::LOCK_SHARED) { if ($targetType === self::LOCK_SHARED) {
if (!$this->memcache->cas($path, 'exclusive', 1)) { if (!$this->memcache->cas($path, 'exclusive', 1)) {
throw new LockedException($path); throw new LockedException($path);
......
<?php <?php
declare(strict_types=1);
/** /**
* @copyright Copyright (c) 2016, ownCloud, Inc. * @copyright Copyright (c) 2016, ownCloud, Inc.
* *
...@@ -32,24 +33,24 @@ use OCP\Lock\ILockingProvider; ...@@ -32,24 +33,24 @@ use OCP\Lock\ILockingProvider;
*/ */
class NoopLockingProvider implements ILockingProvider { class NoopLockingProvider implements ILockingProvider {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function isLocked($path, $type) { public function isLocked(string $path, int $type): bool {
return false; return false;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function acquireLock($path, $type) { public function acquireLock(string $path, int $type) {
// do nothing // do nothing
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function releaseLock($path, $type) { public function releaseLock(string $path, int $type) {
// do nothing // do nothing
} }
...@@ -63,7 +64,7 @@ class NoopLockingProvider implements ILockingProvider { ...@@ -63,7 +64,7 @@ class NoopLockingProvider implements ILockingProvider {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function changeLock($path, $targetType) { public function changeLock(string $path, int $targetType) {
// do nothing // do nothing
} }
} }
<?php <?php
declare(strict_types=1);
/** /**
* @copyright Copyright (c) 2016, ownCloud, Inc. * @copyright Copyright (c) 2016, ownCloud, Inc.
* *
...@@ -45,7 +46,7 @@ interface ILockingProvider { ...@@ -45,7 +46,7 @@ interface ILockingProvider {
* @return bool * @return bool
* @since 8.1.0 * @since 8.1.0
*/ */
public function isLocked($path, $type); public function isLocked(string $path, int $type): bool;
/** /**
* @param string $path * @param string $path
...@@ -53,14 +54,14 @@ interface ILockingProvider { ...@@ -53,14 +54,14 @@ interface ILockingProvider {
* @throws \OCP\Lock\LockedException * @throws \OCP\Lock\LockedException
* @since 8.1.0 * @since 8.1.0
*/ */
public function acquireLock($path, $type); public function acquireLock(string $path, int $type);
/** /**
* @param string $path * @param string $path
* @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
* @since 8.1.0 * @since 8.1.0
*/ */
public function releaseLock($path, $type); public function releaseLock(string $path, int $type);
/** /**
* Change the type of an existing lock * Change the type of an existing lock
...@@ -70,7 +71,7 @@ interface ILockingProvider { ...@@ -70,7 +71,7 @@ interface ILockingProvider {
* @throws \OCP\Lock\LockedException * @throws \OCP\Lock\LockedException
* @since 8.1.0 * @since 8.1.0
*/ */
public function changeLock($path, $targetType); public function changeLock(string $path, int $targetType);
/** /**
* release all lock acquired by this instance * release all lock acquired by this instance
......
<?php <?php
declare(strict_types=1);
/** /**
* @copyright Copyright (c) 2016, ownCloud, Inc. * @copyright Copyright (c) 2016, ownCloud, Inc.
* *
...@@ -48,7 +49,7 @@ class LockedException extends \Exception { ...@@ -48,7 +49,7 @@ class LockedException extends \Exception {
* *
* @since 8.1.0 * @since 8.1.0
*/ */
public function __construct($path, \Exception $previous = null) { public function __construct(string $path, \Exception $previous = null) {
parent::__construct('"' . $path . '" is locked', 0, $previous); parent::__construct('"' . $path . '" is locked', 0, $previous);
$this->path = $path; $this->path = $path;
} }
...@@ -57,7 +58,7 @@ class LockedException extends \Exception { ...@@ -57,7 +58,7 @@ class LockedException extends \Exception {
* @return string * @return string
* @since 8.1.0 * @since 8.1.0
*/ */
public function getPath() { public function getPath(): string {
return $this->path; return $this->path;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment