From 40f1ad60c245bfe35998b718aab2fe0f726f3798 Mon Sep 17 00:00:00 2001 From: Julius Härtl Date: Wed, 23 Mar 2022 13:52:47 +0100 Subject: Add public API for owner based locking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/private/Files/Lock/LockManager.php | 72 ++++++++++++++++++++++++++++++++++ lib/private/Server.php | 6 +++ 2 files changed, 78 insertions(+) create mode 100644 lib/private/Files/Lock/LockManager.php (limited to 'lib/private') 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 @@ +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 -- cgit v1.2.3