Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/testing/lib/Locking/FakeDBLockingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct(IDBConnection $connection, ILogger $logger, ITimeFac
* @param string $path
* @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
if ($type === self::LOCK_SHARED) {
$this->db->executeUpdate(
Expand Down
12 changes: 7 additions & 5 deletions lib/private/Lock/AbstractLockingProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
Expand Down Expand Up @@ -29,6 +30,7 @@
* to release any left over locks at the end of the request
*/
abstract class AbstractLockingProvider implements ILockingProvider {
/** @var int $ttl */
protected $ttl; // how long until we clear stray locks in seconds

protected $acquiredLocks = [
Expand All @@ -43,7 +45,7 @@ abstract class AbstractLockingProvider implements ILockingProvider {
* @param int $type
* @return bool
*/
protected function hasAcquiredLock($path, $type) {
protected function hasAcquiredLock(string $path, int $type): bool {
if ($type === self::LOCK_SHARED) {
return isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 0;
} else {
Expand All @@ -57,7 +59,7 @@ protected function hasAcquiredLock($path, $type) {
* @param string $path
* @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 (!isset($this->acquiredLocks['shared'][$path])) {
$this->acquiredLocks['shared'][$path] = 0;
Expand All @@ -74,7 +76,7 @@ protected function markAcquire($path, $type) {
* @param string $path
* @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 (isset($this->acquiredLocks['shared'][$path]) and $this->acquiredLocks['shared'][$path] > 0) {
$this->acquiredLocks['shared'][$path]--;
Expand All @@ -93,7 +95,7 @@ protected function markRelease($path, $type) {
* @param string $path
* @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) {
unset($this->acquiredLocks['exclusive'][$path]);
if (!isset($this->acquiredLocks['shared'][$path])) {
Expand Down Expand Up @@ -121,7 +123,7 @@ public function releaseAll() {
}
}

protected function getOwnSharedLockCount($path) {
protected function getOwnSharedLockCount(string $path) {
return isset($this->acquiredLocks['shared'][$path]) ? $this->acquiredLocks['shared'][$path] : 0;
}
}
20 changes: 10 additions & 10 deletions lib/private/Lock/DBLockingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class DBLockingProvider extends AbstractLockingProvider {
* @param string $path
* @return bool
*/
protected function isLocallyLocked($path) {
protected function isLocallyLocked(string $path): bool {
return isset($this->sharedLocks[$path]) && $this->sharedLocks[$path];
}

Expand All @@ -71,7 +71,7 @@ protected function isLocallyLocked($path) {
* @param string $path
* @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);
if ($type === self::LOCK_SHARED) {
$this->sharedLocks[$path] = true;
Expand All @@ -84,7 +84,7 @@ protected function markAcquire($path, $type) {
* @param string $path
* @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);
if ($targetType === self::LOCK_SHARED) {
$this->sharedLocks[$path] = true;
Expand All @@ -99,7 +99,7 @@ protected function markChange($path, $targetType) {
* @param \OCP\AppFramework\Utility\ITimeFactory $timeFactory
* @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->logger = $logger;
$this->timeFactory = $timeFactory;
Expand All @@ -114,15 +114,15 @@ public function __construct(IDBConnection $connection, ILogger $logger, ITimeFac
* @return int number of inserted rows
*/

protected function initLockField($path, $lock = 0) {
protected function initLockField(string $path, int $lock = 0): int {
$expire = $this->getExpireTime();
return $this->connection->insertIfNotExist('*PREFIX*file_locks', ['key' => $path, 'lock' => $lock, 'ttl' => $expire], ['key']);
}

/**
* @return int
*/
protected function getExpireTime() {
protected function getExpireTime(): int {
return $this->timeFactory->getTime() + $this->ttl;
}

Expand All @@ -131,7 +131,7 @@ protected function getExpireTime() {
* @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
* @return bool
*/
public function isLocked($path, $type) {
public function isLocked(string $path, int $type): bool {
if ($this->hasAcquiredLock($path, $type)) {
return true;
}
Expand All @@ -157,7 +157,7 @@ public function isLocked($path, $type) {
* @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
* @throws \OCP\Lock\LockedException
*/
public function acquireLock($path, $type) {
public function acquireLock(string $path, int $type) {
$expire = $this->getExpireTime();
if ($type === self::LOCK_SHARED) {
if (!$this->isLocallyLocked($path)) {
Expand Down Expand Up @@ -194,7 +194,7 @@ public function acquireLock($path, $type) {
* @param string $path
* @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);

// we keep shared locks till the end of the request so we can re-use them
Expand All @@ -213,7 +213,7 @@ public function releaseLock($path, $type) {
* @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE
* @throws \OCP\Lock\LockedException
*/
public function changeLock($path, $targetType) {
public function changeLock(string $path, int $targetType) {
$expire = $this->getExpireTime();
if ($targetType === self::LOCK_SHARED) {
$result = $this->connection->executeUpdate(
Expand Down
13 changes: 7 additions & 6 deletions lib/private/Lock/MemcacheLockingProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
Expand Down Expand Up @@ -36,12 +37,12 @@ class MemcacheLockingProvider extends AbstractLockingProvider {
* @param \OCP\IMemcache $memcache
* @param int $ttl
*/
public function __construct(IMemcache $memcache, $ttl = 3600) {
public function __construct(IMemcache $memcache, int $ttl = 3600) {
$this->memcache = $memcache;
$this->ttl = $ttl;
}

private function setTTL($path) {
private function setTTL(string $path) {
if ($this->memcache instanceof IMemcacheTTL) {
$this->memcache->setTTL($path, $this->ttl);
}
Expand All @@ -52,7 +53,7 @@ private function setTTL($path) {
* @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
* @return bool
*/
public function isLocked($path, $type) {
public function isLocked(string $path, int $type): bool {
$lockValue = $this->memcache->get($path);
if ($type === self::LOCK_SHARED) {
return $lockValue > 0;
Expand All @@ -68,7 +69,7 @@ public function isLocked($path, $type) {
* @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
* @throws \OCP\Lock\LockedException
*/
public function acquireLock($path, $type) {
public function acquireLock(string $path, int $type) {
if ($type === self::LOCK_SHARED) {
if (!$this->memcache->inc($path)) {
throw new LockedException($path);
Expand All @@ -87,7 +88,7 @@ public function acquireLock($path, $type) {
* @param string $path
* @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 ($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
Expand All @@ -111,7 +112,7 @@ public function releaseLock($path, $type) {
* @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE
* @throws \OCP\Lock\LockedException
*/
public function changeLock($path, $targetType) {
public function changeLock(string $path, int $targetType) {
if ($targetType === self::LOCK_SHARED) {
if (!$this->memcache->cas($path, 'exclusive', 1)) {
throw new LockedException($path);
Expand Down
23 changes: 12 additions & 11 deletions lib/private/Lock/NoopLockingProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
Expand Down Expand Up @@ -32,24 +33,24 @@
*/
class NoopLockingProvider implements ILockingProvider {

/**
* {@inheritdoc}
*/
public function isLocked($path, $type) {
/**
* {@inheritdoc}
*/
public function isLocked(string $path, int $type): bool {
return false;
}

/**
* {@inheritdoc}
*/
public function acquireLock($path, $type) {
/**
* {@inheritdoc}
*/
public function acquireLock(string $path, int $type) {
// do nothing
}

/**
* {@inheritdoc}
* {@inheritdoc}
*/
public function releaseLock($path, $type) {
public function releaseLock(string $path, int $type) {
// do nothing
}

Expand All @@ -63,7 +64,7 @@ public function releaseAll() {
/**
* {@inheritdoc}
*/
public function changeLock($path, $targetType) {
public function changeLock(string $path, int $targetType) {
// do nothing
}
}
9 changes: 5 additions & 4 deletions lib/public/Lock/ILockingProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
Expand Down Expand Up @@ -45,22 +46,22 @@ interface ILockingProvider {
* @return bool
* @since 8.1.0
*/
public function isLocked($path, $type);
public function isLocked(string $path, int $type): bool;

/**
* @param string $path
* @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
* @throws \OCP\Lock\LockedException
* @since 8.1.0
*/
public function acquireLock($path, $type);
public function acquireLock(string $path, int $type);

/**
* @param string $path
* @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
* @since 8.1.0
*/
public function releaseLock($path, $type);
public function releaseLock(string $path, int $type);

/**
* Change the type of an existing lock
Expand All @@ -70,7 +71,7 @@ public function releaseLock($path, $type);
* @throws \OCP\Lock\LockedException
* @since 8.1.0
*/
public function changeLock($path, $targetType);
public function changeLock(string $path, int $targetType);

/**
* release all lock acquired by this instance
Expand Down
5 changes: 3 additions & 2 deletions lib/public/Lock/LockedException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
Expand Down Expand Up @@ -48,7 +49,7 @@ class LockedException extends \Exception {
*
* @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);
$this->path = $path;
}
Expand All @@ -57,7 +58,7 @@ public function __construct($path, \Exception $previous = null) {
* @return string
* @since 8.1.0
*/
public function getPath() {
public function getPath(): string {
return $this->path;
}
}