diff options
author | Julius Härtl <jus@bitgrid.net> | 2022-03-23 13:52:47 +0100 |
---|---|---|
committer | Julius Härtl <jus@bitgrid.net> | 2022-04-04 09:02:56 +0200 |
commit | 40f1ad60c245bfe35998b718aab2fe0f726f3798 (patch) | |
tree | e98cc8e991ef582f330db2235f54155223ef4121 /lib/private | |
parent | 94004a7bd40e8527b3b89d954c32529ab13efbdc (diff) | |
download | nextcloud-server-40f1ad60c245bfe35998b718aab2fe0f726f3798.tar.gz nextcloud-server-40f1ad60c245bfe35998b718aab2fe0f726f3798.zip |
Add public API for owner based locking
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/Files/Lock/LockManager.php | 72 | ||||
-rw-r--r-- | lib/private/Server.php | 6 |
2 files changed, 78 insertions, 0 deletions
diff --git a/lib/private/Files/Lock/LockManager.php b/lib/private/Files/Lock/LockManager.php new file mode 100644 index 00000000000..59022613fdb --- /dev/null +++ b/lib/private/Files/Lock/LockManager.php @@ -0,0 +1,72 @@ +<?php + +namespace OC\Files\Lock; + +use OCP\Files\Lock\ILock; +use OCP\Files\Lock\ILockManager; +use OCP\Files\Lock\ILockProvider; +use OCP\Files\Lock\LockScope; +use OCP\PreConditionNotMetException; + +class LockManager implements ILockManager { + private ?ILockProvider $lockProvider = null; + private ?LockScope $lockInScope = null; + + public function registerLockProvider(ILockProvider $lockProvider): void { + if ($this->lockProvider) { + throw new PreConditionNotMetException('There is already a registered lock provider'); + } + + $this->lockProvider = $lockProvider; + } + + public function isLockProviderAvailable(): bool { + return $this->lockProvider !== null; + } + + public function runInScope(LockScope $lock, callable $callback): void { + if (!$this->lockProvider) { + $callback(); + return; + } + + if ($this->lockInScope) { + throw new PreConditionNotMetException('Could not obtain lock scope as already in use by ' . $this->lockInScope); + } + + try { + $this->lockInScope = $lock; + $callback(); + } finally { + $this->lockInScope = null; + } + } + + public function getLockInScope(): ?LockScope { + return $this->lockInScope; + } + + public function getLocks(int $fileId): array { + if (!$this->lockProvider) { + throw new PreConditionNotMetException('No lock provider available'); + } + + return $this->lockProvider->getLocks($fileId); + } + + public function lock(LockScope $lockInfo): ILock { + if (!$this->lockProvider) { + throw new PreConditionNotMetException('No lock provider available'); + } + + return $this->lockProvider->lock($lockInfo); + } + + public function unlock(LockScope $lockInfo): void { + if (!$this->lockProvider) { + throw new PreConditionNotMetException('No lock provider available'); + } + + $this->lockProvider->unlock($lockInfo); + } +} diff --git a/lib/private/Server.php b/lib/private/Server.php index 682e6fa06ce..0cc020fe6be 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -88,6 +88,7 @@ use OC\Federation\CloudFederationProviderManager; use OC\Federation\CloudIdManager; use OC\Files\Config\UserMountCache; use OC\Files\Config\UserMountCacheListener; +use OC\Files\Lock\LockManager; use OC\Files\Mount\CacheMountProvider; use OC\Files\Mount\LocalHomeMountProvider; use OC\Files\Mount\ObjectHomeMountProvider; @@ -175,6 +176,7 @@ use OCP\Files\Config\IUserMountCache; use OCP\Files\IMimeTypeDetector; use OCP\Files\IMimeTypeLoader; use OCP\Files\IRootFolder; +use OCP\Files\Lock\ILockManager; use OCP\Files\Mount\IMountManager; use OCP\Files\NotFoundException; use OCP\Files\Storage\IStorageFactory; @@ -1113,6 +1115,10 @@ class Server extends ServerContainer implements IServerContainer { /** @deprecated 19.0.0 */ $this->registerDeprecatedAlias('LockingProvider', ILockingProvider::class); + $this->registerService(ILockManager::class, function (Server $c): LockManager { + return new LockManager(); + }); + $this->registerAlias(ILockdownManager::class, 'LockdownManager'); $this->registerService(SetupManager::class, function ($c) { // create the setupmanager through the mount manager to resolve the cyclic dependency |