2015-07-15 15:44:51 +02:00
|
|
|
<?php
|
2019-12-03 19:57:53 +01:00
|
|
|
|
2018-01-16 19:34:43 +01:00
|
|
|
declare(strict_types=1);
|
2015-07-15 15:44:51 +02:00
|
|
|
/**
|
2024-05-23 09:26:56 +02:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-07-15 15:44:51 +02:00
|
|
|
*/
|
|
|
|
namespace OC\Lock;
|
|
|
|
|
|
|
|
use OCP\Lock\ILockingProvider;
|
|
|
|
|
2015-08-03 15:46:23 +02:00
|
|
|
/**
|
|
|
|
* Base locking provider that keeps track of locks acquired during the current request
|
2022-04-22 15:00:20 +02:00
|
|
|
* to release any leftover locks at the end of the request
|
2015-08-03 15:46:23 +02:00
|
|
|
*/
|
2015-07-15 15:44:51 +02:00
|
|
|
abstract class AbstractLockingProvider implements ILockingProvider {
|
2023-09-26 10:48:17 +03:30
|
|
|
protected array $acquiredLocks = [
|
2015-07-15 15:44:51 +02:00
|
|
|
'shared' => [],
|
|
|
|
'exclusive' => []
|
|
|
|
];
|
|
|
|
|
2023-09-26 10:48:17 +03:30
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param int $ttl how long until we clear stray locks in seconds
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
protected int $ttl,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-04-22 15:00:20 +02:00
|
|
|
/** @inheritDoc */
|
2018-01-16 19:34:43 +01:00
|
|
|
protected function hasAcquiredLock(string $path, int $type): bool {
|
2015-10-20 14:04:50 +02:00
|
|
|
if ($type === self::LOCK_SHARED) {
|
|
|
|
return isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 0;
|
|
|
|
} else {
|
|
|
|
return isset($this->acquiredLocks['exclusive'][$path]) && $this->acquiredLocks['exclusive'][$path] === true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 15:00:20 +02:00
|
|
|
/** @inheritDoc */
|
|
|
|
protected function markAcquire(string $path, int $targetType): void {
|
|
|
|
if ($targetType === self::LOCK_SHARED) {
|
2015-07-15 15:44:51 +02:00
|
|
|
if (!isset($this->acquiredLocks['shared'][$path])) {
|
|
|
|
$this->acquiredLocks['shared'][$path] = 0;
|
|
|
|
}
|
|
|
|
$this->acquiredLocks['shared'][$path]++;
|
|
|
|
} else {
|
|
|
|
$this->acquiredLocks['exclusive'][$path] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 15:00:20 +02:00
|
|
|
/** @inheritDoc */
|
|
|
|
protected function markRelease(string $path, int $type): void {
|
2015-07-15 15:44:51 +02:00
|
|
|
if ($type === self::LOCK_SHARED) {
|
|
|
|
if (isset($this->acquiredLocks['shared'][$path]) and $this->acquiredLocks['shared'][$path] > 0) {
|
|
|
|
$this->acquiredLocks['shared'][$path]--;
|
2016-05-02 14:18:46 +02:00
|
|
|
if ($this->acquiredLocks['shared'][$path] === 0) {
|
|
|
|
unset($this->acquiredLocks['shared'][$path]);
|
|
|
|
}
|
2015-07-15 15:44:51 +02:00
|
|
|
}
|
2020-04-10 10:35:09 +02:00
|
|
|
} elseif ($type === self::LOCK_EXCLUSIVE) {
|
2015-07-15 15:44:51 +02:00
|
|
|
unset($this->acquiredLocks['exclusive'][$path]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 15:00:20 +02:00
|
|
|
/** @inheritDoc */
|
|
|
|
protected function markChange(string $path, int $targetType): void {
|
2015-07-15 15:44:51 +02:00
|
|
|
if ($targetType === self::LOCK_SHARED) {
|
|
|
|
unset($this->acquiredLocks['exclusive'][$path]);
|
|
|
|
if (!isset($this->acquiredLocks['shared'][$path])) {
|
|
|
|
$this->acquiredLocks['shared'][$path] = 0;
|
|
|
|
}
|
|
|
|
$this->acquiredLocks['shared'][$path]++;
|
2020-04-10 10:35:09 +02:00
|
|
|
} elseif ($targetType === self::LOCK_EXCLUSIVE) {
|
2015-07-15 15:44:51 +02:00
|
|
|
$this->acquiredLocks['exclusive'][$path] = true;
|
|
|
|
$this->acquiredLocks['shared'][$path]--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 15:00:20 +02:00
|
|
|
/** @inheritDoc */
|
|
|
|
public function releaseAll(): void {
|
2015-07-15 15:44:51 +02:00
|
|
|
foreach ($this->acquiredLocks['shared'] as $path => $count) {
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
$this->releaseLock($path, self::LOCK_SHARED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->acquiredLocks['exclusive'] as $path => $hasLock) {
|
|
|
|
$this->releaseLock($path, self::LOCK_EXCLUSIVE);
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 12:54:33 +02:00
|
|
|
|
2022-04-22 15:00:20 +02:00
|
|
|
protected function getOwnSharedLockCount(string $path): int {
|
|
|
|
return $this->acquiredLocks['shared'][$path] ?? 0;
|
2016-04-29 12:54:33 +02:00
|
|
|
}
|
2015-07-15 15:44:51 +02:00
|
|
|
}
|