Skip to content

Commit

Permalink
Merge pull request #533 from nextcloud/backport/497/stable28
Browse files Browse the repository at this point in the history
[stable28] fix(timeout): compare creation time to now - timeout
  • Loading branch information
max-nextcloud authored Feb 17, 2025
2 parents 846bb47 + 0b09775 commit 7153e7b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 216 deletions.
8 changes: 5 additions & 3 deletions lib/Controller/LockController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
use OCA\FilesLock\Model\FileLock;
use OCA\FilesLock\Service\FileService;
use OCA\FilesLock\Service\LockService;
use OCA\FilesLock\Tools\Traits\TLogger;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;
Expand All @@ -48,19 +47,21 @@
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;

/**
* Class LockController
*
* @package OCA\FilesLock\Controller
*/
class LockController extends OCSController {
use TLogger;

private int $ocsVersion;
private LoggerInterface $logger;

public function __construct(
IRequest $request,
LoggerInterface $logger,
private IUserSession $userSession,
private FileService $fileService,
private LockService $lockService,
Expand All @@ -82,6 +83,7 @@ public function __construct(
$this->registerResponder('xml', function ($data) {
return $this->buildOCSResponse('xml', $data);
});
$this->logger = $logger;
}


Expand Down Expand Up @@ -197,7 +199,7 @@ protected function fail(
);

if ($log) {
$this->log(2, $status . ' - ' . json_encode($data));
$this->logger->warning('[warning] ' . $status . ' - ' . json_encode($data));
}

return new DataResponse($data, $status);
Expand Down
5 changes: 4 additions & 1 deletion lib/Db/LocksRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Exception;
use OCA\FilesLock\Exceptions\LockNotFoundException;
use OCA\FilesLock\Model\FileLock;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\QueryBuilder\IQueryBuilder;

/**
Expand Down Expand Up @@ -146,8 +147,10 @@ public function getAll(): array {
* @throws Exception
*/
public function getLocksOlderThan(int $timeout): array {
$now = \OC::$server->get(ITimeFactory::class)->getTime();
$oldCreationTime = $now - $timeout * 60;
$qb = $this->getLocksSelectSql();
$qb->andWhere($qb->expr()->lt('l.creation', $qb->createNamedParameter($timeout * 60, IQueryBuilder::PARAM_INT)));
$qb->andWhere($qb->expr()->lt('l.creation', $qb->createNamedParameter($oldCreationTime, IQueryBuilder::PARAM_INT)));

return $this->getLocksFromRequest($qb);
}
Expand Down
22 changes: 11 additions & 11 deletions lib/Service/LockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
use OCA\FilesLock\Exceptions\LockNotFoundException;
use OCA\FilesLock\Exceptions\UnauthorizedUnlockException;
use OCA\FilesLock\Model\FileLock;
use OCA\FilesLock\Tools\Traits\TLogger;
use OCA\FilesLock\Tools\Traits\TStringTools;
use OCP\App\IAppManager;
use OCP\EventDispatcher\IEventDispatcher;
Expand All @@ -49,14 +48,13 @@
use OCP\IRequest;
use OCP\IUserManager;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;

class LockService {
public const PREFIX = 'files_lock';


use TStringTools;
use TLogger;


private IUserManager $userManager;
private IL10N $l10n;
Expand All @@ -67,6 +65,7 @@ class LockService {
private IEventDispatcher $eventDispatcher;
private IUserSession $userSession;
private IRequest $request;
private LoggerInterface $logger;


private array $locks = [];
Expand All @@ -86,6 +85,7 @@ public function __construct(
IEventDispatcher $eventDispatcher,
IUserSession $userSession,
IRequest $request,
LoggerInterface $logger,
) {
$this->l10n = $l10n;
$this->userManager = $userManager;
Expand All @@ -96,8 +96,7 @@ public function __construct(
$this->eventDispatcher = $eventDispatcher;
$this->userSession = $userSession;
$this->request = $request;

$this->setup('app', 'files_lock');
$this->logger = $logger;
}

/**
Expand Down Expand Up @@ -175,7 +174,7 @@ public function lock(LockContext $lockScope): FileLock {
$known->setTimeout(
$known->getETA() !== FileLock::ETA_INFINITE ? $known->getTimeout() - $known->getETA() + $this->configService->getTimeoutSeconds() : 0
);
$this->notice('extending existing lock', false, ['fileLock' => $known]);
$this->logger->notice('extending existing lock', ['fileLock' => $known]);
$this->locksRequest->update($known);
$this->injectMetadata($known);
return $known;
Expand All @@ -187,7 +186,7 @@ public function lock(LockContext $lockScope): FileLock {
$lock = FileLock::fromLockScope($lockScope, $this->configService->getTimeoutSeconds());
$this->generateToken($lock);
$lock->setCreation(time());
$this->notice('locking file', false, ['fileLock' => $lock]);
$this->logger->notice('locking file', ['fileLock' => $lock]);
$this->injectMetadata($lock);
$this->locksRequest->save($lock);
$this->propagateEtag($lockScope);
Expand All @@ -211,7 +210,7 @@ public function getAppName(string $appId): ?string {
* @throws UnauthorizedUnlockException
*/
public function unlock(LockContext $lock, bool $force = false): FileLock {
$this->notice('unlocking file', false, ['fileLock' => $lock]);
$this->logger->notice('unlocking file', ['fileLock' => $lock]);

$known = $this->getLockFromFileId($lock->getNode()->getId());
if (!$force) {
Expand Down Expand Up @@ -302,15 +301,16 @@ public function unlockFile(int $fileId, string $userId, bool $force = false, int
public function getDeprecatedLocks(): array {
$timeout = (int)$this->configService->getAppValue(ConfigService::LOCK_TIMEOUT);
if ($timeout === 0) {
$this->notice(
'ConfigService::LOCK_TIMEOUT is not numerical, using default', true, ['current' => $timeout]
$this->logger->notice(
'ConfigService::LOCK_TIMEOUT is not numerical, using default', ['current' => $timeout, 'exception' => new \Exception()]
);
$timeout = (int)$this->configService->defaults[ConfigService::LOCK_TIMEOUT];
}

try {
$locks = $this->locksRequest->getLocksOlderThan($timeout);
} catch (Exception $e) {
$this->logger->warning('Failed to get locks older then timeout', ['exception' => $e]);
return [];
}

Expand Down Expand Up @@ -398,7 +398,7 @@ function (FileLock $lock) {
}, $locks
);

$this->notice('removing locks', false, ['ids' => $ids]);
$this->logger->notice('removing locks', ['ids' => $ids]);

$this->locksRequest->removeIds($ids);
}
Expand Down
201 changes: 0 additions & 201 deletions lib/Tools/Traits/TLogger.php

This file was deleted.

Loading

0 comments on commit 7153e7b

Please sign in to comment.