1
0

LockManager.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Files\Lock;
  7. use OCP\Files\Lock\ILock;
  8. use OCP\Files\Lock\ILockManager;
  9. use OCP\Files\Lock\ILockProvider;
  10. use OCP\Files\Lock\LockContext;
  11. use OCP\PreConditionNotMetException;
  12. use Psr\Container\ContainerExceptionInterface;
  13. use Psr\Container\NotFoundExceptionInterface;
  14. class LockManager implements ILockManager {
  15. private ?string $lockProviderClass = null;
  16. private ?ILockProvider $lockProvider = null;
  17. private ?LockContext $lockInScope = null;
  18. public function registerLockProvider(ILockProvider $lockProvider): void {
  19. if ($this->lockProvider) {
  20. throw new PreConditionNotMetException('There is already a registered lock provider');
  21. }
  22. $this->lockProvider = $lockProvider;
  23. }
  24. public function registerLazyLockProvider(string $lockProviderClass): void {
  25. if ($this->lockProviderClass || $this->lockProvider) {
  26. throw new PreConditionNotMetException('There is already a registered lock provider');
  27. }
  28. $this->lockProviderClass = $lockProviderClass;
  29. }
  30. private function getLockProvider(): ?ILockProvider {
  31. if ($this->lockProvider) {
  32. return $this->lockProvider;
  33. }
  34. if ($this->lockProviderClass) {
  35. try {
  36. $this->lockProvider = \OCP\Server::get($this->lockProviderClass);
  37. } catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
  38. }
  39. }
  40. return $this->lockProvider;
  41. }
  42. public function isLockProviderAvailable(): bool {
  43. return $this->getLockProvider() !== null;
  44. }
  45. public function runInScope(LockContext $lock, callable $callback): void {
  46. if (!$this->getLockProvider()) {
  47. $callback();
  48. return;
  49. }
  50. if ($this->lockInScope) {
  51. throw new PreConditionNotMetException('Could not obtain lock scope as already in use by ' . $this->lockInScope);
  52. }
  53. try {
  54. $this->lockInScope = $lock;
  55. $callback();
  56. } finally {
  57. $this->lockInScope = null;
  58. }
  59. }
  60. public function getLockInScope(): ?LockContext {
  61. return $this->lockInScope;
  62. }
  63. public function getLocks(int $fileId): array {
  64. if (!$this->getLockProvider()) {
  65. throw new PreConditionNotMetException('No lock provider available');
  66. }
  67. return $this->getLockProvider()->getLocks($fileId);
  68. }
  69. public function lock(LockContext $lockInfo): ILock {
  70. if (!$this->getLockProvider()) {
  71. throw new PreConditionNotMetException('No lock provider available');
  72. }
  73. return $this->getLockProvider()->lock($lockInfo);
  74. }
  75. public function unlock(LockContext $lockInfo): void {
  76. if (!$this->getLockProvider()) {
  77. throw new PreConditionNotMetException('No lock provider available');
  78. }
  79. $this->getLockProvider()->unlock($lockInfo);
  80. }
  81. }