Skip to content

Commit 7153e7b

Browse files
Merge pull request #533 from nextcloud/backport/497/stable28
[stable28] fix(timeout): compare creation time to now - timeout
2 parents 846bb47 + 0b09775 commit 7153e7b

File tree

5 files changed

+32
-216
lines changed

5 files changed

+32
-216
lines changed

lib/Controller/LockController.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
use OCA\FilesLock\Model\FileLock;
3939
use OCA\FilesLock\Service\FileService;
4040
use OCA\FilesLock\Service\LockService;
41-
use OCA\FilesLock\Tools\Traits\TLogger;
4241
use OCP\AppFramework\Http;
4342
use OCP\AppFramework\Http\DataResponse;
4443
use OCP\AppFramework\OCSController;
@@ -48,19 +47,21 @@
4847
use OCP\IL10N;
4948
use OCP\IRequest;
5049
use OCP\IUserSession;
50+
use Psr\Log\LoggerInterface;
5151

5252
/**
5353
* Class LockController
5454
*
5555
* @package OCA\FilesLock\Controller
5656
*/
5757
class LockController extends OCSController {
58-
use TLogger;
5958

6059
private int $ocsVersion;
60+
private LoggerInterface $logger;
6161

6262
public function __construct(
6363
IRequest $request,
64+
LoggerInterface $logger,
6465
private IUserSession $userSession,
6566
private FileService $fileService,
6667
private LockService $lockService,
@@ -82,6 +83,7 @@ public function __construct(
8283
$this->registerResponder('xml', function ($data) {
8384
return $this->buildOCSResponse('xml', $data);
8485
});
86+
$this->logger = $logger;
8587
}
8688

8789

@@ -197,7 +199,7 @@ protected function fail(
197199
);
198200

199201
if ($log) {
200-
$this->log(2, $status . ' - ' . json_encode($data));
202+
$this->logger->warning('[warning] ' . $status . ' - ' . json_encode($data));
201203
}
202204

203205
return new DataResponse($data, $status);

lib/Db/LocksRequest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use Exception;
3636
use OCA\FilesLock\Exceptions\LockNotFoundException;
3737
use OCA\FilesLock\Model\FileLock;
38+
use OCP\AppFramework\Utility\ITimeFactory;
3839
use OCP\DB\QueryBuilder\IQueryBuilder;
3940

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

152155
return $this->getLocksFromRequest($qb);
153156
}

lib/Service/LockService.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
use OCA\FilesLock\Exceptions\LockNotFoundException;
3737
use OCA\FilesLock\Exceptions\UnauthorizedUnlockException;
3838
use OCA\FilesLock\Model\FileLock;
39-
use OCA\FilesLock\Tools\Traits\TLogger;
4039
use OCA\FilesLock\Tools\Traits\TStringTools;
4140
use OCP\App\IAppManager;
4241
use OCP\EventDispatcher\IEventDispatcher;
@@ -49,14 +48,13 @@
4948
use OCP\IRequest;
5049
use OCP\IUserManager;
5150
use OCP\IUserSession;
51+
use Psr\Log\LoggerInterface;
5252

5353
class LockService {
5454
public const PREFIX = 'files_lock';
5555

5656

5757
use TStringTools;
58-
use TLogger;
59-
6058

6159
private IUserManager $userManager;
6260
private IL10N $l10n;
@@ -67,6 +65,7 @@ class LockService {
6765
private IEventDispatcher $eventDispatcher;
6866
private IUserSession $userSession;
6967
private IRequest $request;
68+
private LoggerInterface $logger;
7069

7170

7271
private array $locks = [];
@@ -86,6 +85,7 @@ public function __construct(
8685
IEventDispatcher $eventDispatcher,
8786
IUserSession $userSession,
8887
IRequest $request,
88+
LoggerInterface $logger,
8989
) {
9090
$this->l10n = $l10n;
9191
$this->userManager = $userManager;
@@ -96,8 +96,7 @@ public function __construct(
9696
$this->eventDispatcher = $eventDispatcher;
9797
$this->userSession = $userSession;
9898
$this->request = $request;
99-
100-
$this->setup('app', 'files_lock');
99+
$this->logger = $logger;
101100
}
102101

103102
/**
@@ -175,7 +174,7 @@ public function lock(LockContext $lockScope): FileLock {
175174
$known->setTimeout(
176175
$known->getETA() !== FileLock::ETA_INFINITE ? $known->getTimeout() - $known->getETA() + $this->configService->getTimeoutSeconds() : 0
177176
);
178-
$this->notice('extending existing lock', false, ['fileLock' => $known]);
177+
$this->logger->notice('extending existing lock', ['fileLock' => $known]);
179178
$this->locksRequest->update($known);
180179
$this->injectMetadata($known);
181180
return $known;
@@ -187,7 +186,7 @@ public function lock(LockContext $lockScope): FileLock {
187186
$lock = FileLock::fromLockScope($lockScope, $this->configService->getTimeoutSeconds());
188187
$this->generateToken($lock);
189188
$lock->setCreation(time());
190-
$this->notice('locking file', false, ['fileLock' => $lock]);
189+
$this->logger->notice('locking file', ['fileLock' => $lock]);
191190
$this->injectMetadata($lock);
192191
$this->locksRequest->save($lock);
193192
$this->propagateEtag($lockScope);
@@ -211,7 +210,7 @@ public function getAppName(string $appId): ?string {
211210
* @throws UnauthorizedUnlockException
212211
*/
213212
public function unlock(LockContext $lock, bool $force = false): FileLock {
214-
$this->notice('unlocking file', false, ['fileLock' => $lock]);
213+
$this->logger->notice('unlocking file', ['fileLock' => $lock]);
215214

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

311310
try {
312311
$locks = $this->locksRequest->getLocksOlderThan($timeout);
313312
} catch (Exception $e) {
313+
$this->logger->warning('Failed to get locks older then timeout', ['exception' => $e]);
314314
return [];
315315
}
316316

@@ -398,7 +398,7 @@ function (FileLock $lock) {
398398
}, $locks
399399
);
400400

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

403403
$this->locksRequest->removeIds($ids);
404404
}

lib/Tools/Traits/TLogger.php

Lines changed: 0 additions & 201 deletions
This file was deleted.

0 commit comments

Comments
 (0)