Skip to content

Commit f7aba72

Browse files
committed
point locking
1 parent 868188b commit f7aba72

File tree

4 files changed

+81
-19
lines changed

4 files changed

+81
-19
lines changed

lib/Db/PointRequest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@ public function deletePoint(string $pointId): void {
111111
}
112112

113113

114+
/**
115+
* @param RestoringPoint $point
116+
*/
117+
public function updateLock(RestoringPoint $point): void {
118+
$qb = $this->getPointUpdateSql();
119+
$qb->set('lock', $qb->createNamedParameter($point->getLock()));
120+
$qb->exprLimit('uid', $point->getId());
121+
$qb->execute();
122+
}
123+
124+
114125
/**
115126
* @param int $since
116127
* @param int $until

lib/Model/RestoringPoint.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class RestoringPoint implements IDeserializable, INC23QueryRow, ISignedModel, Js
6060
public const STATUS_COMPRESSED = 2;
6161
public const STATUS_ENCRYPTED = 4;
6262

63+
const LOCK_TIMEOUT = 3600 * 3;
64+
6365
public static $DEF_STATUS =
6466
[
6567
self::STATUS_PACKED => 'packed',
@@ -300,8 +302,8 @@ public function getLock(): int {
300302
/**
301303
* @return bool
302304
*/
303-
public function isLock(): bool {
304-
return ($this->lock > 0);
305+
public function isLocked(): bool {
306+
return ($this->getLock() < (time() - self::LOCK_TIMEOUT));
305307
}
306308

307309

@@ -376,7 +378,9 @@ public function getNCVersion(): string {
376378
return implode('.', $this->getNc());
377379
}
378380

379-
381+
/**
382+
* @return int
383+
*/
380384
public function getNCInt(): int {
381385
$nc = $this->getNc();
382386

@@ -460,19 +464,6 @@ public function getRootFolder(): ISimpleRoot {
460464
*/
461465
public function getRestoringData(): array {
462466
return $this->restoringData;
463-
// $options = $this->getOptions();
464-
// if (!$filtered || $options->getChunk() === '') {
465-
// return $this->chunks;
466-
// }
467-
//
468-
// $options = $this->getOptions();
469-
// foreach ($this->chunks as $chunk) {
470-
// if ($chunk->getName() === $options->getChunk()) {
471-
// return [$chunk];
472-
// }
473-
// }
474-
//
475-
// return [];
476467
}
477468

478469
/**

lib/Service/MetadataService.php

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131

3232
namespace OCA\Backup\Service;
3333

34-
use OCA\Backup\Exceptions\MetadataException;
34+
35+
use OCA\Backup\Db\PointRequest;
36+
use OCA\Backup\Exceptions\RestoringPointException;
37+
use OCA\Backup\Exceptions\RestoringPointNotFoundException;
3538
use OCA\Backup\Model\RestoringPoint;
3639
use OCP\Files\NotFoundException;
3740
use OCP\Files\NotPermittedException;
@@ -44,6 +47,13 @@
4447
class MetadataService {
4548
public const METADATA_FILE = 'restoring-point.data';
4649

50+
51+
const METADATA_FILE = 'restoring-point.data';
52+
53+
54+
/** @var PointRequest */
55+
private $pointRequest;
56+
4757
/** @var RemoteService */
4858
private $remoteService;
4959

@@ -54,13 +64,16 @@ class MetadataService {
5464
/**
5565
* MetadataService constructor.
5666
*
67+
* @param PointRequest $pointRequest
5768
* @param RemoteService $remoteService
5869
* @param ExternalFolderService $externalFolderService
5970
*/
6071
public function __construct(
72+
PointRequest $pointRequest,
6173
RemoteService $remoteService,
6274
ExternalFolderService $externalFolderService
6375
) {
76+
$this->pointRequest = $pointRequest;
6477
$this->remoteService = $remoteService;
6578
$this->externalFolderService = $externalFolderService;
6679
}
@@ -87,11 +100,55 @@ public function saveMetadata(RestoringPoint $point) {
87100

88101
/**
89102
* @param RestoringPoint $point
90-
*
91-
* @throws MetadataException
92103
*/
93104
public function globalUpdate(RestoringPoint $point) {
94105
$this->externalFolderService->updateMetadata($point);
95106
$this->remoteService->updateMetadata($point);
96107
}
108+
109+
110+
/**
111+
* @param RestoringPoint $point
112+
*/
113+
public function lock(RestoringPoint $point): void {
114+
$time = time();
115+
if ($point->getLock() > $time - 60) {
116+
return;
117+
}
118+
119+
$point->setLock($time);
120+
$this->pointRequest->updateLock($point);
121+
}
122+
123+
/**
124+
* @param RestoringPoint $point
125+
*/
126+
public function unlock(RestoringPoint $point): void {
127+
if ($point->getLock() === 0) {
128+
return;
129+
}
130+
131+
$point->setLock(0);
132+
$this->pointRequest->updateLock($point);
133+
}
134+
135+
/**
136+
* @param RestoringPoint $point
137+
*
138+
* @throws RestoringPointException
139+
*/
140+
public function isLock(RestoringPoint $point): void {
141+
try {
142+
$stored = $this->pointRequest->getById($point->getId());
143+
} catch (RestoringPointNotFoundException $e) {
144+
return;
145+
}
146+
147+
$point->setLock($stored->getLock());
148+
149+
if ($point->isLocked()) {
150+
throw new RestoringPointException('point is locked');
151+
}
152+
}
153+
97154
}

lib/Service/PackService.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use OCA\Backup\Exceptions\EncryptionKeyException;
4242
use OCA\Backup\Exceptions\RestoringChunkNotFoundException;
4343
use OCA\Backup\Exceptions\RestoringChunkPartNotFoundException;
44+
use OCA\Backup\Exceptions\RestoringPointException;
4445
use OCA\Backup\Exceptions\RestoringPointNotInitiatedException;
4546
use OCA\Backup\Exceptions\RestoringPointPackException;
4647
use OCA\Backup\Model\RestoringChunk;
@@ -132,6 +133,8 @@ public function packPoint(RestoringPoint $point, bool $force = false): void {
132133
throw new RestoringPointPackException('restoring point is already packed');
133134
}
134135

136+
$this->metadataService->isLock($point);
137+
135138
if ($point->isStatus(RestoringPoint::STATUS_ISSUE)) {
136139
if (!$force && $point->getNotes()->gInt('pack_date') > time() - 3600 * 6) {
137140
throw new RestoringPointPackException('restoring point already failed few hours ago');

0 commit comments

Comments
 (0)