diff options
author | Vincent Petry <vincent@nextcloud.com> | 2022-04-08 17:43:26 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-08 17:43:26 +0200 |
commit | 0d7d28e530cf84e3780e7935b5f0818e48bd68a6 (patch) | |
tree | c56ce50a395cdc8fb8b2c5ddb558f1fc8283836b /lib/private/Files | |
parent | 88e5c0792a7107a4d1be4973205e23628d3a971f (diff) | |
parent | 9b408d556d32ea81883e1f6a61a821e1959025e6 (diff) | |
download | nextcloud-server-0d7d28e530cf84e3780e7935b5f0818e48bd68a6.tar.gz nextcloud-server-0d7d28e530cf84e3780e7935b5f0818e48bd68a6.zip |
Merge pull request #31676 from nextcloud/enh/ocp-owner-lock
Add public API for owner based file locking
Diffstat (limited to 'lib/private/Files')
-rw-r--r-- | lib/private/Files/Lock/LockManager.php | 72 |
1 files changed, 72 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..e2af532a01c --- /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\LockContext; +use OCP\PreConditionNotMetException; + +class LockManager implements ILockManager { + private ?ILockProvider $lockProvider = null; + private ?LockContext $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(LockContext $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(): ?LockContext { + 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(LockContext $lockInfo): ILock { + if (!$this->lockProvider) { + throw new PreConditionNotMetException('No lock provider available'); + } + + return $this->lockProvider->lock($lockInfo); + } + + public function unlock(LockContext $lockInfo): void { + if (!$this->lockProvider) { + throw new PreConditionNotMetException('No lock provider available'); + } + + $this->lockProvider->unlock($lockInfo); + } +} |