HEX
Server: LiteSpeed
System: Linux d8 4.18.0-553.121.1.lve.el8.x86_64 #1 SMP Thu Apr 30 16:40:41 UTC 2026 x86_64
User: wbwebdes (3015)
PHP: 8.1.31
Disabled: exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: /home/wbwebdes/domains/files.wb-cloud.nl/public_html/lib/private/Files/Lock/LockManager.php
<?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);
	}
}