aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Files/Lock/LockManager.php
blob: 978c378e506a2459835b9bfcde38f6f9beb9b084 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php

/**
 * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
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;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class LockManager implements ILockManager {
	private ?string $lockProviderClass = null;
	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 registerLazyLockProvider(string $lockProviderClass): void {
		if ($this->lockProviderClass || $this->lockProvider) {
			throw new PreConditionNotMetException('There is already a registered lock provider');
		}

		$this->lockProviderClass = $lockProviderClass;
	}

	private function getLockProvider(): ?ILockProvider {
		if ($this->lockProvider) {
			return $this->lockProvider;
		}
		if ($this->lockProviderClass) {
			try {
				$this->lockProvider = \OCP\Server::get($this->lockProviderClass);
			} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
			}
		}

		return $this->lockProvider;
	}

	public function isLockProviderAvailable(): bool {
		return $this->getLockProvider() !== null;
	}

	public function runInScope(LockContext $lock, callable $callback): void {
		if (!$this->getLockProvider()) {
			$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->getLockProvider()) {
			throw new PreConditionNotMetException('No lock provider available');
		}

		return $this->getLockProvider()->getLocks($fileId);
	}

	public function lock(LockContext $lockInfo): ILock {
		if (!$this->getLockProvider()) {
			throw new PreConditionNotMetException('No lock provider available');
		}

		return $this->getLockProvider()->lock($lockInfo);
	}

	public function unlock(LockContext $lockInfo): void {
		if (!$this->getLockProvider()) {
			throw new PreConditionNotMetException('No lock provider available');
		}

		$this->getLockProvider()->unlock($lockInfo);
	}
}